179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci 1679a732c7Sopenharmony_ci#include "dm_softbus_cache.h" 1779a732c7Sopenharmony_ci#include "dm_anonymous.h" 1879a732c7Sopenharmony_ci#include "dm_crypto.h" 1979a732c7Sopenharmony_ci#include "dm_constants.h" 2079a732c7Sopenharmony_ci#include "dm_device_info.h" 2179a732c7Sopenharmony_ci#include "dm_log.h" 2279a732c7Sopenharmony_cinamespace OHOS { 2379a732c7Sopenharmony_cinamespace DistributedHardware { 2479a732c7Sopenharmony_ciDM_IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); 2579a732c7Sopenharmony_cibool g_online = false; 2679a732c7Sopenharmony_cibool g_getLocalDevInfo = false; 2779a732c7Sopenharmony_ciDmDeviceInfo localDeviceInfo_; 2879a732c7Sopenharmony_cistd::mutex localDevInfoMutex_; 2979a732c7Sopenharmony_civoid SoftbusCache::SaveLocalDeviceInfo() 3079a732c7Sopenharmony_ci{ 3179a732c7Sopenharmony_ci LOGI("SoftbusCache::SaveLocalDeviceInfo"); 3279a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(localDevInfoMutex_); 3379a732c7Sopenharmony_ci if (g_online) { 3479a732c7Sopenharmony_ci return; 3579a732c7Sopenharmony_ci } 3679a732c7Sopenharmony_ci NodeBasicInfo nodeBasicInfo; 3779a732c7Sopenharmony_ci int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); 3879a732c7Sopenharmony_ci if (ret != DM_OK) { 3979a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); 4079a732c7Sopenharmony_ci return; 4179a732c7Sopenharmony_ci } 4279a732c7Sopenharmony_ci ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); 4379a732c7Sopenharmony_ci LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", 4479a732c7Sopenharmony_ci GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); 4579a732c7Sopenharmony_ci SaveDeviceInfo(localDeviceInfo_); 4679a732c7Sopenharmony_ci SaveDeviceSecurityLevel(localDeviceInfo_.networkId); 4779a732c7Sopenharmony_ci g_online = true; 4879a732c7Sopenharmony_ci g_getLocalDevInfo = true; 4979a732c7Sopenharmony_ci} 5079a732c7Sopenharmony_ci 5179a732c7Sopenharmony_civoid SoftbusCache::DeleteLocalDeviceInfo() 5279a732c7Sopenharmony_ci{ 5379a732c7Sopenharmony_ci LOGI("SoftbusCache::DeleteLocalDeviceInfo networkid %{public}s.", 5479a732c7Sopenharmony_ci GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); 5579a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(localDevInfoMutex_); 5679a732c7Sopenharmony_ci g_online = false; 5779a732c7Sopenharmony_ci g_getLocalDevInfo = false; 5879a732c7Sopenharmony_ci} 5979a732c7Sopenharmony_ci 6079a732c7Sopenharmony_ciint32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) 6179a732c7Sopenharmony_ci{ 6279a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(localDevInfoMutex_); 6379a732c7Sopenharmony_ci if (g_getLocalDevInfo) { 6479a732c7Sopenharmony_ci nodeInfo = localDeviceInfo_; 6579a732c7Sopenharmony_ci LOGI("SoftbusCache::GetLocalDeviceInfo from dm cache."); 6679a732c7Sopenharmony_ci return DM_OK; 6779a732c7Sopenharmony_ci } 6879a732c7Sopenharmony_ci NodeBasicInfo nodeBasicInfo; 6979a732c7Sopenharmony_ci int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); 7079a732c7Sopenharmony_ci if (ret != DM_OK) { 7179a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); 7279a732c7Sopenharmony_ci return ret; 7379a732c7Sopenharmony_ci } 7479a732c7Sopenharmony_ci ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); 7579a732c7Sopenharmony_ci nodeInfo = localDeviceInfo_; 7679a732c7Sopenharmony_ci SaveDeviceInfo(localDeviceInfo_); 7779a732c7Sopenharmony_ci SaveDeviceSecurityLevel(localDeviceInfo_.networkId); 7879a732c7Sopenharmony_ci g_getLocalDevInfo = true; 7979a732c7Sopenharmony_ci LOGI("SoftbusCache::GetLocalDeviceInfo from softbus."); 8079a732c7Sopenharmony_ci return DM_OK; 8179a732c7Sopenharmony_ci} 8279a732c7Sopenharmony_ci 8379a732c7Sopenharmony_civoid SoftbusCache::UpDataLocalDevInfo() 8479a732c7Sopenharmony_ci{ 8579a732c7Sopenharmony_ci LOGI("SoftbusCache::UpDataLocalDevInfo"); 8679a732c7Sopenharmony_ci NodeBasicInfo nodeBasicInfo; 8779a732c7Sopenharmony_ci int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); 8879a732c7Sopenharmony_ci if (ret != DM_OK) { 8979a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); 9079a732c7Sopenharmony_ci return; 9179a732c7Sopenharmony_ci } 9279a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(localDevInfoMutex_); 9379a732c7Sopenharmony_ci ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); 9479a732c7Sopenharmony_ci ChangeDeviceInfo(localDeviceInfo_); 9579a732c7Sopenharmony_ci} 9679a732c7Sopenharmony_ci 9779a732c7Sopenharmony_ciint32_t SoftbusCache::GetUdidByNetworkId(const char *networkId, std::string &udid) 9879a732c7Sopenharmony_ci{ 9979a732c7Sopenharmony_ci uint8_t mUdid[UDID_BUF_LEN] = {0}; 10079a732c7Sopenharmony_ci int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, sizeof(mUdid)); 10179a732c7Sopenharmony_ci if (ret != DM_OK) { 10279a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); 10379a732c7Sopenharmony_ci return ret; 10479a732c7Sopenharmony_ci } 10579a732c7Sopenharmony_ci udid = reinterpret_cast<char *>(mUdid); 10679a732c7Sopenharmony_ci return ret; 10779a732c7Sopenharmony_ci} 10879a732c7Sopenharmony_ci 10979a732c7Sopenharmony_ciint32_t SoftbusCache::GetUuidByNetworkId(const char *networkId, std::string &uuid) 11079a732c7Sopenharmony_ci{ 11179a732c7Sopenharmony_ci uint8_t mUuid[UUID_BUF_LEN] = {0}; 11279a732c7Sopenharmony_ci int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UUID, mUuid, sizeof(mUuid)); 11379a732c7Sopenharmony_ci if (ret != DM_OK) { 11479a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); 11579a732c7Sopenharmony_ci return ret; 11679a732c7Sopenharmony_ci } 11779a732c7Sopenharmony_ci uuid = reinterpret_cast<char *>(mUuid); 11879a732c7Sopenharmony_ci return ret; 11979a732c7Sopenharmony_ci} 12079a732c7Sopenharmony_ci 12179a732c7Sopenharmony_civoid SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) 12279a732c7Sopenharmony_ci{ 12379a732c7Sopenharmony_ci LOGI("SoftbusCache::SaveDeviceInfo"); 12479a732c7Sopenharmony_ci std::string udid = ""; 12579a732c7Sopenharmony_ci std::string uuid = ""; 12679a732c7Sopenharmony_ci GetUdidByNetworkId(deviceInfo.networkId, udid); 12779a732c7Sopenharmony_ci GetUuidByNetworkId(deviceInfo.networkId, uuid); 12879a732c7Sopenharmony_ci char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; 12979a732c7Sopenharmony_ci if (Crypto::GetUdidHash(udid, reinterpret_cast<uint8_t *>(udidHash)) != DM_OK) { 13079a732c7Sopenharmony_ci LOGE("get udidhash by udid: %{public}s failed.", GetAnonyString(udid).c_str()); 13179a732c7Sopenharmony_ci return; 13279a732c7Sopenharmony_ci } 13379a732c7Sopenharmony_ci if (memcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId), udidHash, 13479a732c7Sopenharmony_ci std::min(sizeof(deviceInfo.deviceId), sizeof(udidHash))) != DM_OK) { 13579a732c7Sopenharmony_ci LOGE("SaveDeviceInfo copy deviceId failed."); 13679a732c7Sopenharmony_ci return; 13779a732c7Sopenharmony_ci } 13879a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 13979a732c7Sopenharmony_ci deviceInfo_[udid] = std::pair<std::string, DmDeviceInfo>(uuid, deviceInfo); 14079a732c7Sopenharmony_ci LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", 14179a732c7Sopenharmony_ci GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); 14279a732c7Sopenharmony_ci} 14379a732c7Sopenharmony_ci 14479a732c7Sopenharmony_civoid SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) 14579a732c7Sopenharmony_ci{ 14679a732c7Sopenharmony_ci LOGI("SoftbusCache::DeleteDeviceInfo networkId %{public}s", 14779a732c7Sopenharmony_ci GetAnonyString(std::string(nodeInfo.networkId)).c_str()); 14879a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 14979a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 15079a732c7Sopenharmony_ci if (std::string(item.second.second.networkId) == std::string(nodeInfo.networkId)) { 15179a732c7Sopenharmony_ci LOGI("DeleteDeviceInfo success udid %{public}s", GetAnonyString(item.first).c_str()); 15279a732c7Sopenharmony_ci deviceInfo_.erase(item.first); 15379a732c7Sopenharmony_ci break; 15479a732c7Sopenharmony_ci } 15579a732c7Sopenharmony_ci } 15679a732c7Sopenharmony_ci} 15779a732c7Sopenharmony_ci 15879a732c7Sopenharmony_civoid SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) 15979a732c7Sopenharmony_ci{ 16079a732c7Sopenharmony_ci LOGI("SoftbusCache::ChangeDeviceInfo"); 16179a732c7Sopenharmony_ci std::string udid = ""; 16279a732c7Sopenharmony_ci GetUdidByNetworkId(deviceInfo.networkId, udid); 16379a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 16479a732c7Sopenharmony_ci if (deviceInfo_.find(udid) != deviceInfo_.end()) { 16579a732c7Sopenharmony_ci if (memcpy_s(deviceInfo_[udid].second.deviceName, sizeof(deviceInfo_[udid].second.deviceName), 16679a732c7Sopenharmony_ci deviceInfo.deviceName, sizeof(deviceInfo.deviceName)) != DM_OK) { 16779a732c7Sopenharmony_ci LOGE("ChangeDeviceInfo deviceInfo copy deviceName failed"); 16879a732c7Sopenharmony_ci } 16979a732c7Sopenharmony_ci if (memcpy_s(deviceInfo_[udid].second.networkId, sizeof(deviceInfo_[udid].second.networkId), 17079a732c7Sopenharmony_ci deviceInfo.networkId, sizeof(deviceInfo.networkId)) != DM_OK) { 17179a732c7Sopenharmony_ci LOGE("ChangeDeviceInfo deviceInfo copy networkId failed"); 17279a732c7Sopenharmony_ci } 17379a732c7Sopenharmony_ci deviceInfo_[udid].second.deviceTypeId = deviceInfo.deviceTypeId; 17479a732c7Sopenharmony_ci std::string uuid = ""; 17579a732c7Sopenharmony_ci GetUuidByNetworkId(deviceInfo.networkId, uuid); 17679a732c7Sopenharmony_ci deviceInfo_[udid].first = uuid; 17779a732c7Sopenharmony_ci } 17879a732c7Sopenharmony_ci LOGI("ChangeDeviceInfo sucess udid %{public}s, networkId %{public}s.", 17979a732c7Sopenharmony_ci GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); 18079a732c7Sopenharmony_ci} 18179a732c7Sopenharmony_ci 18279a732c7Sopenharmony_ciint32_t SoftbusCache::GetDeviceInfoFromCache(std::vector<DmDeviceInfo> &deviceInfoList) 18379a732c7Sopenharmony_ci{ 18479a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 18579a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 18679a732c7Sopenharmony_ci if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { 18779a732c7Sopenharmony_ci continue; 18879a732c7Sopenharmony_ci } 18979a732c7Sopenharmony_ci deviceInfoList.push_back(item.second.second); 19079a732c7Sopenharmony_ci } 19179a732c7Sopenharmony_ci return DM_OK; 19279a732c7Sopenharmony_ci} 19379a732c7Sopenharmony_ci 19479a732c7Sopenharmony_civoid SoftbusCache::UpdateDeviceInfoCache() 19579a732c7Sopenharmony_ci{ 19679a732c7Sopenharmony_ci LOGI("SoftbusCache::UpdateDeviceInfoCache"); 19779a732c7Sopenharmony_ci int32_t deviceCount = 0; 19879a732c7Sopenharmony_ci NodeBasicInfo *nodeInfo = nullptr; 19979a732c7Sopenharmony_ci int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount); 20079a732c7Sopenharmony_ci if (ret != DM_OK) { 20179a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %{public}d.", ret); 20279a732c7Sopenharmony_ci return; 20379a732c7Sopenharmony_ci } 20479a732c7Sopenharmony_ci SaveLocalDeviceInfo(); 20579a732c7Sopenharmony_ci for (int32_t i = 0; i < deviceCount; ++i) { 20679a732c7Sopenharmony_ci NodeBasicInfo *nodeBasicInfo = nodeInfo + i; 20779a732c7Sopenharmony_ci DmDeviceInfo deviceInfo; 20879a732c7Sopenharmony_ci ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); 20979a732c7Sopenharmony_ci SaveDeviceInfo(deviceInfo); 21079a732c7Sopenharmony_ci } 21179a732c7Sopenharmony_ci FreeNodeInfo(nodeInfo); 21279a732c7Sopenharmony_ci LOGI("UpdateDeviceInfoCache success, deviceCount: %{public}d.", deviceCount); 21379a732c7Sopenharmony_ci return; 21479a732c7Sopenharmony_ci} 21579a732c7Sopenharmony_ci 21679a732c7Sopenharmony_ciint32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) 21779a732c7Sopenharmony_ci{ 21879a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 21979a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 22079a732c7Sopenharmony_ci if (std::string(item.second.second.networkId) == std::string(networkId)) { 22179a732c7Sopenharmony_ci udid = item.first; 22279a732c7Sopenharmony_ci LOGI("Get udid from cache success, networkId %{public}s, udid %{public}s.", 22379a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str(), GetAnonyString(udid).c_str()); 22479a732c7Sopenharmony_ci return DM_OK; 22579a732c7Sopenharmony_ci } 22679a732c7Sopenharmony_ci } 22779a732c7Sopenharmony_ci int32_t ret = GetUdidByNetworkId(networkId, udid); 22879a732c7Sopenharmony_ci if (ret == DM_OK) { 22979a732c7Sopenharmony_ci LOGI("Get udid from bus success, networkId %{public}s, udid %{public}s.", 23079a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str(), GetAnonyString(udid).c_str()); 23179a732c7Sopenharmony_ci return DM_OK; 23279a732c7Sopenharmony_ci } 23379a732c7Sopenharmony_ci return ret; 23479a732c7Sopenharmony_ci} 23579a732c7Sopenharmony_ci 23679a732c7Sopenharmony_ciint32_t SoftbusCache::GetUuidFromCache(const char *networkId, std::string &uuid) 23779a732c7Sopenharmony_ci{ 23879a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 23979a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 24079a732c7Sopenharmony_ci if (std::string(item.second.second.networkId) == std::string(networkId)) { 24179a732c7Sopenharmony_ci uuid = item.second.first; 24279a732c7Sopenharmony_ci LOGI("Get uuid from cache success, networkId %{public}s, uuid %{public}s.", 24379a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str(), GetAnonyString(uuid).c_str()); 24479a732c7Sopenharmony_ci return DM_OK; 24579a732c7Sopenharmony_ci } 24679a732c7Sopenharmony_ci } 24779a732c7Sopenharmony_ci int32_t ret = GetUuidByNetworkId(networkId, uuid); 24879a732c7Sopenharmony_ci if (ret == DM_OK) { 24979a732c7Sopenharmony_ci LOGI("Get uuid from bus success, networkId %{public}s, uuid %{public}s.", 25079a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str(), GetAnonyString(uuid).c_str()); 25179a732c7Sopenharmony_ci return DM_OK; 25279a732c7Sopenharmony_ci } 25379a732c7Sopenharmony_ci return ret; 25479a732c7Sopenharmony_ci} 25579a732c7Sopenharmony_ci 25679a732c7Sopenharmony_ciint32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceInfo &devInfo) 25779a732c7Sopenharmony_ci{ 25879a732c7Sopenharmony_ci if (memset_s(&devInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)) != DM_OK) { 25979a732c7Sopenharmony_ci LOGE("ConvertNodeBasicInfoToDmDevice memset_s failed."); 26079a732c7Sopenharmony_ci return ERR_DM_FAILED; 26179a732c7Sopenharmony_ci } 26279a732c7Sopenharmony_ci 26379a732c7Sopenharmony_ci if (memcpy_s(devInfo.networkId, sizeof(devInfo.networkId), nodeInfo.networkId, 26479a732c7Sopenharmony_ci std::min(sizeof(devInfo.networkId), sizeof(nodeInfo.networkId))) != DM_OK) { 26579a732c7Sopenharmony_ci LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed."); 26679a732c7Sopenharmony_ci return ERR_DM_FAILED; 26779a732c7Sopenharmony_ci } 26879a732c7Sopenharmony_ci 26979a732c7Sopenharmony_ci if (memcpy_s(devInfo.deviceName, sizeof(devInfo.deviceName), nodeInfo.deviceName, 27079a732c7Sopenharmony_ci std::min(sizeof(devInfo.deviceName), sizeof(nodeInfo.deviceName))) != DM_OK) { 27179a732c7Sopenharmony_ci LOGE("ConvertNodeBasicInfoToDmDevice copy deviceName data failed."); 27279a732c7Sopenharmony_ci return ERR_DM_FAILED; 27379a732c7Sopenharmony_ci } 27479a732c7Sopenharmony_ci 27579a732c7Sopenharmony_ci devInfo.deviceTypeId = nodeInfo.deviceTypeId; 27679a732c7Sopenharmony_ci nlohmann::json extraJson; 27779a732c7Sopenharmony_ci extraJson[PARAM_KEY_OS_TYPE] = nodeInfo.osType; 27879a732c7Sopenharmony_ci extraJson[PARAM_KEY_OS_VERSION] = ConvertCharArray2String(nodeInfo.osVersion, OS_VERSION_BUF_LEN); 27979a732c7Sopenharmony_ci devInfo.extraData = to_string(extraJson); 28079a732c7Sopenharmony_ci return DM_OK; 28179a732c7Sopenharmony_ci} 28279a732c7Sopenharmony_ci 28379a732c7Sopenharmony_civoid SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) 28479a732c7Sopenharmony_ci{ 28579a732c7Sopenharmony_ci LOGI("SoftbusCache::SaveDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); 28679a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceSecurityLevelMutex_); 28779a732c7Sopenharmony_ci if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { 28879a732c7Sopenharmony_ci return; 28979a732c7Sopenharmony_ci } 29079a732c7Sopenharmony_ci int32_t tempSecurityLevel = -1; 29179a732c7Sopenharmony_ci if (GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_DEVICE_SECURITY_LEVEL, 29279a732c7Sopenharmony_ci reinterpret_cast<uint8_t *>(&tempSecurityLevel), LNN_COMMON_LEN) != DM_OK) { 29379a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed."); 29479a732c7Sopenharmony_ci return; 29579a732c7Sopenharmony_ci } 29679a732c7Sopenharmony_ci deviceSecurityLevel_[std::string(networkId)] = tempSecurityLevel; 29779a732c7Sopenharmony_ci} 29879a732c7Sopenharmony_ci 29979a732c7Sopenharmony_civoid SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) 30079a732c7Sopenharmony_ci{ 30179a732c7Sopenharmony_ci LOGI("SoftbusCache::DeleteDeviceSecurityLevel networkId %{public}s.", 30279a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str()); 30379a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceSecurityLevelMutex_); 30479a732c7Sopenharmony_ci if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { 30579a732c7Sopenharmony_ci deviceSecurityLevel_.erase(std::string(networkId)); 30679a732c7Sopenharmony_ci } 30779a732c7Sopenharmony_ci} 30879a732c7Sopenharmony_ci 30979a732c7Sopenharmony_ciint32_t SoftbusCache::GetSecurityDeviceLevel(const char *networkId, int32_t &securityLevel) 31079a732c7Sopenharmony_ci{ 31179a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceSecurityLevelMutex_); 31279a732c7Sopenharmony_ci for (const auto &item : deviceSecurityLevel_) { 31379a732c7Sopenharmony_ci if (item.first == std::string(networkId)) { 31479a732c7Sopenharmony_ci securityLevel = item.second; 31579a732c7Sopenharmony_ci LOGI("Get dev level from cache success, networkId is %{public}s.", 31679a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str()); 31779a732c7Sopenharmony_ci return DM_OK; 31879a732c7Sopenharmony_ci } 31979a732c7Sopenharmony_ci } 32079a732c7Sopenharmony_ci return GetDevLevelFromBus(networkId, securityLevel); 32179a732c7Sopenharmony_ci} 32279a732c7Sopenharmony_ci 32379a732c7Sopenharmony_ciint32_t SoftbusCache::GetDevLevelFromBus(const char *networkId, int32_t &securityLevel) 32479a732c7Sopenharmony_ci{ 32579a732c7Sopenharmony_ci int32_t tempSecurityLevel = -1; 32679a732c7Sopenharmony_ci if (GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_DEVICE_SECURITY_LEVEL, 32779a732c7Sopenharmony_ci reinterpret_cast<uint8_t *>(&tempSecurityLevel), LNN_COMMON_LEN) != DM_OK) { 32879a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed."); 32979a732c7Sopenharmony_ci return ERR_DM_FAILED; 33079a732c7Sopenharmony_ci } 33179a732c7Sopenharmony_ci securityLevel = tempSecurityLevel; 33279a732c7Sopenharmony_ci deviceSecurityLevel_[std::string(networkId)] = tempSecurityLevel; 33379a732c7Sopenharmony_ci LOGI("Get dev level from softbus success, networkId is %{public}s.", 33479a732c7Sopenharmony_ci GetAnonyString(std::string(networkId)).c_str()); 33579a732c7Sopenharmony_ci return DM_OK; 33679a732c7Sopenharmony_ci} 33779a732c7Sopenharmony_ci 33879a732c7Sopenharmony_ciint32_t SoftbusCache::GetDevInfoByNetworkId(const std::string &networkId, DmDeviceInfo &nodeInfo) 33979a732c7Sopenharmony_ci{ 34079a732c7Sopenharmony_ci { 34179a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 34279a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 34379a732c7Sopenharmony_ci if (std::string(item.second.second.networkId) == networkId) { 34479a732c7Sopenharmony_ci nodeInfo = item.second.second; 34579a732c7Sopenharmony_ci LOGI("GetDevInfoByNetworkId success networkId %{public}s, udid %{public}s.", 34679a732c7Sopenharmony_ci GetAnonyString(networkId).c_str(), GetAnonyString(item.first).c_str()); 34779a732c7Sopenharmony_ci return DM_OK; 34879a732c7Sopenharmony_ci } 34979a732c7Sopenharmony_ci } 35079a732c7Sopenharmony_ci } 35179a732c7Sopenharmony_ci int32_t ret = GetDevInfoFromBus(networkId, nodeInfo); 35279a732c7Sopenharmony_ci if (ret != DM_OK) { 35379a732c7Sopenharmony_ci LOGE("GetDevInfoFromBus failed."); 35479a732c7Sopenharmony_ci return ret; 35579a732c7Sopenharmony_ci } 35679a732c7Sopenharmony_ci SaveDeviceInfo(nodeInfo); 35779a732c7Sopenharmony_ci return DM_OK; 35879a732c7Sopenharmony_ci} 35979a732c7Sopenharmony_ci 36079a732c7Sopenharmony_ciint32_t SoftbusCache::GetDevInfoFromBus(const std::string &networkId, DmDeviceInfo &devInfo) 36179a732c7Sopenharmony_ci{ 36279a732c7Sopenharmony_ci int32_t nodeInfoCount = 0; 36379a732c7Sopenharmony_ci NodeBasicInfo *nodeInfo = nullptr; 36479a732c7Sopenharmony_ci int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &nodeInfoCount); 36579a732c7Sopenharmony_ci if (ret != DM_OK) { 36679a732c7Sopenharmony_ci LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %{public}d.", ret); 36779a732c7Sopenharmony_ci return ret; 36879a732c7Sopenharmony_ci } 36979a732c7Sopenharmony_ci for (int32_t i = 0; i < nodeInfoCount; ++i) { 37079a732c7Sopenharmony_ci NodeBasicInfo *nodeBasicInfo = nodeInfo + i; 37179a732c7Sopenharmony_ci if (networkId == std::string(nodeBasicInfo->networkId)) { 37279a732c7Sopenharmony_ci ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, devInfo); 37379a732c7Sopenharmony_ci break; 37479a732c7Sopenharmony_ci } 37579a732c7Sopenharmony_ci } 37679a732c7Sopenharmony_ci FreeNodeInfo(nodeInfo); 37779a732c7Sopenharmony_ci LOGI("GetDeviceInfo complete, deviceName : %{public}s, deviceTypeId : %{public}d.", 37879a732c7Sopenharmony_ci GetAnonyString(devInfo.deviceName).c_str(), devInfo.deviceTypeId); 37979a732c7Sopenharmony_ci return ret; 38079a732c7Sopenharmony_ci} 38179a732c7Sopenharmony_ci 38279a732c7Sopenharmony_ciint32_t SoftbusCache::GetUdidByUdidHash(const std::string &udidHash, std::string &udid) 38379a732c7Sopenharmony_ci{ 38479a732c7Sopenharmony_ci LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str()); 38579a732c7Sopenharmony_ci { 38679a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 38779a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 38879a732c7Sopenharmony_ci if (std::string(item.second.second.deviceId) == udidHash) { 38979a732c7Sopenharmony_ci udid = item.first; 39079a732c7Sopenharmony_ci LOGI("GetUdidByUdidHash success udid %{public}s.", GetAnonyString(udid).c_str()); 39179a732c7Sopenharmony_ci return DM_OK; 39279a732c7Sopenharmony_ci } 39379a732c7Sopenharmony_ci } 39479a732c7Sopenharmony_ci } 39579a732c7Sopenharmony_ci return ERR_DM_FAILED; 39679a732c7Sopenharmony_ci} 39779a732c7Sopenharmony_ci 39879a732c7Sopenharmony_ciint32_t SoftbusCache::GetUuidByUdid(const std::string &udid, std::string &uuid) 39979a732c7Sopenharmony_ci{ 40079a732c7Sopenharmony_ci LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); 40179a732c7Sopenharmony_ci { 40279a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 40379a732c7Sopenharmony_ci if (deviceInfo_.find(udid) != deviceInfo_.end()) { 40479a732c7Sopenharmony_ci uuid = deviceInfo_[udid].first; 40579a732c7Sopenharmony_ci LOGI("success uuid %{public}s.", GetAnonyString(uuid).c_str()); 40679a732c7Sopenharmony_ci return DM_OK; 40779a732c7Sopenharmony_ci } 40879a732c7Sopenharmony_ci } 40979a732c7Sopenharmony_ci return ERR_DM_FAILED; 41079a732c7Sopenharmony_ci} 41179a732c7Sopenharmony_ci 41279a732c7Sopenharmony_ciint32_t SoftbusCache::GetNetworkIdFromCache(const std::string &udid, std::string &networkId) 41379a732c7Sopenharmony_ci{ 41479a732c7Sopenharmony_ci LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); 41579a732c7Sopenharmony_ci { 41679a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 41779a732c7Sopenharmony_ci if (deviceInfo_.find(udid) != deviceInfo_.end()) { 41879a732c7Sopenharmony_ci networkId = deviceInfo_[udid].second.networkId; 41979a732c7Sopenharmony_ci LOGI("GetNetworkIdFromCache success networkId %{public}s, udid %{public}s.", 42079a732c7Sopenharmony_ci GetAnonyString(networkId).c_str(), GetAnonyString(udid).c_str()); 42179a732c7Sopenharmony_ci return DM_OK; 42279a732c7Sopenharmony_ci } 42379a732c7Sopenharmony_ci } 42479a732c7Sopenharmony_ci return ERR_DM_FAILED; 42579a732c7Sopenharmony_ci} 42679a732c7Sopenharmony_ci 42779a732c7Sopenharmony_cibool SoftbusCache::CheckIsOnline(const std::string &deviceId) 42879a732c7Sopenharmony_ci{ 42979a732c7Sopenharmony_ci LOGI("deviceId %{public}s.", GetAnonyString(deviceId).c_str()); 43079a732c7Sopenharmony_ci { 43179a732c7Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(deviceInfosMutex_); 43279a732c7Sopenharmony_ci for (const auto &item : deviceInfo_) { 43379a732c7Sopenharmony_ci if (std::string(item.second.second.deviceId) == deviceId) { 43479a732c7Sopenharmony_ci LOGI("CheckIsOnline is true."); 43579a732c7Sopenharmony_ci return true; 43679a732c7Sopenharmony_ci } 43779a732c7Sopenharmony_ci } 43879a732c7Sopenharmony_ci } 43979a732c7Sopenharmony_ci return false; 44079a732c7Sopenharmony_ci} 44179a732c7Sopenharmony_ci} // namespace DistributedHardware 44279a732c7Sopenharmony_ci} // namespace OHOS 443