diff --git a/sinkhandler/include/distributed_input_sink_handler.h b/sinkhandler/include/distributed_input_sink_handler.h index 58477d3..86090ee 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 a6a49f5..d8d78de 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"); + DHLOGI("DInputSinkHandler FinishStartSA"); std::unique_lock 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,38 @@ void DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilityFail(int32_t DHLOGE("DistributedInputSinkHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId); } +void DistributedInputSinkHandler::DInputSinkSvrRecipient::OnRemoteDied(const wptr &remote) +{ + if (remote == nullptr) { + DHLOGI("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; + } + 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 3a2e634..fb96f65 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 55267c8..23d3bdf 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"); + DHLOGI("DInputSourceHandle FinishStartSA"); std::unique_lock 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,38 @@ void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int3 DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId); } +void DistributedInputSourceHandler::DInputSourceSvrRecipient::OnRemoteDied(const wptr &remote) +{ + if (remote == nullptr) { + DHLOGI("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; + } + 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();