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/distributedhardwarefwkservice/include/accessmanager/access_manager.h b/services/distributedhardwarefwkservice/include/accessmanager/access_manager.h index 48f18c22b1f5d85e4c9a72808090c2f81c37bf42..e5d2b909edc03cce5e30e695455d0661c2e0804c 100644 --- a/services/distributedhardwarefwkservice/include/accessmanager/access_manager.h +++ b/services/distributedhardwarefwkservice/include/accessmanager/access_manager.h @@ -16,17 +16,15 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_ACCESS_MANAGER_H #define OHOS_DISTRIBUTED_HARDWARE_ACCESS_MANAGER_H +#include #include +#include #include -#include -#include -#include +#include -#include "device_manager.h" +#include "device_manager_callback.h" #include "dm_device_info.h" -#include "device_type.h" - namespace OHOS { namespace DistributedHardware { class AccessManager : public std::enable_shared_from_this, diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h b/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h index fe37f6a1d437b1deb88c0a4421b80c4914953c0a..1082b962e06ad14905625927e495084b73220a5d 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h @@ -16,8 +16,11 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_MANAGER_FACTORY_H #define OHOS_DISTRIBUTED_HARDWARE_MANAGER_FACTORY_H +#include #include +#include +#include "device_type.h" #include "idistributed_hardware_manager.h" #include "single_instance.h" diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index 672e9b383459da7d3757fc53b2896db6970265c5..e3cdca01be8427ee7d2987ae4c050cc3bf3b34cc 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -15,9 +15,12 @@ #include "access_manager.h" +#include #include #include +#include "device_manager.h" + #include "anonymous_string.h" #include "constants.h" #include "dh_utils_tool.h" diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index c3442a10bd9f6dcad665606e97f6d97e523b556b..c11e29b47b796c1d39fd044578666b1447299037 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -15,9 +15,14 @@ #include "distributed_hardware_manager_factory.h" +#include #include -#include +#include #include +#include + +#include "dm_device_info.h" +#include "hisysevent.h" #include "anonymous_string.h" #include "constants.h" @@ -27,6 +32,7 @@ #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "idistributed_hardware_manager.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index ce1c1fe1a85356d538efdd48cd6d6612ccf45d20..83328024ead464c63820022252b8344039436970 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -54,6 +54,7 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/db_adapter.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", + "src/task/monitor_task_timer.cpp", "src/task/offline_task.cpp", "src/task/online_task.cpp", "src/task/task.cpp", diff --git a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h index 44dbcbcace72d47ace92d7d25266211d9130ca45..d819636809ad1ef36398cca408ddf745c45f9ba9 100644 --- a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h +++ b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h @@ -15,7 +15,7 @@ #ifndef OHOS_DISTRIBUTED_ENABLED_COMPS_DUMP_H #define OHOS_DISTRIBUTED_ENABLED_COMPS_DUMP_H -#include + #include #include #include 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/resourcemanager/capability_utils.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_utils.h index f6e390853a5b8393b89406a727dff6b64aaaf963..0d8abd86a6cd82102263ba9f79561b90fdb5dc76 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_utils.h @@ -16,13 +16,15 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_UTILS_H #define OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_UTILS_H +#include +#include #include -#include #include "capability_info.h" namespace OHOS { namespace DistributedHardware { +class CapabilityInfo; enum class CapabilityInfoFilter : uint32_t { FILTER_DH_ID = 0, FILTER_DEVICE_ID = 1, 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..e9692c14da60cd1b5f11c2335a4b30bcea49dfec --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/task/monitor_task_timer.h @@ -0,0 +1,46 @@ +/* + * 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..39976467b8b88aeea8562d8316de36b7211aa449 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 SaveEnabledDevice(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..8b0270a3080cf14890ac15743143e19862ce057d 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()) { + DHLOGW("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/hidumphelper/enabled_comps_dump.cpp b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp index 840b8bcc7b68312f4dae2dd2039e64008ffac546..abfd99cb8f04efdeb1c6f224ebe12ce113438bcc 100644 --- a/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp @@ -14,7 +14,6 @@ */ #include "enabled_comps_dump.h" -#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp index 1eaec6e6b54726478b4dd0a170f2765f5bb06066..2239ba7e4c13008a34e41c482245aa9a6e38d01f 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 check!"); + 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()); + continue; + } + 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/localhardwaremanager/plugin_listener_impl.cpp b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/plugin_listener_impl.cpp index f130096f759bcd5bdb61049d18b6093d4deab608..b5a3c57a037c01f6b62973c0459fce289e3cee47 100644 --- a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/plugin_listener_impl.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/plugin_listener_impl.cpp @@ -15,9 +15,12 @@ #include "plugin_listener_impl.h" +#include "anonymous_string.h" +#include "capability_info.h" #include "capability_info_manager.h" #include "dh_context.h" #include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" namespace OHOS { namespace DistributedHardware { 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/resourcemanager/capability_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_utils.cpp index 450df0e91032f21ad058d89496cd192024285515..dce214f9dc87a6d881b378fedb6fd118f3a5da2e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_utils.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_utils.cpp @@ -15,8 +15,6 @@ #include "capability_utils.h" -#include "nlohmann/json.hpp" - #include "capability_info.h" #include "constants.h" #include "distributed_hardware_log.h" 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..87f3f824573503df34deea8864f33d7f1defc1fc 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().SaveEnabledDevice(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..b27f0c185d4108e85fc0f33ff5eed02e1adeca69 --- /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"); +} + +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(); + } + DHLOGI("end"); +} + +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..f11e9698ae7443a29e899b4ef72bbde5e4724ea9 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::SaveEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam) +{ + std::lock_guard lock(enabledDevicesMutex_); + DHLOGI("SaveEnabledDevice 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 diff --git a/utils/include/dh_utils_hisysevent.h b/utils/include/dh_utils_hisysevent.h index fcdfdf6b196c3ebf96dcd2f0c6b4d86b5c42fe4f..b8a0939e9506cebc7606bc36e7bffb13126ac15c 100644 --- a/utils/include/dh_utils_hisysevent.h +++ b/utils/include/dh_utils_hisysevent.h @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_DHUTIL_HISYSEVENT_H #define OHOS_DISTRIBUTED_HARDWARE_DHUTIL_HISYSEVENT_H +#include #include #include "hisysevent.h" diff --git a/utils/include/dh_utils_hitrace.h b/utils/include/dh_utils_hitrace.h index 502215443013275dc73642f6abf56395aec6ce73..1deb311f498d2c4d265abcf150be9841d54b088a 100644 --- a/utils/include/dh_utils_hitrace.h +++ b/utils/include/dh_utils_hitrace.h @@ -16,6 +16,7 @@ #ifndef OHOS_DH_HITRACE_H #define OHOS_DH_HITRACE_H +#include #include #include "device_type.h" diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 77f60983d095cb79109bc02d6122edb31496c195..38a2bf27ff23564416f2944efcc622297df95866 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -17,7 +17,6 @@ #define OHOS_DISTRIBUTED_HARDWARE_DHUTILS_TOOL_H #include -#include #include "device_type.h" diff --git a/utils/src/anonymous_string.cpp b/utils/src/anonymous_string.cpp index 291fd9e9986a1b30dbc890d844ae6eae03b2d73f..be63436b0a5e215ef1bafa0961cdc43d77761258 100644 --- a/utils/src/anonymous_string.cpp +++ b/utils/src/anonymous_string.cpp @@ -15,6 +15,9 @@ #include "anonymous_string.h" +#include +#include + #include "securec.h" namespace OHOS { diff --git a/utils/src/dh_utils_hisysevent.cpp b/utils/src/dh_utils_hisysevent.cpp index aac98804e796d1d939086c8d96f935273ad6e043..499e00a160ab7184c13067b06ca44a1690a340f3 100644 --- a/utils/src/dh_utils_hisysevent.cpp +++ b/utils/src/dh_utils_hisysevent.cpp @@ -14,10 +14,8 @@ */ #include "dh_utils_hisysevent.h" -#include #include -#include "anonymous_string.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" diff --git a/utils/src/dh_utils_hitrace.cpp b/utils/src/dh_utils_hitrace.cpp index 0c0d8cbf49b7917dce6d7f90b33e18289c1665be..3bc80bb3abf387282008399b4a98757e3f873318 100644 --- a/utils/src/dh_utils_hitrace.cpp +++ b/utils/src/dh_utils_hitrace.cpp @@ -14,7 +14,6 @@ */ #include "dh_utils_hitrace.h" -#include #include namespace OHOS { diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index dfc12b8bb7e066a799f8e05981a89aa76ca3e822..a2c78fac053b4d00232d3346f5b1fd7e4c263e4f 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -15,12 +15,16 @@ #include "dh_utils_tool.h" +#include #include +#include +#include #include #include #include #include "openssl/sha.h" +#include "softbus_common.h" #include "softbus_bus_center.h" #include "constants.h" diff --git a/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp b/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp index f76469e508b5b15525a714329150fcde1535575e..e1b65aa13b7a61c02ca47312baa1b526ced6d8ea 100644 --- a/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp +++ b/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "anonymous_string.h" #include "dh_utils_tool.h" diff --git a/utils/test/unittest/common/eventbus/event_bus_test.cpp b/utils/test/unittest/common/eventbus/event_bus_test.cpp index d0015d046587b79878a0d7e4e3279bbca48e6b52..8ca543ca2708480dfaffcdf0b594cf29faf504e3 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.cpp +++ b/utils/test/unittest/common/eventbus/event_bus_test.cpp @@ -13,10 +13,16 @@ * limitations under the License. */ -#include "distributed_hardware_log.h" - #include "event_bus_test.h" +#include +#include +#include + +#include "distributed_hardware_log.h" +#include "event_bus.h" +#include "event_registration.h" + using namespace testing::ext; namespace OHOS { diff --git a/utils/test/unittest/common/eventbus/event_bus_test.h b/utils/test/unittest/common/eventbus/event_bus_test.h index f3435a529c4da32332e1961b2facee90a7b4d91f..3d953e75d55a6f156a8b29e3561b841dbf46391f 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.h +++ b/utils/test/unittest/common/eventbus/event_bus_test.h @@ -16,15 +16,11 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H -#include +#include +#include #include -#include - -#include "event_handler.h" - #include "event.h" -#include "event_bus.h" #include "event_sender.h" #include "eventbus_handler.h" diff --git a/utils/test/unittest/common/utilstool/utils_tool_test.cpp b/utils/test/unittest/common/utilstool/utils_tool_test.cpp index d83b3356d2b15fe2ee75e3ef5c3792dce1aa0361..b8981972b12e9b0bb773b8b36cd2d6321d97730a 100644 --- a/utils/test/unittest/common/utilstool/utils_tool_test.cpp +++ b/utils/test/unittest/common/utilstool/utils_tool_test.cpp @@ -16,7 +16,10 @@ #include "utils_tool_test.h" #include +#include +#include #include +#include #include #include "anonymous_string.h"