1 /*
2 * Copyright (c) 2022 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_adapter.h"
17
18 #include <algorithm>
19 #include <map>
20 #include <mutex>
21
22 #include "error_multimodal.h"
23 #include "timer_manager.h"
24
25 namespace OHOS {
26 namespace MMI {
27 using namespace DistributedHardware::DistributedInput;
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "DistributedInputAdapter"};
30 constexpr int32_t DEFAULT_DELAY_TIME = 4000;
31 constexpr int32_t RETRY_TIME = 2;
32 } // namespace
DistributedInputAdapter()33 DistributedInputAdapter::DistributedInputAdapter()
34 {
35 CALL_INFO_TRACE;
36 simulationEventListener_ = new (std::nothrow) SimulateEventCallbackImpl();
37 CHKPL(simulationEventListener_);
38 DistributedInputKit::RegisterSimulationEventListener(simulationEventListener_);
39 }
40
~DistributedInputAdapter()41 DistributedInputAdapter::~DistributedInputAdapter()
42 {
43 CALL_INFO_TRACE;
44 std::lock_guard<std::mutex> guard(adapterLock_);
45 DistributedInputKit::UnregisterSimulationEventListener(simulationEventListener_);
46 simulationEventListener_ = nullptr;
47 callbackMap_.clear();
48 }
49
IsNeedFilterOut(const std::string & deviceId,const BusinessEvent & event)50 bool DistributedInputAdapter::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event)
51 {
52 CALL_INFO_TRACE;
53 return DistributedInputKit::IsNeedFilterOut(deviceId, event);
54 }
55
StartRemoteInput(const std::string & deviceId,const std::vector<std::string> & dhIds,DInputCallback callback)56 int32_t DistributedInputAdapter::StartRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhIds,
57 DInputCallback callback)
58 {
59 CALL_INFO_TRACE;
60 sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StartDInputCallbackDHIds();
61 CHKPR(cb, ERROR_NULL_POINTER);
62 SaveCallback(CallbackType::StartDInputCallbackDHIds, callback);
63 return DistributedInputKit::StartRemoteInput(deviceId, dhIds, cb);
64 }
65
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,DInputCallback callback)66 int32_t DistributedInputAdapter::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
67 const uint32_t &inputTypes, DInputCallback callback)
68 {
69 CALL_INFO_TRACE;
70 sptr<IStartDInputCallback> cb = new (std::nothrow) StartDInputCallback();
71 CHKPR(cb, ERROR_NULL_POINTER);
72 SaveCallback(CallbackType::StartDInputCallback, callback);
73 return DistributedInputKit::StartRemoteInput(srcId, sinkId, inputTypes, cb);
74 }
75
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,DInputCallback callback)76 int32_t DistributedInputAdapter::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
77 const std::vector<std::string> &dhIds, DInputCallback callback)
78 {
79 CALL_INFO_TRACE;
80 sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StartDInputCallbackSink();
81 CHKPR(cb, ERROR_NULL_POINTER);
82 SaveCallback(CallbackType::StartDInputCallbackSink, callback);
83 return DistributedInputKit::StartRemoteInput(srcId, sinkId, dhIds, cb);
84 }
85
StopRemoteInput(const std::string & deviceId,const std::vector<std::string> & dhIds,DInputCallback callback)86 int32_t DistributedInputAdapter::StopRemoteInput(const std::string &deviceId, const std::vector<std::string> &dhIds,
87 DInputCallback callback)
88 {
89 CALL_INFO_TRACE;
90 sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StopDInputCallbackDHIds();
91 CHKPR(cb, ERROR_NULL_POINTER);
92 SaveCallback(CallbackType::StopDInputCallbackDHIds, callback);
93 return DistributedInputKit::StopRemoteInput(deviceId, dhIds, cb);
94 }
95
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,DInputCallback callback)96 int32_t DistributedInputAdapter::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
97 const uint32_t &inputTypes, DInputCallback callback)
98 {
99 CALL_INFO_TRACE;
100 sptr<IStopDInputCallback> cb = new (std::nothrow) StopDInputCallback();
101 CHKPR(cb, ERROR_NULL_POINTER);
102 SaveCallback(CallbackType::StopDInputCallback, callback);
103 return DistributedInputKit::StopRemoteInput(srcId, sinkId, inputTypes, cb);
104 }
105
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,DInputCallback callback)106 int32_t DistributedInputAdapter::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
107 const std::vector<std::string> &dhIds, DInputCallback callback)
108 {
109 CALL_INFO_TRACE;
110 sptr<IStartStopDInputsCallback> cb = new (std::nothrow) StopDInputCallbackSink();
111 CHKPR(cb, ERROR_NULL_POINTER);
112 SaveCallback(CallbackType::StopDInputCallbackSink, callback);
113 return DistributedInputKit::StopRemoteInput(srcId, sinkId, dhIds, cb);
114 }
115
PrepareRemoteInput(const std::string & srcId,const std::string & sinkId,DInputCallback callback)116 int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
117 DInputCallback callback)
118 {
119 CALL_INFO_TRACE;
120 sptr<IPrepareDInputCallback> cb = new (std::nothrow) PrepareStartDInputCallbackSink();
121 CHKPR(cb, ERROR_NULL_POINTER);
122 SaveCallback(CallbackType::PrepareStartDInputCallbackSink, callback);
123 return DistributedInputKit::PrepareRemoteInput(srcId, sinkId, cb);
124 }
125
UnPrepareRemoteInput(const std::string & srcId,const std::string & sinkId,DInputCallback callback)126 int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
127 DInputCallback callback)
128 {
129 CALL_INFO_TRACE;
130 sptr<IUnprepareDInputCallback> cb = new (std::nothrow) UnPrepareStopDInputCallbackSink();
131 CHKPR(cb, ERROR_NULL_POINTER);
132 SaveCallback(CallbackType::UnPrepareStopDInputCallbackSink, callback);
133 return DistributedInputKit::UnprepareRemoteInput(srcId, sinkId, cb);
134 }
135
PrepareRemoteInput(const std::string & deviceId,DInputCallback callback)136 int32_t DistributedInputAdapter::PrepareRemoteInput(const std::string &deviceId, DInputCallback callback)
137 {
138 CALL_INFO_TRACE;
139 sptr<IPrepareDInputCallback> cb = new (std::nothrow) PrepareStartDInputCallback();
140 CHKPR(cb, ERROR_NULL_POINTER);
141 SaveCallback(CallbackType::PrepareStartDInputCallback, callback);
142 return DistributedInputKit::PrepareRemoteInput(deviceId, cb);
143 }
144
UnPrepareRemoteInput(const std::string & deviceId,DInputCallback callback)145 int32_t DistributedInputAdapter::UnPrepareRemoteInput(const std::string &deviceId, DInputCallback callback)
146 {
147 CALL_INFO_TRACE;
148 sptr<IUnprepareDInputCallback> cb = new (std::nothrow) UnPrepareStopDInputCallback();
149 CHKPR(cb, ERROR_NULL_POINTER);
150 SaveCallback(CallbackType::UnPrepareStopDInputCallback, callback);
151 return DistributedInputKit::UnprepareRemoteInput(deviceId, cb);
152 }
153
RegisterEventCallback(SimulateEventCallback callback)154 int32_t DistributedInputAdapter::RegisterEventCallback(SimulateEventCallback callback)
155 {
156 std::lock_guard<std::mutex> guard(adapterLock_);
157 CHKPR(callback, RET_ERR);
158 SimulateEventCallback_ = callback;
159 return RET_OK;
160 }
UnregisterEventCallback(SimulateEventCallback callback)161 int32_t DistributedInputAdapter::UnregisterEventCallback(SimulateEventCallback callback)
162 {
163 std::lock_guard<std::mutex> guard(adapterLock_);
164 CHKPR(callback, RET_ERR);
165 SimulateEventCallback_ = nullptr;
166 return RET_OK;
167 }
168
SaveCallback(CallbackType type,DInputCallback callback)169 void DistributedInputAdapter::SaveCallback(CallbackType type, DInputCallback callback)
170 {
171 std::lock_guard<std::mutex> guard(adapterLock_);
172 CHKPV(callback);
173 callbackMap_[type] = callback;
174 AddTimer(type);
175 }
176
AddTimer(const CallbackType & type)177 void DistributedInputAdapter::AddTimer(const CallbackType &type)
178 {
179 MMI_HILOGD("AddTimer type:%{public}d", type);
180 int32_t timerId = TimerMgr->AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, type]() {
181 if ((callbackMap_.find(type) == callbackMap_.end()) || (watchingMap_.find(type) == watchingMap_.end())) {
182 MMI_HILOGE("Callback or watching is not exist");
183 return;
184 }
185 if (watchingMap_[type].times == 0) {
186 MMI_HILOGI("It will be retry to call callback next time");
187 watchingMap_[type].times++;
188 return;
189 }
190 callbackMap_[type](false);
191 callbackMap_.erase(type);
192 });
193 if (timerId < 0) {
194 MMI_HILOGE("Add timer failed timeId:%{public}d", timerId);
195 return;
196 }
197 watchingMap_[type].timerId = timerId;
198 watchingMap_[type].times = 0;
199 }
200
RemoveTimer(const CallbackType & type)201 void DistributedInputAdapter::RemoveTimer(const CallbackType &type)
202 {
203 MMI_HILOGD("RemoveTimer type:%{public}d", type);
204 if (watchingMap_.find(type) != watchingMap_.end()) {
205 TimerMgr->RemoveTimer(watchingMap_[type].timerId);
206 watchingMap_.erase(type);
207 }
208 }
209
ProcessDInputCallback(CallbackType type,int32_t status)210 void DistributedInputAdapter::ProcessDInputCallback(CallbackType type, int32_t status)
211 {
212 CALL_INFO_TRACE;
213 std::lock_guard<std::mutex> guard(adapterLock_);
214 RemoveTimer(type);
215 auto it = callbackMap_.find(type);
216 if (it == callbackMap_.end()) {
217 MMI_HILOGI("Dinput callback not exist");
218 return;
219 }
220 it->second(status == RET_OK);
221 callbackMap_.erase(it);
222 }
223
OnSimulationEvent(uint32_t type,uint32_t code,int32_t value)224 void DistributedInputAdapter::OnSimulationEvent(uint32_t type, uint32_t code, int32_t value)
225 {
226 std::lock_guard<std::mutex> guard(adapterLock_);
227 CHKPV(SimulateEventCallback_);
228 SimulateEventCallback_(type, code, value);
229 }
230
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)231 void DistributedInputAdapter::StartDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes,
232 const int32_t &status)
233 {
234 DistributedAdapter->ProcessDInputCallback(CallbackType::StartDInputCallback, status);
235 }
236
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)237 void DistributedInputAdapter::StopDInputCallback::OnResult(const std::string &devId, const uint32_t &inputTypes,
238 const int32_t &status)
239 {
240 DistributedAdapter->ProcessDInputCallback(CallbackType::StopDInputCallback, status);
241 }
242
OnResultDhids(const std::string & devId,const int32_t & status)243 void DistributedInputAdapter::StartDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status)
244 {
245 DistributedAdapter->ProcessDInputCallback(CallbackType::StartDInputCallbackDHIds, status);
246 }
247
OnResultDhids(const std::string & devId,const int32_t & status)248 void DistributedInputAdapter::StopDInputCallbackDHIds::OnResultDhids(const std::string &devId, const int32_t &status)
249 {
250 DistributedAdapter->ProcessDInputCallback(CallbackType::StopDInputCallbackDHIds, status);
251 }
252
OnResultDhids(const std::string & devId,const int32_t & status)253 void DistributedInputAdapter::StartDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
254 {
255 DistributedAdapter->ProcessDInputCallback(CallbackType::StartDInputCallbackSink, status);
256 }
257
OnResultDhids(const std::string & devId,const int32_t & status)258 void DistributedInputAdapter::StopDInputCallbackSink::OnResultDhids(const std::string &devId, const int32_t &status)
259 {
260 DistributedAdapter->ProcessDInputCallback(CallbackType::StopDInputCallbackSink, status);
261 }
262
OnResult(const std::string & devId,const int32_t & status)263 void DistributedInputAdapter::PrepareStartDInputCallback::OnResult(const std::string &devId, const int32_t &status)
264 {
265 DistributedAdapter->ProcessDInputCallback(CallbackType::PrepareStartDInputCallback, status);
266 }
267
OnResult(const std::string & devId,const int32_t & status)268 void DistributedInputAdapter::UnPrepareStopDInputCallback::OnResult(const std::string &devId, const int32_t &status)
269 {
270 DistributedAdapter->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallback, status);
271 }
272
OnResult(const std::string & devId,const int32_t & status)273 void DistributedInputAdapter::PrepareStartDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
274 {
275 DistributedAdapter->ProcessDInputCallback(CallbackType::PrepareStartDInputCallbackSink, status);
276 }
277
OnResult(const std::string & devId,const int32_t & status)278 void DistributedInputAdapter::UnPrepareStopDInputCallbackSink::OnResult(const std::string &devId, const int32_t &status)
279 {
280 DistributedAdapter->ProcessDInputCallback(CallbackType::UnPrepareStopDInputCallbackSink, status);
281 }
282
OnSimulationEvent(uint32_t type,uint32_t code,int32_t value)283 int32_t DistributedInputAdapter::SimulateEventCallbackImpl::OnSimulationEvent(uint32_t type, uint32_t code,
284 int32_t value)
285 {
286 DistributedAdapter->OnSimulationEvent(type, code, value);
287 return RET_OK;
288 }
289 } // namespace MMI
290 } // namespace OHOS
291