diff --git a/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback.h b/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback.h index 9a0fa1292109feab781e92183f3e3c06d33da0e3..dcdea06a6270f00e8d1e848b7d1f075f820e65da 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback.h +++ b/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback.h @@ -33,15 +33,26 @@ public: int32_t status, std::string& data) override; int32_t OnNotifyUnregResult(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) override; + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) override; + int32_t OnDataSyncTrigger(const std::string &devId) override; void PushRegCallback(std::string& reqId, std::shared_ptr& callback); void PopRegCallback(std::string& reqId); void PushUnregCallback(std::string& reqId, std::shared_ptr& callback); void PopUnregCallback(std::string& reqId); + void RegisterStateListener(const std::shared_ptr listener); + void UnRegisterStateListener(); + void RegisterTriggerListener(const std::shared_ptr listener); + void UnRegisterTriggerListener(); private: std::map> regCallbacks_; std::map> unregCallbacks_; std::mutex mapMutex_; + std::mutex stateListenerMtx_; + std::shared_ptr stateListener_ = nullptr; + std::mutex triggerListenerMtx_; + std::shared_ptr triggerListener_ = nullptr; + const size_t DID_MAX_SIZE = 256; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback_stub.h b/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback_stub.h index 40ecb773b300098e5e0f791980a31d000f67f20b..a631adc0ee07f324ee3259bab3c27264a3b3a13e 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback_stub.h +++ b/interfaces/inner_kits/native_cpp/camera_source/include/callback/dcamera_source_callback_stub.h @@ -36,6 +36,8 @@ public: private: int32_t NotifyRegResultInner(MessageParcel &data, MessageParcel &reply); int32_t NotifyUnregResultInner(MessageParcel &data, MessageParcel &reply); + int32_t OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply); + int32_t OnDataSyncTriggerInner(MessageParcel &data, MessageParcel &reply); bool CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId, const std::string& result); diff --git a/interfaces/inner_kits/native_cpp/camera_source/include/callback/idcamera_source_callback.h b/interfaces/inner_kits/native_cpp/camera_source/include/callback/idcamera_source_callback.h index 02ffc9142010bc010c1787044f4f26a026b95c45..e3c534c97ffd0ef30b80a12f374e686b979f0d82 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/include/callback/idcamera_source_callback.h +++ b/interfaces/inner_kits/native_cpp/camera_source/include/callback/idcamera_source_callback.h @@ -26,6 +26,8 @@ public: enum { NOTIFY_REG_RESULT = 0, NOTIFY_UNREG_RESULT = 1, + NOTIFY_STATE_CHANGED = 2, + NOTIFY_DATASYNC_TRIGGER = 3, }; virtual ~IDCameraSourceCallback() {} @@ -33,6 +35,8 @@ public: int32_t status, std::string& data) = 0; virtual int32_t OnNotifyUnregResult(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) = 0; + virtual int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) = 0; + virtual int32_t OnDataSyncTrigger(const std::string &devId) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback.cpp b/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback.cpp index deada283d5e222de275577d6a311dc0da7d4354d..258d3fbbc49fa1076482c0514cada2ef6f779c87 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback.cpp +++ b/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback.cpp @@ -69,6 +69,45 @@ int32_t DCameraSourceCallback::OnNotifyUnregResult(const std::string& devId, con return ret; } +int32_t DCameraSourceCallback::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; + } + BusinessState currentState = static_cast(status); + stateListener_->OnStateChanged(devId, dhId, currentState); + return DCAMERA_OK; +} + +int32_t DCameraSourceCallback::OnDataSyncTrigger(const std::string &devId) +{ + DHLOGI("On data sync trigger, devId: %{public}s", GetAnonyString(devId).c_str()); + std::lock_guard triggerLck(triggerListenerMtx_); + if (devId.empty() || devId.size() > DID_MAX_SIZE) { + DHLOGE("devId is invalid"); + return DCAMERA_BAD_VALUE; + } + if (triggerListener_ == nullptr) { + DHLOGE("Trigger listener is null."); + return DCAMERA_BAD_VALUE; + } + triggerListener_->OnDataSyncTrigger(devId); + return DCAMERA_OK; +} + void DCameraSourceCallback::PushRegCallback(std::string& reqId, std::shared_ptr& callback) { std::lock_guard lock(mapMutex_); @@ -92,5 +131,33 @@ void DCameraSourceCallback::PopUnregCallback(std::string& reqId) std::lock_guard lock(mapMutex_); unregCallbacks_.erase(reqId); } + +void DCameraSourceCallback::RegisterStateListener(const std::shared_ptr listener) +{ + DHLOGD("Register state listener."); + std::lock_guard stateLck(stateListenerMtx_); + stateListener_ = listener; +} + +void DCameraSourceCallback::UnRegisterStateListener() +{ + DHLOGD("UnRegister state listener."); + std::lock_guard stateLck(stateListenerMtx_); + stateListener_ = nullptr; +} + +void DCameraSourceCallback::RegisterTriggerListener(const std::shared_ptr listener) +{ + DHLOGD("Register trigger listener."); + std::lock_guard triggerLck(triggerListenerMtx_); + triggerListener_ = listener; +} + +void DCameraSourceCallback::UnRegisterTriggerListener() +{ + DHLOGD("UnRegister trigger listener."); + std::lock_guard triggerLck(triggerListenerMtx_); + triggerListener_ = nullptr; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback_stub.cpp b/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback_stub.cpp index 4ee0e562b778e03ca0169bba9872df33da0416a8..d7d5fa64f660f844f2900abc10e5ab0182f18d51 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback_stub.cpp +++ b/interfaces/inner_kits/native_cpp/camera_source/src/callback/dcamera_source_callback_stub.cpp @@ -27,6 +27,8 @@ DCameraSourceCallbackStub::DCameraSourceCallbackStub() : IRemoteStub(true) { memberFuncMap_[NOTIFY_REG_RESULT] = &DCameraSourceCallbackStub::NotifyRegResultInner; memberFuncMap_[NOTIFY_UNREG_RESULT] = &DCameraSourceCallbackStub::NotifyUnregResultInner; + memberFuncMap_[NOTIFY_STATE_CHANGED] = &DCameraSourceCallbackStub::OnHardwareStateChangedInner; + memberFuncMap_[NOTIFY_DATASYNC_TRIGGER] = &DCameraSourceCallbackStub::OnDataSyncTriggerInner; } DCameraSourceCallbackStub::~DCameraSourceCallbackStub() @@ -93,6 +95,26 @@ int32_t DCameraSourceCallbackStub::NotifyUnregResultInner(MessageParcel &data, M return DCAMERA_OK; } +int32_t DCameraSourceCallbackStub::OnHardwareStateChangedInner(MessageParcel &data, MessageParcel &reply) +{ + DHLOGI("DCameraSourceCallbackStub 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; +} + +int32_t DCameraSourceCallbackStub::OnDataSyncTriggerInner(MessageParcel &data, MessageParcel &reply) +{ + DHLOGI("DCameraSourceCallbackStub OnDataSyncTriggerInner"); + std::string devId = data.ReadString(); + int32_t ret = OnDataSyncTrigger(devId); + reply.WriteInt32(ret); + return DCAMERA_OK; +} + bool DCameraSourceCallbackStub::CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId, const std::string& result) { diff --git a/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp index 9c5adcb011d0b4fabaf2991f7b4959feba25396c..625403057582dbe0b0b3afe292fd5ff13bf910e2 100644 --- a/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp +++ b/interfaces/inner_kits/native_cpp/camera_source/src/dcamera_source_handler.cpp @@ -183,20 +183,26 @@ int32_t DCameraSourceHandler::ConfigDistributedHardware(const std::string& devId void DCameraSourceHandler::RegisterDistributedHardwareStateListener( std::shared_ptr listener) { - (void)listener; + CHECK_AND_RETURN_LOG(callback_ == nullptr, "%{public}s", "ipc callback is null."); + callback_->RegisterStateListener(listener); } void DCameraSourceHandler::UnregisterDistributedHardwareStateListener() { + CHECK_AND_RETURN_LOG(callback_ == nullptr, "%{public}s", "ipc callback is null."); + callback_->UnRegisterStateListener(); } void DCameraSourceHandler::RegisterDataSyncTriggerListener(std::shared_ptr listener) { - (void)listener; + CHECK_AND_RETURN_LOG(callback_ == nullptr, "%{public}s", "ipc callback is null."); + callback_->RegisterTriggerListener(listener); } void DCameraSourceHandler::UnregisterDataSyncTriggerListener() { + CHECK_AND_RETURN_LOG(callback_ == nullptr, "%{public}s", "ipc callback is null."); + callback_->UnRegisterTriggerListener(); } void DCameraSourceHandler::SetSAState() diff --git a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h index 88c23786cc22b180cf24592cefda1f3b02a1c8e1..54e68558f4c741d3b0bb1982bb263aaa84c4e54e 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_service_state_listener.h @@ -32,6 +32,8 @@ public: int32_t status, std::string& data) override; int32_t OnUnregisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) override; + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) override; + int32_t OnDataSyncTrigger(const std::string &devId) override; void SetCallback(sptr callback) override; private: sptr callbackProxy_; diff --git a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_source_callback_proxy.h b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_source_callback_proxy.h index 1df2535ffa3c90a12c62cc6645fb7ca17ac59c94..7967203d54e8c1a218e0483b6fc4fb04119da355 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/dcamera_source_callback_proxy.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/dcamera_source_callback_proxy.h @@ -36,7 +36,10 @@ public: int32_t status, std::string& data) override; int32_t OnNotifyUnregResult(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) override; + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) override; + int32_t OnDataSyncTrigger(const std::string &devId) override; private: + bool CheckParams(const std::string& devId, const std::string& dhId, int32_t status); bool CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId, std::string& data); diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h index 08c8ceaa3560c893727d246bf4b2f862a6f098cf..d11727837ac7a45e54697b22c5866151d0f55af4 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h +++ b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerainterface/icamera_state_listener.h @@ -30,6 +30,8 @@ public: int32_t status, std::string& data) = 0; virtual int32_t OnUnregisterNotify(const std::string& devId, const std::string& dhId, const std::string& reqId, int32_t status, std::string& data) = 0; + virtual int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) = 0; + virtual int32_t OnDataSyncTrigger(const std::string &devId) = 0; virtual void SetCallback(sptr callback) = 0; }; } // namespace DistributedHardware diff --git a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp index 770b38eb2292fb055488ce44d91399e031aa099a..10d1351149b8ff750b481631eab75121d6e3fa31 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp @@ -113,5 +113,29 @@ int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId return DCAMERA_OK; } + +int32_t DCameraServiceStateListener::OnHardwareStateChanged(const std::string &devId, + const std::string &dhId, int32_t status) +{ + DHLOGI("OnHardwareStateChanged devId: %{public}s, dhId: %{public}s, status: %{public}d", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status); + std::lock_guard autoLock(proxyMutex_); + if (callbackProxy_ == nullptr) { + DHLOGE("callbackProxy_ is nullptr"); + return DCAMERA_BAD_VALUE; + } + return callbackProxy_->OnHardwareStateChanged(devId, dhId, status); +} + +int32_t DCameraServiceStateListener::OnDataSyncTrigger(const std::string &devId) +{ + DHLOGI("OnDataSyncTrigger devId: %{public}s.", GetAnonyString(devId).c_str()); + std::lock_guard autoLock(proxyMutex_); + if (callbackProxy_ == nullptr) { + DHLOGE("callbackProxy_ is nullptr"); + return DCAMERA_BAD_VALUE; + } + return callbackProxy_->OnDataSyncTrigger(devId); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_source_callback_proxy.cpp b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_source_callback_proxy.cpp index b3605bf25905d35853eff0fe9e55ee66ffc5ecba..ab9549461323c3e76e54b9978aed49d73ac98c65 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_source_callback_proxy.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_source_callback_proxy.cpp @@ -83,6 +83,72 @@ int32_t DCameraSourceCallbackProxy::OnNotifyUnregResult(const std::string& devId return result; } +int32_t DCameraSourceCallbackProxy::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(); + if (remote == nullptr) { + DHLOGE("DCameraSourceCallbackProxy remote service null"); + return DCAMERA_BAD_VALUE; + } + + MessageParcel req; + MessageParcel reply; + MessageOption option; + if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::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_CHANGED, req, reply, option); + return reply.ReadInt32(); +} + +int32_t DCameraSourceCallbackProxy::OnDataSyncTrigger(const std::string &devId) +{ + if (devId.empty() || devId.size() > DID_MAX_SIZE) { + DHLOGE("devId is invalid"); + return DCAMERA_BAD_VALUE; + } + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("DCameraSourceCallbackProxy remote service null"); + return DCAMERA_BAD_VALUE; + } + + MessageParcel req; + MessageParcel reply; + MessageOption option; + if (!req.WriteInterfaceToken(DCameraSourceCallbackProxy::GetDescriptor())) { + DHLOGE("write token failed"); + return DCAMERA_BAD_VALUE; + } + if (!req.WriteString(devId)) { + return DCAMERA_BAD_VALUE; + } + remote->SendRequest(NOTIFY_DATASYNC_TRIGGER, req, reply, option); + return reply.ReadInt32(); +} + +bool DCameraSourceCallbackProxy::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; +} + bool DCameraSourceCallbackProxy::CheckParams(const std::string& devId, const std::string& dhId, const std::string& reqId, std::string& data) { diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h index 593cf5a2e16a7d9b35409550b3ac7c38cce0c713..9265254f0ae745d2c3a0dceb1c09bd29a79e9312 100644 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/mock_dcamera_source_state_listener.h @@ -39,6 +39,16 @@ public: return DCAMERA_OK; } + int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, int32_t status) + { + return DCAMERA_OK; + } + + int32_t OnDataSyncTrigger(const std::string &devId) + { + return DCAMERA_OK; + } + void SetCallback(sptr callback) { }