mirror of
https://github.com/openharmony/distributed_hardware_fwk.git
synced 2026-07-01 20:24:00 -04:00
!132 【合入3.2Beta】设备重启后,拔掉热插拔设备,节点不消失,添加监听线程,定时清除已经使能但是数据库被删除的外设
Merge pull request !132 from tangfan/OpenHarmony-3.2-Beta2
This commit is contained in:
@@ -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<DHType, std::string> DHTypePrefixMap = {
|
||||
{DHType::CAMERA, "Camera"},
|
||||
{DHType::DISPLAY, "Screen"},
|
||||
{DHType::INPUT, "Input"},
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
|
||||
@@ -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",
|
||||
|
||||
+3
@@ -20,6 +20,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#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<DHItem> &dhItems, const DHType dhType);
|
||||
void CheckNonExistCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType);
|
||||
void GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap);
|
||||
|
||||
private:
|
||||
std::map<DHType, IHardwareHandler*> compToolFuncsMap_;
|
||||
|
||||
@@ -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 <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#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<OHOS::AppExecFwk::EventHandler> eventHandler);
|
||||
|
||||
private:
|
||||
std::thread monitorTaskTimerThread_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -34,6 +34,9 @@ public:
|
||||
void AddTask(std::shared_ptr<Task> 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<std::string, TaskParam>& GetEnabledDevice();
|
||||
|
||||
void DumpAllTasks(std::vector<TaskDump> &taskInfos);
|
||||
|
||||
@@ -44,6 +47,10 @@ private:
|
||||
std::condition_variable conVar_;
|
||||
std::mutex tasksMtx_;
|
||||
std::unordered_map<std::string, std::shared_ptr<Task>> tasks_;
|
||||
|
||||
/* The key is combination of deviceId and dhId, and the value is taskParam */
|
||||
std::unordered_map<std::string, TaskParam> enabledDevices_;
|
||||
std::mutex enabledDevicesMutex_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
+3
-1
@@ -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<std::mutex> lock(sinkVersionMutex_);
|
||||
sinkVersions_.clear();
|
||||
}
|
||||
MonitorTaskTimer::GetInstance().StopTimer();
|
||||
DHLOGI("Release component success");
|
||||
return DH_FWK_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
+53
@@ -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<DHItem> &dhI
|
||||
}
|
||||
CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos);
|
||||
}
|
||||
|
||||
void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector<DHItem> &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<CapabilityInfo> 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
|
||||
|
||||
+4
-2
@@ -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> CapabilityInfoManager::GetInstance()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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<OHOS::AppExecFwk::EventHandler>(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<OHOS::AppExecFwk::EventHandler> eventHandler)
|
||||
{
|
||||
DHLOGI("start");
|
||||
if (eventHandler == nullptr) {
|
||||
DHLOGE("eventHandler is nullptr!");
|
||||
return;
|
||||
}
|
||||
auto enabledDevices = TaskBoard::GetInstance().GetEnabledDevice();
|
||||
std::string capabilityKey;
|
||||
std::shared_ptr<CapabilityInfo> 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
|
||||
@@ -102,5 +102,28 @@ void TaskBoard::DumpAllTasks(std::vector<TaskDump> &taskInfos)
|
||||
taskInfos.emplace_back(taskInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void TaskBoard::SaveEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
|
||||
DHLOGI("SaveEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str());
|
||||
enabledDevices_[enabledDeviceKey] = taskParam;
|
||||
}
|
||||
|
||||
void TaskBoard::RemoveEnabledDevice(const std::string &enabledDeviceKey)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
|
||||
DHLOGI("RemoveEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str());
|
||||
enabledDevices_.erase(enabledDeviceKey);
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, TaskParam>& TaskBoard::GetEnabledDevice()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
|
||||
if (enabledDevices_.empty()) {
|
||||
DHLOGI("enabledDevices is empty!");
|
||||
}
|
||||
return enabledDevices_;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
Reference in New Issue
Block a user