diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 3989f9329fed208e557b06f09e76d98378a3850b..4433a5e2dc78543f572ae9e55f51a87a3da574cd 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -73,6 +73,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 a3d7134e402eb35f1dd84c30b722f9dd3566c6e8..3a29621482010cbaed2a6e0aa5a34baa9d77fc95 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/offline_task.cpp", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index cdb9e2b901e03d130febd5391fd6a16a1271a2f5..db59cc43ac4aa6e13d92bc76d69e616eb25119c6 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -76,7 +76,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); @@ -113,7 +113,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); int32_t InitComponentHandler(); diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h b/services/distributedhardwarefwkservice/include/distributed_hardware_manager.h index 4751766bf4efe60e591b718cf23de9219a32ca06..30df3d6639d47ed5da1292263c463e374ba6828d 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 81b657b42843e8f5f80a19f102d924e66193694d..44ca49dbdb8e96ff95656aaee23406170345e934 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 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); int Dump(const std::vector &argsStr, std::string &result); 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..e1c2621674acd7affc6754b1ae6d66fb172d3d4f --- /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 \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/utils/impl_utils.h b/services/distributedhardwarefwkservice/include/utils/impl_utils.h index 67ab7dd90678733510da5904dc18929ff3732242..2c40c774b9596a14b5f56e59c0d88e21ed4c7342 100644 --- a/services/distributedhardwarefwkservice/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkservice/include/utils/impl_utils.h @@ -28,7 +28,8 @@ enum class TaskType : int32_t { ENABLE = 1, DISABLE = 2, ON_LINE = 3, - OFF_LINE = 4 + OFF_LINE = 4, + DEVICE_CHANGED = 5 }; enum class TaskStep : int32_t { @@ -38,7 +39,8 @@ enum class TaskStep : int32_t { REGISTER_ONLINE_DISTRIBUTED_HARDWARE = 4, UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE = 5, CLEAR_OFFLINE_INFO = 6, - WAIT_UNREGISTGER_COMPLETE = 7 + WAIT_UNREGISTGER_COMPLETE = 7, + DO_DEVICE_CHANGED = 8 }; enum class TaskState : int32_t { diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index d51862f5676e33b0d3ca9e08bb7dd4163ab8f596..521895f330a1e4e9a4706c5a7ef80588cbf84cef 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -172,8 +172,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 950ae64f91d10a3ff72ae10749ea862b10454285..27b144443fd0943ed22372b0e4f8ca2bfd2c397c 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp @@ -144,6 +144,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 9a706bcb6f4a184a48f5f31e359b89dd830bbf48..153d3fe058227652b3886e603d63e15c61536352 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -218,6 +218,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 7b931f35f72bb26e6cfc4735c57181d72c11338b..1c2a34490dd0bcbcfeccb73c892df22b6eeaa202 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -22,6 +22,7 @@ #include "offline_task.h" #include "online_task.h" #include "task_board.h" +#include "device_changed_task.h" namespace OHOS { namespace DistributedHardware { @@ -56,6 +57,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;