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 #include "ddm_adapter_impl.h"
17
18 #include <algorithm>
19
20 #include "devicestatus_define.h"
21
22 namespace OHOS {
23 namespace Msdp {
24 namespace DeviceStatus {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DDMAdapterImpl" };
27 }
28
29 #define D_DEV_MGR DistributedHardware::DeviceManager::GetInstance()
30
~DDMAdapterImpl()31 DDMAdapterImpl::~DDMAdapterImpl()
32 {
33 Disable();
34 }
35
Enable()36 int32_t DDMAdapterImpl::Enable()
37 {
38 std::lock_guard guard(lock_);
39 std::string pkgName(FI_PKG_NAME);
40 std::string extra;
41 initCb_ = std::make_shared<DmInitCb>();
42
43 int32_t ret = D_DEV_MGR.InitDeviceManager(pkgName, initCb_);
44 if (ret != 0) {
45 FI_HILOGE("DM::InitDeviceManager fail");
46 goto INIT_FAIL;
47 }
48 boardStateCb_ = std::make_shared<DmBoardStateCb>(shared_from_this());
49
50 ret = D_DEV_MGR.RegisterDevStateCallback(pkgName, extra, boardStateCb_);
51 if (ret != 0) {
52 FI_HILOGE("DM::RegisterDevStateCallback fail");
53 goto REG_FAIL;
54 }
55 return RET_OK;
56
57 REG_FAIL:
58 ret = D_DEV_MGR.UnInitDeviceManager(pkgName);
59 if (ret != 0) {
60 FI_HILOGE("DM::UnInitDeviceManager fail");
61 }
62 boardStateCb_.reset();
63
64 INIT_FAIL:
65 initCb_.reset();
66 return RET_ERR;
67 }
68
Disable()69 void DDMAdapterImpl::Disable()
70 {
71 std::lock_guard guard(lock_);
72 std::string pkgName(FI_PKG_NAME);
73
74 if (boardStateCb_ != nullptr) {
75 boardStateCb_.reset();
76 int32_t ret = D_DEV_MGR.UnRegisterDevStateCallback(pkgName);
77 if (ret != 0) {
78 FI_HILOGE("DM::UnRegisterDevStateCallback fail");
79 }
80 }
81 if (initCb_ != nullptr) {
82 initCb_.reset();
83 int32_t ret = D_DEV_MGR.UnInitDeviceManager(pkgName);
84 if (ret != 0) {
85 FI_HILOGE("DM::UnInitDeviceManager fail");
86 }
87 }
88 }
89
AddBoardObserver(std::shared_ptr<IBoardObserver> observer)90 void DDMAdapterImpl::AddBoardObserver(std::shared_ptr<IBoardObserver> observer)
91 {
92 std::lock_guard guard(lock_);
93 CHKPV(observer);
94 observers_.erase(Observer());
95 observers_.emplace(observer);
96 }
97
RemoveBoardObserver(std::shared_ptr<IBoardObserver> observer)98 void DDMAdapterImpl::RemoveBoardObserver(std::shared_ptr<IBoardObserver> observer)
99 {
100 std::lock_guard guard(lock_);
101 CHKPV(observer);
102 observers_.erase(Observer());
103 if (auto iter = observers_.find(Observer(observer)); iter != observers_.end()) {
104 observers_.erase(iter);
105 }
106 }
107
OnBoardOnline(const std::string & networkId)108 void DDMAdapterImpl::OnBoardOnline(const std::string &networkId)
109 {
110 std::lock_guard guard(lock_);
111 std::for_each(observers_.cbegin(), observers_.cend(),
112 [&networkId](const auto &item) {
113 if (auto observer = item.Lock(); observer != nullptr) {
114 observer->OnBoardOnline(networkId);
115 }
116 });
117 }
118
OnBoardOffline(const std::string & networkId)119 void DDMAdapterImpl::OnBoardOffline(const std::string &networkId)
120 {
121 std::lock_guard guard(lock_);
122 std::for_each(observers_.cbegin(), observers_.cend(),
123 [&networkId](const auto &item) {
124 if (auto observer = item.Lock(); observer != nullptr) {
125 observer->OnBoardOffline(networkId);
126 }
127 });
128 }
129 } // namespace DeviceStatus
130 } // namespace Msdp
131 } // namespace OHOS
132