• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "DeviceManagerAdapter"
17 #include "device_manager_adapter.h"
18 
19 #include <thread>
20 
21 #include "kvstore_utils.h"
22 #include "log_print.h"
23 #include "net_conn_callback_stub.h"
24 #include "net_conn_client.h"
25 #include "net_handle.h"
26 
27 namespace OHOS::DistributedData {
28 using namespace OHOS::DistributedHardware;
29 using namespace OHOS::AppDistributedKv;
30 using namespace OHOS::NetManagerStandard;
31 using KvStoreUtils = OHOS::DistributedKv::KvStoreUtils;
32 constexpr int32_t DM_OK = 0;
33 constexpr const char *PKG_NAME = "ohos.distributeddata.service";
34 class DataMgrDmStateCall final : public DistributedHardware::DeviceStateCallback {
35 public:
DataMgrDmStateCall(DeviceManagerAdapter & dmAdapter)36     explicit DataMgrDmStateCall(DeviceManagerAdapter &dmAdapter) : dmAdapter_(dmAdapter) {}
37     void OnDeviceOnline(const DmDeviceInfo &info) override;
38     void OnDeviceOffline(const DmDeviceInfo &info) override;
39     void OnDeviceChanged(const DmDeviceInfo &info) override;
40     void OnDeviceReady(const DmDeviceInfo &info) override;
41 
42 private:
43     DeviceManagerAdapter &dmAdapter_;
44 };
45 
OnDeviceOnline(const DmDeviceInfo & info)46 void DataMgrDmStateCall::OnDeviceOnline(const DmDeviceInfo &info)
47 {
48     dmAdapter_.Online(info);
49 }
50 
OnDeviceOffline(const DmDeviceInfo & info)51 void DataMgrDmStateCall::OnDeviceOffline(const DmDeviceInfo &info)
52 {
53     dmAdapter_.Offline(info);
54 }
55 
OnDeviceChanged(const DmDeviceInfo & info)56 void DataMgrDmStateCall::OnDeviceChanged(const DmDeviceInfo &info)
57 {
58     dmAdapter_.OnChanged(info);
59 }
60 
OnDeviceReady(const DmDeviceInfo & info)61 void DataMgrDmStateCall::OnDeviceReady(const DmDeviceInfo &info)
62 {
63     dmAdapter_.OnReady(info);
64 }
65 
66 class DataMgrDmInitCall final : public DistributedHardware::DmInitCallback {
67 public:
DataMgrDmInitCall(DeviceManagerAdapter & dmAdapter,std::shared_ptr<ExecutorPool> executors)68     explicit DataMgrDmInitCall(DeviceManagerAdapter &dmAdapter, std::shared_ptr<ExecutorPool> executors)
69         : dmAdapter_(dmAdapter), executors_(executors) {}
70     void OnRemoteDied() override;
71 
72 private:
73     DeviceManagerAdapter &dmAdapter_;
74     std::shared_ptr<ExecutorPool> executors_;
75 };
76 
OnRemoteDied()77 void DataMgrDmInitCall::OnRemoteDied()
78 {
79     ZLOGI("device manager died, init again");
80     dmAdapter_.Init(executors_);
81 }
82 
83 class NetConnCallbackObserver : public NetConnCallbackStub {
84 public:
NetConnCallbackObserver(DeviceManagerAdapter & dmAdapter)85     explicit NetConnCallbackObserver(DeviceManagerAdapter &dmAdapter) : dmAdapter_(dmAdapter) {}
86     ~NetConnCallbackObserver() override = default;
87     int32_t NetAvailable(sptr<NetHandle> &netHandle) override;
88     int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap) override;
89     int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) override;
90     int32_t NetLost(sptr<NetHandle> &netHandle) override;
91     int32_t NetUnavailable() override;
92     int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) override;
93 
94 private:
95     DeviceManagerAdapter &dmAdapter_;
96 };
97 
NetAvailable(sptr<NetManagerStandard::NetHandle> & netHandle)98 int32_t NetConnCallbackObserver::NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle)
99 {
100     ZLOGI("OnNetworkAvailable");
101     dmAdapter_.Online(dmAdapter_.cloudDmInfo);
102     dmAdapter_.isNetAvailable_ = true;
103     return DistributedKv::SUCCESS;
104 }
105 
NetUnavailable()106 int32_t NetConnCallbackObserver::NetUnavailable()
107 {
108     ZLOGI("OnNetworkUnavailable");
109     dmAdapter_.isNetAvailable_ = false;
110     return DistributedKv::SUCCESS;
111 }
112 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)113 int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
114     const sptr<NetAllCapabilities> &netAllCap)
115 {
116     ZLOGI("OnNetCapabilitiesChange");
117     return DistributedKv::SUCCESS;
118 }
119 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)120 int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
121     const sptr<NetLinkInfo> &info)
122 {
123     ZLOGI("OnNetConnectionPropertiesChange");
124     return DistributedKv::SUCCESS;
125 }
126 
NetLost(sptr<NetHandle> & netHandle)127 int32_t NetConnCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
128 {
129     ZLOGI("OnNetLost");
130     dmAdapter_.isNetAvailable_ = false;
131     dmAdapter_.Offline(dmAdapter_.cloudDmInfo);
132     return DistributedKv::SUCCESS;
133 }
134 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)135 int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
136 {
137     ZLOGI("OnNetBlockStatusChange");
138     return DistributedKv::SUCCESS;
139 }
140 
DeviceManagerAdapter()141 DeviceManagerAdapter::DeviceManagerAdapter()
142     : cloudDmInfo({ "cloudDeviceId", "cloudDeviceName", 0, "cloudNetworkId", 0 })
143 {
144     ZLOGI("construct");
145 }
146 
~DeviceManagerAdapter()147 DeviceManagerAdapter::~DeviceManagerAdapter()
148 {
149     ZLOGI("Destruct");
150 }
151 
GetInstance()152 DeviceManagerAdapter &DeviceManagerAdapter::GetInstance()
153 {
154     static DeviceManagerAdapter dmAdapter;
155     return dmAdapter;
156 }
157 
Init(std::shared_ptr<ExecutorPool> executors)158 void DeviceManagerAdapter::Init(std::shared_ptr<ExecutorPool> executors)
159 {
160     ZLOGI("begin");
161     if (executors_ == nullptr) {
162         executors_ = std::move(executors);
163     }
164     RegDevCallback()();
165 }
166 
RegDevCallback()167 std::function<void()> DeviceManagerAdapter::RegDevCallback()
168 {
169     return [this]() {
170         auto &devManager = DeviceManager::GetInstance();
171         auto dmStateCall = std::make_shared<DataMgrDmStateCall>(*this);
172         auto dmInitCall = std::make_shared<DataMgrDmInitCall>(*this, executors_);
173         auto resultInit = devManager.InitDeviceManager(PKG_NAME, dmInitCall);
174         auto resultState = devManager.RegisterDevStateCallback(PKG_NAME, "", dmStateCall);
175         auto resultNet = RegOnNetworkChange();
176         if (resultInit == DM_OK && resultState == DM_OK && resultNet) {
177             return;
178         }
179         constexpr int32_t INTERVAL = 500;
180         executors_->Schedule(std::chrono::milliseconds(INTERVAL), RegDevCallback());
181     };
182 }
183 
StartWatchDeviceChange(const AppDeviceChangeListener * observer,const PipeInfo & pipeInfo)184 Status DeviceManagerAdapter::StartWatchDeviceChange(const AppDeviceChangeListener *observer,
185     __attribute__((unused)) const PipeInfo &pipeInfo)
186 {
187     if (observer == nullptr) {
188         ZLOGE("observer is nullptr");
189         return Status::INVALID_ARGUMENT;
190     }
191     if (!observers_.Insert(observer, observer)) {
192         ZLOGE("insert observer fail");
193         return Status::ERROR;
194     }
195     return Status::SUCCESS;
196 }
197 
StopWatchDeviceChange(const AppDeviceChangeListener * observer,const PipeInfo & pipeInfo)198 Status DeviceManagerAdapter::StopWatchDeviceChange(const AppDeviceChangeListener *observer,
199     __attribute__((unused)) const PipeInfo &pipeInfo)
200 {
201     if (observer == nullptr) {
202         ZLOGE("observer is nullptr");
203         return Status::INVALID_ARGUMENT;
204     }
205     if (!observers_.Erase(observer)) {
206         ZLOGE("erase observer fail");
207         return Status::ERROR;
208     }
209     return Status::SUCCESS;
210 }
211 
Online(const DmDeviceInfo & info)212 void DeviceManagerAdapter::Online(const DmDeviceInfo &info)
213 {
214     DeviceInfo dvInfo;
215     if (!GetDeviceInfo(info, dvInfo)) {
216         ZLOGE("get device info fail");
217         return;
218     }
219     ZLOGI("[online] uuid:%{public}s, name:%{public}s, type:%{public}d",
220         KvStoreUtils::ToBeAnonymous(dvInfo.uuid).c_str(), dvInfo.deviceName.c_str(), dvInfo.deviceType);
221     SaveDeviceInfo(dvInfo, DeviceChangeType::DEVICE_ONLINE);
222     syncTask_.Insert(dvInfo.uuid, dvInfo.uuid);
223     auto observers = GetObservers();
224     for (const auto &item : observers) { // notify db
225         if (item == nullptr) {
226             continue;
227         }
228         if (item->GetChangeLevelType() == ChangeLevelType::HIGH) {
229             item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
230             item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
231         }
232     }
233     for (const auto &item : observers) { // sync meta, get device security level
234         if (item == nullptr) {
235             continue;
236         }
237         if (item->GetChangeLevelType() == ChangeLevelType::LOW) {
238             item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
239         }
240     }
241 
242     executors_->Schedule(std::chrono::milliseconds(SYNC_TIMEOUT), [this, dvInfo]() {
243         TimeOut(dvInfo.uuid);
244     });
245 
246     for (const auto &item : observers) { // set compatible identify, sync service meta
247         if (item == nullptr) {
248             continue;
249         }
250         if (item->GetChangeLevelType() == ChangeLevelType::MIN) {
251             item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
252         }
253     }
254 }
255 
TimeOut(const std::string uuid)256 void DeviceManagerAdapter::TimeOut(const std::string uuid)
257 {
258     if (uuid.empty()) {
259         ZLOGE("uuid empty!");
260         return;
261     }
262     if (syncTask_.Contains(uuid) && uuid != CLOUD_DEVICE_UUID) {
263         ZLOGI("[TimeOutReadyEvent] uuid:%{public}s", KvStoreUtils::ToBeAnonymous(uuid).c_str());
264         std::string event = R"({"extra": {"deviceId":")" + uuid + R"(" } })";
265         DeviceManager::GetInstance().NotifyEvent(PKG_NAME, DmNotifyEvent::DM_NOTIFY_EVENT_ONDEVICEREADY, event);
266     }
267     syncTask_.Erase(uuid);
268 }
269 
NotifyReadyEvent(const std::string & uuid)270 void DeviceManagerAdapter::NotifyReadyEvent(const std::string &uuid)
271 {
272     if (uuid.empty() || !syncTask_.Contains(uuid)) {
273         return;
274     }
275 
276     syncTask_.Erase(uuid);
277     ZLOGI("[NotifyReadyEvent] uuid:%{public}s", KvStoreUtils::ToBeAnonymous(uuid).c_str());
278     std::string event = R"({"extra": {"deviceId":")" + uuid + R"(" } })";
279     DeviceManager::GetInstance().NotifyEvent(PKG_NAME, DmNotifyEvent::DM_NOTIFY_EVENT_ONDEVICEREADY, event);
280 }
281 
GetObservers()282 std::vector<const AppDeviceChangeListener *> DeviceManagerAdapter::GetObservers()
283 {
284     std::vector<const AppDeviceChangeListener *> observers;
285     observers.reserve(observers_.Size());
286     observers_.ForEach([&observers](const auto &key, auto &value) {
287         observers.emplace_back(value);
288         return false;
289     });
290     return observers;
291 }
292 
Offline(const DmDeviceInfo & info)293 void DeviceManagerAdapter::Offline(const DmDeviceInfo &info)
294 {
295     DeviceInfo dvInfo;
296     if (!GetDeviceInfo(info, dvInfo)) {
297         ZLOGE("get device info fail");
298         return;
299     }
300     syncTask_.Erase(dvInfo.uuid);
301     ZLOGI("[offline] uuid:%{public}s, name:%{public}s, type:%{public}d",
302         KvStoreUtils::ToBeAnonymous(dvInfo.uuid).c_str(), dvInfo.deviceName.c_str(), dvInfo.deviceType);
303     SaveDeviceInfo(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
304     auto task = [this, dvInfo]() {
305         observers_.ForEachCopies([&dvInfo](const auto &key, auto &value) {
306             if (value != nullptr) {
307                 value->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
308             }
309             return false;
310         });
311     };
312     executors_->Execute(std::move(task));
313 }
314 
OnChanged(const DmDeviceInfo & info)315 void DeviceManagerAdapter::OnChanged(const DmDeviceInfo &info)
316 {
317     DeviceInfo dvInfo;
318     if (!GetDeviceInfo(info, dvInfo)) {
319         ZLOGE("get device info fail");
320         return;
321     }
322     ZLOGI("[OnChanged] uuid:%{public}s, name:%{public}s, type:%{public}d",
323         KvStoreUtils::ToBeAnonymous(dvInfo.uuid).c_str(), dvInfo.deviceName.c_str(), dvInfo.deviceType);
324 }
325 
OnReady(const DmDeviceInfo & info)326 void DeviceManagerAdapter::OnReady(const DmDeviceInfo &info)
327 {
328     DeviceInfo dvInfo;
329     if (!GetDeviceInfo(info, dvInfo)) {
330         ZLOGE("get device info fail");
331         return;
332     }
333     ZLOGI("[OnReady] uuid:%{public}s, name:%{public}s, type:%{public}d",
334         KvStoreUtils::ToBeAnonymous(dvInfo.uuid).c_str(), dvInfo.deviceName.c_str(), dvInfo.deviceType);
335     auto task = [this, dvInfo]() {
336         observers_.ForEachCopies([&dvInfo](const auto &key, auto &value) {
337             if (value != nullptr) {
338                 value->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONREADY);
339             }
340             return false;
341         });
342     };
343     executors_->Execute(std::move(task));
344 }
345 
GetDeviceInfo(const DmDeviceInfo & dmInfo,DeviceInfo & dvInfo)346 bool DeviceManagerAdapter::GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo)
347 {
348     std::string networkId = std::string(dmInfo.networkId);
349     if (networkId.empty()) {
350         return false;
351     }
352     auto uuid = GetUuidByNetworkId(networkId);
353     auto udid = GetUdidByNetworkId(networkId);
354     if (uuid.empty() || udid.empty()) {
355         ZLOGW("uuid or udid empty");
356         return false;
357     }
358     dvInfo = { uuid, udid, networkId, std::string(dmInfo.deviceName), dmInfo.deviceTypeId };
359     return true;
360 }
361 
SaveDeviceInfo(const DeviceInfo & dvInfo,const DeviceChangeType & type)362 void DeviceManagerAdapter::SaveDeviceInfo(const DeviceInfo &dvInfo, const DeviceChangeType &type)
363 {
364     switch (type) {
365         case DeviceChangeType::DEVICE_ONLINE: {
366             deviceInfos_.Set(dvInfo.networkId, dvInfo);
367             deviceInfos_.Set(dvInfo.uuid, dvInfo);
368             deviceInfos_.Set(dvInfo.udid, dvInfo);
369             break;
370         }
371         case DeviceChangeType::DEVICE_OFFLINE: {
372             deviceInfos_.Delete(dvInfo.networkId);
373             deviceInfos_.Delete(dvInfo.uuid);
374             deviceInfos_.Delete(dvInfo.udid);
375             break;
376         }
377         default: {
378             ZLOGW("unknown type.");
379             break;
380         }
381     }
382 }
383 
GetLocalDevice()384 DeviceInfo DeviceManagerAdapter::GetLocalDevice()
385 {
386     std::lock_guard<decltype(devInfoMutex_)> lock(devInfoMutex_);
387     if (!localInfo_.uuid.empty() && !localInfo_.udid.empty()) {
388         return localInfo_;
389     }
390 
391     DmDeviceInfo info;
392     auto ret = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, info);
393     if (ret != DM_OK) {
394         ZLOGE("get local device info fail");
395         return {};
396     }
397     auto networkId = std::string(info.networkId);
398     auto uuid = GetUuidByNetworkId(networkId);
399     auto udid = GetUdidByNetworkId(networkId);
400     if (uuid.empty()) {
401         return {};
402     }
403     ZLOGI("[LocalDevice] uuid:%{public}s, name:%{public}s, type:%{public}d",
404         KvStoreUtils::ToBeAnonymous(uuid).c_str(), info.deviceName, info.deviceTypeId);
405     localInfo_ = { std::move(uuid), std::move(udid), std::move(networkId),
406                    std::string(info.deviceName), info.deviceTypeId };
407     return localInfo_;
408 }
409 
GetRemoteDevices()410 std::vector<DeviceInfo> DeviceManagerAdapter::GetRemoteDevices()
411 {
412     std::vector<DmDeviceInfo> dmInfos;
413     auto ret = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmInfos);
414     if (ret != DM_OK) {
415         ZLOGE("get trusted device:%{public}d", ret);
416         return {};
417     }
418 
419     std::vector<DeviceInfo> dvInfos;
420     for (const auto &dmInfo : dmInfos) {
421         auto networkId = std::string(dmInfo.networkId);
422         auto uuid = GetUuidByNetworkId(networkId);
423         auto udid = GetUdidByNetworkId(networkId);
424         DeviceInfo dvInfo = { std::move(uuid), std::move(udid), std::move(networkId),
425                               std::string(dmInfo.deviceName), dmInfo.deviceTypeId };
426         dvInfos.emplace_back(std::move(dvInfo));
427     }
428     return dvInfos;
429 }
430 
GetDeviceInfo(const std::string & id)431 DeviceInfo DeviceManagerAdapter::GetDeviceInfo(const std::string &id)
432 {
433     return GetDeviceInfoFromCache(id);
434 }
435 
GetDeviceInfoFromCache(const std::string & id)436 DeviceInfo DeviceManagerAdapter::GetDeviceInfoFromCache(const std::string &id)
437 {
438     DeviceInfo dvInfo;
439     if (!deviceInfos_.Get(id, dvInfo)) {
440         UpdateDeviceInfo();
441         deviceInfos_.Get(id, dvInfo);
442     }
443     if (dvInfo.uuid.empty()) {
444         ZLOGE("invalid id:%{public}s", KvStoreUtils::ToBeAnonymous(id).c_str());
445     }
446     return dvInfo;
447 }
448 
UpdateDeviceInfo()449 void DeviceManagerAdapter::UpdateDeviceInfo()
450 {
451     std::vector<DeviceInfo> dvInfos = GetRemoteDevices();
452     if (dvInfos.empty()) {
453         ZLOGW("there is no trusted device!");
454     }
455     dvInfos.emplace_back(GetLocalDevice());
456     for (const auto &info : dvInfos) {
457         if (info.networkId.empty() || info.uuid.empty() || info.udid.empty()) {
458             ZLOGE("networkId:%{public}s, uuid:%{public}d, udid:%{public}d",
459                 KvStoreUtils::ToBeAnonymous(info.networkId).c_str(), info.uuid.empty(), info.udid.empty());
460             continue;
461         }
462         deviceInfos_.Set(info.networkId, info);
463         deviceInfos_.Set(info.uuid, info);
464         deviceInfos_.Set(info.udid, info);
465     }
466 }
467 
GetUuidByNetworkId(const std::string & networkId)468 std::string DeviceManagerAdapter::GetUuidByNetworkId(const std::string &networkId)
469 {
470     if (networkId.empty()) {
471         return "";
472     }
473     if (networkId == DeviceManagerAdapter::cloudDmInfo.networkId) {
474         return CLOUD_DEVICE_UUID;
475     }
476     DeviceInfo dvInfo;
477     if (deviceInfos_.Get(networkId, dvInfo)) {
478         return dvInfo.uuid;
479     }
480     std::string uuid;
481     auto ret = DeviceManager::GetInstance().GetUuidByNetworkId(PKG_NAME, networkId, uuid);
482     if (ret != DM_OK || uuid.empty()) {
483         ZLOGE("failed, result:%{public}d, networkId:%{public}s", ret, KvStoreUtils::ToBeAnonymous(networkId).c_str());
484         return "";
485     }
486     return uuid;
487 }
488 
GetUdidByNetworkId(const std::string & networkId)489 std::string DeviceManagerAdapter::GetUdidByNetworkId(const std::string &networkId)
490 {
491     if (networkId.empty()) {
492         return "";
493     }
494     if (networkId == DeviceManagerAdapter::cloudDmInfo.networkId) {
495         return CLOUD_DEVICE_UDID;
496     }
497     DeviceInfo dvInfo;
498     if (deviceInfos_.Get(networkId, dvInfo)) {
499         return dvInfo.udid;
500     }
501     std::string udid;
502     auto ret = DeviceManager::GetInstance().GetUdidByNetworkId(PKG_NAME, networkId, udid);
503     if (ret != DM_OK || udid.empty()) {
504         ZLOGE("failed, result:%{public}d, networkId:%{public}s", ret, KvStoreUtils::ToBeAnonymous(networkId).c_str());
505         return "";
506     }
507     return udid;
508 }
509 
ToUUID(const std::string & id)510 std::string DeviceManagerAdapter::ToUUID(const std::string &id)
511 {
512     return GetDeviceInfoFromCache(id).uuid;
513 }
514 
ToUDID(const std::string & id)515 std::string DeviceManagerAdapter::ToUDID(const std::string &id)
516 {
517     return GetDeviceInfoFromCache(id).udid;
518 }
519 
ToUUID(const std::vector<std::string> & devices)520 std::vector<std::string> DeviceManagerAdapter::ToUUID(const std::vector<std::string> &devices)
521 {
522     std::vector<std::string> uuids;
523     for (auto &device : devices) {
524         auto uuid = DeviceManagerAdapter::GetInstance().ToUUID(device);
525         if (uuid.empty()) {
526             continue;
527         }
528         uuids.push_back(std::move(uuid));
529     }
530     return uuids;
531 }
532 
ToUUID(std::vector<DeviceInfo> devices)533 std::vector<std::string> DeviceManagerAdapter::ToUUID(std::vector<DeviceInfo> devices)
534 {
535     std::vector<std::string> uuids;
536     for (auto &device : devices) {
537         if (device.uuid.empty()) {
538             continue;
539         }
540         uuids.push_back(std::move(device.uuid));
541     }
542     return uuids;
543 }
544 
ToNetworkID(const std::string & id)545 std::string DeviceManagerAdapter::ToNetworkID(const std::string &id)
546 {
547     return GetDeviceInfoFromCache(id).networkId;
548 }
549 
CalcClientUuid(const std::string & appId,const std::string & uuid)550 std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const std::string &uuid)
551 {
552     if (uuid.empty()) {
553         return "";
554     }
555     std::string encryptedUuid;
556     auto ret = DeviceManager::GetInstance().GenerateEncryptedUuid(PKG_NAME, uuid, appId, encryptedUuid);
557     if (ret != DM_OK) {
558         ZLOGE("failed, result:%{public}d", ret);
559         return "";
560     }
561     return encryptedUuid;
562 }
563 
RegOnNetworkChange()564 bool DeviceManagerAdapter::RegOnNetworkChange()
565 {
566     sptr<NetConnCallbackObserver> observer = new (std::nothrow) NetConnCallbackObserver(*this);
567     if (observer == nullptr) {
568         ZLOGE("new operator error.observer is nullptr");
569         return false;
570     }
571     int nRet = NetConnClient::GetInstance().RegisterNetConnCallback(observer);
572     if (nRet != NETMANAGER_SUCCESS) {
573         ZLOGE("RegisterNetConnCallback failed, ret = %{public}d", nRet);
574         return false;
575     }
576     return true;
577 }
578 
IsNetworkAvailable()579 bool DeviceManagerAdapter::IsNetworkAvailable()
580 {
581     return isNetAvailable_;
582 }
583 } // namespace OHOS::DistributedData
584