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