1 /* 2 * Copyright (c) 2021 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_DEVICE_MANAGER_SOFTBUS_ADAPTER_H 17 #define OHOS_DEVICE_MANAGER_SOFTBUS_ADAPTER_H 18 19 #include <string> 20 #include <mutex> 21 #include <map> 22 #include <memory> 23 #include "softbus_bus_center.h" 24 #include "discovery_service.h" 25 #include "dm_device_info.h" 26 #include "dm_subscribe_info.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 class PublishServiceCallBack { 31 public: 32 static void OnPublishSuccess(int32_t publishId); 33 static void OnPublishFail(int32_t publishId, PublishFailReason reason); 34 }; 35 36 class SoftbusAdapter { 37 public: 38 static int32_t Init(); 39 static int32_t GetTrustDevices(const std::string &pkgName, NodeBasicInfo **info, int32_t *infoNum); 40 static int32_t StartDiscovery(std::string &pkgName, SubscribeInfo *info); 41 static int32_t StopDiscovery(std::string &pkgName, uint16_t subscribeId); 42 static bool IsDeviceOnLine(std::string &deviceId); 43 static int32_t GetConnectionIpAddr(std::string deviceId, std::string &ipAddr); 44 static ConnectionAddr *GetConnectAddr(std::string deviceId); 45 public: 46 static void OnSoftBusDeviceOnline(NodeBasicInfo *info); 47 static void OnSoftbusDeviceOffline(NodeBasicInfo *info); 48 static void OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info); 49 static void OnSoftbusDeviceFound(const DeviceInfo *device); 50 static void OnSoftbusDiscoverFailed(int32_t subscribeId, DiscoveryFailReason failReason); 51 static void OnSoftbusDiscoverySuccess(int32_t subscribeId); 52 private: 53 static bool GetsubscribeIdAdapter(std::string &pkgName, int16_t originId, int32_t &adapterId); 54 static bool GetpkgNameBySubscribeId(int32_t adapterId, std::string &pkgName); 55 static void SaveDiscoverDeviceInfo(const DeviceInfo *deviceInfo); 56 static void RemoveDiscoverDeviceInfo(const std::string deviceId); 57 static void NodeBasicInfoCopyToDmDevice(DmDeviceInfo &dmDeviceInfo, NodeBasicInfo &nodeBasicInfo); 58 static void DeviceInfoCopyToDmDevice(DmDeviceInfo &dmDeviceInfo, const DeviceInfo &deviceInfo); 59 static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type); 60 private: 61 struct SubscribeInfoAdapter { 62 SubscribeInfo info; 63 uint16_t subscribeIdOrigin; 64 uint16_t subscribeIdPrefix; 65 }; 66 static std::map<std::string, std::vector<std::shared_ptr<SubscribeInfoAdapter>>> subscribeInfos_; 67 static std::map<std::string, std::shared_ptr<DeviceInfo>> discoverDeviceInfoMap_; 68 static std::vector<std::shared_ptr<DeviceInfo>> discoverDeviceInfoVector_; 69 static uint16_t subscribeIdPrefix; 70 static std::mutex lock_; 71 static INodeStateCb softbusNodeStateCb_; 72 static IDiscoveryCallback softbusDiscoverCallback_; 73 static IPublishCallback servicePublishCallback_; 74 }; 75 } // namespace DistributedHardware 76 } // namespace OHOS 77 #endif // OHOS_DEVICE_MANAGER_SOFTBUS_ADAPTER_H 78