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_DISTRIBUTED_DNETWORK_ADAPTER_H 17 #define OHOS_DISTRIBUTED_DNETWORK_ADAPTER_H 18 19 #include <set> 20 #include <string> 21 22 #include "event_handler.h" 23 #include "softbus_bus_center.h" 24 25 namespace OHOS { 26 namespace DistributedSchedule { 27 enum DeviceInfoType { 28 UNKNOWN_INFO = 0, 29 BASIC_INFO = 1, 30 NETWORK_INFO = 2, 31 TRUST_INFO = 3, 32 }; 33 34 class DeviceListener { 35 public: 36 DeviceListener() = default; 37 virtual ~DeviceListener() = default; 38 39 virtual void OnDeviceOnline(const NodeBasicInfo* nodeBasicInfo) = 0; 40 virtual void OnDeviceOffline(const NodeBasicInfo* nodeBasicInfo) = 0; 41 virtual void OnDeviceInfoChanged(const std::string& networkId, DeviceInfoType type) = 0; 42 }; 43 44 class DnetworkAdapter { 45 public: 46 DnetworkAdapter() = default; 47 ~DnetworkAdapter() = default; 48 49 void Init(); 50 bool AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener); 51 void RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener); 52 std::string GetUdidByNetworkId(const std::string& networkId); 53 std::string GetUuidByNetworkId(const std::string& networkId); 54 std::shared_ptr<NodeBasicInfo> GetLocalBasicInfo(); 55 56 static std::string AnonymizeDeviceId(const std::string& deviceId); 57 static std::shared_ptr<DnetworkAdapter> GetInstance(); 58 59 private: 60 DISALLOW_COPY_AND_MOVE(DnetworkAdapter); 61 62 std::string GetUuidOrUdidByNetworkId(const std::string& networkId, NodeDeviceInfoKey keyType); 63 static void OnNodeOnline(NodeBasicInfo* info); 64 static void OnNodeOffline(NodeBasicInfo* info); 65 static void OnNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info); 66 67 INodeStateCb nodeStateCb_; 68 static std::shared_ptr<AppExecFwk::EventHandler> dnetworkHandler_; 69 static std::mutex listenerSetMutex_; 70 static std::set<std::shared_ptr<DeviceListener>> listenerSet_; 71 }; 72 } // namespace DistributedSchedule 73 } // namespace OHOS 74 #endif // OHOS_DISTRIBUTED_DNETWORK_ADAPTER_H