diff --git a/services/service/include/advertise/advertise_manager.h b/services/service/include/advertise/advertise_manager.h index e9aa5ad4aff82634ca66de95c19505bc37c8733e..8e0066a795332082cef85aeb613638de9117cf61 100644 --- a/services/service/include/advertise/advertise_manager.h +++ b/services/service/include/advertise/advertise_manager.h @@ -28,14 +28,21 @@ 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); private: void HandleAutoStopAdvertise(const std::string &timerName, const std::string &pkgName, int32_t publishId); - void ConfigAdvParam(const std::map &advertiseParam, DmPublishInfo *dmPubInfo); + void ConfigAdvParam(const std::map &advertiseParam, DmPublishInfo *dmPubInfo, + const std::string &pkgName); + int32_t AddPublishId(const std::string &pkgName, int32_t publishId); + int32_t RemovePublishId(const std::string &pkgName, int32_t publishId); private: std::shared_ptr timer_; std::shared_ptr softbusListener_; + + std::mutex pubMapLock; + std::map> pkgName2PubIdMap_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 3e9718169fd5f8f52e37f84c0f94ee3ac76978d2..6b70ff2d96c189ea33c048830c5cf1ee519d567b 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -230,6 +230,7 @@ public: int32_t UpdateServiceInfo(const DMServiceInfo &serviceInfo); int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); int32_t GetCallerServiceInfos(std::vector &serviceInfos); + void ClearPulishIdCache(const std::string &pkgName); private: bool IsDMServiceImplReady(); diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index ef2abd1e938ad5b2b6af1b31ee8a9d267f1515db..d8d66906e800984db0010f0e625ee14356acf165 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -15,6 +15,8 @@ #include "advertise_manager.h" +#include + #include "dm_constants.h" #include "dm_log.h" #include "dm_publish_info.h" @@ -23,6 +25,17 @@ namespace OHOS { namespace DistributedHardware { const int32_t AUTO_STOP_ADVERTISE_DEFAULT_TIME = 120; const std::string AUTO_STOP_ADVERTISE_TASK = "AutoStopAdvertisingTask"; +const uint16_t DM_MIN_RANDOM = 1; +const uint16_t DM_MAX_RANDOM = 65535; +const uint16_t DM_INVALID_FLAG_ID = 0; + +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); +} AdvertiseManager::AdvertiseManager(std::shared_ptr softbusListener) : softbusListener_(softbusListener) { @@ -43,7 +56,7 @@ int32_t AdvertiseManager::StartAdvertising(const std::string &pkgName, return ERR_DM_INPUT_PARA_INVALID; } DmPublishInfo dmPubInfo; - ConfigAdvParam(advertiseParam, &dmPubInfo); + ConfigAdvParam(advertiseParam, &dmPubInfo, pkgName); std::string capability = DM_CAPABILITY_OSD; if (advertiseParam.find(PARAM_KEY_DISC_CAPABILITY) != advertiseParam.end()) { capability = advertiseParam.find(PARAM_KEY_DISC_CAPABILITY)->second; @@ -84,7 +97,7 @@ int32_t AdvertiseManager::StartAdvertising(const std::string &pkgName, } void AdvertiseManager::ConfigAdvParam(const std::map &advertiseParam, - DmPublishInfo *dmPubInfo) + DmPublishInfo *dmPubInfo, const std::string &pkgName) { if (dmPubInfo == nullptr) { LOGE("ConfigAdvParam failed, dmPubInfo is nullptr."); @@ -100,7 +113,9 @@ void AdvertiseManager::ConfigAdvParam(const std::map & LOGI("StartAdvertising input MetaType=%{public}s", (advertiseParam.find(PARAM_KEY_META_TYPE)->second).c_str()); } if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) != advertiseParam.end()) { - dmPubInfo->publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str()); + int32_t publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str()); + LOGI("PublishId=%{public}d", publishId); + dmPubInfo->publishId = AddPublishId(pkgName, publishId); } if (advertiseParam.find(PARAM_KEY_DISC_MODE) != advertiseParam.end()) { dmPubInfo->mode = @@ -120,12 +135,18 @@ void AdvertiseManager::ConfigAdvParam(const std::map & int32_t AdvertiseManager::StopAdvertising(const std::string &pkgName, int32_t publishId) { - LOGI("AdvertiseManager::StopDiscovering begin for pkgName = %{public}s.", pkgName.c_str()); + LOGI("AdvertiseManager::StopDiscovering begin for pkgName = %{public}s, publishId = %{public}d.", pkgName.c_str(), + publishId); if (pkgName.empty()) { LOGE("Invalid parameter, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } - return softbusListener_->StopPublishSoftbusLNN(publishId); + int32_t tempPublishId = RemovePublishId(pkgName, publishId); + if (tempPublishId == DM_INVALID_FLAG_ID) { + LOGE("Failed: cannot find pkgName in cache map."); + return ERR_DM_INPUT_PARA_INVALID; + } + return softbusListener_->StopPublishSoftbusLNN(tempPublishId); } void AdvertiseManager::HandleAutoStopAdvertise(const std::string &timerName, const std::string &pkgName, @@ -134,5 +155,49 @@ void AdvertiseManager::HandleAutoStopAdvertise(const std::string &timerName, con LOGI("HandleAutoStopAdvertise, auto stop advertise task timeout, timerName=%{public}s", timerName.c_str()); StopAdvertising(pkgName, publishId); } + +int32_t AdvertiseManager::AddPublishId(const std::string &pkgName, int32_t publishId) +{ + int32_t tempPublishId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(pubMapLock); + if (pkgName2PubIdMap_[pkgName].find(publishId) != pkgName2PubIdMap_[pkgName].end()) { + softbusListener_->StopPublishSoftbusLNN(pkgName2PubIdMap_[pkgName][publishId]); + } + if (pkgName2PubIdMap_.find(pkgName) == pkgName2PubIdMap_.end()) { + pkgName2PubIdMap_[pkgName] = std::map(); + } + tempPublishId = GenRandUint(DM_MIN_RANDOM, DM_MAX_RANDOM); + pkgName2PubIdMap_[pkgName][publishId] = tempPublishId; + } + return tempPublishId; +} + +int32_t AdvertiseManager::RemovePublishId(const std::string &pkgName, int32_t publishId) +{ + uint16_t tempPublishId = DM_INVALID_FLAG_ID; + { + std::lock_guard autoLock(pubMapLock); + if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end() && + pkgName2PubIdMap_[pkgName].find(publishId) != pkgName2PubIdMap_[pkgName].end()) { + tempPublishId = pkgName2PubIdMap_[pkgName][publishId]; + pkgName2PubIdMap_[pkgName].erase(publishId); + } + if (pkgName2PubIdMap_[pkgName].empty()) { + pkgName2PubIdMap_.erase(pkgName); + } + } + return tempPublishId; +} + +void AdvertiseManager::ClearPulishIdCache(const std::string &pkgName) +{ + LOGI("Begin for pkgName = %{public}s.", pkgName.c_str()); + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return; + } + pkgName2PubIdMap_.erase(pkgName); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 3375acca07c131f58b9caf1af95e244f1cb3c2ca..1cf78cda908e4b9d7c8681d47cb1153178c338e5 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -2950,5 +2950,11 @@ void DeviceManagerService::AddHmlInfoToBindParam(int32_t actionId, std::string & bindParam = std::string(str); cJSON_Delete(bindParamObj); } + +void DeviceManagerService::ClearPulishIdCache(const std::string &pkgName) +{ + CHECK_NULL_VOID(advertiseMgr_); + advertiseMgr_->ClearPulishIdCache(pkgName); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index e9076897bbad870fccfbe5bc95c65e7396e2c2c0..c01017e48363e1c75942418c315a1b61e63515eb 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -355,6 +355,7 @@ void AppDeathRecipient::OnRemoteDied(const wptr &remote) IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo); DeviceManagerService::GetInstance().ClearDiscoveryCache(processInfo); DeviceManagerServiceNotify::GetInstance().ClearDiedProcessCallback(processInfo); + DeviceManagerService::GetInstance().ClearPulishIdCache(processInfo.pkgName); } void IpcServerStub::AddSystemSA(const std::string &pkgName) diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index 4e37e0fa6e6dcf5a54369a7dbd2b6ae5e4c782d2..11428534c283b3a89fd4cdf7b3a4eee4fababb30 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -200,7 +200,7 @@ HWTEST_F(DeviceManagerServiceTest, PublishDeviceDiscovery_004, testing::ext::Tes int ret = DeviceManagerService::GetInstance().PublishDeviceDiscovery(pkgName, publishInfo); pkgName = "1com.ohos.test1"; ret = DeviceManagerService::GetInstance().PublishDeviceDiscovery(pkgName, publishInfo); - EXPECT_TRUE(ret == SOFTBUS_IPC_ERR || ret == SOFTBUS_DISCOVER_MANAGER_DUPLICATE_PARAM); + EXPECT_TRUE(ret == SOFTBUS_IPC_ERR || ret == SOFTBUS_DISCOVER_MANAGER_DUPLICATE_PARAM || ret == DM_OK); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -246,7 +246,8 @@ HWTEST_F(DeviceManagerServiceTest, UnPublishDeviceDiscovery_003, testing::ext::T int32_t publishId = 1; DeviceManagerService::GetInstance().InitDMServiceListener(); int ret = DeviceManagerService::GetInstance().UnPublishDeviceDiscovery(pkgName, publishId); - EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR); + EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR || + ret == ERR_DM_INPUT_PARA_INVALID); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -1908,7 +1909,8 @@ HWTEST_F(DeviceManagerServiceTest, StopAdvertising_004, testing::ext::TestSize.L std::map advertiseParam; DeviceManagerService::GetInstance().InitDMServiceListener(); int32_t ret = DeviceManagerService::GetInstance().StopAdvertising(pkgName, advertiseParam); - EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR); + EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR || + ret == ERR_DM_INPUT_PARA_INVALID); DeviceManagerService::GetInstance().UninitDMServiceListener(); }