1 /* 2 * Copyright (c) 2021-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_DISTRIBUTED_DNETWORK_ADAPTER_H 17 #define OHOS_DISTRIBUTED_DNETWORK_ADAPTER_H 18 19 #include <iosfwd> 20 #include <memory> 21 #include <mutex> 22 #include <set> 23 #include <string> 24 25 #include "device_manager.h" 26 #include "event_handler.h" 27 #include "nocopyable.h" 28 29 namespace OHOS { 30 namespace DistributedSchedule { 31 enum DeviceInfoType { 32 UNKNOWN_INFO = 0, 33 BASIC_INFO = 1, 34 NETWORK_INFO = 2, 35 TRUST_INFO = 3, 36 }; 37 38 enum NodeDeviceInfoKey { 39 NODE_KEY_UDID = 0, 40 NODE_KEY_UUID = 1, 41 }; 42 43 class DeviceListener { 44 public: 45 DeviceListener() = default; 46 virtual ~DeviceListener() = default; 47 48 virtual void OnDeviceOnline(const DistributedHardware::DmDeviceInfo& deviceInfo) = 0; 49 virtual void OnDeviceOffline(const DistributedHardware::DmDeviceInfo& deviceInfo) = 0; 50 virtual void OnDeviceInfoChanged(const DistributedHardware::DmDeviceInfo& deviceInfo) = 0; 51 }; 52 53 class DnetworkAdapter { 54 public: 55 DnetworkAdapter() = default; 56 ~DnetworkAdapter() = default; 57 58 void Init(); 59 bool AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener); 60 void RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener); 61 std::string GetUdidByNetworkId(const std::string& networkId); 62 std::string GetUuidByNetworkId(const std::string& networkId); 63 bool GetLocalBasicInfo(DistributedHardware::DmDeviceInfo& dmDeviceInfo); 64 65 static std::shared_ptr<DnetworkAdapter> GetInstance(); 66 bool UpdateDeviceInfoStorage(); 67 68 private: 69 DISALLOW_COPY_AND_MOVE(DnetworkAdapter); 70 71 static std::shared_ptr<AppExecFwk::EventHandler> dnetworkHandler_; 72 static std::mutex listenerSetMutex_; 73 static std::set<std::shared_ptr<DeviceListener>> listenerSet_; 74 75 std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCallback_; 76 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_; 77 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 78 void OnRemoteDied() override; 79 }; 80 81 class DmsDeviceStateCallback : public DistributedHardware::DeviceStateCallback { 82 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 83 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 84 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 85 void OnDeviceReady(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 86 }; 87 }; 88 } // namespace DistributedSchedule 89 } // namespace OHOS 90 #endif // OHOS_DISTRIBUTED_DNETWORK_ADAPTER_H