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