diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index 6b620d1ab7bf04b342ac3bb74b4c847c872cf0b7..13dc6a6da2347c904aff67c08771c8fd4e1c9b45 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 485ea6e272eafa1349ba0bfa45e2ca1f467db7ae..5a1e08887ae468b6467af274d95984d8cbd48c25 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -457,6 +457,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 6c0d3e70a6379b4cc185a019bf6169b2c1decf74..b4e4d8a498c73723fc130d3e1cb280e12b84af9e 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" @@ -94,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; @@ -106,14 +105,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; @@ -1792,6 +1783,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); @@ -1966,13 +1958,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; @@ -1994,6 +1987,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); } } @@ -2010,7 +2004,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; } } @@ -2025,6 +2019,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 2007807e64f3603456ec9122fc403891d1fe38a3..92b680c136516dfb302d6bf78461aaf4f546c305 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 d2e3d5b3e93583c22435f3a470bbc6e778b52a8a..9631d39e8b350cb0af333cda4377e01f80e58a4e 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/include/discovery/discovery_manager.h b/services/service/include/discovery/discovery_manager.h index 7839fd1b9038d7d954e7b45f20a1e0ebd3b4d1a1..d4840f208311ac1236df9a683ab93c573c44f4b5 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 b18cd17134bfe96e9bec2629565d8dad7dada22d..aac3add486236ab1cdd5ce85d2865da197cc4560 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 b3b6aa7b5a528ad24617008c1605acdda0ab3c8c..9eb485a37590d0080fdad713442552c769b76410 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 9bbfba67aaa39433471a4bfb5d26ba6dc956805b..969cb30586313042387475b4e4645df1a8e41e90 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 5ed3f75d68946407e834b3906bfaa76ec8be710f..8d47301a6ec0d9144587af0db951b10135833f42 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) diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h index 7b88bb4b5f59f420143135b99790daa38d232c49..fddfe4fb481924d226b8cf23849dc936df1ad33f 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 6649c2102078641f66c61599a5e22085e06f35a0..11eb81fd9c10e4907aad969aa8cb25b2c09a0af2 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