• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "distributed_input_sink_handler.h"
17 
18 #include "dinput_errcode.h"
19 #include "dinput_log.h"
20 #include "hisysevent_util.h"
21 #include "i_distributed_sink_input.h"
22 #include "load_d_input_sink_callback.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
27 IMPLEMENT_SINGLE_INSTANCE(DistributedInputSinkHandler);
28 
~DistributedInputSinkHandler()29 DistributedInputSinkHandler::~DistributedInputSinkHandler()
30 {
31     DHLOGI("~DistributedInputSinkHandler");
32 }
33 
InitSink(const std::string & params)34 int32_t DistributedInputSinkHandler::InitSink(const std::string &params)
35 {
36     DHLOGD("InitSource");
37     std::unique_lock<std::mutex> lock(proxyMutex_);
38     if (!DInputSAManager::GetInstance().HasDInputSinkProxy()) {
39         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40         if (!samgr) {
41             DHLOGE("Failed to get system ability mgr.");
42             return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
43         }
44         sptr<LoadDInputSinkCallback> loadCallback(new LoadDInputSinkCallback(params));
45         HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
46             "dinput sink LoadSystemAbility call");
47         int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, loadCallback);
48         if (ret != ERR_OK) {
49             DHLOGE("Failed to Load systemAbility, systemAbilityId:%d, ret code:%d",
50                    DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, ret);
51             return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
52         }
53     }
54 
55     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
56         [this]() { return (DInputSAManager::GetInstance().HasDInputSinkProxy()); });
57     if (!waitStatus) {
58         DHLOGE("dinput load sa timeout.");
59         return ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL;
60     }
61 
62     return DH_SUCCESS;
63 }
64 
FinishStartSA(const std::string & params,const sptr<IRemoteObject> & remoteObject)65 void DistributedInputSinkHandler::FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject)
66 {
67     DHLOGD("FinishStartSA");
68     std::unique_lock<std::mutex> lock(proxyMutex_);
69     DInputSAManager::GetInstance().SetDInputSinkProxy(remoteObject);
70     DistributedInputClient::GetInstance().InitSink();
71     proxyConVar_.notify_all();
72 }
73 
ReleaseSink()74 int32_t DistributedInputSinkHandler::ReleaseSink()
75 {
76     return DistributedInputClient::GetInstance().ReleaseSink();
77 }
78 
SubscribeLocalHardware(const std::string & dhId,const std::string & params)79 int32_t DistributedInputSinkHandler::SubscribeLocalHardware(const std::string &dhId, const std::string &params)
80 {
81     return DH_SUCCESS;
82 }
83 
UnsubscribeLocalHardware(const std::string & dhId)84 int32_t DistributedInputSinkHandler::UnsubscribeLocalHardware(const std::string &dhId)
85 {
86     return DH_SUCCESS;
87 }
88 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)89 void DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
90     const OHOS::sptr<IRemoteObject> &remoteObject)
91 {
92     currSystemAbilityId = systemAbilityId;
93     currRemoteObject = remoteObject;
94     DHLOGI("DistributedInputSinkHandler OnLoadSystemAbilitySuccess. systemAbilityId=%d", systemAbilityId);
95 }
96 
OnLoadSystemAbilityFail(int32_t systemAbilityId)97 void DistributedInputSinkHandler::SALoadSinkCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
98 {
99     currSystemAbilityId = systemAbilityId;
100     DHLOGE("DistributedInputSinkHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId);
101 }
102 
GetSinkHardwareHandler()103 IDistributedHardwareSink *GetSinkHardwareHandler()
104 {
105     return &DistributedInputSinkHandler::GetInstance();
106 }
107 } // namespace DistributedInput
108 } // namespace DistributedHardware
109 } // namespace OHOS
110