1 /*
2 * Copyright (c) 2021-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 #include "dtbschedmgr_device_info_storage.h"
17
18 #include <chrono>
19 #include <thread>
20
21 #include "device_manager.h"
22 #include "ipc_object_proxy.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26
27 #include "distributed_device_node_listener.h"
28 #include "distributed_sched_service.h"
29 #include "distributed_sched_utils.h"
30 #include "dtbschedmgr_log.h"
31 #include "mission/notification/dms_continue_recv_manager.h"
32 #include "multi_user_manager.h"
33
34 using namespace std;
35 namespace OHOS {
36 namespace DistributedSchedule {
37 using namespace std::chrono_literals;
38 using namespace DistributedHardware;
39
40 namespace {
41 constexpr int32_t RETRY_TIMES = 30;
42 constexpr int32_t CONNECT_SOFTBUS_RETRY_TIMES = 60;
43 const std::string TAG = "DtbschedmgrDeviceInfoStorage";
44 const std::string PKG_NAME = "DBinderBus_Dms_" + std::to_string(getprocpid());
45 }
46
47 IMPLEMENT_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage);
48
Init()49 bool DtbschedmgrDeviceInfoStorage::Init()
50 {
51 if (initHandler_ == nullptr) {
52 auto deviceInfoStorageRunner = AppExecFwk::EventRunner::Create("DmsDeviceInfoStorageManager");
53 initHandler_ = std::make_shared<AppExecFwk::EventHandler>(deviceInfoStorageRunner);
54 }
55
56 auto func = [this]() {
57 HILOGI("begin connect softbus");
58 for (int32_t retryTimes = 0; retryTimes <= CONNECT_SOFTBUS_RETRY_TIMES; retryTimes++) {
59 if (ConnectSoftbus()) {
60 return;
61 }
62 HILOGE("retry connect softbus %{public}d times", retryTimes);
63 std::this_thread::sleep_for(1s);
64 }
65 HILOGE("connect softbus 60times * 30s, error!!");
66 };
67 if (!initHandler_->PostTask(func)) {
68 HILOGE("Init handler postTask failed");
69 return false;
70 }
71 return true;
72 }
73
ConnectSoftbus()74 bool DtbschedmgrDeviceInfoStorage::ConnectSoftbus()
75 {
76 ClearAllDevices();
77 bool isReady = WaitForDnetworkReady();
78 if (!isReady) {
79 HILOGE("ConnectSoftbus wait Dnetwork failed!");
80 return false;
81 }
82 std::shared_ptr<DnetworkAdapter> dnetworkAdapter = DnetworkAdapter::GetInstance();
83 if (dnetworkAdapter == nullptr) {
84 HILOGE("DnetworkAdapter::GetInstance is null");
85 return false;
86 }
87 if (!InitNetworkIdManager(dnetworkAdapter)) {
88 HILOGE("InitNetworkIdManager failed");
89 return false;
90 }
91 HILOGI("ConnectSoftbus success");
92 return true;
93 }
94
InitNetworkIdManager(std::shared_ptr<DnetworkAdapter> dnetworkAdapter)95 bool DtbschedmgrDeviceInfoStorage::InitNetworkIdManager(std::shared_ptr<DnetworkAdapter> dnetworkAdapter)
96 {
97 if (dnetworkAdapter == nullptr) {
98 HILOGE("dnetworkAdapter is null");
99 return false;
100 }
101 if (networkIdMgrHandler_ == nullptr) {
102 auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager");
103 networkIdMgrHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
104 }
105
106 deviceNodeListener_ = std::make_shared<DistributedDeviceNodeListener>();
107 if (!dnetworkAdapter->AddDeviceChangeListener(deviceNodeListener_)) {
108 deviceNodeListener_ = nullptr;
109 HILOGE("AddDeviceChangeListener failed!");
110 return false;
111 }
112 return true;
113 }
114
Stop()115 void DtbschedmgrDeviceInfoStorage::Stop()
116 {
117 ClearAllDevices();
118 if (deviceNodeListener_ != nullptr) {
119 DnetworkAdapter::GetInstance()->RemoveDeviceChangeListener(deviceNodeListener_);
120 deviceNodeListener_ = nullptr;
121 }
122 }
123
WaitForDnetworkReady()124 bool DtbschedmgrDeviceInfoStorage::WaitForDnetworkReady()
125 {
126 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127 if (samgr == nullptr) {
128 HILOGE("WaitForDnetworkReady failed to get samgr!");
129 return false;
130 }
131 int32_t retryTimeout = RETRY_TIMES;
132 do {
133 auto dnetwork = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
134 if (dnetwork != nullptr) {
135 IPCObjectProxy* proxy = reinterpret_cast<IPCObjectProxy*>(dnetwork.GetRefPtr());
136 // make sure the proxy is not dead
137 if (proxy != nullptr && !proxy->IsObjectDead()) {
138 return true;
139 }
140 }
141 HILOGI("Waiting for dnentwork service...");
142 std::this_thread::sleep_for(1s);
143 if (--retryTimeout <= 0) {
144 HILOGI("Waiting for dnentwork service timeout(30)s");
145 return false;
146 }
147 } while (true);
148 return false;
149 }
150
RegisterUuidNetworkIdMap(const std::string & networkId)151 void DtbschedmgrDeviceInfoStorage::RegisterUuidNetworkIdMap(const std::string& networkId)
152 {
153 std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
154 if (uuid.empty()) {
155 HILOGE("GetUuidByNetworkId return an empty uuid!");
156 return;
157 }
158 {
159 std::lock_guard<std::mutex> autoLock(uuidNetworkIdLock_);
160 uuidNetworkIdMap_[uuid] = networkId;
161 }
162 }
163
UnregisterUuidNetworkIdMap(const std::string & networkId)164 void DtbschedmgrDeviceInfoStorage::UnregisterUuidNetworkIdMap(const std::string& networkId)
165 {
166 std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
167 if (uuid.empty()) {
168 HILOGE("GetUuidByNetworkId return an empty uuid");
169 return;
170 }
171 {
172 std::lock_guard<std::mutex> autoLock(uuidNetworkIdLock_);
173 uuidNetworkIdMap_.erase(uuid);
174 }
175 }
176
GetDeviceIdSet(std::set<std::string> & deviceIdSet)177 void DtbschedmgrDeviceInfoStorage::GetDeviceIdSet(std::set<std::string>& deviceIdSet)
178 {
179 deviceIdSet.clear();
180 lock_guard<mutex> autoLock(deviceLock_);
181 for (const auto& device : remoteDevices_) {
182 deviceIdSet.emplace(device.first);
183 }
184 }
185
UpdateDeviceInfoStorage()186 bool DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage()
187 {
188 std::vector<DistributedHardware::DmDeviceInfo> dmDeviceInfoList;
189 int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList);
190 if (errCode != ERR_OK) {
191 HILOGE("Get device manager trusted device list fail, errCode %{public}d", errCode);
192 return false;
193 }
194 for (const auto& dmDeviceInfo : dmDeviceInfoList) {
195 int32_t osType = Constants::OH_OS_TYPE;
196 std::string osVersion = "";
197 if (!GetOsInfoFromDM(dmDeviceInfo.extraData, osType, osVersion)) {
198 HILOGE("Get Os info from DM device info fail, extraData %{public}s.", dmDeviceInfo.extraData.c_str());
199 }
200 auto deviceInfo = std::make_shared<DmsDeviceInfo>(dmDeviceInfo.deviceName, dmDeviceInfo.deviceTypeId,
201 dmDeviceInfo.networkId, ONLINE, osType, osVersion);
202 std::string networkId = deviceInfo->GetNetworkId();
203 RegisterUuidNetworkIdMap(networkId);
204 {
205 HILOGI("Add remote device networkId %{public}s", GetAnonymStr(networkId).c_str());
206 lock_guard<mutex> autoLock(deviceLock_);
207 remoteDevices_[networkId] = deviceInfo;
208 }
209 }
210 HILOGI("Update remote devices info storage success.");
211 return true;
212 }
213
GetLocalDeviceId(std::string & networkId)214 bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceId(std::string& networkId)
215 {
216 auto dnetworkAdapter = DnetworkAdapter::GetInstance();
217 if (dnetworkAdapter == nullptr) {
218 HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null");
219 return false;
220 }
221 DmDeviceInfo dmDeviceInfo;
222 if (!dnetworkAdapter->GetLocalBasicInfo(dmDeviceInfo)) {
223 HILOGE("GetLocalBasicInfo error");
224 return false;
225 }
226 networkId = dmDeviceInfo.networkId;
227 HILOGI("get local networkId from DnetworkAdapter, networkId = %{public}s", GetAnonymStr(networkId).c_str());
228 return true;
229 }
230
GetLocalUdid(std::string & udid)231 bool DtbschedmgrDeviceInfoStorage::GetLocalUdid(std::string& udid)
232 {
233 auto dnetworkAdapter = DnetworkAdapter::GetInstance();
234 if (dnetworkAdapter == nullptr) {
235 HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null");
236 return false;
237 }
238 DmDeviceInfo dmDeviceInfo;
239 if (!dnetworkAdapter->GetLocalBasicInfo(dmDeviceInfo)) {
240 HILOGE("GetLocalBasicInfo error");
241 return false;
242 }
243 udid = GetUdidByNetworkId(dmDeviceInfo.networkId);
244 HILOGD("GetLocalDeviceUdid = %{public}s", GetAnonymStr(udid).c_str());
245 return true;
246 }
247
GetLocalUuid(std::string & uuid)248 bool DtbschedmgrDeviceInfoStorage::GetLocalUuid(std::string& uuid)
249 {
250 auto dnetworkAdapter = DnetworkAdapter::GetInstance();
251 if (dnetworkAdapter == nullptr) {
252 HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null");
253 return false;
254 }
255 DmDeviceInfo dmDeviceInfo;
256 if (!dnetworkAdapter->GetLocalBasicInfo(dmDeviceInfo)) {
257 HILOGE("GetLocalBasicInfo error");
258 return false;
259 }
260 uuid = GetUuidByNetworkId(dmDeviceInfo.networkId);
261 HILOGD("GetLocalDeviceUuid = %{public}s", GetAnonymStr(uuid).c_str());
262 return true;
263 }
264
ClearAllDevices()265 void DtbschedmgrDeviceInfoStorage::ClearAllDevices()
266 {
267 lock_guard<mutex> autoLock(deviceLock_);
268 remoteDevices_.clear();
269 }
270
FindDeviceInfoInStorage(const std::string & networkId)271 std::shared_ptr<DmsDeviceInfo> DtbschedmgrDeviceInfoStorage::FindDeviceInfoInStorage(const std::string& networkId)
272 {
273 lock_guard<mutex> autoLock(deviceLock_);
274 auto iter = remoteDevices_.find(networkId);
275 if (iter == remoteDevices_.end()) {
276 HILOGE("Get remote device info from storage fail, networkId %{public}s.", GetAnonymStr(networkId).c_str());
277 return nullptr;
278 }
279 HILOGI("Get remote device info from storage success, networkId %{public}s.", GetAnonymStr(networkId).c_str());
280 return iter->second;
281 }
282
GetDeviceInfoById(const std::string & networkId)283 std::shared_ptr<DmsDeviceInfo> DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const std::string& networkId)
284 {
285 HILOGI("Get device info by networkId %{public}s start.", GetAnonymStr(networkId).c_str());
286 auto devInfo = FindDeviceInfoInStorage(networkId);
287 if (devInfo != nullptr) {
288 return devInfo;
289 }
290
291 HILOGI("NetworkId %{public}s not in storage, update devices info from device manager.",
292 GetAnonymStr(networkId).c_str());
293 if (!UpdateDeviceInfoStorage()) {
294 HILOGE("Update device info storage from device manager trusted device list fail.");
295 return nullptr;
296 }
297
298 devInfo = FindDeviceInfoInStorage(networkId);
299 return devInfo;
300 }
301
CheckNetworkIdByBundleName(const std::string & bundleName,const std::string & networkId)302 bool DtbschedmgrDeviceInfoStorage::CheckNetworkIdByBundleName(const std::string& bundleName,
303 const std::string& networkId)
304 {
305 HILOGI("called, bundleName: %{public}s", bundleName.c_str());
306 std::vector<DistributedHardware::DmDeviceInfo> dmDeviceInfoList;
307 int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(bundleName, "", dmDeviceInfoList);
308 if (errCode != ERR_OK || dmDeviceInfoList.empty()) {
309 HILOGE("Get device manager trusted device list fail, errCode %{public}d", errCode);
310 return false;
311 }
312 for (auto dmDeviceInfo : dmDeviceInfoList) {
313 if (dmDeviceInfo.networkId == networkId) {
314 return true;
315 }
316 }
317 return false;
318 }
319
GetUuidByNetworkId(const std::string & networkId)320 std::string DtbschedmgrDeviceInfoStorage::GetUuidByNetworkId(const std::string& networkId)
321 {
322 if (networkId.empty()) {
323 HILOGW("GetUuidByNetworkId networkId empty!");
324 return "";
325 }
326 {
327 std::lock_guard<std::mutex> autoLock(uuidNetworkIdLock_);
328 auto iter = uuidNetworkIdMap_.begin();
329 while (iter != uuidNetworkIdMap_.end()) {
330 if (iter->second == networkId) {
331 return iter->first;
332 } else {
333 ++iter;
334 }
335 }
336 }
337 std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
338 return uuid;
339 }
340
GetUdidByNetworkId(const std::string & networkId)341 std::string DtbschedmgrDeviceInfoStorage::GetUdidByNetworkId(const std::string& networkId)
342 {
343 if (networkId.empty()) {
344 HILOGW("GetUdidByNetworkId networkId empty!");
345 return "";
346 }
347 std::string udid = DnetworkAdapter::GetInstance()->GetUdidByNetworkId(networkId);
348 return udid;
349 }
350
GetNetworkIdByUuid(const std::string & uuid)351 std::string DtbschedmgrDeviceInfoStorage::GetNetworkIdByUuid(const std::string& uuid)
352 {
353 if (uuid.empty()) {
354 HILOGW("GetNetworkIdByUuid uuid empty!");
355 return "";
356 }
357 {
358 std::lock_guard<std::mutex> autoLock(uuidNetworkIdLock_);
359 auto iter = uuidNetworkIdMap_.find(uuid);
360 if (iter != uuidNetworkIdMap_.end()) {
361 return iter->second;
362 }
363 return "";
364 }
365 }
366
DeviceOnlineNotify(const std::shared_ptr<DmsDeviceInfo> devInfo)367 void DtbschedmgrDeviceInfoStorage::DeviceOnlineNotify(const std::shared_ptr<DmsDeviceInfo> devInfo)
368 {
369 if (devInfo == nullptr) {
370 HILOGE("DeviceOnlineNotify devInfo null");
371 return;
372 }
373
374 if (networkIdMgrHandler_ == nullptr) {
375 HILOGE("networkIdMgrHandler null");
376 return;
377 }
378 auto nodeOnline = [this, devInfo]() {
379 std::string networkId = devInfo->GetNetworkId();
380 RegisterUuidNetworkIdMap(networkId);
381 std::string uuid = GetUuidByNetworkId(networkId);
382 HILOGI("networkId: %{public}s, uuid: %{public}s, deviceName: %{public}s, osType: %{public}d, "
383 "osVersion: %{public}s.", GetAnonymStr(networkId).c_str(), GetAnonymStr(uuid).c_str(),
384 devInfo->GetDeviceName().c_str(), devInfo->GetDeviceOSType(), devInfo->GetGetDeviceOSVersion().c_str());
385 {
386 lock_guard<mutex> autoLock(deviceLock_);
387 remoteDevices_[networkId] = devInfo;
388 }
389 DistributedSchedService::GetInstance().DeviceOnlineNotify(networkId);
390 };
391 if (!networkIdMgrHandler_->PostTask(nodeOnline)) {
392 HILOGE("DeviceOnlineNotify handler postTask failed");
393 }
394 }
395
DeviceOfflineNotify(const std::string & networkId)396 void DtbschedmgrDeviceInfoStorage::DeviceOfflineNotify(const std::string& networkId)
397 {
398 if (networkId.empty()) {
399 HILOGE("DeviceOfflineNotify networkId empty");
400 return;
401 }
402 HILOGD("DeviceOfflineNotify networkId: %{public}s", GetAnonymStr(networkId).c_str());
403 if (networkIdMgrHandler_ == nullptr) {
404 HILOGE("DeviceOfflineNotify networkIdMgrHandler null");
405 return;
406 }
407 auto nodeOffline = [this, networkId]() {
408 std::string uuid = GetUuidByNetworkId(networkId);
409 HILOGI("DeviceOfflineNotify process networkId: %{public}s, uuid: %{public}s",
410 GetAnonymStr(networkId).c_str(), GetAnonymStr(uuid).c_str());
411 DistributedSchedService::GetInstance().DeviceOfflineNotify(networkId);
412 UnregisterUuidNetworkIdMap(networkId);
413 lock_guard<mutex> autoLock(deviceLock_);
414 remoteDevices_.erase(networkId);
415 DistributedSchedService::GetInstance().DeviceOfflineNotifyAfterDelete(networkId);
416 };
417 if (!networkIdMgrHandler_->PostTask(nodeOffline)) {
418 HILOGE("DeviceOfflineNotify handler postTask failed");
419 }
420 }
421
OnDeviceInfoChanged(const std::string & deviceId)422 void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId)
423 {
424 HILOGI("OnDeviceInfoChanged called");
425 if (!MultiUserManager::GetInstance().CheckRegSoftbusListener() &&
426 DistributedHardware::DeviceManager::GetInstance().IsSameAccount(deviceId)) {
427 HILOGI("DMSContinueRecvMgr need init");
428 MultiUserManager::GetInstance().RegisterSoftbusListener();
429 }
430 }
431
OnRemoteDied(const wptr<IRemoteObject> & remote)432 void DnetServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
433 {
434 HILOGI("OnRemoteDied dnetwork service died");
435 DtbschedmgrDeviceInfoStorage::GetInstance().Init();
436 }
437
GetDeviceName(std::string netWorkId)438 std::string DtbschedmgrDeviceInfoStorage::GetDeviceName(std::string netWorkId)
439 {
440 for (auto device = remoteDevices_.begin(); device != remoteDevices_.end(); ++device) {
441 if (device->second != nullptr && device->second->GetNetworkId() == netWorkId) {
442 HILOGI("deviceName = %{public}s", device->second->GetDeviceName().c_str());
443 return device->second->GetDeviceName();
444 }
445 }
446 return "";
447 }
448
GetNetworkIdList()449 std::vector<std::string> DtbschedmgrDeviceInfoStorage::GetNetworkIdList()
450 {
451 std::vector<std::string> devices;
452 for (auto device = remoteDevices_.begin(); device != remoteDevices_.end(); ++device) {
453 if (device->second != nullptr) {
454 HILOGI("NetworkId: %{public}s", GetAnonymStr(device->second->GetNetworkId()).c_str());
455 devices.push_back(device->second->GetNetworkId());
456 }
457 }
458 return devices;
459 }
460 }
461 }
462