diff --git a/sinkhandler/include/distributed_input_sink_handler.h b/sinkhandler/include/distributed_input_sink_handler.h index 58477d3ec470d392e8e63f29c2377148517ab643..86090ee18f876bc9e1fe9c3b308d24d803009379 100644 --- a/sinkhandler/include/distributed_input_sink_handler.h +++ b/sinkhandler/include/distributed_input_sink_handler.h @@ -27,6 +27,7 @@ #include "system_ability_load_callback_stub.h" #include "distributed_input_client.h" +#include "i_distributed_sink_input.h" namespace OHOS { namespace DistributedHardware { @@ -38,6 +39,7 @@ public: int32_t ReleaseSink() override; int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶ms) override; int32_t UnsubscribeLocalHardware(const std::string &dhId) override; + void OnRemoteSinkSvrDied(const wptr &remote); void FinishStartSA(const std::string ¶ms, const sptr &remoteObject); public: @@ -63,10 +65,16 @@ public: }; private: + class DInputSinkSvrRecipient : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr &remote) override; + }; DistributedInputSinkHandler() = default; ~DistributedInputSinkHandler(); OHOS::sptr sysSinkCallback = nullptr; + sptr dInputSinkProxy_ = nullptr; + sptr sinkSvrRecipient_ = nullptr; std::mutex proxyMutex_; std::condition_variable proxyConVar_; }; diff --git a/sinkhandler/src/distributed_input_sink_handler.cpp b/sinkhandler/src/distributed_input_sink_handler.cpp index a6a49f5f6abebbe4950ad038f0e5c6f99429162f..20a5a1342c96404637520c0a2b07a2c7e178dc89 100644 --- a/sinkhandler/src/distributed_input_sink_handler.cpp +++ b/sinkhandler/src/distributed_input_sink_handler.cpp @@ -64,9 +64,19 @@ int32_t DistributedInputSinkHandler::InitSink(const std::string ¶ms) void DistributedInputSinkHandler::FinishStartSA(const std::string ¶ms, const sptr &remoteObject) { - DHLOGD("FinishStartSA"); - std::unique_lock lock(proxyMutex_); + DHLOGI("DInputSinkHandler FinishStartSA"); + std::lock_guard lock(proxyMutex_); + if (sinkSvrRecipient_ == nullptr) { + DHLOGE("sinkSvrRecipient is nullptr."); + return; + } + remoteObject->AddDeathRecipient(sinkSvrRecipient_); + dInputSinkProxy_ = iface_cast(remoteObject); DInputSAManager::GetInstance().SetDInputSinkProxy(remoteObject); + if ((dInputSinkProxy_ == nullptr) || (dInputSinkProxy_->AsObject() == nullptr)) { + DHLOGE("Faild to get input sink proxy."); + return; + } DistributedInputClient::GetInstance().InitSink(); proxyConVar_.notify_all(); } @@ -100,6 +110,42 @@ void DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilityFail(int32_t DHLOGE("DistributedInputSinkHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId); } +void DistributedInputSinkHandler::DInputSinkSvrRecipient::OnRemoteDied(const wptr &remote) +{ + if (remote == nullptr) { + DHLOGE("OnRemoteDied remote is nullptr."); + return; + } + DHLOGI("DInputSinkSvrRecipient OnRemoteDied."); + DistributedInputSinkHandler::GetInstance().OnRemoteSinkSvrDied(remote); +} + +void DistributedInputSinkHandler::OnRemoteSinkSvrDied(const wptr &remote) +{ + DHLOGI("DInputSinkHandle OnRemoteSinkSvrDied."); + std::lock_guard lock(proxyMutex_); + if (dInputSinkProxy_ == nullptr) { + DHLOGE("dInputSinkProxy_ is nullptr."); + return; + } + if (dInputSinkProxy_->AsObject() == nullptr) { + DHLOGE("AsObject is nullptr."); + return; + } + sptr remoteObject = remote.promote(); + if (remoteObject == nullptr) { + DHLOGE("OnRemoteDied remote promoted failed"); + return; + } + + if (dInputSinkProxy_->AsObject() != remoteObject) { + DHLOGE("OnRemoteSinkSvrDied not found remote object."); + return; + } + dInputSinkProxy_->AsObject()->RemoveDeathRecipient(sinkSvrRecipient_); + dInputSinkProxy_ = nullptr; +} + IDistributedHardwareSink *GetSinkHardwareHandler() { return &DistributedInputSinkHandler::GetInstance(); diff --git a/sourcehandler/include/distributed_input_source_handler.h b/sourcehandler/include/distributed_input_source_handler.h index 3a2e63408f5bf8d3a8f3804e9002416b5b0a0a2a..fb96f6549ca07ac8ad71dcff48754a17b6e43fa9 100644 --- a/sourcehandler/include/distributed_input_source_handler.h +++ b/sourcehandler/include/distributed_input_source_handler.h @@ -28,6 +28,8 @@ #include "distributed_input_client.h" #include "distributed_input_source_manager.h" +#include "i_distributed_source_input.h" +#include "load_d_input_source_callback.h" namespace OHOS { namespace DistributedHardware { @@ -44,6 +46,7 @@ public: int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, const std::string &key, const std::string &value) override; void FinishStartSA(const std::string ¶ms, const sptr &remoteObject); + void OnRemoteSourceSvrDied(const wptr &remote); public: @@ -70,10 +73,17 @@ public: private: DistributedInputSourceHandler() = default; ~DistributedInputSourceHandler(); + class DInputSourceSvrRecipient : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr &remote) override; + }; OHOS::sptr sysSourceCallback = nullptr; std::mutex proxyMutex_; std::condition_variable proxyConVar_; + sptr dInputSourceProxy_ = nullptr; + sptr dInputSourceCallback_ = nullptr; + sptr sourceSvrRecipient_ = nullptr; }; #ifdef __cplusplus diff --git a/sourcehandler/src/distributed_input_source_handler.cpp b/sourcehandler/src/distributed_input_source_handler.cpp index 55267c8cc175d992c6bab7f0afb4529f403d2eed..b5b7c7cfeeafedb56e0377f5827af41208b19865 100644 --- a/sourcehandler/src/distributed_input_source_handler.cpp +++ b/sourcehandler/src/distributed_input_source_handler.cpp @@ -63,9 +63,19 @@ int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms) void DistributedInputSourceHandler::FinishStartSA(const std::string ¶ms, const sptr &remoteObject) { - DHLOGD("FinishStartSA"); - std::unique_lock lock(proxyMutex_); + DHLOGI("DInputSourceHandle FinishStartSA"); + std::lock_guard lock(proxyMutex_); + if (sourceSvrRecipient_ == nullptr) { + DHLOGE("sourceSvrRecipient is nullptr."); + return; + } + remoteObject->AddDeathRecipient(sourceSvrRecipient_); + dInputSourceProxy_ = iface_cast(remoteObject); DInputSAManager::GetInstance().SetDInputSourceProxy(remoteObject); + if ((dInputSourceProxy_ == nullptr) || (dInputSourceProxy_->AsObject() == nullptr)) { + DHLOGE("Faild to get input source proxy."); + return; + } DistributedInputClient::GetInstance().InitSource(); proxyConVar_.notify_all(); } @@ -107,6 +117,42 @@ void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int3 DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId); } +void DistributedInputSourceHandler::DInputSourceSvrRecipient::OnRemoteDied(const wptr &remote) +{ + if (remote == nullptr) { + DHLOGE("OnRemoteDied remote is nullptr."); + return; + } + DHLOGI("DInputSourceSvrRecipient OnRemoteDied."); + DistributedInputSourceHandler::GetInstance().OnRemoteSourceSvrDied(remote); +} + +void DistributedInputSourceHandler::OnRemoteSourceSvrDied(const wptr &remote) +{ + DHLOGI("OnRemoteSourceSvrDied."); + std::lock_guard lock(proxyMutex_); + if (dInputSourceProxy_ == nullptr) { + DHLOGE("dInputSourceProxy is nullptr."); + return; + } + if (dInputSourceProxy_->AsObject() == nullptr) { + DHLOGE("AsObject is nullptr."); + return; + } + sptr remoteObject = remote.promote(); + if (remoteObject == nullptr) { + DHLOGE("OnRemoteDied remote promoted failed"); + return; + } + + if (dInputSourceProxy_->AsObject() != remoteObject) { + DHLOGE("OnRemoteSourceSvrDied not found remote object."); + return; + } + dInputSourceProxy_->AsObject()->RemoveDeathRecipient(sourceSvrRecipient_); + dInputSourceProxy_ = nullptr; +} + IDistributedHardwareSource *GetSourceHardwareHandler() { return &DistributedInputSourceHandler::GetInstance();