1 /*
2 * Copyright (c) 2021-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 "adapter/dnetwork_adapter.h"
17
18 #include <chrono>
19 #include <functional>
20 #include <thread>
21 #include <unistd.h>
22
23 #include "errors.h"
24 #include "dtbschedmgr_device_info_storage.h"
25 #include "dtbschedmgr_log.h"
26 #include "event_runner.h"
27
28 namespace OHOS {
29 namespace DistributedSchedule {
30 using namespace std::chrono_literals;
31 using namespace DistributedHardware;
32
33 namespace {
34 constexpr int32_t RETRY_REGISTER_CALLBACK_TIMES = 5;
35 constexpr int32_t RETRY_REGISTER_CALLBACK_DELAY_TIME = 1000; // 1s
36 const std::string PKG_NAME = "DBinderBus_Dms_" + std::to_string(getpid());
37
38 constexpr int32_t NON_ANONYMIZED_LENGTH = 6;
39 const std::string EMPTY_DEVICE_ID = "";
40 const std::string TAG = "DnetworkAdapter";
41 }
42
43 std::shared_ptr<AppExecFwk::EventHandler> DnetworkAdapter::dnetworkHandler_;
44 std::mutex DnetworkAdapter::listenerSetMutex_;
45 std::set<std::shared_ptr<DeviceListener>> DnetworkAdapter::listenerSet_;
46
GetInstance()47 std::shared_ptr<DnetworkAdapter> DnetworkAdapter::GetInstance()
48 {
49 static auto instance = std::make_shared<DnetworkAdapter>();
50 return instance;
51 }
52
Init()53 void DnetworkAdapter::Init()
54 {
55 initCallback_ = std::make_shared<DeviceInitCallBack>();
56 stateCallback_ = std::make_shared<DmsDeviceStateCallback>();
57 auto runner = AppExecFwk::EventRunner::Create("dmsDnetwork");
58 dnetworkHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
59 }
60
OnRemoteDied()61 void DnetworkAdapter::DeviceInitCallBack::OnRemoteDied()
62 {
63 HILOGI("DeviceInitCallBack OnRemoteDied");
64 DtbschedmgrDeviceInfoStorage::GetInstance().Init();
65 }
66
OnDeviceOnline(const DmDeviceInfo & deviceInfo)67 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo)
68 {
69 HILOGI("OnNodeOnline netwokId = %{public}s", AnonymizeNetworkId(deviceInfo.networkId).c_str());
70 auto onlineNotifyTask = [deviceInfo]() {
71 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
72 for (auto& listener : listenerSet_) {
73 listener->OnDeviceOnline(deviceInfo);
74 }
75 };
76 if (!dnetworkHandler_->PostTask(onlineNotifyTask)) {
77 HILOGE("OnNodeOnline post task failed");
78 return;
79 }
80 }
81
OnDeviceOffline(const DmDeviceInfo & deviceInfo)82 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo)
83 {
84 HILOGI("OnNodeOffline networkId = %{public}s", AnonymizeNetworkId(deviceInfo.networkId).c_str());
85 auto offlineNotifyTask = [deviceInfo]() {
86 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
87 for (auto& listener : listenerSet_) {
88 listener->OnDeviceOffline(deviceInfo);
89 }
90 };
91 if (!dnetworkHandler_->PostTask(offlineNotifyTask)) {
92 HILOGE("OnNodeOffline post task failed");
93 return;
94 }
95 }
96
OnDeviceChanged(const DmDeviceInfo & deviceInfo)97 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo& deviceInfo)
98 {
99 HILOGD("called");
100 }
101
OnDeviceReady(const DmDeviceInfo & deviceInfo)102 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo)
103 {
104 HILOGD("called");
105 }
106
AddDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)107 bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
108 {
109 HILOGD("AddDeviceChangeListener called");
110 if (dnetworkHandler_ == nullptr) {
111 HILOGE("handler is null");
112 return false;
113 }
114 {
115 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
116 if (listenerSet_.find(listener) == listenerSet_.end()) {
117 listenerSet_.insert(listener);
118 }
119 if (listenerSet_.size() > 1) {
120 return true;
121 }
122 }
123 auto registerTask = [this]() {
124 HILOGD("AddDeviceChangeListener register mission...");
125 int32_t retryTimes = 0;
126 int32_t errCode = ERR_OK;
127 while (retryTimes++ < RETRY_REGISTER_CALLBACK_TIMES) {
128 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(PKG_NAME, initCallback_);
129 if (ret != ERR_OK) {
130 HILOGE("init device manager failed, ret:%{public}d", ret);
131 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
132 continue;
133 }
134 errCode = DeviceManager::GetInstance().RegisterDevStateCallback(PKG_NAME, "", stateCallback_);
135 if (errCode != ERR_OK) {
136 HILOGD("AddDeviceChangeListener Reg errCode = %{public}d, retrying...", errCode);
137 errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
138 HILOGD("AddDeviceChangeListener Unreg errCode = %{public}d", errCode);
139 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
140 continue;
141 }
142 if (UpdateDeviceInfoStorage()) {
143 break;
144 }
145 }
146 HILOGI("AddDeviceChangeListener %{public}s", (errCode == ERR_OK) ? "success" : "timeout");
147 };
148 if (!dnetworkHandler_->PostTask(registerTask)) {
149 HILOGE("AddDeviceChangeListener post task failed");
150 return false;
151 }
152 return true;
153 }
154
UpdateDeviceInfoStorage()155 bool DnetworkAdapter::UpdateDeviceInfoStorage()
156 {
157 std::vector<DistributedHardware::DmDeviceInfo> dmDeviceInfoList;
158 int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList);
159 if (errCode != ERR_OK) {
160 HILOGE("GetTrustedDeviceList failed, errCode = %{public}d", errCode);
161 return false;
162 }
163 DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(dmDeviceInfoList);
164 HILOGI("UpdateDeviceInfoStorage success");
165 return true;
166 }
167
RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)168 void DnetworkAdapter::RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
169 {
170 HILOGD("RemoveDeviceChangeListener called");
171 {
172 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
173 listenerSet_.erase(listener);
174 if (listenerSet_.size() > 0) {
175 return;
176 }
177 }
178
179 int32_t errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
180 if (errCode != ERR_OK) {
181 HILOGE("RemoveDeviceChangeListener remove failed, errCode = %{public}d", errCode);
182 }
183 HILOGD("RemoveDeviceChangeListener remove ok");
184 }
185
GetLocalBasicInfo(DmDeviceInfo & dmDeviceInfo)186 bool DnetworkAdapter::GetLocalBasicInfo(DmDeviceInfo& dmDeviceInfo)
187 {
188 int32_t errCode = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, dmDeviceInfo);
189 if (errCode != ERR_OK) {
190 HILOGE("GetLocalBasicInfo errCode = %{public}d", errCode);
191 return false;
192 }
193 return true;
194 }
195
GetUdidByNetworkId(const std::string & networkId)196 std::string DnetworkAdapter::GetUdidByNetworkId(const std::string& networkId)
197 {
198 if (networkId.empty()) {
199 HILOGE("networkId is empty");
200 return "";
201 }
202 std::string udid = "";
203 int32_t errCode = DeviceManager::GetInstance().GetUdidByNetworkId(PKG_NAME, networkId, udid);
204 if (errCode != ERR_OK) {
205 HILOGE("GetUdidByNetworkId errCode = %{public}d", errCode);
206 return "";
207 }
208 return udid;
209 }
210
GetUuidByNetworkId(const std::string & networkId)211 std::string DnetworkAdapter::GetUuidByNetworkId(const std::string& networkId)
212 {
213 if (networkId.empty()) {
214 HILOGE("networkId is empty");
215 return "";
216 }
217 std::string uuid = "";
218 int32_t errCode = DeviceManager::GetInstance().GetUuidByNetworkId(PKG_NAME, networkId, uuid);
219 if (errCode != ERR_OK) {
220 HILOGE("GetUuidByNetworkId errCode = %{public}d", errCode);
221 return "";
222 }
223 return uuid;
224 }
225
AnonymizeNetworkId(const std::string & networkId)226 std::string DnetworkAdapter::AnonymizeNetworkId(const std::string& networkId)
227 {
228 if (networkId.length() < NON_ANONYMIZED_LENGTH) {
229 return EMPTY_DEVICE_ID;
230 }
231 std::string anonNetworkId = networkId.substr(0, NON_ANONYMIZED_LENGTH);
232 anonNetworkId.append("******");
233 return anonNetworkId;
234 }
235 } // namespace DistributedSchedule
236 } // namespace OHOS
237