diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 43905e79d3f464803836b085c4615637dcd39021..9e980d9cc426411cc176b982a96c36da369e026e 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -28,6 +28,7 @@ #include "capability_info.h" #include "device_type.h" #include "dh_comm_tool.h" +#include "dh_modem_context_ext.h" #include "event_handler.h" #include "idistributed_hardware.h" #include "idistributed_hardware_sink.h" @@ -96,7 +97,10 @@ public: int32_t ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor); int32_t CheckIdenticalAccount(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor); - + int32_t EnableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); + int32_t DisableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); class ComponentManagerEventHandler : public AppExecFwk::EventHandler { public: ComponentManagerEventHandler(const std::shared_ptr runner); @@ -245,7 +249,12 @@ private: DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, bool isActive); int32_t RealDisableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor, DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status); - + int32_t EnableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); + int32_t DisableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); private: std::map compSource_; std::shared_mutex compSourceMutex_; diff --git a/services/distributedhardwarefwkservice/include/utils/dh_context.h b/services/distributedhardwarefwkservice/include/utils/dh_context.h index 77ea053cf35fbb0f0f16824004edc0326e052d6a..7e20de30673b6252118364de3bc3ac56e5dfd288 100644 --- a/services/distributedhardwarefwkservice/include/utils/dh_context.h +++ b/services/distributedhardwarefwkservice/include/utils/dh_context.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,6 +21,7 @@ #include #include #include +#include #include "device_type.h" #include "event_handler.h" @@ -82,6 +83,9 @@ public: size_t GetRealTimeOnlineDeviceCount(); void GetOnlineDeviceUdidHash(std::vector &udidHashVec); void GetOnlineDeviceDeviceId(std::vector &deviceIdVec); + void AddOnlineDeviceType(const std::string &networkId, uint16_t deviceType); + void DeleteOnlineDeviceType(const std::string &networkId); + uint16_t GetDeviceTypeByNetworkId(const std::string &networkId); /* DeviceId is which is hashed by sha256 */ std::string GetUUIDByDeviceId(const std::string &deviceId); /** @@ -129,6 +133,9 @@ private: std::unordered_set connectedDevIds_; std::shared_mutex connectDevMutex_; + + std::map onlineDevTypeMap_; + std::shared_mutex onlineDevTypeMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 1f6bf903dbd9beb1ac3b3828adc5609105e0b883..0b69b1f23b03ff50953c9ff06ee1b5d08ecb9e04 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -1656,5 +1656,164 @@ ActionResult ComponentManager::StopSink(DHType dhType) futures.emplace(dhType, f.share()); return futures; } + +int32_t ComponentManager::DisableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr) +{ + std::lock_guard lock(dhSourceStatusMtx_); + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + DHStatusCtrlKey ctrlKey { + .uid = 0, + .pid = 0 + }; + + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::DISABLED) { + DHLOGE("Repeat call DisableSource, DhType = %{public}#X.", dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Check enable reference count + if (enableInfo.refEnable > 1) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad > 1) { + DHLOGI("Meta disable, networkId = %{public}s", GetAnonyString(networkId).c_str()); + if (dhModemExt->Disable(networkId, sourcePtr) != DH_FWK_SUCCESS) { + DHLOGE("Meta disable failed, networkId = %{public}s.", GetAnonyString(networkId).c_str()); + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + auto ret = DisableMetaSourceInternal(networkId, dhDescriptor, statusCtrl, enableInfo, status, + dhModemExt, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableMetaSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::DisableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr) +{ + auto ret = dhModemExt->Disable(networkId, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Meta disable source failed, ret = %{public}d.", ret); + } + auto sourceResult = StopSource(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { + DHLOGE("StopSource timeout!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + ret = UninitCompSource(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSource failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable = 0; + status.refLoad = 0; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr) +{ + DHLOGI("EnableMetaSource enter"); + std::lock_guard lock(dhSourceStatusMtx_); + DHStatusSourceEnableInfoKey enableInfoKey { .networkId = networkId, .dhId = dhDescriptor.id }; + DHStatusCtrlKey ctrlKey { .uid = 0, .pid = 0 }; + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::ENABLED) { + DHLOGE("Repeat call EnableMetaSource, DhType = %{public}#X.", dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Check enable reference count + if (enableInfo.refEnable > 0) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad > 0) { + DHLOGI("Meta enable, networkId = %{public}s", GetAnonyString(networkId).c_str()); + if (dhModemExt->Enable(networkId, sourcePtr) != DH_FWK_SUCCESS) { + DHLOGW("Meta enable failed, networkId = %{public}s.", GetAnonyString(networkId).c_str()); + return ERR_DH_FWK_PARA_INVALID; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + auto ret = EnableMetaSourceInternal(networkId, dhDescriptor, statusCtrl, enableInfo, status, dhModemExt, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableMetaSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, + std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr) +{ + auto ret = InitCompSource(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("InitCompSource failed, ret = %{public}d.", ret); + return ret; + } + auto sourceResult = StartSource(dhDescriptor.dhType); + if (!WaitForResult(Action::START_SOURCE, sourceResult)) { + DHLOGE("StartSource failed, some virtual components maybe cannot work, but want to continue!"); + HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, + "dhfwk start source failed."); + UninitCompSource(dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT; + } + ret = dhModemExt->Enable(networkId, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableMeta failed, ret = %{public}d.", ret); + StopSource(dhDescriptor.dhType); + UninitCompSource(dhDescriptor.dhType); + return ret; + } + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable = 1; + status.refLoad = 1; + return ret; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index 1b6a741174e02665b994efc6956654fc79bf1df7..cf45d7224796eba47c786adde48394dca0d26bfd 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -184,6 +184,7 @@ int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &ne DHContext::GetInstance().AddOnlineDevice(udid, uuid, networkId); DHContext::GetInstance().AddRealTimeOnlineDeviceNetworkId(networkId); + DHContext::GetInstance().AddOnlineDeviceType(networkId, deviceType); if (!isInit_.load() && !Init()) { DHLOGE("distributedHardwareMgr is null"); diff --git a/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp b/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp index 4326e47663e3f378bd267ddb365213482c86fd9c..3aa24e6c9f693cfc5af41bf5bda4f0ca38549167 100644 --- a/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/meta_disable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,6 +21,8 @@ #include "anonymous_string.h" #include "component_manager.h" +#include "component_loader.h" +#include "dh_context.h" #include "dh_modem_context_ext.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -69,6 +71,7 @@ void MetaDisableTask::DoTaskInner() std::string taskId = GetId(); std::shared_ptr father = GetFatherTask().lock(); TaskBoard::GetInstance().RemoveTask(taskId); + DHContext::GetInstance().DeleteOnlineDeviceType(GetNetworkId()); /* if finish task, notify father finish */ if (father != nullptr) { auto offLineTask = std::static_pointer_cast(father); @@ -78,23 +81,32 @@ void MetaDisableTask::DoTaskInner() int32_t MetaDisableTask::Disable() { - IDistributedHardwareSource *sourcePtr = ComponentManager::GetInstance().GetDHSourceInstance(DHType::MODEM); + DHLOGI("MetaDisableTask enter."); + IDistributedHardwareSource *sourcePtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSource(DHType::MODEM, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get Modem Source failed."); + return ret; + } if (sourcePtr == nullptr) { DHLOGW("GetDHSourceInstance is nullptr."); return ERR_DH_FWK_POINTER_IS_NULL; } - std::shared_ptr distributedModemExt_ = - DHModemContextExt::GetInstance().GetModemExtInstance(); - if (distributedModemExt_ == nullptr) { + std::shared_ptr dhModemExt = DHModemContextExt::GetInstance().GetModemExtInstance(); + if (dhModemExt == nullptr) { DHLOGE("GetModemExtInstance is nullptr."); return ERR_DH_FWK_POINTER_IS_NULL; } - DHLOGI("Meta disable, networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str()); - if (distributedModemExt_->Disable(GetNetworkId(), sourcePtr) != DH_FWK_SUCCESS) { - DHLOGW("Meta disable failed, dhId = %{public}s.", GetAnonyString(GetDhId()).c_str()); - return ERR_DH_FWK_PARA_INVALID; + + DHDescriptor dhDescriptor { + .id = GetDhId(), + .dhType = GetDhType() + }; + ret = ComponentManager::GetInstance().DisableMetaSource(GetNetworkId(), dhDescriptor, dhModemExt, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableMetaSource DhType = %{public}#X, failed!", GetDhType()); } - return DH_FWK_SUCCESS; + return ret; } } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkservice/src/task/meta_enable_task.cpp b/services/distributedhardwarefwkservice/src/task/meta_enable_task.cpp index 5a356426f343f7205db887242443f561db03d162..50dca43fe31636d3a73a3c9f98aa190225660a3b 100644 --- a/services/distributedhardwarefwkservice/src/task/meta_enable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/meta_enable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,6 +21,7 @@ #include "anonymous_string.h" #include "component_manager.h" +#include "component_loader.h" #include "device_type.h" #include "dh_modem_context_ext.h" #include "distributed_hardware_errno.h" @@ -70,27 +71,36 @@ void MetaEnableTask::DoTaskInner() int32_t MetaEnableTask::Enable() { + DHLOGI("MetaEnableTask enter."); if (DHModemContextExt::GetInstance().GetHandler() != DH_FWK_SUCCESS) { DHLOGE("load distributed modem_ext so failed"); } - IDistributedHardwareSource *sourcePtr = ComponentManager::GetInstance().GetDHSourceInstance(DHType::MODEM); + IDistributedHardwareSource *sourcePtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSource(DHType::MODEM, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get Modem Source failed."); + return ret; + } if (sourcePtr == nullptr) { DHLOGW("GetDHSourceInstance is nullptr."); return ERR_DH_FWK_POINTER_IS_NULL; } - std::shared_ptr distributedModemExt_ = - DHModemContextExt::GetInstance().GetModemExtInstance(); - if (distributedModemExt_ == nullptr) { + + std::shared_ptr dhModemExt = DHModemContextExt::GetInstance().GetModemExtInstance(); + if (dhModemExt == nullptr) { DHLOGE("GetModemExtInstance is nullptr."); return ERR_DH_FWK_POINTER_IS_NULL; } - DHLOGI("Meta enable, networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str()); - if (distributedModemExt_->Enable(GetNetworkId(), sourcePtr) != DH_FWK_SUCCESS) { - DHLOGW("Meta enable failed, dhId = %{public}s udid = %{public}s.", - GetAnonyString(GetDhId()).c_str(), GetAnonyString(GetUDID()).c_str()); - return ERR_DH_FWK_PARA_INVALID; + + DHDescriptor dhDescriptor { + .id = GetDhId(), + .dhType = GetDhType() + }; + ret = ComponentManager::GetInstance().EnableMetaSource(GetNetworkId(), dhDescriptor, dhModemExt, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableMetaSource DhType = %{public}#X, failed!", GetDhType()); } - return DH_FWK_SUCCESS; + return ret; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/offline_task.cpp b/services/distributedhardwarefwkservice/src/task/offline_task.cpp index a5cd375a9e1febab74bfa647f9bf5ca470d667c7..b4307fff4be8eb35c481d6e3594a838804a9b63a 100644 --- a/services/distributedhardwarefwkservice/src/task/offline_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/offline_task.cpp @@ -23,7 +23,6 @@ #include "anonymous_string.h" #include "capability_info_manager.h" #include "constants.h" -#include "device_manager.h" #include "dh_context.h" #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" @@ -36,6 +35,9 @@ namespace OHOS { namespace DistributedHardware { +namespace { + constexpr uint16_t PHONE_TYPE = 14; +} #undef DH_LOG_TAG #define DH_LOG_TAG "OffLineTask" @@ -213,17 +215,16 @@ void OffLineTask::CreateMetaDisableTask() { DHLOGI("CreateMetaDisableTask, networkId = %{public}s, uuid = %{public}s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str()); - int32_t deviceType = DmDeviceType::DEVICE_TYPE_UNKNOWN; - DeviceManager::GetInstance().GetDeviceType(DH_FWK_PKG_NAME, GetNetworkId(), deviceType); - if (deviceType != DmDeviceType::DEVICE_TYPE_PHONE) { - DHLOGI("CreateMetaDisableTask, offline device not phone, deviceType = %{public}d", deviceType); + uint16_t deviceType = DHContext::GetInstance().GetDeviceTypeByNetworkId(GetNetworkId()); + if (deviceType != PHONE_TYPE) { + DHLOGI("offline device not phone, deviceType = %{public}d", deviceType); return; } TaskParam taskParam = { .networkId = GetNetworkId(), .uuid = GetUUID(), - .dhId = GetDhId(), - .dhType = GetDhType() + .dhId = "Modem_1234", + .dhType = DHType::MODEM }; auto task = TaskFactory::GetInstance().CreateTask(TaskType::META_DISABLE, taskParam, shared_from_this()); TaskExecutor::GetInstance().PushTask(task); diff --git a/services/distributedhardwarefwkservice/src/task/online_task.cpp b/services/distributedhardwarefwkservice/src/task/online_task.cpp index f8ea343a4d6fd13e26fa6b30c5813a7b4bd36401..f3f2e05da32a69b09f6f773e1d5bea463f3ffe27 100644 --- a/services/distributedhardwarefwkservice/src/task/online_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/online_task.cpp @@ -17,10 +17,10 @@ #include "anonymous_string.h" #include "capability_info_manager.h" -#include "device_manager.h" #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "dh_context.h" #include "local_capability_info_manager.h" #include "meta_info_manager.h" #include "task_board.h" @@ -30,6 +30,9 @@ namespace OHOS { namespace DistributedHardware { +namespace { + constexpr uint16_t PHONE_TYPE = 14; +} #undef DH_LOG_TAG #define DH_LOG_TAG "OnLineTask" @@ -185,18 +188,17 @@ void OnLineTask::CreateMetaEnableTask() { DHLOGI("CreateMetaEnableTask, networkId: %{public}s, uuid: %{public}s, udid: %{public}s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str()); - int32_t deviceType = DmDeviceType::DEVICE_TYPE_UNKNOWN; - DeviceManager::GetInstance().GetDeviceType(DH_FWK_PKG_NAME, GetNetworkId(), deviceType); - if (deviceType != DmDeviceType::DEVICE_TYPE_PHONE) { - DHLOGI("CreateMetaEnableTask, online device not phone, deviceType = %{public}d", deviceType); + uint16_t deviceType = DHContext::GetInstance().GetDeviceTypeByNetworkId(GetNetworkId()); + if (deviceType != PHONE_TYPE) { + DHLOGI("offline device not phone, deviceType = %{public}d", deviceType); return; } TaskParam taskParam = { .networkId = GetNetworkId(), .uuid = GetUUID(), .udid = GetUDID(), - .dhId = GetDhId(), - .dhType = GetDhType() + .dhId = "Modem_1234", + .dhType = DHType::MODEM }; auto task = TaskFactory::GetInstance().CreateTask(TaskType::META_ENABLE, taskParam, shared_from_this()); TaskExecutor::GetInstance().PushTask(task); diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 28726c78e89536d603f42fca4b601a35bc591cd2..ae19d4d6d12d4706d5cabb18d0f7404e688104a9 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,6 +24,7 @@ #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "device_manager.h" #include "publisher.h" namespace OHOS { @@ -390,5 +391,34 @@ sptr DHContext::DHFWKIsomerismListener::AsObject() { return nullptr; } + +void DHContext::AddOnlineDeviceType(const std::string &networkId, uint16_t deviceType) +{ + DHLOGI("Add DeviceType, networkId: %{public}s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(onlineDevTypeMutex_); + onlineDevTypeMap_.insert(std::make_pair(networkId, deviceType)); +} + +void DHContext::DeleteOnlineDeviceType(const std::string &networkId) +{ + DHLOGI("Delete DeviceType, networkId: %{public}s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(onlineDevTypeMutex_); + auto iter = onlineDevTypeMap_.find(networkId); + if (iter != onlineDevTypeMap_.end()) { + onlineDevTypeMap_.erase(networkId); + } +} + +uint16_t DHContext::GetDeviceTypeByNetworkId(const std::string &networkId) +{ + DHLOGI("Get DeviceType networkId: %{public}s", GetAnonyString(networkId).c_str()); + std::shared_lock lock(onlineDevTypeMutex_); + auto iter = onlineDevTypeMap_.find(networkId); + if (iter == onlineDevTypeMap_.end()) { + DHLOGE("NetworkId not exist."); + return static_cast(DmDeviceType::DEVICE_TYPE_UNKNOWN); + } + return onlineDevTypeMap_[networkId]; +} } // namespace DistributedHardware } // namespace OHOS