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