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
18 #include "device_manager_adapter.h"
19
20 #include "log_print.h"
21 #include "serializable/serializable.h"
22 #include "utils/anonymous.h"
23
24 namespace OHOS::DistributedData {
25 using namespace OHOS::DistributedHardware;
26 using namespace OHOS::AppDistributedKv;
27 constexpr int32_t DM_OK = 0;
28 constexpr const char *PKG_NAME = "ohos.distributeddata.service";
29 __attribute__((used)) static bool g_delegateInit =
30 DeviceManagerDelegate::RegisterInstance(&DeviceManagerAdapter::GetInstance());
31 class DataMgrDmStateCall final : public DistributedHardware::DeviceStateCallback {
32 public:
DataMgrDmStateCall(DeviceManagerAdapter & dmAdapter)33 explicit DataMgrDmStateCall(DeviceManagerAdapter &dmAdapter) : dmAdapter_(dmAdapter) {}
34 void OnDeviceOnline(const DmDeviceInfo &info) override;
35 void OnDeviceOffline(const DmDeviceInfo &info) override;
36 void OnDeviceChanged(const DmDeviceInfo &info) override;
37 void OnDeviceReady(const DmDeviceInfo &info) override;
38
39 private:
40 DeviceManagerAdapter &dmAdapter_;
41 };
42
OnDeviceOnline(const DmDeviceInfo & info)43 void DataMgrDmStateCall::OnDeviceOnline(const DmDeviceInfo &info)
44 {
45 dmAdapter_.Online(info);
46 }
47
OnDeviceOffline(const DmDeviceInfo & info)48 void DataMgrDmStateCall::OnDeviceOffline(const DmDeviceInfo &info)
49 {
50 dmAdapter_.Offline(info);
51 }
52
OnDeviceChanged(const DmDeviceInfo & info)53 void DataMgrDmStateCall::OnDeviceChanged(const DmDeviceInfo &info)
54 {
55 dmAdapter_.OnChanged(info);
56 }
57
OnDeviceReady(const DmDeviceInfo & info)58 void DataMgrDmStateCall::OnDeviceReady(const DmDeviceInfo &info)
59 {
60 dmAdapter_.OnReady(info);
61 }
62
63 class DataMgrDmInitCall final : public DistributedHardware::DmInitCallback {
64 public:
DataMgrDmInitCall(DeviceManagerAdapter & dmAdapter,std::shared_ptr<ExecutorPool> executors)65 explicit DataMgrDmInitCall(DeviceManagerAdapter &dmAdapter, std::shared_ptr<ExecutorPool> executors)
66 : dmAdapter_(dmAdapter), executors_(executors) {}
67 void OnRemoteDied() override;
68
69 private:
70 DeviceManagerAdapter &dmAdapter_;
71 std::shared_ptr<ExecutorPool> executors_;
72 };
73
OnRemoteDied()74 void DataMgrDmInitCall::OnRemoteDied()
75 {
76 ZLOGI("device manager died, init again");
77 dmAdapter_.Init(executors_);
78 }
79
80 struct DeviceExtraInfo final : public Serializable {
81 static constexpr int32_t OH_OS_TYPE = 10;
82
83 int32_t OS_TYPE = OH_OS_TYPE;
84
DeviceExtraInfoOHOS::DistributedData::DeviceExtraInfo85 DeviceExtraInfo() {};
~DeviceExtraInfoOHOS::DistributedData::DeviceExtraInfo86 ~DeviceExtraInfo() {};
MarshalOHOS::DistributedData::DeviceExtraInfo87 bool Marshal(json &node) const override
88 {
89 return SetValue(node[GET_NAME(OS_TYPE)], OS_TYPE);
90 };
UnmarshalOHOS::DistributedData::DeviceExtraInfo91 bool Unmarshal(const json &node) override
92 {
93 return GetValue(node, GET_NAME(OS_TYPE), OS_TYPE);
94 };
95 };
96
DeviceManagerAdapter()97 DeviceManagerAdapter::DeviceManagerAdapter()
98 : cloudDmInfo({ "cloudDeviceId", "cloudDeviceName", 0, "cloudNetworkId", 0 })
99 {
100 ZLOGI("construct");
101 }
102
~DeviceManagerAdapter()103 DeviceManagerAdapter::~DeviceManagerAdapter()
104 {
105 ZLOGI("Destruct");
106 }
107
GetInstance()108 DeviceManagerAdapter &DeviceManagerAdapter::GetInstance()
109 {
110 static DeviceManagerAdapter dmAdapter;
111 return dmAdapter;
112 }
113
Init(std::shared_ptr<ExecutorPool> executors)114 void DeviceManagerAdapter::Init(std::shared_ptr<ExecutorPool> executors)
115 {
116 ZLOGI("begin");
117 if (executors_ == nullptr) {
118 executors_ = std::move(executors);
119 }
120 RegDevCallback()();
121 }
122
RegDevCallback()123 std::function<void()> DeviceManagerAdapter::RegDevCallback()
124 {
125 return [this]() {
126 auto &devManager = DeviceManager::GetInstance();
127 auto dmStateCall = std::make_shared<DataMgrDmStateCall>(*this);
128 auto dmInitCall = std::make_shared<DataMgrDmInitCall>(*this, executors_);
129 auto resultInit = devManager.InitDeviceManager(PKG_NAME, dmInitCall);
130 auto resultState = devManager.RegisterDevStateCallback(PKG_NAME, "", dmStateCall);
131 if (resultInit == DM_OK && resultState == DM_OK) {
132 InitDeviceInfo(false);
133 return;
134 }
135 constexpr int32_t INTERVAL = 500;
136 executors_->Schedule(std::chrono::milliseconds(INTERVAL), RegDevCallback());
137 };
138 }
139
StartWatchDeviceChange(const AppDeviceChangeListener * observer,const PipeInfo & pipeInfo)140 Status DeviceManagerAdapter::StartWatchDeviceChange(const AppDeviceChangeListener *observer,
141 __attribute__((unused)) const PipeInfo &pipeInfo)
142 {
143 if (observer == nullptr) {
144 ZLOGE("observer is nullptr");
145 return Status::INVALID_ARGUMENT;
146 }
147 if (!observers_.Insert(observer, observer)) {
148 ZLOGE("insert observer fail");
149 return Status::ERROR;
150 }
151 return Status::SUCCESS;
152 }
153
StopWatchDeviceChange(const AppDeviceChangeListener * observer,const PipeInfo & pipeInfo)154 Status DeviceManagerAdapter::StopWatchDeviceChange(const AppDeviceChangeListener *observer,
155 __attribute__((unused)) const PipeInfo &pipeInfo)
156 {
157 if (observer == nullptr) {
158 ZLOGE("observer is nullptr");
159 return Status::INVALID_ARGUMENT;
160 }
161 if (!observers_.Erase(observer)) {
162 ZLOGE("erase observer fail");
163 return Status::ERROR;
164 }
165 return Status::SUCCESS;
166 }
167
Online(const DmDeviceInfo & info)168 void DeviceManagerAdapter::Online(const DmDeviceInfo &info)
169 {
170 DeviceInfo dvInfo;
171 if (!GetDeviceInfo(info, dvInfo)) {
172 ZLOGE("get device info fail");
173 return;
174 }
175 ZLOGI("[online] uuid:%{public}s, name:%{public}s, type:%{public}d, authForm:%{public}d, osType:%{public}d",
176 Anonymous::Change(dvInfo.uuid).c_str(), Anonymous::Change(dvInfo.deviceName).c_str(), dvInfo.deviceType,
177 static_cast<int32_t>(dvInfo.authForm), dvInfo.osType);
178 SaveDeviceInfo(dvInfo, DeviceChangeType::DEVICE_ONLINE);
179 syncTask_.Insert(dvInfo.uuid, dvInfo.uuid);
180 auto observers = GetObservers();
181 ResetLocalDeviceInfo();
182
183 for (const auto &item : observers) { // notify db
184 if (item == nullptr) {
185 continue;
186 }
187 if (item->GetChangeLevelType() == ChangeLevelType::HIGH) {
188 item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
189 item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
190 }
191 }
192 for (const auto &item : observers) { // judge water version, get device security level
193 if (item == nullptr) {
194 continue;
195 }
196 if (item->GetChangeLevelType() == ChangeLevelType::LOW) {
197 item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
198 }
199 }
200 for (const auto &item : observers) { // set compatible identify, sync service meta
201 if (item == nullptr) {
202 continue;
203 }
204 if (item->GetChangeLevelType() == ChangeLevelType::MIN) {
205 item->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONLINE);
206 }
207 }
208 }
209
NotifyReadyEvent(const std::string & uuid)210 void DeviceManagerAdapter::NotifyReadyEvent(const std::string &uuid)
211 {
212 if (uuid.empty() || !syncTask_.Contains(uuid)) {
213 return;
214 }
215
216 syncTask_.Erase(uuid);
217 if (uuid == CLOUD_DEVICE_UUID) {
218 return;
219 }
220 ZLOGI("[NotifyReadyEvent] uuid:%{public}s", Anonymous::Change(uuid).c_str());
221 std::string event = R"({"extra": {"deviceId":")" + uuid + R"(" } })";
222 DeviceManager::GetInstance().NotifyEvent(PKG_NAME, DmNotifyEvent::DM_NOTIFY_EVENT_ONDEVICEREADY, event);
223 }
224
GetObservers()225 std::vector<const AppDeviceChangeListener *> DeviceManagerAdapter::GetObservers()
226 {
227 std::vector<const AppDeviceChangeListener *> observers;
228 observers.reserve(observers_.Size());
229 observers_.ForEach([&observers](const auto &key, auto &value) {
230 observers.emplace_back(value);
231 return false;
232 });
233 return observers;
234 }
235
Offline(const DmDeviceInfo & info)236 void DeviceManagerAdapter::Offline(const DmDeviceInfo &info)
237 {
238 DeviceInfo dvInfo;
239 if (!GetDeviceInfo(info, dvInfo)) {
240 ZLOGE("get device info fail");
241 return;
242 }
243 syncTask_.Erase(dvInfo.uuid);
244 ZLOGI("[offline] uuid:%{public}s, name:%{public}s, type:%{public}d, authForm:%{public}d, osType:%{public}d",
245 Anonymous::Change(dvInfo.uuid).c_str(), Anonymous::Change(dvInfo.deviceName).c_str(), dvInfo.deviceType,
246 static_cast<int32_t>(dvInfo.authForm), dvInfo.osType);
247 SaveDeviceInfo(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
248 auto task = [this, dvInfo]() {
249 observers_.ForEachCopies([&dvInfo](const auto &key, auto &value) {
250 if (value != nullptr) {
251 value->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_OFFLINE);
252 }
253 return false;
254 });
255 };
256 executors_->Execute(std::move(task));
257 }
258
OnChanged(const DmDeviceInfo & info)259 void DeviceManagerAdapter::OnChanged(const DmDeviceInfo &info)
260 {
261 DeviceInfo dvInfo;
262 if (!GetDeviceInfo(info, dvInfo)) {
263 ZLOGE("get device info fail");
264 return;
265 }
266 ZLOGI("[OnChanged] uuid:%{public}s, name:%{public}s, type:%{public}d, authForm:%{public}d osType:%{public}d",
267 Anonymous::Change(dvInfo.uuid).c_str(), Anonymous::Change(dvInfo.deviceName).c_str(), dvInfo.deviceType,
268 static_cast<int32_t>(dvInfo.authForm), dvInfo.osType);
269 }
270
OnReady(const DmDeviceInfo & info)271 void DeviceManagerAdapter::OnReady(const DmDeviceInfo &info)
272 {
273 DeviceInfo dvInfo;
274 if (!GetDeviceInfo(info, dvInfo)) {
275 ZLOGE("get device info fail");
276 return;
277 }
278 readyDevices_.InsertOrAssign(dvInfo.uuid, std::make_pair(DeviceState::DEVICE_ONREADY, dvInfo));
279 ZLOGI("[OnReady] uuid:%{public}s, name:%{public}s, type:%{public}d, authForm:%{public}d, osType:%{public}d",
280 Anonymous::Change(dvInfo.uuid).c_str(), Anonymous::Change(dvInfo.deviceName).c_str(), dvInfo.deviceType,
281 static_cast<int32_t>(dvInfo.authForm), dvInfo.osType);
282 auto task = [this, dvInfo]() {
283 observers_.ForEachCopies([&dvInfo](const auto &key, auto &value) {
284 if (value != nullptr) {
285 value->OnDeviceChanged(dvInfo, DeviceChangeType::DEVICE_ONREADY);
286 }
287 return false;
288 });
289 };
290 executors_->Execute(std::move(task));
291 }
292
GetDeviceInfo(const DmDeviceInfo & dmInfo,DeviceInfo & dvInfo)293 bool DeviceManagerAdapter::GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo)
294 {
295 std::string networkId = std::string(dmInfo.networkId);
296 if (networkId.empty()) {
297 return false;
298 }
299 auto uuid = GetUuidByNetworkId(networkId);
300 auto udid = GetUdidByNetworkId(networkId);
301 if (uuid.empty() || udid.empty()) {
302 ZLOGW("uuid or udid empty");
303 return false;
304 }
305 if (uuid == CLOUD_DEVICE_UUID) {
306 dvInfo = { uuid, udid, networkId, std::string(dmInfo.deviceName), dmInfo.deviceTypeId, OH_OS_TYPE,
307 static_cast<int32_t>(dmInfo.authForm)};
308 return true;
309 }
310 DeviceExtraInfo deviceExtraInfo;
311 if (!DistributedData::Serializable::Unmarshall(dmInfo.extraData, deviceExtraInfo)) {
312 ZLOGE("Unmarshall failed, deviceExtraInfo:%{public}s", dmInfo.extraData.c_str());
313 return false;
314 }
315 dvInfo = { uuid, udid, networkId, std::string(dmInfo.deviceName), dmInfo.deviceTypeId, deviceExtraInfo.OS_TYPE,
316 static_cast<int32_t>(dmInfo.authForm)};
317 return true;
318 }
319
SaveDeviceInfo(const DeviceInfo & dvInfo,const DeviceChangeType & type)320 void DeviceManagerAdapter::SaveDeviceInfo(const DeviceInfo &dvInfo, const DeviceChangeType &type)
321 {
322 if (dvInfo.networkId == DeviceManagerAdapter::cloudDmInfo.networkId) {
323 return;
324 }
325 switch (type) {
326 case DeviceChangeType::DEVICE_ONLINE: {
327 deviceInfos_.Set(dvInfo.networkId, dvInfo);
328 deviceInfos_.Set(dvInfo.uuid, dvInfo);
329 deviceInfos_.Set(dvInfo.udid, dvInfo);
330 readyDevices_.InsertOrAssign(dvInfo.uuid, std::make_pair(DeviceState::DEVICE_ONLINE, dvInfo));
331 if (dvInfo.osType != OH_OS_TYPE) {
332 otherDeviceInfos_.Set(dvInfo.networkId, dvInfo);
333 otherDeviceInfos_.Set(dvInfo.uuid, dvInfo);
334 otherDeviceInfos_.Set(dvInfo.udid, dvInfo);
335 }
336 break;
337 }
338 case DeviceChangeType::DEVICE_OFFLINE: {
339 deviceInfos_.Delete(dvInfo.networkId);
340 deviceInfos_.Delete(dvInfo.uuid);
341 deviceInfos_.Delete(dvInfo.udid);
342 readyDevices_.Erase(dvInfo.uuid);
343 if (dvInfo.osType != OH_OS_TYPE) {
344 otherDeviceInfos_.Delete(dvInfo.networkId);
345 otherDeviceInfos_.Delete(dvInfo.uuid);
346 otherDeviceInfos_.Delete(dvInfo.udid);
347 }
348 break;
349 }
350 default: {
351 ZLOGW("unknown type.");
352 break;
353 }
354 }
355 }
356
GetLocalDevice()357 DeviceInfo DeviceManagerAdapter::GetLocalDevice()
358 {
359 std::lock_guard<decltype(devInfoMutex_)> lock(devInfoMutex_);
360 if (!localInfo_.uuid.empty() && !localInfo_.udid.empty()) {
361 return localInfo_;
362 }
363 localInfo_ = GetLocalDeviceInfo();
364 return localInfo_;
365 }
366
GetRemoteDevices()367 std::vector<DeviceInfo> DeviceManagerAdapter::GetRemoteDevices()
368 {
369 std::vector<DmDeviceInfo> dmInfos;
370 auto ret = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmInfos);
371 if (ret != DM_OK) {
372 ZLOGE("get trusted device:%{public}d", ret);
373 return {};
374 }
375
376 std::vector<DeviceInfo> dvInfos;
377 for (const auto &dmInfo : dmInfos) {
378 auto networkId = std::string(dmInfo.networkId);
379 auto uuid = GetUuidByNetworkId(networkId);
380 auto udid = GetUdidByNetworkId(networkId);
381 DeviceExtraInfo deviceExtraInfo;
382 if (!DistributedData::Serializable::Unmarshall(dmInfo.extraData, deviceExtraInfo)) {
383 ZLOGE("Unmarshall failed, deviceExtraInfo:%{public}s", dmInfo.extraData.c_str());
384 continue;
385 }
386 DeviceInfo dvInfo = { std::move(uuid), std::move(udid), std::move(networkId),
387 std::string(dmInfo.deviceName), dmInfo.deviceTypeId, deviceExtraInfo.OS_TYPE,
388 static_cast<int32_t>(dmInfo.authForm) };
389 dvInfos.emplace_back(std::move(dvInfo));
390 }
391 return dvInfos;
392 }
393
GetOnlineDevices()394 std::vector<DeviceInfo> DeviceManagerAdapter::GetOnlineDevices()
395 {
396 std::vector<DeviceInfo> devices;
397 devices.reserve(readyDevices_.Size());
398 readyDevices_.ForEach([&devices](auto &, auto &info) {
399 devices.push_back(info.second);
400 return false;
401 });
402 return devices;
403 }
404
IsDeviceReady(const std::string & id)405 bool DeviceManagerAdapter::IsDeviceReady(const std::string& id)
406 {
407 auto it = readyDevices_.Find(id);
408 return (it.first && it.second.first == DeviceState::DEVICE_ONREADY);
409 }
410
IsOHOSType(const std::string & id)411 bool DeviceManagerAdapter::IsOHOSType(const std::string &id)
412 {
413 DeviceInfo dvInfo;
414 if (!deviceInfos_.Get(id, dvInfo)) {
415 InitDeviceInfo();
416 deviceInfos_.Get(id, dvInfo);
417 }
418 return dvInfo.osType == OH_OS_TYPE;
419 }
420
GetAuthType(const std::string & id)421 int32_t DeviceManagerAdapter::GetAuthType(const std::string &id)
422 {
423 DeviceInfo dvInfo;
424 if (!otherDeviceInfos_.Get(id, dvInfo) && !deviceInfos_.Get(id, dvInfo)) {
425 InitDeviceInfo();
426 deviceInfos_.Get(id, dvInfo);
427 }
428 return static_cast<int32_t>(dvInfo.authForm);
429 }
430
GetOnlineSize()431 size_t DeviceManagerAdapter::GetOnlineSize()
432 {
433 return readyDevices_.Size();
434 }
435
GetDeviceInfo(const std::string & id)436 DeviceInfo DeviceManagerAdapter::GetDeviceInfo(const std::string &id)
437 {
438 return GetDeviceInfoFromCache(id);
439 }
440
GetDeviceInfoFromCache(const std::string & id)441 DeviceInfo DeviceManagerAdapter::GetDeviceInfoFromCache(const std::string &id)
442 {
443 DeviceInfo dvInfo;
444 if (!deviceInfos_.Get(id, dvInfo)) {
445 InitDeviceInfo();
446 deviceInfos_.Get(id, dvInfo);
447 }
448 if (dvInfo.uuid.empty()) {
449 ZLOGE("invalid id:%{public}s", Anonymous::Change(id).c_str());
450 }
451 return dvInfo;
452 }
453
InitDeviceInfo(bool onlyCache)454 void DeviceManagerAdapter::InitDeviceInfo(bool onlyCache)
455 {
456 std::vector<DeviceInfo> dvInfos = GetRemoteDevices();
457 if (dvInfos.empty()) {
458 ZLOGW("there is no trusted device!");
459 }
460 if (!onlyCache) {
461 readyDevices_.Clear();
462 }
463 for (const auto &info : dvInfos) {
464 if (info.networkId.empty() || info.uuid.empty() || info.udid.empty()) {
465 ZLOGE("networkId:%{public}s, uuid:%{public}d, udid:%{public}d",
466 Anonymous::Change(info.networkId).c_str(), info.uuid.empty(), info.udid.empty());
467 continue;
468 }
469 deviceInfos_.Set(info.networkId, info);
470 deviceInfos_.Set(info.uuid, info);
471 deviceInfos_.Set(info.udid, info);
472 if (!onlyCache) {
473 readyDevices_.InsertOrAssign(info.uuid, std::make_pair(DeviceState::DEVICE_ONREADY, info));
474 }
475 }
476 auto local = GetLocalDeviceInfo();
477 deviceInfos_.Set(local.networkId, local);
478 deviceInfos_.Set(local.uuid, local);
479 deviceInfos_.Set(local.udid, local);
480 }
481
GetLocalDeviceInfo()482 DeviceInfo DeviceManagerAdapter::GetLocalDeviceInfo()
483 {
484 DmDeviceInfo info;
485 auto ret = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, info);
486 if (ret != DM_OK) {
487 ZLOGE("get local device info fail");
488 return {};
489 }
490 auto networkId = std::string(info.networkId);
491 auto uuid = GetUuidByNetworkId(networkId);
492 auto udid = GetUdidByNetworkId(networkId);
493 if (uuid.empty()) {
494 return {};
495 }
496 DeviceExtraInfo deviceExtraInfo;
497 if (!DistributedData::Serializable::Unmarshall(info.extraData, deviceExtraInfo)) {
498 ZLOGE("Unmarshall failed, deviceExtraInfo:%{public}s", info.extraData.c_str());
499 return {};
500 }
501 ZLOGI("[LocalDevice] uuid:%{public}s, name:%{public}s, type:%{public}d, osType:%{public}d",
502 Anonymous::Change(uuid).c_str(), Anonymous::Change(info.deviceName).c_str(), info.deviceTypeId,
503 deviceExtraInfo.OS_TYPE);
504 return { std::move(uuid), std::move(udid), std::move(networkId), std::string(info.deviceName), info.deviceTypeId,
505 deviceExtraInfo.OS_TYPE, static_cast<int32_t>(info.authForm) };
506 }
507
GetUuidByNetworkId(const std::string & networkId)508 std::string DeviceManagerAdapter::GetUuidByNetworkId(const std::string &networkId)
509 {
510 if (networkId.empty()) {
511 return "";
512 }
513 if (networkId == DeviceManagerAdapter::cloudDmInfo.networkId) {
514 return CLOUD_DEVICE_UUID;
515 }
516 DeviceInfo dvInfo;
517 if (deviceInfos_.Get(networkId, dvInfo)) {
518 return dvInfo.uuid;
519 }
520 std::string uuid;
521 auto ret = DeviceManager::GetInstance().GetUuidByNetworkId(PKG_NAME, networkId, uuid);
522 if (ret != DM_OK || uuid.empty()) {
523 ZLOGE("failed, result:%{public}d, networkId:%{public}s", ret, Anonymous::Change(networkId).c_str());
524 return "";
525 }
526 dvInfo.uuid = uuid;
527 dvInfo.networkId = networkId;
528 deviceInfos_.Set(uuid, dvInfo);
529 return uuid;
530 }
531
GetUdidByNetworkId(const std::string & networkId)532 std::string DeviceManagerAdapter::GetUdidByNetworkId(const std::string &networkId)
533 {
534 if (networkId.empty()) {
535 return "";
536 }
537 if (networkId == DeviceManagerAdapter::cloudDmInfo.networkId) {
538 return CLOUD_DEVICE_UDID;
539 }
540 DeviceInfo dvInfo;
541 if (deviceInfos_.Get(networkId, dvInfo)) {
542 return dvInfo.udid;
543 }
544 std::string udid;
545 auto ret = DeviceManager::GetInstance().GetUdidByNetworkId(PKG_NAME, networkId, udid);
546 if (ret != DM_OK || udid.empty()) {
547 ZLOGE("failed, result:%{public}d, networkId:%{public}s", ret, Anonymous::Change(networkId).c_str());
548 return "";
549 }
550 return udid;
551 }
552
ToUUID(const std::string & id)553 std::string DeviceManagerAdapter::ToUUID(const std::string &id)
554 {
555 return GetDeviceInfoFromCache(id).uuid;
556 }
557
ToUDID(const std::string & id)558 std::string DeviceManagerAdapter::ToUDID(const std::string &id)
559 {
560 return GetDeviceInfoFromCache(id).udid;
561 }
562
ToUUID(const std::vector<std::string> & devices)563 std::vector<std::string> DeviceManagerAdapter::ToUUID(const std::vector<std::string> &devices)
564 {
565 std::vector<std::string> uuids;
566 for (auto &device : devices) {
567 auto uuid = DeviceManagerAdapter::GetInstance().ToUUID(device);
568 if (uuid.empty()) {
569 continue;
570 }
571 uuids.push_back(std::move(uuid));
572 }
573 return uuids;
574 }
575
ToUUID(std::vector<DeviceInfo> devices)576 std::vector<std::string> DeviceManagerAdapter::ToUUID(std::vector<DeviceInfo> devices)
577 {
578 std::vector<std::string> uuids;
579 for (auto &device : devices) {
580 if (device.uuid.empty()) {
581 continue;
582 }
583 uuids.push_back(std::move(device.uuid));
584 }
585 return uuids;
586 }
587
ToNetworkID(const std::string & id)588 std::string DeviceManagerAdapter::ToNetworkID(const std::string &id)
589 {
590 return GetDeviceInfoFromCache(id).networkId;
591 }
592
CalcClientUuid(const std::string & appId,const std::string & uuid)593 std::string DeviceManagerAdapter::CalcClientUuid(const std::string &appId, const std::string &uuid)
594 {
595 if (uuid.empty()) {
596 return "";
597 }
598 std::string encryptedUuid;
599 auto ret = DeviceManager::GetInstance().GenerateEncryptedUuid(PKG_NAME, uuid, appId, encryptedUuid);
600 if (ret != DM_OK) {
601 ZLOGE("failed, result:%{public}d", ret);
602 return "";
603 }
604 return encryptedUuid;
605 }
606
GetEncryptedUuidByNetworkId(const std::string & networkId)607 std::string DeviceManagerAdapter::GetEncryptedUuidByNetworkId(const std::string &networkId)
608 {
609 if (networkId.empty()) {
610 ZLOGE("failed! networkId empty");
611 return "";
612 }
613 std::string encryptedUuid;
614 auto ret = DeviceManager::GetInstance().GetEncryptedUuidByNetworkId(PKG_NAME, networkId, encryptedUuid);
615 if (ret != DM_OK) {
616 ZLOGE("failed, result:%{public}d", ret);
617 return "";
618 }
619 return encryptedUuid;
620 }
621
IsSameAccount(const std::string & id)622 bool DeviceManagerAdapter::IsSameAccount(const std::string &id)
623 {
624 if (id.empty()) {
625 ZLOGE("params id is empty");
626 return false;
627 }
628 auto networkId = DeviceManagerAdapter::GetInstance().ToNetworkID(id);
629 return DeviceManager::GetInstance().IsSameAccount(networkId);
630 }
631
CheckAccessControl(const AccessCaller & accCaller,const AccessCallee & accCallee)632 bool DeviceManagerAdapter::CheckAccessControl(const AccessCaller &accCaller, const AccessCallee &accCallee)
633 {
634 DmAccessCaller dmAccessCaller = { .accountId = accCaller.accountId, .pkgName = accCaller.bundleName,
635 .networkId = accCaller.networkId, .userId = accCaller.userId };
636 DmAccessCallee dmAccessCallee = { .accountId = accCallee.accountId, .networkId = accCallee.networkId,
637 .userId = accCallee.userId };
638 return DeviceManager::GetInstance().CheckAccessControl(dmAccessCaller, dmAccessCallee);
639 }
640
IsSameAccount(const AccessCaller & accCaller,const AccessCallee & accCallee)641 bool DeviceManagerAdapter::IsSameAccount(const AccessCaller &accCaller, const AccessCallee &accCallee)
642 {
643 DmAccessCaller dmAccessCaller = { .accountId = accCaller.accountId, .networkId = accCaller.networkId,
644 .userId = accCaller.userId };
645 DmAccessCallee dmAccessCallee = { .accountId = accCallee.accountId, .networkId = accCallee.networkId,
646 .userId = accCallee.userId };
647 return DeviceManager::GetInstance().CheckIsSameAccount(dmAccessCaller, dmAccessCallee);
648 }
649
ResetLocalDeviceInfo()650 void DeviceManagerAdapter::ResetLocalDeviceInfo()
651 {
652 auto local = GetLocalDeviceInfo();
653 DeviceInfo dvInfo;
654 if (deviceInfos_.Get(local.udid, dvInfo)) {
655 deviceInfos_.Delete(dvInfo.networkId);
656 }
657 deviceInfos_.Set(local.uuid, local);
658 deviceInfos_.Set(local.udid, local);
659 deviceInfos_.Set(local.networkId, local);
660 }
661 } // namespace OHOS::DistributedData
662