1 /* 2 * Copyright (c) 2024 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 DDM_ADAPTER_IMPL_H 17 #define DDM_ADAPTER_IMPL_H 18 19 #include <mutex> 20 #include <set> 21 22 #include "device_manager.h" 23 #include "nocopyable.h" 24 25 #include "i_ddm_adapter.h" 26 27 namespace OHOS { 28 namespace Msdp { 29 namespace DeviceStatus { 30 class DDMAdapterImpl final : public IDDMAdapter, public std::enable_shared_from_this<DDMAdapterImpl> { 31 public: 32 DDMAdapterImpl() = default; 33 ~DDMAdapterImpl(); 34 DISALLOW_COPY_AND_MOVE(DDMAdapterImpl); 35 36 int32_t Enable() override; 37 void Disable() override; 38 39 void AddBoardObserver(std::shared_ptr<IBoardObserver> observer) override; 40 void RemoveBoardObserver(std::shared_ptr<IBoardObserver> observer) override; 41 bool CheckSameAccountToLocal(const std::string &networkId) override; 42 43 private: 44 int32_t GetTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> &deviceList); 45 46 private: 47 class Observer final { 48 public: Observer(std::shared_ptr<IBoardObserver> observer)49 explicit Observer(std::shared_ptr<IBoardObserver> observer) 50 : observer_(observer) {} 51 52 Observer() = default; 53 ~Observer() = default; 54 DISALLOW_COPY_AND_MOVE(Observer); 55 Lock()56 std::shared_ptr<IBoardObserver> Lock() const noexcept 57 { 58 return observer_.lock(); 59 } 60 61 bool operator<(const Observer &other) const noexcept 62 { 63 return (observer_.lock() < other.observer_.lock()); 64 } 65 66 private: 67 std::weak_ptr<IBoardObserver> observer_; 68 }; 69 70 class DmInitCb final : public DistributedHardware::DmInitCallback { 71 public: 72 DmInitCb() = default; 73 ~DmInitCb() = default; 74 DISALLOW_COPY_AND_MOVE(DmInitCb); 75 OnRemoteDied()76 void OnRemoteDied() override {} 77 }; 78 79 class DmBoardStateCb final : public DistributedHardware::DeviceStateCallback { 80 public: DmBoardStateCb(std::shared_ptr<DDMAdapterImpl> dm)81 DmBoardStateCb(std::shared_ptr<DDMAdapterImpl> dm) : dm_(dm) {} 82 ~DmBoardStateCb() = default; 83 DISALLOW_COPY_AND_MOVE(DmBoardStateCb); 84 OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)85 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override 86 { 87 std::shared_ptr<DDMAdapterImpl> dm = dm_.lock(); 88 if (dm != nullptr) { 89 dm->OnBoardOnline(deviceInfo.networkId); 90 } 91 } 92 OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)93 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override 94 { 95 std::shared_ptr<DDMAdapterImpl> dm = dm_.lock(); 96 if (dm != nullptr) { 97 dm->OnBoardOffline(deviceInfo.networkId); 98 } 99 } 100 OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)101 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)102 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} 103 104 private: 105 std::weak_ptr<DDMAdapterImpl> dm_; 106 }; 107 108 void OnBoardOnline(const std::string &networkId); 109 void OnBoardOffline(const std::string &networkId); 110 111 std::mutex lock_; 112 std::shared_ptr<DmInitCb> initCb_; 113 std::shared_ptr<DmBoardStateCb> boardStateCb_; 114 std::set<Observer> observers_; 115 }; 116 } // namespace DeviceStatus 117 } // namespace Msdp 118 } // namespace OHOS 119 #endif // DDM_ADAPTER_IMPL_H 120