From 17b0271964b107649252d59d84dada60297aaa3f Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Mon, 27 Jan 2025 15:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=A1=AC=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=99=9A=E6=8B=9F=E7=A1=AC=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8C=89=E9=9C=80=E5=90=AF=E5=8A=A8=EF=BC=8C=E5=88=86?= =?UTF-8?q?=E5=B8=83=E5=BC=8F=E7=A1=AC=E4=BB=B6=E6=94=AF=E6=8C=81=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E4=B8=BB=E5=8A=A8=E4=BD=BF=E8=83=BD/=E5=8E=BB?= =?UTF-8?q?=E4=BD=BF=E8=83=BD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 --- common/utils/include/constants.h | 5 +- common/utils/include/dhardware_descriptor.h | 33 + .../include/dhardware_ipc_interface_code.h | 11 +- .../include/distributed_hardware_errno.h | 10 +- common/utils/include/idistributed_hardware.h | 14 +- common/utils/include/ihardware_handler.h | 2 +- .../utils/include/ihardware_status_listener.h | 55 + interfaces/inner_kits/BUILD.gn | 1 + .../include/distributed_hardware_fwk_kit.h | 81 +- .../include/ipc/distributed_hardware_proxy.h | 15 +- .../ipc/hardware_status_listener_stub.h | 46 + .../src/distributed_hardware_fwk_kit.cpp | 131 +- .../src/ipc/distributed_hardware_proxy.cpp | 328 +++++ .../src/ipc/hardware_status_listener_stub.cpp | 92 ++ .../include/dhfwk_sa_manager_test.h | 61 +- .../include/distributed_hardware_proxy_test.h | 9 + .../src/distributed_hardware_proxy_test.cpp | 68 + .../distributedhardwarefwkservice/BUILD.gn | 1 + .../componentloader/component_loader.h | 44 +- .../componentmanager/component_manager.h | 146 +- .../include/distributed_hardware_service.h | 11 +- .../include/distributed_hardware_stub.h | 15 +- .../ipc/hardware_status_listener_proxy.h | 50 + .../include/resourcemanager/version_info.h | 3 +- .../include/task/disable_task.h | 23 +- .../include/task/enable_task.h | 23 +- .../include/task/task_factory.h | 5 +- .../include/utils/impl_utils.h | 15 +- .../src/componentloader/component_loader.cpp | 222 +++- .../componentmanager/component_manager.cpp | 1183 ++++++++++++----- .../src/distributed_hardware_service.cpp | 180 ++- .../src/distributed_hardware_stub.cpp | 247 +++- .../ipc/hardware_status_listener_proxy.cpp | 181 +++ .../src/resourcemanager/version_info.cpp | 63 +- .../src/task/disable_task.cpp | 115 +- .../src/task/enable_task.cpp | 115 +- .../src/task/online_task.cpp | 11 +- .../src/task/task_factory.cpp | 34 +- .../src/component_manager_test.cpp | 87 +- .../include/distributed_hardware_stub_test.h | 61 +- 40 files changed, 3280 insertions(+), 517 deletions(-) create mode 100644 common/utils/include/dhardware_descriptor.h create mode 100644 common/utils/include/ihardware_status_listener.h create mode 100644 interfaces/inner_kits/include/ipc/hardware_status_listener_stub.h create mode 100644 interfaces/inner_kits/src/ipc/hardware_status_listener_stub.cpp create mode 100644 services/distributedhardwarefwkservice/include/ipc/hardware_status_listener_proxy.h create mode 100644 services/distributedhardwarefwkservice/src/ipc/hardware_status_listener_proxy.cpp diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index c3719d62..2da0b285 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -40,6 +40,7 @@ namespace DistributedHardware { constexpr uint32_t MAX_JSON_SIZE = 40 * 1024 * 1024; constexpr uint32_t MAX_HASH_SIZE = 64; constexpr uint32_t MAX_KEY_SIZE = 256; + constexpr uint32_t MAX_DH_DESCRIPTOR_ARRAY_SIZE = 4096; const std::string LOW_LATENCY_KEY = "identity"; const std::u16string DHMS_STUB_INTERFACE_TOKEN = u"ohos.distributedhardware.accessToken"; @@ -65,6 +66,8 @@ namespace DistributedHardware { const std::string HANDLER = "handler"; const std::string SOURCE_VER = "source_ver"; const std::string SINK_VER = "sink_ver"; + const std::string SOURCE_FEATURE_FILTER = "source_feature_filter"; + const std::string SINK_SUPPORTED_FEATURE = "sink_supported_feature"; const std::string DH_TASK_NAME_PREFIX = "Task_"; const std::string DH_FWK_PKG_NAME = "ohos.dhardware"; const std::string DH_FWK_SESSION_NAME = "ohos.dhardware.session_"; diff --git a/common/utils/include/dhardware_descriptor.h b/common/utils/include/dhardware_descriptor.h new file mode 100644 index 00000000..8ead0889 --- /dev/null +++ b/common/utils/include/dhardware_descriptor.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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_DESCRIPTOR_H +#define OHOS_DISTRIBUTED_HARDWARE_DESCRIPTOR_H + +#include +#include + +#include "device_type.h" + +namespace OHOS { +namespace DistributedHardware { + +struct DHDescriptor { + std::string id; + DHType dhType; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_DESCRIPTOR_H diff --git a/common/utils/include/dhardware_ipc_interface_code.h b/common/utils/include/dhardware_ipc_interface_code.h index 173e96bf..902c02a0 100644 --- a/common/utils/include/dhardware_ipc_interface_code.h +++ b/common/utils/include/dhardware_ipc_interface_code.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -34,6 +34,15 @@ enum class DHMsgInterfaceCode : uint32_t { PAUSE_DISTRIBUTED_HARDWARE = 480010, RESUME_DISTRIBUTED_HARDWARE = 480011, STOP_DISTRIBUTED_HARDWARE = 480012, + GET_DISTRIBUTED_HARDWARE = 480013, + REG_DH_SINK_STATUS_LISTNER = 480014, + UNREG_DH_SINK_STATUS_LISTNER = 480015, + REG_DH_SOURCE_STATUS_LISTNER = 480016, + UNREG_DH_SOURCE_STATUS_LISTNER = 480017, + ENABLE_SINK = 480018, + DISABLE_SINK = 480019, + ENABLE_SOURCE = 480020, + DISABLE_SOURCE = 480021, NOTIFY_SOURCE_DEVICE_REMOTE_DMSDP_STARTED = 1, }; } // namespace DistributedHardware diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a2894d5d..09068db9 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -49,6 +49,8 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_COMPONENT_MONITOR_NULL = -10011; constexpr int32_t ERR_DH_FWK_COMPONENT_TRANSPORT_OPT_FAILED = -10012; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_ENABLE_PARAM_FAILED = -10013; + constexpr int32_t ERR_DH_FWK_COMPONENT_LIMIT_DEMAND_START = -10014; + constexpr int32_t ERR_DH_FWK_COMPONENT_REPEAT_CALL = -10015; /* ResourceManager errno, range: [-10400, -10499] */ constexpr int32_t ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL = -10400; @@ -79,6 +81,12 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL = -10602; constexpr int32_t ERR_DH_FWK_LOADER_DLCLOSE_FAIL = -10603; constexpr int32_t ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL = -10604; + constexpr int32_t ERR_DH_FWK_LOADER_SINK_LOAD = -10605; + constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_LOAD = -10606; + constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_LOAD = -10607; + constexpr int32_t ERR_DH_FWK_LOADER_SINK_UNLOAD = -10608; + constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_UNLOAD = -10609; + constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_UNLOAD = -10610; /* Task errno, range: [-10700, -10799] */ constexpr int32_t ERR_DH_FWK_TASK_TIMEOUT = -10700; diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index f7390e53..f219c268 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -22,6 +22,8 @@ #include "iremote_broker.h" #include "device_type.h" +#include "dhardware_descriptor.h" +#include "ihardware_status_listener.h" #include "distributed_hardware_fwk_kit_paras.h" #include "iav_trans_control_center_callback.h" @@ -49,6 +51,16 @@ public: virtual int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) = 0; virtual int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) = 0; virtual int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) = 0; + virtual int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) = 0; + virtual int32_t RegisterDHStatusListener(sptr listener) = 0; + virtual int32_t UnregisterDHStatusListener(sptr listener) = 0; + virtual int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) = 0; + virtual int32_t UnregisterDHStatusListener(const std::string &networkId, + sptr listener) = 0; + virtual int32_t EnableSink(const std::vector &descriptors) = 0; + virtual int32_t DisableSink(const std::vector &descriptors) = 0; + virtual int32_t EnableSource(const std::string &networkId, const std::vector &descriptors) = 0; + virtual int32_t DisableSource(const std::string &networkId, const std::vector &descriptors) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/utils/include/ihardware_handler.h b/common/utils/include/ihardware_handler.h index 10e5cd2a..a80cd368 100644 --- a/common/utils/include/ihardware_handler.h +++ b/common/utils/include/ihardware_handler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 diff --git a/common/utils/include/ihardware_status_listener.h b/common/utils/include/ihardware_status_listener.h new file mode 100644 index 00000000..17e355eb --- /dev/null +++ b/common/utils/include/ihardware_status_listener.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 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_IHARDWARE_STATUS_LISTENER_H +#define OHOS_IHARDWARE_STATUS_LISTENER_H + +#include +#include + +#include + +#include "dhardware_descriptor.h" + +namespace OHOS { +namespace DistributedHardware { +class IHDSinkStatusListener : public IRemoteBroker { +public: + virtual void OnEnable(const DHDescriptor &dhDescriptor) = 0; + virtual void OnDisable(const DHDescriptor &dhDescriptor) = 0; + + enum class Message : uint32_t { + ON_ENABLE, + ON_DISABLE + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedHardwareFwk.IHDSinkStatusListener"); +}; + +class IHDSourceStatusListener : public IRemoteBroker { +public: + virtual void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) = 0; + virtual void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) = 0; + + enum class Message : uint32_t { + ON_ENABLE, + ON_DISABLE + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedHardwareFwk.IHDSourceStatusListener"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_IHARDWARE_STATUS_LISTENER_H \ No newline at end of file diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index cf6108c5..a322c46b 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -53,6 +53,7 @@ ohos_shared_library("libdhfwk_sdk") { "${innerkits_path}/src/distributed_hardware_fwk_kit.cpp", "${innerkits_path}/src/ipc/dhfwk_sa_manager.cpp", "${innerkits_path}/src/ipc/distributed_hardware_proxy.cpp", + "${innerkits_path}/src/ipc/hardware_status_listener_stub.cpp", "${innerkits_path}/src/ipc/publisher_listener_stub.cpp", ] diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h index c66ef9de..a531c680 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -24,8 +24,10 @@ #include "refbase.h" #include "distributed_hardware_fwk_kit_paras.h" +#include "dhardware_descriptor.h" #include "ipublisher_listener.h" #include "idistributed_hardware.h" +#include "ihardware_status_listener.h" #ifndef API_EXPORT #define API_EXPORT __attribute__((visibility("default"))) @@ -157,6 +159,83 @@ public: */ API_EXPORT int32_t StopDistributedHardware(DHType dhType, const std::string &networkId); + /** + * @brief Get distributed hardware. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors); + + /** + * @brief Register distributed hardware status listener. + * + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t RegisterDHStatusListener(sptr listener); + + /** + * @brief Unregister distributed hardware status listener. + * + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t UnregisterDHStatusListener(sptr listener); + + /** + * @brief Register distributed hardware status listener. + * + * @param networkId distributed hardware networkId. + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener); + + /** + * @brief Unregister distributed hardware status listener. + * + * @param networkId distributed hardware networkId. + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener); + + /** + * @brief Enable distributed hardware sink. + * + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t EnableSink(const std::vector &descriptors); + + /** + * @brief Disable distributed hardware sink. + * + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t DisableSink(const std::vector &descriptors); + + /** + * @brief Enable distributed hardware source. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t EnableSource(const std::string &networkId, const std::vector &descriptors); + + /** + * @brief Disable distributed hardware source. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t DisableSource(const std::string &networkId, const std::vector &descriptors); + private: /** * @brief Determine whether the topic is valid. diff --git a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index c0533a7d..5f57ee29 100644 --- a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -47,6 +47,19 @@ public: int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) override; int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) override; int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) override; + int32_t RegisterDHStatusListener(sptr listener) override; + int32_t UnregisterDHStatusListener(sptr listener) override; + int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) override; + int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener) override; + int32_t EnableSink(const std::vector &descriptors) override; + int32_t DisableSink(const std::vector &descriptors) override; + int32_t EnableSource(const std::string &networkId, const std::vector &descriptors) override; + int32_t DisableSource(const std::string &networkId, const std::vector &descriptors) override; + +private: + int32_t ReadDescriptors(MessageParcel &data, std::vector &descriptors); + int32_t WriteDescriptors(MessageParcel &data, const std::vector &descriptors); private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_kits/include/ipc/hardware_status_listener_stub.h b/interfaces/inner_kits/include/ipc/hardware_status_listener_stub.h new file mode 100644 index 00000000..c3426d31 --- /dev/null +++ b/interfaces/inner_kits/include/ipc/hardware_status_listener_stub.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 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_HARDWARE_STATUS_LISTENER_STUB_H +#define OHOS_HARDWARE_STATUS_LISTENER_STUB_H + +#include "ihardware_status_listener.h" + +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class HDSinkStatusListenerStub : public IRemoteStub { +public: + HDSinkStatusListenerStub(); + virtual ~HDSinkStatusListenerStub() override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + DISALLOW_COPY_AND_MOVE(HDSinkStatusListenerStub); +}; + +class HDSourceStatusListenerStub : public IRemoteStub { +public: + HDSourceStatusListenerStub(); + virtual ~HDSourceStatusListenerStub() override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + DISALLOW_COPY_AND_MOVE(HDSourceStatusListenerStub); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_HARDWARE_STATUS_LISTENER_STUB_H \ No newline at end of file diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index a37929a9..2ed3502f 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -28,6 +28,23 @@ namespace OHOS { namespace DistributedHardware { + +std::string DumpDescriptors(const std::vector &descriptors) +{ + std::string descriptorsInfo = "["; + for (auto& descriptor : descriptors) { + descriptorsInfo += "["; + descriptorsInfo += "dhType:"; + descriptorsInfo += std::to_string((uint32_t)descriptor.dhType); + descriptorsInfo += ","; + descriptorsInfo += "id:"; + descriptorsInfo += descriptor.id; + descriptorsInfo += "]"; + } + descriptorsInfo += "]"; + return descriptorsInfo; +} + DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false) { DHLOGI("Ctor DistributedHardwareFwkKit"); @@ -251,5 +268,117 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const "user stop sink ui."); return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); } + +int32_t DistributedHardwareFwkKit::GetDistributedHardware( + const std::string &networkId, std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Get distributed hardware networkId %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, descriptors); +} + +int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(sptr listener) +{ + DHLOGI("Register distributed hardware status sink listener."); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(listener); +} + +int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(sptr listener) +{ + DHLOGI("Unregister distributed hardware status sink listener."); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(listener); +} + +int32_t DistributedHardwareFwkKit::RegisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Register distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(networkId, listener); +} + +int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Unregister distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(networkId, listener); +} + +int32_t DistributedHardwareFwkKit::EnableSink(const std::vector &descriptors) +{ + DHLOGI("Enable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSink(descriptors); +} + +int32_t DistributedHardwareFwkKit::DisableSink(const std::vector &descriptors) +{ + DHLOGI("Disable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSink(descriptors); +} + +int32_t DistributedHardwareFwkKit::EnableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Enable distributed hardware source networkId %{public}s, descriptors %{public}s.", + GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSource(networkId, descriptors); +} + +int32_t DistributedHardwareFwkKit::DisableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Disable distributed hardware source networkId %{public}s, descriptors %{public}s.", + GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSource(networkId, descriptors); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index 8c49e1bb..4f89b6b5 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -530,5 +530,333 @@ int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const s return reply.ReadInt32(); } + +int32_t DistributedHardwareProxy::GetDistributedHardware( + const std::string &networkId, std::vector &descriptors) +{ + DHLOGI("DistributedHardwareProxy GetDistributedHardware."); + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::GET_DISTRIBUTED_HARDWARE), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + descriptors.clear(); + ReadDescriptors(reply, descriptors); + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::RegisterDHStatusListener(sptr listener) +{ + DHLOGI("DistributedHardwareProxy RegisterDHStatusListener."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("Write listener failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::REG_DH_SINK_STATUS_LISTNER), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::UnregisterDHStatusListener(sptr listener) +{ + DHLOGI("DistributedHardwareProxy UnregisterDHStatusListener."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("Write listener failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::UNREG_DH_SINK_STATUS_LISTNER), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::RegisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + DHLOGI("DistributedHardwareProxy RegisterDHStatusListener."); + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("Write listener failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::REG_DH_SOURCE_STATUS_LISTNER), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::UnregisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + DHLOGI("DistributedHardwareProxy UnregisterDHStatusListener."); + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("Write listener failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::UNREG_DH_SOURCE_STATUS_LISTNER), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::EnableSink(const std::vector &descriptors) +{ + DHLOGI("DistributedHardwareProxy EnableSink."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::ENABLE_SINK), data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::DisableSink(const std::vector &descriptors) +{ + DHLOGI("DistributedHardwareProxy DisableSink."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::DISABLE_SINK), data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::EnableSource( + const std::string &networkId, const std::vector &descriptors) +{ + DHLOGI("DistributedHardwareProxy EnableSource."); + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::ENABLE_SOURCE), data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::DisableSource( + const std::string &networkId, const std::vector &descriptors) +{ + DHLOGI("DistributedHardwareProxy DisableSource."); + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null!"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::DISABLE_SOURCE), data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %{public}d!", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::ReadDescriptors(MessageParcel &data, std::vector &descriptors) +{ + int32_t size = data.ReadInt32(); + if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { + DHLOGE("The array descriptors are too large, size: %{public}d!", size); + return ERR_DH_FWK_PARA_INVALID; + } + for (int32_t i = 0; i < size; ++i) { + DHDescriptor descriptor; + descriptor.dhType = static_cast(data.ReadInt32()); + descriptor.id = data.ReadString(); + descriptors.push_back(descriptor); + } + return NO_ERROR; +} + +int32_t DistributedHardwareProxy::WriteDescriptors(MessageParcel &data, const std::vector &descriptors) +{ + int32_t size = (int32_t)descriptors.size(); + if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { + DHLOGE("The array descriptors are too large, size: %{public}d!", size); + return ERR_DH_FWK_PARA_INVALID; + } + if (!data.WriteInt32(size)) { + DHLOGE("Write descriptors size failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + for (int32_t i = 0; i < size; ++i) { + const DHDescriptor &descriptor = descriptors.at(i); + int32_t type = static_cast(descriptor.dhType); + if (!data.WriteInt32(type)) { + DHLOGE("Write descriptor.dhType failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(descriptor.id)) { + DHLOGE("Write descriptor.id failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + } + return NO_ERROR; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/hardware_status_listener_stub.cpp b/interfaces/inner_kits/src/ipc/hardware_status_listener_stub.cpp new file mode 100644 index 00000000..ed8fb2e1 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/hardware_status_listener_stub.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2025 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 "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "hardware_status_listener_stub.h" + +namespace OHOS { +namespace DistributedHardware { +HDSinkStatusListenerStub::HDSinkStatusListenerStub() +{ +} + +HDSinkStatusListenerStub::~HDSinkStatusListenerStub() +{ +} + +int32_t HDSinkStatusListenerStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("HDSinkStatusListenerStub read valid token failed!"); + return ERR_INVALID_DATA; + } + DHDescriptor dhDescriptor; + dhDescriptor.dhType = static_cast(data.ReadUint32()); + dhDescriptor.id = data.ReadString(); + IHDSinkStatusListener::Message msgCode = static_cast(code); + switch (msgCode) { + case IHDSinkStatusListener::Message::ON_ENABLE: { + OnEnable(dhDescriptor); + break; + } + case IHDSinkStatusListener::Message::ON_DISABLE: { + OnDisable(dhDescriptor); + break; + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return DH_FWK_SUCCESS; +} + +HDSourceStatusListenerStub::HDSourceStatusListenerStub() +{ +} + +HDSourceStatusListenerStub::~HDSourceStatusListenerStub() +{ +} + +int32_t HDSourceStatusListenerStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("PublisherListenerStub read valid token failed!"); + return ERR_INVALID_DATA; + } + std::string networkId = data.ReadString(); + DHDescriptor dhDescriptor; + dhDescriptor.dhType = static_cast(data.ReadUint32()); + dhDescriptor.id = data.ReadString(); + IHDSinkStatusListener::Message msgCode = static_cast(code); + switch (msgCode) { + case IHDSinkStatusListener::Message::ON_ENABLE: { + OnEnable(networkId, dhDescriptor); + break; + } + case IHDSinkStatusListener::Message::ON_DISABLE: { + OnDisable(networkId, dhDescriptor); + break; + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/test/unittest/common/ipc/dhfwk_sa_manager/include/dhfwk_sa_manager_test.h b/interfaces/inner_kits/test/unittest/common/ipc/dhfwk_sa_manager/include/dhfwk_sa_manager_test.h index 15182bd6..e814b7d5 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/dhfwk_sa_manager/include/dhfwk_sa_manager_test.h +++ b/interfaces/inner_kits/test/unittest/common/ipc/dhfwk_sa_manager/include/dhfwk_sa_manager_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -130,6 +130,65 @@ int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) (void)networkId; return DH_FWK_SUCCESS; } + +int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t RegisterDHStatusListener(sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t UnregisterDHStatusListener(sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t EnableSink(const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DisableSink(const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t EnableSource(const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DisableSource(const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h index 3be4ea15..7108d198 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h +++ b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/include/distributed_hardware_proxy_test.h @@ -54,6 +54,15 @@ public: int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId); int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId); int32_t StopDistributedHardware(DHType dhType, const std::string &networkId); + int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors); + int32_t RegisterDHStatusListener(sptr listener); + int32_t UnregisterDHStatusListener(sptr listener); + int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener); + int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener); + int32_t EnableSink(const std::vector &descriptors); + int32_t DisableSink(const std::vector &descriptors); + int32_t EnableSource(const std::string &networkId, const std::vector &descriptors); + int32_t DisableSource(const std::string &networkId, const std::vector &descriptors); }; }; diff --git a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp index 04d47c92..4a56ec13 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp +++ b/interfaces/inner_kits/test/unittest/common/ipc/distributed_hardware_proxy/src/distributed_hardware_proxy_test.cpp @@ -129,6 +129,74 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::StopDistribut return DH_FWK_SUCCESS; } +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::GetDistributedHardware( + const std::string &networkId, std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::RegisterDHStatusListener( + sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::UnregisterDHStatusListener( + sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::RegisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::UnregisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::EnableSink( + const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::DisableSink( + const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::EnableSource( + const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::DisableSource( + const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + /** * @tc.name: RegisterPublisherListener_001 * @tc.desc: Verify the RegisterPublisherListener function diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index b3b10248..0f8256f9 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -77,6 +77,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/distributed_hardware_stub.cpp", "src/hidumphelper/enabled_comps_dump.cpp", "src/hidumphelper/hidump_helper.cpp", + "src/ipc/hardware_status_listener_proxy.cpp", "src/ipc/publisher_listener_proxy.cpp", "src/localhardwaremanager/local_hardware_manager.cpp", "src/localhardwaremanager/plugin_listener_impl.cpp", diff --git a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h index bc5315e7..ff3efe86 100644 --- a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -38,17 +38,6 @@ struct ResourceDesc { }; namespace { -struct CompHandler { - DHType type; - void *sourceHandler; - int32_t sourceSaId; - void *sinkHandler; - int32_t sinkSaId; - void *hardwareHandler; - std::vector resourceDesc; -}; -} - struct CompConfig { std::string name; DHType type; @@ -61,8 +50,22 @@ struct CompConfig { std::string compSinkVersion; int32_t compSinkSaId; std::vector compResourceDesc; + bool haveFeature; + std::vector sourceFeatureFilters; + std::vector sinkSupportedFeatures; }; +struct CompHandler { + DHType type; + void *sourceHandler; + int32_t sourceSaId; + void *sinkHandler; + int32_t sinkSaId; + void *hardwareHandler; + CompConfig compConfig; +}; +} + class ComponentLoader { DECLARE_SINGLE_INSTANCE_BASE(ComponentLoader); @@ -76,15 +79,20 @@ public: int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr); int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr); int32_t UnInit(); - int32_t ReleaseHardwareHandler(const DHType dhType); - int32_t ReleaseSource(const DHType dhType); - int32_t ReleaseSink(const DHType dhType); std::vector GetAllCompTypes(); int32_t GetLocalDHVersion(DHVersion &dhVersion); int32_t GetSourceSaId(const DHType dhType); DHType GetDHTypeBySrcSaId(const int32_t saId); std::map GetCompResourceDesc(); + int32_t GetSource(const DHType dhType); + int32_t ReleaseSource(const DHType dhType); + int32_t GetSink(const DHType dhType); + int32_t ReleaseSink(const DHType dhType); + int32_t GetHardwareHandler(const DHType dhType); + int32_t ReleaseHardwareHandler(const DHType dhType); + bool IsDHTypeSupport(DHType dhType); + private: void *GetHandler(const std::string &soName); void GetAllHandler(std::map &dhtypeMap); @@ -94,9 +102,15 @@ private: int32_t ParseConfig(); void StoreLocalDHVersionInDB(); bool IsDHTypeExist(DHType dhType); + bool IsDHTypeSinkLoaded(DHType dhType); + bool IsDHTypeSourceLoaded(DHType dhType); + bool IsDHTypeHandlerLoaded(DHType dhType); std::string Readfile(const std::string &filePath); void ParseCompConfigFromJson(cJSON *component, CompConfig &config); void ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config); + void ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config); + void ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config); + void CheckAndParseFeatures(cJSON *component, CompConfig &config); bool CheckComponentEnable(const CompConfig &config); private: diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 4a7ee0a9..e71453a3 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -79,6 +79,22 @@ public: */ bool FetchNeedRefreshTask(const std::pair &taskKey, TaskParam &taskParam); + int32_t CheckDemandStart(const std::string &uuid, const DHType dhType, bool &enableSink, bool &enableSource); + int32_t RegisterDHStatusListener(sptr listener, int32_t callingUid, int32_t callingPid); + int32_t UnregisterDHStatusListener(sptr listener, int32_t callingUid, int32_t callingPid); + int32_t RegisterDHStatusListener(const std::string &networkId, + sptr listener, int32_t callingUid, int32_t callingPid); + int32_t UnregisterDHStatusListener(const std::string &networkId, + sptr listener, int32_t callingUid, int32_t callingPid); + int32_t EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); + int32_t DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); + int32_t EnableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); + int32_t DisableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); + int32_t ForceDisableSink(const DHDescriptor &dhDescriptor); + int32_t ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor); + class ComponentManagerEventHandler : public AppExecFwk::EventHandler { public: ComponentManagerEventHandler(const std::shared_ptr runner); @@ -95,15 +111,95 @@ private: STOP_SINK }; + enum class EnableState : int32_t { + DISABLED, + ENABLED + }; + + struct DHStatusCtrlKey { + int32_t uid; + int32_t pid; + + bool operator == (const DHStatusCtrlKey &other) const + { + return (uid == other.uid) && (pid == other.pid); + } + + bool operator < (const DHStatusCtrlKey &other) const + { + if (uid < other.uid) { + return true; + } else if (uid > other.uid) { + return false; + } + return pid < other.pid; + } + }; + + struct DHStatusCtrl { + EnableState enableState; + DHStatusCtrl() + { + enableState = EnableState::DISABLED; + } + }; + + struct DHStatusEnableInfo { + int32_t refEnable; + std::map dhStatusCtrl; + DHStatusEnableInfo() + { + refEnable = 0; + } + }; + + struct DHStatusSourceEnableInfoKey { + std::string networkId; + std::string dhId; + bool operator == (const DHStatusSourceEnableInfoKey &other) const + { + return (networkId == other.networkId) && (dhId == other.dhId); + } + bool operator < (const DHStatusSourceEnableInfoKey &other) const + { + if (networkId < other.networkId) { + return true; + } else if (networkId > other.networkId) { + return false; + } + return dhId < other.dhId; + } + }; + + struct DHSinkStatus { + int32_t refLoad; + std::map enableInfos; // key is dhid + std::map> listeners; + DHSinkStatus() + { + refLoad = 0; + } + }; + + struct DHSourceStatus { + int32_t refLoad; + std::map enableInfos; + std::map> listeners; + DHSourceStatus() + { + refLoad = 0; + } + }; + DHType GetDHType(const std::string &uuid, const std::string &dhId) const; - bool InitCompSource(); - bool InitCompSink(); - ActionResult StartSource(); + int32_t InitCompSource(DHType dhType); + int32_t UninitCompSource(DHType dhType); + int32_t InitCompSink(DHType dhType); + int32_t UninitCompSink(DHType dhType); ActionResult StartSource(DHType dhType); - ActionResult StopSource(); - ActionResult StartSink(); + ActionResult StopSource(DHType dhType); ActionResult StartSink(DHType dhType); - ActionResult StopSink(); + ActionResult StopSink(DHType dhType); bool WaitForResult(const Action &action, ActionResult result); int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m); @@ -118,18 +214,6 @@ private: 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(); - int32_t InitSAMonitor(); - void StartComponent(); - void RegisterDHStateListener(); - void RegisterDataSyncTriggerListener(); - void InitDHCommTool(); - - void UnInitSAMonitor(); - void UnregisterDHStateListener(); - void UnregisterDataSyncTriggerListener(); - void UnInitDHCommTool(); - void StopComponent(); void StopPrivacy(); int32_t GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr capability); @@ -140,6 +224,26 @@ private: std::shared_ptr &metaCapPtr); int32_t CheckSubtypeResource(const std::string &subtype, const std::string &networkId); + int32_t GetRemoteVerInfo(VersionInfo &versionInfo, const std::string &uuid, DHType dhType); + bool IsFeatureMatched(const std::vector &sourceFeatureFilters, + const std::vector &sinkSupportedFeatures); + int32_t EnableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener); + int32_t DisableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener); + int32_t EnableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener); + int32_t DisableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener); + int32_t ForceDisableSinkInternal( + const DHDescriptor &dhDescriptor, std::vector> &listeners); + int32_t ForceDisableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, std::vector> &listeners); + int32_t RealEnableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status); + int32_t RealDisableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor, + DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status); + private: std::map compSource_; std::shared_mutex compSourceMutex_; @@ -164,6 +268,12 @@ private: // save those remote dh that need refresh by full capability, {{device networkId, dhId}, TaskParam}. std::map, TaskParam> needRefreshTaskParams_; std::mutex needRefreshTaskParamsMtx_; + + // distributed hardware enable status maintenance. + std::map dhSinkStatus_; + std::mutex dhSinkStatusMtx_; + std::map dhSourceStatus_; + std::mutex dhSourceStatusMtx_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h index cb6d7408..827ff5ec 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -52,6 +52,15 @@ public: int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) override; int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) override; int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) override; + int32_t RegisterDHStatusListener(sptr listener) override; + int32_t UnregisterDHStatusListener(sptr listener) override; + int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) override; + int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener) override; + int32_t EnableSink(const std::vector &descriptors) override; + int32_t DisableSink(const std::vector &descriptors) override; + int32_t EnableSource(const std::string &networkId, const std::vector &descriptors) override; + int32_t DisableSource(const std::string &networkId, const std::vector &descriptors) override; protected: void OnStart() override; diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index a8100a28..4f3641d0 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -41,6 +41,19 @@ private: int32_t PauseDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); int32_t ResumeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); int32_t StopDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); + int32_t GetDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); + int32_t RegisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t UnregisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t RegisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t UnregisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply); + int32_t EnableSinkInner(MessageParcel &data, MessageParcel &reply); + int32_t DisableSinkInner(MessageParcel &data, MessageParcel &reply); + int32_t EnableSourceInner(MessageParcel &data, MessageParcel &reply); + int32_t DisableSourceInner(MessageParcel &data, MessageParcel &reply); + + int32_t ReadDescriptors(MessageParcel &data, std::vector &descriptors); + int32_t WriteDescriptors(MessageParcel &data, const std::vector &descriptors); + int32_t OnRemoteRequestEx(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); bool ValidTopic(uint32_t topic); bool ValidQueryLocalSpec(uint32_t spec); diff --git a/services/distributedhardwarefwkservice/include/ipc/hardware_status_listener_proxy.h b/services/distributedhardwarefwkservice/include/ipc/hardware_status_listener_proxy.h new file mode 100644 index 00000000..020c4d70 --- /dev/null +++ b/services/distributedhardwarefwkservice/include/ipc/hardware_status_listener_proxy.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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_HARDWARE_STATUS_LISTENER_PROXY_H +#define OHOS_HARDWARE_STATUS_LISTENER_PROXY_H + +#include "ihardware_status_listener.h" + +#include "iremote_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class HDSinkStatusListenerProxy : public IRemoteProxy { +public: + explicit HDSinkStatusListenerProxy(const sptr object); + virtual ~HDSinkStatusListenerProxy() override; + + virtual void OnEnable(const DHDescriptor &dhDescriptor) override; + virtual void OnDisable(const DHDescriptor &dhDescriptor) override; + +private: + static inline BrokerDelegator delegator_; +}; + +class HDSourceStatusListenerProxy : public IRemoteProxy { +public: + explicit HDSourceStatusListenerProxy(const sptr object); + virtual ~HDSourceStatusListenerProxy() override; + + virtual void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; + virtual void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // DistributedHardware +} // OHOS +#endif // OHOS_HARDWARE_STATUS_LISTENER_PROXY_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h index 0acd872d..eb97edc6 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -31,6 +31,7 @@ struct VersionInfo { std::string ToJsonString() const; }; +void ToJson(cJSON *jsonObject, const CompVersion &compVer); void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo); void FromJson(const cJSON *jsonObject, CompVersion &compVer); void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo); diff --git a/services/distributedhardwarefwkservice/include/task/disable_task.h b/services/distributedhardwarefwkservice/include/task/disable_task.h index 1904d4a5..346c32ae 100644 --- a/services/distributedhardwarefwkservice/include/task/disable_task.h +++ b/services/distributedhardwarefwkservice/include/task/disable_task.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -28,10 +28,31 @@ public: virtual ~DisableTask(); virtual void DoTask(); + void SetEffectSink(bool isEffect); + bool GetEffectSink(); + void SetEffectSource(bool isEffect); + bool GetEffectSource(); + void SetCallingUid(int32_t callingUid); + int32_t GetCallingUid(); + void SetCallingPid(int32_t callingPid); + int32_t GetCallingPid(); + private: /* synchronous function for unregister distributed hardware, return on asynchronous unregister finish */ int32_t UnRegisterHardware(); void DoTaskInner(); + int32_t DoAutoDisable(); + int32_t DoActiveDisable(); + +private: + // effect sink + bool effectSink_{ false }; + // effect source + bool effectSource_{ false }; + // enable or disable calling uid + int32_t callingUid_{ 0 }; + // enable or disable calling pid + int32_t callingPid_{ 0 }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/task/enable_task.h b/services/distributedhardwarefwkservice/include/task/enable_task.h index 36323aa3..f6a26ae4 100644 --- a/services/distributedhardwarefwkservice/include/task/enable_task.h +++ b/services/distributedhardwarefwkservice/include/task/enable_task.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -28,10 +28,31 @@ public: virtual ~EnableTask(); virtual void DoTask(); + void SetEffectSink(bool isEffect); + bool GetEffectSink(); + void SetEffectSource(bool isEffect); + bool GetEffectSource(); + void SetCallingUid(int32_t callingUid); + int32_t GetCallingUid(); + void SetCallingPid(int32_t callingPid); + int32_t GetCallingPid(); + private: /* synchronous function for register distributed hardware, return on asynchronous register finish */ int32_t RegisterHardware(); void DoTaskInner(); + int32_t DoAutoEnable(); + int32_t DoActiveEnable(); + +private: + // effect sink + bool effectSink_{ false }; + // effect source + bool effectSource_{ false }; + // enable or disable calling uid + int32_t callingUid_{ 0 }; + // enable or disable calling pid + int32_t callingPid_{ 0 }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/task/task_factory.h b/services/distributedhardwarefwkservice/include/task/task_factory.h index c52c5b3f..c733bd8d 100644 --- a/services/distributedhardwarefwkservice/include/task/task_factory.h +++ b/services/distributedhardwarefwkservice/include/task/task_factory.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -27,6 +27,9 @@ class TaskFactory { DECLARE_SINGLE_INSTANCE(TaskFactory); public: std::shared_ptr CreateTask(TaskType taskType, TaskParam taskParam, std::shared_ptr fatherTask); +private: + std::shared_ptr CreateEnableTask(const TaskParam &taskParam); + std::shared_ptr CreateDisableTask(const TaskParam &taskParam); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/utils/impl_utils.h b/services/distributedhardwarefwkservice/include/utils/impl_utils.h index b3b5a18b..389c093e 100644 --- a/services/distributedhardwarefwkservice/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkservice/include/utils/impl_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -60,6 +60,9 @@ struct CompVersion { std::string handlerVersion; std::string sourceVersion; std::string sinkVersion; + bool haveFeature; + std::vector sourceFeatureFilters; + std::vector sinkSupportedFeatures; }; struct DHVersion { @@ -78,7 +81,15 @@ struct TaskParam { // remote device dhid std::string dhId; // remote device dh type - DHType dhType; + DHType dhType{ DHType::UNKNOWN }; + // effect sink + bool effectSink{ false }; + // effect source + bool effectSource{ false }; + // enable or disable calling uid + int32_t callingUid{ 0 }; + // enable or disable calling pid + int32_t callingPid{ 0 }; }; struct TaskDump { diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index ed54b9c8..63225e94 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -250,6 +250,9 @@ CompVersion ComponentLoader::GetCompVersionFromComConfig(const CompConfig& cCfg) compVersions.handlerVersion = cCfg.compHandlerVersion; compVersions.sinkVersion = cCfg.compSinkVersion; compVersions.sourceVersion = cCfg.compSourceVersion; + compVersions.haveFeature = cCfg.haveFeature; + compVersions.sourceFeatureFilters = cCfg.sourceFeatureFilters; + compVersions.sinkSupportedFeatures = cCfg.sinkSupportedFeatures; return compVersions; } @@ -351,6 +354,7 @@ void ComponentLoader::ParseCompConfigFromJson(cJSON *component, CompConfig &conf if (IsArray(resourceDescs)) { ParseResourceDescFromJson(resourceDescs, config); } + CheckAndParseFeatures(component, config); } void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config) @@ -380,6 +384,41 @@ void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig } } +void ComponentLoader::ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config) +{ + cJSON *filter = nullptr; + config.sourceFeatureFilters.clear(); + cJSON_ArrayForEach(filter, sourceFeatureFilters) { + config.sourceFeatureFilters.push_back(filter->valuestring); + } +} + +void ComponentLoader::ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config) +{ + cJSON *feature = nullptr; + config.sinkSupportedFeatures.clear(); + cJSON_ArrayForEach(feature, sinkSupportedFeatures) { + config.sinkSupportedFeatures.push_back(feature->valuestring); + } +} + +void ComponentLoader::CheckAndParseFeatures(cJSON *component, CompConfig &config) +{ + cJSON *sourceFeatureFilters = cJSON_GetObjectItem(component, SOURCE_FEATURE_FILTER.c_str()); + cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(component, SINK_SUPPORTED_FEATURE.c_str()); + if (sourceFeatureFilters || sinkSupportedFeatures) { + config.haveFeature = true; + if (IsArray(sourceFeatureFilters)) { + ParseSourceFeatureFiltersFromJson(sourceFeatureFilters, config); + } + if (IsArray(sinkSupportedFeatures)) { + ParseSinkSupportedFeaturesFromJson(sinkSupportedFeatures, config); + } + } else { + config.haveFeature = false; + } +} + int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) { if (!isLocalVersionInit_.load()) { @@ -426,16 +465,13 @@ void ComponentLoader::GetAllHandler(std::map &dhtypeMap) for (itor = dhtypeMap.begin(); itor != dhtypeMap.end(); ++itor) { CompHandler comHandler; comHandler.type = itor->second.type; - comHandler.hardwareHandler = GetHandler(itor->second.compHandlerLoc); - comHandler.sourceHandler = GetHandler(itor->second.compSourceLoc); comHandler.sourceSaId = itor->second.compSourceSaId; - comHandler.sinkHandler = GetHandler(itor->second.compSinkLoc); comHandler.sinkSaId = itor->second.compSinkSaId; std::vector compResourceDesc = itor->second.compResourceDesc; for (auto it = compResourceDesc.begin(); it != compResourceDesc.end(); it++) { resDescMap_[it->subtype] = it->sensitiveValue; } - comHandler.resourceDesc = itor->second.compResourceDesc; + comHandler.compConfig = itor->second; compHandlerMap_[itor->second.type] = comHandler; } } @@ -443,22 +479,25 @@ void ComponentLoader::GetAllHandler(std::map &dhtypeMap) int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } - - if (compHandlerMap_[dhType].hardwareHandler == nullptr) { - DHLOGE("hardwareHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.hardwareHandler == nullptr) { + compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc); + if (compHandler.hardwareHandler == nullptr) { + DHLOGE("get hardware handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - - GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandlerMap_[dhType].hardwareHandler, + GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandler.hardwareHandler, COMPONENT_LOADER_GET_HARDWARE_HANDLER.c_str()); if (getHardwareClassHandler == nullptr) { DHLOGE("get getHardwareClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].hardwareHandler); - compHandlerMap_[dhType].hardwareHandler = nullptr; + ReleaseHandler(compHandler.hardwareHandler); + compHandler.hardwareHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } hardwareHandlerPtr = getHardwareClassHandler(); @@ -468,22 +507,25 @@ int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandle int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } - - if (compHandlerMap_[dhType].sourceHandler == nullptr) { - DHLOGE("sourceHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.sourceHandler == nullptr) { + compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc); + if (compHandler.sourceHandler == nullptr) { + DHLOGE("get source handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - GetSourceHardwareClass getSourceHardClassHandler = (GetSourceHardwareClass)dlsym( - compHandlerMap_[dhType].sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str()); + compHandler.sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str()); if (getSourceHardClassHandler == nullptr) { DHLOGE("get getSourceHardClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].sourceHandler); - compHandlerMap_[dhType].sourceHandler = nullptr; + ReleaseHandler(compHandler.sourceHandler); + compHandler.sourceHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } sourcePtr = getSourceHardClassHandler(); @@ -493,22 +535,25 @@ int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSour int32_t ComponentLoader::GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } - - if (compHandlerMap_[dhType].sinkHandler == nullptr) { - DHLOGE("sinkHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.sinkHandler == nullptr) { + compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc); + if (compHandler.sinkHandler == nullptr) { + DHLOGE("get sink handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - GetSinkHardwareClass getSinkHardwareClassHandler = - (GetSinkHardwareClass)dlsym(compHandlerMap_[dhType].sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str()); + (GetSinkHardwareClass)dlsym(compHandler.sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str()); if (getSinkHardwareClassHandler == nullptr) { DHLOGE("get getSinkHardwareClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].sinkHandler); - compHandlerMap_[dhType].sinkHandler = nullptr; + ReleaseHandler(compHandler.sinkHandler); + compHandler.sinkHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } sinkPtr = getSinkHardwareClassHandler(); @@ -584,17 +629,21 @@ int32_t ComponentLoader::UnInit() DHLOGI("release all handler"); DHTraceStart(COMPONENT_RELEASE_START); std::lock_guard lock(compHandlerMapMutex_); - int32_t ret = DH_FWK_SUCCESS; - for (std::map::iterator iter = compHandlerMap_.begin(); - iter != compHandlerMap_.end(); ++iter) { - ret += ReleaseHardwareHandler(iter->first); - ret += ReleaseSource(iter->first); - ret += ReleaseSink(iter->first); + for (auto iter = compHandlerMap_.begin(); iter != compHandlerMap_.end(); ++iter) { + if (iter->second.sinkHandler) { + ReleaseHandler(iter->second.sinkHandler); + } + if (iter->second.sourceHandler) { + ReleaseHandler(iter->second.sourceHandler); + } + if (iter->second.hardwareHandler) { + ReleaseHandler(iter->second.hardwareHandler); + } } compHandlerMap_.clear(); resDescMap_.clear(); DHTraceEnd(); - return ret; + return DH_FWK_SUCCESS; } int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType) @@ -602,12 +651,16 @@ int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeHandlerLoaded(dhType)) { + return ERR_DH_FWK_LOADER_HANDLER_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].hardwareHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release hardware handler failed."); } + compHandlerMap_[dhType].hardwareHandler = nullptr; return ret; } @@ -616,12 +669,16 @@ int32_t ComponentLoader::ReleaseSource(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeSourceLoaded(dhType)) { + return ERR_DH_FWK_LOADER_SOURCE_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sourceHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release source failed."); } + compHandlerMap_[dhType].sourceHandler = nullptr; return ret; } @@ -630,12 +687,16 @@ int32_t ComponentLoader::ReleaseSink(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeSinkLoaded(dhType)) { + return ERR_DH_FWK_LOADER_SINK_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sinkHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release sink failed."); } + compHandlerMap_[dhType].sinkHandler = nullptr; return ret; } @@ -648,6 +709,33 @@ bool ComponentLoader::IsDHTypeExist(DHType dhType) return true; } +bool ComponentLoader::IsDHTypeSinkLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].sinkHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X sink not loaded", dhType); + return false; + } + return true; +} + +bool ComponentLoader::IsDHTypeSourceLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].sourceHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X source not loaded", dhType); + return false; + } + return true; +} + +bool ComponentLoader::IsDHTypeHandlerLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].hardwareHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X handler not loaded", dhType); + return false; + } + return true; +} + int32_t ComponentLoader::GetSourceSaId(const DHType dhType) { std::lock_guard lock(compHandlerMapMutex_); @@ -675,5 +763,61 @@ std::map ComponentLoader::GetCompResourceDesc() { return resDescMap_; } + +int32_t ComponentLoader::GetSource(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.sourceHandler != nullptr) { + DHLOGE("sourceHandler is loaded."); + return ERR_DH_FWK_LOADER_SOURCE_LOAD; + } + compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc); + return DH_FWK_SUCCESS; +} + +int32_t ComponentLoader::GetSink(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.sinkHandler != nullptr) { + DHLOGE("sinkHandler is loaded."); + return ERR_DH_FWK_LOADER_SINK_LOAD; + } + compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc); + return DH_FWK_SUCCESS; +} + +int32_t ComponentLoader::GetHardwareHandler(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.hardwareHandler != nullptr) { + DHLOGE("hardwareHandler is loaded."); + return ERR_DH_FWK_LOADER_HANDLER_LOAD; + } + compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc); + return DH_FWK_SUCCESS; +} + +bool ComponentLoader::IsDHTypeSupport(DHType dhType) +{ + return IsDHTypeExist(dhType); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index aaf3d257..8ddc61c8 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -64,6 +64,8 @@ namespace { constexpr int32_t ENABLE_PARAM_RETRY_TIME = 500 * 1000; constexpr int32_t INVALID_SA_ID = -1; constexpr int32_t UNINIT_COMPONENT_TIMEOUT_SECONDS = 2; + constexpr int32_t SYNC_DEVICE_INFO_TIMEOUT_MILLISECONDS = 2000; + constexpr int32_t SYNC_DEVICE_INFO_INTERVAL_MILLISECONDS = 200; const std::string MONITOR_TASK_TIMER_ID = "monitor_task_timer_id"; } @@ -89,18 +91,6 @@ int32_t ComponentManager::Init() { DHLOGI("start."); DHTraceStart(COMPONENT_INIT_START); - InitComponentHandler(); - - int32_t ret = InitSAMonitor(); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("Init SA monitor failed, ret: %{public}d", ret); - return ret; - } - - StartComponent(); - RegisterDHStateListener(); - RegisterDataSyncTriggerListener(); - InitDHCommTool(); #ifdef DHARDWARE_LOW_LATENCY Publisher::GetInstance().RegisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_); #endif @@ -109,180 +99,22 @@ int32_t ComponentManager::Init() return DH_FWK_SUCCESS; } -void ComponentManager::InitComponentHandler() -{ - DHLOGI("start."); - if (!InitCompSource()) { - DHLOGE("InitCompSource failed."); - DHTraceEnd(); - } - if (!InitCompSink()) { - DHLOGE("InitCompSink failed."); - DHTraceEnd(); - } -} - -int32_t ComponentManager::InitSAMonitor() -{ - std::unique_lock lock(compSourceMutex_); - if (compMonitorPtr_ == nullptr) { - DHLOGE("compMonitorPtr_ is null."); - return ERR_DH_FWK_COMPONENT_MONITOR_NULL; - } - for (const auto &comp : compSource_) { - if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { - continue; - } - compMonitorPtr_->AddSAMonitor(compSrcSaId_.at(comp.first)); - } - return DH_FWK_SUCCESS; -} - -void ComponentManager::StartComponent() -{ - auto sourceResult = StartSource(); - auto sinkResult = StartSink(); - - if (!WaitForResult(Action::START_SOURCE, sourceResult)) { - DHLOGE("StartSource failed, some virtual components maybe cannot work, but want to continue"); - HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, - "dhfwk start source failed."); - } - if (!WaitForResult(Action::START_SINK, sinkResult)) { - DHLOGE("StartSink failed, some virtual components maybe cannot work, but want to continue"); - HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, - "dhfwk start sink failed."); - } -} - -void ComponentManager::RegisterDHStateListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Register DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->RegisterDistributedHardwareStateListener(dhStateListener_); - } -} - -void ComponentManager::RegisterDataSyncTriggerListener() -{ - std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); - eventHandler_ = std::make_shared(runner); - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Register Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->RegisterDataSyncTriggerListener(dataSyncTriggerListener_); - } -} - -void ComponentManager::InitDHCommTool() -{ - if (dhCommToolPtr_ == nullptr) { - DHLOGE("DH communication tool ptr is null"); - return; - } - DHLOGI("Init DH communication tool"); - dhCommToolPtr_->Init(); -} - int32_t ComponentManager::UnInit() { DHLOGI("start."); - UnregisterDHStateListener(); - UnregisterDataSyncTriggerListener(); - UnInitDHCommTool(); StopPrivacy(); - UnInitSAMonitor(); - StopComponent(); - #ifdef DHARDWARE_LOW_LATENCY Publisher::GetInstance().UnregisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_); LowLatency::GetInstance().CloseLowLatency(); #endif DHLOGI("Release component success"); - if (isUnInitTimeOut_.load()) { DHLOGE("Some component stop timeout, FORCE exit!"); _Exit(0); } - return DH_FWK_SUCCESS; } -void ComponentManager::UnInitSAMonitor() -{ - // clear SA monitor - std::unique_lock lock(compSourceMutex_); - if (compMonitorPtr_ == nullptr) { - DHLOGE("compMonitorPtr_ is null."); - return; - } - for (const auto &comp : compSource_) { - if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { - continue; - } - compMonitorPtr_->RemoveSAMonitor(compSrcSaId_.at(comp.first)); - } -} - -void ComponentManager::UnregisterDHStateListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Unregister DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->UnregisterDistributedHardwareStateListener(); - } -} - -void ComponentManager::UnregisterDataSyncTriggerListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Unregister Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->UnregisterDataSyncTriggerListener(); - } -} - -void ComponentManager::UnInitDHCommTool() -{ - if (dhCommToolPtr_ == nullptr) { - DHLOGE("DH communication tool ptr is null"); - return; - } - DHLOGI("UnInit DH communication tool"); - dhCommToolPtr_->UnInit(); -} - -void ComponentManager::StopComponent() -{ - // stop source and sink sa - auto sourceResult = StopSource(); - auto sinkResult = StopSink(); - - if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { - DHLOGE("StopSource failed, but want to continue"); - } - if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSink failed, but want to continue"); - } -} - void ComponentManager::StopPrivacy() { // stop privacy @@ -297,30 +129,6 @@ void ComponentManager::StopPrivacy() } } -ActionResult ComponentManager::StartSource() -{ - DHLOGI("start."); - std::unique_lock lock(compSourceMutex_); - std::unordered_map> futures; - std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; - for (const auto &item : compSource_) { - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); - auto params = compversion.sourceVersion; - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item, params] () mutable { - p.set_value(item.second->InitSource(params)); - }).detach(); - futures.emplace(item.first, f.share()); - } - return futures; -} - ActionResult ComponentManager::StartSource(DHType dhType) { DHLOGI("Start Source, dhType: %{public}" PRIu32, (uint32_t)dhType); @@ -348,38 +156,6 @@ ActionResult ComponentManager::StartSource(DHType dhType) return futures; } -ActionResult ComponentManager::StartSink() -{ - DHLOGI("start."); - std::unique_lock lock(compSinkMutex_); - std::unordered_map> futures; - std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; - for (const auto &item : compSink_) { - if (item.second == nullptr) { - DHLOGE("comp sink ptr is null"); - continue; - } - CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); - auto params = compversion.sinkVersion; - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item, params] () mutable { - p.set_value(item.second->InitSink(params)); - }).detach(); - futures.emplace(item.first, f.share()); - if (cameraCompPrivacy_ == nullptr && item.first == DHType::CAMERA) { - cameraCompPrivacy_ = std::make_shared(); - item.second->RegisterPrivacyResources(cameraCompPrivacy_); - } - if (audioCompPrivacy_ == nullptr && item.first == DHType::AUDIO) { - audioCompPrivacy_ = std::make_shared(); - item.second->RegisterPrivacyResources(audioCompPrivacy_); - } - } - return futures; -} - ActionResult ComponentManager::StartSink(DHType dhType) { DHLOGI("Start Sink, dhType: %{public}" PRIu32, (uint32_t)dhType); @@ -415,56 +191,6 @@ ActionResult ComponentManager::StartSink(DHType dhType) return futures; } -ActionResult ComponentManager::StopSource() -{ - DHLOGI("start."); - std::unique_lock lock(compSourceMutex_); - std::unordered_map> futures; - for (const auto &item : compSource_) { - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item] () mutable { - p.set_value(item.second->ReleaseSource()); - }).detach(); - futures.emplace(item.first, f.share()); - } - compSource_.clear(); - return futures; -} - -ActionResult ComponentManager::StopSink() -{ - DHLOGI("start."); - std::unique_lock lock(compSinkMutex_); - std::unordered_map> futures; - for (const auto &item : compSink_) { - if (item.second == nullptr) { - DHLOGE("comp sink ptr is null"); - continue; - } - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item] () mutable { - p.set_value(item.second->ReleaseSink()); - IHardwareHandler *hardwareHandler = nullptr; - int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(item.first, hardwareHandler); - if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) { - DHLOGE("GetHardwareHandler %{public}#X failed", item.first); - return status; - } - hardwareHandler->UnRegisterPluginListener(); - return status; - }).detach(); - futures.emplace(item.first, f.share()); - } - compSink_.clear(); - return futures; -} - bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsResult) { DHLOGD("start."); @@ -496,51 +222,6 @@ bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsR return ret; } -bool ComponentManager::InitCompSource() -{ - auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); - std::unique_lock lock(compSourceMutex_); - for (const auto &type : compTypes) { - IDistributedHardwareSource *sourcePtr = nullptr; - auto ret = ComponentLoader::GetInstance().GetSource(type, sourcePtr); - if (ret != DH_FWK_SUCCESS) { - DHLOGW("GetSource failed, compType = %{public}#X, ret = %{public}d.", type, ret); - continue; - } - if (sourcePtr == nullptr) { - DHLOGW("sourcePtr is null, compType = %{public}#X.", type); - continue; - } - compSource_.insert(std::make_pair(type, sourcePtr)); - - int32_t saId = ComponentLoader::GetInstance().GetSourceSaId(type); - if (saId != INVALID_SA_ID) { - compSrcSaId_.insert(std::make_pair(type, saId)); - } - } - return !compSource_.empty(); -} - -bool ComponentManager::InitCompSink() -{ - auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); - std::unique_lock lock(compSinkMutex_); - for (const auto &type : compTypes) { - IDistributedHardwareSink *sinkPtr = nullptr; - auto ret = ComponentLoader::GetInstance().GetSink(type, sinkPtr); - if (ret != DH_FWK_SUCCESS) { - DHLOGW("GetSink failed, compType = %{public}#X, ret = %{public}d.", type, ret); - continue; - } - if (sinkPtr == nullptr) { - DHLOGW("sinkPtr is null, compType = %{public}#X.", type); - continue; - } - compSink_.insert(std::make_pair(type, sinkPtr)); - } - return !compSink_.empty(); -} - int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { @@ -1138,5 +819,865 @@ std::shared_ptr ComponentManager return this->eventHandler_; } +int32_t ComponentManager::CheckDemandStart(const std::string &uuid, + const DHType dhType, bool &enableSink, bool &enableSource) +{ + // Initialize output parameters + enableSink = false; + enableSource = false; + + // Get remote config + VersionInfo versionInfo; + auto ret = GetRemoteVerInfo(versionInfo, uuid, dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetRemoteVerInfo fail, errCode = %{public}d!", ret); + return ret; + } + + // Get local config + DHVersion dhVersion; + ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetLocalDHVersion fail, errCode = %{public}d!", ret); + return ret; + } + + auto iterLocal = dhVersion.compVersions.find(dhType); + if (iterLocal == dhVersion.compVersions.end()) { + DHLOGE("Not find dhType in local: %{public}#X!", dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto iterRemote = versionInfo.compVersions.find(dhType); + if (iterRemote == versionInfo.compVersions.end()) { + DHLOGE("Not find dhType in remote: %{public}#X!", dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + // Check local config + if (!iterLocal->second.haveFeature) { + enableSink = true; + enableSource = true; + return DH_FWK_SUCCESS; + } + + if (iterLocal->second.sinkSupportedFeatures.size()) { + enableSink = true; + } + + if (iterLocal->second.sourceFeatureFilters.size() == 0) { + return DH_FWK_SUCCESS; + } + + // Check remote config + if (!iterRemote->second.haveFeature) { // Remote config is null, need enable source + enableSource = true; + return DH_FWK_SUCCESS; + } + + if (iterRemote->second.sinkSupportedFeatures.size() == 0) { // Remote sink config is empty, not enable source + return DH_FWK_SUCCESS; + } + + // Check if the configurations on both ends match + enableSource = IsFeatureMatched(iterLocal->second.sourceFeatureFilters, iterRemote->second.sinkSupportedFeatures); + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::RegisterDHStatusListener( + sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSinkStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + if (listeners.find(ctrlKey) != listeners.end()) { + DHLOGE("Repeat call RegisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners[ctrlKey] = listener; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UnregisterDHStatusListener( + sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSinkStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + auto it = listeners.find(ctrlKey); + if (it == listeners.end()) { + DHLOGE("Repeat call UnregisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners.erase(it); + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::RegisterDHStatusListener( + const std::string &networkId, sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSourceStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSourceStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + if (listeners.find(ctrlKey) != listeners.end()) { + DHLOGE("Repeat call RegisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners[ctrlKey] = listener; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UnregisterDHStatusListener( + const std::string &networkId, sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSourceStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSourceStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + auto it = listeners.find(ctrlKey); + if (it == listeners.end()) { + DHLOGE("Repeat call UnregisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners.erase(it); + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = EnableSinkInternal(dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnEnable(dhDescriptor); + DHLOGI("Callback business sink OnEnable."); + } + } + return ret; +} + +int32_t ComponentManager::DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = DisableSinkInternal(dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnDisable(dhDescriptor); + DHLOGI("Callback business sink OnDisable."); + } + } + return ret; +} + +int32_t ComponentManager::EnableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = EnableSourceInternal(networkId, dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnEnable(networkId, dhDescriptor); + DHLOGI("Callback business source OnEnable."); + } + } + return ret; +} + +int32_t ComponentManager::DisableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = DisableSourceInternal(networkId, dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnDisable(networkId, dhDescriptor); + DHLOGI("Callback business source OnDisable."); + } + } + return ret; +} + +int32_t ComponentManager::ForceDisableSink(const DHDescriptor &dhDescriptor) +{ + std::vector> listeners; + int32_t ret = ForceDisableSinkInternal(dhDescriptor, listeners); + if (ret == DH_FWK_SUCCESS) { + for (auto listener : listeners) { + listener->OnDisable(dhDescriptor); + DHLOGI("Callback business sink OnDisable."); + } + } + return ret; +} + +int32_t ComponentManager::ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor) +{ + std::vector> listeners; + int32_t ret = ForceDisableSourceInternal(networkId, dhDescriptor, listeners); + if (ret == DH_FWK_SUCCESS) { + for (auto listener : listeners) { + listener->OnDisable(networkId, dhDescriptor); + DHLOGI("Callback business source OnDisable."); + } + } + return ret; +} + +int32_t ComponentManager::GetRemoteVerInfo(VersionInfo &versionInfo, const std::string &uuid, DHType dhType) +{ + auto deviceId = GetDeviceIdByUUID(uuid); + auto ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get Version info Manager failed, uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + for (int32_t retryCount = 0; + retryCount < SYNC_DEVICE_INFO_TIMEOUT_MILLISECONDS/SYNC_DEVICE_INFO_INTERVAL_MILLISECONDS; + ++retryCount) { + std::this_thread::sleep_for(std::chrono::milliseconds(SYNC_DEVICE_INFO_INTERVAL_MILLISECONDS)); + ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + if (ret == DH_FWK_SUCCESS) { + break; + } else { + DHLOGE("Attempted to retrieve VersionInfo again but still failed, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + } + } + if (ret != DH_FWK_SUCCESS) { + DHLOGE("After trying again, it ultimately failed, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + return ret; + } else { + DHLOGI("After multiple attempts, obtaining VersionInfo was successful, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + } + } + return ret; +} + +bool ComponentManager::IsFeatureMatched(const std::vector &sourceFeatureFilters, + const std::vector &sinkSupportedFeatures) +{ + for (const auto &filter : sourceFeatureFilters) { + for (const auto &feature : sinkSupportedFeatures) { + if (feature == filter) { + return true; + } + } + } + return false; +} + +int32_t ComponentManager::EnableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto &status = dhSinkStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[dhDescriptor.id]; + + // Check if the business is being called repeatedly + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::ENABLED) { + DHLOGE("Repeat call EnableSink, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check reference count + if (enableInfo.refEnable || status.refLoad) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + // Start enabling hardware sink + auto ret = InitCompSink(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("InitCompSink failed, ret = %{public}d.", ret); + return ret; + } + auto sinkResult = StartSink(dhDescriptor.dhType); + if (!WaitForResult(Action::START_SINK, sinkResult)) { + DHLOGE("StartSink failed, some virtual components maybe cannot work, but want to continue!"); + HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, + "dhfwk start sink failed."); + UninitCompSink(dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable = 1; + status.refLoad = 1; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::DisableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto &status = dhSinkStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[dhDescriptor.id]; + + // Check if the business is being called repeatedly + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::DISABLED) { + DHLOGE("Repeat call DisableSink, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check reference count + if (enableInfo.refEnable > 1 || status.refLoad > 1) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + // Start disabling hardware sink + auto sinkResult = StopSink(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SINK, sinkResult)) { + DHLOGE("StopSource failed, but want to continue!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + auto ret = UninitCompSink(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSink failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable = 0; + status.refLoad = 0; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid, sptr &listener) +{ + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + + std::lock_guard lock(dhSourceStatusMtx_); + + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::ENABLED) { + DHLOGE("Repeat call EnableSource, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check enable reference count + if (enableInfo.refEnable) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad) { + auto ret = Enable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Enable failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + auto ret = RealEnableSource(networkId, uuid, dhDescriptor, statusCtrl, enableInfo, status); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("RealEnableSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::DisableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid, sptr &listener) +{ + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + + std::lock_guard lock(dhSourceStatusMtx_); + + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::DISABLED) { + DHLOGE("Repeat call DisableSource, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check enable reference count + if (enableInfo.refEnable > 1) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad > 1) { + auto ret = Disable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Disable failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + auto ret = RealDisableSource(networkId, uuid, dhDescriptor, statusCtrl, enableInfo, status); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("RealDisableSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::ForceDisableSinkInternal( + const DHDescriptor &dhDescriptor, std::vector> &listeners) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto &status = dhSinkStatus_[dhDescriptor.dhType]; + auto itEnableInfo = status.enableInfos.find(dhDescriptor.id); + if (itEnableInfo == status.enableInfos.end()) { + DHLOGE("Repeat call ForceDisableSink, dhType = %{public}u, id = %{public}s.", + dhDescriptor.dhType, dhDescriptor.id.c_str()); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + auto &enableInfo = itEnableInfo->second; + + // Collect listeners and reduce the load count + for (auto &item : enableInfo.dhStatusCtrl) { + if (item.second.enableState != EnableState::DISABLED) { + auto it = status.listeners.find(item.first); + if (it != status.listeners.end()) { + auto listener = it->second; + listeners.push_back(listener); + } + } + } + status.refLoad -= enableInfo.refEnable; + status.enableInfos.erase(itEnableInfo); + if (status.refLoad > 0) { + return DH_FWK_SUCCESS; + } + + // Unload component + auto sinkResult = StopSink(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SINK, sinkResult)) { + DHLOGE("StopSource failed, but want to continue!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + auto ret = UninitCompSink(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSink failed, ret = %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::ForceDisableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, std::vector> &listeners) +{ + std::lock_guard lock(dhSourceStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto itEnableInfo = status.enableInfos.find(enableInfoKey); + if (itEnableInfo == status.enableInfos.end()) { + DHLOGE("Repeat call ForceDisableSource, networkId = %{public}s, dhType = %{public}u, id = %{public}s.", + GetAnonyString(networkId).c_str(), dhDescriptor.dhType, dhDescriptor.id.c_str()); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + auto &enableInfo = itEnableInfo->second; + + // First, disable the hardware + auto uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + auto ret = Disable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Disable failed, ret = %{public}d.", ret); + return ret; + } + + // Then collect listeners and reduce the load count + for (auto &item : enableInfo.dhStatusCtrl) { + if (item.second.enableState != EnableState::DISABLED) { + auto it = status.listeners.find(item.first); + if (it != status.listeners.end()) { + auto listener = it->second; + listeners.push_back(listener); + } + } + } + status.refLoad -= enableInfo.refEnable; + status.enableInfos.erase(itEnableInfo); + if (status.refLoad > 0) { + return DH_FWK_SUCCESS; + } + + // Unload component + auto sourceResult = StopSource(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { + DHLOGE("StopSource timeout!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + ret = UninitCompSource(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSource failed, ret = %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::RealEnableSource(const std::string &networkId, const std::string &uuid, + const DHDescriptor &dhDescriptor, DHStatusCtrl &statusCtrl, + DHStatusEnableInfo &enableInfo, DHSourceStatus &status) +{ + auto ret = InitCompSource(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("InitCompSource failed, ret = %{public}d.", ret); + return ret; + } + auto sourceResult = StartSource(dhDescriptor.dhType); + if (!WaitForResult(Action::START_SOURCE, sourceResult)) { + DHLOGE("StartSource failed, some virtual components maybe cannot work, but want to continue!"); + HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, + "dhfwk start source failed."); + UninitCompSource(dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT; + } + ret = Enable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Enable failed, ret = %{public}d.", ret); + StopSource(dhDescriptor.dhType); + UninitCompSource(dhDescriptor.dhType); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable = 1; + status.refLoad = 1; + return ret; +} + +int32_t ComponentManager::RealDisableSource(const std::string &networkId, const std::string &uuid, + const DHDescriptor &dhDescriptor, DHStatusCtrl &statusCtrl, + DHStatusEnableInfo &enableInfo, DHSourceStatus &status) +{ + auto ret = Disable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Disable failed, ret = %{public}d.", ret); + return ret; + } + auto sourceResult = StopSource(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { + DHLOGE("StopSource timeout!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + ret = UninitCompSource(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSource failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable = 0; + status.refLoad = 0; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::InitCompSource(DHType dhType) +{ + std::unique_lock lock(compSourceMutex_); + IDistributedHardwareSource *sourcePtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSource(dhType, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + if (sourcePtr == nullptr) { + DHLOGE("sourcePtr is null, compType = %{public}#X.", dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + compSource_.insert(std::make_pair(dhType, sourcePtr)); + auto saId = ComponentLoader::GetInstance().GetSourceSaId(dhType); + if (saId != INVALID_SA_ID) { + compSrcSaId_.insert(std::make_pair(dhType, saId)); + if (compMonitorPtr_ == nullptr) { + DHLOGE("compMonitorPtr_ is null."); + return ERR_DH_FWK_COMPONENT_MONITOR_NULL; + } + compMonitorPtr_->AddSAMonitor(saId); + } else { + DHLOGE("GetSourceSaId return INVALID_SA_ID, compType = %{public}#X.", dhType); + } + sourcePtr->RegisterDistributedHardwareStateListener(dhStateListener_); + sourcePtr->RegisterDataSyncTriggerListener(dataSyncTriggerListener_); + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UninitCompSource(DHType dhType) +{ + std::unique_lock lock(compSourceMutex_); + IDistributedHardwareSource *sourcePtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSource(dhType, sourcePtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + if (sourcePtr == nullptr) { + DHLOGE("sourcePtr is null, compType = %{public}#X.", dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + sourcePtr->UnregisterDataSyncTriggerListener(); + sourcePtr->UnregisterDistributedHardwareStateListener(); + auto it = compSrcSaId_.find(dhType); + if (it != compSrcSaId_.end()) { + if (compMonitorPtr_ == nullptr) { + DHLOGE("compMonitorPtr_ is null."); + return ERR_DH_FWK_COMPONENT_MONITOR_NULL; + } + compMonitorPtr_->RemoveSAMonitor(it->second); + compSrcSaId_.erase(it); + } + ret = ComponentLoader::GetInstance().ReleaseSource(dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + compSource_.erase(dhType); + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::InitCompSink(DHType dhType) +{ + std::unique_lock lock(compSinkMutex_); + IDistributedHardwareSink *sinkPtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSink(dhType, sinkPtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSink failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + if (sinkPtr == nullptr) { + DHLOGE("sinkPtr is null, compType = %{public}#X.", dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + compSink_.insert(std::make_pair(dhType, sinkPtr)); + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UninitCompSink(DHType dhType) +{ + std::unique_lock lock(compSinkMutex_); + auto ret = ComponentLoader::GetInstance().ReleaseSink(dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + compSink_.erase(dhType); + return DH_FWK_SUCCESS; +} + +ActionResult ComponentManager::StopSource(DHType dhType) +{ + std::shared_lock lock(compSourceMutex_); + std::unordered_map> futures; + if (compSource_.find(dhType) == compSource_.end()) { + DHLOGE("Component for DHType: %{public}" PRIu32 " not init source handler.", (uint32_t)dhType); + return futures; + } + auto sourcePtr = compSource_[dhType]; + if (sourcePtr == nullptr) { + DHLOGE("comp source ptr is null."); + return futures; + } + std::promise p; + std::future f = p.get_future(); + std::thread([p = std::move(p), sourcePtr] () mutable { + p.set_value(sourcePtr->ReleaseSource()); + }).detach(); + futures.emplace(dhType, f.share()); + return futures; +} + +ActionResult ComponentManager::StopSink(DHType dhType) +{ + std::shared_lock lock(compSinkMutex_); + std::unordered_map> futures; + if (compSink_.find(dhType) == compSink_.end()) { + DHLOGE("Component for DHType: %{public}" PRIu32 " not init sink handler.", (uint32_t)dhType); + return futures; + } + auto sinkPtr = compSink_[dhType]; + if (sinkPtr == nullptr) { + DHLOGE("comp sink ptr is null."); + return futures; + } + std::promise p; + std::future f = p.get_future(); + std::thread([p = std::move(p), sinkPtr, dhType] () mutable { + p.set_value(sinkPtr->ReleaseSink()); + IHardwareHandler *hardwareHandler = nullptr; + int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandler); + if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) { + DHLOGE("GetHardwareHandler %{public}#X failed.", dhType); + return status; + } + hardwareHandler->UnRegisterPluginListener(); + return status; + }).detach(); + futures.emplace(dhType, f.share()); + return futures; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index ff88dc9a..30d48e10 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -30,6 +30,7 @@ #include "access_manager.h" #include "av_trans_control_center.h" #include "capability_info_manager.h" +#include "meta_info_manager.h" #include "component_manager.h" #include "dh_context.h" #include "dh_utils_tool.h" @@ -39,6 +40,8 @@ #include "distributed_hardware_log.h" #include "distributed_hardware_manager_factory.h" #include "publisher.h" +#include "task_executor.h" +#include "task_factory.h" namespace OHOS { namespace DistributedHardware { @@ -48,6 +51,7 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DistributedHardwareService, DISTRIBUTED_HARDWARE_S namespace { constexpr int32_t INIT_BUSINESS_DELAY_TIME_MS = 5 * 100; const std::string INIT_TASK_ID = "CheckAndInitDH"; + const std::string LOCAL_NETWORKID_ALIAS = "local"; } DistributedHardwareService::DistributedHardwareService(int32_t saId, bool runOnCreate) @@ -358,5 +362,179 @@ int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const } return DH_FWK_SUCCESS; } + +int32_t DistributedHardwareService::GetDistributedHardware( + const std::string &networkId, std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + std::string deviceId; + if (networkId == LOCAL_NETWORKID_ALIAS) { + deviceId = GetLocalDeviceInfo().deviceId; + } else { + deviceId = GetDeviceIdByUUID(DHContext::GetInstance().GetUUIDByNetworkId(networkId)); + } + std::vector> capabilities; + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); + if (capabilities.empty()) { + CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(networkId); + std::string udidHash = Sha256(udid); + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + for (const auto &metaCapInfo : metaCapInfos) { + DHDescriptor descriptor; + descriptor.id = metaCapInfo->GetDHId(); + descriptor.dhType = metaCapInfo->GetDHType(); + descriptors.push_back(descriptor); + } + } else { + for (const auto &capabilitie : capabilities) { + DHDescriptor descriptor; + descriptor.id = capabilitie->GetDHId(); + descriptor.dhType = capabilitie->GetDHType(); + descriptors.push_back(descriptor); + } + } + + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::RegisterDHStatusListener(sptr listener) +{ + int32_t callingUid = IPCSkeleton::GetCallingUid(); + int32_t callingPid = IPCSkeleton::GetCallingPid(); + int32_t ret = ComponentManager::GetInstance().RegisterDHStatusListener(listener, callingUid, callingPid); + if (ret != 0) { + DHLOGE("RegisterDHStatusListener failed, ret: %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::UnregisterDHStatusListener(sptr listener) +{ + int32_t callingUid = IPCSkeleton::GetCallingUid(); + int32_t callingPid = IPCSkeleton::GetCallingPid(); + int32_t ret = ComponentManager::GetInstance().UnregisterDHStatusListener(listener, callingUid, callingPid); + if (ret != 0) { + DHLOGE("UnregisterDHStatusListener failed, ret: %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::RegisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + int32_t callingPid = IPCSkeleton::GetCallingPid(); + int32_t ret = ComponentManager::GetInstance().RegisterDHStatusListener( + networkId, listener, callingUid, callingPid); + if (ret != 0) { + DHLOGE("RegisterDHStatusListener failed, ret: %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::UnregisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + int32_t callingPid = IPCSkeleton::GetCallingPid(); + int32_t ret = ComponentManager::GetInstance().UnregisterDHStatusListener( + networkId, listener, callingUid, callingPid); + if (ret != 0) { + DHLOGE("UnregisterDHStatusListener failed, ret: %{public}d.", ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::EnableSink(const std::vector &descriptors) +{ + for (const auto &descriptor : descriptors) { + TaskParam taskParam = { + .dhId = descriptor.id, + .dhType = descriptor.dhType, + .effectSink = true, + .effectSource = false, + .callingUid = IPCSkeleton::GetCallingUid(), + .callingPid = IPCSkeleton::GetCallingPid() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::DisableSink(const std::vector &descriptors) +{ + for (const auto &descriptor : descriptors) { + TaskParam taskParam = { + .dhId = descriptor.id, + .dhType = descriptor.dhType, + .effectSink = true, + .effectSource = false, + .callingUid = IPCSkeleton::GetCallingUid(), + .callingPid = IPCSkeleton::GetCallingPid() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::EnableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + for (const auto &descriptor : descriptors) { + TaskParam taskParam = { + .networkId = networkId, + .dhId = descriptor.id, + .dhType = descriptor.dhType, + .effectSink = false, + .effectSource = true, + .callingUid = IPCSkeleton::GetCallingUid(), + .callingPid = IPCSkeleton::GetCallingPid() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::DisableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + for (const auto &descriptor : descriptors) { + TaskParam taskParam = { + .networkId = networkId, + .dhId = descriptor.id, + .dhType = descriptor.dhType, + .effectSink = false, + .effectSource = true, + .callingUid = IPCSkeleton::GetCallingUid(), + .callingPid = IPCSkeleton::GetCallingPid() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } + return DH_FWK_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index 66405c6b..d1466d0f 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -82,7 +82,7 @@ int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &d return StopDistributedHardwareInner(data, reply); } default: - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + return OnRemoteRequestEx(code, data, reply, option); } return DH_FWK_SUCCESS; } @@ -389,6 +389,249 @@ int32_t DistributedHardwareStub::StopDistributedHardwareInner(MessageParcel &dat return DH_FWK_SUCCESS; } +int32_t DistributedHardwareStub::GetDistributedHardwareInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::vector descriptors; + std::string networkId = data.ReadString(); + int32_t ret = GetDistributedHardware(networkId, descriptors); + if (WriteDescriptors(reply, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::RegisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Input distributed hardware status sink listener is null"); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = RegisterDHStatusListener(listener); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::UnregisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Input distributed hardware status sink listener is null"); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = UnregisterDHStatusListener(listener); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::RegisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::string networkId = data.ReadString(); + sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Input distributed hardware status source listener is null"); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = RegisterDHStatusListener(networkId, listener); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::UnregisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::string networkId = data.ReadString(); + sptr listener = iface_cast(data.ReadRemoteObject()); + if (listener == nullptr) { + DHLOGE("Input distributed hardware status source listener is null"); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = UnregisterDHStatusListener(networkId, listener); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::EnableSinkInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::vector descriptors; + ReadDescriptors(data, descriptors); + int32_t ret = EnableSink(descriptors); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::DisableSinkInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::vector descriptors; + ReadDescriptors(data, descriptors); + int32_t ret = DisableSink(descriptors); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::EnableSourceInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::string networkId = data.ReadString(); + std::vector descriptors; + ReadDescriptors(data, descriptors); + int32_t ret = EnableSource(networkId, descriptors); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::DisableSourceInner(MessageParcel &data, MessageParcel &reply) +{ + if (!HasAccessDHPermission()) { + DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); + return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; + } + std::string networkId = data.ReadString(); + std::vector descriptors; + ReadDescriptors(data, descriptors); + int32_t ret = DisableSource(networkId, descriptors); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed!"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::ReadDescriptors(MessageParcel &data, std::vector &descriptors) +{ + int32_t size = data.ReadInt32(); + if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { + DHLOGE("The array descriptors are too large, size: %{public}d!", size); + return ERR_DH_FWK_PARA_INVALID; + } + for (int32_t i = 0; i < size; ++i) { + DHDescriptor descriptor; + descriptor.dhType = static_cast(data.ReadInt32()); + descriptor.id = data.ReadString(); + descriptors.push_back(descriptor); + } + return NO_ERROR; +} + +int32_t DistributedHardwareStub::WriteDescriptors(MessageParcel &data, const std::vector &descriptors) +{ + int32_t size = (int32_t)descriptors.size(); + if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { + DHLOGE("The array descriptors are too large, size: %{public}d!", size); + return ERR_DH_FWK_PARA_INVALID; + } + if (!data.WriteInt32(size)) { + DHLOGE("Write descriptors size failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + for (int32_t i = 0; i < size; ++i) { + const DHDescriptor &descriptor = descriptors.at(i); + int32_t type = static_cast(descriptor.dhType); + if (!data.WriteInt32(type)) { + DHLOGE("Write descriptor.dhType failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(descriptor.id)) { + DHLOGE("Write descriptor.id failed!"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + } + return NO_ERROR; +} + +int32_t DistributedHardwareStub::OnRemoteRequestEx(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + switch (code) { + case static_cast(DHMsgInterfaceCode::GET_DISTRIBUTED_HARDWARE): { + return GetDistributedHardwareInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::REG_DH_SINK_STATUS_LISTNER): { + return RegisterDHStatusSinkListenerInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::UNREG_DH_SINK_STATUS_LISTNER): { + return UnregisterDHStatusSinkListenerInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::REG_DH_SOURCE_STATUS_LISTNER): { + return RegisterDHStatusSourceListenerInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::UNREG_DH_SOURCE_STATUS_LISTNER): { + return UnregisterDHStatusSourceListenerInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::ENABLE_SINK): { + return EnableSinkInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::DISABLE_SINK): { + return DisableSinkInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::ENABLE_SOURCE): { + return EnableSourceInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::DISABLE_SOURCE): { + return DisableSourceInner(data, reply); + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return DH_FWK_SUCCESS; +} + bool DistributedHardwareStub::HasAccessDHPermission() { Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); diff --git a/services/distributedhardwarefwkservice/src/ipc/hardware_status_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/hardware_status_listener_proxy.cpp new file mode 100644 index 00000000..df0c9a4e --- /dev/null +++ b/services/distributedhardwarefwkservice/src/ipc/hardware_status_listener_proxy.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2025 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 "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_log.h" +#include "hardware_status_listener_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +HDSinkStatusListenerProxy::HDSinkStatusListenerProxy(const sptr object) + : IRemoteProxy(object) +{ +} + +HDSinkStatusListenerProxy::~HDSinkStatusListenerProxy() +{ +} + +void HDSinkStatusListenerProxy::OnEnable(const DHDescriptor &dhDescriptor) +{ + DHLOGI("HDSinkStatusListenerProxy OnEnable."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("HDSinkStatusListenerProxy write token failed!"); + return; + } + if (!data.WriteUint32((uint32_t)dhDescriptor.dhType)) { + DHLOGE("HDSinkStatusListenerProxy write dhDescriptor.dhType failed!"); + return; + } + if (!data.WriteString(dhDescriptor.id)) { + DHLOGE("HDSinkStatusListenerProxy write dhDescriptor.id failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IHDSinkStatusListener::Message::ON_ENABLE), data, reply, option); + if (ret != 0) { + DHLOGE("HDSinkStatusListenerProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +void HDSinkStatusListenerProxy::OnDisable(const DHDescriptor &dhDescriptor) +{ + DHLOGI("HDSinkStatusListenerProxy OnDisable."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("HDSinkStatusListenerProxy write token failed!"); + return; + } + if (!data.WriteUint32((uint32_t)dhDescriptor.dhType)) { + DHLOGE("HDSinkStatusListenerProxy write dhDescriptor.dhType failed!"); + return; + } + if (!data.WriteString(dhDescriptor.id)) { + DHLOGE("HDSinkStatusListenerProxy write dhDescriptor.id failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IHDSinkStatusListener::Message::ON_DISABLE), data, reply, option); + if (ret != 0) { + DHLOGE("HDSinkStatusListenerProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +HDSourceStatusListenerProxy::HDSourceStatusListenerProxy(const sptr object) + : IRemoteProxy(object) +{ +} + +HDSourceStatusListenerProxy::~HDSourceStatusListenerProxy() +{ +} + +void HDSourceStatusListenerProxy::OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) +{ + DHLOGI("HDSourceStatusListenerProxy OnEnable."); + if (!IsIdLengthValid(networkId)) { + DHLOGE("IsIdLengthValid check failed!"); + return; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("HDSourceStatusListenerProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("HDSourceStatusListenerProxy write networkId failed!"); + return; + } + if (!data.WriteUint32((uint32_t)dhDescriptor.dhType)) { + DHLOGE("HDSourceStatusListenerProxy write dhDescriptor.dhType failed!"); + return; + } + if (!data.WriteString(dhDescriptor.id)) { + DHLOGE("HDSourceStatusListenerProxy write dhDescriptor.id failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(HDSourceStatusListenerProxy::Message::ON_ENABLE), data, reply, option); + if (ret != 0) { + DHLOGE("HDSourceStatusListenerProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +void HDSourceStatusListenerProxy::OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) +{ + DHLOGI("HDSourceStatusListenerProxy OnDisable."); + if (!IsIdLengthValid(networkId)) { + DHLOGE("IsIdLengthValid check failed!"); + return; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("HDSourceStatusListenerProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("HDSourceStatusListenerProxy write networkId failed!"); + return; + } + if (!data.WriteUint32((uint32_t)dhDescriptor.dhType)) { + DHLOGE("HDSourceStatusListenerProxy write dhDescriptor.dhType failed!"); + return; + } + if (!data.WriteString(dhDescriptor.id)) { + DHLOGE("HDSourceStatusListenerProxy write dhDescriptor.id failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(HDSourceStatusListenerProxy::Message::ON_DISABLE), data, reply, option); + if (ret != 0) { + DHLOGE("HDSourceStatusListenerProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 7c8fdace..a4f7d7d2 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -64,6 +64,40 @@ std::string VersionInfo::ToJsonString() const return result; } +void ToJson(cJSON *jsonObject, const CompVersion &compVer) +{ + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } + cJSON_AddStringToObject(jsonObject, NAME.c_str(), compVer.name.c_str()); + cJSON_AddNumberToObject(jsonObject, TYPE.c_str(), (double)compVer.dhType); + cJSON_AddStringToObject(jsonObject, HANDLER.c_str(), compVer.handlerVersion.c_str()); + cJSON_AddStringToObject(jsonObject, SOURCE_VER.c_str(), compVer.sourceVersion.c_str()); + cJSON_AddStringToObject(jsonObject, SINK_VER.c_str(), compVer.sinkVersion.c_str()); + if (compVer.haveFeature) { + cJSON *sourceFeatureFilters = cJSON_CreateArray(); + if (sourceFeatureFilters == NULL) { + DHLOGE("Failed to create cJSON object."); + return; + } + cJSON *sinkSupportedFeatures = cJSON_CreateArray(); + if (sinkSupportedFeatures == NULL) { + cJSON_Delete(sourceFeatureFilters); + DHLOGE("Failed to create cJSON object."); + return; + } + for (const auto &filter : compVer.sourceFeatureFilters) { + cJSON_AddItemToArray(sourceFeatureFilters, cJSON_CreateString(filter.c_str())); + } + cJSON_AddItemToObject(jsonObject, SOURCE_FEATURE_FILTER.c_str(), sourceFeatureFilters); + for (const auto &feature : compVer.sinkSupportedFeatures) { + cJSON_AddItemToArray(sinkSupportedFeatures, cJSON_CreateString(feature.c_str())); + } + cJSON_AddItemToObject(jsonObject, SINK_SUPPORTED_FEATURE.c_str(), sinkSupportedFeatures); + } +} + void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) { if (jsonObject == nullptr) { @@ -85,11 +119,7 @@ void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) DHLOGE("Failed to create cJSON object."); return; } - cJSON_AddStringToObject(compVer, NAME.c_str(), compVersion.second.name.c_str()); - cJSON_AddNumberToObject(compVer, TYPE.c_str(), (double)compVersion.second.dhType); - cJSON_AddStringToObject(compVer, HANDLER.c_str(), compVersion.second.handlerVersion.c_str()); - cJSON_AddStringToObject(compVer, SOURCE_VER.c_str(), compVersion.second.sourceVersion.c_str()); - cJSON_AddStringToObject(compVer, SINK_VER.c_str(), compVersion.second.sinkVersion.c_str()); + ToJson(compVer, compVersion.second); cJSON_AddItemToArray(compVers, compVer); } cJSON_AddItemToObject(jsonObject, COMP_VER.c_str(), compVers); @@ -121,6 +151,27 @@ void FromJson(const cJSON *jsonObject, CompVersion &compVer) if (IsString(sinkVerJson)) { compVer.sinkVersion = sinkVerJson->valuestring; } + cJSON *sourceFeatureFilters = cJSON_GetObjectItem(jsonObject, SOURCE_FEATURE_FILTER.c_str()); + cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(jsonObject, SINK_SUPPORTED_FEATURE.c_str()); + if (sourceFeatureFilters || sinkSupportedFeatures) { + compVer.haveFeature = true; + if (sourceFeatureFilters) { + cJSON *filterObj = nullptr; + compVer.sourceFeatureFilters.clear(); + cJSON_ArrayForEach(filterObj, sourceFeatureFilters) { + compVer.sourceFeatureFilters.push_back(filterObj->valuestring); + } + } + if (sinkSupportedFeatures) { + cJSON *featureObj = nullptr; + compVer.sinkSupportedFeatures.clear(); + cJSON_ArrayForEach(featureObj, sinkSupportedFeatures) { + compVer.sinkSupportedFeatures.push_back(featureObj->valuestring); + } + } + } else { + compVer.haveFeature = false; + } } void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo) diff --git a/services/distributedhardwarefwkservice/src/task/disable_task.cpp b/services/distributedhardwarefwkservice/src/task/disable_task.cpp index 0314c571..3c875fef 100644 --- a/services/distributedhardwarefwkservice/src/task/disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/disable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -23,6 +23,7 @@ #include "capability_utils.h" #include "component_manager.h" #include "constants.h" +#include "dh_context.h" #include "dh_utils_hitrace.h" #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" @@ -87,12 +88,116 @@ void DisableTask::DoTaskInner() int32_t DisableTask::UnRegisterHardware() { DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_DISABLE_START); - auto result = ComponentManager::GetInstance().Disable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType()); - DHLOGI("disable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s", - (result == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), + + int32_t ret = DH_FWK_SUCCESS; + + // Determine whether it is an active disable + if (GetCallingUid() || GetCallingPid()) { + // It is an active disable + ret = DoActiveDisable(); + } else { + // It is an auto disable + ret = DoAutoDisable(); + } + DHLOGI("disable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.", + (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str()); DHTraceEnd(); - return result; + return ret; +} + +void DisableTask::SetEffectSink(bool isEffect) +{ + effectSink_ = isEffect; +} + +bool DisableTask::GetEffectSink() +{ + return effectSink_; +} + +void DisableTask::SetEffectSource(bool isEffect) +{ + effectSource_ = isEffect; +} + +bool DisableTask::GetEffectSource() +{ + return effectSource_; +} + +void DisableTask::SetCallingUid(int32_t callingUid) +{ + callingUid_ = callingUid; +} + +int32_t DisableTask::GetCallingUid() +{ + return callingUid_; +} + +void DisableTask::SetCallingPid(int32_t callingPid) +{ + callingPid_ = callingPid; +} + +int32_t DisableTask::GetCallingPid() +{ + return callingPid_; +} + +int32_t DisableTask::DoAutoDisable() +{ + bool disableSink = false; + bool disableSource = false; + int32_t ret = ComponentManager::GetInstance().CheckDemandStart(GetUUID(), GetDhType(), disableSink, disableSource); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("CheckDemandStart failed!"); + return ret; + } + if (DHContext::GetInstance().GetRealTimeOnlineDeviceCount() == 0 && + DHContext::GetInstance().GetIsomerismConnectCount() == 0) { + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (disableSink) { + ret = ComponentManager::GetInstance().ForceDisableSink(dhDescriptor); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSink failed!"); + } + } + if (disableSource) { + ret = ComponentManager::GetInstance().ForceDisableSource(GetNetworkId(), dhDescriptor); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSource failed!"); + } + } + } + return ret; +} + +int32_t DisableTask::DoActiveDisable() +{ + int32_t ret = DH_FWK_SUCCESS; + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (GetEffectSink()) { + ret = ComponentManager::GetInstance().DisableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSink failed!"); + } + } + if (GetEffectSource()) { + ret = ComponentManager::GetInstance().DisableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSource failed!"); + } + } + return ret; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/enable_task.cpp b/services/distributedhardwarefwkservice/src/task/enable_task.cpp index 1287e69f..90a61a4d 100644 --- a/services/distributedhardwarefwkservice/src/task/enable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/enable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -83,12 +83,117 @@ void EnableTask::DoTaskInner() int32_t EnableTask::RegisterHardware() { DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_ENABLE_START); - auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType()); - DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s", - (result == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), + + int32_t ret = DH_FWK_SUCCESS; + + // Determine whether it is an active enable + if (GetCallingUid() || GetCallingPid()) { + // It is an active enable + ret = DoActiveEnable(); + } else { + // It is an auto enable + ret = DoAutoEnable(); + } + DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.", + (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str()); DHTraceEnd(); - return result; + return ret; +} + +void EnableTask::SetEffectSink(bool isEffect) +{ + effectSink_ = isEffect; +} + +bool EnableTask::GetEffectSink() +{ + return effectSink_; +} + +void EnableTask::SetEffectSource(bool isEffect) +{ + effectSource_ = isEffect; +} + +bool EnableTask::GetEffectSource() +{ + return effectSource_; +} + +void EnableTask::SetCallingUid(int32_t callingUid) +{ + callingUid_ = callingUid; +} + +int32_t EnableTask::GetCallingUid() +{ + return callingUid_; +} + +void EnableTask::SetCallingPid(int32_t callingPid) +{ + callingPid_ = callingPid; +} + +int32_t EnableTask::GetCallingPid() +{ + return callingPid_; +} + +int32_t EnableTask::DoAutoEnable() +{ + bool enableSink = false; + bool enableSource = false; + int32_t ret = ComponentManager::GetInstance().CheckDemandStart(GetUUID(), GetDhType(), enableSink, enableSource); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("CheckDemandStart failed!"); + return ret; + } + if (!enableSink && !enableSource) { + return ERR_DH_FWK_COMPONENT_LIMIT_DEMAND_START; + } + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (enableSink) { + ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSink failed!"); + } + } + if (enableSource) { + ret = ComponentManager::GetInstance().EnableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSource failed!"); + } + } + return ret; +} + +int32_t EnableTask::DoActiveEnable() +{ + int32_t ret = DH_FWK_SUCCESS; + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (GetEffectSink()) { + ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSink failed!"); + } + } + if (GetEffectSource()) { + ret = ComponentManager::GetInstance().EnableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSource failed!"); + } + } + return ret; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/online_task.cpp b/services/distributedhardwarefwkservice/src/task/online_task.cpp index eb747826..f5c990c2 100644 --- a/services/distributedhardwarefwkservice/src/task/online_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/online_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -87,7 +87,14 @@ void OnLineTask::DoSyncInfo() "udid: %{public}s, udidHash: %{public}s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(deviceId).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str(), GetAnonyString(udidHash).c_str()); - auto ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + + auto ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("SyncVersionInfoFromDB failed, deviceId = %{public}s, uuid = %{public}s, errCode = %{public}d", + GetAnonyString(deviceId).c_str(), GetAnonyString(GetUUID()).c_str(), ret); + } + + ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncLocalCapabilityInfoFromDB failed, deviceId = %{public}s, errCode = %{public}d", GetAnonyString(deviceId).c_str(), ret); diff --git a/services/distributedhardwarefwkservice/src/task/task_factory.cpp b/services/distributedhardwarefwkservice/src/task/task_factory.cpp index 36ebbfe2..27f791c7 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -39,13 +39,11 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP std::shared_ptr task = nullptr; switch (taskType) { case TaskType::ENABLE: { - task = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, - taskParam.dhId, taskParam.dhType); + task = CreateEnableTask(taskParam); break; } case TaskType::DISABLE: { - task = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, - taskParam.dhId, taskParam.dhType); + task = CreateDisableTask(taskParam); break; } case TaskType::ON_LINE: { @@ -82,5 +80,31 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP return task; } + +std::shared_ptr TaskFactory::CreateEnableTask(const TaskParam &taskParam) +{ + auto enableTask = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, + taskParam.dhId, taskParam.dhType); + if (enableTask != nullptr) { + enableTask->SetEffectSink(taskParam.effectSink); + enableTask->SetEffectSource(taskParam.effectSource); + enableTask->SetCallingUid(taskParam.callingUid); + enableTask->SetCallingPid(taskParam.callingPid); + } + return enableTask; +} + +std::shared_ptr TaskFactory::CreateDisableTask(const TaskParam &taskParam) +{ + auto disableTask = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, + taskParam.dhId, taskParam.dhType); + if (disableTask != nullptr) { + disableTask->SetEffectSink(taskParam.effectSink); + disableTask->SetEffectSource(taskParam.effectSource); + disableTask->SetCallingUid(taskParam.callingUid); + disableTask->SetCallingPid(taskParam.callingPid); + } + return disableTask; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 9bd53e90..d2bc80c6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -174,7 +174,7 @@ HWTEST_F(ComponentManagerTest, init_compSource_test_001, TestSize.Level0) { ComponentLoader::GetInstance().Init(); ComponentManager::GetInstance().compSource_.clear(); - auto ret = ComponentManager::GetInstance().InitCompSource(); + auto ret = ComponentManager::GetInstance().InitCompSource(DHType::INPUT); EXPECT_NE(ret, ComponentManager::GetInstance().compSource_.empty()); } @@ -188,7 +188,7 @@ HWTEST_F(ComponentManagerTest, init_compSink_test_001, TestSize.Level0) { ComponentLoader::GetInstance().Init(); ComponentManager::GetInstance().compSink_.clear(); - auto ret = ComponentManager::GetInstance().InitCompSink(); + auto ret = ComponentManager::GetInstance().InitCompSink(DHType::INPUT); EXPECT_NE(ret, ComponentManager::GetInstance().compSink_.empty()); } @@ -302,7 +302,7 @@ HWTEST_F(ComponentManagerTest, StartSource_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSource *sourcePtr = nullptr; ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - auto ret = ComponentManager::GetInstance().StartSource(); + auto ret = ComponentManager::GetInstance().StartSource(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSource_.clear(); } @@ -333,22 +333,6 @@ HWTEST_F(ComponentManagerTest, StartSource_002, TestSize.Level0) * @tc.require: AR000GHSJM */ HWTEST_F(ComponentManagerTest, StartSink_001, TestSize.Level0) -{ - DHType dhType = DHType::CAMERA; - IDistributedHardwareSink *sinkPtr = nullptr; - ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); - auto ret = ComponentManager::GetInstance().StartSink(); - EXPECT_EQ(true, ret.empty()); - ComponentManager::GetInstance().compSink_.clear(); -} - -/** - * @tc.name: StartSink_002 - * @tc.desc: Verify the StartSink function - * @tc.type: FUNC - * @tc.require: AR000GHSJM - */ -HWTEST_F(ComponentManagerTest, StartSink_002, TestSize.Level0) { DHType dhType = DHType::CAMERA; IDistributedHardwareSink *sinkPtr = nullptr; @@ -369,7 +353,7 @@ HWTEST_F(ComponentManagerTest, StopSource_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSource *sourcePtr = nullptr; ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - auto ret = ComponentManager::GetInstance().StopSource(); + auto ret = ComponentManager::GetInstance().StopSource(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSource_.clear(); } @@ -385,7 +369,7 @@ HWTEST_F(ComponentManagerTest, StopSink_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSink *sinkPtr = nullptr; ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); - auto ret = ComponentManager::GetInstance().StopSink(); + auto ret = ComponentManager::GetInstance().StopSink(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSink_.clear(); } @@ -413,7 +397,7 @@ HWTEST_F(ComponentManagerTest, WaitForResult_001, TestSize.Level0) */ HWTEST_F(ComponentManagerTest, InitCompSource_001, TestSize.Level0) { - bool ret = ComponentManager::GetInstance().InitCompSource(); + bool ret = ComponentManager::GetInstance().InitCompSource(DHType::AUDIO); EXPECT_EQ(true, ret); } #endif @@ -426,7 +410,7 @@ HWTEST_F(ComponentManagerTest, WaitForResult_001, TestSize.Level0) */ HWTEST_F(ComponentManagerTest, InitCompSink_001, TestSize.Level0) { - bool ret = ComponentManager::GetInstance().InitCompSink(); + bool ret = ComponentManager::GetInstance().InitCompSink(DHType::AUDIO); EXPECT_EQ(true, ret); } @@ -1068,62 +1052,5 @@ HWTEST_F(ComponentManagerTest, GetDHSourceInstance_002, TestSize.Level0) EXPECT_EQ(nullptr, dhSourcePtr); ComponentManager::GetInstance().compSource_.clear(); } - -HWTEST_F(ComponentManagerTest, InitSAMonitor_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(DHType::AUDIO, 1)); - auto ret = ComponentManager::GetInstance().InitSAMonitor(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(dhType, 1)); - ret = ComponentManager::GetInstance().InitSAMonitor(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ComponentManager::GetInstance().compSource_.clear(); - ComponentManager::GetInstance().compSrcSaId_.clear(); -} - -HWTEST_F(ComponentManagerTest, RegisterDHStateListener_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RegisterDHStateListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnregisterDHStateListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RegisterDataSyncTriggerListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnregisterDataSyncTriggerListener()); - ComponentManager::GetInstance().compSource_.clear(); -} - -HWTEST_F(ComponentManagerTest, InitDHCommTool_001, TestSize.Level0) -{ - ComponentManager::GetInstance().dhCommToolPtr_ = nullptr; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().InitDHCommTool()); -} - -HWTEST_F(ComponentManagerTest, UnInitSAMonitor_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(DHType::AUDIO, 1)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitSAMonitor()); - - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(dhType, 1)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitSAMonitor()); - ComponentManager::GetInstance().compSource_.clear(); - ComponentManager::GetInstance().compSrcSaId_.clear(); -} - -HWTEST_F(ComponentManagerTest, UnInitDHCommTool_001, TestSize.Level0) -{ - ComponentManager::GetInstance().dhCommToolPtr_ = nullptr; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitDHCommTool()); -} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/include/distributed_hardware_stub_test.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/include/distributed_hardware_stub_test.h index 55e90c90..bc5d86fb 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/include/distributed_hardware_stub_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/include/distributed_hardware_stub_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -122,6 +122,65 @@ int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) (void)networkId; return DH_FWK_SUCCESS; } + +int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t RegisterDHStatusListener(sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t UnregisterDHStatusListener(sptr listener) +{ + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener) +{ + (void)networkId; + (void)listener; + return DH_FWK_SUCCESS; +} + +int32_t EnableSink(const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DisableSink(const std::vector &descriptors) +{ + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t EnableSource(const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} + +int32_t DisableSource(const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; + return DH_FWK_SUCCESS; +} }; } // namespace DistributedHardware } // namespace OHOS -- Gitee