1 /* 2 * Copyright (c) 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 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 42 private: 43 class Observer final { 44 public: Observer(std::shared_ptr<IBoardObserver> observer)45 explicit Observer(std::shared_ptr<IBoardObserver> observer) 46 : observer_(observer) {} 47 48 Observer() = default; 49 ~Observer() = default; 50 DISALLOW_COPY_AND_MOVE(Observer); 51 Lock()52 std::shared_ptr<IBoardObserver> Lock() const noexcept 53 { 54 return observer_.lock(); 55 } 56 57 bool operator<(const Observer &other) const noexcept 58 { 59 return (observer_.lock() < other.observer_.lock()); 60 } 61 62 private: 63 std::weak_ptr<IBoardObserver> observer_; 64 }; 65 66 class DmInitCb final : public DistributedHardware::DmInitCallback { 67 public: 68 DmInitCb() = default; 69 ~DmInitCb() = default; 70 DISALLOW_COPY_AND_MOVE(DmInitCb); 71 OnRemoteDied()72 void OnRemoteDied() override {} 73 }; 74 75 class DmBoardStateCb final : public DistributedHardware::DeviceStateCallback { 76 public: DmBoardStateCb(std::shared_ptr<DDMAdapterImpl> dm)77 DmBoardStateCb(std::shared_ptr<DDMAdapterImpl> dm) : dm_(dm) {} 78 ~DmBoardStateCb() = default; 79 DISALLOW_COPY_AND_MOVE(DmBoardStateCb); 80 OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)81 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override 82 { 83 std::shared_ptr<DDMAdapterImpl> dm = dm_.lock(); 84 if (dm != nullptr) { 85 dm->OnBoardOnline(deviceInfo.networkId); 86 } 87 } 88 OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)89 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override 90 { 91 std::shared_ptr<DDMAdapterImpl> dm = dm_.lock(); 92 if (dm != nullptr) { 93 dm->OnBoardOffline(deviceInfo.networkId); 94 } 95 } 96 OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)97 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)98 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} 99 100 private: 101 std::weak_ptr<DDMAdapterImpl> dm_; 102 }; 103 104 void OnBoardOnline(const std::string &networkId); 105 void OnBoardOffline(const std::string &networkId); 106 107 std::mutex lock_; 108 std::shared_ptr<DmInitCb> initCb_; 109 std::shared_ptr<DmBoardStateCb> boardStateCb_; 110 std::set<Observer> observers_; 111 }; 112 } // namespace DeviceStatus 113 } // namespace Msdp 114 } // namespace OHOS 115 #endif // DDM_ADAPTER_IMPL_H 116