diff --git a/BUILD.gn b/BUILD.gn index a31bbc435525080ab48f4d7c9b4a5bd694ce4000..71aed68449e47af4cf6194b7532a0c41993ef8a9 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -32,6 +32,7 @@ if (defined(ohos_lite)) { "services/implementation:devicemanagerserviceimpl", "interfaces/inner_kits/native_cpp:devicemanagersdk", "test/smallunittest:lite_devicemanager_test", + "services/softbuscache:dmdevicecache", ] } lite_component("device_manager_fwk") { @@ -53,6 +54,7 @@ if (defined(ohos_lite)) { "services/etc:ohos.para.dac", "services/implementation:devicemanagerserviceimpl", "services/service:devicemanagerservice", + "services/softbuscache:dmdevicecache", ] } group("device_manager_fwk") { diff --git a/device_manager.gni b/device_manager.gni index 9738706cdf462872f898b026041716917e9992c2..1a9bdad7281299c225c4c678d67f44abee85dde5 100644 --- a/device_manager.gni +++ b/device_manager.gni @@ -18,7 +18,11 @@ common_path = "${devicemanager_path}/common" utils_path = "${devicemanager_path}/utils" services_path = "${devicemanager_path}/services/service" servicesimpl_path = "${devicemanager_path}/services/implementation" +softbuscache_parh = "${devicemanager_path}/services/softbuscache" innerkits_path = "${devicemanager_path}/interfaces/inner_kits" +hilog_path = "//base/hiviewdfx/hilog_lite/frameworks/featured" +third_path = "//third_party/bounds_checking_function" +dsoftbussdk_path = "//foundation/communication/dsoftbus/sdk" ext_path = "${devicemanager_path}/ext" ability_runtime_innerkits_path = "//foundation/ability/ability_runtime/interfaces/inner_api" diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 26cfd5c8736e6675a498b377b55b8074f89fdd97..dab1be9c68a16fd0ec4073bc89298ee50e5b1e69 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -50,6 +50,7 @@ if (defined(ohos_lite)) { "//foundation/distributedshedule/samgr_lite/interfaces/kits/samgr", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", "//commonlibrary/c_utils/base/include", + "${softbuscache_parh}/include", ] sources = [ @@ -74,6 +75,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/radar:devicemanagerradar", + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//base/security/device_auth/services:deviceauth_sdk", @@ -137,6 +139,7 @@ if (defined(ohos_lite)) { "${utils_path}/include/timer", "${services_path}/include", "${services_path}/include/ipc/standard", + "${softbuscache_parh}/include", ] } @@ -189,6 +192,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency:devicemanagerdependency", "${devicemanager_path}/radar:devicemanagerradar", "${innerkits_path}/native_cpp:devicemanagersdk", + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", ] diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index e5aea0ed5707a353ae3e521aa5e5fdb4910fee09..59790ad29a322b31ae513281543c6ca17170e463 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -24,6 +24,7 @@ #include "dm_device_info.h" #include "dm_log.h" #include "dm_radar_helper.h" +#include "dm_softbus_cache.h" #include "nlohmann/json.hpp" #include "parameter.h" #include "system_ability_definition.h" @@ -264,27 +265,13 @@ void SoftbusConnector::JoinLnn(const std::string &deviceId) int32_t SoftbusConnector::GetUdidByNetworkId(const char *networkId, std::string &udid) { LOGI("start, networkId: %{public}s.", GetAnonyString(std::string(networkId)).c_str()); - uint8_t tmpUdid[UDID_BUF_LEN] = {0}; - int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UDID, tmpUdid, sizeof(tmpUdid)); - if (ret != DM_OK) { - LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - udid = reinterpret_cast(tmpUdid); - return ret; + return SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid); } int32_t SoftbusConnector::GetUuidByNetworkId(const char *networkId, std::string &uuid) { LOGI("start, networkId: %{public}s.", GetAnonyString(std::string(networkId)).c_str()); - uint8_t tmpUuid[UUID_BUF_LEN] = {0}; - int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UUID, tmpUuid, sizeof(tmpUuid)); - if (ret != DM_OK) { - LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - uuid = reinterpret_cast(tmpUuid); - return ret; + return SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 700aef00f112c4a1de03659b183d32841bf87ddc..de93f67611e4140ca8a09b945806fb67850846b8 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -52,6 +52,7 @@ if (defined(ohos_lite)) { "//foundation/distributedshedule/samgr_lite/interfaces/kits/samgr", "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include", "${devicemanager_path}/radar/include", + "${softbuscache_parh}/include", ] sources = [ @@ -81,6 +82,7 @@ if (defined(ohos_lite)) { ] deps = [ + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", "//base/security/device_auth/services:deviceauth_sdk", "//base/startup/init/interfaces/innerkits:libbegetutil", @@ -129,6 +131,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency/include", "${devicemanager_path}/radar/include", "${innerkits_path}/native_cpp/include", + "${softbuscache_parh}/include", "${utils_path}/include/appInfo/standard", "${utils_path}/include/crypto", "${utils_path}/include/timer", @@ -178,6 +181,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/commondependency:devicemanagerdependency", + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", ] diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 57cf5a776031c261fa18815946f3ef5df3944cad..a38eb001bc797b18919dc02dc5a3b9f00745b042 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -73,7 +73,7 @@ public: int32_t GetLocalDeviceType(int32_t &deviceType); int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info); int32_t ShiftLNNGear(); - int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); + static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); int32_t GetDeviceSecurityLevel(const char *networkId, int32_t &securityLevel); int32_t GetNetworkTypeByNetworkId(const char *networkId, int32_t &networkType); int32_t RefreshSoftbusLNN(const char *pkgName, const DmSubscribeInfo &dmSubInfo, const std::string &customData); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 6bebde7b534d3185de5d0e88d9a318c47f8ac6f4..244c7cb054aaab3f01406af68e8b1bfe7cafd53d 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -24,9 +24,9 @@ #include "dm_crypto.h" #include "dm_hidumper.h" #include "dm_log.h" +#include "dm_softbus_cache.h" #include "parameter.h" #include "permission_manager.h" - #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "common_event_support.h" #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) @@ -69,6 +69,7 @@ int32_t DeviceManagerService::InitSoftbusListener() if (softbusListener_ == nullptr) { softbusListener_ = std::make_shared(); } + SoftbusCache::GetInstance().UpdateDeviceInfoCache(); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) SubscribePublishCommonEvent(); @@ -177,11 +178,6 @@ int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, c std::string udid = ""; SoftbusListener::GetUdidByNetworkId(item.networkId, udid); if (udidMap.find(udid) != udidMap.end()) { - std::string deviceIdHash = ""; - dmServiceImpl_->GetUdidHashByNetWorkId(item.networkId, deviceIdHash); - if (memcpy_s(item.deviceId, DM_MAX_DEVICE_ID_LEN, deviceIdHash.c_str(), deviceIdHash.length()) != 0) { - LOGE("get deviceId: %{public}s failed", GetAnonyString(deviceIdHash).c_str()); - } item.authForm = udidMap[udid].first; item.extraData = udidMap[udid].second; deviceList.push_back(item); @@ -370,7 +366,7 @@ int32_t DeviceManagerService::GetUuidByNetworkId(const std::string &pkgName, con GetAnonyString(netWorkId).c_str()); return ERR_DM_INPUT_PARA_INVALID; } - softbusListener_->GetUuidByNetworkId(netWorkId.c_str(), uuid); + SoftbusListener::GetUuidByNetworkId(netWorkId.c_str(), uuid); return DM_OK; } diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index f02bfb25767e5f89094f0bfeae287b6b42ec3c49..08ea42fffedf04416253657be4de5a1077a8a5b0 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -24,9 +24,11 @@ #include "device_manager_service.h" #include "dm_anonymous.h" +#include "dm_crypto.h" #include "dm_constants.h" #include "dm_device_info.h" #include "dm_log.h" +#include "dm_softbus_cache.h" #include "parameter.h" #include "system_ability_definition.h" #include "softbus_adapter.cpp" @@ -149,6 +151,9 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); + SoftbusCache::GetInstance().SaveDeviceInfo(dmDeviceInfo); + SoftbusCache::GetInstance().SaveDeviceSecurityLevel(dmDeviceInfo.networkId); + SoftbusCache::GetInstance().SaveLocalDeviceInfo(); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); if (ret != DM_OK) { @@ -186,6 +191,9 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); + SoftbusCache::GetInstance().DeleteDeviceInfo(dmDeviceInfo); + SoftbusCache::GetInstance().DeleteDeviceSecurityLevel(dmDeviceInfo.networkId); + SoftbusCache::GetInstance().DeleteLocalDeviceInfo(); LOGI("device offline networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); std::thread deviceOffLine(DeviceOffLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOffLine.native_handle(), DEVICE_OFFLINE); @@ -236,6 +244,7 @@ void SoftbusListener::OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBas ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device changed networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); dmDeviceInfo.networkType = networkType; + SoftbusCache::GetInstance().ChangeDeviceInfo(dmDeviceInfo); std::thread deviceInfoChange(DeviceNameChange, dmDeviceInfo); if (pthread_setname_np(deviceInfoChange.native_handle(), DEVICE_NAME_CHANGE) != DM_OK) { LOGE("DeviceNameChange setname failed."); @@ -501,33 +510,14 @@ int32_t SoftbusListener::UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName) int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) { + int32_t ret = SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList); static int32_t radarDeviceCount = 0; - int32_t deviceCount = 0; - NodeBasicInfo *nodeInfo = nullptr; - int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount); + int32_t deviceCount = deviceInfoList.size(); char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); struct RadarInfo radarInfo = { .localUdid = std::string(localDeviceId), }; - if (ret != DM_OK) { - radarInfo.stageRes = static_cast(StageRes::STAGE_FAIL); - radarInfo.errCode = ERR_DM_FAILED; - radarInfo.discoverDevList = ""; - if (IsDmRadarHelperReady() && GetDmRadarHelperObj() != nullptr) { - if (!GetDmRadarHelperObj()->ReportGetTrustDeviceList(radarInfo)) { - LOGE("ReportGetTrustDeviceList failed"); - } - } - LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - for (int32_t i = 0; i < deviceCount; ++i) { - NodeBasicInfo *nodeBasicInfo = nodeInfo + i; - DmDeviceInfo deviceInfo; - ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); - deviceInfoList.push_back(deviceInfo); - } radarInfo.stageRes = static_cast(StageRes::STAGE_SUCC); if (radarDeviceCount != deviceCount && deviceCount > 0 && IsDmRadarHelperReady() && GetDmRadarHelperObj() != nullptr) { @@ -537,8 +527,7 @@ int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceI LOGE("ReportGetTrustDeviceList failed"); } } - FreeNodeInfo(nodeInfo); - LOGI("GetTrustDevices success, deviceCount: %{public}d.", deviceCount); + LOGI("GetTrustedDeviceList success from cache deviceInfoList size is %{public}d.", deviceCount); return ret; } @@ -591,14 +580,7 @@ int32_t SoftbusListener::GetDeviceInfo(const std::string &networkId, DmDeviceInf int32_t SoftbusListener::GetLocalDeviceInfo(DmDeviceInfo &deviceInfo) { - NodeBasicInfo nodeBasicInfo; - int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); - if (ret != DM_OK) { - LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, deviceInfo); - return ret; + return SoftbusCache::GetInstance().GetLocalDeviceInfo(deviceInfo); } int32_t SoftbusListener::GetLocalDeviceNetworkId(std::string &networkId) @@ -639,26 +621,12 @@ int32_t SoftbusListener::GetLocalDeviceType(int32_t &deviceType) int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string &udid) { - uint8_t mUdid[UDID_BUF_LEN] = {0}; - int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, sizeof(mUdid)); - if (ret != DM_OK) { - LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - udid = reinterpret_cast(mUdid); - return ret; + return SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid); } int32_t SoftbusListener::GetUuidByNetworkId(const char *networkId, std::string &uuid) { - uint8_t mUuid[UUID_BUF_LEN] = {0}; - int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UUID, mUuid, sizeof(mUuid)); - if (ret != DM_OK) { - LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); - return ERR_DM_FAILED; - } - uuid = reinterpret_cast(mUuid); - return ret; + return SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid); } int32_t SoftbusListener::ShiftLNNGear() @@ -796,15 +764,7 @@ int32_t SoftbusListener::GetNetworkTypeByNetworkId(const char *networkId, int32_ int32_t SoftbusListener::GetDeviceSecurityLevel(const char *networkId, int32_t &securityLevel) { - int32_t tempSecurityLevel = -1; - if (GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_DEVICE_SECURITY_LEVEL, - reinterpret_cast(&tempSecurityLevel), LNN_COMMON_LEN) != DM_OK) { - LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed."); - return ERR_DM_FAILED; - } - securityLevel = tempSecurityLevel; - LOGI("GetDeviceSecurityLevel success, securityLevel = %{public}d.", securityLevel); - return DM_OK; + return SoftbusCache::GetInstance().GetSecurityDeviceLevel(networkId, securityLevel); } void SoftbusListener::CacheDiscoveredDevice(const DeviceInfo *device) diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4cdcad1db4a38c0b6a48baed98cb94fb3c44cdaa --- /dev/null +++ b/services/softbuscache/BUILD.gn @@ -0,0 +1,90 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") +} else { + import("//build/ohos.gni") +} +import("//foundation/distributedhardware/device_manager/device_manager.gni") +if (defined(ohos_lite)) { + if (ohos_kernel_type == "linux") { + shared_library("dmdevicecache") { + include_dirs = [ + "include", + "${common_path}/include", + "${innerkits_path}/native_cpp/include", + "${utils_path}/include/crypto", + "//third_party/json/include", + ] + + sources = [ + "${common_path}/src/dm_anonymous.cpp", + "src/dm_softbus_cache.cpp", + ] + + defines = [ + "LITE_DEVICE", + "DH_LOG_TAG=\"dmdevicecache\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [ + "${dsoftbussdk_path}:softbus_client", + "${hilog_path}:hilog_shared", + "${third_path}:libsec_shared", + "${utils_path}:devicemanagerutils", + ] + } + } +} else { + ohos_shared_library("dmdevicecache") { + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + + include_dirs = [ + "include", + "${common_path}/include", + "${utils_path}/include/crypto", + ] + + sources = [ + "${common_path}/src/dm_anonymous.cpp", + "src/dm_softbus_cache.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dmdevicecache\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [ "${utils_path}:devicemanagerutils" ] + + external_deps = [ + "bounds_checking_function:libsec_shared", + "dsoftbus:softbus_client", + "hilog:libhilog", + ] + + subsystem_name = "distributedhardware" + + part_name = "device_manager" + } +} diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h new file mode 100644 index 0000000000000000000000000000000000000000..34b7dc6434e436b998dc22c74c44961789ebef34 --- /dev/null +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_SOFTBUS_CACHE_H +#define OHOS_DM_SOFTBUS_CACHE_H + +#include +#include +#include +#include "dm_device_info.h" +#include "single_instance.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" + +namespace OHOS { +namespace DistributedHardware { +class SoftbusCache { + DECLARE_SINGLE_INSTANCE(SoftbusCache); + +public: + void SaveDeviceInfo(DmDeviceInfo deviceInfo); + void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo); + void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); + void SaveDeviceSecurityLevel(const char *networkId); + void DeleteDeviceSecurityLevel(const char *networkId); + int32_t GetDeviceInfoFromCache(std::vector &deviceInfoList); + int32_t GetUdidFromCache(const char *networkId, std::string &udid); + int32_t GetUuidFromCache(const char *networkId, std::string &uuid); + int32_t GetSecurityDeviceLevel(const char *networkId, int32_t &securityLevel); + void UpdateDeviceInfoCache(); + void SaveLocalDeviceInfo(); + void DeleteLocalDeviceInfo(); + int32_t GetLocalDeviceInfo(DmDeviceInfo &nodeInfo); +private: + int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); + int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); + int32_t ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceInfo &devInfo); + +private: + std::mutex deviceInfosMutex_; + std::mutex deviceSecurityLevelMutex_; + std::unordered_map> deviceInfo_; + std::unordered_map deviceSecurityLevel_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_PERMISSION_STANDARD_PERMISSION_MANAGER_H diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd8539f14ddabd111aeb4508b8d11e67023a869 --- /dev/null +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -0,0 +1,281 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dm_softbus_cache.h" +#include "dm_anonymous.h" +#include "dm_crypto.h" +#include "dm_constants.h" +#include "dm_device_info.h" +#include "dm_log.h" +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); +bool g_online = false; +bool g_getLocalDevInfo = false; +DmDeviceInfo localDeviceInfo_; +int32_t g_onlinDeviceNum = 0; +void SoftbusCache::SaveLocalDeviceInfo() +{ + if (g_online) { + return; + } + NodeBasicInfo nodeBasicInfo; + int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); + if (ret != DM_OK) { + LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); + return; + } + ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); + LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", + GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); + SaveDeviceInfo(localDeviceInfo_); + g_online = true; + g_getLocalDevInfo = true; +} + +void SoftbusCache::DeleteLocalDeviceInfo() +{ + LOGI("SoftbusCache::DeleteLocalDeviceInfo networkid %{public}s.", + GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); + std::lock_guard mutexLock(deviceInfosMutex_); + if (g_onlinDeviceNum == 0) { + g_online = false; + g_getLocalDevInfo = false; + } +} + +int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) +{ + if (g_getLocalDevInfo) { + nodeInfo = localDeviceInfo_; + return DM_OK; + } + NodeBasicInfo nodeBasicInfo; + int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo); + if (ret != DM_OK) { + LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %{public}d.", ret); + return ERR_DM_FAILED; + } + ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); + SaveDeviceInfo(localDeviceInfo_); + g_getLocalDevInfo = true; + return DM_OK; +} + +int32_t SoftbusCache::GetUdidByNetworkId(const char *networkId, std::string &udid) +{ + uint8_t mUdid[UDID_BUF_LEN] = {0}; + int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, sizeof(mUdid)); + if (ret != DM_OK) { + LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); + return ERR_DM_FAILED; + } + udid = reinterpret_cast(mUdid); + return ret; +} + +int32_t SoftbusCache::GetUuidByNetworkId(const char *networkId, std::string &uuid) +{ + uint8_t mUuid[UUID_BUF_LEN] = {0}; + int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UUID, mUuid, sizeof(mUuid)); + if (ret != DM_OK) { + LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %{public}d.", ret); + return ERR_DM_FAILED; + } + uuid = reinterpret_cast(mUuid); + return ret; +} + +void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) +{ + LOGI("SoftbusCache::SaveDeviceInfo"); + std::string udid = ""; + std::string uuid = ""; + GetUdidByNetworkId(deviceInfo.networkId, udid); + GetUuidByNetworkId(deviceInfo.networkId, uuid); + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + if (Crypto::GetUdidHash(udid, reinterpret_cast(udidHash)) != DM_OK) { + LOGE("get udidhash by udid: %{public}s failed.", GetAnonyString(udid).c_str()); + return; + } + if (memcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId), udidHash, + std::min(sizeof(deviceInfo.deviceId), sizeof(udidHash))) != DM_OK) { + LOGE("SaveDeviceInfo copy deviceId failed."); + return; + } + std::lock_guard mutexLock(deviceInfosMutex_); + deviceInfo_[udid] = std::pair(uuid, deviceInfo); + g_onlinDeviceNum++; + LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", + GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); +} + +void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) +{ + LOGI("SoftbusCache::DeleteDeviceInfo networkId %{public}s", + GetAnonyString(std::string(nodeInfo.networkId)).c_str()); + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(nodeInfo.networkId)) { + LOGI("DeleteDeviceInfo success udid %{public}s", GetAnonyString(item.first).c_str()); + deviceInfo_.erase(item.first); + g_onlinDeviceNum--; + } + } +} + +void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) +{ + LOGI("SoftbusCache::ChangeDeviceInfo"); + std::string udid = ""; + GetUdidByNetworkId(deviceInfo.networkId, udid); + std::lock_guard mutexLock(deviceInfosMutex_); + if (deviceInfo_.find(udid) != deviceInfo_.end()) { + if (memcpy_s(deviceInfo_[udid].second.deviceName, sizeof(deviceInfo_[udid].second.deviceName), + deviceInfo.deviceName, sizeof(deviceInfo.deviceName)) != DM_OK) { + LOGE("ChangeDeviceInfo deviceInfo copy deviceName failed"); + } + if (memcpy_s(deviceInfo_[udid].second.networkId, sizeof(deviceInfo_[udid].second.networkId), + deviceInfo.networkId, sizeof(deviceInfo.networkId)) != DM_OK) { + LOGE("ChangeDeviceInfo deviceInfo copy networkId failed"); + } + deviceInfo_[udid].second.deviceTypeId = deviceInfo.deviceTypeId; + } + LOGI("ChangeDeviceInfo sucess udid %{public}s, networkId %{public}s.", + GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); +} + +int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceInfoList) +{ + LOGI("SoftbusCache::GetDeviceInfoFromCache."); + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + deviceInfoList.push_back(item.second.second); + } + return DM_OK; +} + +void SoftbusCache::UpdateDeviceInfoCache() +{ + LOGI("SoftbusCache::UpdateDeviceInfoCache"); + int32_t deviceCount = 0; + NodeBasicInfo *nodeInfo = nullptr; + int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount); + if (ret != DM_OK) { + LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %{public}d.", ret); + return; + } + if (deviceCount != 0) { + SaveLocalDeviceInfo(); + } + for (int32_t i = 0; i < deviceCount; ++i) { + NodeBasicInfo *nodeBasicInfo = nodeInfo + i; + DmDeviceInfo deviceInfo; + ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); + SaveDeviceInfo(deviceInfo); + } + FreeNodeInfo(nodeInfo); + LOGI("UpdateDeviceInfoCache success, deviceCount: %{public}d.", deviceCount); + return; +} + +int32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) +{ + LOGI("SoftbusCache::GetUdidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + udid = item.first; + LOGI("GetUdidFromCache success udid %{public}s.", GetAnonyString(udid).c_str()); + return DM_OK; + } + } + return ERR_DM_FAILED; +} + +int32_t SoftbusCache::GetUuidFromCache(const char *networkId, std::string &uuid) +{ + LOGI("SoftbusCache::GetUuidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + uuid = item.second.first; + LOGI("GetUuidFromCache success uuid %{public}s.", GetAnonyString(uuid).c_str()); + return DM_OK; + } + } + return ERR_DM_FAILED; +} + +int32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceInfo &devInfo) +{ + (void)memset_s(&devInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)); + if (memcpy_s(devInfo.networkId, sizeof(devInfo.networkId), nodeInfo.networkId, + std::min(sizeof(devInfo.networkId), sizeof(nodeInfo.networkId))) != DM_OK) { + LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed."); + } + + if (memcpy_s(devInfo.deviceName, sizeof(devInfo.deviceName), nodeInfo.deviceName, + std::min(sizeof(devInfo.deviceName), sizeof(nodeInfo.deviceName))) != DM_OK) { + LOGE("ConvertNodeBasicInfoToDmDevice copy deviceName data failed."); + } + devInfo.deviceTypeId = nodeInfo.deviceTypeId; + nlohmann::json extraJson; + extraJson[PARAM_KEY_OS_TYPE] = nodeInfo.osType; + extraJson[PARAM_KEY_OS_VERSION] = std::string(nodeInfo.osVersion); + devInfo.extraData = to_string(extraJson); + return DM_OK; +} + +void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) +{ + LOGI("SoftbusCache::SaveDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceSecurityLevelMutex_); + if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { + return; + } + int32_t tempSecurityLevel = -1; + if (GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_DEVICE_SECURITY_LEVEL, + reinterpret_cast(&tempSecurityLevel), LNN_COMMON_LEN) != DM_OK) { + LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed."); + return; + } + deviceSecurityLevel_[std::string(networkId)] = tempSecurityLevel; +} + +void SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) +{ + LOGI("SoftbusCache::DeleteDeviceSecurityLevel networkId %{public}s.", + GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceSecurityLevelMutex_); + if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { + deviceSecurityLevel_.erase(std::string(networkId)); + } +} + +int32_t SoftbusCache::GetSecurityDeviceLevel(const char *networkId, int32_t &securityLevel) +{ + LOGI("SoftbusCache::GetSecurityDeviceLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceSecurityLevelMutex_); + for (const auto &item : deviceSecurityLevel_) { + if (item.first == std::string(networkId)) { + securityLevel = item.second; + return DM_OK; + } + } + return ERR_DM_FAILED; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 059690c275aaf73adb232316e0ba3e5d8647ff0c..fbc9cf031b6a204583f60529b4b30e39816cb872 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1057,6 +1057,7 @@ ohos_unittest("UTTest_dm_publish_manager") { "${servicesimpl_path}/src/dependency/softbus/softbus_connector.cpp", "${servicesimpl_path}/src/dependency/softbus/softbus_session.cpp", "${servicesimpl_path}/src/publish/dm_publish_manager.cpp", + "${softbuscache_parh}/src/dm_softbus_cache.cpp", ] deps = [ ":device_manager_test_common" ] @@ -1161,6 +1162,7 @@ ohos_static_library("device_manager_test_common") { "${innerkits_path}/native_cpp:devicemanagersdk", "${services_path}:devicemanagerservice", "${servicesimpl_path}:devicemanagerserviceimpl", + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", "//third_party/googletest:gmock", ]