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