12d4d9a4dSopenharmony_ci/*
22d4d9a4dSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
32d4d9a4dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42d4d9a4dSopenharmony_ci * you may not use this file except in compliance with the License.
52d4d9a4dSopenharmony_ci * You may obtain a copy of the License at
62d4d9a4dSopenharmony_ci *
72d4d9a4dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82d4d9a4dSopenharmony_ci *
92d4d9a4dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102d4d9a4dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112d4d9a4dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122d4d9a4dSopenharmony_ci * See the License for the specific language governing permissions and
132d4d9a4dSopenharmony_ci * limitations under the License.
142d4d9a4dSopenharmony_ci */
152d4d9a4dSopenharmony_ci
162d4d9a4dSopenharmony_ci#include "distributed_input_sink_handler.h"
172d4d9a4dSopenharmony_ci
182d4d9a4dSopenharmony_ci#include "dinput_errcode.h"
192d4d9a4dSopenharmony_ci#include "dinput_log.h"
202d4d9a4dSopenharmony_ci#include "hisysevent_util.h"
212d4d9a4dSopenharmony_ci#include "i_distributed_sink_input.h"
222d4d9a4dSopenharmony_ci#include "load_d_input_sink_callback.h"
232d4d9a4dSopenharmony_ci
242d4d9a4dSopenharmony_cinamespace OHOS {
252d4d9a4dSopenharmony_cinamespace DistributedHardware {
262d4d9a4dSopenharmony_cinamespace DistributedInput {
272d4d9a4dSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DistributedInputSinkHandler);
282d4d9a4dSopenharmony_ci
292d4d9a4dSopenharmony_ciDistributedInputSinkHandler::DistributedInputSinkHandler()
302d4d9a4dSopenharmony_ci{
312d4d9a4dSopenharmony_ci    DHLOGI("DInputSinkHandler construct.");
322d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
332d4d9a4dSopenharmony_ci    if (sinkSvrRecipient_ == nullptr) {
342d4d9a4dSopenharmony_ci        sinkSvrRecipient_ = new (std::nothrow) DInputSinkSvrRecipient();
352d4d9a4dSopenharmony_ci    }
362d4d9a4dSopenharmony_ci}
372d4d9a4dSopenharmony_ci
382d4d9a4dSopenharmony_ciDistributedInputSinkHandler::~DistributedInputSinkHandler()
392d4d9a4dSopenharmony_ci{
402d4d9a4dSopenharmony_ci    DHLOGI("~DistributedInputSinkHandler");
412d4d9a4dSopenharmony_ci}
422d4d9a4dSopenharmony_ci
432d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::InitSink(const std::string &params)
442d4d9a4dSopenharmony_ci{
452d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSinkHandler InitSink begin");
462d4d9a4dSopenharmony_ci    std::unique_lock<std::mutex> lock(proxyMutex_);
472d4d9a4dSopenharmony_ci    sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
482d4d9a4dSopenharmony_ci    if (!samgr) {
492d4d9a4dSopenharmony_ci        DHLOGE("Failed to get system ability mgr.");
502d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
512d4d9a4dSopenharmony_ci    }
522d4d9a4dSopenharmony_ci    sptr<LoadDInputSinkCallback> loadCallback(new LoadDInputSinkCallback(params));
532d4d9a4dSopenharmony_ci    HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
542d4d9a4dSopenharmony_ci        "dinput sink LoadSystemAbility call");
552d4d9a4dSopenharmony_ci    int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, loadCallback);
562d4d9a4dSopenharmony_ci    if (ret != ERR_OK) {
572d4d9a4dSopenharmony_ci        DHLOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
582d4d9a4dSopenharmony_ci               DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, ret);
592d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
602d4d9a4dSopenharmony_ci    }
612d4d9a4dSopenharmony_ci
622d4d9a4dSopenharmony_ci    auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
632d4d9a4dSopenharmony_ci        [this]() { return (DInputSAManager::GetInstance().HasDInputSinkProxy()); });
642d4d9a4dSopenharmony_ci    if (!waitStatus) {
652d4d9a4dSopenharmony_ci        DHLOGE("dinput load sink sa timeout.");
662d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
672d4d9a4dSopenharmony_ci    }
682d4d9a4dSopenharmony_ci
692d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSinkHandler InitSink end");
702d4d9a4dSopenharmony_ci    return DH_SUCCESS;
712d4d9a4dSopenharmony_ci}
722d4d9a4dSopenharmony_ci
732d4d9a4dSopenharmony_civoid DistributedInputSinkHandler::FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject)
742d4d9a4dSopenharmony_ci{
752d4d9a4dSopenharmony_ci    DHLOGI("DInputSinkHandler FinishStartSA");
762d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
772d4d9a4dSopenharmony_ci    if (sinkSvrRecipient_ == nullptr) {
782d4d9a4dSopenharmony_ci        DHLOGE("sinkSvrRecipient is nullptr.");
792d4d9a4dSopenharmony_ci        return;
802d4d9a4dSopenharmony_ci    }
812d4d9a4dSopenharmony_ci    remoteObject->AddDeathRecipient(sinkSvrRecipient_);
822d4d9a4dSopenharmony_ci    dInputSinkProxy_ = iface_cast<IDistributedSinkInput>(remoteObject);
832d4d9a4dSopenharmony_ci    DInputSAManager::GetInstance().SetDInputSinkProxy(remoteObject);
842d4d9a4dSopenharmony_ci    if ((dInputSinkProxy_ == nullptr) || (dInputSinkProxy_->AsObject() == nullptr)) {
852d4d9a4dSopenharmony_ci        DHLOGE("Faild to get input sink proxy.");
862d4d9a4dSopenharmony_ci        return;
872d4d9a4dSopenharmony_ci    }
882d4d9a4dSopenharmony_ci    DistributedInputClient::GetInstance().InitSink();
892d4d9a4dSopenharmony_ci    proxyConVar_.notify_all();
902d4d9a4dSopenharmony_ci}
912d4d9a4dSopenharmony_ci
922d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::ReleaseSink()
932d4d9a4dSopenharmony_ci{
942d4d9a4dSopenharmony_ci    return DistributedInputClient::GetInstance().ReleaseSink();
952d4d9a4dSopenharmony_ci}
962d4d9a4dSopenharmony_ci
972d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::SubscribeLocalHardware(const std::string &dhId, const std::string &params)
982d4d9a4dSopenharmony_ci{
992d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1002d4d9a4dSopenharmony_ci}
1012d4d9a4dSopenharmony_ci
1022d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::UnsubscribeLocalHardware(const std::string &dhId)
1032d4d9a4dSopenharmony_ci{
1042d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1052d4d9a4dSopenharmony_ci}
1062d4d9a4dSopenharmony_ci
1072d4d9a4dSopenharmony_civoid DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
1082d4d9a4dSopenharmony_ci    const OHOS::sptr<IRemoteObject> &remoteObject)
1092d4d9a4dSopenharmony_ci{
1102d4d9a4dSopenharmony_ci    currSystemAbilityId = systemAbilityId;
1112d4d9a4dSopenharmony_ci    currRemoteObject = remoteObject;
1122d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSinkHandler OnLoadSystemAbilitySuccess. systemAbilityId=%{public}d", systemAbilityId);
1132d4d9a4dSopenharmony_ci}
1142d4d9a4dSopenharmony_ci
1152d4d9a4dSopenharmony_civoid DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
1162d4d9a4dSopenharmony_ci{
1172d4d9a4dSopenharmony_ci    currSystemAbilityId = systemAbilityId;
1182d4d9a4dSopenharmony_ci    DHLOGE("DistributedInputSinkHandler OnLoadSystemAbilityFail. systemAbilityId=%{public}d", systemAbilityId);
1192d4d9a4dSopenharmony_ci}
1202d4d9a4dSopenharmony_ci
1212d4d9a4dSopenharmony_civoid DistributedInputSinkHandler::DInputSinkSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1222d4d9a4dSopenharmony_ci{
1232d4d9a4dSopenharmony_ci    if (remote == nullptr) {
1242d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteDied remote is nullptr.");
1252d4d9a4dSopenharmony_ci        return;
1262d4d9a4dSopenharmony_ci    }
1272d4d9a4dSopenharmony_ci    DHLOGI("DInputSinkSvrRecipient OnRemoteDied.");
1282d4d9a4dSopenharmony_ci    DistributedInputSinkHandler::GetInstance().OnRemoteSinkSvrDied(remote);
1292d4d9a4dSopenharmony_ci}
1302d4d9a4dSopenharmony_ci
1312d4d9a4dSopenharmony_civoid DistributedInputSinkHandler::OnRemoteSinkSvrDied(const wptr<IRemoteObject> &remote)
1322d4d9a4dSopenharmony_ci{
1332d4d9a4dSopenharmony_ci    DHLOGI("DInputSinkHandle OnRemoteSinkSvrDied.");
1342d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
1352d4d9a4dSopenharmony_ci    if (dInputSinkProxy_ == nullptr) {
1362d4d9a4dSopenharmony_ci        DHLOGE("dInputSinkProxy_ is nullptr.");
1372d4d9a4dSopenharmony_ci        return;
1382d4d9a4dSopenharmony_ci    }
1392d4d9a4dSopenharmony_ci    if (dInputSinkProxy_->AsObject() == nullptr) {
1402d4d9a4dSopenharmony_ci        DHLOGE("AsObject is nullptr.");
1412d4d9a4dSopenharmony_ci        return;
1422d4d9a4dSopenharmony_ci    }
1432d4d9a4dSopenharmony_ci    sptr<IRemoteObject> remoteObject = remote.promote();
1442d4d9a4dSopenharmony_ci    if (remoteObject == nullptr) {
1452d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteDied remote promoted failed");
1462d4d9a4dSopenharmony_ci        return;
1472d4d9a4dSopenharmony_ci    }
1482d4d9a4dSopenharmony_ci
1492d4d9a4dSopenharmony_ci    if (dInputSinkProxy_->AsObject() != remoteObject) {
1502d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteSinkSvrDied not found remote object.");
1512d4d9a4dSopenharmony_ci        return;
1522d4d9a4dSopenharmony_ci    }
1532d4d9a4dSopenharmony_ci    dInputSinkProxy_->AsObject()->RemoveDeathRecipient(sinkSvrRecipient_);
1542d4d9a4dSopenharmony_ci    dInputSinkProxy_ = nullptr;
1552d4d9a4dSopenharmony_ci}
1562d4d9a4dSopenharmony_ci
1572d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::RegisterPrivacyResources(std::shared_ptr<PrivacyResourcesListener> listener)
1582d4d9a4dSopenharmony_ci{
1592d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1602d4d9a4dSopenharmony_ci}
1612d4d9a4dSopenharmony_ci
1622d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::PauseDistributedHardware(const std::string &networkId)
1632d4d9a4dSopenharmony_ci{
1642d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1652d4d9a4dSopenharmony_ci}
1662d4d9a4dSopenharmony_ci
1672d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::ResumeDistributedHardware(const std::string &networkId)
1682d4d9a4dSopenharmony_ci{
1692d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1702d4d9a4dSopenharmony_ci}
1712d4d9a4dSopenharmony_ci
1722d4d9a4dSopenharmony_ciint32_t DistributedInputSinkHandler::StopDistributedHardware(const std::string &networkId)
1732d4d9a4dSopenharmony_ci{
1742d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1752d4d9a4dSopenharmony_ci}
1762d4d9a4dSopenharmony_ci
1772d4d9a4dSopenharmony_ciIDistributedHardwareSink *GetSinkHardwareHandler()
1782d4d9a4dSopenharmony_ci{
1792d4d9a4dSopenharmony_ci    return &DistributedInputSinkHandler::GetInstance();
1802d4d9a4dSopenharmony_ci}
1812d4d9a4dSopenharmony_ci} // namespace DistributedInput
1822d4d9a4dSopenharmony_ci} // namespace DistributedHardware
1832d4d9a4dSopenharmony_ci} // namespace OHOS
184