diff --git a/common/utils/include/dhardware_descriptor.h b/common/utils/include/dhardware_descriptor.h index 8ead0889c41dc7e320a06cdabc91f2b1a86ed816..7437f16465db3ebd34ec6de2f7608fe6553f53a8 100644 --- a/common/utils/include/dhardware_descriptor.h +++ b/common/utils/include/dhardware_descriptor.h @@ -28,6 +28,13 @@ struct DHDescriptor { std::string id; DHType dhType; }; + +enum class EnableStep : uint32_t { + ENABLE_SOURCE = 1, + DISABLE_SOURCE = 2, + ENABLE_SINK = 3, + DISABLE_SINK = 4 +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DISTRIBUTED_HARDWARE_DESCRIPTOR_H diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 406412c17813967bb87fd339c9989885ea0b232b..bbd6cdf419e8be11dc25eb360546f23d50586f98 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 f219c26882c767cf3397a3640515bb80fcccc38a..e17488b9d7696b128cf587913a2136c18aecbdd9 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 "iget_dh_descriptors_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, EnableStep enableStep, + 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/iget_dh_descriptors_callback.h b/common/utils/include/iget_dh_descriptors_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..5d3fc8be3a5d7a841fd8e01940f5c00a9178d45a --- /dev/null +++ b/common/utils/include/iget_dh_descriptors_callback.h @@ -0,0 +1,43 @@ +/* + * 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_IGET_DH_DESCRIPTORS_CALLBACK_H +#define OHOS_IGET_DH_DESCRIPTORS_CALLBACK_H + +#include +#include + +#include + +#include "dhardware_descriptor.h" + +namespace OHOS { +namespace DistributedHardware { +class IGetDhDescriptorsCallback : public IRemoteBroker { +public: + virtual void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) = 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.IGetDhDescriptorsCallback"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_IGET_DH_DESCRIPTORS_CALLBACK_H \ No newline at end of file diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index a322c46b91beb57ce54ec277d66f3cd78a1c4f10..0b9a495e961243652e5868d3393fce68e9bbe148 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/get_dh_descriptors_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 a531c6807aa5274e2beea8f5baab87aab35a0212..d2744e90ae8325ccaf4618ae051ae412b1ce5771 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 "iget_dh_descriptors_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, EnableStep enableStep, + 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 5f57ee297d7e1c4f7a196025a66805f3dca3f92f..90937e86472e681bc581ab49c035420aac3309d4 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, EnableStep enableStep, + 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/get_dh_descriptors_callback_stub.h b/interfaces/inner_kits/include/ipc/get_dh_descriptors_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..f009f2f427635862883f0f9788f9a8da42651a12 --- /dev/null +++ b/interfaces/inner_kits/include/ipc/get_dh_descriptors_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_GET_DH_DESCRIPTORS_CALLBACK_STUB_H +#define OHOS_GET_DH_DESCRIPTORS_CALLBACK_STUB_H + +#include "iget_dh_descriptors_callback.h" + +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class GetDhDescriptorsCallbackStub : public IRemoteStub { +public: + GetDhDescriptorsCallbackStub(); + virtual ~GetDhDescriptorsCallbackStub() 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(GetDhDescriptorsCallbackStub); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_GET_DH_DESCRIPTORS_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 2ed3502fbfbaef1fd6d5d0e87b8aedc0998a3119..91ea88ca18963ff81e1adff3e1c26a84fde9b169 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -269,18 +269,18 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); } -int32_t DistributedHardwareFwkKit::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) +int32_t DistributedHardwareFwkKit::GetDistributedHardware(const std::string &networkId, EnableStep enableStep, + 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, enableStep, 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 4f89b6b52777b8cc6131057c6e785493c2bc9c8f..47ee352db885aa38588b58f8b7ae2fa6008c8d69 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -531,13 +531,17 @@ int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const s return reply.ReadInt32(); } -int32_t DistributedHardwareProxy::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) +int32_t DistributedHardwareProxy::GetDistributedHardware(const std::string &networkId, EnableStep enableStep, + 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,20 @@ int32_t DistributedHardwareProxy::GetDistributedHardware( DHLOGE("Write networkId failed!"); return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; } + if (!data.WriteUint32(static_cast(enableStep))) { + DHLOGE("Write enableStep 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/get_dh_descriptors_callback_stub.cpp b/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81790ffbde6f2e050f92778c38746b5965e1d348 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp @@ -0,0 +1,77 @@ +/* + * 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 "get_dh_descriptors_callback_stub.h" + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +GetDhDescriptorsCallbackStub::GetDhDescriptorsCallbackStub() +{ +} + +GetDhDescriptorsCallbackStub::~GetDhDescriptorsCallbackStub() +{ +} + +int32_t GetDhDescriptorsCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("GetDhDescriptorsCallbackStub read valid token failed!"); + return ERR_INVALID_DATA; + } + IGetDhDescriptorsCallback::Message msgCode = static_cast(code); + switch (msgCode) { + case IGetDhDescriptorsCallback::Message::ON_SUCCESS: { + std::vector descriptors; + std::string networkId = data.ReadString(); + ReadDescriptors(data, descriptors); + EnableStep enableStep = static_cast(data.ReadUint32()); + OnSuccess(networkId, descriptors, enableStep); + break; + } + case IGetDhDescriptorsCallback::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 GetDhDescriptorsCallbackStub::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.ReadUint32()); + 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 42628ce7af7279f9e3404c98eaeadd55ab8e81b5..a164535c5623c8718b0a2e7a844cc51010b2d250 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,20 @@ void TestHDSourceStatusListener::OnDisable( (void)dhDescriptor; } +void TestGetDistributedHardwareCallback::OnSuccess(const std::string &networkId, + const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +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 +214,11 @@ 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); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + dhfwkKit.GetDistributedHardware(networkId, enableStep, 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 76e62164cec1df2e507ebe6759219e56abfed9c7..fefe64db6581733a60c04f825091c77a1f5df6bc 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 "get_dh_descriptors_callback_stub.h" #include "hardware_status_listener_stub.h" #include "publisher_listener_stub.h" @@ -48,6 +49,16 @@ protected: void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; }; + +class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { +public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; +protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) 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 793ec486ebf0685ce474b11e3652fbb54fc2e7fa..347ad53e734b03c748cb2d95b61ef93b1320358b 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 2e6995ec7ed2dfc776641dc9c3eb90fa071f7485..dd30bdf8aa09102deea99869b169afeaf4b2aeae 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 "get_dh_descriptors_callback_stub.h" #include "hardware_status_listener_stub.h" #include "publisher_listener_stub.h" #include "system_ability_load_callback_stub.h" @@ -69,6 +73,24 @@ 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 GetDhDescriptorsCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) 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 624462491479e831bd393ac81c5306b03bb9285b..eb5994952bd5cde0524a5a2a9c1fedd363c94c99 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,25 @@ void DistributedHardwareFwkKitTest::TestHDSourceStatusListener::OnDisable( (void)dhDescriptor; } +void DistributedHardwareFwkKitTest::TestDmInitCallback::OnRemoteDied() +{ +} + +void DistributedHardwareFwkKitTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +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 +462,13 @@ HWTEST_F(DistributedHardwareFwkKitTest, GetDistributedHardware_001, testing::ext { ASSERT_TRUE(dhfwkPtr_ != nullptr); std::string networkId = ""; - std::vector descriptors; - int32_t ret = dhfwkPtr_->GetDistributedHardware(networkId, descriptors); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback(new TestGetDistributedHardwareCallback()); + int32_t ret = dhfwkPtr_->GetDistributedHardware(networkId, enableStep, callback); EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); networkId = "networkId_test"; - ret = dhfwkPtr_->GetDistributedHardware(networkId, descriptors); + ret = dhfwkPtr_->GetDistributedHardware(networkId, enableStep, 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 e814b7d5058ec0b6162b7229a57d8021d425af41..7639175f2aaac9a861f6d58b847f179a0a0785be 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,12 @@ 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, EnableStep enableStep, + const sptr callback) { (void)networkId; - (void)descriptors; + (void)enableStep; + (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 3d8a96fc8964aee1f583feddf946540b844c20cf..7466ec10807fca29fd650c67832bce93c9e4d6bc 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 "get_dh_descriptors_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, EnableStep enableStep, + 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,16 @@ public: int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; }; + + class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) 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 0e79016bbe37ce80cf042c878713e2380189bd4a..0195307b1453a40e6fef55f90eb412201671e41a 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,11 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::StopDistribut } int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) + const std::string &networkId, EnableStep enableStep, const sptr callback) { (void)networkId; - (void)descriptors; + (void)enableStep; + (void)callback; return DH_FWK_SUCCESS; } @@ -211,6 +208,21 @@ int32_t DistributedHardwareProxyTest::TestDistributedHardwareStub::DisableSource return DH_FWK_SUCCESS; } +void DistributedHardwareProxyTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +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 +551,12 @@ 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()); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + ASSERT_TRUE(callback != nullptr); + auto ret = dhProxy.GetDistributedHardware(networkId, enableStep, callback); EXPECT_EQ(ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL, ret); - ret = dhProxy.GetDistributedHardware(std::string(), descriptors); + ret = dhProxy.GetDistributedHardware(std::string(), enableStep, callback); EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } @@ -552,9 +565,11 @@ HWTEST_F(DistributedHardwareProxyTest, GetDistributedHardware_002, TestSize.Leve std::string networkId = "123456"; sptr dhStubPtr(new TestDistributedHardwareStub2()); ASSERT_TRUE(dhStubPtr != nullptr); + sptr callback(new TestGetDistributedHardwareCallback()); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + ASSERT_TRUE(callback != nullptr); DistributedHardwareProxy dhProxy(dhStubPtr); - std::vector descriptors; - auto ret = dhProxy.GetDistributedHardware(networkId, descriptors); + auto ret = dhProxy.GetDistributedHardware(networkId, enableStep, callback); EXPECT_EQ(DH_FWK_SUCCESS, ret); } diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 0f8256f9ab7d563263440f375509c775d339a98f..fc1776f9147ab259241599c431a40130d4b387a1 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/get_dh_descriptors_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 827ff5ecb12d82de918d867ee4d23b18d96c43d2..c535261565401fe474c15190c31d0aa249706880 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, EnableStep enableStep, + 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/get_dh_descriptors_callback_proxy.h b/services/distributedhardwarefwkservice/include/ipc/get_dh_descriptors_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..7e8835bf6bdef932909c17c0fcb071abb3e90aab --- /dev/null +++ b/services/distributedhardwarefwkservice/include/ipc/get_dh_descriptors_callback_proxy.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_GET_DH_DESCRIPTORS_CALLBACK_PROXY_H +#define OHOS_GET_DH_DESCRIPTORS_CALLBACK_PROXY_H + +#include "iget_dh_descriptors_callback.h" + +#include "iremote_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class GetDhDescriptorsCallbackProxy : public IRemoteProxy { +public: + explicit GetDhDescriptorsCallbackProxy(const sptr object); + virtual ~GetDhDescriptorsCallbackProxy() override; + + virtual void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) 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_GET_DH_DESCRIPTORS_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 d9bfe80ab011e368cf438ee60a4548d4b9da02e8..4979c5f0430f29116713dc6750db8c8cff309c33 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 "iget_dh_descriptors_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, EnableStep enableStep, + const sptr callback); + void DoAsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, + 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 30d48e103e16dfaed5fe4c50722b0c50c5cb32bb..bc63bfd32eb30daf8b48eaecc4f66acac5539bc6 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -42,6 +42,7 @@ #include "publisher.h" #include "task_executor.h" #include "task_factory.h" +#include "version_info_manager.h" namespace OHOS { namespace DistributedHardware { @@ -363,41 +364,54 @@ int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const return DH_FWK_SUCCESS; } -int32_t DistributedHardwareService::GetDistributedHardware( - const std::string &networkId, std::vector &descriptors) +int32_t DistributedHardwareService::GetDistributedHardware(const std::string &networkId, EnableStep enableStep, + 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); + 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()) { for (const auto &metaCapInfo : metaCapInfos) { DHDescriptor descriptor; descriptor.id = metaCapInfo->GetDHId(); descriptor.dhType = metaCapInfo->GetDHType(); descriptors.push_back(descriptor); } - } else { + DHLOGI("Get MetacapInfo Success, networkId: %{public}s.", realNetworkId.c_str()); + callback->OnSuccess(realNetworkId, descriptors, enableStep); + return DH_FWK_SUCCESS; + } + + std::vector> capabilities; + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); + if (!capabilities.empty()) { for (const auto &capabilitie : capabilities) { DHDescriptor descriptor; descriptor.id = capabilitie->GetDHId(); descriptor.dhType = capabilitie->GetDHType(); descriptors.push_back(descriptor); } + DHLOGI("Get CapabilitieInfo Success, deviceId: %{public}s.", deviceId.c_str()); + callback->OnSuccess(realNetworkId, descriptors, enableStep); + return DH_FWK_SUCCESS; } - + VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); + CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(realNetworkId, enableStep, callback); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index d1466d0f1c5245c4931996734623fa35b1e70878..ae7d472cf32c3ef6c3690ca11ca72b0b0553e312 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -395,13 +395,15 @@ 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; + EnableStep enableStep = static_cast(data.ReadUint32()); + 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, enableStep, 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/get_dh_descriptors_callback_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..637706dcc6330e7fdfc3368250d1d02bb0453ae0 --- /dev/null +++ b/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp @@ -0,0 +1,129 @@ +/* + * 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 "get_dh_descriptors_callback_proxy.h" + +#include "av_trans_errno.h" +#include "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +GetDhDescriptorsCallbackProxy::GetDhDescriptorsCallbackProxy(const sptr object) + : IRemoteProxy(object) +{ +} + +GetDhDescriptorsCallbackProxy::~GetDhDescriptorsCallbackProxy() +{ +} + +void GetDhDescriptorsCallbackProxy::OnSuccess(const std::string &networkId, + const std::vector &descriptors, EnableStep enableStep) +{ + DHLOGI("GetDhDescriptorsCallbackProxy OnSuccess."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("GetDhDescriptorsCallbackProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("GetDhDescriptorsCallbackProxy write networkId failed!"); + return; + } + if (WriteDescriptors(data, descriptors)) { + DHLOGE("WriteDescriptors failed!"); + return; + } + if (!data.WriteUint32(static_cast(enableStep))) { + DHLOGE("GetDhDescriptorsCallbackProxy write enableStep failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IGetDhDescriptorsCallback::Message::ON_SUCCESS), data, reply, option); + if (ret != 0) { + DHLOGE("GetDhDescriptorsCallbackProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +void GetDhDescriptorsCallbackProxy::OnError(const std::string &networkId, int32_t error) +{ + DHLOGI("GetDhDescriptorsCallbackProxy OnError."); + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("Get Remote IRemoteObject failed!"); + return; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("GetDhDescriptorsCallbackProxy write token failed!"); + return; + } + if (!data.WriteString(networkId)) { + DHLOGE("GetDhDescriptorsCallbackProxy write networkId failed!"); + return; + } + if (!data.WriteInt32(error)) { + DHLOGE("GetDhDescriptorsCallbackProxy write error failed!"); + return; + } + int32_t ret = remote->SendRequest( + static_cast(IGetDhDescriptorsCallback::Message::ON_ERROR), data, reply, option); + if (ret != 0) { + DHLOGE("GetDhDescriptorsCallbackProxy send requeset failed, ret: %{public}d!", ret); + return; + } +} + +int32_t GetDhDescriptorsCallbackProxy::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); + DHType type = static_cast(descriptor.dhType); + if (!data.WriteUint32(static_cast(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 47edfd741f8bf9db188012394a6717ab073684e7..6dc0ef0b4b0a249d5426c7a27cd743f18d351762 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 * 10; + CapabilityInfoManager::CapabilityInfoManager() : dbAdapterPtr_(nullptr) { DHLOGI("CapabilityInfoManager construction!"); @@ -395,6 +397,9 @@ void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector condition(syncDataMutex_); + syncDataCondVar_.notify_all(); } void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector &updateRecords) @@ -439,6 +444,9 @@ void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector condition(syncDataMutex_); + syncDataCondVar_.notify_all(); } void CapabilityInfoManager::HandleCapabilityDeleteChange(const std::vector &deleteRecords) @@ -591,6 +599,51 @@ void CapabilityInfoManager::DumpCapabilityInfos(std::vector &cap } } +void CapabilityInfoManager::AsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, + 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, enableStep); + 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, EnableStep enableStep, + const sptr callback) +{ + DHLOGI("DoAsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); + std::thread([this, networkId, enableStep, callback]() { + this->AsyncGetDistributedHardware(networkId, enableStep, 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 c0a7080c67bec71965f867ec3786aa4cf513574d..5f3ab1bb2f00886644e88114ce289ea383a7c0aa 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 "get_dh_descriptors_callback_stub.h" namespace OHOS { namespace DistributedHardware { @@ -49,6 +50,25 @@ public: } }; +class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { +public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; +protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override + { + (void)networkId; + (void)descriptors; + (void)enableStep; + } + 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 +147,9 @@ 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()); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + service.GetDistributedHardware(networkId, enableStep, 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 07b27d021b4e202b74879b55b960b11e2066001b..1e67bb281ac18fca0512b62e6547fb4ea08f4b29 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,18 @@ 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); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + auto ret = service.GetDistributedHardware(networkId, enableStep, nullptr); EXPECT_EQ(ret, DH_FWK_SUCCESS); networkId = "local"; - ret = service.GetDistributedHardware(networkId, descriptors); + ret = service.GetDistributedHardware(networkId, enableStep, nullptr); EXPECT_EQ(ret, DH_FWK_SUCCESS); networkId = ""; - ret = service.GetDistributedHardware(networkId, descriptors); + ret = service.GetDistributedHardware(networkId, enableStep, 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 f7a38b77a106a304dfcfb0a3648465bd631e7c86..9e6d4975c98a8056493d3fe57d9a276b27a8c3b7 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,12 @@ 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, EnableStep enableStep, + const sptr callback) { (void)networkId; - (void)descriptors; + (void)enableStep; + (void)callback; return DH_FWK_SUCCESS; } @@ -221,6 +223,22 @@ public: (void)dhDescriptor; } }; + +class MockGetDhDescriptorsCallbackStub : public IRemoteStub { +public: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override + { + (void)networkId; + (void)descriptors; + (void)enableStep; + } + 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 5338ad995db3cc84a54ebdae9da920eb5de76c3a..4e39062c4e9facce6a05a66399b46b88e41665e4 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,10 @@ HWTEST_F(DistributedHardwareStubTest, OnRemoteRequest_028, TestSize.Level1) MessageOption option; data.WriteInterfaceToken(stubTest_->GetDescriptor()); data.WriteString("netWorkId_test"); + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + data.WriteUint32(static_cast(enableStep)); + sptr callback(new MockGetDhDescriptorsCallbackStub()); + data.WriteRemoteObject(callback); auto ret = stubTest_->OnRemoteRequest(code, data, reply, option); EXPECT_EQ(DH_FWK_SUCCESS, ret); }