1 /*
2 * Copyright (c) 2021-2024 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 DHLOGI("DInputSourceHandler construct.");
32 std::lock_guard<std::mutex> lock(proxyMutex_);
33 if (sourceSvrRecipient_ == nullptr) {
34 sourceSvrRecipient_ = new (std::nothrow) DInputSourceSvrRecipient();
35 }
36
37 if (dInputSourceCallback_ == nullptr) {
38 dInputSourceCallback_ = new (std::nothrow) LoadDInputSourceCallback();
39 }
40 }
41
InitSource(const std::string & params)42 int32_t DistributedInputSourceHandler::InitSource(const std::string ¶ms)
43 {
44 DHLOGI("DistributedInputSourceHandler InitSource begin");
45 std::unique_lock<std::mutex> lock(proxyMutex_);
46 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47 if (!samgr) {
48 DHLOGE("Failed to get system ability mgr.");
49 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
50 }
51 sptr<LoadDInputSourceCallback> loadCallback(new LoadDInputSourceCallback(params));
52 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_INIT,
53 "dinput init source sa start.");
54 int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, loadCallback);
55 if (ret != ERR_OK) {
56 DHLOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
57 DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, ret);
58 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
59 }
60
61 auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(INPUT_LOAD_SA_TIMEOUT_MS),
62 [this]() { return (DInputSAManager::GetInstance().HasDInputSourceProxy()); });
63 if (!waitStatus) {
64 DHLOGE("dinput load source sa timeout.");
65 return ERR_DH_INPUT_SINK_HANDLER_INIT_SOURCE_SA_FAIL;
66 }
67
68 DHLOGI("DistributedInputSourceHandler InitSource end");
69 return DH_SUCCESS;
70 }
71
FinishStartSA(const std::string & params,const sptr<IRemoteObject> & remoteObject)72 void DistributedInputSourceHandler::FinishStartSA(const std::string ¶ms, const sptr<IRemoteObject> &remoteObject)
73 {
74 DHLOGI("DInputSourceHandle FinishStartSA");
75 std::lock_guard<std::mutex> lock(proxyMutex_);
76 if (sourceSvrRecipient_ == nullptr) {
77 DHLOGE("sourceSvrRecipient is nullptr.");
78 return;
79 }
80 remoteObject->AddDeathRecipient(sourceSvrRecipient_);
81 dInputSourceProxy_ = iface_cast<IDistributedSourceInput>(remoteObject);
82 DInputSAManager::GetInstance().SetDInputSourceProxy(remoteObject);
83 if ((dInputSourceProxy_ == nullptr) || (dInputSourceProxy_->AsObject() == nullptr)) {
84 DHLOGE("Faild to get input source proxy.");
85 return;
86 }
87 DistributedInputClient::GetInstance().InitSource();
88 proxyConVar_.notify_all();
89 }
90
ReleaseSource()91 int32_t DistributedInputSourceHandler::ReleaseSource()
92 {
93 return DistributedInputClient::GetInstance().ReleaseSource();
94 }
95
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,std::shared_ptr<RegisterCallback> callback)96 int32_t DistributedInputSourceHandler::RegisterDistributedHardware(const std::string &devId,
97 const std::string &dhId, const EnableParam ¶m, std::shared_ptr<RegisterCallback> callback)
98 {
99 return DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, param.sinkAttrs, callback);
100 }
101
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,std::shared_ptr<UnregisterCallback> callback)102 int32_t DistributedInputSourceHandler::UnregisterDistributedHardware(const std::string &devId,
103 const std::string &dhId, std::shared_ptr<UnregisterCallback> callback)
104 {
105 return DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback);
106 }
107
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)108 int32_t DistributedInputSourceHandler::ConfigDistributedHardware(const std::string &devId,
109 const std::string &dhId, const std::string &key, const std::string &value)
110 {
111 return DH_SUCCESS;
112 }
113
RegisterDistributedHardwareStateListener(std::shared_ptr<DistributedHardwareStateListener> listener)114 void DistributedInputSourceHandler::RegisterDistributedHardwareStateListener(
115 std::shared_ptr<DistributedHardwareStateListener> listener)
116 {
117 (void)listener;
118 }
119
UnregisterDistributedHardwareStateListener()120 void DistributedInputSourceHandler::UnregisterDistributedHardwareStateListener()
121 {
122 }
123
RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener)124 void DistributedInputSourceHandler::RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener)
125 {
126 (void)listener;
127 }
128
UnregisterDataSyncTriggerListener()129 void DistributedInputSourceHandler::UnregisterDataSyncTriggerListener()
130 {
131 }
132
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)133 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
134 const OHOS::sptr<IRemoteObject> &remoteObject)
135 {
136 currSystemAbilityId = systemAbilityId;
137 currRemoteObject = remoteObject;
138 DHLOGI("DistributedInputSourceHandler OnLoadSystemAbilitySuccess. systemAbilityId=%{public}d", systemAbilityId);
139 }
140
OnLoadSystemAbilityFail(int32_t systemAbilityId)141 void DistributedInputSourceHandler::SALoadSourceCb::OnLoadSystemAbilityFail(int32_t systemAbilityId)
142 {
143 currSystemAbilityId = systemAbilityId;
144 DHLOGE("DistributedInputSourceHandler OnLoadSystemAbilityFail. systemAbilityId=%{public}d", systemAbilityId);
145 }
146
OnRemoteDied(const wptr<IRemoteObject> & remote)147 void DistributedInputSourceHandler::DInputSourceSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
148 {
149 if (remote == nullptr) {
150 DHLOGE("OnRemoteDied remote is nullptr.");
151 return;
152 }
153 DHLOGI("DInputSourceSvrRecipient OnRemoteDied.");
154 DistributedInputSourceHandler::GetInstance().OnRemoteSourceSvrDied(remote);
155 }
156
OnRemoteSourceSvrDied(const wptr<IRemoteObject> & remote)157 void DistributedInputSourceHandler::OnRemoteSourceSvrDied(const wptr<IRemoteObject> &remote)
158 {
159 DHLOGI("OnRemoteSourceSvrDied.");
160 std::lock_guard<std::mutex> lock(proxyMutex_);
161 if (dInputSourceProxy_ == nullptr) {
162 DHLOGE("dInputSourceProxy is nullptr.");
163 return;
164 }
165 if (dInputSourceProxy_->AsObject() == nullptr) {
166 DHLOGE("AsObject is nullptr.");
167 return;
168 }
169 sptr<IRemoteObject> remoteObject = remote.promote();
170 if (remoteObject == nullptr) {
171 DHLOGE("OnRemoteDied remote promoted failed");
172 return;
173 }
174
175 if (dInputSourceProxy_->AsObject() != remoteObject) {
176 DHLOGE("OnRemoteSourceSvrDied not found remote object.");
177 return;
178 }
179 dInputSourceProxy_->AsObject()->RemoveDeathRecipient(sourceSvrRecipient_);
180 dInputSourceProxy_ = nullptr;
181 }
182
GetSourceHardwareHandler()183 IDistributedHardwareSource *GetSourceHardwareHandler()
184 {
185 return &DistributedInputSourceHandler::GetInstance();
186 }
187 } // namespace DistributedInput
188 } // namespace DistributedHardware
189 } // namespace OHOS
190