From 7b1ee48bfbec3e651eb3b2b2517af5621b1b9c34 Mon Sep 17 00:00:00 2001 From: chen0088 Date: Tue, 19 Aug 2025 17:38:42 +0800 Subject: [PATCH] add sink status Signed-off-by: chen0088 --- .../include/callback/dcamera_sink_callback.h | 9 ++- .../callback/dcamera_sink_callback_stub.h | 3 +- .../include/callback/idcamera_sink_callback.h | 4 +- .../include/dcamera_sink_handler.h | 5 +- .../src/callback/dcamera_sink_callback.cpp | 39 ++++++++- .../callback/dcamera_sink_callback_stub.cpp | 16 +++- .../camera_sink/src/dcamera_sink_handler.cpp | 15 +++- .../native_cpp/test/sinkfuzztest/BUILD.gn | 3 +- .../BUILD.gn | 75 +++++++++++++++++ ...sinkonhardwarestatechangedinner_fuzzer.cpp | 60 ++++++++++++++ ...cksinkonhardwarestatechangedinner_fuzzer.h | 21 +++++ .../corpus/init | 14 ++++ .../project.xml | 25 ++++++ .../dcamera_sink_callback_stub_test.cpp | 51 +++++++++++- .../callback/dcamera_sink_callback_test.cpp | 59 +++++++++++++- .../dcamera_sink_callback_proxy.h | 6 +- .../dcamera_sink_controller.h | 6 ++ .../dcamera_sink_callback_proxy.cpp | 39 ++++++++- .../dcamera_sink_controller.cpp | 13 ++- .../common/distributedcamera/BUILD.gn | 2 + .../dcamera_sink_callback_proxy_test.cpp | 80 ++++++++++++++++++- .../distributed_camera_allconnect_manager.cpp | 2 +- 22 files changed, 526 insertions(+), 21 deletions(-) create mode 100644 interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/BUILD.gn create mode 100644 interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.cpp create mode 100644 interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.h create mode 100644 interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/corpus/init create mode 100644 interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/project.xml diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback.h b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback.h index 5d44e712..4d4c6f20 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback.h +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -31,11 +31,18 @@ public: int32_t OnNotifyResourceInfo(const ResourceEventType &type, const std::string &subtype, const std::string &networkId, bool &isSensitive, bool &isSameAccout) override; + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) override; + void PushPrivacyResCallback(const std::shared_ptr &listener); + void RegisterStateListener(const std::shared_ptr listener); + void UnRegisterStateListener(); private: std::mutex privacyResMutex_; std::vector> privacyResCallback_; + std::shared_ptr stateListener_ = nullptr; + std::mutex stateListenerMtx_; + const size_t DID_MAX_SIZE = 256; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback_stub.h b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback_stub.h index 576b624b..55a73900 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback_stub.h +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/dcamera_sink_callback_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -34,6 +34,7 @@ public: private: int32_t OnNotifyResourceInfoInner(MessageParcel &data, MessageParcel &reply); + int32_t OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply); using DCameraFunc = int32_t (DCameraSinkCallbackStub::*)(MessageParcel &data, MessageParcel &reply); std::map memberFuncMap_; diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/idcamera_sink_callback.h b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/idcamera_sink_callback.h index 45e8754d..1ec28b97 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/include/callback/idcamera_sink_callback.h +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/callback/idcamera_sink_callback.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,11 +26,13 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.dcamerasinkcallback"); enum { NOTIFY_RESOURCEINFO = 0, + NOTIFY_STATE_CHANGEINFO = 1, }; virtual ~IDCameraSinkCallback() {} virtual int32_t OnNotifyResourceInfo(const ResourceEventType &type, const std::string &subtype, const std::string &networkId, bool &isSensitive, bool &isSameAccout) = 0; + virtual int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h index 3b143205..511bd754 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h +++ b/interfaces/inner_kits/native_cpp/camera_sink/include/dcamera_sink_handler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -37,6 +37,9 @@ public: int32_t PauseDistributedHardware(const std::string &networkId) override; int32_t ResumeDistributedHardware(const std::string &networkId) override; int32_t StopDistributedHardware(const std::string &networkId) override; + void RegisterDistributedHardwareSinkStateListener( + std::shared_ptr listener) override; + void UnregisterDistributedHardwareSinkStateListener() override; void SetSAState(); private: typedef enum { diff --git a/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback.cpp b/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback.cpp index 39b05747..082bccd6 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback.cpp +++ b/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -43,11 +43,48 @@ int32_t DCameraSinkCallback::OnNotifyResourceInfo(const ResourceEventType &type, return ret; } +int32_t DCameraSinkCallback::OnHardwareStateChanged(const std::string &devId, const std::string &dhId, + int32_t status) +{ + DHLOGI("On hardware state changed, devId: %{public}s, dhId: %{public}s, status: %{public}d", + GetAnonyString(devId).c_str(), dhId.c_str(), status); + std::lock_guard stateLck(stateListenerMtx_); + if (stateListener_ == nullptr) { + DHLOGE("State listener is null."); + return DCAMERA_BAD_VALUE; + } + if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) { + DHLOGE("devId or dhId is invalid"); + return DCAMERA_BAD_VALUE; + } + if (status < 0) { + DHLOGE("status in invalid."); + return DCAMERA_BAD_VALUE; + } + BusinessSinkState currentState = static_cast(status); + stateListener_->OnStateChanged(devId, dhId, currentState); + return DCAMERA_OK; +} + void DCameraSinkCallback::PushPrivacyResCallback(const std::shared_ptr &listener) { DHLOGI("push resource info callback."); std::lock_guard lock(privacyResMutex_); privacyResCallback_.push_back(listener); } + +void DCameraSinkCallback::RegisterStateListener(const std::shared_ptr listener) +{ + DHLOGD("Register sink state listener."); + std::lock_guard stateLck(stateListenerMtx_); + stateListener_ = listener; +} + +void DCameraSinkCallback::UnRegisterStateListener() +{ + DHLOGD("UnRegister sink state listener."); + std::lock_guard stateLck(stateListenerMtx_); + stateListener_ = nullptr; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback_stub.cpp b/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback_stub.cpp index 2df1db9f..e31cf03d 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback_stub.cpp +++ b/interfaces/inner_kits/native_cpp/camera_sink/src/callback/dcamera_sink_callback_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,6 +26,7 @@ namespace DistributedHardware { DCameraSinkCallbackStub::DCameraSinkCallbackStub() : IRemoteStub(true) { memberFuncMap_[NOTIFY_RESOURCEINFO] = &DCameraSinkCallbackStub::OnNotifyResourceInfoInner; + memberFuncMap_[NOTIFY_STATE_CHANGEINFO] = &DCameraSinkCallbackStub::OnHardwareStateChangedInner; } DCameraSinkCallbackStub::~DCameraSinkCallbackStub() @@ -45,6 +46,8 @@ int32_t DCameraSinkCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &d switch (code) { case NOTIFY_RESOURCEINFO: return OnNotifyResourceInfoInner(data, reply); + case NOTIFY_STATE_CHANGEINFO: + return OnHardwareStateChangedInner(data, reply); default: DHLOGE("Invalid OnRemoteRequest code=%{public}d", code); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -69,5 +72,16 @@ int32_t DCameraSinkCallbackStub::OnNotifyResourceInfoInner(MessageParcel &data, reply.WriteBool(isSameAccout); return DCAMERA_OK; } + +int32_t DCameraSinkCallbackStub::OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply) +{ + DHLOGI("DCameraSinkCallbackStub OnHardwareStateChangedInner"); + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + int32_t status = data.ReadInt32(); + int32_t ret = OnHardwareStateChanged(devId, dhId, status); + reply.WriteInt32(ret); + return DCAMERA_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp index 0d2c6ba8..6055a07a 100644 --- a/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp +++ b/interfaces/inner_kits/native_cpp/camera_sink/src/dcamera_sink_handler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -195,6 +195,19 @@ int32_t DCameraSinkHandler::StopDistributedHardware(const std::string &networkId return dCameraSinkSrv->StopDistributedHardware(networkId); } +void DCameraSinkHandler::RegisterDistributedHardwareSinkStateListener( + std::shared_ptr listener) +{ + CHECK_AND_RETURN_LOG(dCameraSinkCallback_ == nullptr, "%{public}s", "ipc sink callback is null."); + dCameraSinkCallback_->RegisterStateListener(listener); +} + +void DCameraSinkHandler::UnregisterDistributedHardwareSinkStateListener() +{ + CHECK_AND_RETURN_LOG(dCameraSinkCallback_ == nullptr, "%{public}s", "ipc sink callback is null."); + dCameraSinkCallback_->UnRegisterStateListener(); +} + void DCameraSinkHandler::SetSAState() { std::unique_lock lock(producerMutex_); diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/BUILD.gn index ffaf0139..8de920c9 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2025 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -16,6 +16,7 @@ group("fuzztest") { deps = [ "callbackonnotifyresourceinfo_fuzzer:fuzztest", + "callbacksinkonhardwarestatechangedinner_fuzzer:fuzztest", "callbacksinkonremoterequest_fuzzer:fuzztest", "encodeoninputbufferavailable_fuzzer:fuzztest", "encodeonoutputbufferavailable_fuzzer:fuzztest", diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/BUILD.gn new file mode 100644 index 00000000..10d1cf6a --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/BUILD.gn @@ -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. + +#####################hydra-fuzz################### +base_root = "../../../../../../../../../" +import("//build/config/features.gni") +import("//build/test.gni") +import( + "$base_root/foundation/distributedhardware/distributed_camera/distributedcamera.gni") + +##############################fuzztest########################################## +ohos_fuzztest("CallbackSinkOnHardwareStateChangedInnerFuzzTest") { + module_out_path = "${fuzz_test_output_path}/callbacksinkonhardwarestatechangedinner" + + fuzz_config_file = "${innerkits_path}/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer" + + include_dirs = [ "${innerkits_path}/native_cpp/camera_sink/include/callback" ] + + include_dirs += [ + "include", + "${common_path}/include", + "${common_path}/include/constants", + "${innerkits_path}/native_cpp/camera_sink/include", + "${innerkits_path}/native_cpp/camera_sink/include/callback", + "${innerkits_path}/native_cpp/test/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + + sources = [ "callbacksinkonhardwarestatechangedinner_fuzzer.cpp" ] + + deps = [ + "${common_path}:distributed_camera_utils", + "${innerkits_path}/native_cpp/camera_sink:distributed_camera_sink_sdk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"CallbackSinkOnHardwareStateChangedInnerFuzzTest\"", + "LOG_DOMAIN=0xD004150", + ] + + external_deps = [ + "c_utils:utils", + "distributed_hardware_fwk:distributedhardwareutils", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":CallbackSinkOnHardwareStateChangedInnerFuzzTest" ] +} +############################################################################### diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.cpp new file mode 100644 index 00000000..1b717545 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.cpp @@ -0,0 +1,60 @@ +/* + * 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 "callbacksinkonhardwarestatechangedinner_fuzzer.h" +#include + +#include "dcamera_sink_callback.h" +#include "dcamera_sink_callback_stub.h" +#include "distributed_camera_constants.h" +#include "iremote_object.h" +#include "message_option.h" +#include "message_parcel.h" + +namespace OHOS { +namespace DistributedHardware { +void CallbackSinkOnHardwareStateChangedInnerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + MessageParcel pdata; + MessageParcel reply; + int32_t tempStrLen = 32; + int32_t statusLen = 4; + FuzzedDataProvider fdp(data, size); + std::string devId = fdp.ConsumeRandomLengthString(tempStrLen); + std::string dhId = fdp.ConsumeRandomLengthString(tempStrLen); + int32_t status = fdp.ConsumeIntegral() % statusLen; + pdata.WriteInt32(status); + pdata.WriteString(devId); + pdata.WriteString(dhId); + + sptr dcameraSinkCallback(new (std::nothrow) DCameraSinkCallback()); + if (dcameraSinkCallback == nullptr) { + return; + } + dcameraSinkCallback->OnHardwareStateChangedInner(pdata, reply); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::CallbackSinkOnHardwareStateChangedInnerFuzzTest(data, size); + return 0; +} diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.h b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.h new file mode 100644 index 00000000..d5c545cc --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/callbacksinkonhardwarestatechangedinner_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 CALLBACKSINKONHARDWARESTATECHANGEDINNER_FUZZER_H +#define CALLBACKSINKONHARDWARESTATECHANGEDINNER_FUZZER_H + +#define FUZZ_PROJECT_NAME "callbacksinkonhardwarestatechangedinner_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/corpus/init b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/corpus/init new file mode 100644 index 00000000..65af8ee8 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/project.xml b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/project.xml new file mode 100644 index 00000000..66e1dcac --- /dev/null +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/callbacksinkonhardwarestatechangedinner_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_stub_test.cpp b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_stub_test.cpp index 6a955c8b..c5ed7f6f 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_stub_test.cpp +++ b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_stub_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -73,20 +73,63 @@ HWTEST_F(DCameraSinkCallbackStubTest, OnRemoteRequest_001, TestSize.Level1) EXPECT_EQ(false, sinkCallbackStub_ == nullptr); int32_t ret = DCAMERA_OK; - int32_t UNKNOW_TRANS_ERR = 305; uint32_t code = 0; MessageParcel data; MessageParcel reply; MessageOption option; - code = 1; data.WriteInterfaceToken(DCameraSinkCallbackStub::GetDescriptor()); ret = sinkCallbackStub_->OnRemoteRequest(code, data, reply, option); - EXPECT_EQ(UNKNOW_TRANS_ERR, ret); + EXPECT_EQ(DCAMERA_OK, ret); data.WriteInterfaceToken(u""); ret = sinkCallbackStub_->OnRemoteRequest(code, data, reply, option); EXPECT_EQ(ERR_INVALID_DATA, ret); } + +/** + * @tc.name: OnRemoteRequest_002 + * @tc.desc: Verify the OnHardwareStateChanged function. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DCameraSinkCallbackStubTest, OnRemoteRequest_002, TestSize.Level1) +{ + DHLOGI("DCameraSinkCallbackStubTest OnRemoteRequest_002."); + EXPECT_EQ(false, sinkCallbackStub_ == nullptr); + + int32_t ret = DCAMERA_OK; + uint32_t code = 1; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(DCameraSinkCallbackStub::GetDescriptor()); + ret = sinkCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(DCAMERA_OK, ret); +} + +/** + * @tc.name: OnRemoteRequest_003 + * @tc.desc: Verify the OnRemoteRequest function. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DCameraSinkCallbackStubTest, OnRemoteRequest_003, TestSize.Level1) +{ + DHLOGI("DCameraSinkCallbackStubTest OnRemoteRequest_003."); + EXPECT_EQ(false, sinkCallbackStub_ == nullptr); + + int32_t ret = DCAMERA_OK; + int32_t UNKNOW_TRANS_ERR = 305; + uint32_t code = 2; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + data.WriteInterfaceToken(DCameraSinkCallbackStub::GetDescriptor()); + ret = sinkCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(UNKNOW_TRANS_ERR, ret); +} } } \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_test.cpp b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_test.cpp index fd73fb22..3424f79e 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_test.cpp +++ b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_sink/callback/dcamera_sink_callback_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -25,6 +25,9 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { +namespace { +constexpr uint32_t DID_MAX_SIZE = 256; +} class DCameraSinkCallbackTest : public testing::Test { public: static void SetUpTestCase(void); @@ -47,6 +50,17 @@ public: } }; +class MockDistributedHardwareSinkStateListener : public DistributedHardwareSinkStateListener { +public: + MockDistributedHardwareSinkStateListener() = default; + virtual ~MockDistributedHardwareSinkStateListener() = default; + + void OnStateChanged(const std::string &uuid, const std::string &dhId, const BusinessSinkState state) override + { + DHLOGI("Handle state changed event"); + } +}; + void DCameraSinkCallbackTest::SetUpTestCase(void) { DHLOGI("enter"); @@ -93,5 +107,48 @@ HWTEST_F(DCameraSinkCallbackTest, dcamera_sink_callback_test_001, TestSize.Level ret = sinkCallback_->OnNotifyResourceInfo(type, subType, networkId, isSensitive, isSameAccout); EXPECT_EQ(DCAMERA_OK, ret); } + +/** + * @tc.name: dcamera_sink_callback_test_002 + * @tc.desc: Verify the OnNotifyRegResult function. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DCameraSinkCallbackTest, dcamera_sink_callback_test_002, TestSize.Level1) +{ + DHLOGI("DCameraSinkCallbackTest dcamera_sink_callback_test_002."); + ASSERT_NE(sinkCallback_, nullptr); + + std::string devId = "devId"; + std::string dhId = "dhId"; + int32_t status = 1; + int32_t ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + + std::shared_ptr listener = + std::make_shared(); + sinkCallback_->RegisterStateListener(listener); + devId = ""; + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + devId += std::string(DID_MAX_SIZE + 1, 'a'); + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + devId = "devId"; + dhId = ""; + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + dhId += std::string(DID_MAX_SIZE + 1, 'a'); + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + dhId = "dhId"; + status = -1; + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + EXPECT_EQ(DCAMERA_BAD_VALUE, ret); + status = 1; + ret = sinkCallback_->OnHardwareStateChanged(devId, dhId, status); + sinkCallback_->UnRegisterStateListener(); + EXPECT_EQ(DCAMERA_OK, ret); +} } } \ No newline at end of file diff --git a/services/cameraservice/sinkservice/include/distributedcamera/dcamera_sink_callback_proxy.h b/services/cameraservice/sinkservice/include/distributedcamera/dcamera_sink_callback_proxy.h index ebf3df15..070a4c64 100644 --- a/services/cameraservice/sinkservice/include/distributedcamera/dcamera_sink_callback_proxy.h +++ b/services/cameraservice/sinkservice/include/distributedcamera/dcamera_sink_callback_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -34,8 +34,12 @@ public: int32_t OnNotifyResourceInfo(const ResourceEventType &type, const std::string &subtype, const std::string &networkId, bool &isSensitive, bool &isSameAccout) override; + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) override; private: + bool CheckParams(const std::string& devId, const std::string& dhId, int32_t status); + static inline BrokerDelegator delegator_; + const size_t DID_MAX_SIZE = 256; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_controller.h b/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_controller.h index c43050c3..d66e2396 100644 --- a/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_controller.h +++ b/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_controller.h @@ -34,6 +34,12 @@ namespace OHOS { namespace DistributedHardware { constexpr uint32_t EVENT_FRAME_TRIGGER = 1; constexpr uint32_t EVENT_AUTHORIZATION = 2; +enum DcameraBusinessState : int32_t { + UNKNOWN, + IDLE, + RUNNING, + PAUSING +}; class DCameraSinkController : public ICameraController, public std::enable_shared_from_this { public: explicit DCameraSinkController(std::shared_ptr& accessControl, diff --git a/services/cameraservice/sinkservice/src/distributedcamera/dcamera_sink_callback_proxy.cpp b/services/cameraservice/sinkservice/src/distributedcamera/dcamera_sink_callback_proxy.cpp index 28570433..492e0ab2 100644 --- a/services/cameraservice/sinkservice/src/distributedcamera/dcamera_sink_callback_proxy.cpp +++ b/services/cameraservice/sinkservice/src/distributedcamera/dcamera_sink_callback_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -50,5 +50,42 @@ int32_t DCameraSinkCallbackProxy::OnNotifyResourceInfo(const ResourceEventType & isSameAccout = reply.ReadBool(); return result; } + +int32_t DCameraSinkCallbackProxy::OnHardwareStateChanged(const std::string &devId, + const std::string &dhId, int32_t status) +{ + if (!CheckParams(devId, dhId, status)) { + DHLOGE("input is invalid"); + return DCAMERA_BAD_VALUE; + } + sptr remote = Remote(); + CHECK_AND_RETURN_RET_LOG(remote == nullptr, DCAMERA_BAD_VALUE, "DCameraSinkCallbackProxy remote service null."); + + MessageParcel req; + MessageParcel reply; + MessageOption option; + if (!req.WriteInterfaceToken(DCameraSinkCallbackProxy::GetDescriptor())) { + DHLOGE("write token failed"); + return DCAMERA_BAD_VALUE; + } + if (!req.WriteString(devId) || !req.WriteString(dhId) || !req.WriteInt32(status)) { + return DCAMERA_BAD_VALUE; + } + remote->SendRequest(NOTIFY_STATE_CHANGEINFO, req, reply, option); + return reply.ReadInt32(); +} + +bool DCameraSinkCallbackProxy::CheckParams(const std::string& devId, const std::string& dhId, int32_t status) +{ + if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) { + DHLOGE("devId or dhId is invalid"); + return false; + } + if (status < 0) { + DHLOGE("status in invalid."); + return false; + } + return true; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp index 3da5adf7..b94c8414 100644 --- a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp +++ b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp @@ -526,24 +526,29 @@ void DCameraSinkController::OnSessionState(int32_t state, std::string networkId) DHLOGI("DCameraSinkController::OnSessionState dhId: %{public}s, state: %{public}d", GetAnonyString(dhId_).c_str(), state); sessionState_ = state; + std::string sinkDevId; + int32_t val = GetLocalDeviceNetworkId(sinkDevId); + CHECK_AND_LOG(val != DCAMERA_OK, "GetLocalDeviceNetworkId failed, val: %{public}d", val); switch (state) { case DCAMERA_CHANNEL_STATE_CONNECTING: DHLOGI("channel is connecting"); break; case DCAMERA_CHANNEL_STATE_CONNECTED: { DHLOGI("channel is connected"); + if (sinkCallback_ != nullptr) { + sinkCallback_->OnHardwareStateChanged(sinkDevId, dhId_, DcameraBusinessState::RUNNING); + } if (!ManageSelectChannel::GetInstance().GetSinkConnect()) { break; } srcDevId_ = networkId; - if (operator_ == nullptr) { - DHLOGE("operator_ is nullptr"); - break; - } break; } case DCAMERA_CHANNEL_STATE_DISCONNECTED: DHLOGI("channel is disconnected"); + if (sinkCallback_ != nullptr) { + sinkCallback_->OnHardwareStateChanged(sinkDevId, dhId_, DcameraBusinessState::IDLE); + } ffrt::submit([this]() { DHLOGI("DCameraSinkController::OnSessionState %{public}s new thread session state: %{public}d", GetAnonyString(dhId_).c_str(), sessionState_); diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/BUILD.gn b/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/BUILD.gn index ef828170..cc418cb4 100644 --- a/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/BUILD.gn +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/BUILD.gn @@ -55,6 +55,8 @@ ohos_unittest("DistributedCameraSinkServiceTest") { configs = [ ":module_private_config" ] + cflags = [ "-Dprivate=public" ] + deps = [ "${common_path}:distributed_camera_utils", "${innerkits_path}/native_cpp/camera_sink:distributed_camera_sink_sdk", diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/dcamera_sink_callback_proxy_test.cpp b/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/dcamera_sink_callback_proxy_test.cpp index 6348a813..c1148459 100644 --- a/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/dcamera_sink_callback_proxy_test.cpp +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcamera/dcamera_sink_callback_proxy_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -34,8 +34,17 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + + std::string GenerateString(size_t length) + { + return std::string(length, 'a'); + } }; +namespace { +const size_t DID_MAX_SIZE = 256; +} + void DcameraSinkCallbackProxyTest::SetUpTestCase(void) { DHLOGI("DcameraSinkCallbackProxyTest::SetUpTestCase"); @@ -94,6 +103,75 @@ HWTEST_F(DcameraSinkCallbackProxyTest, dcamera_sink_callback_proxy_test_002, Tes bool isSameAccout = false; ret = callbackProxy->OnNotifyResourceInfo(type, subtype, newworkId, isSensitive, isSameAccout); EXPECT_EQ(DCAMERA_OK, ret); + remoteObject = nullptr; + callbackProxy = nullptr; +} + +/** + * @tc.name: dcamera_sink_callback_proxy_test_003 + * @tc.desc: Verify the OnHardwareStateChanged function. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DcameraSinkCallbackProxyTest, dcamera_sink_callback_proxy_test_003, TestSize.Level1) +{ + DHLOGI("DcameraSinkCallbackProxyTest::dcamera_sink_callback_proxy_test_003"); + sptr remoteObject = sptr(new DCameraSinkCallback()); + sptr callbackProxy(new DCameraSinkCallbackProxy(remoteObject)); + EXPECT_EQ(false, callbackProxy == nullptr); + + int32_t ret = DCAMERA_BAD_VALUE; + std::string devId = "test_dev_id"; + std::string dhId = "test_dh_id"; + int32_t status = 1; + ret = callbackProxy->OnHardwareStateChanged(devId, dhId, status); + EXPECT_NE(DCAMERA_OK, ret); + remoteObject = nullptr; + callbackProxy = nullptr; +} + +/** + * @tc.name: dcamera_sink_callback_proxy_test_004 + * @tc.desc: Verify the CheckParams function. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(DcameraSinkCallbackProxyTest, dcamera_sink_callback_proxy_test_004, TestSize.Level1) +{ + DHLOGI("DcameraSinkCallbackProxyTest::dcamera_sink_callback_proxy_test_004"); + sptr remoteObject = sptr(new DCameraSinkCallback()); + sptr callbackProxy(new DCameraSinkCallbackProxy(remoteObject)); + EXPECT_EQ(false, callbackProxy == nullptr); + + std::string devId = ""; + std::string dhId = "valid_dh_id"; + int32_t status = 0; + bool result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_FALSE(result); + + devId = GenerateString(DID_MAX_SIZE + 1); + result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_FALSE(result); + + devId = "valid_dev_id"; + dhId = ""; + result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_FALSE(result); + + dhId = GenerateString(DID_MAX_SIZE + 1); + result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_FALSE(result); + + dhId = "valid_dh_id"; + status = -1; + result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_FALSE(result); + + status = 0; + result = callbackProxy->CheckParams(devId, dhId, status); + EXPECT_TRUE(result); + remoteObject = nullptr; + callbackProxy = nullptr; } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/channel/src/allconnect/distributed_camera_allconnect_manager.cpp b/services/channel/src/allconnect/distributed_camera_allconnect_manager.cpp index 89b429dc..db82de29 100644 --- a/services/channel/src/allconnect/distributed_camera_allconnect_manager.cpp +++ b/services/channel/src/allconnect/distributed_camera_allconnect_manager.cpp @@ -229,7 +229,7 @@ int32_t DCameraAllConnectManager::UnRegisterLifecycleCallback() return DistributedCameraErrno::DCAMERA_ERR_DLOPEN; } if (allConnect_.dCameraCollaborationUnRegisterLifecycleCallback == nullptr) { - DHLOGE("DCamera allconnect RegisterLifecycleCallback is nullptr, all connect function not load."); + DHLOGE("DCamera allconnect UnRegisterLifecycleCallback is nullptr, all connect function not load."); return DistributedCameraErrno::DCAMERA_ERR_DLOPEN; } -- Gitee