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_source_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_source_input.h"
222d4d9a4dSopenharmony_ci#include "load_d_input_source_callback.h"
232d4d9a4dSopenharmony_ci
242d4d9a4dSopenharmony_cinamespace OHOS {
252d4d9a4dSopenharmony_cinamespace DistributedHardware {
262d4d9a4dSopenharmony_cinamespace DistributedInput {
272d4d9a4dSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DistributedInputSourceHandler);
282d4d9a4dSopenharmony_ci
292d4d9a4dSopenharmony_ciDistributedInputSourceHandler::DistributedInputSourceHandler()
302d4d9a4dSopenharmony_ci{
312d4d9a4dSopenharmony_ci    DHLOGI("DInputSourceHandler construct.");
322d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
332d4d9a4dSopenharmony_ci    if (sourceSvrRecipient_ == nullptr) {
342d4d9a4dSopenharmony_ci        sourceSvrRecipient_ = new (std::nothrow) DInputSourceSvrRecipient();
352d4d9a4dSopenharmony_ci    }
362d4d9a4dSopenharmony_ci
372d4d9a4dSopenharmony_ci    if (dInputSourceCallback_ == nullptr) {
382d4d9a4dSopenharmony_ci        dInputSourceCallback_ = new (std::nothrow) LoadDInputSourceCallback();
392d4d9a4dSopenharmony_ci    }
402d4d9a4dSopenharmony_ci}
412d4d9a4dSopenharmony_ci
422d4d9a4dSopenharmony_ciint32_t DistributedInputSourceHandler::InitSource(const std::string &params)
432d4d9a4dSopenharmony_ci{
442d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSourceHandler InitSource begin");
452d4d9a4dSopenharmony_ci    std::unique_lock<std::mutex> lock(proxyMutex_);
462d4d9a4dSopenharmony_ci    sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
472d4d9a4dSopenharmony_ci    if (!samgr) {
482d4d9a4dSopenharmony_ci        DHLOGE("Failed to get system ability mgr.");
492d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
502d4d9a4dSopenharmony_ci    }
512d4d9a4dSopenharmony_ci    sptr<LoadDInputSourceCallback> loadCallback(new LoadDInputSourceCallback(params));
522d4d9a4dSopenharmony_ci    HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
532d4d9a4dSopenharmony_ci        "dinput init source sa start.");
542d4d9a4dSopenharmony_ci    int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, loadCallback);
552d4d9a4dSopenharmony_ci    if (ret != ERR_OK) {
562d4d9a4dSopenharmony_ci        DHLOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
572d4d9a4dSopenharmony_ci            DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, ret);
582d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
592d4d9a4dSopenharmony_ci    }
602d4d9a4dSopenharmony_ci
612d4d9a4dSopenharmony_ci    auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
622d4d9a4dSopenharmony_ci        [this]() { return (DInputSAManager::GetInstance().HasDInputSourceProxy()); });
632d4d9a4dSopenharmony_ci    if (!waitStatus) {
642d4d9a4dSopenharmony_ci        DHLOGE("dinput load source sa timeout.");
652d4d9a4dSopenharmony_ci        return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
662d4d9a4dSopenharmony_ci    }
672d4d9a4dSopenharmony_ci
682d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSourceHandler InitSource end");
692d4d9a4dSopenharmony_ci    return DH_SUCCESS;
702d4d9a4dSopenharmony_ci}
712d4d9a4dSopenharmony_ci
722d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject)
732d4d9a4dSopenharmony_ci{
742d4d9a4dSopenharmony_ci    DHLOGI("DInputSourceHandle FinishStartSA");
752d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
762d4d9a4dSopenharmony_ci    if (sourceSvrRecipient_ == nullptr) {
772d4d9a4dSopenharmony_ci        DHLOGE("sourceSvrRecipient is nullptr.");
782d4d9a4dSopenharmony_ci        return;
792d4d9a4dSopenharmony_ci    }
802d4d9a4dSopenharmony_ci    remoteObject->AddDeathRecipient(sourceSvrRecipient_);
812d4d9a4dSopenharmony_ci    dInputSourceProxy_ = iface_cast<IDistributedSourceInput>(remoteObject);
822d4d9a4dSopenharmony_ci    DInputSAManager::GetInstance().SetDInputSourceProxy(remoteObject);
832d4d9a4dSopenharmony_ci    if ((dInputSourceProxy_ == nullptr) || (dInputSourceProxy_->AsObject() == nullptr)) {
842d4d9a4dSopenharmony_ci        DHLOGE("Faild to get input source proxy.");
852d4d9a4dSopenharmony_ci        return;
862d4d9a4dSopenharmony_ci    }
872d4d9a4dSopenharmony_ci    DistributedInputClient::GetInstance().InitSource();
882d4d9a4dSopenharmony_ci    proxyConVar_.notify_all();
892d4d9a4dSopenharmony_ci}
902d4d9a4dSopenharmony_ci
912d4d9a4dSopenharmony_ciint32_t DistributedInputSourceHandler::ReleaseSource()
922d4d9a4dSopenharmony_ci{
932d4d9a4dSopenharmony_ci    return DistributedInputClient::GetInstance().ReleaseSource();
942d4d9a4dSopenharmony_ci}
952d4d9a4dSopenharmony_ci
962d4d9a4dSopenharmony_ciint32_t DistributedInputSourceHandler::RegisterDistributedHardware(const std::string &devId,
972d4d9a4dSopenharmony_ci    const std::string &dhId, const EnableParam &param, std::shared_ptr<RegisterCallback> callback)
982d4d9a4dSopenharmony_ci{
992d4d9a4dSopenharmony_ci    return DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, param.sinkAttrs, callback);
1002d4d9a4dSopenharmony_ci}
1012d4d9a4dSopenharmony_ci
1022d4d9a4dSopenharmony_ciint32_t DistributedInputSourceHandler::UnregisterDistributedHardware(const std::string &devId,
1032d4d9a4dSopenharmony_ci    const std::string &dhId, std::shared_ptr<UnregisterCallback> callback)
1042d4d9a4dSopenharmony_ci{
1052d4d9a4dSopenharmony_ci    return DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback);
1062d4d9a4dSopenharmony_ci}
1072d4d9a4dSopenharmony_ci
1082d4d9a4dSopenharmony_ciint32_t DistributedInputSourceHandler::ConfigDistributedHardware(const std::string &devId,
1092d4d9a4dSopenharmony_ci    const std::string &dhId, const std::string &key, const std::string &value)
1102d4d9a4dSopenharmony_ci{
1112d4d9a4dSopenharmony_ci    return DH_SUCCESS;
1122d4d9a4dSopenharmony_ci}
1132d4d9a4dSopenharmony_ci
1142d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::RegisterDistributedHardwareStateListener(
1152d4d9a4dSopenharmony_ci    std::shared_ptr<DistributedHardwareStateListener> listener)
1162d4d9a4dSopenharmony_ci{
1172d4d9a4dSopenharmony_ci    (void)listener;
1182d4d9a4dSopenharmony_ci}
1192d4d9a4dSopenharmony_ci
1202d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::UnregisterDistributedHardwareStateListener()
1212d4d9a4dSopenharmony_ci{
1222d4d9a4dSopenharmony_ci}
1232d4d9a4dSopenharmony_ci
1242d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener)
1252d4d9a4dSopenharmony_ci{
1262d4d9a4dSopenharmony_ci    (void)listener;
1272d4d9a4dSopenharmony_ci}
1282d4d9a4dSopenharmony_ci
1292d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::UnregisterDataSyncTriggerListener()
1302d4d9a4dSopenharmony_ci{
1312d4d9a4dSopenharmony_ci}
1322d4d9a4dSopenharmony_ci
1332d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
1342d4d9a4dSopenharmony_ci    const OHOS::sptr<IRemoteObject> &remoteObject)
1352d4d9a4dSopenharmony_ci{
1362d4d9a4dSopenharmony_ci    currSystemAbilityId = systemAbilityId;
1372d4d9a4dSopenharmony_ci    currRemoteObject = remoteObject;
1382d4d9a4dSopenharmony_ci    DHLOGI("DistributedInputSourceHandler OnLoadSystemAbilitySuccess. systemAbilityId=%{public}d", systemAbilityId);
1392d4d9a4dSopenharmony_ci}
1402d4d9a4dSopenharmony_ci
1412d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
1422d4d9a4dSopenharmony_ci{
1432d4d9a4dSopenharmony_ci    currSystemAbilityId = systemAbilityId;
1442d4d9a4dSopenharmony_ci    DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%{public}d", systemAbilityId);
1452d4d9a4dSopenharmony_ci}
1462d4d9a4dSopenharmony_ci
1472d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::DInputSourceSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1482d4d9a4dSopenharmony_ci{
1492d4d9a4dSopenharmony_ci    if (remote == nullptr) {
1502d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteDied remote is nullptr.");
1512d4d9a4dSopenharmony_ci        return;
1522d4d9a4dSopenharmony_ci    }
1532d4d9a4dSopenharmony_ci    DHLOGI("DInputSourceSvrRecipient OnRemoteDied.");
1542d4d9a4dSopenharmony_ci    DistributedInputSourceHandler::GetInstance().OnRemoteSourceSvrDied(remote);
1552d4d9a4dSopenharmony_ci}
1562d4d9a4dSopenharmony_ci
1572d4d9a4dSopenharmony_civoid DistributedInputSourceHandler::OnRemoteSourceSvrDied(const wptr<IRemoteObject> &remote)
1582d4d9a4dSopenharmony_ci{
1592d4d9a4dSopenharmony_ci    DHLOGI("OnRemoteSourceSvrDied.");
1602d4d9a4dSopenharmony_ci    std::lock_guard<std::mutex> lock(proxyMutex_);
1612d4d9a4dSopenharmony_ci    if (dInputSourceProxy_ == nullptr) {
1622d4d9a4dSopenharmony_ci        DHLOGE("dInputSourceProxy is nullptr.");
1632d4d9a4dSopenharmony_ci        return;
1642d4d9a4dSopenharmony_ci    }
1652d4d9a4dSopenharmony_ci    if (dInputSourceProxy_->AsObject() == nullptr) {
1662d4d9a4dSopenharmony_ci        DHLOGE("AsObject is nullptr.");
1672d4d9a4dSopenharmony_ci        return;
1682d4d9a4dSopenharmony_ci    }
1692d4d9a4dSopenharmony_ci    sptr<IRemoteObject> remoteObject = remote.promote();
1702d4d9a4dSopenharmony_ci    if (remoteObject == nullptr) {
1712d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteDied remote promoted failed");
1722d4d9a4dSopenharmony_ci        return;
1732d4d9a4dSopenharmony_ci    }
1742d4d9a4dSopenharmony_ci
1752d4d9a4dSopenharmony_ci    if (dInputSourceProxy_->AsObject() != remoteObject) {
1762d4d9a4dSopenharmony_ci        DHLOGE("OnRemoteSourceSvrDied not found remote object.");
1772d4d9a4dSopenharmony_ci        return;
1782d4d9a4dSopenharmony_ci    }
1792d4d9a4dSopenharmony_ci    dInputSourceProxy_->AsObject()->RemoveDeathRecipient(sourceSvrRecipient_);
1802d4d9a4dSopenharmony_ci    dInputSourceProxy_ = nullptr;
1812d4d9a4dSopenharmony_ci}
1822d4d9a4dSopenharmony_ci
1832d4d9a4dSopenharmony_ciIDistributedHardwareSource *GetSourceHardwareHandler()
1842d4d9a4dSopenharmony_ci{
1852d4d9a4dSopenharmony_ci    return &DistributedInputSourceHandler::GetInstance();
1862d4d9a4dSopenharmony_ci}
1872d4d9a4dSopenharmony_ci} // namespace DistributedInput
1882d4d9a4dSopenharmony_ci} // namespace DistributedHardware
1892d4d9a4dSopenharmony_ci} // namespace OHOS
190