From bde20c78ec790081df2ef0a2ebf93dc440e4a5d4 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 12:08:35 +0800 Subject: [PATCH 01/13] =?UTF-8?q?subId=E5=86=85=E9=83=A8=E7=AE=A1=E7=90=86?= 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 | 19 ++- .../include/advertise/advertise_manager.h | 2 +- .../service/include/device_manager_service.h | 2 +- .../include/discovery/discovery_manager.h | 8 +- .../src/advertise/advertise_manager.cpp | 5 +- .../service/src/device_manager_service.cpp | 4 +- .../src/discovery/discovery_manager.cpp | 118 +++++++++++++----- .../src/ipc/standard/ipc_server_stub.cpp | 2 +- .../UTTest_device_manager_service_two.cpp | 2 +- utils/include/dm_random.h | 3 + utils/src/dm_random.cpp | 33 +++++ 13 files changed, 148 insertions(+), 55 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index 188e23efb..d34c92f52 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_random.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 ef690405b..510c9a54f 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -464,6 +464,9 @@ private: std::map pkgName2PubIdMap_; std::string anonyLocalUdid_; + + 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 2b7920aa6..4e1553f45 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -24,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" @@ -98,8 +99,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; @@ -110,14 +109,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); -} - DeviceManagerImpl &DeviceManagerImpl::GetInstance() { static DeviceManagerImpl instance; @@ -1796,6 +1787,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()); + randPubIdSet_.emplace(publishId); } std::string adverParaStr = ConvertMapToJsonString(advertiseParam); @@ -1970,13 +1962,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()); + 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 = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); + subscribeId = GetUniqueRandUint(randSubIdSet_); pkgName2SubIdMap_[pkgNameTemp] = subscribeId; } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) { pkgName2SubIdMap_[pkgNameTemp] = subscribeId; @@ -1998,6 +1991,7 @@ uint16_t DeviceManagerImpl::RemoveDiscoveryCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) { subscribeId = pkgName2SubIdMap_[pkgName]; + randSubIdSet_.erase(subscribeId); pkgName2SubIdMap_.erase(pkgName); } } @@ -2014,7 +2008,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(randPubIdSet_); pkgName2PubIdMap_[pkgName] = publishId; } } @@ -2029,6 +2023,7 @@ int32_t DeviceManagerImpl::RemovePublishCallback(const std::string &pkgName) std::lock_guard autoLock(subMapLock); if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; + randPubIdSet_.erase(publishId); pkgName2PubIdMap_.erase(pkgName); } } 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 88805515b..a78a2152c 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -232,7 +232,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(); int32_t GetDeviceNetworkIdList(const std::string &pkgName, const NetworkIdQueryFilter &queryFilter, std::vector &networkIds); diff --git a/services/service/include/discovery/discovery_manager.h b/services/service/include/discovery/discovery_manager.h index 7839fd1b9..d4840f208 100644 --- a/services/service/include/discovery/discovery_manager.h +++ b/services/service/include/discovery/discovery_manager.h @@ -100,12 +100,16 @@ 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_; + // 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 +126,8 @@ private: static IDeviceProfileConnector *dpConnector_; static void *dpConnectorHandle_; #endif + + std::set randSubIdSet_; }; } // namespace DistributedHardware } // namespace OHOS 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 a747606cb..96db581d5 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -3283,10 +3283,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 0dd6b5ebf..a1d59f9d3 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" @@ -79,12 +80,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 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; @@ -121,13 +120,11 @@ 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; - 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 innerSubId = DM_INVALID_FLAG_ID; + if (extraParam.find(PARAM_KEY_SUBSCRIBE_ID) != extraParam.end() && + IsNumberString(extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second)) { + uint16_t externalSubId = std::atoi((extraParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str()); + innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, externalSubId); } { 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; } @@ -320,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()) { @@ -341,9 +348,9 @@ int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t s } softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); #if (defined(MINE_HARMONY)) - return mineSoftbusListener_->StopRefreshSoftbusLNN(subscribeId); + return mineSoftbusListener_->StopRefreshSoftbusLNN(innerSubId); #else - return softbusListener_->StopRefreshSoftbusLNN(subscribeId); + return softbusListener_->StopRefreshSoftbusLNN(innerSubId); #endif } @@ -393,6 +400,14 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t discoveryContext = iter->second; } } + uint16_t externalSubId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(subIdMapLocks_); + if (auto iter : pkgName2SubIdMap_[pkgName]) { + externalSubId = iter.first; + break; + } + } if (!isIndiscoveryContextMap) { { std::lock_guard capLock(capabilityMapLocks_); @@ -401,13 +416,8 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t return; } } - uint16_t subscribeId = 0; - { - std::lock_guard autoLock(subIdMapLocks_); - subscribeId = pkgName2SubIdMap_[pkgName]; - } 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; @@ -420,7 +430,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); } } @@ -448,10 +458,20 @@ 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; + { + 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; - listener_->OnDiscoverySuccess(processInfo, subscribeId); + listener_->OnDiscoverySuccess(processInfo, externalSubId); return; } { @@ -472,7 +492,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); } @@ -688,15 +708,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; } @@ -755,5 +772,40 @@ 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(); + } + tempSubId = GetUniqueRandUint(randSubIdSet_); + 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]; + pkgName2SubIdMap_[pkgName].erase(subId); + randSubIdSet_.erase(tempSubId); + } + if (pkgName2SubIdMap_[pkgName].empty()) { + pkgName2SubIdMap_.erase(pkgName); + } + } + return tempSubId; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 882758434..d185bf774 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -412,7 +412,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) diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index 61b869072..03669d05e 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -1491,7 +1491,7 @@ HWTEST_F(DeviceManagerServiceTest, RegisterLocalServiceInfo_201, testing::ext::T foregroundUserIds, backgroundUserIds); std::string pkgName = "pkgName"; - DeviceManagerService::GetInstance().ClearPulishIdCache(pkgName); + DeviceManagerService::GetInstance().ClearPublishIdCache(pkgName); DeviceManagerService::GetInstance().hichainListener_ = nullptr; } diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 7b88bb4b5..fddfe4fb4 100644 --- a/utils/include/dm_random.h +++ b/utils/include/dm_random.h @@ -17,11 +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 6649c2102..11eb81fd9 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__) @@ -43,5 +51,30 @@ 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); +} + +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 6bba9309548f25c4c64039a0aa36dcaecfcf3414 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 14:37:08 +0800 Subject: [PATCH 02/13] =?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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index a1d59f9d3..76d1bd821 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -403,7 +403,7 @@ void DiscoveryManager::OnDeviceFound(const std::string &pkgName, const uint32_t uint16_t externalSubId = DM_INVALID_FLAG_ID; { std::lock_guard autoLock(subIdMapLocks_); - if (auto iter : pkgName2SubIdMap_[pkgName]) { + for (auto iter : pkgName2SubIdMap_[pkgName]) { externalSubId = iter.first; break; } -- Gitee From 314479f6f426ad08546700f61dfa416a4b99fa05 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 19:19:51 +0800 Subject: [PATCH 03/13] =?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: 史晓晓 --- .../include/discovery/discovery_manager.h | 1 + .../src/advertise/advertise_manager.cpp | 3 +- .../src/discovery/discovery_manager.cpp | 37 +++++++++++++------ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/services/service/include/discovery/discovery_manager.h b/services/service/include/discovery/discovery_manager.h index d4840f208..a4c6ebc4c 100644 --- a/services/service/include/discovery/discovery_manager.h +++ b/services/service/include/discovery/discovery_manager.h @@ -102,6 +102,7 @@ private: 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); + int32_t StopDiscoveringByInnerSubId(const std::string &pkgName, uint16_t subscribeId); private: std::mutex locks_; diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index aac3add48..120020f0b 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -162,7 +162,8 @@ int32_t AdvertiseManager::GenInnerPublishId(const std::string &pkgName, int32_t bool isExist = false; do { tempPublishId = GenRandInt(DM_MIN_RANDOM, DM_MAX_RANDOM); - if (publishIdSet_.find(tempPublishId) != publishIdSet_.end()) { + if (publishIdSet_.find(tempPublishId) != publishIdSet_.end() || + tempPublishId == DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID) { LOGE("The tempPublishId: %{public}d is exist.", tempPublishId); isExist = true; } else { diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 76d1bd821..5553515ca 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -316,37 +316,48 @@ int32_t DiscoveryManager::StartDiscovering4MetaType(const std::string &pkgName, int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t subscribeId) { + LOGI("DiscoveryManager::StopDiscovering begin for pkgName = %{public}s.", pkgName.c_str()); if (pkgName.empty()) { LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } std::string pkgNameTemp = RemoveMultiUserIdentify(pkgName); - uint16_t innerSubId = GetAndRemoveInnerSubId(pkgNameTemp, subscribeId); + return StopDiscoveringByInnerSubId(pkgNameTemp, subscribeId); +} + +int32_t DiscoveryManager::StopDiscoveringByInnerSubId(const std::string &pkgName, uint16_t subscribeId) +{ + LOGI("DiscoveryManager::StopDiscovering begin for pkgName = %{public}s.", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + uint16_t innerSubId = GetAndRemoveInnerSubId(pkgName, 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()) { - pkgNameSet_.erase(pkgNameTemp); + if (pkgNameSet_.find(pkgName) != pkgNameSet_.end()) { + pkgNameSet_.erase(pkgName); } - if (discoveryContextMap_.find(pkgNameTemp) != discoveryContextMap_.end()) { - discoveryContextMap_.erase(pkgNameTemp); + if (discoveryContextMap_.find(pkgName) != discoveryContextMap_.end()) { + discoveryContextMap_.erase(pkgName); if (timer_ != nullptr) { - timer_->DeleteTimer(pkgNameTemp); + timer_->DeleteTimer(pkgName); } } } { std::lock_guard capLock(capabilityMapLocks_); - if (capabilityMap_.find(pkgNameTemp) != capabilityMap_.end()) { - capabilityMap_.erase(pkgNameTemp); + if (capabilityMap_.find(pkgName) != capabilityMap_.end()) { + capabilityMap_.erase(pkgName); } } - softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); + softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgName); #if (defined(MINE_HARMONY)) return mineSoftbusListener_->StopRefreshSoftbusLNN(innerSubId); #else @@ -534,7 +545,7 @@ int32_t DiscoveryManager::HandleDiscoveryQueue(const std::string &pkgName, uint1 void DiscoveryManager::HandleDiscoveryTimeout(const std::string &pkgName) { - LOGI("DiscoveryManager::HandleDiscoveryTimeout"); + LOGI("DiscoveryManager::HandleDiscoveryTimeout, pkgName: %{public}s.", pkgName.c_str()); uint16_t subscribeId = 0; { std::lock_guard autoLock(locks_); @@ -550,7 +561,11 @@ void DiscoveryManager::HandleDiscoveryTimeout(const std::string &pkgName) } subscribeId = discoveryContextMap_[pkgName].subscribeId; } - StopDiscovering(pkgName, subscribeId); + StopDiscoveringByInnerSubId(pkgName, subscribeId); + { + std::lock_guard autoLock(multiUserDiscLocks_); + multiUserDiscMap_.erase(pkgName); + } } void DiscoveryManager::UpdateInfoFreq( -- Gitee From 08475272999a4ef71568ed313fad8f26cf4e9f26 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 19:41:46 +0800 Subject: [PATCH 04/13] =?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: 史晓晓 --- .../service/src/advertise/advertise_manager.cpp | 2 ++ .../service/src/discovery/discovery_manager.cpp | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index 120020f0b..b85149d1a 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -19,6 +19,8 @@ #include "dm_log.h" #include "dm_publish_info.h" #include "dm_random.h" +#include "system_ability_definition.h" + namespace OHOS { namespace DistributedHardware { diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 5553515ca..5cda5199e 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -316,7 +316,6 @@ int32_t DiscoveryManager::StartDiscovering4MetaType(const std::string &pkgName, int32_t DiscoveryManager::StopDiscovering(const std::string &pkgName, uint16_t subscribeId) { - LOGI("DiscoveryManager::StopDiscovering begin for pkgName = %{public}s.", pkgName.c_str()); if (pkgName.empty()) { LOGE("Invalid parameter, pkgName is empty."); @@ -723,11 +722,16 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk } { std::lock_guard autoLock(subIdMapLocks_); - for (auto iter : pkgName2SubIdMap_[pkgName]) { - subscribeIdSet.insert(iter.second); - randSubIdSet_.erase(iter.second); + 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).second); + randSubIdSet_.erase((it->second).second); + it = pkgName2SubIdMap_.erase(it); + } else { + ++it; + } } - pkgName2SubIdMap_.erase(pkgName); LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", pkgName.c_str()); } return subscribeIdSet; -- Gitee From 7869d5520c481f3725d92f9412d8534c21b418b4 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 19:43:20 +0800 Subject: [PATCH 05/13] =?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 | 1 - 1 file changed, 1 deletion(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 5cda5199e..8f7df52fd 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -732,7 +732,6 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk ++it; } } - LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", pkgName.c_str()); } return subscribeIdSet; } -- Gitee From 70414fb26cf7fb7edea48ebc6bb315801715157e 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 19:45:47 +0800 Subject: [PATCH 06/13] =?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/advertise/advertise_manager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index b85149d1a..5a852f18a 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -21,7 +21,6 @@ #include "dm_random.h" #include "system_ability_definition.h" - namespace OHOS { namespace DistributedHardware { const int32_t AUTO_STOP_ADVERTISE_DEFAULT_TIME = 120; -- Gitee From 7aac43d2c3747d6c247d34c7979fd924e615a21e 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 21:12:59 +0800 Subject: [PATCH 07/13] =?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 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 8f7df52fd..9fee03f15 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -725,8 +725,10 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk 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).second); - randSubIdSet_.erase((it->second).second); + for (auto iter : it->second) { + subscribeIdSet.insert(iter.second); + randSubIdSet_.erase(iter.second); + } it = pkgName2SubIdMap_.erase(it); } else { ++it; -- Gitee From 63c1402db71c907336627dd0d7c97cfd67e5a992 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 21:52:03 +0800 Subject: [PATCH 08/13] =?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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 9fee03f15..480e419c0 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -151,10 +151,10 @@ 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; } + dmSubInfo.subscribeId = GenInnerSubId(pkgNameTemp, dmSubInfo.subscribeId); bool isStandardMetaNode = true; if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) { @@ -480,7 +480,7 @@ void DiscoveryManager::OnDiscoveringResult(const std::string &pkgName, int32_t s } if (result == 0) { std::lock_guard autoLock(locks_); - discoveryContextMap_[pkgName].subscribeId = (uint32_t)subscribeId; + discoveryContextMap_[pkgName].subscribeId = (uint32_t)externalSubId; listener_->OnDiscoverySuccess(processInfo, externalSubId); return; } -- Gitee From d117a515f0a188732ae7d60180792861629c1fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Tue, 18 Mar 2025 11:41:06 +0800 Subject: [PATCH 09/13] =?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 ++ interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp | 4 ++-- services/service/src/discovery/discovery_manager.cpp | 2 +- utils/include/dm_random.h | 2 +- utils/src/dm_random.cpp | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index d34c92f52..eb86d886f 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -82,6 +82,7 @@ if (defined(ohos_lite)) { "${samgr_lite_path}/kits/samgr", "${samgr_lite_path}/kits/registry", "${utils_lite_path}/include", + "${utils_path}/include", "//third_party/bounds_checking_function/include", "//third_party/json/include", "${interfaces_path}/c/ipc/include", @@ -97,6 +98,7 @@ if (defined(ohos_lite)) { "${common_path}/src/dm_constants.cpp", "${common_path}/src/json_object.cpp", "${devicemanager_path}/radar/src/lite/dm_radar_helper.cpp", + "${utils_path}/src/dm_random.cpp", "src/device_manager.cpp", "src/device_manager_impl.cpp", "src/dm_device_info.cpp", 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 4e1553f45..ce5e27635 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -1969,7 +1969,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 = GetUniqueRandUint(randSubIdSet_); + subscribeId = GenUniqueRandUint(randSubIdSet_); pkgName2SubIdMap_[pkgNameTemp] = subscribeId; } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) { pkgName2SubIdMap_[pkgNameTemp] = subscribeId; @@ -2008,7 +2008,7 @@ int32_t DeviceManagerImpl::AddPublishCallback(const std::string &pkgName) if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) { publishId = pkgName2PubIdMap_[pkgName]; } else { - publishId = GetUniqueRandUint(randPubIdSet_); + publishId = GenUniqueRandUint(randPubIdSet_); pkgName2PubIdMap_[pkgName] = publishId; } } diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 480e419c0..cdc44f665 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -804,7 +804,7 @@ int32_t DiscoveryManager::GenInnerSubId(const std::string &pkgName, uint16_t sub if (pkgName2SubIdMap_.find(pkgName) == pkgName2SubIdMap_.end()) { pkgName2SubIdMap_[pkgName] = std::map(); } - tempSubId = GetUniqueRandUint(randSubIdSet_); + tempSubId = GenUniqueRandUint(randSubIdSet_); randSubIdSet_.emplace(tempSubId); pkgName2SubIdMap_[pkgName][subId] = tempSubId; } diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index fddfe4fb4..901cfab0b 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 GenUniqueRandUint(std::set &randUint16Set); } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp index 11eb81fd9..0d5955f40 100644 --- a/utils/src/dm_random.cpp +++ b/utils/src/dm_random.cpp @@ -60,7 +60,7 @@ uint16_t GenRandUint(uint16_t randMin, uint16_t randMax) return disRand(genRand); } -uint16_t GetUniqueRandUint(std::set &randUint16Set) +uint16_t GenUniqueRandUint(std::set &randUint16Set) { uint16_t randUint = DM_INVALID_FLAG_ID; bool isExist = false; -- Gitee From 0c252c6eb8f7ab494f9165b95691c97ce6d5c1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Tue, 18 Mar 2025 14:12:45 +0800 Subject: [PATCH 10/13] =?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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index cdc44f665..5c4260a73 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -676,7 +676,12 @@ void DiscoveryManager::ClearDiscoveryCache(const ProcessInfo &processInfo) for (auto it : subscribeIdSet) { std::string pkgNameTemp = (ComposeStr(ComposeStr(processInfo.pkgName, it), processInfo.userId)); softbusListener_->UnRegisterSoftbusLnnOpsCbk(pkgNameTemp); - softbusListener_->StopRefreshSoftbusLNN(it); + uint16_t innerSubId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(subIdMapLocks_); + innerSubId = pkgName2SubIdMap_[pkgNameTemp][it]; + } + softbusListener_->StopRefreshSoftbusLNN(innerSubId); } CHECK_NULL_VOID(timer_); @@ -726,7 +731,7 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk if (it->first.find(pkgName) != std::string::npos) { LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", it->first.c_str()); for (auto iter : it->second) { - subscribeIdSet.insert(iter.second); + subscribeIdSet.insert(iter.first); randSubIdSet_.erase(iter.second); } it = pkgName2SubIdMap_.erase(it); -- Gitee From 9088560bbe806be6fa73d6da535cfdef228e530e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Tue, 18 Mar 2025 14:57:11 +0800 Subject: [PATCH 11/13] =?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 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index 5c4260a73..ae41727f2 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -688,6 +688,10 @@ void DiscoveryManager::ClearDiscoveryCache(const ProcessInfo &processInfo) for (auto it : subscribeIdSet) { std::string pkgNameTemp = (ComposeStr(ComposeStr(processInfo.pkgName, it), processInfo.userId)); timer_->DeleteTimer(pkgNameTemp); + { + std::lock_guard autoLock(subIdMapLocks_); + pkgName2SubIdMap_.erase(pkgNameTemp); + } } } @@ -734,9 +738,6 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk subscribeIdSet.insert(iter.first); randSubIdSet_.erase(iter.second); } - it = pkgName2SubIdMap_.erase(it); - } else { - ++it; } } } -- Gitee From 63da4a74eb007c49373f47e0a81d8cc692f7c10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Tue, 18 Mar 2025 15:28:00 +0800 Subject: [PATCH 12/13] =?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 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index ae41727f2..23caf7092 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -680,6 +680,8 @@ void DiscoveryManager::ClearDiscoveryCache(const ProcessInfo &processInfo) { std::lock_guard autoLock(subIdMapLocks_); innerSubId = pkgName2SubIdMap_[pkgNameTemp][it]; + randSubIdSet_.erase(innerSubId); + pkgName2SubIdMap_.erase(pkgNameTemp); } softbusListener_->StopRefreshSoftbusLNN(innerSubId); } @@ -688,10 +690,6 @@ void DiscoveryManager::ClearDiscoveryCache(const ProcessInfo &processInfo) for (auto it : subscribeIdSet) { std::string pkgNameTemp = (ComposeStr(ComposeStr(processInfo.pkgName, it), processInfo.userId)); timer_->DeleteTimer(pkgNameTemp); - { - std::lock_guard autoLock(subIdMapLocks_); - pkgName2SubIdMap_.erase(pkgNameTemp); - } } } @@ -731,12 +729,11 @@ std::set DiscoveryManager::ClearDiscoveryPkgName(const std::string &pk } { std::lock_guard autoLock(subIdMapLocks_); - for (auto it = pkgName2SubIdMap_.begin(); it != pkgName2SubIdMap_.end();) { + for (auto it = pkgName2SubIdMap_.begin(); it != pkgName2SubIdMap_.end(); ++it) { if (it->first.find(pkgName) != std::string::npos) { LOGI("Erase pkgname %{public}s from pkgName2SubIdMap_.", it->first.c_str()); for (auto iter : it->second) { subscribeIdSet.insert(iter.first); - randSubIdSet_.erase(iter.second); } } } -- Gitee From 92284a843a50ce2feb782303e7c8878d02e18674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=99=93=E6=99=93?= Date: Tue, 18 Mar 2025 16:15:25 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史晓晓 --- test/commonunittest/UTTest_discovery_manager.cpp | 4 ++-- test/unittest/UTTest_device_manager_service.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/commonunittest/UTTest_discovery_manager.cpp b/test/commonunittest/UTTest_discovery_manager.cpp index 95f3f90eb..6568eace6 100644 --- a/test/commonunittest/UTTest_discovery_manager.cpp +++ b/test/commonunittest/UTTest_discovery_manager.cpp @@ -99,7 +99,7 @@ HWTEST_F(DiscoveryManagerTest, DisableDiscoveryListener_002, testing::ext::TestS extraParam.insert(std::pair("META_TYPE", "ohos.test")); extraParam.insert(std::pair("SUBSCRIBE_ID", "ohos.test")); int32_t ret = manager->DisableDiscoveryListener(pkgName, extraParam); - EXPECT_EQ(true, checkSoftbusRes(ret)); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } HWTEST_F(DiscoveryManagerTest, DisableDiscoveryListener_003, testing::ext::TestSize.Level0) @@ -107,7 +107,7 @@ HWTEST_F(DiscoveryManagerTest, DisableDiscoveryListener_003, testing::ext::TestS std::string pkgName = "pkgName"; std::map extraParam; int32_t ret = manager->DisableDiscoveryListener(pkgName, extraParam); - EXPECT_EQ(true, checkSoftbusRes(ret)); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } HWTEST_F(DiscoveryManagerTest, StartDiscovering_001, testing::ext::TestSize.Level0) diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index 431d7e72b..08e5dd4e5 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -1736,7 +1736,7 @@ HWTEST_F(DeviceManagerServiceTest, StartDiscovering_003, testing::ext::TestSize. int32_t ret = DeviceManagerService::GetInstance().StartDiscovering(pkgName, discoverParam, filterOptions); EXPECT_TRUE(ret == SOFTBUS_IPC_ERR || ret == DM_OK || ret == SOFTBUS_DISCOVER_MANAGER_INNERFUNCTION_FAIL); ret = DeviceManagerService::GetInstance().StopDiscovering(pkgName, discoverParam); - EXPECT_EQ(ret, SOFTBUS_NETWORK_NOT_INIT); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -1783,7 +1783,7 @@ HWTEST_F(DeviceManagerServiceTest, StopDiscovering_003, testing::ext::TestSize.L EXPECT_CALL(*softbusListenerMock_, StopRefreshSoftbusLNN(_)).Times(::testing::AtLeast(1)) .WillOnce(Return(SOFTBUS_NETWORK_NOT_INIT)); int32_t ret = DeviceManagerService::GetInstance().StopDiscovering(pkgName, discoverParam); - EXPECT_EQ(ret, SOFTBUS_NETWORK_NOT_INIT); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -1858,7 +1858,7 @@ HWTEST_F(DeviceManagerServiceTest, DisableDiscoveryListener_004, testing::ext::T DeviceManagerService::GetInstance().InitDMServiceListener(); EXPECT_CALL(*softbusListenerMock_, StopRefreshSoftbusLNN(_)).WillOnce(Return(SOFTBUS_NETWORK_NOT_INIT)); int32_t ret = DeviceManagerService::GetInstance().DisableDiscoveryListener(pkgName, extraParam); - EXPECT_EQ(ret, SOFTBUS_NETWORK_NOT_INIT); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); DeviceManagerService::GetInstance().UninitDMServiceListener(); } -- Gitee