1 /* 2 * Copyright (c) 2022-2023 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 #include "softbus_discovery_callback.h" 31 #include "softbus_publish_callback.h" 32 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 33 #include "softbus_session.h" 34 #endif 35 #include "softbus_state_callback.h" 36 37 namespace OHOS { 38 namespace DistributedHardware { 39 class SoftbusConnector { 40 public: 41 /** 42 * @tc.name: SoftbusConnector::OnSoftbusPublishResult 43 * @tc.desc: OnSoftbusPublishResult of the SoftbusConnector 44 * @tc.type: FUNC 45 */ 46 static void OnSoftbusPublishResult(int32_t publishId, PublishResult result); 47 48 /** 49 * @tc.name: SoftbusConnector::OnSoftbusDeviceFound 50 * @tc.desc: OnSoftbus DeviceFound of the SoftbusConnector 51 * @tc.type: FUNC 52 */ 53 static void OnSoftbusDeviceFound(const DeviceInfo *device); 54 55 /** 56 * @tc.name: SoftbusConnector::OnSoftbusDeviceDiscovery 57 * @tc.desc: OnSoftbus DeviceDiscovery of the SoftbusConnector 58 * @tc.type: FUNC 59 */ 60 static void OnSoftbusDeviceDiscovery(const DeviceInfo *device); 61 62 /** 63 * @tc.name: SoftbusConnector::OnSoftbusDiscoveryResult 64 * @tc.desc: OnSoftbus Discovery Result of the SoftbusConnector 65 * @tc.type: FUNC 66 */ 67 static void OnSoftbusDiscoveryResult(int subscribeId, RefreshResult result); 68 69 /** 70 * @tc.name: SoftbusConnector::OnSoftbusJoinLNNResult 71 * @tc.desc: OnSoftbus JoinLNN Result of the SoftbusConnector 72 * @tc.type: FUNC 73 */ 74 static void OnSoftbusJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t result); 75 76 /** 77 * @tc.name: SoftbusConnector::OnParameterChgCallback 78 * @tc.desc: OnParameter Chg Callback of the SoftbusConnector 79 * @tc.type: FUNC 80 */ 81 static void OnParameterChgCallback(const char *key, const char *value, void *context); 82 83 /** 84 * @tc.name: SoftbusConnector::GetConnectAddr 85 * @tc.desc: Get Connect Addr of the SoftbusConnector 86 * @tc.type: FUNC 87 */ 88 static ConnectionAddr *GetConnectAddr(const std::string &deviceId, std::string &connectAddr); 89 90 /** 91 * @tc.name: SoftbusConnector::GetUdidByNetworkId 92 * @tc.desc: Get Udid By NetworkId of the SoftbusConnector 93 * @tc.type: FUNC 94 */ 95 static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); 96 97 /** 98 * @tc.name: SoftbusConnector::GetUuidByNetworkId 99 * @tc.desc: Get Uuid By NetworkId of the SoftbusConnector 100 * @tc.type: FUNC 101 */ 102 static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); 103 104 /** 105 * @tc.name: SoftbusConnector::GetDeviceUdidByUdidHash 106 * @tc.desc: Get Udid By DeviceId of the SoftbusConnector 107 * @tc.type: FUNC 108 */ 109 static std::string GetDeviceUdidByUdidHash(const std::string &udidHash); 110 111 /** 112 * @tc.name: SoftbusConnector::JoinLnn 113 * @tc.desc: Add the current device to the LNN where a specified device resides 114 * @tc.type: FUNC 115 */ 116 static void JoinLnn(const std::string &deviceId); 117 public: 118 SoftbusConnector(); 119 ~SoftbusConnector(); 120 int32_t RegisterSoftbusDiscoveryCallback(const std::string &pkgName, 121 const std::shared_ptr<ISoftbusDiscoveryCallback> callback); 122 int32_t UnRegisterSoftbusDiscoveryCallback(const std::string &pkgName); 123 int32_t RegisterSoftbusPublishCallback(const std::string &pkgName, 124 const std::shared_ptr<ISoftbusPublishCallback> callback); 125 int32_t UnRegisterSoftbusPublishCallback(const std::string &pkgName); 126 int32_t RegisterSoftbusStateCallback(const std::shared_ptr<ISoftbusStateCallback> callback); 127 int32_t UnRegisterSoftbusStateCallback(); 128 int32_t PublishDiscovery(const DmPublishInfo &dmPublishInfo); 129 int32_t UnPublishDiscovery(int32_t publishId); 130 int32_t StartDiscovery(const DmSubscribeInfo &dmSubscribeInfo); 131 int32_t StartDiscovery(const uint16_t subscribeId); 132 int32_t StopDiscovery(uint16_t subscribeId); 133 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 134 std::shared_ptr<SoftbusSession> GetSoftbusSession(); 135 #endif 136 bool HaveDeviceInMap(std::string deviceId); 137 std::string GetDeviceUdidHashByUdid(const std::string &udid); 138 void EraseUdidFromMap(const std::string &udid); 139 std::string GetLocalDeviceName(); 140 std::string GetLocalDeviceNetworkId(); 141 int32_t GetLocalDeviceTypeId(); 142 int32_t AddMemberToDiscoverMap(const std::string &deviceId, std::shared_ptr<DeviceInfo> deviceInfo); 143 std::string GetNetworkIdByDeviceId(const std::string &deviceId); 144 void HandleDeviceOnline(std::string deviceId, int32_t authForm); 145 void HandleDeviceOffline(std::string deviceId); 146 void SetPkgName(std::string pkgName); 147 bool CheckIsOnline(const std::string &targetDeviceId); 148 void SetPkgNameVec(std::vector<std::string> pkgNameVec); 149 std::vector<std::string> GetPkgName(); 150 void ClearPkgName(); 151 DmDeviceInfo GetDeviceInfoByDeviceId(const std::string &deviceId); 152 void DeleteOffLineTimer(std::string &udidHash); 153 154 private: 155 static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo); 156 static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceBasicInfo &dmDeviceBasicInfo); 157 static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type); 158 static void ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo, DmDeviceInfo &dmDeviceInfo); 159 160 private: 161 enum PulishStatus { 162 STATUS_UNKNOWN = 0, 163 ALLOW_BE_DISCOVERY = 1, 164 NOT_ALLOW_BE_DISCOVERY = 2, 165 }; 166 static std::string remoteUdidHash_; 167 static PulishStatus publishStatus; 168 static IRefreshCallback softbusDiscoveryCallback_; 169 static IRefreshCallback softbusDiscoveryByIdCallback_; 170 static IPublishCb softbusPublishCallback_; 171 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 172 std::shared_ptr<SoftbusSession> softbusSession_; 173 #endif 174 static std::map<std::string, std::shared_ptr<DeviceInfo>> discoveryDeviceInfoMap_; 175 static std::map<std::string, std::shared_ptr<ISoftbusDiscoveryCallback>> discoveryCallbackMap_; 176 static std::map<std::string, std::shared_ptr<ISoftbusPublishCallback>> publishCallbackMap_; 177 std::shared_ptr<ISoftbusStateCallback> deviceStateManagerCallback_; 178 static std::queue<std::string> discoveryDeviceIdQueue_; 179 static std::unordered_map<std::string, std::string> deviceUdidMap_; 180 static std::vector<std::string> pkgNameVec_; 181 static std::mutex discoveryCallbackMutex_; 182 static std::mutex discoveryDeviceInfoMutex_; 183 static std::mutex deviceUdidLocks_; 184 static std::mutex pkgNameVecMutex_; 185 }; 186 } // namespace DistributedHardware 187 } // namespace OHOS 188 #endif // OHOS_DM_SOFTBUS_CONNECTOR_H 189