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 #include "device_info_manager.h"
17 #include "constant_common.h"
18
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 namespace {
23 std::recursive_mutex g_instanceMutex;
24 }
GetInstance()25 DeviceInfoManager &DeviceInfoManager::GetInstance()
26 {
27 static DeviceInfoManager* instance = nullptr;
28 if (instance == nullptr) {
29 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
30 if (instance == nullptr) {
31 DeviceInfoManager* tmp = new (std::nothrow) DeviceInfoManager();
32 instance = std::move(tmp);
33 }
34 }
35 return *instance;
36 }
37
GetDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType,DeviceInfo & deviceInfo) const38 bool DeviceInfoManager::GetDeviceInfo(
39 const std::string &nodeId, DeviceIdType deviceIdType, DeviceInfo &deviceInfo) const
40 {
41 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
42 }
43
ExistDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType) const44 bool DeviceInfoManager::ExistDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType) const
45 {
46 DeviceInfo deviceInfo;
47 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
48 }
49
AddDeviceInfo(const std::string & networkId,const std::string & universallyUniqueId,const std::string & uniqueDeviceId,const std::string & deviceName,const std::string & deviceType)50 void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::string &universallyUniqueId,
51 const std::string &uniqueDeviceId, const std::string &deviceName, const std::string &deviceType)
52 {
53 if (!DataValidator::IsDeviceIdValid(networkId) ||
54 !DataValidator::IsDeviceIdValid(universallyUniqueId) ||
55 !DataValidator::IsDeviceIdValid(uniqueDeviceId) || deviceName.empty() || deviceType.empty()) {
56 LOGE(ATM_DOMAIN, ATM_TAG, "AddDeviceInfo: input param is invalid");
57 return;
58 }
59 DeviceInfoRepository::GetInstance().SaveDeviceInfo(
60 networkId, universallyUniqueId, uniqueDeviceId, deviceName, deviceType);
61 }
62
RemoveAllRemoteDeviceInfo()63 void DeviceInfoManager::RemoveAllRemoteDeviceInfo()
64 {
65 std::string localDevice = ConstantCommon::GetLocalDeviceId();
66
67 DeviceInfo localDeviceInfoOpt;
68 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(
69 localDevice, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) {
70 DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt);
71 }
72 }
73
RemoveRemoteDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType)74 void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType)
75 {
76 if (!DataValidator::IsDeviceIdValid(nodeId)) {
77 LOGE(ATM_DOMAIN, ATM_TAG, "RemoveDeviceInfoByNetworkId: nodeId is invalid");
78 } else {
79 DeviceInfo deviceInfo;
80 std::string localDevice = ConstantCommon::GetLocalDeviceId();
81 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) {
82 if (deviceInfo.deviceId.uniqueDeviceId != localDevice) {
83 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType);
84 }
85 }
86 }
87 }
88
ConvertToUniversallyUniqueIdOrFetch(const std::string & nodeId) const89 std::string DeviceInfoManager::ConvertToUniversallyUniqueIdOrFetch(const std::string &nodeId) const
90 {
91 std::string result;
92 if (!DataValidator::IsDeviceIdValid(nodeId)) {
93 LOGE(ATM_DOMAIN, ATM_TAG, "ConvertToUniversallyUniqueIdOrFetch: nodeId is invalid.");
94 return result;
95 }
96 DeviceInfo deviceInfo;
97 if (!(DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo))) {
98 return result;
99 }
100 return deviceInfo.deviceId.universallyUniqueId;
101 }
102
ConvertToUniqueDeviceIdOrFetch(const std::string & nodeId) const103 std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string &nodeId) const
104 {
105 std::string result;
106 if (!DataValidator::IsDeviceIdValid(nodeId)) {
107 LOGE(ATM_DOMAIN, ATM_TAG, "ConvertToUniqueDeviceIdOrFetch: nodeId is invalid.");
108 return result;
109 }
110 DeviceInfo deviceInfo;
111 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo)) {
112 std::string uniqueDeviceId = deviceInfo.deviceId.uniqueDeviceId;
113 if (uniqueDeviceId.empty()) {
114 std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(nodeId);
115 if (!udid.empty()) {
116 result = udid;
117 } else {
118 LOGD(ATM_DOMAIN, ATM_TAG,
119 "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)",
120 ConstantCommon::EncryptDevId(nodeId).c_str());
121 }
122 } else {
123 LOGD(ATM_DOMAIN, ATM_TAG,
124 "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ",
125 ConstantCommon::EncryptDevId(nodeId).c_str());
126 result = uniqueDeviceId;
127 }
128 } else {
129 LOGD(ATM_DOMAIN, ATM_TAG, "FindDeviceInfo failed, nodeId(%{public}s)",
130 ConstantCommon::EncryptDevId(nodeId).c_str());
131 auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo();
132 auto iter = list.begin();
133 for (; iter != list.end(); iter++) {
134 DeviceInfo info = (*iter);
135 LOGD(ATM_DOMAIN, ATM_TAG, ">>> DeviceInfoRepository device name: %{public}s", info.deviceName.c_str());
136 LOGD(ATM_DOMAIN, ATM_TAG, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str());
137 LOGD(ATM_DOMAIN, ATM_TAG, ">>> DeviceInfoRepository device network id: %{public}s",
138 ConstantCommon::EncryptDevId(info.deviceId.networkId).c_str());
139 }
140 }
141 return result;
142 }
143
IsDeviceUniversallyUniqueId(const std::string & nodeId) const144 bool DeviceInfoManager::IsDeviceUniversallyUniqueId(const std::string &nodeId) const
145 {
146 if (!DataValidator::IsDeviceIdValid(nodeId)) {
147 LOGE(ATM_DOMAIN, ATM_TAG, "IsDeviceUniversallyUniqueId: nodeId is invalid");
148 return false;
149 }
150 DeviceInfo deviceInfo;
151 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNIVERSALLY_UNIQUE_ID, deviceInfo)) {
152 return deviceInfo.deviceId.universallyUniqueId == nodeId;
153 }
154 return false;
155 }
156 } // namespace AccessToken
157 } // namespace Security
158 } // namespace OHOS
159