1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_DM_SERVICE_IMPL_H
17 #define OHOS_DM_SERVICE_IMPL_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "access_control_profile.h"
23 #include "dm_ability_manager.h"
24 #include "dm_auth_manager.h"
25 #include "dm_common_event_manager.h"
26 #include "dm_credential_manager.h"
27 #include "dm_device_info.h"
28 #include "dm_device_state_manager.h"
29 #include "dm_discovery_manager.h"
30 #include "dm_publish_manager.h"
31 #include "idevice_manager_service_impl.h"
32 #include "dm_single_instance.h"
33 #include "softbus_connector.h"
34 #include "mine_hichain_connector.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 class DeviceManagerServiceImpl : public IDeviceManagerServiceImpl {
39 public:
40     DeviceManagerServiceImpl();
41     virtual ~DeviceManagerServiceImpl();
42 
43     int32_t Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener);
44 
45     void Release();
46 
47     int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
48                                  const std::string &extra);
49 
50     int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
51                                  const std::string &filterOptions);
52 
53     int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
54 
55     int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo);
56 
57     int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId);
58 
59     int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel);
60 
61     int32_t UnBindDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel);
62 
63     int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string &params);
64 
65     void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo);
66 
67     int OnSessionOpened(int sessionId, int result);
68 
69     void OnSessionClosed(int sessionId);
70 
71     void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
72 
73     int OnPinHolderSessionOpened(int sessionId, int result);
74 
75     void OnPinHolderSessionClosed(int sessionId);
76 
77     void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen);
78 
79     int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
80 
81     int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
82 
83     int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
84 
85     int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr);
86 
87     int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
88         std::string &returnJsonStr);
89 
90     int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
91         std::string &returnJsonStr);
92 
93     int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
94         std::string &returnJsonStr);
95 
96     int32_t RegisterCredentialCallback(const std::string &pkgName);
97 
98     int32_t UnRegisterCredentialCallback(const std::string &pkgName);
99 
100     int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event);
101 
102     int32_t GetGroupType(std::vector<DmDeviceInfo> &deviceList);
103 
104     int32_t GetUdidHashByNetWorkId(const char *networkId, std::string &deviceId);
105 
106     void LoadHardwareFwkService();
107 
108     int32_t RegisterUiStateCallback(const std::string &pkgName);
109 
110     int32_t UnRegisterUiStateCallback(const std::string &pkgName);
111 
112     int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode);
113 
114     int32_t ExportAuthCode(std::string &authCode);
115 
116     int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
117         const std::map<std::string, std::string> &bindParam);
118 
119     std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceIdList(std::string pkgname);
120 
121     int32_t DpAclAdd(const std::string &udid);
122     int32_t IsSameAccount(const std::string &udid);
123     void ScreenCommonEventCallback(std::string commonEventType);
124     int32_t CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
125         const DmAccessCallee &callee, const std::string &sinkUdid);
126     int32_t CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
127         const DmAccessCallee &callee, const std::string &sinkUdid);
128     void HandleDeviceNotTrust(const std::string &udid);
129     int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid,
130         const std::string &udid, uint64_t &tokenId);
131     void HandleIdentAccountLogout(const std::string &udid, int32_t userId, const std::string &accountId);
132     void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo);
133     int32_t StopAuthenticateDevice(const std::string &pkgName);
134     void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode);
135     int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId);
136 
137 private:
138     int32_t PraseNotifyEventJson(const std::string &event, nlohmann::json &jsonObject);
139     std::string GetUdidHashByNetworkId(const std::string &networkId);
140     void HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo);
141     void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo);
142     void PutIdenticalAccountToAcl(std::string requestDeviceId, std::string trustDeviceId);
143     std::map<std::string, int32_t> GetDeviceIdAndBindType(int32_t userId, const std::string &accountId);
144     void HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash,
145         const std::string &remoteUdid);
146     void HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid);
147     void HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId);
148     void HandleUserRemoved(int32_t preUserId);
149     DmAuthForm ConvertBindTypeToAuthForm(int32_t bindType);
150     void HandleUserSwitched(int32_t switchUserId);
151 
152 private:
153     std::shared_ptr<DmAuthManager> authMgr_;
154     std::shared_ptr<DmDeviceStateManager> deviceStateMgr_;
155     std::shared_ptr<DmDiscoveryManager> discoveryMgr_;
156     std::shared_ptr<DmPublishManager> publishMgr_;
157     std::shared_ptr<SoftbusConnector> softbusConnector_;
158     std::shared_ptr<DmAbilityManager> abilityMgr_;
159     std::shared_ptr<HiChainConnector> hiChainConnector_;
160     std::shared_ptr<MineHiChainConnector> mineHiChainConnector_;
161     std::shared_ptr<DmCredentialManager> credentialMgr_;
162     std::shared_ptr<DmCommonEventManager> commonEventManager_;
163     std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_;
164     std::shared_ptr<IDeviceManagerServiceListener> listener_;
165     std::atomic<bool> isCredentialType_ = false;
166 };
167 
168 using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void);
169 } // namespace DistributedHardware
170 } // namespace OHOS
171 #endif // OHOS_DM_SERVICE_IMPL_H
172