diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 9e980d9cc426411cc176b982a96c36da369e026e..d49d33a3cef2c913e7fba3b662d8d23b67f630ba 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -215,8 +215,6 @@ private: void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo); void DoRecover(DHType dhType); - void ReStartSA(DHType dhType); - void RecoverDistributedHardware(DHType dhType); bool IsIdenticalAccount(const std::string &networkId); int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m); @@ -255,6 +253,12 @@ private: int32_t DisableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); + void ResetSinkEnableStatus(DHType dhType); + void ResetSourceEnableStatus(DHType dhType); + void RecoverAutoEnableSink(DHType dhType); + void RecoverAutoEnableSource(DHType dhType); + void RecoverActiveEnableSink(DHType dhType); + void RecoverActiveEnableSource(DHType dhType); private: std::map compSource_; std::shared_mutex compSourceMutex_; diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 7067acf5833021141e322188b4ddffff5576d90a..35b43d0a9867225b3e7a00b99439ef45e8ecf6f3 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -620,55 +620,16 @@ void ComponentManager::DoRecover(DHType dhType) if (ret != DH_FWK_SUCCESS) { DHLOGE("DoRecover setname failed."); } - // step1: restart sa process - ReStartSA(dhType); - // step2: recover distributed hardware virtual driver - RecoverDistributedHardware(dhType); -} - -void ComponentManager::ReStartSA(DHType dhType) -{ - DHLOGI("Restart SA for DHType %{public}" PRIu32, (uint32_t)dhType); - auto sourceResult = StartSource(dhType); - auto sinkResult = StartSink(dhType); - - if (!WaitForResult(Action::START_SOURCE, sourceResult)) { - DHLOGE("ReStartSource failed, DHType: %{public}" PRIu32, (uint32_t)dhType); - } - - if (!WaitForResult(Action::START_SINK, sinkResult)) { - DHLOGE("ReStartSink failed, DHType: %{public}" PRIu32, (uint32_t)dhType); - } - DHLOGI("Finish Restart"); -} - -void ComponentManager::RecoverDistributedHardware(DHType dhType) -{ - MetaCapInfoMap metaInfoMap; - MetaInfoManager::GetInstance()->GetMetaDataByDHType(dhType, metaInfoMap); - for (const auto &metaInfo : metaInfoMap) { - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(metaInfo.second->GetDeviceId()); - if (uuid.empty()) { - DHLOGE("Can not find uuid by capability deviceId: %{public}s", - GetAnonyString(metaInfo.second->GetDeviceId()).c_str()); - continue; - } - - std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); - if (networkId.empty()) { - DHLOGI("Can not find network id by uuid: %{public}s", GetAnonyString(uuid).c_str()); - continue; - } - - TaskParam taskParam = { - .networkId = networkId, - .uuid = uuid, - .dhId = metaInfo.second->GetDHId(), - .dhType = metaInfo.second->GetDHType() - }; - auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); - TaskExecutor::GetInstance().PushTask(task); - } + // reset enable status + DHLOGI("Reset enable status for DHType %{public}" PRIu32, (uint32_t)dhType); + ResetSinkEnableStatus(dhType); + ResetSourceEnableStatus(dhType); + // recover distributed hardware virtual driver + DHLOGI("Recover distributed hardware virtual driver for DHType %{public}" PRIu32, (uint32_t)dhType); + RecoverAutoEnableSink(dhType); + RecoverAutoEnableSource(dhType); + RecoverActiveEnableSink(dhType); + RecoverActiveEnableSource(dhType); } std::map ComponentManager::GetDHSinkInstance() @@ -1199,7 +1160,7 @@ int32_t ComponentManager::DisableSinkInternal(const DHDescriptor &dhDescriptor, // Start disabling hardware sink auto sinkResult = StopSink(dhDescriptor.dhType); if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSource failed, but want to continue!"); + DHLOGE("StopSink failed, but want to continue!"); return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; } auto ret = UninitCompSink(dhDescriptor.dhType); @@ -1387,7 +1348,7 @@ int32_t ComponentManager::ForceDisableSinkInternal( // Unload component auto sinkResult = StopSink(dhDescriptor.dhType); if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSource failed, but want to continue!"); + DHLOGE("StopSink failed, but want to continue!"); return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; } auto ret = UninitCompSink(dhDescriptor.dhType); @@ -1517,6 +1478,209 @@ int32_t ComponentManager::RealDisableSource(const std::string &networkId, const return DH_FWK_SUCCESS; } +void ComponentManager::ResetSinkEnableStatus(DHType dhType) +{ + DHLOGI("ResetSinkEnableStatus begin, dhType = %{public}#X.", dhType); + std::lock_guard lock(dhSinkStatusMtx_); + auto ret = UninitCompSink(dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSink failed, ret = %{public}d, but want to continue!", ret); + } + auto itSinkStatus = dhSinkStatus_.find(dhType); + if (itSinkStatus == dhSinkStatus_.end()) { + DHLOGI("No need to reset the auto-enable-sink of this type, dhType: %{public}#X.", dhType); + return; + } + auto &sinkStatus = itSinkStatus->second; + sinkStatus.refLoad = 0; + for (auto &itEnableInfo : sinkStatus.enableInfos) { + auto &enableInfoKey = itEnableInfo.first; + auto &enableInfo = itEnableInfo.second; + enableInfo.refEnable = 0; + for (auto &itStatusCtrl : enableInfo.dhStatusCtrl) { + auto &statusCtrlKey = itStatusCtrl.first; + auto &statusCtrl = itStatusCtrl.second; + if (statusCtrlKey.uid == 0 && statusCtrlKey.pid == 0) { + statusCtrl.enableState = EnableState::DISABLED; + DHLOGI("reset the auto-enable-sink, dhId = %{public}s, dhType: %{public}#X.", + GetAnonyString(enableInfoKey).c_str(), dhType); + } + } + } + DHLOGI("ResetSinkEnableStatus end, dhType = %{public}#X.", dhType); +} + +void ComponentManager::ResetSourceEnableStatus(DHType dhType) +{ + DHLOGI("ResetSourceEnableStatus begin, dhType = %{public}#X.", dhType); + std::lock_guard lock(dhSourceStatusMtx_); + auto ret = UninitCompSource(dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSource failed, ret = %{public}d, but want to continue!", ret); + } + auto itSourceStatus = dhSourceStatus_.find(dhType); + if (itSourceStatus == dhSourceStatus_.end()) { + DHLOGI("No need to reset the auto-enable-source of this type, dhType: %{public}#X.", dhType); + return; + } + auto &sourcekStatus = itSourceStatus->second; + sourcekStatus.refLoad = 0; + for (auto &itEnableInfo : sourcekStatus.enableInfos) { + auto &enableInfoKey = itEnableInfo.first; + auto &enableInfo = itEnableInfo.second; + enableInfo.refEnable = 0; + for (auto &itStatusCtrl : enableInfo.dhStatusCtrl) { + auto &statusCtrlKey = itStatusCtrl.first; + auto &statusCtrl = itStatusCtrl.second; + if (statusCtrlKey.uid == 0 && statusCtrlKey.pid == 0) { + statusCtrl.enableState = EnableState::DISABLED; + DHLOGI("reset the auto-enable-source, networkId = %{public}s, dhId = %{public}s, dhType: %{public}#X.", + GetAnonyString(enableInfoKey.networkId).c_str(), + GetAnonyString(enableInfoKey.dhId).c_str(), dhType); + } + } + } + DHLOGI("ResetSourceEnableStatus end, dhType = %{public}#X.", dhType); +} + +void ComponentManager::RecoverAutoEnableSink(DHType dhType) +{ + DHLOGI("RecoverAutoEnableSink begin, dhType = %{public}#X.", dhType); + DeviceInfo localDeviceInfo = GetLocalDeviceInfo(); + std::vector> localMetaInfos; + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(localDeviceInfo.udidHash, metaCapInfos); + std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr localMetaInfo) { + if (localMetaInfo->GetDHType() == dhType) { + localMetaInfos.push_back({localMetaInfo->GetDHId(), localMetaInfo->GetDHType()}); + } + }); + if (localMetaInfos.empty()) { + DHLOGE("No need to recover auto enable sink, dhType = %{public}#X.", dhType); + return; + } + for (const auto &localInfo : localMetaInfos) { + TaskParam taskParam = { + .networkId = localDeviceInfo.networkId, + .uuid = localDeviceInfo.uuid, + .udid = localDeviceInfo.udid, + .dhId = localInfo.first, + .dhType = localInfo.second + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + DHLOGI("RecoverAutoEnableSink end, dhType = %{public}#X.", dhType); +} + +void ComponentManager::RecoverAutoEnableSource(DHType dhType) +{ + DHLOGI("RecoverAutoEnableSource begin, dhType = %{public}#X.", dhType); + MetaCapInfoMap metaInfoMap; + MetaInfoManager::GetInstance()->GetMetaDataByDHType(dhType, metaInfoMap); + for (const auto &metaInfo : metaInfoMap) { + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(metaInfo.second->GetDeviceId()); + if (uuid.empty()) { + DHLOGE("Can not find uuid by capability deviceId: %{public}s", + GetAnonyString(metaInfo.second->GetDeviceId()).c_str()); + continue; + } + + std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); + if (networkId.empty()) { + DHLOGI("Can not find network id by uuid: %{public}s", GetAnonyString(uuid).c_str()); + continue; + } + + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .dhId = metaInfo.second->GetDHId(), + .dhType = metaInfo.second->GetDHType() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + DHLOGI("RecoverAutoEnableSource end, dhType = %{public}#X.", dhType); +} + +void ComponentManager::RecoverActiveEnableSink(DHType dhType) +{ + DHLOGI("RecoverActiveEnableSink begin, dhType = %{public}#X.", dhType); + std::lock_guard lock(dhSinkStatusMtx_); + auto itSinkStatus = dhSinkStatus_.find(dhType); + if (itSinkStatus == dhSinkStatus_.end()) { + DHLOGE("No need to recover active enable sink, dhType = %{public}#X.", dhType); + return; + } + auto &sinkStatus = itSinkStatus->second; + for (auto &itEnableInfo : sinkStatus.enableInfos) { + auto &enableInfoKey = itEnableInfo.first; + auto &enableInfo = itEnableInfo.second; + for (auto &itStatusCtrl : enableInfo.dhStatusCtrl) { + auto &statusCtrlKey = itStatusCtrl.first; + auto &statusCtrl = itStatusCtrl.second; + if ((statusCtrlKey.uid != 0 || statusCtrlKey.pid != 0) + && statusCtrl.enableState == EnableState::ENABLED) { + statusCtrl.enableState = EnableState::DISABLED; + TaskParam taskParam = { + .dhId = enableInfoKey, + .dhType = dhType, + .effectSink = true, + .effectSource = false, + .callingUid = statusCtrlKey.uid, + .callingPid = statusCtrlKey.pid + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + DHLOGI("Create enable task for recover active-enable-sink, dhId = %{public}s, dhType = %{public}#X.", + GetAnonyString(enableInfoKey).c_str(), dhType); + } + } + } + DHLOGI("RecoverActiveEnableSink end, dhType = %{public}#X.", dhType); +} + +void ComponentManager::RecoverActiveEnableSource(DHType dhType) +{ + DHLOGI("RecoverActiveEnableSource begin, dhType = %{public}#X.", dhType); + std::lock_guard lock(dhSourceStatusMtx_); + auto itSourceStatus = dhSourceStatus_.find(dhType); + if (itSourceStatus == dhSourceStatus_.end()) { + DHLOGE("No need to recover active enable source, dhType = %{public}#X.", dhType); + return; + } + auto &sourcekStatus = itSourceStatus->second; + for (auto &itEnableInfo : sourcekStatus.enableInfos) { + auto &enableInfoKey = itEnableInfo.first; + auto &enableInfo = itEnableInfo.second; + for (auto &itStatusCtrl : enableInfo.dhStatusCtrl) { + auto &statusCtrlKey = itStatusCtrl.first; + auto &statusCtrl = itStatusCtrl.second; + if ((statusCtrlKey.uid != 0 || statusCtrlKey.pid != 0) + && statusCtrl.enableState == EnableState::ENABLED) { + statusCtrl.enableState = EnableState::DISABLED; + TaskParam taskParam = { + .networkId = enableInfoKey.networkId, + .dhId = enableInfoKey.dhId, + .dhType = dhType, + .effectSink = false, + .effectSource = true, + .callingUid = statusCtrlKey.uid, + .callingPid = statusCtrlKey.pid + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + DHLOGI("Create enable task for recover active-enable-sink, " + "networkId = %{public}s, dhId = %{public}s, dhType = %{public}#X.", + GetAnonyString(enableInfoKey.networkId).c_str(), + GetAnonyString(enableInfoKey.dhId).c_str(), dhType); + } + } + } + DHLOGI("RecoverActiveEnableSource end, dhType = %{public}#X.", dhType); +} + int32_t ComponentManager::InitCompSource(DHType dhType) { std::unique_lock lock(compSourceMutex_); diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 156aa6f36dce35180e34e38c7982b500f29f57de..8a74b6649767ade354f53f23886b4f1752b33ca7 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -926,25 +926,12 @@ HWTEST_F(ComponentManagerTest, DoRecover_001, TestSize.Level1) } /** - * @tc.name: ReStartSA_001 - * @tc.desc: Verify the ReStartSA function - * @tc.type: FUNC - * @tc.require: AR000GHSJM - */ -HWTEST_F(ComponentManagerTest, ReStartSA_001, TestSize.Level1) -{ - DHType dhType = DHType::CAMERA; - ComponentManager::GetInstance().ReStartSA(dhType); - EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); -} - -/** - * @tc.name: RecoverDistributedHardware_001 - * @tc.desc: Verify the RecoverDistributedHardware function + * @tc.name: DoRecover_002 + * @tc.desc: Verify the DoRecover function * @tc.type: FUNC * @tc.require: AR000GHSJM */ -HWTEST_F(ComponentManagerTest, RecoverDistributedHardware_001, TestSize.Level1) +HWTEST_F(ComponentManagerTest, DoRecover_002, TestSize.Level1) { CompVersion compVersion; compVersion.sinkVersion = "1.0"; @@ -954,15 +941,12 @@ HWTEST_F(ComponentManagerTest, RecoverDistributedHardware_001, TestSize.Level1) std::string udidHash = Sha256(UDID_TEST); std::string key = udidHash + "###" + DH_ID_TEST; MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapInfo; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverDistributedHardware(dhType)); - - DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, ""); - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = META_INFO_PTR_TEST; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverDistributedHardware(dhType)); - - DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, NETWORK_TEST); - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = META_INFO_PTR_TEST; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverDistributedHardware(dhType)); + ComponentManager::DHSinkStatus sinkStatus; + ComponentManager::GetInstance().dhSinkStatus_[dhType] = sinkStatus; + ComponentManager::DHSourceStatus sourceStatus; + ComponentManager::GetInstance().dhSourceStatus_[dhType] = sourceStatus; + ComponentManager::GetInstance().DoRecover(dhType); + EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); } /**