From a9300adb173457e0a0b9b22e92fb1c6f508460db Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 12:18:31 +0800 Subject: [PATCH 01/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../include/softbus/softbus_listener.h | 9 +- .../service/src/device_manager_service.cpp | 8 +- .../service/src/softbus/softbus_listener.cpp | 132 ++++++++++++++++++ 3 files changed, 142 insertions(+), 7 deletions(-) diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 57cf5a776..4cbe09d41 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); @@ -84,6 +84,7 @@ public: int32_t RegisterSoftbusLnnOpsCbk(const std::string &pkgName, const std::shared_ptr callback); int32_t UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName); + void UpdateDeviceInfoCache(); static IDmRadarHelper* GetDmRadarHelperObj(); static bool IsDmRadarHelperReady(); static bool CloseDmRadarHelperObj(std::string name); @@ -95,6 +96,12 @@ public: ConnectionAddrType &addrType); private: int32_t InitSoftPublishLNN(); + static void SaveDeviceInfo(DmDeviceInfo deviceInfo); + static void DeleteDeviceInfo(const NodeBasicInfo &nodeInfo); + static void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); + static void GetDeviceInfoFromCache(std::vector &deviceInfoList); + static int32_t GetUdidFromCache(const char *networkId, std::string &udid); + static int32_t GetUuidFromCache(const char *networkId, std::string &uuid); private: static std::string hostName_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 6bebde7b5..589f36cf6 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -68,6 +68,7 @@ int32_t DeviceManagerService::InitSoftbusListener() { if (softbusListener_ == nullptr) { softbusListener_ = std::make_shared(); + softbusListener_->UpdateDeviceInfoCache(); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) @@ -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 f02bfb257..736640a48 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -24,6 +24,7 @@ #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" @@ -60,6 +61,8 @@ IDmRadarHelper* SoftbusListener::dmRadarHelper_ = nullptr; std::shared_ptr SoftbusListener::timer_ = std::make_shared(); void* SoftbusListener::radarHandle_ = nullptr; std::string SoftbusListener::hostName_ = ""; +static std::mutex deviceInfosMutex_; +static std::unordered_map> deviceInfo_; static int OnSessionOpened(int sessionId, int result) { @@ -149,6 +152,7 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); + SaveDeviceInfo(dmDeviceInfo); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); if (ret != DM_OK) { @@ -186,6 +190,7 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); + DeleteDeviceInfo(dmDeviceInfo); 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 +241,7 @@ void SoftbusListener::OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBas ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device changed networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); dmDeviceInfo.networkType = networkType; + ChangeDeviceInfo(dmDeviceInfo); std::thread deviceInfoChange(DeviceNameChange, dmDeviceInfo); if (pthread_setname_np(deviceInfoChange.native_handle(), DEVICE_NAME_CHANGE) != DM_OK) { LOGE("DeviceNameChange setname failed."); @@ -501,6 +507,10 @@ int32_t SoftbusListener::UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName) int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) { + if (!deviceInfo_.empty()) { + GetDeviceInfoFromCache(deviceInfoList); + return DM_OK ; + } static int32_t radarDeviceCount = 0; int32_t deviceCount = 0; NodeBasicInfo *nodeInfo = nullptr; @@ -526,6 +536,7 @@ int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceI NodeBasicInfo *nodeBasicInfo = nodeInfo + i; DmDeviceInfo deviceInfo; ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); + SaveDeviceInfo(deviceInfo); deviceInfoList.push_back(deviceInfo); } radarInfo.stageRes = static_cast(StageRes::STAGE_SUCC); @@ -639,6 +650,9 @@ int32_t SoftbusListener::GetLocalDeviceType(int32_t &deviceType) int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string &udid) { + if (!deviceInfo_.empty() && GetUdidFromCache(networkId, udid) == DM_OK) { + return DM_OK; + } 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) { @@ -651,6 +665,9 @@ int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string & int32_t SoftbusListener::GetUuidByNetworkId(const char *networkId, std::string &uuid) { + if (!deviceInfo_.empty() && GetUuidFromCache(networkId, uuid) == DM_OK) { + return DM_OK; + } 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) { @@ -1019,6 +1036,121 @@ int32_t SoftbusListener::GetIPAddrTypeFromCache(const std::string &deviceId, con return ERR_DM_BIND_INPUT_PARA_INVALID; } +void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) +{ + LOGI("SoftbusListener::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); + } +} + +void SoftbusListener::DeleteDeviceInfo(const NodeBasicInfo &nodeInfo) +{ + LOGI("SoftbusListener::DeleteDeviceInfo"); + std::string udid = ""; + GetUdidByNetworkId(nodeInfo.networkId, udid); + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (deviceInfo_.find(udid) != deviceInfo_.end()) { + deviceInfo_.erase(udid); + } + } +} + +void SoftbusListener::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) +{ + LOGI("SoftbusListener::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; + } + } +} + +void SoftbusListener::GetDeviceInfoFromCache(std::vector &deviceInfoList) +{ + LOGI("SoftbusListener::GetDeviceInfoFromCache"); + std::lock_guard mutexLock(deviceInfosMutex_); + { + for (const auto &item : deviceInfo_) { + deviceInfoList.push_back(item.second.second); + } + } +} + +void SoftbusListener::UpdateDeviceInfoCache() +{ + LOGI("SoftbusListener::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; + } + 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 SoftbusListener::GetUdidFromCache(const char *networkId, std::string &udid) +{ + LOGI("SoftbusListener::GetUdidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + udid = item.first; + return DM_OK; + } + } + LOGI("SoftbusListener::GetUdidFromCache success udid%{public}s", GetAnonyString(udid).c_str()); + return ERR_DM_FAILED; +} +int32_t SoftbusListener::GetUuidFromCache(const char *networkId, std::string &uuid) +{ + LOGI("SoftbusListener::GetUuidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + uuid = item.second.first; + return DM_OK; + } + } + LOGI("SoftbusListener::GetUuidFromCache success uuid%{public}s", GetAnonyString(uuid).c_str()); + return ERR_DM_FAILED; +} + IRefreshCallback &SoftbusListener::GetSoftbusRefreshCb() { return softbusRefreshCallback_; -- Gitee From 59e6c4dc271457636b470b735544dfa5df8a861d Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 12:26:44 +0800 Subject: [PATCH 02/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/include/softbus/softbus_listener.h | 2 +- services/service/src/softbus/softbus_listener.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 4cbe09d41..07d9135eb 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -97,7 +97,7 @@ public: private: int32_t InitSoftPublishLNN(); static void SaveDeviceInfo(DmDeviceInfo deviceInfo); - static void DeleteDeviceInfo(const NodeBasicInfo &nodeInfo); + static void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo); static void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); static void GetDeviceInfoFromCache(std::vector &deviceInfoList); static int32_t GetUdidFromCache(const char *networkId, std::string &udid); diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 736640a48..2638f3aca 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1059,7 +1059,7 @@ void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) } } -void SoftbusListener::DeleteDeviceInfo(const NodeBasicInfo &nodeInfo) +void SoftbusListener::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { LOGI("SoftbusListener::DeleteDeviceInfo"); std::string udid = ""; -- Gitee From 72e0a9b26a3885abce48d3c547f9dd4ca89d513b Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 12:28:14 +0800 Subject: [PATCH 03/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/softbus/softbus_listener.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 2638f3aca..b8af3c122 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1036,7 +1036,7 @@ int32_t SoftbusListener::GetIPAddrTypeFromCache(const std::string &deviceId, con return ERR_DM_BIND_INPUT_PARA_INVALID; } -void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) +void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusListener::SaveDeviceInfo"); std::string udid = ""; @@ -1123,7 +1123,6 @@ void SoftbusListener::UpdateDeviceInfoCache() FreeNodeInfo(nodeInfo); LOGI("UpdateDeviceInfoCache success, deviceCount: %{public}d.", deviceCount); return; - } int32_t SoftbusListener::GetUdidFromCache(const char *networkId, std::string &udid) @@ -1138,6 +1137,7 @@ int32_t SoftbusListener::GetUdidFromCache(const char *networkId, std::string &ud LOGI("SoftbusListener::GetUdidFromCache success udid%{public}s", GetAnonyString(udid).c_str()); return ERR_DM_FAILED; } + int32_t SoftbusListener::GetUuidFromCache(const char *networkId, std::string &uuid) { LOGI("SoftbusListener::GetUuidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); -- Gitee From 84270503fdbe754612879315c054c497cbf27f87 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 13:00:51 +0800 Subject: [PATCH 04/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../service/src/softbus/softbus_listener.cpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index b8af3c122..7c1fc81c0 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1128,26 +1128,30 @@ void SoftbusListener::UpdateDeviceInfoCache() int32_t SoftbusListener::GetUdidFromCache(const char *networkId, std::string &udid) { LOGI("SoftbusListener::GetUdidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); - for (const auto &item : deviceInfo_) { - if (std::string(item.second.second.networkId) == std::string(networkId)) { - udid = item.first; - return DM_OK; + std::lock_guard mutexLock(deviceInfosMutex_); + { + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + udid = item.first; + return DM_OK; + } } } - LOGI("SoftbusListener::GetUdidFromCache success udid%{public}s", GetAnonyString(udid).c_str()); return ERR_DM_FAILED; } int32_t SoftbusListener::GetUuidFromCache(const char *networkId, std::string &uuid) { LOGI("SoftbusListener::GetUuidFromCache networkId %{public}s", GetAnonyString(std::string(networkId)).c_str()); - for (const auto &item : deviceInfo_) { - if (std::string(item.second.second.networkId) == std::string(networkId)) { - uuid = item.second.first; - return DM_OK; + std::lock_guard mutexLock(deviceInfosMutex_); + { + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(networkId)) { + uuid = item.second.first; + return DM_OK; + } } } - LOGI("SoftbusListener::GetUuidFromCache success uuid%{public}s", GetAnonyString(uuid).c_str()); return ERR_DM_FAILED; } -- Gitee From d149125305402136f364efd81ca489d2ad68b196 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 14:48:42 +0800 Subject: [PATCH 05/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/softbus/softbus_listener.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 7c1fc81c0..e46011f7d 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1057,17 +1057,21 @@ void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) { deviceInfo_[udid] = std::pair(uuid, deviceInfo); } + LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", + GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); } void SoftbusListener::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { - LOGI("SoftbusListener::DeleteDeviceInfo"); + LOGI("SoftbusListener::DeleteDeviceInfo networkId %{public}s", + GetAnonyString(std::string(nodeInfo.networkId)).c_str()); std::string udid = ""; GetUdidByNetworkId(nodeInfo.networkId, udid); std::lock_guard mutexLock(deviceInfosMutex_); { if (deviceInfo_.find(udid) != deviceInfo_.end()) { deviceInfo_.erase(udid); + LOGI("DeleteDeviceInfo success udid %{public}s" GetAnonyString(udid).c_str()); } } } @@ -1091,6 +1095,8 @@ void SoftbusListener::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) 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()); } void SoftbusListener::GetDeviceInfoFromCache(std::vector &deviceInfoList) @@ -1133,6 +1139,7 @@ int32_t SoftbusListener::GetUdidFromCache(const char *networkId, std::string &ud 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; } } @@ -1148,6 +1155,7 @@ int32_t SoftbusListener::GetUuidFromCache(const char *networkId, std::string &uu 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; } } -- Gitee From aedc91a1fe531cb17ab54f0034813f014e5f65da Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 14:53:20 +0800 Subject: [PATCH 06/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/softbus/softbus_listener.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index e46011f7d..ca8686f96 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1071,7 +1071,7 @@ void SoftbusListener::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { if (deviceInfo_.find(udid) != deviceInfo_.end()) { deviceInfo_.erase(udid); - LOGI("DeleteDeviceInfo success udid %{public}s" GetAnonyString(udid).c_str()); + LOGI("DeleteDeviceInfo success udid %{public}s", GetAnonyString(udid).c_str()); } } } -- Gitee From 857aab8f20d52f5c72cd353af8d7fde487ca0b09 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 17:34:36 +0800 Subject: [PATCH 07/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/implementation/BUILD.gn | 3 + .../dependency/softbus/softbus_connector.cpp | 7 + services/service/BUILD.gn | 3 + .../include/softbus/softbus_listener.h | 6 - .../service/src/device_manager_service.cpp | 4 +- .../service/src/softbus/softbus_listener.cpp | 144 +------------- services/softbuscache/BUILD.gn | 44 +++++ .../softbuscache/include/dm_softbus_cache.h | 46 +++++ .../softbuscache/src/dm_softbus_cache.cpp | 185 ++++++++++++++++++ 9 files changed, 299 insertions(+), 143 deletions(-) create mode 100644 services/softbuscache/BUILD.gn create mode 100644 services/softbuscache/include/dm_softbus_cache.h create mode 100644 services/softbuscache/src/dm_softbus_cache.cpp diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 26cfd5c87..2e194f604 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", + "${services_path}/softbuscache/include", ] sources = [ @@ -137,6 +138,7 @@ if (defined(ohos_lite)) { "${utils_path}/include/timer", "${services_path}/include", "${services_path}/include/ipc/standard", + "${services_path}/softbuscache/include", ] } @@ -188,6 +190,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/commondependency:devicemanagerdependency", "${devicemanager_path}/radar:devicemanagerradar", + "${services_path}:dmdevicecache", "${innerkits_path}/native_cpp:devicemanagersdk", "${utils_path}:devicemanagerutils", ] diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index e5aea0ed5..ac2173e49 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,6 +265,9 @@ 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()); + if (SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid) == DM_OK) { + return DM_OK; + } 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) { @@ -277,6 +281,9 @@ int32_t SoftbusConnector::GetUdidByNetworkId(const char *networkId, std::string int32_t SoftbusConnector::GetUuidByNetworkId(const char *networkId, std::string &uuid) { LOGI("start, networkId: %{public}s.", GetAnonyString(std::string(networkId)).c_str()); + if (SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid) == DM_OK) { + return DM_OK; + } 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) { diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 700aef00f..73ca52acb 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", + "${services_path}/softbuscache/include", ] sources = [ @@ -129,6 +130,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency/include", "${devicemanager_path}/radar/include", "${innerkits_path}/native_cpp/include", + "${services_path}/softbuscache/include", "${utils_path}/include/appInfo/standard", "${utils_path}/include/crypto", "${utils_path}/include/timer", @@ -178,6 +180,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/commondependency:devicemanagerdependency", + "${services_path}:dmdevicecache", "${utils_path}:devicemanagerutils", ] diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 07d9135eb..286d42ed0 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -96,12 +96,6 @@ public: ConnectionAddrType &addrType); private: int32_t InitSoftPublishLNN(); - static void SaveDeviceInfo(DmDeviceInfo deviceInfo); - static void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo); - static void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); - static void GetDeviceInfoFromCache(std::vector &deviceInfoList); - static int32_t GetUdidFromCache(const char *networkId, std::string &udid); - static int32_t GetUuidFromCache(const char *networkId, std::string &uuid); private: static std::string hostName_; diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 589f36cf6..244c7cb05 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) @@ -68,8 +68,8 @@ int32_t DeviceManagerService::InitSoftbusListener() { if (softbusListener_ == nullptr) { softbusListener_ = std::make_shared(); - softbusListener_->UpdateDeviceInfoCache(); } + SoftbusCache::GetInstance().UpdateDeviceInfoCache(); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) SubscribePublishCommonEvent(); diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index ca8686f96..2708816b5 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -28,6 +28,7 @@ #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" @@ -152,7 +153,7 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); - SaveDeviceInfo(dmDeviceInfo); + SoftbusCache::GetInstance().SaveDeviceInfo(dmDeviceInfo); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); if (ret != DM_OK) { @@ -190,7 +191,7 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) } DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); - DeleteDeviceInfo(dmDeviceInfo); + SoftbusCache::GetInstance().DeleteDeviceInfo(dmDeviceInfo); 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); @@ -241,7 +242,7 @@ void SoftbusListener::OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBas ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device changed networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); dmDeviceInfo.networkType = networkType; - ChangeDeviceInfo(dmDeviceInfo); + 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."); @@ -507,8 +508,8 @@ int32_t SoftbusListener::UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName) int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) { - if (!deviceInfo_.empty()) { - GetDeviceInfoFromCache(deviceInfoList); + if (SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList) == DM_OK) { + LOGI("GetTrustedDeviceList success from cache."); return DM_OK ; } static int32_t radarDeviceCount = 0; @@ -536,7 +537,7 @@ int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceI NodeBasicInfo *nodeBasicInfo = nodeInfo + i; DmDeviceInfo deviceInfo; ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); - SaveDeviceInfo(deviceInfo); + SoftbusCache::GetInstance().SaveDeviceInfo(deviceInfo); deviceInfoList.push_back(deviceInfo); } radarInfo.stageRes = static_cast(StageRes::STAGE_SUCC); @@ -650,7 +651,7 @@ int32_t SoftbusListener::GetLocalDeviceType(int32_t &deviceType) int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string &udid) { - if (!deviceInfo_.empty() && GetUdidFromCache(networkId, udid) == DM_OK) { + if (SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid) == DM_OK) { return DM_OK; } uint8_t mUdid[UDID_BUF_LEN] = {0}; @@ -665,7 +666,7 @@ int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string & int32_t SoftbusListener::GetUuidByNetworkId(const char *networkId, std::string &uuid) { - if (!deviceInfo_.empty() && GetUuidFromCache(networkId, uuid) == DM_OK) { + if (SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid) == DM_OK) { return DM_OK; } uint8_t mUuid[UUID_BUF_LEN] = {0}; @@ -1036,133 +1037,6 @@ int32_t SoftbusListener::GetIPAddrTypeFromCache(const std::string &deviceId, con return ERR_DM_BIND_INPUT_PARA_INVALID; } -void SoftbusListener::SaveDeviceInfo(DmDeviceInfo deviceInfo) -{ - LOGI("SoftbusListener::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); - } - LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", - GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); -} - -void SoftbusListener::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) -{ - LOGI("SoftbusListener::DeleteDeviceInfo networkId %{public}s", - GetAnonyString(std::string(nodeInfo.networkId)).c_str()); - std::string udid = ""; - GetUdidByNetworkId(nodeInfo.networkId, udid); - std::lock_guard mutexLock(deviceInfosMutex_); - { - if (deviceInfo_.find(udid) != deviceInfo_.end()) { - deviceInfo_.erase(udid); - LOGI("DeleteDeviceInfo success udid %{public}s", GetAnonyString(udid).c_str()); - } - } -} - -void SoftbusListener::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) -{ - LOGI("SoftbusListener::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()); -} - -void SoftbusListener::GetDeviceInfoFromCache(std::vector &deviceInfoList) -{ - LOGI("SoftbusListener::GetDeviceInfoFromCache"); - std::lock_guard mutexLock(deviceInfosMutex_); - { - for (const auto &item : deviceInfo_) { - deviceInfoList.push_back(item.second.second); - } - } -} - -void SoftbusListener::UpdateDeviceInfoCache() -{ - LOGI("SoftbusListener::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; - } - 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 SoftbusListener::GetUdidFromCache(const char *networkId, std::string &udid) -{ - LOGI("SoftbusListener::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 SoftbusListener::GetUuidFromCache(const char *networkId, std::string &uuid) -{ - LOGI("SoftbusListener::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; -} - IRefreshCallback &SoftbusListener::GetSoftbusRefreshCb() { return softbusRefreshCallback_; diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn new file mode 100644 index 000000000..f81b358c4 --- /dev/null +++ b/services/softbuscache/BUILD.gn @@ -0,0 +1,44 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/distributedhardware/device_manager/device_manager.gni") +ohos_shared_library("dmdevicecache") { + include_dirs = [ + "include", + "${common_path}/include", + "${utils_path}/include/crypto", + ] + + sources = [ + "src/softbus_cache.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dmdevicecache\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [ + "${utils_path}:devicemanagerutils", + ] + + external_deps = [ + "dsoftbus:softbus_client", + ] + + 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 000000000..6ee82f34c --- /dev/null +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -0,0 +1,46 @@ +/* + * 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" + +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 GetDeviceInfoFromCache(std::vector &deviceInfoList); + int32_t GetUdidFromCache(const char *networkId, std::string &udid); + int32_t GetUuidFromCache(const char *networkId, std::string &uuid); +private: + int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); + int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); +private: + std::mutex deviceInfosMutex_; + std::unordered_map> deviceInfo_; +}; +} // 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 000000000..54b91a6ab --- /dev/null +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2022-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" +#include "parameter.h" + +using namespace OHOS::Security::AccessToken; + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(PermissionManager); + +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); + } + 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 (item.second.second.networkId == nodeInfo.networkId) { + LOGI("DeleteDeviceInfo success udid %{public}s", GetAnonyString(item.first).c_str()); + deviceInfo_.erase(item.first); + } + } + } +} + +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 deviceInfo size is %{public}d.", deviceInfo_.size()); + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (deviceInfo_.empty()) { + return ERR_DM_FAILED; + } + 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; + } + 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; +} +} // namespace DistributedHardware +} // namespace OHOS -- Gitee From da0eda059705e1f042a8a72ad56ec3ed043bde3b Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sun, 16 Jun 2024 21:13:17 +0800 Subject: [PATCH 08/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- BUILD.gn | 2 + device_manager.gni | 1 + services/implementation/BUILD.gn | 7 +- .../dependency/softbus/softbus_connector.cpp | 24 +----- services/service/BUILD.gn | 7 +- .../service/src/softbus/softbus_listener.cpp | 73 ++++--------------- services/softbuscache/BUILD.gn | 14 +++- .../softbuscache/include/dm_softbus_cache.h | 12 ++- .../softbuscache/src/dm_softbus_cache.cpp | 67 ++++++++++++++--- 9 files changed, 109 insertions(+), 98 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index a31bbc435..71aed6844 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 9738706cd..ea2b66b2f 100644 --- a/device_manager.gni +++ b/device_manager.gni @@ -18,6 +18,7 @@ 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" ext_path = "${devicemanager_path}/ext" ability_runtime_innerkits_path = diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 2e194f604..c2dff44d1 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -50,7 +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", - "${services_path}/softbuscache/include", + "${softbuscache_parh}/include", ] sources = [ @@ -76,6 +76,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/radar:devicemanagerradar", "${utils_path}:devicemanagerutils", + "${softbuscache_parh}:dmdevicecache", "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//base/security/device_auth/services:deviceauth_sdk", "//base/startup/init/interfaces/innerkits:libbegetutil", @@ -138,7 +139,7 @@ if (defined(ohos_lite)) { "${utils_path}/include/timer", "${services_path}/include", "${services_path}/include/ipc/standard", - "${services_path}/softbuscache/include", + "${softbuscache_parh}/include", ] } @@ -190,8 +191,8 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/commondependency:devicemanagerdependency", "${devicemanager_path}/radar:devicemanagerradar", - "${services_path}:dmdevicecache", "${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 ac2173e49..59790ad29 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -265,33 +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()); - if (SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid) == DM_OK) { - return DM_OK; - } - 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()); - if (SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid) == DM_OK) { - return DM_OK; - } - 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 73ca52acb..ec2e8f2b9 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -52,7 +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", - "${services_path}/softbuscache/include", + "${softbuscache_parh}/include", ] sources = [ @@ -83,6 +83,7 @@ if (defined(ohos_lite)) { deps = [ "${utils_path}:devicemanagerutils", + "${softbuscache_parh}:dmdevicecache", "//base/security/device_auth/services:deviceauth_sdk", "//base/startup/init/interfaces/innerkits:libbegetutil", "//commonlibrary/utils_lite:utils", @@ -130,7 +131,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency/include", "${devicemanager_path}/radar/include", "${innerkits_path}/native_cpp/include", - "${services_path}/softbuscache/include", + "${softbuscache_parh}/include", "${utils_path}/include/appInfo/standard", "${utils_path}/include/crypto", "${utils_path}/include/timer", @@ -180,7 +181,7 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/commondependency:devicemanagerdependency", - "${services_path}:dmdevicecache", + "${softbuscache_parh}:dmdevicecache", "${utils_path}:devicemanagerutils", ] diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 2708816b5..85aec7fbd 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -154,6 +154,13 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); SoftbusCache::GetInstance().SaveDeviceInfo(dmDeviceInfo); + 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; + } + SoftbusCache::GetInstance().SaveDeviceSecurityLevel(dmDeviceInfo.networkId, tempSecurityLevel); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); if (ret != DM_OK) { @@ -192,6 +199,7 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) DmDeviceInfo dmDeviceInfo; ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); SoftbusCache::GetInstance().DeleteDeviceInfo(dmDeviceInfo); + SoftbusCache::GetInstance().DeleteDeviceSecurityLevel(dmDeviceInfo.networkId); 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); @@ -508,38 +516,14 @@ int32_t SoftbusListener::UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName) int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) { - if (SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList) == DM_OK) { - LOGI("GetTrustedDeviceList success from cache."); - return DM_OK ; - } + 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); - SoftbusCache::GetInstance().SaveDeviceInfo(deviceInfo); - deviceInfoList.push_back(deviceInfo); - } radarInfo.stageRes = static_cast(StageRes::STAGE_SUCC); if (radarDeviceCount != deviceCount && deviceCount > 0 && IsDmRadarHelperReady() && GetDmRadarHelperObj() != nullptr) { @@ -549,8 +533,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; } @@ -651,32 +634,12 @@ int32_t SoftbusListener::GetLocalDeviceType(int32_t &deviceType) int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string &udid) { - if (SoftbusCache::GetInstance().GetUdidFromCache(networkId, udid) == DM_OK) { - return DM_OK; - } - 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) { - if (SoftbusCache::GetInstance().GetUuidFromCache(networkId, uuid) == DM_OK) { - return DM_OK; - } - 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() @@ -814,15 +777,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 index f81b358c4..14a13697f 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -14,6 +14,15 @@ import("//build/ohos.gni") import("//foundation/distributedhardware/device_manager/device_manager.gni") 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", @@ -21,7 +30,8 @@ ohos_shared_library("dmdevicecache") { ] sources = [ - "src/softbus_cache.cpp", + "${common_path}/src/dm_anonymous.cpp", + "src/dm_softbus_cache.cpp", ] defines = [ @@ -35,7 +45,9 @@ ohos_shared_library("dmdevicecache") { ] external_deps = [ + "bounds_checking_function:libsec_shared", "dsoftbus:softbus_client", + "hilog:libhilog", ] subsystem_name = "distributedhardware" diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 6ee82f34c..78f10bbf5 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -21,6 +21,8 @@ #include #include "dm_device_info.h" #include "single_instance.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" namespace OHOS { namespace DistributedHardware { @@ -31,15 +33,23 @@ public: void SaveDeviceInfo(DmDeviceInfo deviceInfo); void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo); void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); - void GetDeviceInfoFromCache(std::vector &deviceInfoList); + void SaveDeviceSecurityLevel(const char *networkId, const int32_t &securityLevel); + 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(); 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 diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 54b91a6ab..2717f8845 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -19,13 +19,9 @@ #include "dm_constants.h" #include "dm_device_info.h" #include "dm_log.h" -#include "parameter.h" - -using namespace OHOS::Security::AccessToken; - namespace OHOS { namespace DistributedHardware { -IMPLEMENT_SINGLE_INSTANCE(PermissionManager); +IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); int32_t SoftbusCache::GetUdidByNetworkId(const char *networkId, std::string &udid) { @@ -116,12 +112,9 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceInfoList) { - LOGI("SoftbusCache::GetDeviceInfoFromCache deviceInfo size is %{public}d.", deviceInfo_.size()); + LOGI("SoftbusCache::GetDeviceInfoFromCache."); std::lock_guard mutexLock(deviceInfosMutex_); { - if (deviceInfo_.empty()) { - return ERR_DM_FAILED; - } for (const auto &item : deviceInfo_) { deviceInfoList.push_back(item.second.second); } @@ -181,5 +174,61 @@ int32_t SoftbusCache::GetUuidFromCache(const char *networkId, std::string &uuid) } 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, const int32_t &securityLevel) +{ + LOGI("SoftbusCache::SaveDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); + std::lock_guard mutexLock(deviceSecurityLevelMutex_); + { + deviceSecurityLevel_[std::string(networkId)] = securityLevel; + } +} + +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(networkId) != deviceSecurityLevel_.end()) { + deviceSecurityLevel_.erase(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 -- Gitee From 825398d621e7dd15480b5b6b8bc80136b5f8f127 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 18:29:50 +0800 Subject: [PATCH 09/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../service/src/softbus/softbus_listener.cpp | 19 +--- services/softbuscache/BUILD.gn | 90 +++++++++++------- .../softbuscache/include/dm_softbus_cache.h | 5 +- .../softbuscache/src/dm_softbus_cache.cpp | 92 ++++++++++++++++++- 4 files changed, 151 insertions(+), 55 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 85aec7fbd..13abbdf07 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -154,13 +154,8 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); SoftbusCache::GetInstance().SaveDeviceInfo(dmDeviceInfo); - 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; - } - SoftbusCache::GetInstance().SaveDeviceSecurityLevel(dmDeviceInfo.networkId, tempSecurityLevel); + SoftbusCache::GetInstance().SaveDeviceSecurityLevel(mDeviceInfo.networkId); + SoftbusCache::GetInstance().SaveLocalDeviceInfo(); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); if (ret != DM_OK) { @@ -200,6 +195,7 @@ void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info) 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); @@ -586,14 +582,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) diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 14a13697f..212556098 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -11,46 +11,68 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//build/ohos.gni") +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") -ohos_shared_library("dmdevicecache") { - sanitize = { - boundary_sanitize = true - cfi = true - cfi_cross_dso = true - debug = false - integer_overflow = true - ubsan = true - } +if (defined(ohos_lite)) { + if (ohos_kernel_type == "linux") { + shared_library("dmdevicecache") { + include_dirs = [ + "include", + "${common_path}/include", + "${utils_path}/include/crypto", + ] + + sources = [ + "${common_path}/src/dm_anonymous.cpp", + "src/dm_softbus_cache.cpp", + ] - include_dirs = [ - "include", - "${common_path}/include", - "${utils_path}/include/crypto", - ] + defines = [ + "LITE_DEVICE", + "DH_LOG_TAG=\"dmdevicecache\"", + "LOG_DOMAIN=0xD004110", + ] - sources = [ - "${common_path}/src/dm_anonymous.cpp", - "src/dm_softbus_cache.cpp", - ] + deps = [ + "${utils_path}:devicemanagerutils", + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//third_party/bounds_checking_function:libsec_shared", + ] + } + } +} else { + ohos_shared_library("dmdevicecache") { + 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", - ] + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dmdevicecache\"", + "LOG_DOMAIN=0xD004110", + ] - deps = [ - "${utils_path}:devicemanagerutils", - ] + deps = [ "${utils_path}:devicemanagerutils" ] - external_deps = [ - "bounds_checking_function:libsec_shared", - "dsoftbus:softbus_client", - "hilog:libhilog", - ] + external_deps = [ + "bounds_checking_function:libsec_shared", + "dsoftbus:softbus_client", + "hilog:libhilog", + ] - subsystem_name = "distributedhardware" + subsystem_name = "distributedhardware" - part_name = "device_manager" + part_name = "device_manager" + } } diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 78f10bbf5..34b7dc643 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -33,13 +33,16 @@ public: void SaveDeviceInfo(DmDeviceInfo deviceInfo); void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo); void ChangeDeviceInfo(const DmDeviceInfo deviceInfo); - void SaveDeviceSecurityLevel(const char *networkId, const int32_t &securityLevel); + 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); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 2717f8845..dc3707a5e 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -22,6 +22,77 @@ namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); +bool online = false; +DmDeviceInfo localDeviceInfo_; +int32_t onlinDeviceNum_ = 0; +void SoftbusCache::SaveLocalDeviceInfo() +{ + LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", + GetAnonyString(std::string(deviceInfo.networkId)).c_str()); + if (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_); + online = true; + bool devInMap = false; + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { + devInMap = true; + } + } + if (!devInMap) { + SaveDeviceInfo(localDeviceInfo_); + } +} + +void SoftbusCache::DeleteLocalDeviceInfo() +{ + LOGI("SoftbusCache::DeleteLocalDeviceInfo networkid %{public}s.", + GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (onlinDeviceNum_ == 0) { + online = false; + } + } +} + +int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) +{ + bool devInfoEmpty = false; + std::lock_guard mutexLock(deviceInfosMutex_); + { + devInfoEmpty = deviceInfo_.empty(); + } + if (devInfoEmpty) { + 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_); + } + bool devInMap = false; + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { + devInMap = true; + } + } + if (!devInMap) { + SaveDeviceInfo(localDeviceInfo_); + } + nodeInfo = localDeviceInfo_; + return DM_OK; +} int32_t SoftbusCache::GetUdidByNetworkId(const char *networkId, std::string &udid) { @@ -67,6 +138,7 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) std::lock_guard mutexLock(deviceInfosMutex_); { deviceInfo_[udid] = std::pair(uuid, deviceInfo); + onlinDeviceNum_++; } LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); @@ -79,9 +151,10 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) std::lock_guard mutexLock(deviceInfosMutex_); { for (const auto &item : deviceInfo_) { - if (item.second.second.networkId == nodeInfo.networkId) { + 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); + onlinDeviceNum_--; } } } @@ -195,12 +268,21 @@ int32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeIn return DM_OK; } -void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId, const int32_t &securityLevel) +void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) { LOGI("SoftbusCache::SaveDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); std::lock_guard mutexLock(deviceSecurityLevelMutex_); { - deviceSecurityLevel_[std::string(networkId)] = securityLevel; + 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; } } @@ -210,8 +292,8 @@ void SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) GetAnonyString(std::string(networkId)).c_str()); std::lock_guard mutexLock(deviceSecurityLevelMutex_); { - if (deviceSecurityLevel_.find(networkId) != deviceSecurityLevel_.end()) { - deviceSecurityLevel_.erase(networkId); + if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { + deviceSecurityLevel_.erase(std::string(networkId)); } } } -- Gitee From 2a71eaa4eeceb10f06edf0105d8e89ab9bc4a236 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 18:33:17 +0800 Subject: [PATCH 10/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index dc3707a5e..b67ece93e 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -122,8 +122,14 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusCache::SaveDeviceInfo"); std::string udid = ""; - std::string uuid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); + std::lock_guard mutexLock(deviceInfosMutex_); + { + if (deviceSecurityLevel_.find(udid) != deviceSecurityLevel_.end()) { + return; + } + } + std::string uuid = ""; GetUuidByNetworkId(deviceInfo.networkId, uuid); char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; if (Crypto::GetUdidHash(udid, reinterpret_cast(udidHash)) != DM_OK) { -- Gitee From 603a6c3421bccc974c75d8fe07db0267ae8270d5 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 18:34:55 +0800 Subject: [PATCH 11/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/softbus/softbus_listener.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 13abbdf07..cf22f2079 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -62,8 +62,6 @@ IDmRadarHelper* SoftbusListener::dmRadarHelper_ = nullptr; std::shared_ptr SoftbusListener::timer_ = std::make_shared(); void* SoftbusListener::radarHandle_ = nullptr; std::string SoftbusListener::hostName_ = ""; -static std::mutex deviceInfosMutex_; -static std::unordered_map> deviceInfo_; static int OnSessionOpened(int sessionId, int result) { -- Gitee From 9653d9ec92502fd3dbf9556d56c70a34444b087f Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 18:41:01 +0800 Subject: [PATCH 12/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../softbuscache/src/dm_softbus_cache.cpp | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index b67ece93e..870786478 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -27,8 +27,6 @@ DmDeviceInfo localDeviceInfo_; int32_t onlinDeviceNum_ = 0; void SoftbusCache::SaveLocalDeviceInfo() { - LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", - GetAnonyString(std::string(deviceInfo.networkId)).c_str()); if (online) { return; } @@ -39,12 +37,16 @@ void SoftbusCache::SaveLocalDeviceInfo() return; } ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); + LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", + GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); online = true; bool devInMap = false; std::lock_guard mutexLock(deviceInfosMutex_); { - if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { - devInMap = true; + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { + devInMap = true; + } } } if (!devInMap) { @@ -80,16 +82,7 @@ int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) } ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); } - bool devInMap = false; - std::lock_guard mutexLock(deviceInfosMutex_); - { - if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { - devInMap = true; - } - } - if (!devInMap) { - SaveDeviceInfo(localDeviceInfo_); - } + SaveDeviceInfo(localDeviceInfo_); nodeInfo = localDeviceInfo_; return DM_OK; } -- Gitee From 9e6fea2265d40690b60ede09621f8a64e2a882b6 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 18:55:44 +0800 Subject: [PATCH 13/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../service/include/softbus/softbus_listener.h | 1 - services/softbuscache/src/dm_softbus_cache.cpp | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 286d42ed0..a38eb001b 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -84,7 +84,6 @@ public: int32_t RegisterSoftbusLnnOpsCbk(const std::string &pkgName, const std::shared_ptr callback); int32_t UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName); - void UpdateDeviceInfoCache(); static IDmRadarHelper* GetDmRadarHelperObj(); static bool IsDmRadarHelperReady(); static bool CloseDmRadarHelperObj(std::string name); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 870786478..b3fcd4251 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -22,12 +22,12 @@ namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); -bool online = false; +bool g_online = false; DmDeviceInfo localDeviceInfo_; -int32_t onlinDeviceNum_ = 0; +int32_t g_onlinDeviceNum = 0; void SoftbusCache::SaveLocalDeviceInfo() { - if (online) { + if (g_online) { return; } NodeBasicInfo nodeBasicInfo; @@ -39,7 +39,7 @@ void SoftbusCache::SaveLocalDeviceInfo() ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); - online = true; + g_online = true; bool devInMap = false; std::lock_guard mutexLock(deviceInfosMutex_); { @@ -60,8 +60,8 @@ void SoftbusCache::DeleteLocalDeviceInfo() GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); std::lock_guard mutexLock(deviceInfosMutex_); { - if (onlinDeviceNum_ == 0) { - online = false; + if (g_onlinDeviceNum == 0) { + g_online = false; } } } @@ -137,7 +137,7 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) std::lock_guard mutexLock(deviceInfosMutex_); { deviceInfo_[udid] = std::pair(uuid, deviceInfo); - onlinDeviceNum_++; + g_onlinDeviceNum++; } LOGI("SaveDeviceInfo success udid %{public}s, networkId %{public}s", GetAnonyString(udid).c_str(), GetAnonyString(std::string(deviceInfo.networkId)).c_str()); @@ -153,7 +153,7 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) 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); - onlinDeviceNum_--; + g_onlinDeviceNum--; } } } -- Gitee From 5fc75c4cec1823008189acf3828d7a8c5c088d95 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 19:16:50 +0800 Subject: [PATCH 14/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/softbus/softbus_listener.cpp | 4 ++-- services/softbuscache/BUILD.gn | 9 +++++++++ services/softbuscache/src/dm_softbus_cache.cpp | 6 ------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index cf22f2079..08ea42fff 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -152,7 +152,7 @@ void SoftbusListener::OnSoftbusDeviceOnline(NodeBasicInfo *info) ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo); LOGI("device online networkId: %{public}s.", GetAnonyString(dmDeviceInfo.networkId).c_str()); SoftbusCache::GetInstance().SaveDeviceInfo(dmDeviceInfo); - SoftbusCache::GetInstance().SaveDeviceSecurityLevel(mDeviceInfo.networkId); + SoftbusCache::GetInstance().SaveDeviceSecurityLevel(dmDeviceInfo.networkId); SoftbusCache::GetInstance().SaveLocalDeviceInfo(); std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo); int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE); @@ -510,7 +510,7 @@ int32_t SoftbusListener::UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName) int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) { - SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList); + int32_t ret = SoftbusCache::GetInstance().GetDeviceInfoFromCache(deviceInfoList); static int32_t radarDeviceCount = 0; int32_t deviceCount = deviceInfoList.size(); char localDeviceId[DEVICE_UUID_LENGTH] = {0}; diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 212556098..027ab8c13 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -46,6 +46,15 @@ if (defined(ohos_lite)) { } } 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", diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index b3fcd4251..6e30064a1 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -116,12 +116,6 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) LOGI("SoftbusCache::SaveDeviceInfo"); std::string udid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); - std::lock_guard mutexLock(deviceInfosMutex_); - { - if (deviceSecurityLevel_.find(udid) != deviceSecurityLevel_.end()) { - return; - } - } std::string uuid = ""; GetUuidByNetworkId(deviceInfo.networkId, uuid); char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; -- Gitee From b33a3e8418cae55825ab9b0335acce1f50ce0872 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 19:53:03 +0800 Subject: [PATCH 15/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- device_manager.gni | 2 ++ services/softbuscache/BUILD.gn | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/device_manager.gni b/device_manager.gni index ea2b66b2f..de4e08c4a 100644 --- a/device_manager.gni +++ b/device_manager.gni @@ -20,6 +20,8 @@ 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" ext_path = "${devicemanager_path}/ext" ability_runtime_innerkits_path = "//foundation/ability/ability_runtime/interfaces/inner_api" diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 027ab8c13..66d155dc4 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -23,6 +23,7 @@ if (defined(ohos_lite)) { include_dirs = [ "include", "${common_path}/include", + "${innerkits_path}/native_cpp/include", "${utils_path}/include/crypto", ] @@ -38,9 +39,9 @@ if (defined(ohos_lite)) { ] deps = [ - "${utils_path}:devicemanagerutils", - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//third_party/bounds_checking_function:libsec_shared", + "${hilog_path}:hilog_shared", + "${third_path}:libsec_shared", + "${utils_path}:devicemanagerutils", ] } } @@ -54,7 +55,7 @@ if (defined(ohos_lite)) { integer_overflow = true ubsan = true } - + include_dirs = [ "include", "${common_path}/include", -- Gitee From c3759b84e94cf345b11011bc4b68671b6c094797 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 20:17:57 +0800 Subject: [PATCH 16/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/implementation/BUILD.gn | 2 +- services/softbuscache/BUILD.gn | 4 +++- test/unittest/BUILD.gn | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index c2dff44d1..dab1be9c6 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -75,8 +75,8 @@ if (defined(ohos_lite)) { deps = [ "${devicemanager_path}/radar:devicemanagerradar", - "${utils_path}:devicemanagerutils", "${softbuscache_parh}:dmdevicecache", + "${utils_path}:devicemanagerutils", "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//base/security/device_auth/services:deviceauth_sdk", "//base/startup/init/interfaces/innerkits:libbegetutil", diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 66d155dc4..744ec0561 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -41,7 +41,9 @@ if (defined(ohos_lite)) { deps = [ "${hilog_path}:hilog_shared", "${third_path}:libsec_shared", - "${utils_path}:devicemanagerutils", + "${utils_path}:devicemanagerutils", + "//foundation/communication/dsoftbus/sdk:softbus_client", + "//third_party/json/include", ] } } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 059690c27..fbc9cf031 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", ] -- Gitee From 37f59f7d44049b96d8341fc994d2ec6ec6747545 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 20:26:20 +0800 Subject: [PATCH 17/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- device_manager.gni | 1 + services/softbuscache/BUILD.gn | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/device_manager.gni b/device_manager.gni index de4e08c4a..1a9bdad72 100644 --- a/device_manager.gni +++ b/device_manager.gni @@ -22,6 +22,7 @@ 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/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 744ec0561..22bc15f4c 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -25,6 +25,7 @@ if (defined(ohos_lite)) { "${common_path}/include", "${innerkits_path}/native_cpp/include", "${utils_path}/include/crypto", + "//third_party/json/include", ] sources = [ @@ -39,11 +40,10 @@ if (defined(ohos_lite)) { ] deps = [ + "${dsoftbussdk_path}:softbus_client", "${hilog_path}:hilog_shared", "${third_path}:libsec_shared", "${utils_path}:devicemanagerutils", - "//foundation/communication/dsoftbus/sdk:softbus_client", - "//third_party/json/include", ] } } -- Gitee From 6f1fe863e69215d332fe2f4b98c3e760c0042caa Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 20:51:26 +0800 Subject: [PATCH 18/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index ec2e8f2b9..de93f6761 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -82,8 +82,8 @@ if (defined(ohos_lite)) { ] deps = [ - "${utils_path}:devicemanagerutils", "${softbuscache_parh}:dmdevicecache", + "${utils_path}:devicemanagerutils", "//base/security/device_auth/services:deviceauth_sdk", "//base/startup/init/interfaces/innerkits:libbegetutil", "//commonlibrary/utils_lite:utils", -- Gitee From 7cc3325cbed4a73429f8d6876c2f64673b801b55 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 21:14:41 +0800 Subject: [PATCH 19/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/softbuscache/BUILD.gn b/services/softbuscache/BUILD.gn index 22bc15f4c..4cdcad1db 100644 --- a/services/softbuscache/BUILD.gn +++ b/services/softbuscache/BUILD.gn @@ -43,7 +43,7 @@ if (defined(ohos_lite)) { "${dsoftbussdk_path}:softbus_client", "${hilog_path}:hilog_shared", "${third_path}:libsec_shared", - "${utils_path}:devicemanagerutils", + "${utils_path}:devicemanagerutils", ] } } -- Gitee From 3f27cde8da1ac2497390ca66b0cf66d26ede47e9 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 23:12:09 +0800 Subject: [PATCH 20/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../softbuscache/src/dm_softbus_cache.cpp | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 6e30064a1..27fef0645 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * 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 @@ -81,8 +81,8 @@ int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) return ERR_DM_FAILED; } ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); + SaveDeviceInfo(localDeviceInfo_); } - SaveDeviceInfo(localDeviceInfo_); nodeInfo = localDeviceInfo_; return DM_OK; } @@ -114,22 +114,25 @@ int32_t SoftbusCache::GetUuidByNetworkId(const char *networkId, std::string &uui void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusCache::SaveDeviceInfo"); - std::string udid = ""; - GetUdidByNetworkId(deviceInfo.networkId, udid); - std::string uuid = ""; - 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_); { + std::string udid = ""; + GetUdidByNetworkId(deviceInfo.networkId, udid); + if (deviceInfo_.find(udid) != deviceInfo_.end()) { + return; + } + std::string uuid = ""; + 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; + } deviceInfo_[udid] = std::pair(uuid, deviceInfo); g_onlinDeviceNum++; } @@ -204,6 +207,9 @@ void SoftbusCache::UpdateDeviceInfoCache() ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, deviceInfo); SaveDeviceInfo(deviceInfo); } + if (deviceCount != 0) { + SaveLocalDeviceInfo(); + } FreeNodeInfo(nodeInfo); LOGI("UpdateDeviceInfoCache success, deviceCount: %{public}d.", deviceCount); return; -- Gitee From c777a9604838c827ce562307401fbcf559546718 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 23:19:46 +0800 Subject: [PATCH 21/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 27fef0645..7b159db5c 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -114,9 +114,9 @@ int32_t SoftbusCache::GetUuidByNetworkId(const char *networkId, std::string &uui void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusCache::SaveDeviceInfo"); + std::string udid = ""; std::lock_guard mutexLock(deviceInfosMutex_); { - std::string udid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); if (deviceInfo_.find(udid) != deviceInfo_.end()) { return; -- Gitee From 1d262f67c4204ba62d9f69a665b662130f257085 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Mon, 17 Jun 2024 23:26:28 +0800 Subject: [PATCH 22/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 7b159db5c..4bf3186b0 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -115,9 +115,9 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusCache::SaveDeviceInfo"); std::string udid = ""; + GetUdidByNetworkId(deviceInfo.networkId, udid); std::lock_guard mutexLock(deviceInfosMutex_); { - GetUdidByNetworkId(deviceInfo.networkId, udid); if (deviceInfo_.find(udid) != deviceInfo_.end()) { return; } -- Gitee From 64dad520482303c2b51d99ef3bd29fc0a94710e8 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 09:50:55 +0800 Subject: [PATCH 23/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 4bf3186b0..7371f4745 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -43,7 +43,8 @@ void SoftbusCache::SaveLocalDeviceInfo() bool devInMap = false; std::lock_guard mutexLock(deviceInfosMutex_); { - for (const auto &item : deviceInfo_) { + + for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { devInMap = true; } @@ -118,9 +119,6 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) GetUdidByNetworkId(deviceInfo.networkId, udid); std::lock_guard mutexLock(deviceInfosMutex_); { - if (deviceInfo_.find(udid) != deviceInfo_.end()) { - return; - } std::string uuid = ""; GetUuidByNetworkId(deviceInfo.networkId, uuid); char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; -- Gitee From 83550d550f2d51b2dfb0c1640adcb736ff85524f Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 09:58:32 +0800 Subject: [PATCH 24/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 7371f4745..940c0152c 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -43,8 +43,7 @@ void SoftbusCache::SaveLocalDeviceInfo() bool devInMap = false; std::lock_guard mutexLock(deviceInfosMutex_); { - - for (const auto &item : deviceInfo_) { + for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { devInMap = true; } -- Gitee From c7bf066c56ba31d92b197dbc241fbc83e7b6a4d1 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 11:08:54 +0800 Subject: [PATCH 25/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 940c0152c..e3019ce4d 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -181,6 +181,9 @@ int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceIn LOGI("SoftbusCache::GetDeviceInfoFromCache."); std::lock_guard mutexLock(deviceInfosMutex_); { + if (deviceInfo_.empty()) { + return ERR_DM_FAILED; + } for (const auto &item : deviceInfo_) { deviceInfoList.push_back(item.second.second); } @@ -299,6 +302,9 @@ int32_t SoftbusCache::GetSecurityDeviceLevel(const char *networkId, int32_t &sec LOGI("SoftbusCache::GetSecurityDeviceLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); std::lock_guard mutexLock(deviceSecurityLevelMutex_); { + if (deviceSecurityLevel_.empty()) { + return ERR_DM_FAILED; + } for (const auto &item : deviceSecurityLevel_) { if (item.first == std::string(networkId)) { securityLevel = item.second; -- Gitee From 4032333cfc70e36b03152c38e4a7a58d010f1b1e Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 11:20:23 +0800 Subject: [PATCH 26/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/softbuscache/src/dm_softbus_cache.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index e3019ce4d..3ef0c42a6 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -43,9 +43,11 @@ void SoftbusCache::SaveLocalDeviceInfo() bool devInMap = false; std::lock_guard mutexLock(deviceInfosMutex_); { - for (const auto &item : deviceInfo_) { - if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { - devInMap = true; + if (!deviceInfo_.empty()) { + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { + devInMap = true; + } } } } @@ -143,6 +145,9 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) GetAnonyString(std::string(nodeInfo.networkId)).c_str()); std::lock_guard mutexLock(deviceInfosMutex_); { + if (deviceInfo_.empty()) { + return; + } 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()); @@ -220,6 +225,9 @@ 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_); { + if (deviceInfo_.empty()) { + return ERR_DM_FAILED; + } for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(networkId)) { udid = item.first; @@ -236,6 +244,9 @@ 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_); { + if (deviceInfo_.empty()) { + return ERR_DM_FAILED; + } for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(networkId)) { uuid = item.second.first; -- Gitee From cc8e48f98613eeedab3d1630cdb81c4ef703c939 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 13:14:05 +0800 Subject: [PATCH 27/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../softbuscache/src/dm_softbus_cache.cpp | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 3ef0c42a6..70de7d27d 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -41,8 +41,8 @@ void SoftbusCache::SaveLocalDeviceInfo() GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); g_online = true; bool devInMap = false; - std::lock_guard mutexLock(deviceInfosMutex_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (!deviceInfo_.empty()) { for (const auto &item : deviceInfo_) { if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { @@ -60,8 +60,8 @@ void SoftbusCache::DeleteLocalDeviceInfo() { LOGI("SoftbusCache::DeleteLocalDeviceInfo networkid %{public}s.", GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); - std::lock_guard mutexLock(deviceInfosMutex_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (g_onlinDeviceNum == 0) { g_online = false; } @@ -71,8 +71,8 @@ void SoftbusCache::DeleteLocalDeviceInfo() int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) { bool devInfoEmpty = false; - std::lock_guard mutexLock(deviceInfosMutex_); { + std::lock_guard mutexLock(deviceInfosMutex_); devInfoEmpty = deviceInfo_.empty(); } if (devInfoEmpty) { @@ -117,21 +117,21 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) { LOGI("SoftbusCache::SaveDeviceInfo"); std::string udid = ""; + std::string uuid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); - std::lock_guard mutexLock(deviceInfosMutex_); + 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::string uuid = ""; - 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++; } @@ -143,8 +143,8 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { LOGI("SoftbusCache::DeleteDeviceInfo networkId %{public}s", GetAnonyString(std::string(nodeInfo.networkId)).c_str()); - std::lock_guard mutexLock(deviceInfosMutex_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (deviceInfo_.empty()) { return; } @@ -163,8 +163,8 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) LOGI("SoftbusCache::ChangeDeviceInfo"); std::string udid = ""; GetUdidByNetworkId(deviceInfo.networkId, udid); - std::lock_guard mutexLock(deviceInfosMutex_); { + 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) { @@ -184,8 +184,8 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceInfoList) { LOGI("SoftbusCache::GetDeviceInfoFromCache."); - std::lock_guard mutexLock(deviceInfosMutex_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (deviceInfo_.empty()) { return ERR_DM_FAILED; } @@ -223,8 +223,8 @@ void SoftbusCache::UpdateDeviceInfoCache() 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_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (deviceInfo_.empty()) { return ERR_DM_FAILED; } @@ -242,8 +242,8 @@ int32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) 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_); { + std::lock_guard mutexLock(deviceInfosMutex_); if (deviceInfo_.empty()) { return ERR_DM_FAILED; } @@ -281,8 +281,8 @@ int32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeIn void SoftbusCache::SaveDeviceSecurityLevel(const char *networkId) { LOGI("SoftbusCache::SaveDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); - std::lock_guard mutexLock(deviceSecurityLevelMutex_); { + std::lock_guard mutexLock(deviceSecurityLevelMutex_); if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { return; } @@ -300,8 +300,8 @@ void SoftbusCache::DeleteDeviceSecurityLevel(const char *networkId) { LOGI("SoftbusCache::DeleteDeviceSecurityLevel networkId %{public}s.", GetAnonyString(std::string(networkId)).c_str()); - std::lock_guard mutexLock(deviceSecurityLevelMutex_); { + std::lock_guard mutexLock(deviceSecurityLevelMutex_); if (deviceSecurityLevel_.find(std::string(networkId)) != deviceSecurityLevel_.end()) { deviceSecurityLevel_.erase(std::string(networkId)); } @@ -311,8 +311,8 @@ void SoftbusCache::DeleteDeviceSecurityLevel(const char *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_); { + std::lock_guard mutexLock(deviceSecurityLevelMutex_); if (deviceSecurityLevel_.empty()) { return ERR_DM_FAILED; } -- Gitee From 92954026bd80e6e37d53227936db8c1c38fbe5fe Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Tue, 18 Jun 2024 16:14:19 +0800 Subject: [PATCH 28/28] =?UTF-8?q?=E6=9F=A5=E8=AF=A2DM=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../softbuscache/src/dm_softbus_cache.cpp | 194 +++++++----------- 1 file changed, 73 insertions(+), 121 deletions(-) diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 70de7d27d..4fd8539f1 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -23,6 +23,7 @@ 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() @@ -39,53 +40,37 @@ void SoftbusCache::SaveLocalDeviceInfo() ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); LOGI("SoftbusCache::SaveLocalDeviceInfo networkid %{public}s.", GetAnonyString(std::string(localDeviceInfo_.networkId)).c_str()); + SaveDeviceInfo(localDeviceInfo_); g_online = true; - bool devInMap = false; - { - std::lock_guard mutexLock(deviceInfosMutex_); - if (!deviceInfo_.empty()) { - for (const auto &item : deviceInfo_) { - if (std::string(item.second.second.networkId) == std::string(localDeviceInfo_.networkId)) { - devInMap = true; - } - } - } - } - if (!devInMap) { - SaveDeviceInfo(localDeviceInfo_); - } + 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; - } + std::lock_guard mutexLock(deviceInfosMutex_); + if (g_onlinDeviceNum == 0) { + g_online = false; + g_getLocalDevInfo = false; } } int32_t SoftbusCache::GetLocalDeviceInfo(DmDeviceInfo &nodeInfo) { - bool devInfoEmpty = false; - { - std::lock_guard mutexLock(deviceInfosMutex_); - devInfoEmpty = deviceInfo_.empty(); + if (g_getLocalDevInfo) { + nodeInfo = localDeviceInfo_; + return DM_OK; } - if (devInfoEmpty) { - 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_); + 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; } - nodeInfo = localDeviceInfo_; + ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, localDeviceInfo_); + SaveDeviceInfo(localDeviceInfo_); + g_getLocalDevInfo = true; return DM_OK; } @@ -130,11 +115,9 @@ void SoftbusCache::SaveDeviceInfo(DmDeviceInfo deviceInfo) LOGE("SaveDeviceInfo copy deviceId failed."); return; } - { - std::lock_guard mutexLock(deviceInfosMutex_); - deviceInfo_[udid] = std::pair(uuid, deviceInfo); - g_onlinDeviceNum++; - } + 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()); } @@ -143,17 +126,12 @@ void SoftbusCache::DeleteDeviceInfo(const DmDeviceInfo &nodeInfo) { LOGI("SoftbusCache::DeleteDeviceInfo networkId %{public}s", GetAnonyString(std::string(nodeInfo.networkId)).c_str()); - { - std::lock_guard mutexLock(deviceInfosMutex_); - if (deviceInfo_.empty()) { - return; - } - 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--; - } + 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--; } } } @@ -163,19 +141,17 @@ 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; + 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()); @@ -184,14 +160,9 @@ void SoftbusCache::ChangeDeviceInfo(const DmDeviceInfo deviceInfo) int32_t SoftbusCache::GetDeviceInfoFromCache(std::vector &deviceInfoList) { LOGI("SoftbusCache::GetDeviceInfoFromCache."); - { - std::lock_guard mutexLock(deviceInfosMutex_); - if (deviceInfo_.empty()) { - return ERR_DM_FAILED; - } - for (const auto &item : deviceInfo_) { - deviceInfoList.push_back(item.second.second); - } + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + deviceInfoList.push_back(item.second.second); } return DM_OK; } @@ -206,15 +177,15 @@ void SoftbusCache::UpdateDeviceInfoCache() 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); } - if (deviceCount != 0) { - SaveLocalDeviceInfo(); - } FreeNodeInfo(nodeInfo); LOGI("UpdateDeviceInfoCache success, deviceCount: %{public}d.", deviceCount); return; @@ -223,17 +194,12 @@ void SoftbusCache::UpdateDeviceInfoCache() 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_); - if (deviceInfo_.empty()) { - return ERR_DM_FAILED; - } - 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; - } + 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; @@ -242,17 +208,12 @@ int32_t SoftbusCache::GetUdidFromCache(const char *networkId, std::string &udid) 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_); - if (deviceInfo_.empty()) { - return ERR_DM_FAILED; - } - 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; - } + 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; @@ -281,46 +242,37 @@ int32_t SoftbusCache::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeIn 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; + 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)); - } + 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_); - if (deviceSecurityLevel_.empty()) { - return ERR_DM_FAILED; - } - for (const auto &item : deviceSecurityLevel_) { - if (item.first == std::string(networkId)) { - securityLevel = item.second; - return DM_OK; - } + 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; -- Gitee