diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a2894d5d0231f16abcbc92fae5aca1ff8000768b..50673ac4015d1530c78f6ef60beaaa23ea6d6204 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 92adc75b9de6e74d5ac2420dcdbdacf100428f02..9cf29f053a748959abfcf521abd2843b0d1f8368 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 ea3349e6c91061ec1828e132447fee9cfa8017ed..21aaa0a5b33348bfcd73b63b15dc110cb69496e2 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 55c0d60322e45409162eecd152e9fa122347e275..5fec9088e3055e37d0520214554c2b9510e68953 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 8ed117585e7871932eb5f5a7977f838bacaa03cf..a01bfc11fa41c11c0127ec910ce9e62346faf9aa 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 91c8df2c3ed613166f1e4b3069d510517ae372ec..47e16d2ceefff0dd6227a602ef3b049dc61e3675 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 0000000000000000000000000000000000000000..7eacf2263b3623ac0ed0d98d72e630f287e3a28d --- /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 b3b5a18b08397c92db13463225401e2de029c3de..148c1055cff620151bad5cde413ab0c2f2a467bd 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 496d89b95a5f56ef13f81b891fa1538e9d39b79f..b793062bf31676dc1a73bd5a441de97d466e2cef 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 aa264aa18924c405e83eda1c3794962019631b6e..dabb0420a69a4a11006b11f0ef0d2f7057a1fcf1 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 61761b36866e90e0513ffea90f2dc1635631074b..ee04e63571506373302b3465cbf679bd072a7feb 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 0000000000000000000000000000000000000000..0c7fdb280e8ea387c7b6a4c2711453164fe7c713 --- /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 36ebbfe240094d06a4c4d177a89c8e3517244d89..177fe56b0904b4ce63bc6c63a6ec9e837a2d2e1a 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;