mirror of
https://github.com/openharmony/distributed_hardware_fwk.git
synced 2026-06-30 20:27:58 -04:00
add MonitorTaskTimer and fix hotplugin bug when restart device
Signed-off-by: tangfan <tangfan5@huawei.com>
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_;
|
||||
|
||||
@@ -34,6 +34,9 @@ public:
|
||||
void AddTask(std::shared_ptr<Task> 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<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()) {
|
||||
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);
|
||||
|
||||
+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 remove!");
|
||||
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());
|
||||
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
|
||||
|
||||
+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().AddEnabledDevice(enabledDeviceKey, taskParam);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t EnableTask::RegisterHardware()
|
||||
|
||||
@@ -102,5 +102,28 @@ void TaskBoard::DumpAllTasks(std::vector<TaskDump> &taskInfos)
|
||||
taskInfos.emplace_back(taskInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void TaskBoard::AddEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
|
||||
DHLOGI("AddEnabledDevice 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