From 83f8aa2d323b131f275c6a8d95f8d780c998d8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Thu, 13 Mar 2025 16:46:56 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=8F=91=E7=8E=B0id=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E6=95=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../inner_kits/native_cpp/src/device_manager_impl.cpp | 2 +- utils/include/dm_random.h | 1 + utils/src/dm_random.cpp | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 6c0d3e70a..7c05a86e8 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -1972,7 +1972,7 @@ uint16_t DeviceManagerImpl::AddDiscoveryCallback(const std::string &pkgName, std::lock_guard autoLock(subMapLock); auto item = pkgName2SubIdMap_.find(pkgNameTemp); if (item == pkgName2SubIdMap_.end() && subscribeId == DM_INVALID_FLAG_ID) { - subscribeId = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); + subscribeId = GenRandUint16(); pkgName2SubIdMap_[pkgNameTemp] = subscribeId; } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) { pkgName2SubIdMap_[pkgNameTemp] = subscribeId; diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 7b88bb4b5..215b89d30 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -22,6 +22,7 @@ namespace OHOS { namespace DistributedHardware { int32_t GenRandInt(int32_t randMin, int32_t randMax); int64_t GenRandLongLong(int64_t randMin, int64_t randMax); +uint16_t GenRandUint16(); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 6649c2102..466207678 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -43,5 +43,13 @@ int64_t GenRandLongLong(int64_t randMin, int64_t randMax) std::uniform_int_distribution disRand(randMin, randMax); return disRand(genRand); } + +uint16_t GenRandUint16() +{ + int64_t currentTime = std::chrono::duration_cast( + steadyClock::now().time_since_epoch() + ).count(); + return static_cast(currentTime % UINT16_MAX); +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From fab32be9f95acdd9ba337a8cb9cfe0095f6aab49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Thu, 13 Mar 2025 17:18:46 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../inner_kits/native_cpp/src/device_manager_impl.cpp | 8 ++++++++ utils/include/dm_random.h | 1 - utils/src/dm_random.cpp | 8 -------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 7c05a86e8..6c4f4f1de 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -14,6 +14,7 @@ */ #include "device_manager_impl.h" +#include #include #include "device_manager_ipc_interface_code.h" #include "device_manager_notify.h" @@ -114,6 +115,13 @@ uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) return disRand(genRand); } +uint16_t GenRandUint16() +{ + uint64_t currentTickMillseconds = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + return static_cast(currentTickMillseconds % UINT16_MAX); +} + DeviceManagerImpl &DeviceManagerImpl::GetInstance() { static DeviceManagerImpl instance; diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 215b89d30..7b88bb4b5 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -22,7 +22,6 @@ namespace OHOS { namespace DistributedHardware { int32_t GenRandInt(int32_t randMin, int32_t randMax); int64_t GenRandLongLong(int64_t randMin, int64_t randMax); -uint16_t GenRandUint16(); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 466207678..6649c2102 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -43,13 +43,5 @@ int64_t GenRandLongLong(int64_t randMin, int64_t randMax) std::uniform_int_distribution disRand(randMin, randMax); return disRand(genRand); } - -uint16_t GenRandUint16() -{ - int64_t currentTime = std::chrono::duration_cast( - steadyClock::now().time_since_epoch() - ).count(); - return static_cast(currentTime % UINT16_MAX); -} } // namespace DistributedHardware } // namespace OHOS -- Gitee From d84a8e805bd81a6b36cbe2a5eed2a45e800f73ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Fri, 14 Mar 2025 12:06:00 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E5=9B=9E=E8=B0=83subID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- interfaces/inner_kits/native_cpp/BUILD.gn | 2 + .../native_cpp/include/device_manager_impl.h | 3 + .../native_cpp/src/device_manager_impl.cpp | 42 ++++++---- .../include/discovery/discovery_manager.h | 9 +- .../src/discovery/discovery_manager.cpp | 83 +++++++++++++++---- utils/include/dm_random.h | 1 + utils/src/dm_random.cpp | 8 ++ 7 files changed, 113 insertions(+), 35 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index 6b620d1ab..512634063 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -142,6 +142,7 @@ if (defined(ohos_lite)) { "${common_path}/include/dfx", "${common_path}/include/dfx/standard", "${devicemanager_path}/radar/include", + "${utils_path}/include", ] } @@ -174,6 +175,7 @@ if (defined(ohos_lite)) { "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "${common_path}/src/json_object.cpp", "${devicemanager_path}/radar/src/dm_radar_helper.cpp", + "${utils_path}/src/dm_anonymous.cpp", "src/device_manager.cpp", "src/device_manager_impl.cpp", "src/dm_device_info.cpp", diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 485ea6e27..27413f333 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -444,6 +444,7 @@ private: uint16_t GetSubscribeIdFromMap(const std::string &pkgName); void SyncCallbackToService(DmCommonNotifyEvent dmCommonNotifyEvent, const std::string &pkgName); int32_t GetAnonyLocalUdid(const std::string &pkgName, std::string &anonyUdid); + uint16_t GetUniqueRandUint(); private: #if !defined(__LITEOS_M__) @@ -457,6 +458,8 @@ private: std::map pkgName2PubIdMap_; std::string anonyLocalUdid_; + + std::set randUint16Set_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 6c4f4f1de..dfc5242e9 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -14,7 +14,6 @@ */ #include "device_manager_impl.h" -#include #include #include "device_manager_ipc_interface_code.h" #include "device_manager_notify.h" @@ -25,6 +24,7 @@ #include "dm_hitrace.h" #include "dm_log.h" #include "dm_radar_helper.h" +#include "dm_random.h" #include "ipc_acl_profile_req.h" #include "ipc_authenticate_device_req.h" #include "ipc_bind_device_req.h" @@ -107,21 +107,6 @@ const int32_t USLEEP_TIME_US_100000 = 100000; // 100ms constexpr int32_t SERVICE_INIT_MAX_NUM = 20; constexpr int32_t DM_STRING_LENGTH_MAX = 1024; -uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) -{ - std::random_device randDevice; - std::mt19937 genRand(randDevice()); - std::uniform_int_distribution disRand(randMin, randMax); - return disRand(genRand); -} - -uint16_t GenRandUint16() -{ - uint64_t currentTickMillseconds = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()).count(); - return static_cast(currentTickMillseconds % UINT16_MAX); -} - DeviceManagerImpl &DeviceManagerImpl::GetInstance() { static DeviceManagerImpl instance; @@ -1800,6 +1785,7 @@ int32_t DeviceManagerImpl::StartAdvertising(const std::string &pkgName, advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(publishId); } else { publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str()); + randUint16Set_.emplace(publishId); } std::string adverParaStr = ConvertMapToJsonString(advertiseParam); @@ -1974,13 +1960,14 @@ uint16_t DeviceManagerImpl::AddDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId = DM_INVALID_FLAG_ID; if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) { subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + randUint16Set_.emplace(subscribeId); } std::string pkgNameTemp = ComposeStr(pkgName, subscribeId); { std::lock_guard autoLock(subMapLock); auto item = pkgName2SubIdMap_.find(pkgNameTemp); if (item == pkgName2SubIdMap_.end() && subscribeId == DM_INVALID_FLAG_ID) { - subscribeId = GenRandUint16(); + subscribeId = GetUniqueRandUint(); pkgName2SubIdMap_[pkgNameTemp] = subscribeId; } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) { pkgName2SubIdMap_[pkgNameTemp] = subscribeId; @@ -2002,6 +1989,7 @@ uint16_t DeviceManagerImpl::RemoveDiscoveryCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { subscribeId = pkgName2SubIdMap_[pkgName]; + randUint16Set_.erase(subscribeId); pkgName2SubIdMap_.erase(pkgName); } } @@ -2018,7 +2006,7 @@ int32_t DeviceManagerImpl::AddPublishCallback(const std::string &pkgName) if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; } else { - publishId = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); + publishId = GetUniqueRandUint(); pkgName2PubIdMap_[pkgName] = publishId; } } @@ -2033,6 +2021,7 @@ int32_t DeviceManagerImpl::RemovePublishCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; + randUint16Set_.erase(publishId); pkgName2PubIdMap_.erase(pkgName); } } @@ -2823,5 +2812,22 @@ int32_t DeviceManagerImpl::GetLocalServiceInfoByBundleNameAndPinExchangeType( LOGI("Completed"); return DM_OK; } + +uint16_t DeviceManagerImpl::GetUniqueRandUint() +{ + uint16_t randInt = DM_INVALID_FLAG_ID; + bool isExist = false; + do { + randInt = GenRandInt(DM_MIN_RANDOM, DM_MAX_RANDOM); + if (randUint16Set_.find(randInt) != randUint16Set_.end()) { + LOGE("The randInt: %{public}d is exist.", randInt); + isExist = true; + } else { + isExist = false; + } + } while (isExist); + randUint16Set_.emplace(randInt); + return randInt; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/include/discovery/discovery_manager.h b/services/service/include/discovery/discovery_manager.h index 7839fd1b9..7aeb5b87c 100644 --- a/services/service/include/discovery/discovery_manager.h +++ b/services/service/include/discovery/discovery_manager.h @@ -100,12 +100,17 @@ private: std::string AddMultiUserIdentify(const std::string &pkgName); std::string RemoveMultiUserIdentify(const std::string &pkgName); void GetPkgNameAndUserId(const std::string &pkgName, std::string &callerPkgName, int32_t &userId); + int32_t GenInnerSubId(const std::string &pkgName, uint16_t subId); + int32_t GetAndRemoveInnerSubId(const std::string &pkgName, uint16_t subId); private: std::mutex locks_; std::mutex subIdMapLocks_; std::shared_ptr timer_; - std::map pkgName2SubIdMap_; + // std::map pkgName2SubIdMap_; + // Caller subId to inner subId. The key is the caller pkgName, the value is + // a list of externals subId to inner subId. + std::map> pkgName2SubIdMap_; std::shared_ptr softbusListener_; std::shared_ptr mineSoftbusListener_; std::shared_ptr listener_; @@ -122,6 +127,8 @@ private: static IDeviceProfileConnector *dpConnector_; static void *dpConnectorHandle_; #endif + + std::set randSubIdSet_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 9bbfba67a..c90f16cd2 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -32,6 +32,8 @@ const uint16_t DM_INVALID_FLAG_ID = 0; constexpr const char* LNN_DISC_CAPABILITY = "capability"; const std::string TYPE_MINE = "findDeviceMode"; const int32_t DECIMALISM = 10; +const uint16_t DM_MIN_RANDOM = 1; +const uint16_t DM_MAX_RANDOM = UINT16_MAX; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) static std::mutex comDependencyLoadLock; @@ -79,12 +81,10 @@ int32_t DiscoveryManager::EnableDiscoveryListener(const std::string &pkgName, std::string metaType = discoverParam.find(PARAM_KEY_META_TYPE)->second; LOGI("EnableDiscoveryListener, input MetaType = %{public}s in discoverParam map.", metaType.c_str()); } - if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) { - dmSubInfo.subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); - { - std::lock_guard autoLock(subIdMapLocks_); - pkgName2SubIdMap_[pkgNameTemp] = dmSubInfo.subscribeId; - } + if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end() && + IsNumberString((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second))) { + uint16_t subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, subscribeId); } if (discoverParam.find(PARAM_KEY_DISC_CAPABILITY) != discoverParam.end()) { std::string capability = discoverParam.find(PARAM_KEY_DISC_CAPABILITY)->second; @@ -121,13 +121,10 @@ int32_t DiscoveryManager::DisableDiscoveryListener(const std::string &pkgName, LOGI("DisableDiscoveryListener, input MetaType = %{public}s", (extraParam.find(PARAM_KEY_META_TYPE)->second).c_str()); } - uint16_t subscribeId = DM_INVALID_FLAG_ID; + uint16_t innerSubId = DM_INVALID_FLAG_ID; if (extraParam.find(PARAM_KEY_SUBSCRIBE_ID) != extraParam.end()) { - subscribeId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); - { - std::lock_guard autoLock(subIdMapLocks_); - pkgName2SubIdMap_.erase(pkgNameTemp); - } + uint16_t subscribeId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); } { std::lock_guard capLock(capabilityMapLocks_); @@ -136,7 +133,11 @@ int32_t DiscoveryManager::DisableDiscoveryListener(const std::string &pkgName, } } softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); - return softbusListener_->StopRefreshSoftbusLNN(subscribeId); + if (innerSubId == DM_INVALID_FLAG_ID) { + LOGE("Invalid parameter, cannot find subscribeId in cache map."); + return ERR_DM_INPUT_PARA_INVALID; + } + return softbusListener_->StopRefreshSoftbusLNN(innerSubId); } int32_t DiscoveryManager::StartDiscovering(const std::string &pkgName, @@ -150,6 +151,7 @@ int32_t DiscoveryManager::StartDiscovering(const std::string &pkgName, std::string pkgNameTemp = AddMultiUserIdentify(pkgName); DmSubscribeInfo dmSubInfo; ConfigDiscParam(discoverParam, &dmSubInfo); + dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, dmSubInfo.subscribeId); if (HandleDiscoveryQueue(pkgNameTemp, dmSubInfo.subscribeId, filterOptions) != DM_OK) { return ERR_DM_DISCOVERY_REPEATED; } @@ -193,7 +195,7 @@ void DiscoveryManager::ConfigDiscParam(const std::map dmSubInfo->isSameAccount = false; dmSubInfo->isWakeRemote = false; if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) { - dmSubInfo->subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + dmSubInfo.subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); } if (discoverParam.find(PARAM_KEY_DISC_MEDIUM) != discoverParam.end()) { int32_t medium = std::atoi((discoverParam.find(PARAM_KEY_DISC_MEDIUM)->second).c_str()); @@ -326,6 +328,7 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s pkgNameSet_.erase(pkgNameTemp); } + if (discoveryContextMap_.find(pkgNameTemp) != discoveryContextMap_.end()) { discoveryContextMap_.erase(pkgNameTemp); if (timer_ != nullptr) { @@ -340,10 +343,15 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s } } softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); + uint16_t innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); + if (innerSubId == DM_INVALID_FLAG_ID) { + LOGE("Invalid parameter, cannot find subscribeId in cache map."); + return ERR_DM_INPUT_PARA_INVALID; + } #if (defined(MINE_HARMONY)) - return mineSoftbusListener_->StopRefreshSoftbusLNN(subscribeId); + return mineSoftbusListener_->StopRefreshSoftbusLNN(innerSubId); #else - return softbusListener_->StopRefreshSoftbusLNN(subscribeId); + return softbusListener_->StopRefreshSoftbusLNN(innerSubId); #endif } @@ -755,5 +763,48 @@ void DiscoveryManager::GetPkgNameAndUserId(const std::string &pkgName, std::stri } LOGE("find failed PkgName %{public}s.", pkgName.c_str()); } + +int32_t DiscoveryManager::GenInnerSubId(const std::string &pkgName, uint16_t subId) +{ + uint16_t tempSubId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(subIdMapLocks_); + if (pkgName2SubIdMap_[pkgName].find(subId) != pkgName2SubIdMap_[pkgName].end()) { + return pkgName2SubIdMap_[pkgName][subId]; + } + if (pkgName2SubIdMap_.find(pkgName) == pkgName2SubIdMap_.end()) { + pkgName2SubIdMap_[pkgName] = std::map(); + } + bool isExist = false; + do { + tempSubId = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); + if (randSubIdSet_.find(tempSubId) != randSubIdSet_.end()) { + LOGE("The tempSubId: %{public}d is exist.", tempSubId); + isExist = true; + } else { + isExist = false; + } + } while (isExist); + randSubIdSet_.emplace(tempSubId); + pkgName2SubIdMap_[pkgName][subId] = tempSubId; + } + return tempSubId; +} + +int32_t DiscoveryManager::GetAndRemoveInnerSubId(const std::string &pkgName, uint16_t subId) +{ + uint16_t tempSubId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(subIdMapLocks_); + if (pkgName2SubIdMap_[pkgName].find(subId) != pkgName2SubIdMap_[pkgName].end()) { + tempSubId = pkgName2SubIdMap_[pkgName][subId]; + randSubIdSet_.erase(tempSubId); + } + if (pkgName2SubIdMap_[pkgName].empty()) { + pkgName2SubIdMap_.erase(pkgName); + } + } + return tempSubId; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 7b88bb4b5..0fa7fe327 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -22,6 +22,7 @@ namespace OHOS { namespace DistributedHardware { int32_t GenRandInt(int32_t randMin, int32_t randMax); int64_t GenRandLongLong(int64_t randMin, int64_t randMax); +uint16_t GenRandUint(uint16_t randMin, uint16_t randMax); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 6649c2102..96dba21dd 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -43,5 +43,13 @@ int64_t GenRandLongLong(int64_t randMin, int64_t randMax) std::uniform_int_distribution disRand(randMin, randMax); return disRand(genRand); } + +uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) +{ + std::random_device randDevice; + std::mt19937 genRand(randDevice()); + std::uniform_int_distribution disRand(randMin, randMax); + return disRand(genRand); +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From 7fd54fcbc775bafba9e2ab0d4ec5204fd063ece5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Fri, 14 Mar 2025 15:15:50 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=85=E9=99=A4?= =?UTF-8?q?=E5=8F=8A=E5=9B=9E=E8=B0=83=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../src/discovery/discovery_manager.cpp | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index c90f16cd2..4d47de5ea 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -412,7 +412,10 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t uint16_t subscribeId = 0; { std::lock_guard autoLock(subIdMapLocks_); - subscribeId = pkgName2SubIdMap_[pkgName]; + for (auto iter : pkgName2SubIdMap_[pkgName]) { + subscribeId = iter.second; + break; + } } LOGD("OnDeviceFound, pkgName = %{public}s, cabability = %{public}d", pkgName.c_str(), capabilityType); listener_->OnDeviceFound(processInfo, subscribeId, info); @@ -456,10 +459,17 @@ void DiscoveryManager::OnDiscoveringResult(const std::string &pkgName, int32_t s LOGE("DiscoveryManager::OnDiscoveringResult failed, IDeviceManagerServiceListener is null."); return; } + uint16_t externalSubId = DM_INVALID_FLAG_ID; + for (auto iter : pkgName2SubIdMap_[pkgName]) { + if (iter.second == subscribeId) { + externalSubId = iter.first; + break; + } + } if (result == 0) { std::lock_guard autoLock(locks_); discoveryContextMap_[pkgName].subscribeId = (uint32_t)subscribeId; - listener_->OnDiscoverySuccess(processInfo, subscribeId); + listener_->OnDiscoverySuccess(processInfo, externalSubId); return; } { @@ -480,7 +490,7 @@ void DiscoveryManager::OnDiscoveringResult(const std::string &pkgName, int32_t s capabilityMap_.erase(pkgName); } } - listener_->OnDiscoveryFailed(processInfo, (uint32_t)subscribeId, result); + listener_->OnDiscoveryFailed(processInfo, (uint32_t)externalSubId, result); softbusListener_->StopRefreshSoftbusLNN(subscribeId); } @@ -696,15 +706,12 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk } { std::lock_guard autoLock(subIdMapLocks_); - for (auto it = pkgName2SubIdMap_.begin(); it != pkgName2SubIdMap_.end();) { - if (it->first.find(pkgName) != std::string::npos) { - LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", it->first.c_str()); - subscribeIdSet.insert(it->second); - it = pkgName2SubIdMap_.erase(it); - } else { - ++it; - } + for (auto iter : pkgName2SubIdMap_[pkgName]) { + subscribeIdSet.insert(iter.second); + randSubIdSet_.erase(iter.second); } + pkgName2SubIdMap_.erase(pkgName); + LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", pkgName.c_str()); } return subscribeIdSet; } -- Gitee From 8b12bea7a80578133f2e714c111253c56b6732d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Fri, 14 Mar 2025 16:16:40 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../service/include/discovery/discovery_manager.h | 1 - .../service/src/discovery/discovery_manager.cpp | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/services/service/include/discovery/discovery_manager.h b/services/service/include/discovery/discovery_manager.h index 7aeb5b87c..d4840f208 100644 --- a/services/service/include/discovery/discovery_manager.h +++ b/services/service/include/discovery/discovery_manager.h @@ -107,7 +107,6 @@ private: std::mutex locks_; std::mutex subIdMapLocks_; std::shared_ptr timer_; - // std::map pkgName2SubIdMap_; // Caller subId to inner subId. The key is the caller pkgName, the value is // a list of externals subId to inner subId. std::map> pkgName2SubIdMap_; diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 4d47de5ea..6e39b3362 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -122,7 +122,8 @@ int32_t DiscoveryManager::DisableDiscoveryListener(const std::string &pkgName, (extraParam.find(PARAM_KEY_META_TYPE)->second).c_str()); } uint16_t innerSubId = DM_INVALID_FLAG_ID; - if (extraParam.find(PARAM_KEY_SUBSCRIBE_ID) != extraParam.end()) { + if (extraParam.find(PARAM_KEY_SUBSCRIBE_ID) != extraParam.end() && + IsNumberString(extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second)) { uint16_t subscribeId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); } @@ -195,7 +196,7 @@ void DiscoveryManager::ConfigDiscParam(const std::map dmSubInfo->isSameAccount = false; dmSubInfo->isWakeRemote = false; if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) { - dmSubInfo.subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + dmSubInfo->subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); } if (discoverParam.find(PARAM_KEY_DISC_MEDIUM) != discoverParam.end()) { int32_t medium = std::atoi((discoverParam.find(PARAM_KEY_DISC_MEDIUM)->second).c_str()); @@ -328,7 +329,6 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s pkgNameSet_.erase(pkgNameTemp); } - if (discoveryContextMap_.find(pkgNameTemp) != discoveryContextMap_.end()) { discoveryContextMap_.erase(pkgNameTemp); if (timer_ != nullptr) { @@ -409,16 +409,16 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t return; } } - uint16_t subscribeId = 0; + uint16_t externalSubId = 0; { std::lock_guard autoLock(subIdMapLocks_); for (auto iter : pkgName2SubIdMap_[pkgName]) { - subscribeId = iter.second; + externalSubId = iter.first; break; } } LOGD("OnDeviceFound, pkgName = %{public}s, cabability = %{public}d", pkgName.c_str(), capabilityType); - listener_->OnDeviceFound(processInfo, subscribeId, info); + listener_->OnDeviceFound(processInfo, externalSubId, info); return; } DiscoveryFilter filter; @@ -431,7 +431,7 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t } } LOGD("OnDeviceFound, pkgName = %{public}s, cabability = %{public}d", pkgName.c_str(), capabilityType); - listener_->OnDeviceFound(processInfo, discoveryContext.subscribeId, info); + listener_->OnDeviceFound(processInfo, externalSubId, info); } } -- Gitee From a74c0836c16ccd0fb9276a4f39550b8706ad54e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Fri, 14 Mar 2025 17:11:17 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- interfaces/inner_kits/native_cpp/BUILD.gn | 2 +- .../src/discovery/discovery_manager.cpp | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index 512634063..13dc6a6da 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -175,7 +175,7 @@ if (defined(ohos_lite)) { "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "${common_path}/src/json_object.cpp", "${devicemanager_path}/radar/src/dm_radar_helper.cpp", - "${utils_path}/src/dm_anonymous.cpp", + "${utils_path}/src/dm_random.cpp", "src/device_manager.cpp", "src/device_manager_impl.cpp", "src/dm_device_info.cpp", diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 6e39b3362..46e21c3bb 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -20,6 +20,7 @@ #include "dm_anonymous.h" #include "dm_constants.h" +#include "dm_random.h" #include "parameter.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "multiple_user_connector.h" @@ -83,8 +84,8 @@ int32_t DiscoveryManager::EnableDiscoveryListener(const std::string &pkgName, } if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end() && IsNumberString((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second))) { - uint16_t subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); - dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, subscribeId); + uint16_t externalSubId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, externalSubId); } if (discoverParam.find(PARAM_KEY_DISC_CAPABILITY) != discoverParam.end()) { std::string capability = discoverParam.find(PARAM_KEY_DISC_CAPABILITY)->second; @@ -124,8 +125,8 @@ int32_t DiscoveryManager::DisableDiscoveryListener(const std::string &pkgName, uint16_t innerSubId = DM_INVALID_FLAG_ID; if (extraParam.find(PARAM_KEY_SUBSCRIBE_ID) != extraParam.end() && IsNumberString(extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second)) { - uint16_t subscribeId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); - innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); + uint16_t externalSubId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, externalSubId); } { std::lock_guard capLock(capabilityMapLocks_); @@ -401,6 +402,14 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t discoveryContext = iter->second; } } + uint16_t externalSubId = 0; + { + std::lock_guard autoLock(subIdMapLocks_); + for (auto iter : pkgName2SubIdMap_[pkgName]) { + externalSubId = iter.first; + break; + } + } if (!isIndiscoveryContextMap) { { std::lock_guard capLock(capabilityMapLocks_); @@ -409,14 +418,6 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t return; } } - uint16_t externalSubId = 0; - { - std::lock_guard autoLock(subIdMapLocks_); - for (auto iter : pkgName2SubIdMap_[pkgName]) { - externalSubId = iter.first; - break; - } - } LOGD("OnDeviceFound, pkgName = %{public}s, cabability = %{public}d", pkgName.c_str(), capabilityType); listener_->OnDeviceFound(processInfo, externalSubId, info); return; @@ -460,12 +461,15 @@ void DiscoveryManager::OnDiscoveringResult(const std::string &pkgName, int32_t s return; } uint16_t externalSubId = DM_INVALID_FLAG_ID; - for (auto iter : pkgName2SubIdMap_[pkgName]) { + { + std::lock_guard autoLock(subIdMapLocks_); + for (auto iter : pkgName2SubIdMap_[pkgName]) { if (iter.second == subscribeId) { externalSubId = iter.first; break; } } + } if (result == 0) { std::lock_guard autoLock(locks_); discoveryContextMap_[pkgName].subscribeId = (uint32_t)subscribeId; -- Gitee From 45081f31dff5e6493e0a2e91986d775f318c26af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Sat, 15 Mar 2025 10:57:27 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- services/service/include/advertise/advertise_manager.h | 2 +- services/service/include/device_manager_service.h | 2 +- services/service/src/advertise/advertise_manager.cpp | 5 ++--- services/service/src/device_manager_service.cpp | 4 ++-- services/service/src/discovery/discovery_manager.cpp | 1 + services/service/src/ipc/standard/ipc_server_stub.cpp | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/service/include/advertise/advertise_manager.h b/services/service/include/advertise/advertise_manager.h index 2007807e6..92b680c13 100644 --- a/services/service/include/advertise/advertise_manager.h +++ b/services/service/include/advertise/advertise_manager.h @@ -28,7 +28,7 @@ public: int32_t StartAdvertising(const std::string &pkgName, const std::map &advertiseParam); int32_t StopAdvertising(const std::string &pkgName, int32_t publishId); - void ClearPulishIdCache(const std::string &pkgName); + void ClearPublishIdCache(const std::string &pkgName); private: void HandleAutoStopAdvertise(const std::string &timerName, const std::string &pkgName, int32_t publishId); diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index d2e3d5b3e..9631d39e8 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -229,7 +229,7 @@ public: int32_t UpdateLocalServiceInfo(const DMLocalServiceInfo &serviceInfo); int32_t GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string &bundleName, int32_t pinExchangeType, DMLocalServiceInfo &serviceInfo); - void ClearPulishIdCache(const std::string &pkgName); + void ClearPublishIdCache(const std::string &pkgName); bool IsPC(); private: diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index b18cd1713..aac3add48 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -154,8 +154,7 @@ int32_t AdvertiseManager::GenInnerPublishId(const std::string &pkgName, int32_t { std::lock_guard autoLock(pubMapLock_); if (pkgName2PubIdMap_[pkgName].find(publishId) != pkgName2PubIdMap_[pkgName].end()) { - softbusListener_->StopPublishSoftbusLNN(pkgName2PubIdMap_[pkgName][publishId]); - publishIdSet_.erase(pkgName2PubIdMap_[pkgName][publishId]); + return pkgName2PubIdMap_[pkgName][publishId]; } if (pkgName2PubIdMap_.find(pkgName) == pkgName2PubIdMap_.end()) { pkgName2PubIdMap_[pkgName] = std::map(); @@ -194,7 +193,7 @@ int32_t AdvertiseManager::GetAndRemoveInnerPublishId(const std::string &pkgName, return tempPublishId; } -void AdvertiseManager::ClearPulishIdCache(const std::string &pkgName) +void AdvertiseManager::ClearPublishIdCache(const std::string &pkgName) { LOGI("Begin for pkgName = %{public}s.", pkgName.c_str()); if (pkgName.empty()) { diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index b3b6aa7b5..9eb485a37 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -3188,10 +3188,10 @@ void DeviceManagerService::AddHmlInfoToBindParam(int32_t actionId, std::string & cJSON_Delete(bindParamObj); } -void DeviceManagerService::ClearPulishIdCache(const std::string &pkgName) +void DeviceManagerService::ClearPublishIdCache(const std::string &pkgName) { CHECK_NULL_VOID(advertiseMgr_); - advertiseMgr_->ClearPulishIdCache(pkgName); + advertiseMgr_->ClearPublishIdCache(pkgName); } bool DeviceManagerService::IsPC() diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 46e21c3bb..06e6ba92f 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -809,6 +809,7 @@ int32_t DiscoveryManager::GetAndRemoveInnerSubId(const std::string &pkgName, uin std::lock_guard autoLock(subIdMapLocks_); if (pkgName2SubIdMap_[pkgName].find(subId) != pkgName2SubIdMap_[pkgName].end()) { tempSubId = pkgName2SubIdMap_[pkgName][subId]; + pkgName2SubIdMap_[pkgName].erase(subId); randSubIdSet_.erase(tempSubId); } if (pkgName2SubIdMap_[pkgName].empty()) { diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 5ed3f75d6..8d47301a6 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -347,7 +347,7 @@ void AppDeathRecipient::OnRemoteDied(const wptr &remote) IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo); DeviceManagerService::GetInstance().ClearDiscoveryCache(processInfo); DeviceManagerServiceNotify::GetInstance().ClearDiedProcessCallback(processInfo); - DeviceManagerService::GetInstance().ClearPulishIdCache(processInfo.pkgName); + DeviceManagerService::GetInstance().ClearPublishIdCache(processInfo.pkgName); } void IpcServerStub::AddSystemSA(const std::string &pkgName) -- Gitee From 5b17a909ccb5a8ea75da33232ab7b24793e00674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Sat, 15 Mar 2025 11:27:01 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../native_cpp/include/device_manager_impl.h | 3 ++- .../native_cpp/src/device_manager_impl.cpp | 12 ++++----- .../src/discovery/discovery_manager.cpp | 11 +------- utils/include/dm_random.h | 2 ++ utils/src/dm_random.cpp | 25 +++++++++++++++++++ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 27413f333..1975ef161 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -459,7 +459,8 @@ private: std::string anonyLocalUdid_; - std::set randUint16Set_; + std::set randSubIdSet_; + std::set randPubIdSet_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index dfc5242e9..aed0eba7a 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -1785,7 +1785,7 @@ int32_t DeviceManagerImpl::StartAdvertising(const std::string &pkgName, advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(publishId); } else { publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str()); - randUint16Set_.emplace(publishId); + randPubIdSet_.emplace(publishId); } std::string adverParaStr = ConvertMapToJsonString(advertiseParam); @@ -1960,14 +1960,14 @@ uint16_t DeviceManagerImpl::AddDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId = DM_INVALID_FLAG_ID; if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) { subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); - randUint16Set_.emplace(subscribeId); + randSubIdSet_.emplace(subscribeId); } std::string pkgNameTemp = ComposeStr(pkgName, subscribeId); { std::lock_guard autoLock(subMapLock); auto item = pkgName2SubIdMap_.find(pkgNameTemp); if (item == pkgName2SubIdMap_.end() && subscribeId == DM_INVALID_FLAG_ID) { - subscribeId = GetUniqueRandUint(); + subscribeId = GetUniqueRandUint(randSubIdSet_); pkgName2SubIdMap_[pkgNameTemp] = subscribeId; } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) { pkgName2SubIdMap_[pkgNameTemp] = subscribeId; @@ -1989,7 +1989,7 @@ uint16_t DeviceManagerImpl::RemoveDiscoveryCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { subscribeId = pkgName2SubIdMap_[pkgName]; - randUint16Set_.erase(subscribeId); + randSubIdSet_.erase(subscribeId); pkgName2SubIdMap_.erase(pkgName); } } @@ -2006,7 +2006,7 @@ int32_t DeviceManagerImpl::AddPublishCallback(const std::string &pkgName) if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; } else { - publishId = GetUniqueRandUint(); + publishId = GetUniqueRandUint(randPubIdSet_); pkgName2PubIdMap_[pkgName] = publishId; } } @@ -2021,7 +2021,7 @@ int32_t DeviceManagerImpl::RemovePublishCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; - randUint16Set_.erase(publishId); + randPubIdSet_.erase(publishId); pkgName2PubIdMap_.erase(pkgName); } } diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 06e6ba92f..eb7378b7b 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -786,16 +786,7 @@ int32_t DiscoveryManager::GenInnerSubId(const std::string &pkgName, uint16_t sub if (pkgName2SubIdMap_.find(pkgName) == pkgName2SubIdMap_.end()) { pkgName2SubIdMap_[pkgName] = std::map(); } - bool isExist = false; - do { - tempSubId = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); - if (randSubIdSet_.find(tempSubId) != randSubIdSet_.end()) { - LOGE("The tempSubId: %{public}d is exist.", tempSubId); - isExist = true; - } else { - isExist = false; - } - } while (isExist); + tempSubId = GetUniqueRandUint(randSubIdSet_); randSubIdSet_.emplace(tempSubId); pkgName2SubIdMap_[pkgName][subId] = tempSubId; } diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 0fa7fe327..4c6ba87d9 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -17,12 +17,14 @@ #define OHOS_DM_RANDOM_H #include +#include namespace OHOS { namespace DistributedHardware { int32_t GenRandInt(int32_t randMin, int32_t randMax); int64_t GenRandLongLong(int64_t randMin, int64_t randMax); uint16_t GenRandUint(uint16_t randMin, uint16_t randMax); +uint16_t GetUniqueRandUint(std::set &randUint16Set_); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 96dba21dd..3bb857115 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -17,12 +17,20 @@ #include +#include "dm_log.h" + #if defined(__LITEOS_M__) #include #endif namespace OHOS { namespace DistributedHardware { +namespace { +const int32_t DM_MIN_RANDOM = 1; +const int32_t DM_MAX_RANDOM_UINT16 = INT16_MAX; +const int32_t DM_INVALID_FLAG_ID = 0; +} + int32_t GenRandInt(int32_t randMin, int32_t randMax) { #if defined(__LITEOS_M__) @@ -51,5 +59,22 @@ uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) std::uniform_int_distribution disRand(randMin, randMax); return disRand(genRand); } + +uint16_t GetUniqueRandUint(std::set &randUint16Set_) +{ + uint16_t randUint = DM_INVALID_FLAG_ID; + bool isExist = false; + do { + randUint = GenRandInt(DM_MIN_RANDOM, DM_MAX_RANDOM_UINT16); + if (randUint16Set_.find(randUint) != randUint16Set_.end()) { + LOGE("The randUint: %{public}d is exist.", randUint); + isExist = true; + } else { + isExist = false; + } + } while (isExist); + randUint16Set_.emplace(randUint); + return randUint; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From 788c48307ee1879e932c6233e46204c5d6a3cae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Sat, 15 Mar 2025 11:29:35 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../native_cpp/src/device_manager_impl.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index aed0eba7a..68f495573 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -2812,22 +2812,5 @@ int32_t DeviceManagerImpl::GetLocalServiceInfoByBundleNameAndPinExchangeType( LOGI("Completed"); return DM_OK; } - -uint16_t DeviceManagerImpl::GetUniqueRandUint() -{ - uint16_t randInt = DM_INVALID_FLAG_ID; - bool isExist = false; - do { - randInt = GenRandInt(DM_MIN_RANDOM, DM_MAX_RANDOM); - if (randUint16Set_.find(randInt) != randUint16Set_.end()) { - LOGE("The randInt: %{public}d is exist.", randInt); - isExist = true; - } else { - isExist = false; - } - } while (isExist); - randUint16Set_.emplace(randInt); - return randInt; -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee From bd8c4d5cc273e03accfa1b3253f482148eb03e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Sat, 15 Mar 2025 20:23:40 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../inner_kits/native_cpp/include/device_manager_impl.h | 1 - services/service/src/discovery/discovery_manager.cpp | 7 ++----- utils/include/dm_random.h | 2 +- utils/src/dm_random.cpp | 4 ++-- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 1975ef161..5a1e08887 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -444,7 +444,6 @@ private: uint16_t GetSubscribeIdFromMap(const std::string &pkgName); void SyncCallbackToService(DmCommonNotifyEvent dmCommonNotifyEvent, const std::string &pkgName); int32_t GetAnonyLocalUdid(const std::string &pkgName, std::string &anonyUdid); - uint16_t GetUniqueRandUint(); private: #if !defined(__LITEOS_M__) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index eb7378b7b..aacc68d66 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -33,8 +33,6 @@ const uint16_t DM_INVALID_FLAG_ID = 0; constexpr const char* LNN_DISC_CAPABILITY = "capability"; const std::string TYPE_MINE = "findDeviceMode"; const int32_t DECIMALISM = 10; -const uint16_t DM_MIN_RANDOM = 1; -const uint16_t DM_MAX_RANDOM = UINT16_MAX; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) static std::mutex comDependencyLoadLock; @@ -83,7 +81,7 @@ int32_t DiscoveryManager::EnableDiscoveryListener(const std::string &pkgName, LOGI("EnableDiscoveryListener, input MetaType = %{public}s in discoverParam map.", metaType.c_str()); } if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end() && - IsNumberString((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second))) { + IsNumberString((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second))) { uint16_t externalSubId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, externalSubId); } @@ -405,9 +403,8 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t uint16_t externalSubId = 0; { std::lock_guard autoLock(subIdMapLocks_); - for (auto iter : pkgName2SubIdMap_[pkgName]) { + if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { externalSubId = iter.first; - break; } } if (!isIndiscoveryContextMap) { diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 4c6ba87d9..fddfe4fb4 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -24,7 +24,7 @@ namespace DistributedHardware { int32_t GenRandInt(int32_t randMin, int32_t randMax); int64_t GenRandLongLong(int64_t randMin, int64_t randMax); uint16_t GenRandUint(uint16_t randMin, uint16_t randMax); -uint16_t GetUniqueRandUint(std::set &randUint16Set_); +uint16_t GetUniqueRandUint(std::set &randUint16Set); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 3bb857115..502bec439 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -60,13 +60,13 @@ uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) return disRand(genRand); } -uint16_t GetUniqueRandUint(std::set &randUint16Set_) +uint16_t GetUniqueRandUint(std::set &randUint16Set) { uint16_t randUint = DM_INVALID_FLAG_ID; bool isExist = false; do { randUint = GenRandInt(DM_MIN_RANDOM, DM_MAX_RANDOM_UINT16); - if (randUint16Set_.find(randUint) != randUint16Set_.end()) { + if (randUint16Set.find(randUint) != randUint16Set.end()) { LOGE("The randUint: %{public}d is exist.", randUint); isExist = true; } else { -- Gitee From e0c12badddbcb3e701ce49e7d6a7ea0e7f8978dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Mon, 17 Mar 2025 10:17:04 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- .../native_cpp/src/device_manager_impl.cpp | 2 -- .../src/discovery/discovery_manager.cpp | 20 +++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 68f495573..b4e4d8a49 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -95,8 +95,6 @@ constexpr const char* DM_HITRACE_GET_LOCAL_DEVICE_INFO = "DM_HITRACE_GET_LOCAL_D constexpr const char* DM_HITRACE_AUTH_TO_CONSULT = "DM_HITRACE_AUTH_TO_CONSULT"; constexpr const char* DM_HITRACE_INIT = "DM_HITRACE_INIT"; -const uint16_t DM_MIN_RANDOM = 1; -const uint16_t DM_MAX_RANDOM = 65535; const uint16_t DM_INVALID_FLAG_ID = 0; const uint16_t DM_IMPORT_AUTH_CODE_MIN_LENGTH = 6; const uint16_t DM_IMPORT_AUTH_CODE_MAX_LENGTH = 1024; diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index aacc68d66..09dadab58 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -322,6 +322,11 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s return ERR_DM_INPUT_PARA_INVALID; } std::string pkgNameTemp = RemoveMultiUserIdentify(pkgName); + uint16_t innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); + if (innerSubId == DM_INVALID_FLAG_ID) { + LOGE("Invalid parameter, cannot find subscribeId in cache map."); + return ERR_DM_INPUT_PARA_INVALID; + } { std::lock_guard autoLock(locks_); if (pkgNameSet_.find(pkgNameTemp) != pkgNameSet_.end()) { @@ -342,11 +347,6 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s } } softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); - uint16_t innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); - if (innerSubId == DM_INVALID_FLAG_ID) { - LOGE("Invalid parameter, cannot find subscribeId in cache map."); - return ERR_DM_INPUT_PARA_INVALID; - } #if (defined(MINE_HARMONY)) return mineSoftbusListener_->StopRefreshSoftbusLNN(innerSubId); #else @@ -404,7 +404,7 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t { std::lock_guard autoLock(subIdMapLocks_); if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { - externalSubId = iter.first; + externalSubId = pkgName2SubIdMap_[pkgName].first; } } if (!isIndiscoveryContextMap) { @@ -461,12 +461,12 @@ void DiscoveryManager::OnDiscoveringResult(const std::string &pkgName, int32_t s { std::lock_guard autoLock(subIdMapLocks_); for (auto iter : pkgName2SubIdMap_[pkgName]) { - if (iter.second == subscribeId) { - externalSubId = iter.first; - break; + if (iter.second == subscribeId) { + externalSubId = iter.first; + break; + } } } - } if (result == 0) { std::lock_guard autoLock(locks_); discoveryContextMap_[pkgName].subscribeId = (uint32_t)subscribeId; -- Gitee From 5408949d9eadf52f1dab13c6f284e44e0f660c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Mon, 17 Mar 2025 11:50:13 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- services/service/src/discovery/discovery_manager.cpp | 7 ++++--- utils/src/dm_random.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 09dadab58..969cb3058 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -400,11 +400,12 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t discoveryContext = iter->second; } } - uint16_t externalSubId = 0; + uint16_t externalSubId = DM_INVALID_FLAG_ID; { std::lock_guard autoLock(subIdMapLocks_); - if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { - externalSubId = pkgName2SubIdMap_[pkgName].first; + if (auto iter : pkgName2SubIdMap_[pkgName]) { + externalSubId = iter.first; + break; } } if (!isIndiscoveryContextMap) { diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 502bec439..11eb81fd9 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -73,7 +73,7 @@ uint16_t GetUniqueRandUint(std::set &randUint16Set) isExist = false; } } while (isExist); - randUint16Set_.emplace(randUint); + randUint16Set.emplace(randUint); return randUint; } } // namespace DistributedHardware -- Gitee