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_SOFTBUS_CONNECTOR_H 17 #define OHOS_DM_SOFTBUS_CONNECTOR_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include <vector> 25 26 #include "softbus_bus_center.h" 27 #include "dm_device_info.h" 28 #include "dm_publish_info.h" 29 #include "dm_subscribe_info.h" 30 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 31 #include "deviceprofile_connector.h" 32 #include "softbus_session.h" 33 #endif 34 #include "softbus_connector_callback.h" 35 #include "softbus_state_callback.h" 36 #include "hichain_auth_connector.h" 37 38 namespace OHOS { 39 namespace DistributedHardware { 40 class SoftbusConnector { 41 public: 42 /** 43 * @tc.name: SoftbusConnector::OnSoftbusJoinLNNResult 44 * @tc.desc: OnSoftbus JoinLNN Result of the SoftbusConnector 45 * @tc.type: FUNC 46 */ 47 static void OnSoftbusJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t result); 48 49 /** 50 * @tc.name: SoftbusConnector::GetConnectAddr 51 * @tc.desc: Get Connect Addr of the SoftbusConnector 52 * @tc.type: FUNC 53 */ 54 static std::shared_ptr<ConnectionAddr> GetConnectAddr(const std::string &deviceId, std::string &connectAddr); 55 56 /** 57 * @tc.name: SoftbusConnector::GetUdidByNetworkId 58 * @tc.desc: Get Udid By NetworkId of the SoftbusConnector 59 * @tc.type: FUNC 60 */ 61 static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); 62 63 /** 64 * @tc.name: SoftbusConnector::GetUuidByNetworkId 65 * @tc.desc: Get Uuid By NetworkId of the SoftbusConnector 66 * @tc.type: FUNC 67 */ 68 static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); 69 70 /** 71 * @tc.name: SoftbusConnector::GetDeviceUdidByUdidHash 72 * @tc.desc: Get Udid By DeviceId of the SoftbusConnector 73 * @tc.type: FUNC 74 */ 75 static std::string GetDeviceUdidByUdidHash(const std::string &udidHash); 76 77 /** 78 * @tc.name: SoftbusConnector::JoinLnn 79 * @tc.desc: Add the current device to the LNN where a specified device resides 80 * @tc.type: FUNC 81 */ 82 static void JoinLnn(const std::string &deviceId, bool isForceJoin = false); 83 84 /** 85 * @tc.name: SoftbusConnector::JoinLnnByHml 86 * @tc.desc: join lnn by session id 87 * @tc.type: FUNC 88 */ 89 static void JoinLnnByHml(int32_t sessionId, int32_t sessionKeyId, int32_t remoteSessionKeyId, 90 bool isForceJoin = false); 91 92 static void JoinLnn(const std::string &deviceId, const std::string &remoteUdidHash); 93 94 static void JoinLNNBySkId(int32_t sessionId, int32_t sessionKeyId, int32_t remoteSessionKeyId, 95 std::string udid, std::string udidHash, bool isForceJoin = false); 96 97 /** 98 * @tc.name: SoftbusConnector::RegisterConnectorCallback 99 * @tc.desc: RegisterConnectorCallback of the Softbus Connector 100 * @tc.type: FUNC 101 */ 102 int32_t RegisterConnectorCallback(std::shared_ptr<ISoftbusConnectorCallback> callback); 103 104 /** 105 * @tc.name: SoftbusConnector::UnRegisterConnectorCallback 106 * @tc.desc: UnRegister ConnectorCallback of the Softbus Connector 107 * @tc.type: FUNC 108 */ 109 int32_t UnRegisterConnectorCallback(); 110 111 public: 112 SoftbusConnector(); 113 ~SoftbusConnector(); 114 int32_t RegisterSoftbusStateCallback(const std::shared_ptr<ISoftbusStateCallback> callback); 115 int32_t UnRegisterSoftbusStateCallback(); 116 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 117 std::shared_ptr<SoftbusSession> GetSoftbusSession(); 118 void SortAclListDesc(const std::vector<AclHashItem> &remoteAllAclList, std::vector<std::string> &aclVerDesc, 119 std::map<std::string, AclHashItem> &remoteAllAclMap); 120 std::string MatchTargetVersion(const std::string &localVersion, const std::vector<std::string> &remoteVerDesc); 121 #endif 122 bool HaveDeviceInMap(std::string deviceId); 123 std::string GetDeviceUdidHashByUdid(const std::string &udid); 124 void EraseUdidFromMap(const std::string &udid); 125 std::string GetLocalDeviceName(); 126 std::string GetLocalDeviceNetworkId(); 127 int32_t GetLocalDeviceTypeId(); 128 int32_t AddMemberToDiscoverMap(const std::string &deviceId, std::shared_ptr<DeviceInfo> deviceInfo); 129 std::string GetNetworkIdByDeviceId(const std::string &deviceId); 130 void HandleDeviceOnline(std::string deviceId, int32_t authForm); 131 void HandleDeviceOffline(std::string deviceId); 132 void SetProcessInfo(ProcessInfo processInfo); 133 bool CheckIsNeedJoinLnn(const std::string &udid, const std::string &deviceId); 134 bool CheckIsOnline(const std::string &targetDeviceId); 135 bool CheckIsOnline(const std::string &targetDeviceIdHash, bool isHash); 136 void SetProcessInfoVec(std::vector<ProcessInfo> processInfoVec); 137 std::vector<ProcessInfo> GetProcessInfo(); 138 void ClearProcessInfo(); 139 void SetChangeProcessInfo(ProcessInfo processInfo); 140 std::vector<ProcessInfo> GetChangeProcessInfo(); 141 void ClearChangeProcessInfo(); 142 DmDeviceInfo GetDeviceInfoByDeviceId(const std::string &deviceId); 143 void DeleteOffLineTimer(std::string &udidHash); 144 void SyncAclList(int32_t userId, std::string credId, int32_t sessionKeyId, int32_t aclId); 145 int32_t SyncLocalAclListProcess(const DevUserInfo &localDevUserInfo, 146 const DevUserInfo &remoteDevUserInfo, std::string remoteAclList); 147 int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, 148 const DevUserInfo &remoteDevUserInfo, std::string &aclList); 149 150 private: 151 static std::shared_ptr<ConnectionAddr> SetAddrAndJson(const ConnectionAddr *addr, 152 JsonObject &jsonPara, std::string &connectAddr); 153 static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo); 154 static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceBasicInfo &dmDeviceBasicInfo); 155 static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type); 156 static void ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo, DmDeviceInfo &dmDeviceInfo); 157 static std::shared_ptr<DeviceInfo> GetDeviceInfoFromMap(const std::string &deviceId); 158 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 159 int32_t ParaseAclChecksumList(const std::string &jsonString, std::vector<AclHashItem> &remoteAllAclList); 160 int32_t SyncLocalAclList5_1_0(const std::string localUdid, const std::string remoteUdid, 161 DistributedDeviceProfile::AccessControlProfile &localAcl, std::vector<std::string> &acLStrList); 162 int32_t GetLocalVersion(const std::string localUdid, const std::string remoteUdid, 163 std::string &localVersion, DistributedDeviceProfile::AccessControlProfile &localAcl); 164 #endif 165 private: 166 static std::string remoteUdidHash_; 167 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 168 std::shared_ptr<SoftbusSession> softbusSession_; 169 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_; 170 #endif 171 static std::map<std::string, std::shared_ptr<DeviceInfo>> discoveryDeviceInfoMap_; 172 std::shared_ptr<ISoftbusStateCallback> deviceStateManagerCallback_; 173 static std::unordered_map<std::string, std::string> deviceUdidMap_; 174 static std::vector<ProcessInfo> processInfoVec_; 175 static std::vector<ProcessInfo> processChangeInfoVec_; 176 static std::mutex discoveryDeviceInfoMutex_; 177 static std::mutex deviceUdidLocks_; 178 static std::mutex processInfoVecMutex_; 179 static std::mutex processChangeInfoVecMutex_; 180 static std::shared_ptr<ISoftbusConnectorCallback> connectorCallback_; 181 }; 182 } // namespace DistributedHardware 183 } // namespace OHOS 184 #endif // OHOS_DM_SOFTBUS_CONNECTOR_H 185