diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 06f425140584ff27d6ca83ca41681e17096869b1..0391994a2122d4e57beee74d6a4e7b364383c547 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -58,6 +58,13 @@ struct DeviceInfo { explicit DeviceInfo(std::string uuid, std::string deviceId, std::string deviceName, uint16_t deviceType) : uuid(uuid), deviceId(deviceId), deviceName(deviceName), deviceType(deviceType) {} }; + +/* The key is DHType, the value is the prefix of DHId */ +const std::unordered_map DHTypePrefixMap = { + {DHType::CAMERA, "Camera"}, + {DHType::DISPLAY, "Screen"}, + {DHType::INPUT, "Input"}, +}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index ce1c1fe1a85356d538efdd48cd6d6612ccf45d20..cf81e8c2b652519001d3ac620fb293b850290599 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -62,6 +62,7 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/task/task_factory.cpp", "src/utils/dh_context.cpp", "src/versionmanager/version_manager.cpp", + "src/task/monitor_task_timer.cpp", ] deps = [ diff --git a/services/distributedhardwarefwkserviceimpl/include/localhardwaremanager/local_hardware_manager.h b/services/distributedhardwarefwkserviceimpl/include/localhardwaremanager/local_hardware_manager.h index 1d75f08535abbf7ebecb9d373c1ebbfb55341330..26008e273bd4974da2e0e32c683c90b54702f2fb 100644 --- a/services/distributedhardwarefwkserviceimpl/include/localhardwaremanager/local_hardware_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/localhardwaremanager/local_hardware_manager.h @@ -20,6 +20,7 @@ #include #include +#include "capability_info.h" #include "device_type.h" #include "ihardware_handler.h" #include "single_instance.h" @@ -43,6 +44,8 @@ public: private: void QueryLocalHardware(const DHType dhType, IHardwareHandler *hardwareHandler); void AddLocalCapabilityInfo(const std::vector &dhItems, const DHType dhType); + void CheckNonExistCapabilityInfo(const std::vector &dhItems, const DHType dhType); + void GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap); private: std::map compToolFuncsMap_; diff --git a/services/distributedhardwarefwkserviceimpl/include/task/monitor_task_timer.h b/services/distributedhardwarefwkserviceimpl/include/task/monitor_task_timer.h new file mode 100644 index 0000000000000000000000000000000000000000..cc1ebdf3356ff1eade1659ffb4b74f1362addd0f --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/task/monitor_task_timer.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTED_HARDWARE_FWK_MONITOR_TASK_TIMER_H +#define DISTRIBUTED_HARDWARE_FWK_MONITOR_TASK_TIMER_H + +#include +#include +#include +#include + +#include "event_handler.h" + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class MonitorTaskTimer { +DECLARE_SINGLE_INSTANCE_BASE(MonitorTaskTimer); +public: + ~MonitorTaskTimer(); + + void StartTimer(); + + void StopTimer(); + +private: + MonitorTaskTimer(); + void Execute(const std::shared_ptr eventHandler); + +private: + std::thread monitorTaskTimerThread_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/task/task_board.h b/services/distributedhardwarefwkserviceimpl/include/task/task_board.h index 463955120c671563abfd57ee5b6911484c2a7d6f..dd194c9ff123e20575ccb18d8c05daf3c0200687 100644 --- a/services/distributedhardwarefwkserviceimpl/include/task/task_board.h +++ b/services/distributedhardwarefwkserviceimpl/include/task/task_board.h @@ -34,6 +34,9 @@ public: void AddTask(std::shared_ptr task); void RemoveTask(std::string taskId); int32_t WaitForALLTaskFinish(); + void AddEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam); + void RemoveEnabledDevice(const std::string &enabledDeviceKey); + const std::unordered_map& GetEnabledDevice(); void DumpAllTasks(std::vector &taskInfos); @@ -44,6 +47,10 @@ private: std::condition_variable conVar_; std::mutex tasksMtx_; std::unordered_map> tasks_; + + /* The key is combination of deviceId and dhId, and the value is taskParam */ + std::unordered_map enabledDevices_; + std::mutex enabledDevicesMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index fa9d0939882d5ef09b7a2fd15e3309a3ac43815d..7f79eb5c76c844ecefb74c81c571637ebe72e654 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -33,6 +33,7 @@ #include "enabled_comps_dump.h" #include "ipc_object_stub.h" #include "iservice_registry.h" +#include "monitor_task_timer.h" #include "system_ability_definition.h" #include "version_manager.h" @@ -83,7 +84,7 @@ int32_t ComponentManager::Init() HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, "dhfwk start sink failed."); } - + MonitorTaskTimer::GetInstance().StartTimer(); DHLOGI("Init component success"); DHTraceEnd(); return DH_FWK_SUCCESS; @@ -108,6 +109,7 @@ int32_t ComponentManager::UnInit() std::lock_guard lock(sinkVersionMutex_); sinkVersions_.clear(); } + MonitorTaskTimer::GetInstance().StopTimer(); DHLOGI("Release component success"); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index ffccdaa8228078a83725d13bd93d83f787ffe3ad..01cfddbc9b1bcd9c776fc134e3387e3a21dd92c5 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -127,6 +127,10 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; } + if (uuid.empty()) { + DHLOGI("uuid is empty"); + } + // when other device restart, the device receives online and offline messages in sequence // So, make the cache device handle offline event when other device restart std::string cacheUUID = DHContext::GetInstance().GetUUIDByNetworkId(networkId); diff --git a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp index 1eaec6e6b54726478b4dd0a170f2765f5bb06066..3de1205beaf53794973009aa3938760ac960d168 100644 --- a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp @@ -19,6 +19,7 @@ #include "capability_info_manager.h" #include "component_loader.h" +#include "constants.h" #include "device_type.h" #include "dh_context.h" #include "dh_utils_hitrace.h" @@ -85,6 +86,12 @@ void LocalHardwareManager::QueryLocalHardware(const DHType dhType, IHardwareHand usleep(QUERY_INTERVAL_TIME); } else { DHLOGI("Query hardwareHandler success, dhType: %#X!", dhType); + + /* + * Failed to delete data when the device restarts or other exception situation. + * So check and remove the non-exist local capabilityInfo. + */ + CheckNonExistCapabilityInfo(dhItems, dhType); AddLocalCapabilityInfo(dhItems, dhType); break; } @@ -106,5 +113,51 @@ void LocalHardwareManager::AddLocalCapabilityInfo(const std::vector &dhI } CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos); } + +void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector &dhItems, const DHType dhType) +{ + DHLOGI("start"); + if (dhType != DHType::INPUT) { + DHLOGI("This dhType is not input and no need remove!"); + return; + } + CapabilityInfoMap allLocalCapabilityInfos; + GetLocalCapabilityMapByPrefix(dhType, allLocalCapabilityInfos); + for (auto capabilityInfo : allLocalCapabilityInfos) { + std::shared_ptr capabilityValue = capabilityInfo.second; + if (capabilityValue == nullptr) { + DHLOGE("capabilityInfo value is nullptr, key: %s", capabilityValue->GetAnonymousKey().c_str()); + return; + } + DHLOGI("The key in allLocalCapabilityInfos is %s", capabilityValue->GetAnonymousKey().c_str()); + bool isExist = false; + for (auto dhItem : dhItems) { + DHLOGI("This data key is: %s, dhItem: %s", capabilityValue->GetAnonymousKey().c_str(), + GetAnonyString(dhItem.dhId).c_str()); + if (capabilityValue->GetDHId() == dhItem.dhId) { + DHLOGI("This data is exist, no need removed key: %s", capabilityValue->GetAnonymousKey().c_str()); + isExist = true; + break; + } + } + if (!isExist) { + DHLOGI("This data is non-exist, it should be removed, key: %s", capabilityValue->GetAnonymousKey().c_str()); + CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capabilityValue->GetKey()); + } + } + DHLOGI("end"); +} + +void LocalHardwareManager::GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap) +{ + std::string localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (DHTypePrefixMap.find(dhType) == DHTypePrefixMap.end()) { + DHLOGE("DHTypePrefixMap can not find dhType: %#X", dhType); + return; + } + std::string prefix = DHTypePrefixMap.find(dhType)->second; + std::string localCapabilityPrefix = localDeviceId + RESOURCE_SEPARATOR + prefix; + CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(localCapabilityPrefix, capabilityInfoMap); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp index 07e054739173af88fb187d3193add9e4b8d6fae1..4d89d59262046de00fd7bc36305576bbd9c0f69a 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp @@ -34,11 +34,13 @@ namespace DistributedHardware { #define DH_LOG_TAG "CapabilityInfoManager" CapabilityInfoManager::CapabilityInfoManager() : dbAdapterPtr_(nullptr) -{} +{ + DHLOGI("CapabilityInfoManager construction!"); +} CapabilityInfoManager::~CapabilityInfoManager() { - DHLOGI("CapabilityInfoManager Destruction!"); + DHLOGI("CapabilityInfoManager destruction!"); } std::shared_ptr CapabilityInfoManager::GetInstance() diff --git a/services/distributedhardwarefwkserviceimpl/src/task/disable_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/disable_task.cpp index f8a10a8cfdfbcfeb6cb6b1324c70f289368b5c91..3a4831ab0d879c0452e40a293d84f2aebc28954b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/disable_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/disable_task.cpp @@ -16,8 +16,10 @@ #include "disable_task.h" #include "anonymous_string.h" +#include "capability_utils.h" #include "component_manager.h" #include "dh_utils_hitrace.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "offline_task.h" @@ -65,6 +67,10 @@ void DisableTask::DoTaskInner() } DHLOGD("finish disable task, remove it, id = %s", GetId().c_str()); TaskBoard::GetInstance().RemoveTask(GetId()); + if (result == DH_FWK_SUCCESS) { + std::string enabledDeviceKey = CapabilityUtils::GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId()); + TaskBoard::GetInstance().RemoveEnabledDevice(enabledDeviceKey); + } } int32_t DisableTask::UnRegisterHardware() diff --git a/services/distributedhardwarefwkserviceimpl/src/task/enable_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/enable_task.cpp index 5f343cdf61bd74c6edf356827fbf4f515722a54c..4f3bd9e59604f00c9032cbbc651737a6bb05508b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/enable_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/enable_task.cpp @@ -16,8 +16,10 @@ #include "enable_task.h" #include "anonymous_string.h" +#include "capability_utils.h" #include "component_manager.h" #include "dh_utils_hitrace.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "task_board.h" @@ -55,6 +57,16 @@ void EnableTask::DoTaskInner() SetTaskState(state); DHLOGD("finish enable task, remove it, id = %s", GetId().c_str()); TaskBoard::GetInstance().RemoveTask(GetId()); + if (result == DH_FWK_SUCCESS) { + TaskParam taskParam = { + .networkId = GetNetworkId(), + .uuid = GetUUID(), + .dhId = GetDhId(), + .dhType = GetDhType() + }; + std::string enabledDeviceKey = CapabilityUtils::GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId()); + TaskBoard::GetInstance().AddEnabledDevice(enabledDeviceKey, taskParam); + } } int32_t EnableTask::RegisterHardware() diff --git a/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp b/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e2c59df1d1c30fbca83a2ea9f0326125e88f41b --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "monitor_task_timer.h" + +#include "anonymous_string.h" +#include "capability_info_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "task_board.h" +#include "task_executor.h" +#include "task_factory.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(MonitorTaskTimer); +namespace { + const std::string MONITOR_TASK_TIMER_HANDLER = "monitor_task_timer_handler"; + const std::string MONITOR_TASK_TIMER_ID = "monitor_task_timer_id"; + constexpr int32_t DELAY_TIME_MS = 5000; +} +#undef DH_LOG_TAG +#define DH_LOG_TAG "MonitorTaskTimer" + +MonitorTaskTimer::MonitorTaskTimer() +{ + DHLOGI("MonitorTaskTimer construction"); +} + +MonitorTaskTimer::~MonitorTaskTimer() +{ + DHLOGI("MonitorTaskTimer destruction"); + StopTimer(); +} + +void MonitorTaskTimer::StartTimer() +{ + DHLOGI("start"); + auto busRunner = OHOS::AppExecFwk::EventRunner::Create(MONITOR_TASK_TIMER_HANDLER); + auto eventHandler = std::make_shared(busRunner); + if (eventHandler == nullptr) { + DHLOGI("eventHandler construction, this point is empty"); + return; + } + monitorTaskTimerThread_ = std::thread(&MonitorTaskTimer::Execute, this, eventHandler); +} + +void MonitorTaskTimer::StopTimer() +{ + DHLOGI("start"); + if (monitorTaskTimerThread_.joinable()) { + monitorTaskTimerThread_.join(); + } +} + +void MonitorTaskTimer::Execute(const std::shared_ptr eventHandler) +{ + DHLOGI("start"); + if (eventHandler == nullptr) { + DHLOGE("eventHandler is nullptr!"); + return; + } + auto enabledDevices = TaskBoard::GetInstance().GetEnabledDevice(); + std::string capabilityKey; + std::shared_ptr capInfoPtr = nullptr; + TaskParam taskParam; + for (auto item : enabledDevices) { + capabilityKey = item.first; + taskParam = item.second; + if (taskParam.dhType != DHType::INPUT) { + continue; + } + if (CapabilityInfoManager::GetInstance()->GetDataByKey(capabilityKey, capInfoPtr) != DH_FWK_SUCCESS) { + DHLOGI("CapabilityInfoManager can not find this key in DB, key: %s, networkId: %s, uuid: %s, dhId: %s", + GetAnonyString(capabilityKey).c_str(), GetAnonyString(taskParam.networkId).c_str(), + GetAnonyString(taskParam.uuid).c_str(), GetAnonyString(taskParam.dhId).c_str()); + auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + } + auto monitorTaskTimer = [this, eventHandler] {Execute(eventHandler);}; + eventHandler->PostTask(monitorTaskTimer, MONITOR_TASK_TIMER_ID, DELAY_TIME_MS); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp b/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp index 59b635b77373d2d4a788028295ec819c29a45372..fed74d74bea12a68ae485cb4b582e43d5c5e13b6 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp @@ -102,5 +102,28 @@ void TaskBoard::DumpAllTasks(std::vector &taskInfos) taskInfos.emplace_back(taskInfo); } } + +void TaskBoard::AddEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam) +{ + std::lock_guard lock(enabledDevicesMutex_); + DHLOGI("AddEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str()); + enabledDevices_[enabledDeviceKey] = taskParam; +} + +void TaskBoard::RemoveEnabledDevice(const std::string &enabledDeviceKey) +{ + std::lock_guard lock(enabledDevicesMutex_); + DHLOGI("RemoveEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str()); + enabledDevices_.erase(enabledDeviceKey); +} + +const std::unordered_map& TaskBoard::GetEnabledDevice() +{ + std::lock_guard lock(enabledDevicesMutex_); + if (enabledDevices_.empty()) { + DHLOGI("enabledDevices is empty!"); + } + return enabledDevices_; +} } // namespace DistributedHardware } // namespace OHOS