From 5a8be61bc490a636f8e6b8ab58bae7b6fc210db6 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Mon, 28 Apr 2025 14:51:16 +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=A1=86=E6=9E=B6=E8=8E=B7=E5=8F=96=E7=A1=AC=E4=BB=B6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=E6=8E=A5=E5=8F=A3=E6=94=B9=E6=88=90=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 --- .../include/distributed_hardware_errno.h | 1 + common/utils/include/idistributed_hardware.h | 4 +- .../igetdistributedhardware_callback.h | 42 ++++++ interfaces/inner_kits/BUILD.gn | 1 + .../include/distributed_hardware_fwk_kit.h | 4 +- .../include/ipc/distributed_hardware_proxy.h | 3 +- .../getdistributedhardware_callback_stub.h | 37 ++++++ .../src/distributed_hardware_fwk_kit.cpp | 6 +- .../src/ipc/distributed_hardware_proxy.cpp | 12 +- .../getdistributedhardware_callback_stub.cpp | 75 +++++++++++ .../distributedhardwarefwkkit_fuzzer.cpp | 17 ++- .../distributedhardwarefwkkit_fuzzer.h | 10 ++ .../common/distributedhardwarefwkkit/BUILD.gn | 5 + .../distributed_hardware_fwk_kit_test.h | 21 +++ .../src/distributed_hardware_fwk_kit_test.cpp | 30 ++++- .../include/dhfwk_sa_manager_test.h | 4 +- .../include/distributed_hardware_proxy_test.h | 13 +- .../src/distributed_hardware_proxy_test.cpp | 35 +++-- .../distributedhardwarefwkservice/BUILD.gn | 1 + .../include/distributed_hardware_service.h | 3 +- .../getdistributedhardware_callback_proxy.h | 41 ++++++ .../resourcemanager/capability_info_manager.h | 7 + .../src/distributed_hardware_service.cpp | 42 +++--- .../src/distributed_hardware_stub.cpp | 11 +- .../getdistributedhardware_callback_proxy.cpp | 124 ++++++++++++++++++ .../capability_info_manager.cpp | 54 ++++++++ .../distributedfwkservices_fuzzer.cpp | 23 +++- .../src/distributed_hardware_service_test.cpp | 7 +- .../include/distributed_hardware_stub_test.h | 18 ++- .../src/distributed_hardware_stub_test.cpp | 2 + 30 files changed, 592 insertions(+), 61 deletions(-) create mode 100644 common/utils/include/igetdistributedhardware_callback.h create mode 100644 interfaces/inner_kits/include/ipc/getdistributedhardware_callback_stub.h create mode 100644 interfaces/inner_kits/src/ipc/getdistributedhardware_callback_stub.cpp create mode 100644 services/distributedhardwarefwkservice/include/ipc/getdistributedhardware_callback_proxy.h create mode 100644 services/distributedhardwarefwkservice/src/ipc/getdistributedhardware_callback_proxy.cpp diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 406412c1..bbd6cdf4 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -104,6 +104,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_PUBLISHER_LISTENER_IS_NULL = -10808; constexpr int32_t ERR_DH_FWK_SERVICE_MSG_INVALID = -10809; constexpr int32_t ERR_DH_FWK_AVTRANS_CALLBACK_IS_NULL = -10810; + constexpr int32_t ERR_DH_FWK_GETDISTRIBUTEDHARDWARE_TIMEOUT = -10811; /* AccessManager errno, range: [-10900, -10999] */ constexpr int32_t ERR_DH_FWK_ACCESS_INIT_DM_FAILED = -10900; diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index f219c268..e3833c92 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -23,6 +23,7 @@ #include "iremote_broker.h" #include "device_type.h" #include "dhardware_descriptor.h" +#include "igetdistributedhardware_callback.h" #include "ihardware_status_listener.h" #include "distributed_hardware_fwk_kit_paras.h" @@ -51,7 +52,8 @@ 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 GetDistributedHardware(const std::string &networkId, + const sptr callback) = 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; diff --git a/common/utils/include/igetdistributedhardware_callback.h b/common/utils/include/igetdistributedhardware_callback.h new file mode 100644 index 00000000..e9f41150 --- /dev/null +++ b/common/utils/include/igetdistributedhardware_callback.h @@ -0,0 +1,42 @@ +/* + * 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_IGETDISTRIBUTEDHARDWARE_CALLBACK_H +#define OHOS_IGETDISTRIBUTEDHARDWARE_CALLBACK_H + +#include +#include + +#include + +#include "dhardware_descriptor.h" + +namespace OHOS { +namespace DistributedHardware { +class IGetDistributedHardwareCallback : public IRemoteBroker { +public: + virtual void OnSuccess(const std::string &networkId, const std::vector &descriptors) = 0; + virtual void OnError(const std::string &networkId, int32_t error) = 0; + + enum class Message : uint32_t { + ON_SUCCESS, + ON_ERROR + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedHardwareFwk.IGetDistributedHardwareCallback"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_IGETDISTRIBUTEDHARDWARE_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index a322c46b..d61e8acc 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/getdistributedhardware_callback_stub.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 a531c680..47503c9b 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -27,6 +27,7 @@ #include "dhardware_descriptor.h" #include "ipublisher_listener.h" #include "idistributed_hardware.h" +#include "igetdistributedhardware_callback.h" #include "ihardware_status_listener.h" #ifndef API_EXPORT @@ -166,7 +167,8 @@ public: * @param descriptors distributed hardware descriptor list. * @return Returns 0 if success. */ - API_EXPORT int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors); + API_EXPORT int32_t GetDistributedHardware(const std::string &networkId, + const sptr callback); /** * @brief Register distributed hardware status listener. diff --git a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 5f57ee29..19ea2852 100644 --- a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -47,7 +47,8 @@ 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 GetDistributedHardware(const std::string &networkId, + const sptr callback) override; int32_t RegisterDHStatusListener(sptr listener) override; int32_t UnregisterDHStatusListener(sptr listener) override; int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) override; diff --git a/interfaces/inner_kits/include/ipc/getdistributedhardware_callback_stub.h b/interfaces/inner_kits/include/ipc/getdistributedhardware_callback_stub.h new file mode 100644 index 00000000..63f5a8ad --- /dev/null +++ b/interfaces/inner_kits/include/ipc/getdistributedhardware_callback_stub.h @@ -0,0 +1,37 @@ +/* + * 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_GETDISTRIBUTEDHARDWARE_CALLBACK_STUB_H +#define OHOS_GETDISTRIBUTEDHARDWARE_CALLBACK_STUB_H + +#include "igetdistributedhardware_callback.h" + +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class GetDistributedHardwareCallbackStub : public IRemoteStub { +public: + GetDistributedHardwareCallbackStub(); + virtual ~GetDistributedHardwareCallbackStub() override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t ReadDescriptors(MessageParcel &data, std::vector &descriptors); +private: + DISALLOW_COPY_AND_MOVE(GetDistributedHardwareCallbackStub); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_GETDISTRIBUTEDHARDWARE_CALLBACK_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 2ed3502f..fdb34a4b 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -270,17 +270,17 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const } int32_t DistributedHardwareFwkKit::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) + const std::string &networkId, const sptr callback) { 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."); + DHLOGI("DHFWK not online or get proxy failed, can not get distributed hardware."); return ERR_DH_FWK_POINTER_IS_NULL; } - return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, descriptors); + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, callback); } int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(sptr listener) diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index 4f89b6b5..4314f235 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -532,12 +532,16 @@ int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const s } int32_t DistributedHardwareProxy::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) + const std::string &networkId, const sptr callback) { DHLOGI("DistributedHardwareProxy GetDistributedHardware."); if (!IsIdLengthValid(networkId)) { return ERR_DH_FWK_PARA_INVALID; } + if (callback == nullptr) { + DHLOGE("get distributed hardware callback is null"); + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null!"); @@ -554,14 +558,16 @@ int32_t DistributedHardwareProxy::GetDistributedHardware( DHLOGE("Write networkId failed!"); return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; } + if (!data.WriteRemoteObject(callback->AsObject())) { + DHLOGE("Write callback failed!"); + return ERR_DH_FWK_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(); } diff --git a/interfaces/inner_kits/src/ipc/getdistributedhardware_callback_stub.cpp b/interfaces/inner_kits/src/ipc/getdistributedhardware_callback_stub.cpp new file mode 100644 index 00000000..db575396 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/getdistributedhardware_callback_stub.cpp @@ -0,0 +1,75 @@ +/* + * 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 "getdistributedhardware_callback_stub.h" + +namespace OHOS { +namespace DistributedHardware { +GetDistributedHardwareCallbackStub::GetDistributedHardwareCallbackStub() +{ +} + +GetDistributedHardwareCallbackStub::~GetDistributedHardwareCallbackStub() +{ +} + +int32_t GetDistributedHardwareCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("GetDistributedHardwareCallbackStub read valid token failed!"); + return ERR_INVALID_DATA; + } + IGetDistributedHardwareCallback::Message msgCode = static_cast(code); + switch (msgCode) { + case IGetDistributedHardwareCallback::Message::ON_SUCCESS: { + std::vector descriptors; + std::string networkId = data.ReadString(); + ReadDescriptors(data, descriptors); + OnSuccess(networkId, descriptors); + break; + } + case IGetDistributedHardwareCallback::Message::ON_ERROR: { + std::string networkId = data.ReadString(); + int32_t error = data.ReadInt32(); + OnError(networkId, error); + break; + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return DH_FWK_SUCCESS; +} + +int32_t GetDistributedHardwareCallbackStub::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; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp index 42628ce7..cb215963 100644 --- a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp @@ -64,6 +64,19 @@ void TestHDSourceStatusListener::OnDisable( (void)dhDescriptor; } +void TestGetDistributedHardwareCallback::OnSuccess(const std::string &networkId, + const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; +} + +void TestGetDistributedHardwareCallback::OnError(const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + void RegisterPublisherListenerFuzzTest(const uint8_t *data, size_t size) { if ((data == nullptr) || (size < sizeof(uint32_t))) { @@ -200,10 +213,10 @@ void GetDistributedHardwareFuzzTest(const uint8_t *data, size_t size) return; } + sptr callback(new TestGetDistributedHardwareCallback()); DistributedHardwareFwkKit dhfwkKit; - std::vector descriptors; std::string networkId(reinterpret_cast(data), size); - dhfwkKit.GetDistributedHardware(networkId, descriptors); + dhfwkKit.GetDistributedHardware(networkId, callback); } void RegisterDHStatusListenerFuzzTest(const uint8_t *data, size_t size) diff --git a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h index 76e62164..743e6c52 100644 --- a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h @@ -19,6 +19,7 @@ #define FUZZ_PROJECT_NAME "distributedhardwarefwkkit_fuzzer" #include +#include "getdistributedhardware_callback_stub.h" #include "hardware_status_listener_stub.h" #include "publisher_listener_stub.h" @@ -48,6 +49,15 @@ protected: void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; }; + +class TestGetDistributedHardwareCallback : public GetDistributedHardwareCallbackStub { +public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; +protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors) override; + void OnError(const std::string &networkId, int32_t error) override; +}; } // namespace DistributedHardware } // namespace OHOS #endif \ No newline at end of file diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn index 793ec486..347ad53e 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn @@ -47,8 +47,13 @@ ohos_unittest("DistributedHardwareFwkKitTest") { ] external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", "cJSON:cjson", "c_utils:utils", + "device_manager:devicemanagersdk", + "dsoftbus:softbus_client", "hilog:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h index 2e6995ec..2819140f 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h @@ -23,7 +23,11 @@ #include #include +#include "dm_device_info.h" +#include "device_manager.h" + #include "distributed_hardware_fwk_kit.h" +#include "getdistributedhardware_callback_stub.h" #include "hardware_status_listener_stub.h" #include "publisher_listener_stub.h" #include "system_ability_load_callback_stub.h" @@ -69,6 +73,23 @@ public: void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; }; + class TestDmInitCallback : public DmInitCallback { + public: + TestDmInitCallback() = default; + virtual ~TestDmInitCallback() = default; + protected: + void OnRemoteDied() override; + }; + + class TestGetDistributedHardwareCallback : public GetDistributedHardwareCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors) override; + void OnError(const std::string &networkId, int32_t error) override; + }; + public: std::shared_ptr dhfwkPtr_ = nullptr; }; diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp index 62446249..45aaf082 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp @@ -20,6 +20,12 @@ #include #include +#include "anonymous_string.h" +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "softbus_common.h" + #include "distributed_hardware_errno.h" #include "dhfwk_sa_manager.h" #include "distributed_hardware_fwk_kit.h" @@ -97,6 +103,24 @@ void DistributedHardwareFwkKitTest::TestHDSourceStatusListener::OnDisable( (void)dhDescriptor; } +void DistributedHardwareFwkKitTest::TestDmInitCallback::OnRemoteDied() +{ +} + +void DistributedHardwareFwkKitTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; +} + +void DistributedHardwareFwkKitTest::TestGetDistributedHardwareCallback::OnError( + const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: RegisterPublisherListener_001 * @tc.desc: Verify the RegisterPublisherListener function @@ -437,12 +461,12 @@ HWTEST_F(DistributedHardwareFwkKitTest, GetDistributedHardware_001, testing::ext { ASSERT_TRUE(dhfwkPtr_ != nullptr); std::string networkId = ""; - std::vector descriptors; - int32_t ret = dhfwkPtr_->GetDistributedHardware(networkId, descriptors); + sptr callback(new TestGetDistributedHardwareCallback()); + int32_t ret = dhfwkPtr_->GetDistributedHardware(networkId, callback); EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); networkId = "networkId_test"; - ret = dhfwkPtr_->GetDistributedHardware(networkId, descriptors); + ret = dhfwkPtr_->GetDistributedHardware(networkId, callback); EXPECT_EQ(ERR_DH_FWK_POINTER_IS_NULL, ret); } 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 e814b7d5..36ee7c3a 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 @@ -131,10 +131,10 @@ int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) return DH_FWK_SUCCESS; } -int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) +int32_t GetDistributedHardware(const std::string &networkId, const sptr callback) { (void)networkId; - (void)descriptors; + (void)callback; return DH_FWK_SUCCESS; } 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 3d8a96fc..0a288ac7 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 @@ -25,6 +25,7 @@ #include "idistributed_hardware.h" #include "distributed_hardware_proxy.h" #include "distributed_hardware_stub.h" +#include "getdistributedhardware_callback_stub.h" namespace OHOS { namespace DistributedHardware { @@ -54,7 +55,8 @@ 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 GetDistributedHardware(const std::string &networkId, + const sptr callback); int32_t RegisterDHStatusListener(sptr listener); int32_t UnregisterDHStatusListener(sptr listener); int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener); @@ -70,6 +72,15 @@ public: int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; }; + + class TestGetDistributedHardwareCallback : public GetDistributedHardwareCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors) override; + void OnError(const std::string &networkId, int32_t error) override; + }; }; class MockIPublisherListener : public IPublisherListener { 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 0e79016b..2fb7b1cc 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 @@ -36,10 +36,6 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub2::OnRemoteRequ MessageParcel &reply, MessageOption &option) { if (code == static_cast(DHMsgInterfaceCode::GET_DISTRIBUTED_HARDWARE)) { - std::vector descriptors; - descriptors.emplace_back(DHDescriptor()); - descriptors.emplace_back(DHDescriptor()); - OHOS::DistributedHardware::DistributedHardwareStub::WriteDescriptors(reply, descriptors); return DH_FWK_SUCCESS; } return OHOS::DistributedHardware::DistributedHardwareStub::OnRemoteRequest(code, data, reply, option); @@ -144,10 +140,10 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::StopDistribut } int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) + const std::string &networkId, const sptr callback) { (void)networkId; - (void)descriptors; + (void)callback; return DH_FWK_SUCCESS; } @@ -211,6 +207,20 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::DisableSource return DH_FWK_SUCCESS; } +void DistributedHardwareProxyTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors) +{ + (void)networkId; + (void)descriptors; +} + +void DistributedHardwareProxyTest::TestGetDistributedHardwareCallback::OnError( + const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: RegisterPublisherListener_001 * @tc.desc: Verify the RegisterPublisherListener function @@ -539,11 +549,11 @@ HWTEST_F(DistributedHardwareProxyTest, GetDistributedHardware_001, TestSize.Leve sptr dhStubPtr(new TestDistributedHardwareStub()); ASSERT_TRUE(dhStubPtr != nullptr); DistributedHardwareProxy dhProxy(dhStubPtr); - - std::vector descriptors; - auto ret = dhProxy.GetDistributedHardware(networkId, descriptors); + sptr callback(new TestGetDistributedHardwareCallback()); + ASSERT_TRUE(callback != nullptr); + auto ret = dhProxy.GetDistributedHardware(networkId, callback); EXPECT_EQ(ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL, ret); - ret = dhProxy.GetDistributedHardware(std::string(), descriptors); + ret = dhProxy.GetDistributedHardware(std::string(), callback); EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } @@ -552,9 +562,10 @@ HWTEST_F(DistributedHardwareProxyTest, GetDistributedHardware_002, TestSize.Leve std::string networkId = "123456"; sptr dhStubPtr(new TestDistributedHardwareStub2()); ASSERT_TRUE(dhStubPtr != nullptr); + sptr callback(new TestGetDistributedHardwareCallback()); + ASSERT_TRUE(callback != nullptr); DistributedHardwareProxy dhProxy(dhStubPtr); - std::vector descriptors; - auto ret = dhProxy.GetDistributedHardware(networkId, descriptors); + auto ret = dhProxy.GetDistributedHardware(networkId, callback); EXPECT_EQ(DH_FWK_SUCCESS, ret); } diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 0f8256f9..7e91ebcd 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/getdistributedhardware_callback_proxy.cpp", "src/ipc/hardware_status_listener_proxy.cpp", "src/ipc/publisher_listener_proxy.cpp", "src/localhardwaremanager/local_hardware_manager.cpp", diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h index 827ff5ec..927045ad 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -52,7 +52,8 @@ 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 GetDistributedHardware(const std::string &networkId, + const sptr callback) override; int32_t RegisterDHStatusListener(sptr listener) override; int32_t UnregisterDHStatusListener(sptr listener) override; int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener) override; diff --git a/services/distributedhardwarefwkservice/include/ipc/getdistributedhardware_callback_proxy.h b/services/distributedhardwarefwkservice/include/ipc/getdistributedhardware_callback_proxy.h new file mode 100644 index 00000000..47f9560e --- /dev/null +++ b/services/distributedhardwarefwkservice/include/ipc/getdistributedhardware_callback_proxy.h @@ -0,0 +1,41 @@ +/* + * 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_GETDISTRIBUTEDHARDWARE_CALLBACK_PROXY_H +#define OHOS_GETDISTRIBUTEDHARDWARE_CALLBACK_PROXY_H + +#include "igetdistributedhardware_callback.h" + +#include "iremote_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class GetDistributedGardwareCallbackProxy : public IRemoteProxy { +public: + explicit GetDistributedGardwareCallbackProxy(const sptr object); + virtual ~GetDistributedGardwareCallbackProxy() override; + + virtual void OnSuccess(const std::string &networkId, const std::vector &descriptors) override; + virtual void OnError(const std::string &networkId, int32_t error) override; + +private: + int32_t WriteDescriptors(MessageParcel &data, const std::vector &descriptors); + +private: + static inline BrokerDelegator delegator_; +}; +} // DistributedHardware +} // OHOS +#endif // OHOS_GETDISTRIBUTEDHARDWARE_CALLBACK_PROXY_H \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h index d9bfe80a..4537075e 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h @@ -26,6 +26,7 @@ #include "capability_utils.h" #include "db_adapter.h" #include "event_handler.h" +#include "igetdistributedhardware_callback.h" class DBAdapter; namespace OHOS { @@ -85,6 +86,10 @@ public: std::shared_ptr GetEventHandler(); void DumpCapabilityInfos(std::vector &capInfos); + void AsyncGetDistributedHardware(const std::string &networkId, + const sptr callback); + void DoAsyncGetDistributedHardware(const std::string &networkId, + const sptr callback); private: void HandleCapabilityAddChange(const std::vector &insertRecords); @@ -98,6 +103,8 @@ private: CapabilityInfoMap globalCapInfoMap_; std::shared_ptr eventHandler_; + std::mutex syncDataMutex_; + std::condition_variable syncDataCondVar_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 30d48e10..0cbaa7b0 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -364,40 +364,48 @@ int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const } int32_t DistributedHardwareService::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) + const std::string &networkId, const sptr callback) { if (!IsIdLengthValid(networkId)) { return ERR_DH_FWK_PARA_INVALID; } std::string deviceId; + std::string realNetworkId; if (networkId == LOCAL_NETWORKID_ALIAS) { deviceId = GetLocalDeviceInfo().deviceId; + realNetworkId = GetLocalDeviceInfo().networkId; } 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) { + realNetworkId = networkId; + } + std::vector descriptors; + std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(realNetworkId); + std::string udidHash = Sha256(udid); + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + if (metaCapInfos.empty()) { + std::vector> capabilities; + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); + for (const auto &capabilitie : capabilities) { DHDescriptor descriptor; - descriptor.id = metaCapInfo->GetDHId(); - descriptor.dhType = metaCapInfo->GetDHType(); + descriptor.id = capabilitie->GetDHId(); + descriptor.dhType = capabilitie->GetDHType(); descriptors.push_back(descriptor); } } else { - for (const auto &capabilitie : capabilities) { + for (const auto &metaCapInfo : metaCapInfos) { DHDescriptor descriptor; - descriptor.id = capabilitie->GetDHId(); - descriptor.dhType = capabilitie->GetDHType(); + descriptor.id = metaCapInfo->GetDHId(); + descriptor.dhType = metaCapInfo->GetDHType(); descriptors.push_back(descriptor); } } - + if (descriptors.size()) { + callback->OnSuccess(realNetworkId, descriptors); + DHLOGI("GetDistributedHardware call OnSuccess, networkId: %{public}s.", realNetworkId.c_str()); + } else { + CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(realNetworkId, callback); + } return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index d1466d0f..4674c97a 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -395,13 +395,14 @@ int32_t DistributedHardwareStub::GetDistributedHardwareInner(MessageParcel &data 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; + sptr callback = + iface_cast(data.ReadRemoteObject()); + if (callback == nullptr) { + DHLOGE("Input get distributed hardware callback is null!"); + return ERR_DH_FWK_PARA_INVALID; } + int32_t ret = GetDistributedHardware(networkId, callback); if (!reply.WriteInt32(ret)) { DHLOGE("Write ret code failed!"); return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; diff --git a/services/distributedhardwarefwkservice/src/ipc/getdistributedhardware_callback_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/getdistributedhardware_callback_proxy.cpp new file mode 100644 index 00000000..51cc8dac --- /dev/null +++ b/services/distributedhardwarefwkservice/src/ipc/getdistributedhardware_callback_proxy.cpp @@ -0,0 +1,124 @@ +/* + * 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 "av_trans_errno.h" +#include "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "getdistributedhardware_callback_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +GetDistributedGardwareCallbackProxy::GetDistributedGardwareCallbackProxy(const sptr object) + : IRemoteProxy(object) +{ +} + +GetDistributedGardwareCallbackProxy::~GetDistributedGardwareCallbackProxy() +{ +} + +void GetDistributedGardwareCallbackProxy::OnSuccess(const std::string &networkId, + const std::vector &descriptors) +{ + DHLOGI("GetDistributedGardwareCallbackProxy OnSuccess."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("GetDistributedGardwareCallbackProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("GetDistributedGardwareCallbackProxy write networkId failed!"); + return; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IGetDistributedHardwareCallback::Message::ON_SUCCESS), data, reply, option); + if (ret != 0) { + DHLOGE("GetDistributedGardwareCallbackProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +void GetDistributedGardwareCallbackProxy::OnError(const std::string &networkId, int32_t error) +{ + DHLOGI("GetDistributedGardwareCallbackProxy OnError."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("GetDistributedGardwareCallbackProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("GetDistributedGardwareCallbackProxy write networkId failed!"); + return; + } + if (!data.WriteInt32(error)) { + DHLOGE("GetDistributedGardwareCallbackProxy write error failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IGetDistributedHardwareCallback::Message::ON_ERROR), data, reply, option); + if (ret != 0) { + DHLOGE("GetDistributedGardwareCallbackProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +int32_t GetDistributedGardwareCallbackProxy::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/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index 47edfd74..8d7f9413 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -33,6 +33,8 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "CapabilityInfoManager" +constexpr int32_t SYNC_DATA_TIMEOUT_MS = 1000 * 2; + CapabilityInfoManager::CapabilityInfoManager() : dbAdapterPtr_(nullptr) { DHLOGI("CapabilityInfoManager construction!"); @@ -381,6 +383,9 @@ void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector condition(syncDataMutex_); + syncDataCondVar_.notify_all(); } const auto keyString = capPtr->GetKey(); @@ -421,6 +426,9 @@ void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector condition(syncDataMutex_); + syncDataCondVar_.notify_all(); } std::string enabledDeviceKey = GetCapabilityKey(capPtr->GetDeviceId(), capPtr->GetDHId()); if (TaskBoard::GetInstance().IsEnabledDevice(enabledDeviceKey)) { @@ -465,6 +473,9 @@ void CapabilityInfoManager::HandleCapabilityDeleteChange(const std::vector condition(syncDataMutex_); + syncDataCondVar_.notify_all(); } TaskParam taskParam = { .networkId = networkId, @@ -591,6 +602,49 @@ void CapabilityInfoManager::DumpCapabilityInfos(std::vector &cap } } +void CapabilityInfoManager::AsyncGetDistributedHardware(const std::string &networkId, + const sptr callback) +{ + DHLOGI("AsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); + int32_t waitTimeMill = SYNC_DATA_TIMEOUT_MS; + while (waitTimeMill > 0) { + auto beginTime = std::chrono::steady_clock::now(); + std::unique_lock locker(syncDataMutex_); + syncDataCondVar_.wait_for(locker, std::chrono::milliseconds(waitTimeMill)); + + std::vector descriptors; + std::vector> capabilities; + std::string deviceId = GetDeviceIdByUUID(DHContext::GetInstance().GetUUIDByNetworkId(networkId)); + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); + for (const auto &capabilitie : capabilities) { + DHDescriptor descriptor; + descriptor.id = capabilitie->GetDHId(); + descriptor.dhType = capabilitie->GetDHType(); + descriptors.push_back(descriptor); + } + if (descriptors.size()) { + DHLOGI("AsyncGetDistributedHardware call OnSuccess, networkId: %{public}s.", + GetAnonyString(networkId).c_str()); + callback->OnSuccess(networkId, descriptors); + return; + } + + auto endTime = std::chrono::steady_clock::now(); + auto costTime = std::chrono::duration_cast(endTime - beginTime); + waitTimeMill -= costTime.count(); + DHLOGI("AsyncGetDistributedHardware do retry, networkId: %{public}s.", GetAnonyString(networkId).c_str()); + } + callback->OnError(networkId, ERR_DH_FWK_GETDISTRIBUTEDHARDWARE_TIMEOUT); + DHLOGI("AsyncGetDistributedHardware call OnError, networkId: %{public}s.", GetAnonyString(networkId).c_str()); +} + +void CapabilityInfoManager::DoAsyncGetDistributedHardware(const std::string &networkId, + const sptr callback) +{ + DHLOGI("DoAsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); + std::thread([this, networkId, callback]() { this->AsyncGetDistributedHardware(networkId, callback); }).detach(); +} + std::vector CapabilityInfoManager::GetEntriesByKeys(const std::vector &keys) { if (!IsArrayLengthValid(keys)) { diff --git a/services/distributedhardwarefwkservice/test/fuzztest/distributedfwkservices_fuzzer/distributedfwkservices_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/distributedfwkservices_fuzzer/distributedfwkservices_fuzzer.cpp index c0a7080c..986aace8 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/distributedfwkservices_fuzzer/distributedfwkservices_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/distributedfwkservices_fuzzer/distributedfwkservices_fuzzer.cpp @@ -24,6 +24,7 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_service.h" +#include "getdistributedhardware_callback_stub.h" namespace OHOS { namespace DistributedHardware { @@ -49,6 +50,23 @@ public: } }; +class TestGetDistributedHardwareCallback : public GetDistributedHardwareCallbackStub { +public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; +protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors) override + { + (void)networkId; + (void)descriptors; + } + void OnError(const std::string &networkId, int32_t error) override + { + (void)networkId; + (void)error; + } +}; + void FwkServicesQueryLocalSysSpecFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(uint32_t))) { @@ -127,9 +145,8 @@ void FwkServicesGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size) } DistributedHardwareService service(SAID, true); std::string networkId(reinterpret_cast(data), size); - std::vector descriptors; - - service.GetDistributedHardware(networkId, descriptors); + sptr callback(new TestGetDistributedHardwareCallback()); + service.GetDistributedHardware(networkId, callback); } void FwkServicesRegisterDHStatusListenerFuzzTest(const uint8_t* data, size_t size) diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp index 07b27d02..35a0f55d 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp @@ -294,18 +294,17 @@ HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_001, TestSize.L HWTEST_F(DistributedHardwareServiceTest, GetDistributedHardware_001, TestSize.Level1) { DistributedHardwareService service(ASID, true); - std::vector descriptors; std::string networkId = "111"; - auto ret = service.GetDistributedHardware(networkId, descriptors); + auto ret = service.GetDistributedHardware(networkId, nullptr); EXPECT_EQ(ret, DH_FWK_SUCCESS); networkId = "local"; - ret = service.GetDistributedHardware(networkId, descriptors); + ret = service.GetDistributedHardware(networkId, nullptr); EXPECT_EQ(ret, DH_FWK_SUCCESS); networkId = ""; - ret = service.GetDistributedHardware(networkId, descriptors); + ret = service.GetDistributedHardware(networkId, nullptr); EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } 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 f7a38b77..b1bf8b17 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 @@ -125,10 +125,10 @@ int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) return DH_FWK_SUCCESS; } -int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors) +int32_t GetDistributedHardware(const std::string &networkId, const sptr callback) { (void)networkId; - (void)descriptors; + (void)callback; return DH_FWK_SUCCESS; } @@ -221,6 +221,20 @@ public: (void)dhDescriptor; } }; + +class MockGetDistributedHardwareCallbackStub : public IRemoteStub { +public: + void OnSuccess(const std::string &networkId, const std::vector &descriptors) override + { + (void)networkId; + (void)descriptors; + } + void OnError(const std::string &networkId, int32_t error) override + { + (void)networkId; + (void)error; + } +}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/src/distributed_hardware_stub_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/src/distributed_hardware_stub_test.cpp index 5338ad99..99de88a9 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/src/distributed_hardware_stub_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwarestub/src/distributed_hardware_stub_test.cpp @@ -448,6 +448,8 @@ HWTEST_F(DistributedHardwareStubTest, OnRemoteRequest_028, TestSize.Level1) MessageOption option; data.WriteInterfaceToken(stubTest_->GetDescriptor()); data.WriteString("netWorkId_test"); + sptr callback(new MockGetDistributedHardwareCallbackStub()); + data.WriteRemoteObject(callback); auto ret = stubTest_->OnRemoteRequest(code, data, reply, option); EXPECT_EQ(DH_FWK_SUCCESS, ret); } -- Gitee