From e4a438b99e83c181a7f8dc70eb7f846fdb073cef Mon Sep 17 00:00:00 2001 From: saga Date: Tue, 3 Dec 2024 10:06:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=8F=AF=E4=BF=A1=E5=85=B3?= =?UTF-8?q?=E7=B3=BB=E5=8F=98=E6=9B=B4=E6=97=B6=E9=9C=80=E8=A6=81=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E8=A7=A6=E5=8F=91=E7=A1=AC=E4=BB=B6=E4=BD=BF=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: saga --- .../include/distributed_hardware_errno.h | 1 + .../include/idistributed_hardware_manager.h | 2 + .../distributedhardwarefwkservice/BUILD.gn | 1 + .../componentmanager/component_manager.h | 3 +- .../include/distributed_hardware_manager.h | 2 + .../distributed_hardware_manager_factory.h | 2 + .../include/task/device_changed_task.h | 36 +++++ .../include/utils/impl_utils.h | 6 +- .../src/accessmanager/access_manager.cpp | 24 +++- .../src/distributed_hardware_manager.cpp | 22 +++ .../distributed_hardware_manager_factory.cpp | 27 ++++ .../src/task/device_changed_task.cpp | 128 ++++++++++++++++++ .../src/task/task_factory.cpp | 6 + 13 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 services/distributedhardwarefwkservice/include/task/device_changed_task.h create mode 100644 services/distributedhardwarefwkservice/src/task/device_changed_task.cpp diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a2894d5d..50673ac4 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -72,6 +72,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE = -10501; constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED = -10502; + constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_NOT_ONLINE = -10503; /* ComponentLoader errno, range: [-10600, -10699] */ constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_IS_NULL = -10600; diff --git a/common/utils/include/idistributed_hardware_manager.h b/common/utils/include/idistributed_hardware_manager.h index 92adc75b..9cf29f05 100644 --- a/common/utils/include/idistributed_hardware_manager.h +++ b/common/utils/include/idistributed_hardware_manager.h @@ -32,6 +32,8 @@ public: const std::string &udid, uint16_t deviceType) = 0; virtual int32_t SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) = 0; + virtual int32_t SendDeviceChangedEvent(const std::string &networkId, const std::string &uuid, + const std::string &udid, uint16_t deviceType) = 0; virtual size_t GetOnLineCount() = 0; virtual int32_t GetComponentVersion(std::unordered_map &versionMap) = 0; virtual int32_t Dump(const std::vector &argsStr, std::string &result) = 0; diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index ea3349e6..21aaa0a5 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -94,6 +94,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/resourcemanager/meta_info_manager.cpp", "src/resourcemanager/version_info.cpp", "src/resourcemanager/version_info_manager.cpp", + "src/task/device_changed_task.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/meta_disable_task.cpp", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 55c0d603..5fec9088 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -77,7 +77,7 @@ public: * @return false if the task param not exist, return false. */ bool FetchNeedRefreshTask(const std::pair &taskKey, TaskParam &taskParam); - + bool IsIdenticalAccount(const std::string &networkId); class ComponentManagerEventHandler : public AppExecFwk::EventHandler { public: ComponentManagerEventHandler(const std::shared_ptr runner); @@ -114,7 +114,6 @@ private: void DoRecover(DHType dhType); void ReStartSA(DHType dhType); void RecoverDistributedHardware(DHType dhType); - bool IsIdenticalAccount(const std::string &networkId); int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m); void InitComponentHandler(); diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h b/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h index 8ed11758..a01bfc11 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h @@ -35,6 +35,8 @@ public: uint16_t deviceType) override; int32_t SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) override; + int32_t SendDeviceChangedEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, + uint16_t deviceType) override; size_t GetOnLineCount() override; int32_t GetComponentVersion(std::unordered_map &versionMap) override; diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h b/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h index 91c8df2c..47e16d2c 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_manager_factory.h @@ -35,6 +35,8 @@ public: uint16_t deviceType, int32_t osType); int32_t SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType); + int32_t SendDeviceChangedEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, + uint16_t deviceType); int32_t GetComponentVersion(std::unordered_map &versionMap); void ClearRemoteDeviceMetaInfoData(const std::string &peerudid, const std::string &peeruuid); void ClearRemoteDeviceLocalInfoData(const std::string &peeruuid); diff --git a/services/distributedhardwarefwkservice/include/task/device_changed_task.h b/services/distributedhardwarefwkservice/include/task/device_changed_task.h new file mode 100644 index 00000000..7eacf226 --- /dev/null +++ b/services/distributedhardwarefwkservice/include/task/device_changed_task.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 OHOS_DISTRIBUTED_HARDWARE_DEVICE_CHANGED_TASK_H +#define OHOS_DISTRIBUTED_HARDWARE_DEVICE_CHANGED_TASK_H + +#include "task.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceChangedTask : public Task { +public: + DeviceChangedTask() = delete; + DeviceChangedTask(const std::string &networkId, const std::string &uuid, const std::string &udid, + const std::string &dhId, const DHType dhType); + virtual ~DeviceChangedTask(); + virtual void DoTask(); + +private: + void HandleDeviceChanged(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/include/utils/impl_utils.h b/services/distributedhardwarefwkservice/include/utils/impl_utils.h index b3b5a18b..148c1055 100644 --- a/services/distributedhardwarefwkservice/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkservice/include/utils/impl_utils.h @@ -30,7 +30,8 @@ enum class TaskType : int32_t { ON_LINE = 3, OFF_LINE = 4, META_ENABLE = 5, - META_DISABLE = 6 + META_DISABLE = 6, + DEVICE_CHANGED = 7 }; enum class TaskStep : int32_t { @@ -44,7 +45,8 @@ enum class TaskStep : int32_t { META_ENABLE_TASK = 8, META_DISABLE_TASK = 9, DO_MODEM_META_ENABLE = 10, - DO_MODEM_META_DISABLE = 11 + DO_MODEM_META_DISABLE = 11, + DO_DEVICE_CHANGED = 12 }; enum class TaskState : int32_t { diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index 496d89b9..b793062b 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -175,8 +175,28 @@ void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo) void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo) { - (void)deviceInfo; - return; + std::lock_guard lock(accessMutex_); + DHLOGI("start, networkId = %{public}s, deviceName = %{public}s, authForm = %{public}d", + GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(), + deviceInfo.authForm); + + auto networkId = std::string(deviceInfo.networkId); + if (!IsIdLengthValid(networkId)) { + return; + } + auto uuid = GetUUIDByDm(networkId); + if (!IsIdLengthValid(uuid)) { + return; + } + auto udid = GetUDIDByDm(networkId); + if (!IsIdLengthValid(udid)) { + return; + } + auto ret = + DistributedHardwareManagerFactory::GetInstance().SendDeviceChangedEvent(networkId, uuid, udid, + deviceInfo.deviceTypeId); + DHLOGI("device changed result = %{public}d, networkId = %{public}s, uuid = %{public}s, udid: %{public}s", + ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str()); } void AccessManager::CheckTrustedDeviceOnline() diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp index aa264aa1..dabb0420 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp @@ -146,6 +146,28 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI return DH_FWK_SUCCESS; } +int32_t DistributedHardwareManager::SendDeviceChangedEvent(const std::string &networkId, const std::string &uuid, + const std::string &udid, uint16_t deviceType) +{ + if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } + (void)deviceType; + DHLOGI("networkId = %{public}s, uuid = %{public}s, udid = %{public}s", + GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str()); + + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .udid = udid, + .dhId = "", + .dhType = DHType::UNKNOWN + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::DEVICE_CHANGED, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + return DH_FWK_SUCCESS; +} + size_t DistributedHardwareManager::GetOnLineCount() { return DHContext::GetInstance().GetOnlineCount(); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index 61761b36..ee04e635 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -232,6 +232,33 @@ int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &n return DH_FWK_SUCCESS; } +int32_t DistributedHardwareManagerFactory::SendDeviceChangedEvent(const std::string &networkId, const std::string &uuid, + const std::string &udid, uint16_t deviceType) +{ + if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } + + if (!IsInit()) { + DHLOGI("distributedHardwareMgr is null"); + return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_NOT_ONLINE; + } + //if device not online, no need to handle device change event + if (!DHContext::GetInstance().IsDeviceOnline(uuid)) { + DHLOGI("Device not online, networkId: %{public}s, uuid: %{public}s", + GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str()); + return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_NOT_ONLINE; + } + + auto result = DistributedHardwareManager::GetInstance().SendDeviceChangedEvent(networkId, uuid, udid, deviceType); + if (result != DH_FWK_SUCCESS) { + DHLOGE("offline failed, errCode = %{public}d", result); + return result; + } + + return DH_FWK_SUCCESS; +} + int32_t DistributedHardwareManagerFactory::GetComponentVersion(std::unordered_map &versionMap) { DHLOGI("start"); diff --git a/services/distributedhardwarefwkservice/src/task/device_changed_task.cpp b/services/distributedhardwarefwkservice/src/task/device_changed_task.cpp new file mode 100644 index 00000000..0c7fdb28 --- /dev/null +++ b/services/distributedhardwarefwkservice/src/task/device_changed_task.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021-2024 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 "device_changed_task.h" + +#include "anonymous_string.h" +#include "capability_info_manager.h" +#include "component_manager.h" +#include "dh_utils_tool.h" +#include "capability_utils.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "local_capability_info_manager.h" +#include "meta_info_manager.h" +#include "task_board.h" +#include "task_executor.h" +#include "task_factory.h" + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "DeviceChangedTask" + +DeviceChangedTask::DeviceChangedTask(const std::string &networkId, const std::string &uuid, const std::string &udid, + const std::string &dhId, const DHType dhType) : Task(networkId, uuid, udid, dhId, dhType) +{ + SetTaskType(TaskType::DEVICE_CHANGED); + SetTaskSteps(std::vector { TaskStep::DO_DEVICE_CHANGED }); + DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(uuid).c_str()); +} + +DeviceChangedTask::~DeviceChangedTask() +{ + DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str()); +} + +void DeviceChangedTask::DoTask() +{ + DHLOGD("start device changed task, id = %{public}s, uuid = %{public}s", GetId().c_str(), + GetAnonyString(GetUUID()).c_str()); + this->SetTaskState(TaskState::RUNNING); + for (const auto& step : this->GetTaskSteps()) { + switch (step) { + case TaskStep::DO_DEVICE_CHANGED: { + HandleDeviceChanged(); + break; + } + default: { + break; + } + } + } + SetTaskState(TaskState::SUCCESS); + DHLOGD("finish device changed task, remove it, id = %{public}s.", GetId().c_str()); + TaskBoard::GetInstance().RemoveTask(this->GetId()); +} + + +void DeviceChangedTask::HandleDeviceChanged() +{ + DHLOGI("networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str()); + if (!ComponentManager::GetInstance().IsIdenticalAccount(GetNetworkId())) { + DHLOGI("not identical account"); + return; + } + std::string deviceId = GetDeviceIdByUUID(GetUUID()); + std::vector> devDhInfos; + std::vector> capabilityInfos; + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos); + std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr cap) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + }); + + if (devDhInfos.empty()) { + DHLOGW("Can not get cap info from CapabilityInfo, try use local Capability info"); + LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos); + std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr cap) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + }); + } + + if (devDhInfos.empty()) { + DHLOGW("Can not get cap info from local Capbility, try use meta info"); + std::string udidHash = Sha256(GetUDID()); + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr cap) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + }); + } + + if (devDhInfos.empty()) { + DHLOGE("Can not get cap info"); + return; + } + + auto enabledDevices = TaskBoard::GetInstance().GetEnabledDevice(); + for (const auto &info : devDhInfos) { + //skip enabled component + std::string deviceKey = GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), info.first); + if (enabledDevices.find(deviceKey) != enabledDevices.end()) { + continue; + } + TaskParam taskParam = { + .networkId = GetNetworkId(), + .uuid = GetUUID(), + .udid = GetUDID(), + .dhId = info.first, + .dhType = info.second + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, shared_from_this()); + TaskExecutor::GetInstance().PushTask(task); + } +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/task_factory.cpp b/services/distributedhardwarefwkservice/src/task/task_factory.cpp index 36ebbfe2..177fe56b 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -24,6 +24,7 @@ #include "offline_task.h" #include "online_task.h" #include "task_board.h" +#include "device_changed_task.h" namespace OHOS { namespace DistributedHardware { @@ -68,6 +69,11 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP taskParam.dhId, taskParam.dhType); break; } + case TaskType::DEVICE_CHANGED: { + task = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, + taskParam.dhId, taskParam.dhType); + break; + } default: { DHLOGE("CreateTask type invalid, type: %{public}d", taskType); return nullptr; -- Gitee