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_source_handler.h"
17
18 #include "dinput_errcode.h"
19 #include "dinput_log.h"
20 #include "hisysevent_util.h"
21 #include "i_distributed_source_input.h"
22 #include "load_d_input_source_callback.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
27 IMPLEMENT_SINGLE_INSTANCE(DistributedInputSourceHandler);
28
~DistributedInputSourceHandler()29 DistributedInputSourceHandler::~DistributedInputSourceHandler()
30 {
31 }
32
InitSource(const std::string & params)33 int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms)
34 {
35 DHLOGD("InitSource");
36 std::unique_lock<std::mutex> lock(proxyMutex_);
37 if (!DInputSAManager::GetInstance().HasDInputSourceProxy()) {
38 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 if (!samgr) {
40 DHLOGE("Failed to get system ability mgr.");
41 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
42 }
43 sptr<LoadDInputSourceCallback> loadCallback(new LoadDInputSourceCallback(params));
44 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
45 "dinput init source sa start.");
46 int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, loadCallback);
47 if (ret != ERR_OK) {
48 DHLOGE("Failed to Load systemAbility, systemAbilityId:%d, ret code:%d",
49 DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, ret);
50 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
51 }
52 }
53
54 auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
55 [this]() { return (DInputSAManager::GetInstance().HasDInputSourceProxy()); });
56 if (!waitStatus) {
57 DHLOGE("dinput load sa timeout.");
58 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
59 }
60
61 return DH_SUCCESS;
62 }
63
FinishStartSA(const std::string & params,const sptr<IRemoteObject> & remoteObject)64 void DistributedInputSourceHandler::FinishStartSA(const std::string ¶ms, const sptr<IRemoteObject> &remoteObject)
65 {
66 DHLOGD("FinishStartSA");
67 std::unique_lock<std::mutex> lock(proxyMutex_);
68 DInputSAManager::GetInstance().SetDInputSourceProxy(remoteObject);
69 DistributedInputClient::GetInstance().InitSource();
70 proxyConVar_.notify_all();
71 }
72
ReleaseSource()73 int32_t DistributedInputSourceHandler::ReleaseSource()
74 {
75 return DistributedInputClient::GetInstance().ReleaseSource();
76 }
77
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,std::shared_ptr<RegisterCallback> callback)78 int32_t DistributedInputSourceHandler::RegisterDistributedHardware(const std::string &devId,
79 const std::string &dhId, const EnableParam ¶m, std::shared_ptr<RegisterCallback> callback)
80 {
81 return DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, param.attrs, callback);
82 }
83
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,std::shared_ptr<UnregisterCallback> callback)84 int32_t DistributedInputSourceHandler::UnregisterDistributedHardware(const std::string &devId,
85 const std::string &dhId, std::shared_ptr<UnregisterCallback> callback)
86 {
87 return DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback);
88 }
89
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)90 int32_t DistributedInputSourceHandler::ConfigDistributedHardware(const std::string &devId,
91 const std::string &dhId, const std::string &key, const std::string &value)
92 {
93 return DH_SUCCESS;
94 }
95
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)96 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
97 const OHOS::sptr<IRemoteObject> &remoteObject)
98 {
99 currSystemAbilityId = systemAbilityId;
100 currRemoteObject = remoteObject;
101 DHLOGI("DistributedInputSourceHandler OnLoadSystemAbilitySuccess. systemAbilityId=%d", systemAbilityId);
102 }
103
OnLoadSystemAbilityFail(int32_t systemAbilityId)104 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
105 {
106 currSystemAbilityId = systemAbilityId;
107 DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%d", systemAbilityId);
108 }
109
GetSourceHardwareHandler()110 IDistributedHardwareSource *GetSourceHardwareHandler()
111 {
112 return &DistributedInputSourceHandler::GetInstance();
113 }
114 } // namespace DistributedInput
115 } // namespace DistributedHardware
116 } // namespace OHOS
117