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 <string> 23 #include <vector> 24 25 #include "softbus_bus_center.h" 26 #include "dm_device_info.h" 27 #include "dm_publish_info.h" 28 #include "dm_subscribe_info.h" 29 #include "softbus_discovery_callback.h" 30 #include "softbus_publish_callback.h" 31 #include "softbus_session.h" 32 #include "softbus_state_callback.h" 33 34 namespace OHOS { 35 namespace DistributedHardware { 36 class SoftbusConnector { 37 public: 38 /** 39 * @tc.name: SoftbusConnector::OnSoftbusPublishResult 40 * @tc.desc: OnSoftbusPublishResult of the SoftbusConnector 41 * @tc.type: FUNC 42 */ 43 static void OnSoftbusPublishResult(int publishId, PublishResult result); 44 45 /** 46 * @tc.name: SoftbusConnector::OnSoftbusDeviceFound 47 * @tc.desc: OnSoftbus DeviceFound of the SoftbusConnector 48 * @tc.type: FUNC 49 */ 50 static void OnSoftbusDeviceFound(const DeviceInfo *device); 51 52 /** 53 * @tc.name: SoftbusConnector::OnSoftbusDiscoveryResult 54 * @tc.desc: OnSoftbus Discovery Result of the SoftbusConnector 55 * @tc.type: FUNC 56 */ 57 static void OnSoftbusDiscoveryResult(int subscribeId, RefreshResult result); 58 59 /** 60 * @tc.name: SoftbusConnector::OnSoftbusJoinLNNResult 61 * @tc.desc: OnSoftbus JoinLNN Result of the SoftbusConnector 62 * @tc.type: FUNC 63 */ 64 static void OnSoftbusJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t result); 65 66 /** 67 * @tc.name: SoftbusConnector::OnParameterChgCallback 68 * @tc.desc: OnParameter Chg Callback of the SoftbusConnector 69 * @tc.type: FUNC 70 */ 71 static void OnParameterChgCallback(const char *key, const char *value, void *context); 72 73 /** 74 * @tc.name: SoftbusConnector::GetConnectionIpAddress 75 * @tc.desc: Get Connection Ip Address of the SoftbusConnector 76 * @tc.type: FUNC 77 */ 78 static int32_t GetConnectionIpAddress(const std::string &deviceId, std::string &ipAddress); 79 80 /** 81 * @tc.name: SoftbusConnector::GetConnectAddr 82 * @tc.desc: Get Connect Addr of the SoftbusConnector 83 * @tc.type: FUNC 84 */ 85 static ConnectionAddr *GetConnectAddr(const std::string &deviceId, std::string &connectAddr); 86 87 /** 88 * @tc.name: SoftbusConnector::IsDeviceOnLine 89 * @tc.desc: Judge Device OnLine of the SoftbusConnector 90 * @tc.type: FUNC 91 */ 92 static bool IsDeviceOnLine(const std::string &deviceId); 93 94 /** 95 * @tc.name: SoftbusConnector::GetUdidByNetworkId 96 * @tc.desc: Get Udid By NetworkId of the SoftbusConnector 97 * @tc.type: FUNC 98 */ 99 static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); 100 101 /** 102 * @tc.name: SoftbusConnector::GetUuidByNetworkId 103 * @tc.desc: Get Uuid By NetworkId of the SoftbusConnector 104 * @tc.type: FUNC 105 */ 106 static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); 107 108 /** 109 * @tc.name: SoftbusConnector::JoinLnn 110 * @tc.desc: Add the current device to the LNN where a specified device resides 111 * @tc.type: FUNC 112 */ 113 static void JoinLnn(const std::string &deviceId); 114 public: 115 SoftbusConnector(); 116 ~SoftbusConnector(); 117 int32_t RegisterSoftbusStateCallback(const std::string &pkgName, 118 const std::shared_ptr<ISoftbusStateCallback> callback); 119 int32_t UnRegisterSoftbusStateCallback(const std::string &pkgName); 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 PublishDiscovery(const DmPublishInfo &dmPublishInfo); 127 int32_t UnPublishDiscovery(int32_t publishId); 128 int32_t StartDiscovery(const DmSubscribeInfo &subscribeInfo); 129 int32_t StopDiscovery(uint16_t subscribeId); 130 std::shared_ptr<SoftbusSession> GetSoftbusSession(); 131 bool HaveDeviceInMap(std::string deviceId); 132 void HandleDeviceOnline(const DmDeviceInfo &info); 133 void HandleDeviceOffline(const DmDeviceInfo &info); 134 135 private: 136 int32_t Init(); 137 static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo); 138 static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type); 139 140 private: 141 enum PulishStatus { 142 STATUS_UNKNOWN = 0, 143 ALLOW_BE_DISCOVERY = 1, 144 NOT_ALLOW_BE_DISCOVERY = 2, 145 }; 146 static PulishStatus publishStatus; 147 static IRefreshCallback softbusDiscoveryCallback_; 148 static IPublishCb softbusPublishCallback_; 149 std::shared_ptr<SoftbusSession> softbusSession_; 150 static std::map<std::string, std::shared_ptr<DeviceInfo>> discoveryDeviceInfoMap_; 151 static std::map<std::string, std::shared_ptr<ISoftbusStateCallback>> stateCallbackMap_; 152 static std::map<std::string, std::shared_ptr<ISoftbusDiscoveryCallback>> discoveryCallbackMap_; 153 static std::map<std::string, std::shared_ptr<ISoftbusPublishCallback>> publishCallbackMap_; 154 static std::mutex discoveryDeviceInfoMutex_; 155 static std::mutex stateCallbackMutex_; 156 static std::mutex discoveryCallbackMutex_; 157 static std::mutex publishCallbackMutex_; 158 }; 159 } // namespace DistributedHardware 160 } // namespace OHOS 161 #endif // OHOS_DM_SOFTBUS_CONNECTOR_H 162