1 /*
2 * Copyright (c) 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 "device_manager_util.h"
17
18 #include "device_manager.h"
19 #include "parameter.h"
20
21 #include "iam_check.h"
22
23 #define LOG_TAG "USER_AUTH_SA"
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 using DeviceManager = OHOS::DistributedHardware::DeviceManager;
28 namespace {
29 static const std::string USER_AUTH_PACKAGE_NAME = "ohos.useriam";
30 }
31
GetInstance()32 DeviceManagerUtil &DeviceManagerUtil::GetInstance()
33 {
34 static DeviceManagerUtil instance;
35 return instance;
36 }
37
GetUdidByNetworkId(const std::string & networkId,std::string & udid)38 bool DeviceManagerUtil::GetUdidByNetworkId(const std::string &networkId, std::string &udid)
39 {
40 int32_t ret = DeviceManager::GetInstance().GetUdidByNetworkId(USER_AUTH_PACKAGE_NAME, networkId, udid);
41 IF_FALSE_LOGE_AND_RETURN_VAL(ret == 0, false);
42 IF_FALSE_LOGE_AND_RETURN_VAL(!udid.empty(), false);
43
44 return true;
45 }
46
GetLocalDeviceNetWorkId(std::string & networkId)47 bool DeviceManagerUtil::GetLocalDeviceNetWorkId(std::string &networkId)
48 {
49 int32_t ret = DeviceManager::GetInstance().GetLocalDeviceNetWorkId(USER_AUTH_PACKAGE_NAME, networkId);
50 IF_FALSE_LOGE_AND_RETURN_VAL(ret == 0, false);
51 IF_FALSE_LOGE_AND_RETURN_VAL(!networkId.empty(), false);
52
53 return true;
54 }
55
GetLocalDeviceUdid(std::string & udid)56 bool DeviceManagerUtil::GetLocalDeviceUdid(std::string &udid)
57 {
58 std::lock_guard<std::recursive_mutex> lock(mutex_);
59
60 if (localUdid_.has_value()) {
61 udid = localUdid_.value();
62 return true;
63 }
64
65 constexpr uint32_t MAX_UDID_STR_LEN = 65;
66 char udidStr[MAX_UDID_STR_LEN] = { 0 };
67 if (GetDevUdid(udidStr, MAX_UDID_STR_LEN) != 0) {
68 IAM_LOGE("GetDevUdid failed");
69 return false;
70 }
71 udid = udidStr;
72 localUdid_ = udid;
73
74 return true;
75 }
76
GetNetworkIdByUdid(const std::string & udid,std::string & networkId)77 bool DeviceManagerUtil::GetNetworkIdByUdid(const std::string &udid, std::string &networkId)
78 {
79 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
80 int32_t ret = DeviceManager::GetInstance().GetTrustedDeviceList(USER_AUTH_PACKAGE_NAME, "", deviceList);
81 IF_FALSE_LOGE_AND_RETURN_VAL(ret == 0, false);
82
83 networkId = "";
84 for (auto &device : deviceList) {
85 std::string deviceUdid(device.deviceId);
86 if (deviceUdid == udid) {
87 networkId = std::string(device.networkId);
88 break;
89 }
90 }
91 if (networkId.empty()) {
92 IAM_LOGE("networkId not found");
93 return false;
94 }
95 return true;
96 }
97 } // namespace UserAuth
98 } // namespace UserIam
99 } // namespace OHOS