diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index fc2124e..230d60c 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("libdinput_sdk") { "${common_path}/include/white_list_util.cpp", "${innerkits_path}/src/distributed_input_kit.cpp", "${ipc_path}/src/distributed_input_client.cpp", + "${ipc_path}/src/dinput_sa_manager.cpp", "${ipc_path}/src/distributed_input_source_proxy.cpp", "${ipc_path}/src/distributed_input_source_stub.cpp", "${ipc_path}/src/distributed_input_sink_proxy.cpp", diff --git a/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp b/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp index 16db85e..b1ab56f 100644 --- a/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp +++ b/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp @@ -158,16 +158,6 @@ DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& return serverType; } -bool DistributedInputClient::GetDInputSourceProxy() -{ - return true; -} - -bool DistributedInputClient::GetDInputSinkProxy() -{ - return true; -} - bool DistributedInputClient::IsJsonData(std::string strData) const { return true; diff --git a/interfaces/ipc/include/dinput_sa_manager.h b/interfaces/ipc/include/dinput_sa_manager.h new file mode 100644 index 0000000..85a738c --- /dev/null +++ b/interfaces/ipc/include/dinput_sa_manager.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 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 DINPUT_SA_MANAGER_H +#define DINPUT_SA_MANAGER_H + +#include +#include + +#include "i_distributed_source_input.h" +#include "i_distributed_sink_input.h" +#include "single_instance.h" +#include "system_ability_status_change_stub.h" + +#include "idistributed_hardware_source.h" +#include "idistributed_hardware_sink.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DinputSAManager { +DECLARE_SINGLE_INSTANCE_BASE(DinputSAManager); +public: + void Init(); + bool GetDInputSourceProxy(); + bool HasDInputSourceProxy(); + bool SetDInputSourceProxy(const sptr &remoteObject); + bool GetDInputSinkProxy(); + bool HasDInputSinkProxy(); + bool SetDInputSinkProxy(const sptr &remoteObject); + +public: +class SystemAbilityListener : public SystemAbilityStatusChangeStub { + public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + }; + +private: + DinputSAManager() = default; + ~DinputSAManager() = default; + +public: + std::atomic dInputSourceSAOnline = false; + std::atomic dInputSinkSAOnline = false; + std::atomic isSubscribeSrcSAChangeListener = false; + std::atomic isSubscribeSinkSAChangeListener = false; + std::mutex sinkMutex_; + std::mutex sourceMutex_; + + sptr dInputSourceProxy_ = nullptr; + sptr dInputSinkProxy_ = nullptr; + sptr saListenerCallback = nullptr; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS + +#endif // DINPUT_SA_MANAGER_H \ No newline at end of file diff --git a/interfaces/ipc/include/distributed_input_client.h b/interfaces/ipc/include/distributed_input_client.h index 26d90a2..7f30c6a 100644 --- a/interfaces/ipc/include/distributed_input_client.h +++ b/interfaces/ipc/include/distributed_input_client.h @@ -29,9 +29,9 @@ #include "start_d_input_server_call_back_stub.h" #include "unregister_d_input_call_back_stub.h" +#include "dinput_sa_manager.h" #include "idistributed_hardware_source.h" #include "idistributed_hardware_sink.h" -#include "system_ability_status_change_stub.h" namespace OHOS { namespace DistributedHardware { @@ -70,14 +70,6 @@ public: DInputServerType IsStartDistributedInput(const uint32_t& inputType); - bool HasDInputSourceProxy(); - - bool SetDInputSourceProxy(const sptr &remoteObject); - - bool HasDInputSinkProxy(); - - bool SetDInputSinkProxy(const sptr &remoteObject); - public: class RegisterDInputCb : public OHOS::DistributedHardware::DistributedInput::RegisterDInputCallbackStub { public: @@ -114,30 +106,14 @@ public: void OnResult(const std::string &deviceId); }; - class SystemAbilityListener : public SystemAbilityStatusChangeStub { - public: - void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - }; - private: DistributedInputClient(); - void Init(); - bool GetDInputSourceProxy(); - bool GetDInputSinkProxy(); bool IsJsonData(std::string strData) const; void AddWhiteListInfos(const std::string &deviceId, const std::string &strJson) const; void DelWhiteListInfos(const std::string &deviceId) const; private: static std::shared_ptr instance; - std::mutex mutex_; - std::atomic dInputSourceSAOnline = false; - std::atomic dInputSinkSAOnline = false; - std::atomic isSubscribeSrcSAChangeListener = false; - std::atomic isSubscribeSinkSAChangeListener = false; - sptr dInputSourceProxy_ = nullptr; - sptr dInputSinkProxy_ = nullptr; bool m_bIsAlreadyInitWhiteList = false; const std::string localDevId_ = "localNodeDevice"; @@ -151,7 +127,6 @@ private: sptr sourceTypeCallback = nullptr; sptr addWhiteListCallback = nullptr; sptr delWhiteListCallback = nullptr; - sptr saListenerCallback = nullptr; struct DHardWareFwkRegistInfo { std::string devId; diff --git a/interfaces/ipc/src/dinput_sa_manager.cpp b/interfaces/ipc/src/dinput_sa_manager.cpp new file mode 100644 index 0000000..82083e1 --- /dev/null +++ b/interfaces/ipc/src/dinput_sa_manager.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2022 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 "dinput_sa_manager.h" + +#include "distributed_hardware_log.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +#include "constants_dinput.h" +#include "dinput_errcode.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +IMPLEMENT_SINGLE_INSTANCE(DinputSAManager); + +void DinputSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, + const std::string& deviceId) +{ + if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID) { + DinputSAManager::GetInstance().dInputSourceSAOnline.store(false); + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + DinputSAManager::GetInstance().dInputSourceProxy_ = nullptr; + } else if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID) { + DinputSAManager::GetInstance().dInputSinkSAOnline.store(false); + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + DinputSAManager::GetInstance().dInputSinkProxy_ = nullptr; + } + DHLOGI("sa %d is removed.", systemAbilityId); +} + +void DinputSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +{ + if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID) { + DinputSAManager::GetInstance().dInputSourceSAOnline.store(true); + } else if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID) { + DinputSAManager::GetInstance().dInputSinkSAOnline.store(true); + } + DHLOGI("sa %d is added.", systemAbilityId); +} + +void DinputSAManager::Init() +{ + saListenerCallback = new(std::nothrow) SystemAbilityListener(); + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + + if (!systemAbilityManager) { + DHLOGE("get system ability manager failed."); + return; + } + + if (!isSubscribeSrcSAChangeListener.load()) { + DHLOGI("try subscribe source sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); + int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, + saListenerCallback); + if (ret != DH_SUCCESS) { + DHLOGE("subscribe source sa change failed: %d", ret); + return; + } + isSubscribeSrcSAChangeListener.store(true); + } + + if (!isSubscribeSinkSAChangeListener.load()) { + DHLOGI("try subscribe sink sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); + int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, + saListenerCallback); + if (ret != DH_SUCCESS) { + DHLOGE("subscribe sink sa change failed: %d", ret); + return; + } + isSubscribeSinkSAChangeListener.store(true); + } +} + +bool DinputSAManager::GetDInputSourceProxy() +{ + if (!isSubscribeSrcSAChangeListener.load()) { + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + if (!isSubscribeSrcSAChangeListener.load()) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + DHLOGE("get system ability manager failed."); + return false; + } + + DHLOGI("try subscribe source sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); + int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, + saListenerCallback); + if (ret != DH_SUCCESS) { + DHLOGE("subscribe source sa change failed: %d", ret); + return false; + } + isSubscribeSrcSAChangeListener.store(true); + } + } + + if (dInputSourceSAOnline.load() && !dInputSourceProxy_) { + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + if (dInputSourceProxy_ != nullptr) { + DHLOGI("dinput source proxy has already got."); + return true; + } + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + + if (!systemAbilityManager) { + DHLOGE("get system ability manager failed."); + return false; + } + DHLOGI("%s try get sa: %d", __func__, DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); + sptr remoteObject = systemAbilityManager->GetSystemAbility( + DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); + if (!remoteObject) { + return false; + } + + dInputSourceProxy_ = iface_cast(remoteObject); + + if ((!dInputSourceProxy_) || (!dInputSourceProxy_->AsObject())) { + return false; + } + } + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + return dInputSourceProxy_ != nullptr; +} + +bool DinputSAManager::HasDInputSourceProxy() +{ + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + return dInputSourceProxy_ != nullptr; +} + +bool DinputSAManager::SetDInputSourceProxy(const sptr &remoteObject) +{ + std::lock_guard lock(DinputSAManager::GetInstance().sourceMutex_); + dInputSourceProxy_ = iface_cast(remoteObject); + + if ((!dInputSourceProxy_) || (!dInputSourceProxy_->AsObject())) { + DHLOGE("Failed to get dinput source proxy."); + return false; + } + return true; +} + +bool DinputSAManager::GetDInputSinkProxy() +{ + if (!isSubscribeSinkSAChangeListener.load()) { + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + if (!isSubscribeSinkSAChangeListener.load()) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + DHLOGE("get system ability manager failed."); + return false; + } + + DHLOGI("try subscribe sink sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); + int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, + saListenerCallback); + if (ret != DH_SUCCESS) { + DHLOGE("subscribe sink sa change failed: %d", ret); + return false; + } + isSubscribeSinkSAChangeListener.store(true); + } + } + + if (dInputSinkSAOnline.load() && !dInputSinkProxy_) { + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + if (dInputSinkProxy_ != nullptr) { + DHLOGI("dinput sink proxy has already got."); + return true; + } + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + DHLOGE("get system ability manager failed."); + return false; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility( + DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); + if (!remoteObject) { + return false; + } + + dInputSinkProxy_ = iface_cast(remoteObject); + + if ((!dInputSinkProxy_) || (!dInputSinkProxy_->AsObject())) { + return false; + } + } + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + return dInputSinkProxy_ != nullptr; +} + +bool DinputSAManager::HasDInputSinkProxy() +{ + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + return dInputSinkProxy_ != nullptr; +} + +bool DinputSAManager::SetDInputSinkProxy(const sptr &remoteObject) +{ + std::lock_guard lock(DinputSAManager::GetInstance().sinkMutex_); + dInputSinkProxy_ = iface_cast(remoteObject); + + if ((!dInputSinkProxy_) || (!dInputSinkProxy_->AsObject())) { + DHLOGE("Failed to get dinput sink proxy."); + return false; + } + return true; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 5d75f39..9fe2b90 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -33,41 +33,7 @@ std::shared_ptr DistributedInputClient::instance(new Dis DistributedInputClient::DistributedInputClient() { - Init(); -} - -void DistributedInputClient::Init() -{ - saListenerCallback = new(std::nothrow) SystemAbilityListener(); - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - - if (!systemAbilityManager) { - DHLOGE("get system ability manager failed."); - return; - } - - if (!isSubscribeSrcSAChangeListener.load()) { - DHLOGI("try subscribe source sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); - int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, - saListenerCallback); - if (ret != DH_SUCCESS) { - DHLOGE("subscribe source sa change failed: %d", ret); - return; - } - isSubscribeSrcSAChangeListener.store(true); - } - - if (!isSubscribeSinkSAChangeListener.load()) { - DHLOGI("try subscribe sink sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); - int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, - saListenerCallback); - if (ret != DH_SUCCESS) { - DHLOGE("subscribe sink sa change failed: %d", ret); - return; - } - isSubscribeSinkSAChangeListener.store(true); - } + DinputSAManager::GetInstance().Init(); } DistributedInputClient &DistributedInputClient::GetInstance() @@ -92,7 +58,6 @@ void DistributedInputClient::RegisterDInputCb::OnResult( void DistributedInputClient::UnregisterDInputCb::OnResult( const std::string& devId, const std::string& dhId, const int32_t& status) - { for (std::vector::iterator iter = DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.begin(); @@ -132,51 +97,25 @@ void DistributedInputClient::DelWhiteListInfosCb::OnResult(const std::string& de DistributedInputClient::GetInstance().DelWhiteListInfos(deviceId); } -void DistributedInputClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, - const std::string& deviceId) -{ - if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID) { - DistributedInputClient::GetInstance().dInputSourceSAOnline.store(true); - } else if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID) { - DistributedInputClient::GetInstance().dInputSinkSAOnline.store(true); - } - DHLOGI("sa %d is added.", systemAbilityId); -} - -void DistributedInputClient::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, - const std::string& deviceId) -{ - if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID) { - DistributedInputClient::GetInstance().dInputSourceSAOnline.store(false); - std::lock_guard lock(DistributedInputClient::GetInstance().mutex_); - DistributedInputClient::GetInstance().dInputSourceProxy_ = nullptr; - } else if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID) { - DistributedInputClient::GetInstance().dInputSinkSAOnline.store(false); - std::lock_guard lock(DistributedInputClient::GetInstance().mutex_); - DistributedInputClient::GetInstance().dInputSinkProxy_ = nullptr; - } - DHLOGI("sa %d is removed.", systemAbilityId); -} - int32_t DistributedInputClient::InitSource() { - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - return dInputSourceProxy_->Init(); + return DinputSAManager::GetInstance().dInputSourceProxy_->Init(); } int32_t DistributedInputClient::InitSink() { - if (!GetDInputSinkProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSinkProxy()) { return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL; } - return dInputSinkProxy_->Init(); + return DinputSAManager::GetInstance().dInputSinkProxy_->Init(); } int32_t DistributedInputClient::ReleaseSource() { - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -190,12 +129,12 @@ int32_t DistributedInputClient::ReleaseSource() addWhiteListCallback = nullptr; delWhiteListCallback = nullptr; WhiteListUtil::GetInstance().ClearWhiteList(); - return dInputSourceProxy_->Release(); + return DinputSAManager::GetInstance().dInputSourceProxy_->Release(); } int32_t DistributedInputClient::ReleaseSink() { - if (!GetDInputSinkProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSinkProxy()) { return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL; } serverType = DInputServerType::NULL_SERVER_TYPE; @@ -203,7 +142,7 @@ int32_t DistributedInputClient::ReleaseSink() m_bIsAlreadyInitWhiteList = false; sinkTypeCallback = nullptr; WhiteListUtil::GetInstance().ClearWhiteList(localDevId_); - return dInputSinkProxy_->Release(); + return DinputSAManager::GetInstance().dInputSinkProxy_->Release(); } int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& devId, const std::string& dhId, @@ -212,7 +151,7 @@ int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& d DHLOGI("%s called, deviceId: %s, dhId: %s, parameters: %s", __func__, GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), parameters.c_str()); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("RegisterDistributedHardware client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -234,7 +173,8 @@ int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& d dHardWareFwkRstInfos.push_back(info); callbackRegister = new(std::nothrow) RegisterDInputCb(); - return dInputSourceProxy_->RegisterDistributedHardware(devId, dhId, parameters, callbackRegister); + return DinputSAManager::GetInstance().dInputSourceProxy_->RegisterDistributedHardware(devId, dhId, parameters, + callbackRegister); } int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& devId, const std::string& dhId, @@ -243,7 +183,7 @@ int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& DHLOGI("%s called, deviceId: %s, dhId: %s", __func__, GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("UnregisterDistributedHardware client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -265,7 +205,8 @@ int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& dHardWareFwkUnRstInfos.push_back(info); callbackUnregister = new(std::nothrow) UnregisterDInputCb(); - return dInputSourceProxy_->UnregisterDistributedHardware(devId, dhId, callbackUnregister); + return DinputSAManager::GetInstance().dInputSourceProxy_->UnregisterDistributedHardware(devId, dhId, + callbackUnregister); } int32_t DistributedInputClient::PrepareRemoteInput( @@ -273,7 +214,7 @@ int32_t DistributedInputClient::PrepareRemoteInput( { DHLOGI("%s called, deviceId: %s", __func__, GetAnonyString(deviceId).c_str()); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("PrepareRemoteInput client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -283,7 +224,8 @@ int32_t DistributedInputClient::PrepareRemoteInput( } addWhiteListCallback = new(std::nothrow) AddWhiteListInfosCb(); - return dInputSourceProxy_->PrepareRemoteInput(deviceId, callback, addWhiteListCallback); + return DinputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(deviceId, callback, + addWhiteListCallback); } int32_t DistributedInputClient::UnprepareRemoteInput( @@ -291,7 +233,7 @@ int32_t DistributedInputClient::UnprepareRemoteInput( { DHLOGI("%s called, deviceId: %s", __func__, GetAnonyString(deviceId).c_str()); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("PrepareRemoteInput client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -301,7 +243,8 @@ int32_t DistributedInputClient::UnprepareRemoteInput( } delWhiteListCallback = new(std::nothrow) DelWhiteListInfosCb(); - return dInputSourceProxy_->UnprepareRemoteInput(deviceId, callback, delWhiteListCallback); + return DinputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(deviceId, callback, + delWhiteListCallback); } int32_t DistributedInputClient::StartRemoteInput( @@ -309,7 +252,7 @@ int32_t DistributedInputClient::StartRemoteInput( { DHLOGI("%s called, deviceId: %s, inputTypes: %d", __func__, GetAnonyString(deviceId).c_str(), inputTypes); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("StartRemoteInput client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -321,7 +264,7 @@ int32_t DistributedInputClient::StartRemoteInput( return ERR_DH_INPUT_CLIENT_START_FAIL; } - return dInputSourceProxy_->StartRemoteInput(deviceId, inputTypes, callback); + return DinputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(deviceId, inputTypes, callback); } int32_t DistributedInputClient::StopRemoteInput( @@ -329,7 +272,7 @@ int32_t DistributedInputClient::StopRemoteInput( { DHLOGI("%s called, deviceId: %s, inputTypes: %d", __func__, GetAnonyString(deviceId).c_str(), inputTypes); - if (!GetDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().GetDInputSourceProxy()) { DHLOGE("StopRemoteInput client fail"); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } @@ -341,7 +284,7 @@ int32_t DistributedInputClient::StopRemoteInput( return ERR_DH_INPUT_CLIENT_STOP_FAIL; } - return dInputSourceProxy_->StopRemoteInput(deviceId, inputTypes, callback); + return DinputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(deviceId, inputTypes, callback); } bool DistributedInputClient::IsNeedFilterOut(const std::string& deviceId, const BusinessEvent& event) @@ -374,14 +317,15 @@ DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& int32_t retSource = 0; int32_t retSink = 0; - if (sourceTypeCallback == nullptr && GetDInputSourceProxy()) { + if (sourceTypeCallback == nullptr && DinputSAManager::GetInstance().GetDInputSourceProxy()) { sourceTypeCallback = new(std::nothrow) StartDInputServerCb(); - retSource = dInputSourceProxy_->IsStartDistributedInput(inputType, sourceTypeCallback); + retSource = DinputSAManager::GetInstance().dInputSourceProxy_->IsStartDistributedInput(inputType, + sourceTypeCallback); } - if (sinkTypeCallback == nullptr && GetDInputSinkProxy()) { + if (sinkTypeCallback == nullptr && DinputSAManager::GetInstance().GetDInputSinkProxy()) { sinkTypeCallback = new(std::nothrow) StartDInputServerCb(); - retSink = dInputSinkProxy_->IsStartDistributedInput(inputType, sinkTypeCallback); + retSink = DinputSAManager::GetInstance().dInputSinkProxy_->IsStartDistributedInput(inputType, sinkTypeCallback); } if (static_cast(retSource) != DInputServerType::NULL_SERVER_TYPE) { @@ -397,141 +341,6 @@ DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& } } -bool DistributedInputClient::GetDInputSourceProxy() -{ - if (!isSubscribeSrcSAChangeListener.load()) { - std::lock_guard lock(mutex_); - if (!isSubscribeSrcSAChangeListener.load()) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - DHLOGE("get system ability manager failed."); - return false; - } - - DHLOGI("try subscribe source sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); - int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, - saListenerCallback); - if (ret != DH_SUCCESS) { - DHLOGE("subscribe source sa change failed: %d", ret); - return false; - } - isSubscribeSrcSAChangeListener.store(true); - } - } - - if (dInputSourceSAOnline.load() && !dInputSourceProxy_) { - std::lock_guard lock(mutex_); - if (dInputSourceProxy_ != nullptr) { - DHLOGI("dinput source proxy has already got."); - return true; - } - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - - if (!systemAbilityManager) { - DHLOGE("get system ability manager failed."); - return false; - } - DHLOGI("%s try get sa: %d", __func__, DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); - sptr remoteObject = systemAbilityManager->GetSystemAbility( - DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID); - if (!remoteObject) { - return false; - } - - dInputSourceProxy_ = iface_cast(remoteObject); - - if ((!dInputSourceProxy_) || (!dInputSourceProxy_->AsObject())) { - return false; - } - } - return dInputSourceProxy_ != nullptr; -} - -bool DistributedInputClient::HasDInputSourceProxy() -{ - return dInputSourceProxy_ != nullptr; -} - -bool DistributedInputClient::SetDInputSourceProxy(const sptr &remoteObject) -{ - dInputSourceProxy_ = iface_cast(remoteObject); - - if ((!dInputSourceProxy_) || (!dInputSourceProxy_->AsObject())) { - DHLOGE("Failed to get dinput source proxy."); - return false; - } - return true; -} - -bool DistributedInputClient::HasDInputSinkProxy() -{ - return dInputSinkProxy_ != nullptr; -} - -bool DistributedInputClient::SetDInputSinkProxy(const sptr &remoteObject) -{ - dInputSinkProxy_ = iface_cast(remoteObject); - - if ((!dInputSinkProxy_) || (!dInputSinkProxy_->AsObject())) { - DHLOGE("Failed to get dinput sink proxy."); - return false; - } - return true; -} - -bool DistributedInputClient::GetDInputSinkProxy() -{ - if (!isSubscribeSinkSAChangeListener.load()) { - std::lock_guard lock(mutex_); - if (!isSubscribeSinkSAChangeListener.load()) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - DHLOGE("get system ability manager failed."); - return false; - } - - DHLOGI("try subscribe sink sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); - int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, - saListenerCallback); - if (ret != DH_SUCCESS) { - DHLOGE("subscribe sink sa change failed: %d", ret); - return false; - } - isSubscribeSinkSAChangeListener.store(true); - } - } - - if (dInputSinkSAOnline.load() && !dInputSinkProxy_) { - std::lock_guard lock(mutex_); - if (dInputSinkProxy_ != nullptr) { - DHLOGI("dinput sink proxy has already got."); - return true; - } - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - DHLOGE("get system ability manager failed."); - return false; - } - - sptr remoteObject = systemAbilityManager->GetSystemAbility( - DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID); - if (!remoteObject) { - return false; - } - - dInputSinkProxy_ = iface_cast(remoteObject); - - if ((!dInputSinkProxy_) || (!dInputSinkProxy_->AsObject())) { - return false; - } - } - return dInputSinkProxy_ != nullptr; -} - bool DistributedInputClient::IsJsonData(std::string strData) const { if (strData[0] != '{') { diff --git a/sinkhandler/src/distributed_input_sink_handler.cpp b/sinkhandler/src/distributed_input_sink_handler.cpp index 664363f..bfef14a 100644 --- a/sinkhandler/src/distributed_input_sink_handler.cpp +++ b/sinkhandler/src/distributed_input_sink_handler.cpp @@ -36,7 +36,7 @@ int32_t DistributedInputSinkHandler::InitSink(const std::string ¶ms) { DHLOGD("InitSource"); std::unique_lock lock(proxyMutex_); - if (!DistributedInputClient::GetInstance().HasDInputSinkProxy()) { + if (!DinputSAManager::GetInstance().HasDInputSinkProxy()) { sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!samgr) { DHLOGE("Failed to get system ability mgr."); @@ -54,7 +54,7 @@ int32_t DistributedInputSinkHandler::InitSink(const std::string ¶ms) } auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS), - [this]() { return (DistributedInputClient::GetInstance().HasDInputSinkProxy()); }); + [this]() { return (DinputSAManager::GetInstance().HasDInputSinkProxy()); }); if (!waitStatus) { DHLOGE("dinput load sa timeout."); return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL; @@ -67,7 +67,7 @@ void DistributedInputSinkHandler::FinishStartSA(const std::string ¶ms, const { DHLOGD("FinishStartSA"); std::unique_lock lock(proxyMutex_); - DistributedInputClient::GetInstance().SetDInputSinkProxy(remoteObject); + DinputSAManager::GetInstance().SetDInputSinkProxy(remoteObject); DistributedInputClient::GetInstance().InitSink(); proxyConVar_.notify_all(); } diff --git a/sinkhandler/test/unittest/mock/mock_distributed_input_client.cpp b/sinkhandler/test/unittest/mock/mock_distributed_input_client.cpp index 9ac649a..cae940e 100644 --- a/sinkhandler/test/unittest/mock/mock_distributed_input_client.cpp +++ b/sinkhandler/test/unittest/mock/mock_distributed_input_client.cpp @@ -154,16 +154,6 @@ DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& return serverType; } -bool DistributedInputClient::GetDInputSourceProxy() -{ - return true; -} - -bool DistributedInputClient::GetDInputSinkProxy() -{ - return true; -} - bool DistributedInputClient::IsJsonData(std::string strData) const { return true; diff --git a/sourcehandler/src/distributed_input_source_handler.cpp b/sourcehandler/src/distributed_input_source_handler.cpp index 20f11c0..f68e0ca 100644 --- a/sourcehandler/src/distributed_input_source_handler.cpp +++ b/sourcehandler/src/distributed_input_source_handler.cpp @@ -35,7 +35,7 @@ int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms) { DHLOGD("InitSource"); std::unique_lock lock(proxyMutex_); - if (!DistributedInputClient::GetInstance().HasDInputSourceProxy()) { + if (!DinputSAManager::GetInstance().HasDInputSourceProxy()) { sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!samgr) { DHLOGE("Failed to get system ability mgr."); @@ -53,7 +53,7 @@ int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms) } auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS), - [this]() { return (DistributedInputClient::GetInstance().HasDInputSourceProxy()); }); + [this]() { return (DinputSAManager::GetInstance().HasDInputSourceProxy()); }); if (!waitStatus) { DHLOGE("dinput load sa timeout."); return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL; @@ -66,7 +66,7 @@ void DistributedInputSourceHandler::FinishStartSA(const std::string ¶ms, con { DHLOGD("FinishStartSA"); std::unique_lock lock(proxyMutex_); - DistributedInputClient::GetInstance().SetDInputSourceProxy(remoteObject); + DinputSAManager::GetInstance().SetDInputSourceProxy(remoteObject); DistributedInputClient::GetInstance().InitSource(); proxyConVar_.notify_all(); } diff --git a/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp b/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp index dbbd434..a451ea7 100644 --- a/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp +++ b/sourcehandler/test/unittest/mock/mock_distributed_input_client.cpp @@ -154,16 +154,6 @@ DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& return serverType; } -bool DistributedInputClient::GetDInputSourceProxy() -{ - return true; -} - -bool DistributedInputClient::GetDInputSinkProxy() -{ - return true; -} - bool DistributedInputClient::IsJsonData(std::string strData) const { return true;