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_H
17 #define OHOS_DM_SERVICE_H
18 
19 #include <string>
20 #include <memory>
21 #if defined(__LITEOS_M__)
22 #include "dm_mutex.h"
23 #else
24 #include <mutex>
25 #endif
26 
27 #include "advertise_manager.h"
28 #include "discovery_manager.h"
29 #include "pin_holder.h"
30 #include "device_manager_service_listener.h"
31 #include "idevice_manager_service_impl.h"
32 #include "i_dm_service_impl_ext.h"
33 #include "dm_single_instance.h"
34 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
35 #include "dm_account_common_event.h"
36 #include "dm_package_common_event.h"
37 #include "dm_screen_common_event.h"
38 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
39 #include "dm_publish_common_event.h"
40 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
41 #endif
42 
43 namespace OHOS {
44 namespace DistributedHardware {
45 class DeviceManagerService {
46 DM_DECLARE_SINGLE_INSTANCE_BASE(DeviceManagerService);
47 public:
DeviceManagerService()48     DeviceManagerService() {}
49 
50     ~DeviceManagerService();
51 
52     int32_t Init();
53 
54     int32_t InitSoftbusListener();
55 
56     void RegisterCallerAppId(const std::string &pkgName);
57 
58     void UnRegisterCallerAppId(const std::string &pkgName);
59 
60     void UninitSoftbusListener();
61 
62     int32_t InitDMServiceListener();
63 
64     void UninitDMServiceListener();
65 
66     int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
67                                  std::vector<DmDeviceInfo> &deviceList);
68 
69     int32_t ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp);
70 
71     int32_t GetLocalDeviceInfo(DmDeviceInfo &info);
72 
73     int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info);
74 
75     int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid);
76 
77     int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid);
78 
79     int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
80                                  const std::string &extra);
81 
82     int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeInfo,
83                                 const std::string &filterOptions);
84 
85     int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
86 
87     int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo);
88 
89     int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId);
90 
91     int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
92                                const std::string &extra);
93 
94     int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId);
95 
96     int32_t StopAuthenticateDevice(const std::string &pkgName);
97 
98     int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
99         const std::string &bindParam);
100 
101     int32_t UnBindDevice(const std::string &pkgName, const std::string &deviceId);
102 
103     int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string &params);
104 
105     void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo);
106 
107     int OnSessionOpened(int sessionId, int result);
108 
109     void OnSessionClosed(int sessionId);
110 
111     void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
112 
113     int OnPinHolderSessionOpened(int sessionId, int result);
114 
115     void OnPinHolderSessionClosed(int sessionId);
116 
117     void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen);
118 
119     int32_t DmHiDumper(const std::vector<std::string>& args, std::string &result);
120 
121     int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
122 
123     int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
124 
125     int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
126 
127     int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr);
128 
129     int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
130         std::string &returnJsonStr);
131 
132     int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
133         std::string &returnJsonStr);
134 
135     int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
136         std::string &returnJsonStr);
137 
138     int32_t RegisterCredentialCallback(const std::string &pkgName);
139 
140     int32_t UnRegisterCredentialCallback(const std::string &pkgName);
141 
142     int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event);
143 
144     int32_t CheckApiPermission(int32_t permissionLevel);
145 
146     void LoadHardwareFwkService();
147 
148     int32_t GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId, std::string &uuid);
149 
150     int32_t GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid, const std::string &appId,
151         std::string &encryptedUuid);
152 
153     int32_t GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId, int32_t &networkType);
154     int32_t RegisterUiStateCallback(const std::string &pkgName);
155     int32_t UnRegisterUiStateCallback(const std::string &pkgName);
156     int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode);
157     int32_t ExportAuthCode(std::string &authCode);
158 
159     int32_t RegisterPinHolderCallback(const std::string &pkgName);
160     int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
161         DmPinType pinType, const std::string &payload);
162     int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
163         DmPinType pinType, const std::string &payload);
164 
165     // The following interfaces are provided since OpenHarmony 4.1 Version.
166     int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
167         const std::map<std::string, std::string> &filterOptions);
168 
169     int32_t StopDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam);
170 
171     int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
172         const std::map<std::string, std::string> &filterOptions);
173 
174     int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam);
175 
176     int32_t StartAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
177 
178     int32_t StopAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
179 
180     int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
181         const std::map<std::string, std::string> &bindParam);
182 
183     int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
184         const std::map<std::string, std::string> &unbindParam);
185 
186     void OnUnbindSessionOpened(int32_t socket, PeerSocketInfo info);
187     void OnUnbindSessionCloseed(int32_t socket);
188     void OnUnbindBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
189 
190     int32_t DpAclAdd(const std::string &udid);
191 
192     int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType);
193     int32_t IsSameAccount(const std::string &networkId);
194     int32_t InitAccountInfo();
195     int32_t InitScreenLockEvent();
196     bool CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee);
197     bool CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee);
198     void HandleDeviceNotTrust(const std::string &msg);
199 
200     int32_t SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy);
201 
202     void SubscribePackageCommonEvent();
203     int32_t GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid, std::string &networkId);
204     void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo);
205     int32_t GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
206         int32_t &screenStatus);
207     void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode);
208 private:
209     bool IsDMServiceImplReady();
210     bool IsDMServiceAdapterLoad();
211     bool IsDMImplSoLoaded();
212     void UnloadDMServiceImplSo();
213     void UnloadDMServiceAdapter();
214 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
215     void SubscribeAccountCommonEvent();
216     void AccountCommonEventCallback(int32_t userId, std::string commonEventType);
217     void SubscribeScreenLockEvent();
218     void ScreenCommonEventCallback(std::string commonEventType);
219     void ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo);
220     int32_t ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &anoyDeviceId);
221     int32_t GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash);
222     void HandleAccountLogout(int32_t userId, const std::string &accountId);
223     void HandleUserRemoved(int32_t preUserId);
224 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
225     void SubscribePublishCommonEvent();
226     void QueryDependsSwitchState();
227 #endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
228 #endif
229 
230 private:
231     bool isImplsoLoaded_ = false;
232     bool isAdapterSoLoaded_ = false;
233     std::mutex isImplLoadLock_;
234     std::mutex isAdapterLoadLock_;
235     std::shared_ptr<AdvertiseManager> advertiseMgr_;
236     std::shared_ptr<DiscoveryManager> discoveryMgr_;
237     std::shared_ptr<SoftbusListener> softbusListener_;
238     std::shared_ptr<DeviceManagerServiceListener> listener_;
239     std::shared_ptr<IDeviceManagerServiceImpl> dmServiceImpl_;
240     std::shared_ptr<IDMServiceImplExt> dmServiceImplExt_;
241     std::string localDeviceId_;
242     std::shared_ptr<PinHolder> pinHolder_;
243 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
244     std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager_;
245     std::shared_ptr<DmPackageCommonEventManager> packageCommonEventManager_;
246     std::shared_ptr<DmScreenCommonEventManager> screenCommonEventManager_;
247 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
248     std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager_;
249 #endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
250 #endif
251 };
252 } // namespace DistributedHardware
253 } // namespace OHOS
254 #endif // OHOS_DM_SERVICE_H