1 /* 2 * Copyright (c) 2022-2025 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 "hichain_listener.h" 33 #include "i_dm_service_impl_ext_resident.h" 34 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 35 #include "dm_account_common_event.h" 36 #include "dm_datashare_common_event.h" 37 #include "dm_package_common_event.h" 38 #include "dm_screen_common_event.h" 39 #include "relationship_sync_mgr.h" 40 #include "local_service_info.h" 41 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) 42 #include "dm_publish_common_event.h" 43 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI 44 #endif 45 46 namespace OHOS { 47 namespace DistributedHardware { 48 class DeviceManagerService { 49 DM_DECLARE_SINGLE_INSTANCE_BASE(DeviceManagerService); 50 public: DeviceManagerService()51 DeviceManagerService() {} 52 53 ~DeviceManagerService(); 54 55 int32_t Init(); 56 57 int32_t InitSoftbusListener(); 58 59 void InitHichainListener(); 60 61 void RegisterCallerAppId(const std::string &pkgName); 62 63 void UnRegisterCallerAppId(const std::string &pkgName); 64 65 void UninitSoftbusListener(); 66 67 int32_t InitDMServiceListener(); 68 69 void UninitDMServiceListener(); 70 71 int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, 72 std::vector<DmDeviceInfo> &deviceList); 73 74 int32_t ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp); 75 76 int32_t GetLocalDeviceInfo(DmDeviceInfo &info); 77 78 int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info); 79 80 int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid); 81 82 int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid); 83 84 int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo); 85 86 int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId); 87 88 int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 89 const std::string &extra); 90 91 int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId); 92 93 int32_t StopAuthenticateDevice(const std::string &pkgName); 94 95 int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 96 const std::string &bindParam); 97 98 int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash); 99 100 int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra); 101 102 int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms); 103 104 void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo); 105 106 int OnSessionOpened(int sessionId, int result); 107 108 void OnSessionClosed(int sessionId); 109 110 void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); 111 112 int OnPinHolderSessionOpened(int sessionId, int result); 113 114 void OnPinHolderSessionClosed(int sessionId); 115 116 void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen); 117 118 int32_t DmHiDumper(const std::vector<std::string>& args, std::string &result); 119 120 int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); 121 122 int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); 123 124 int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); 125 126 int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr); 127 128 int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr, 129 std::string &returnJsonStr); 130 131 int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr, 132 std::string &returnJsonStr); 133 134 int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr, 135 std::string &returnJsonStr); 136 137 int32_t RegisterCredentialCallback(const std::string &pkgName); 138 139 int32_t UnRegisterCredentialCallback(const std::string &pkgName); 140 141 int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event); 142 143 int32_t CheckApiPermission(int32_t permissionLevel); 144 145 void LoadHardwareFwkService(); 146 147 int32_t GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId, std::string &uuid); 148 149 int32_t GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid, const std::string &appId, 150 std::string &encryptedUuid); 151 152 int32_t GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId, int32_t &networkType); 153 int32_t RegisterUiStateCallback(const std::string &pkgName); 154 int32_t UnRegisterUiStateCallback(const std::string &pkgName); 155 int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode); 156 int32_t ExportAuthCode(std::string &authCode); 157 158 int32_t RegisterPinHolderCallback(const std::string &pkgName); 159 int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId, 160 DmPinType pinType, const std::string &payload); 161 int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, 162 DmPinType pinType, const std::string &payload); 163 164 // The following interfaces are provided since OpenHarmony 4.1 Version. 165 int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, 166 const std::map<std::string, std::string> &filterOptions); 167 168 int32_t StopDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam); 169 170 int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, 171 const std::map<std::string, std::string> &filterOptions); 172 173 int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam); 174 175 int32_t StartAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam); 176 177 int32_t StopAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam); 178 179 int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId, 180 const std::map<std::string, std::string> &bindParam); 181 182 int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, 183 const std::map<std::string, std::string> &unbindParam); 184 185 int32_t DpAclAdd(const std::string &udid); 186 187 int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType); 188 int32_t IsSameAccount(const std::string &networkId); 189 int32_t InitAccountInfo(); 190 int32_t InitScreenLockEvent(); 191 bool CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee); 192 bool CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); 193 void HandleDeviceNotTrust(const std::string &msg); 194 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 195 void HandleDeviceTrustedChange(const std::string &msg); 196 void HandleUserIdCheckSumChange(const std::string &msg); 197 void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); 198 void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid, 199 const std::vector<std::string> &acceptEventUdids); 200 #endif 201 int32_t SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy); 202 void ClearDiscoveryCache(const ProcessInfo &processInfo); 203 void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo); 204 int32_t GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId, 205 int32_t &screenStatus); 206 void SubscribePackageCommonEvent(); 207 int32_t GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid, std::string &networkId); 208 void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode); 209 void ProcessSyncUserIds(const std::vector<uint32_t> &foregroundUserIds, 210 const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid); 211 int32_t SetLocalDisplayNameToSoftbus(const std::string &displayName); 212 void RemoveNotifyRecord(const ProcessInfo &processInfo); 213 int32_t RegDevStateCallbackToService(const std::string &pkgName); 214 int32_t GetTrustedDeviceList(const std::string &pkgName, std::vector<DmDeviceInfo> &deviceList); 215 void HandleDeviceUnBind(const char *peerUdid, const GroupInformation &groupInfo); 216 int32_t GetAnonyLocalUdid(const std::string &pkgName, std::string &anonyUdid); 217 int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, 218 std::vector<DmDeviceInfo> &deviceList); 219 int32_t RegisterAuthenticationType(const std::string &pkgName, 220 const std::map<std::string, std::string> &authParam); 221 int32_t GetDeviceProfileInfoList(const std::string &pkgName, DmDeviceProfileInfoFilterOptions &filterOptions); 222 int32_t GetDeviceIconInfo(const std::string &pkgName, DmDeviceIconInfoFilterOptions &filterOptions); 223 int32_t PutDeviceProfileInfoList(const std::string &pkgName, 224 std::vector<DmDeviceProfileInfo> &deviceProfileInfoList); 225 int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, std::string &displayName); 226 int32_t SetLocalDeviceName(const std::string &pkgName, const std::string &deviceName); 227 int32_t SetRemoteDeviceName(const std::string &pkgName, const std::string &deviceId, const std::string &deviceName); 228 std::vector<std::string> GetDeviceNamePrefixs(); 229 int32_t RestoreLocalDeviceName(const std::string &pkgName); 230 int32_t RegisterLocalServiceInfo(const DMLocalServiceInfo &serviceInfo); 231 int32_t UnRegisterLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType); 232 int32_t UpdateLocalServiceInfo(const DMLocalServiceInfo &serviceInfo); 233 int32_t GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string &bundleName, int32_t pinExchangeType, 234 DMLocalServiceInfo &serviceInfo); 235 void ClearPulishIdCache(const std::string &pkgName); 236 bool IsPC(); 237 int32_t GetDeviceNetworkIdList(const std::string &pkgName, const NetworkIdQueryFilter &queryFilter, 238 std::vector<std::string> &networkIds); 239 void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); 240 241 private: 242 bool IsDMServiceImplReady(); 243 bool IsDMImplSoLoaded(); 244 bool IsDMServiceAdapterSoLoaded(); 245 bool IsDMServiceAdapterResidentLoad(); 246 void UnloadDMServiceImplSo(); 247 void UnloadDMServiceAdapterResident(); 248 void SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId, 249 int32_t bindLevel); 250 void SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId, 251 int32_t bindLevel, uint64_t peerTokenId); 252 void SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId); 253 void SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId); 254 void SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, 255 uint64_t tokenId, uint64_t peerTokenId); 256 void SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId); 257 void SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids, const std::string &accountId, 258 const std::string &accountName, int32_t userId); 259 /** 260 * @brief send local foreground or background userids by broadcast 261 * 262 * @param peerUdids the broadcast target device udid list 263 * @param foregroundUserIds local foreground userids 264 * @param backgroundUserIds local background userids 265 */ 266 void SendUserIdsBroadCast(const std::vector<std::string> &peerUdids, 267 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds, 268 bool isNeedResponse); 269 void SendUserRemovedBroadCast(const std::vector<std::string> &peerUdids, int32_t userId); 270 /** 271 * @brief parse dsoftbus checksum msg 272 * 273 * @param msg checksum msg 274 * @param networkId remote device networkid 275 * @param discoveryType remote device link type, wifi or ble/br 276 * @return int32_t 0 for success 277 */ 278 int32_t ParseCheckSumMsg(const std::string &msg, std::string &networkId, uint32_t &discoveryType, bool &isChange); 279 void ProcessCheckSumByWifi(std::string networkId, std::vector<int32_t> foregroundUserIds, 280 std::vector<int32_t> backgroundUserIds); 281 void ProcessCheckSumByBT(std::string networkId, std::vector<int32_t> foregroundUserIds, 282 std::vector<int32_t> backgroundUserIds); 283 void AddHmlInfoToBindParam(int32_t actionId, std::string &bindParam); 284 285 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 286 void SubscribeAccountCommonEvent(); 287 void AccountCommonEventCallback(const std::string commonEventType, int32_t currentUserId, int32_t beforeUserId); 288 void SubscribeScreenLockEvent(); 289 void ScreenCommonEventCallback(std::string commonEventType); 290 void ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo); 291 int32_t ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &anoyDeviceId); 292 int32_t GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash); 293 void HandleAccountLogout(int32_t userId, const std::string &accountId, const std::string &accountName); 294 void HandleUserRemoved(int32_t removedUserId); 295 /** 296 * @brief process the user switch 297 * 298 * @param currentUserId the user id which switched to foreground. 299 * @param beforeUserId the user id which switched to backend. 300 */ 301 void HandleUserSwitched(int32_t curUserId, int32_t preUserId); 302 void HandleUserIdsBroadCast(const std::vector<UserIdInfo> &remoteUserIdInfos, 303 const std::string &remoteUdid, bool isNeedResponse); 304 void NotifyRemoteLocalUserSwitch(int32_t curUserId, int32_t preUserId, const std::vector<std::string> &peerUdids, 305 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds); 306 void NotifyRemoteLocalUserSwitchByWifi(int32_t curUserId, int32_t preUserId, 307 const std::map<std::string, std::string> &wifiDevices, const std::vector<int32_t> &foregroundUserIds, 308 const std::vector<int32_t> &backgroundUserIds); 309 int32_t SendUserIdsByWifi(const std::string &networkId, const std::vector<int32_t> &foregroundUserIds, 310 const std::vector<int32_t> &backgroundUserIds); 311 void HandleUserSwitchTimeout(int32_t curUserId, int32_t preUserId, const std::string &udid); 312 bool InitDPLocalServiceInfo(const DMLocalServiceInfo &serviceInfo, 313 DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceItem); 314 void InitServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceItem, 315 DMLocalServiceInfo &serviceInfo); 316 void InitServiceInfos(const std::vector<DistributedDeviceProfile::LocalServiceInfo> &dpLocalServiceItems, 317 std::vector<DMLocalServiceInfo> &serviceInfos); 318 void HandleUserSwitched(); 319 void NotifyRemoteLocalUserSwitch(const std::string &localUdid, const std::vector<std::string> &peerUdids, 320 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds); 321 void NotifyRemoteLocalUserSwitchByWifi(const std::string &localUdid, 322 const std::map<std::string, std::string> &wifiDevices, const std::vector<int32_t> &foregroundUserIds, 323 const std::vector<int32_t> &backgroundUserIds); 324 void HandleUserSwitchTimeout(const std::string &localUdid, const std::vector<int32_t> &foregroundUserIds, 325 const std::vector<int32_t> &backgroundUserIds, const std::string &udid); 326 void UpdateAclAndDeleteGroup(const std::string &localUdid, const std::vector<std::string> &deviceVec, 327 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds); 328 void HandleUserSwitchedEvent(int32_t currentUserId, int32_t beforeUserId); 329 void HandleUserStopEvent(int32_t stopUserId); 330 void DivideNotifyMethod(const std::vector<std::string> &peerUdids, std::vector<std::string> &bleUdids, 331 std::map<std::string, std::string> &wifiDevices); 332 void NotifyRemoteLocalUserStop(const std::string &localUdid, 333 const std::vector<std::string> &peerUdids, int32_t stopUserId); 334 void SendUserStopBroadCast(const std::vector<std::string> &peerUdids, int32_t stopUserId); 335 void HandleUserStopBroadCast(int32_t stopUserId, const std::string &remoteUdid); 336 void NotifyRemoteLocalUserStopByWifi(const std::string &localUdid, 337 const std::map<std::string, std::string> &wifiDevices, int32_t stopUserId); 338 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) 339 void SubscribePublishCommonEvent(); 340 void QueryDependsSwitchState(); 341 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI 342 void SubscribeDataShareCommonEvent(); 343 #endif 344 void CheckRegisterInfoWithWise(int32_t curUserId); 345 void NotifyRemoteLocalLogout(const std::vector<std::string> &peerUdids, 346 const std::string &accountIdHash, const std::string &accountName, int32_t userId); 347 348 private: 349 bool isImplsoLoaded_ = false; 350 bool isAdapterResidentSoLoaded_ = false; 351 void *residentSoHandle_ = nullptr; 352 std::mutex isImplLoadLock_; 353 std::mutex isAdapterResidentLoadLock_; 354 std::mutex hichainListenerLock_; 355 std::shared_ptr<AdvertiseManager> advertiseMgr_; 356 std::shared_ptr<DiscoveryManager> discoveryMgr_; 357 std::shared_ptr<SoftbusListener> softbusListener_; 358 std::shared_ptr<HichainListener> hichainListener_; 359 std::shared_ptr<DeviceManagerServiceListener> listener_; 360 std::shared_ptr<IDeviceManagerServiceImpl> dmServiceImpl_; 361 std::shared_ptr<IDMServiceImplExtResident> dmServiceImplExtResident_; 362 std::string localDeviceId_; 363 std::shared_ptr<PinHolder> pinHolder_; 364 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 365 std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager_; 366 std::shared_ptr<DmPackageCommonEventManager> packageCommonEventManager_; 367 std::shared_ptr<DmScreenCommonEventManager> screenCommonEventManager_; 368 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) 369 std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager_; 370 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI 371 std::shared_ptr<DmDataShareCommonEventManager> dataShareCommonEventManager_; 372 #endif 373 std::string localNetWorkId_ = ""; 374 std::shared_ptr<DmTimer> timer_; 375 }; 376 } // namespace DistributedHardware 377 } // namespace OHOS 378 #endif // OHOS_DM_SERVICE_H 379