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_sink_transport.h"
17
18 #include <cinttypes>
19
20 #include "linux/input.h"
21
22 #include "distributed_hardware_fwk_kit.h"
23 #include "securec.h"
24
25 #include "constants_dinput.h"
26 #include "dinput_context.h"
27 #include "dinput_errcode.h"
28 #include "dinput_log.h"
29 #include "dinput_softbus_define.h"
30 #include "dinput_utils_tool.h"
31 #include "hidumper.h"
32 #include "softbus_bus_center.h"
33 #include "xcollie/watchdog.h"
34
35 #include "distributed_input_transport_base.h"
36
37 namespace OHOS {
38 namespace DistributedHardware {
39 namespace DistributedInput {
40 namespace {
41 // each time, we send msg batch with MAX 20 events.
42 constexpr int32_t MSG_BTACH_MAX_SIZE = 20;
43 }
DistributedInputSinkTransport()44 DistributedInputSinkTransport::DistributedInputSinkTransport() : mySessionName_("")
45 {
46 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
47 eventHandler_ = std::make_shared<DistributedInputSinkTransport::DInputSinkEventHandler>(runner);
48
49 if (OHOS::HiviewDFX::Watchdog::GetInstance().AddThread("dinputwatchdog", eventHandler_,
50 WATCHDOG_INTERVAL_TIME_MS)) {
51 DHLOGE("HiviewDFX::Watchdog::GetInstance().AddThread() Failed.");
52 }
53 DHLOGI("DistributedInputSinkTransport ctor.");
54 }
55
~DistributedInputSinkTransport()56 DistributedInputSinkTransport::~DistributedInputSinkTransport()
57 {
58 DHLOGI("DistributedInputSinkTransport dtor.");
59 }
60
DInputSinkEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)61 DistributedInputSinkTransport::DInputSinkEventHandler::DInputSinkEventHandler(
62 const std::shared_ptr<AppExecFwk::EventRunner> &runner) : AppExecFwk::EventHandler(runner)
63 {
64 }
65
GetInstance()66 DistributedInputSinkTransport &DistributedInputSinkTransport::GetInstance()
67 {
68 static DistributedInputSinkTransport instance;
69 return instance;
70 }
71
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)72 void DistributedInputSinkTransport::DInputSinkEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
73 {
74 if (event == nullptr) {
75 DHLOGE("event is nullptr.");
76 return;
77 }
78 EHandlerMsgType eventId = static_cast<EHandlerMsgType>(event->GetInnerEventId());
79 switch (eventId) {
80 case EHandlerMsgType::DINPUT_SINK_EVENT_HANDLER_MSG: {
81 std::shared_ptr<nlohmann::json> innerMsg = event->GetSharedObject<nlohmann::json>();
82 if (innerMsg == nullptr) {
83 DHLOGE("innerMsg is null.");
84 break;
85 }
86 nlohmann::json jsonStr;
87 jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_BODY_DATA;
88 jsonStr[DINPUT_SOFTBUS_KEY_INPUT_DATA] = innerMsg->dump();
89 std::string smsg = jsonStr.dump();
90 RecordEventLog(innerMsg);
91 int32_t sessionId = DistributedInputSinkSwitch::GetInstance().GetSwitchOpenedSession();
92 if (sessionId > 0) {
93 DistributedInputSinkTransport::GetInstance().SendMessage(sessionId, smsg);
94 } else {
95 DHLOGE("ProcessEvent can't send input data, because no session switch on.");
96 }
97 break;
98 }
99 default:
100 DHLOGE("ProcessEvent error, because eventId is unkonwn.");
101 break;
102 }
103 }
104
Init()105 int32_t DistributedInputSinkTransport::Init()
106 {
107 DHLOGI("Init");
108
109 int32_t ret = DistributedInputTransportBase::GetInstance().Init();
110 if (ret != DH_SUCCESS) {
111 DHLOGE("Init Sink Transport Failed");
112 return ret;
113 }
114
115 statuslistener_ = std::make_shared<DInputTransbaseSinkListener>(this);
116 DistributedInputTransportBase::GetInstance().RegisterSinkHandleSessionCallback(statuslistener_);
117 return DH_SUCCESS;
118 }
119
GetEventHandler()120 std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> DistributedInputSinkTransport::GetEventHandler()
121 {
122 DHLOGI("GetEventHandler");
123 return eventHandler_;
124 }
125
RegistSinkRespCallback(std::shared_ptr<DInputSinkTransCallback> callback)126 void DistributedInputSinkTransport::RegistSinkRespCallback(std::shared_ptr<DInputSinkTransCallback> callback)
127 {
128 DHLOGI("RegistSinkRespCallback");
129 callback_ = callback;
130 }
131
RespPrepareRemoteInput(const int32_t sessionId,std::string & smsg)132 int32_t DistributedInputSinkTransport::RespPrepareRemoteInput(
133 const int32_t sessionId, std::string &smsg)
134 {
135 if (sessionId > 0) {
136 DHLOGI("RespPrepareRemoteInput sessionId: %{public}d, smsg:%{public}s.", sessionId, SetAnonyId(smsg).c_str());
137 int32_t ret = SendMessage(sessionId, smsg);
138 if (ret != DH_SUCCESS) {
139 DHLOGE("RespPrepareRemoteInput error, SendMessage fail.");
140 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPPREPARE_FAIL;
141 }
142 return DH_SUCCESS;
143 } else {
144 DHLOGE("RespPrepareRemoteInput error, sessionId <= 0.");
145 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPPREPARE_FAIL;
146 }
147 }
148
RespUnprepareRemoteInput(const int32_t sessionId,std::string & smsg)149 int32_t DistributedInputSinkTransport::RespUnprepareRemoteInput(const int32_t sessionId, std::string &smsg)
150 {
151 if (sessionId > 0) {
152 DHLOGI("RespUnprepareRemoteInput sessionId: %{public}d, smsg:%{public}s.", sessionId, SetAnonyId(smsg).c_str());
153 int32_t ret = SendMessage(sessionId, smsg);
154 if (ret != DH_SUCCESS) {
155 DHLOGE("RespUnprepareRemoteInput error, SendMessage fail.");
156 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPUNPREPARE_FAIL;
157 }
158 return DH_SUCCESS;
159 } else {
160 DHLOGE("RespUnprepareRemoteInput error, sessionId <= 0.");
161 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPUNPREPARE_FAIL;
162 }
163 }
164
RespStartRemoteInput(const int32_t sessionId,std::string & smsg)165 int32_t DistributedInputSinkTransport::RespStartRemoteInput(const int32_t sessionId, std::string &smsg)
166 {
167 if (sessionId > 0) {
168 DHLOGI("RespStartRemoteInput sessionId: %{public}d, smsg:%{public}s.", sessionId, SetAnonyId(smsg).c_str());
169 int32_t ret = SendMessage(sessionId, smsg);
170 if (ret != DH_SUCCESS) {
171 DHLOGE("RespStartRemoteInput error, SendMessage fail.");
172 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPSTART_FAIL;
173 }
174 return DH_SUCCESS;
175 } else {
176 DHLOGE("RespStartRemoteInput error, sessionId <= 0.");
177 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPSTART_FAIL;
178 }
179 }
180
RespStopRemoteInput(const int32_t sessionId,std::string & smsg)181 int32_t DistributedInputSinkTransport::RespStopRemoteInput(const int32_t sessionId, std::string &smsg)
182 {
183 if (sessionId > 0) {
184 DHLOGI("RespStopRemoteInput sessionId: %{public}d, smsg:%{public}s.", sessionId, SetAnonyId(smsg).c_str());
185 int32_t ret = SendMessage(sessionId, smsg);
186 if (ret != DH_SUCCESS) {
187 DHLOGE("RespStopRemoteInput error, SendMessage fail.");
188 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPSTOP_FAIL;
189 }
190 return DH_SUCCESS;
191 } else {
192 DHLOGE("RespStopRemoteInput error, sessionId <= 0.");
193 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESPSTOP_FAIL;
194 }
195 }
196
RespLatency(const int32_t sessionId,std::string & smsg)197 int32_t DistributedInputSinkTransport::RespLatency(const int32_t sessionId, std::string &smsg)
198 {
199 if (sessionId <= 0) {
200 DHLOGE("RespLatency error, sessionId <= 0.");
201 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESP_LATENCY_FAIL;
202 }
203
204 int32_t ret = SendMessage(sessionId, smsg);
205 if (ret != DH_SUCCESS) {
206 DHLOGE("RespLatency error, SendMessage fail.");
207 return ERR_DH_INPUT_SERVER_SINK_TRANSPORT_RESP_LATENCY_FAIL;
208 }
209
210 return DH_SUCCESS;
211 }
212
SendKeyStateNodeMsg(const int32_t sessionId,const std::string & dhId,uint32_t type,const uint32_t btnCode,int32_t value)213 void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, const std::string &dhId,
214 uint32_t type, const uint32_t btnCode, int32_t value)
215 {
216 if (sessionId <= 0) {
217 DHLOGE("SendKeyStateNodeMsg error, sessionId <= 0.");
218 return;
219 }
220 DHLOGI("SendKeyStateNodeMsg sessionId: %{public}d, btnCode: %{public}u.", sessionId, btnCode);
221 nlohmann::json jsonStr;
222 jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE;
223 jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_DHID] = dhId;
224 jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_TYPE] = type;
225 jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_CODE] = btnCode;
226 jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_VALUE] = value;
227 std::string msg = jsonStr.dump();
228 int32_t ret = SendMessage(sessionId, msg);
229 if (ret != DH_SUCCESS) {
230 DHLOGE("SendKeyStateNodeMsg error, SendMessage fail.");
231 }
232 RecordEventLog(dhId, type, btnCode, value);
233 }
234
SendKeyStateNodeMsgBatch(const int32_t sessionId,const std::vector<struct RawEvent> & events)235 void DistributedInputSinkTransport::SendKeyStateNodeMsgBatch(const int32_t sessionId,
236 const std::vector<struct RawEvent> &events)
237 {
238 if (sessionId <= 0) {
239 DHLOGE("SendKeyStateNodeMsgBatch error, sessionId <= 0.");
240 return;
241 }
242 DHLOGI("SendKeyStateNodeMsgBatch sessionId: %{public}d, event size: %{public}zu ", sessionId, events.size());
243
244 int32_t cnt = 0;
245 std::vector<struct RawEvent> eventBatch;
246 for (auto ev : events) {
247 eventBatch.push_back(ev);
248 cnt++;
249 if (cnt == MSG_BTACH_MAX_SIZE) {
250 DoSendMsgBatch(sessionId, eventBatch);
251 eventBatch.clear();
252 cnt = 0;
253 }
254 }
255
256 if (!eventBatch.empty()) {
257 DoSendMsgBatch(sessionId, eventBatch);
258 }
259 }
260
DoSendMsgBatch(const int32_t sessionId,const std::vector<struct RawEvent> & events)261 void DistributedInputSinkTransport::DoSendMsgBatch(const int32_t sessionId, const std::vector<struct RawEvent> &events)
262 {
263 int64_t currentTimeNs = static_cast<int64_t>(GetCurrentTimeUs()) * 1000LL;
264 std::shared_ptr<nlohmann::json> eventsJsonArr = std::make_shared<nlohmann::json>();
265 for (const auto &ev : events) {
266 nlohmann::json tmpJson;
267 tmpJson[INPUT_KEY_WHEN] = currentTimeNs;
268 tmpJson[INPUT_KEY_TYPE] = ev.type;
269 tmpJson[INPUT_KEY_CODE] = ev.code;
270 tmpJson[INPUT_KEY_VALUE] = ev.value;
271 tmpJson[INPUT_KEY_DESCRIPTOR] = ev.descriptor;
272 tmpJson[INPUT_KEY_PATH] = ev.path;
273 eventsJsonArr->push_back(tmpJson);
274 }
275
276 nlohmann::json jsonStr;
277 jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE_BATCH;
278 jsonStr[DINPUT_SOFTBUS_KEY_INPUT_DATA] = eventsJsonArr->dump();
279 std::string msg = jsonStr.dump();
280 int32_t ret = SendMessage(sessionId, msg);
281 if (ret != DH_SUCCESS) {
282 DHLOGE("SendKeyStateNodeMsgBatch error, SendMessage fail.");
283 }
284 RecordEventLog(events);
285 }
286
RecordEventLog(const std::vector<struct RawEvent> & events)287 void DistributedInputSinkTransport::RecordEventLog(const std::vector<struct RawEvent> &events)
288 {
289 for (auto &ev : events) {
290 RecordEventLog(ev.descriptor, ev.type, ev.code, ev.value);
291 }
292 }
293
RecordEventLog(const std::string & dhId,int32_t type,int32_t code,int32_t value)294 void DistributedInputSinkTransport::RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value)
295 {
296 std::string eventType;
297 switch (type) {
298 case EV_KEY:
299 eventType = "EV_KEY";
300 break;
301 case EV_REL:
302 eventType = "EV_REL";
303 break;
304 case EV_ABS:
305 eventType = "EV_ABS";
306 break;
307 default:
308 eventType = "other type " + std::to_string(type);
309 break;
310 }
311
312 DHLOGD("2.E2E-Test Sink softBus send, EventType: %{public}s, Code: %{public}d, Value: %{public}d, dhId: %{public}s",
313 eventType.c_str(), code, value, GetAnonyString(dhId).c_str());
314 }
315
SendMessage(int32_t sessionId,std::string & message)316 int32_t DistributedInputSinkTransport::SendMessage(int32_t sessionId, std::string &message)
317 {
318 return DistributedInputTransportBase::GetInstance().SendMsg(sessionId, message);
319 }
320
DInputTransbaseSinkListener(DistributedInputSinkTransport * transport)321 DistributedInputSinkTransport::DInputTransbaseSinkListener::DInputTransbaseSinkListener(
322 DistributedInputSinkTransport *transport)
323 {
324 sinkTransportObj_ = transport;
325 DHLOGI("DInputTransbaseSinkListener init.");
326 }
327
~DInputTransbaseSinkListener()328 DistributedInputSinkTransport::DInputTransbaseSinkListener::~DInputTransbaseSinkListener()
329 {
330 sinkTransportObj_ = nullptr;
331 DHLOGI("DInputTransbaseSinkListener destory.");
332 }
333
NotifyPrepareRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)334 void DistributedInputSinkTransport::NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
335 {
336 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_PREPARE.");
337 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID)) {
338 DHLOGE("The key is invaild.");
339 return;
340 }
341 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
342 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_PREPARE deviceId:%{public}s.",
343 GetAnonyString(deviceId).c_str());
344 if (callback_ == nullptr) {
345 DHLOGE("callback_ is nullptr.");
346 return;
347 }
348 callback_->OnPrepareRemoteInput(sessionId, deviceId);
349 }
350
NotifyUnprepareRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)351 void DistributedInputSinkTransport::NotifyUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
352 {
353 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID)) {
354 DHLOGE("The key is invaild.");
355 return;
356 }
357 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
358 DHLOGI("OnBytesReceived cmdType TRANS_SOURCE_MSG_UNPREPARE deviceId:%{public}s.",
359 GetAnonyString(deviceId).c_str());
360 if (callback_ == nullptr) {
361 DHLOGE("callback_ is nullptr.");
362 return;
363 }
364 callback_->OnUnprepareRemoteInput(sessionId);
365 }
366
NotifyStartRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)367 void DistributedInputSinkTransport::NotifyStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
368 {
369 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
370 !IsUInt32(recMsg, DINPUT_SOFTBUS_KEY_INPUT_TYPE)) {
371 DHLOGE("The key is invaild.");
372 return;
373 }
374 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
375 uint32_t inputTypes = recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE];
376 DHLOGI("OnBytesRecei,ved cmdType is TRANS_SOURCE_MSG_START_TYPE deviceId:%{public}s inputTypes:%{public}d .",
377 GetAnonyString(deviceId).c_str(), inputTypes);
378 if (callback_ == nullptr) {
379 DHLOGE("callback_ is nullptr.");
380 return;
381 }
382 callback_->OnStartRemoteInput(sessionId, inputTypes);
383 }
384
NotifyStopRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)385 void DistributedInputSinkTransport::NotifyStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
386 {
387 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
388 !IsUInt32(recMsg, DINPUT_SOFTBUS_KEY_INPUT_TYPE)) {
389 DHLOGE("The key is invaild.");
390 return;
391 }
392 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
393 uint32_t inputTypes = recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE];
394 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_STOP_TYPE deviceId:%{public}s.",
395 GetAnonyString(deviceId).c_str());
396 if (callback_ == nullptr) {
397 DHLOGE("callback_ is nullptr.");
398 return;
399 }
400 callback_->OnStopRemoteInput(sessionId, inputTypes);
401 }
402
NotifyLatency(int32_t sessionId,const nlohmann::json & recMsg)403 void DistributedInputSinkTransport::NotifyLatency(int32_t sessionId, const nlohmann::json &recMsg)
404 {
405 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID)) {
406 DHLOGE("The key is invaild.");
407 return;
408 }
409
410 nlohmann::json jsonStr;
411 jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_LATENCY;
412 jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true;
413 std::string smsg = jsonStr.dump();
414 RespLatency(sessionId, smsg);
415 }
416
NotifyStartRemoteInputDhid(int32_t sessionId,const nlohmann::json & recMsg)417 void DistributedInputSinkTransport::NotifyStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg)
418 {
419 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
420 !IsString(recMsg, DINPUT_SOFTBUS_KEY_VECTOR_DHID)) {
421 DHLOGE("The key is invaild.");
422 return;
423 }
424 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
425 std::string strTmp = recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID];
426 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_START_DHID deviceId:%{public}s .",
427 GetAnonyString(deviceId).c_str());
428 if (callback_ == nullptr) {
429 DHLOGE("callback_ is nullptr.");
430 return;
431 }
432 callback_->OnStartRemoteInputDhid(sessionId, strTmp);
433 }
434
NotifyStopRemoteInputDhid(int32_t sessionId,const nlohmann::json & recMsg)435 void DistributedInputSinkTransport::NotifyStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg)
436 {
437 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
438 !IsString(recMsg, DINPUT_SOFTBUS_KEY_VECTOR_DHID)) {
439 DHLOGE("The key is invaild.");
440 return;
441 }
442 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
443 std::string strTmp = recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID];
444 DHLOGE("OnBytesReceived cmdType is TRANS_SOURCE_MSG_STOP_DHID deviceId:%{public}s.",
445 GetAnonyString(deviceId).c_str());
446 if (callback_ == nullptr) {
447 DHLOGE("callback_ is nullptr.");
448 return;
449 }
450 callback_->OnStopRemoteInputDhid(sessionId, strTmp);
451 }
452
NotifyRelayPrepareRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)453 void DistributedInputSinkTransport::NotifyRelayPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
454 {
455 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
456 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID)) {
457 DHLOGE("The key is invaild.");
458 return;
459 }
460 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
461 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
462 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_PREPARE_FOR_REL deviceId:%{public}s.",
463 GetAnonyString(deviceId).c_str());
464 if (callback_ == nullptr) {
465 DHLOGE("callback_ is nullptr.");
466 return;
467 }
468 callback_->OnRelayPrepareRemoteInput(toSrcSessionId, sessionId, deviceId);
469 }
470
NotifyRelayUnprepareRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)471 void DistributedInputSinkTransport::NotifyRelayUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
472 {
473 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
474 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID)) {
475 DHLOGE("The key is invaild.");
476 return;
477 }
478 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
479 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
480 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_UNPREPARE_FOR_REL deviceId:%{public}s.",
481 GetAnonyString(deviceId).c_str());
482 if (callback_ == nullptr) {
483 DHLOGE("callback_ is nullptr.");
484 return;
485 }
486 callback_->OnRelayUnprepareRemoteInput(toSrcSessionId, sessionId, deviceId);
487 }
488
NotifyRelayStartDhidRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)489 void DistributedInputSinkTransport::NotifyRelayStartDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
490 {
491 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
492 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID) ||
493 !IsString(recMsg, DINPUT_SOFTBUS_KEY_VECTOR_DHID)) {
494 DHLOGE("The key is invaild.");
495 return;
496 }
497 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
498 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
499 std::string dhids = recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID];
500 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_START_DHID_FOR_REL deviceId:%{public}s.",
501 GetAnonyString(deviceId).c_str());
502 if (callback_ == nullptr) {
503 DHLOGE("callback_ is nullptr.");
504 return;
505 }
506 callback_->OnRelayStartDhidRemoteInput(toSrcSessionId, sessionId, deviceId, dhids);
507 }
508
NotifyRelayStopDhidRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)509 void DistributedInputSinkTransport::NotifyRelayStopDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
510 {
511 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
512 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID) ||
513 !IsString(recMsg, DINPUT_SOFTBUS_KEY_VECTOR_DHID)) {
514 DHLOGE("The key is invaild.");
515 return;
516 }
517 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
518 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
519 std::string dhids = recMsg[DINPUT_SOFTBUS_KEY_VECTOR_DHID];
520 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_STOP_DHID_FOR_REL deviceId:%{public}s.",
521 GetAnonyString(deviceId).c_str());
522 if (callback_ == nullptr) {
523 DHLOGE("callback_ is nullptr.");
524 return;
525 }
526 callback_->OnRelayStopDhidRemoteInput(toSrcSessionId, sessionId, deviceId, dhids);
527 }
528
NotifyRelayStartTypeRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)529 void DistributedInputSinkTransport::NotifyRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
530 {
531 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
532 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID) ||
533 !IsUInt32(recMsg, DINPUT_SOFTBUS_KEY_INPUT_TYPE)) {
534 DHLOGE("The key is invaild.");
535 return;
536 }
537 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
538 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
539 uint32_t inputTypes = recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE];
540 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_START_TYPE_FOR_REL deviceId:%{public}s.",
541 GetAnonyString(deviceId).c_str());
542 if (callback_ == nullptr) {
543 DHLOGE("callback_ is nullptr.");
544 return;
545 }
546 callback_->OnRelayStartTypeRemoteInput(toSrcSessionId, sessionId, deviceId, inputTypes);
547 }
548
NotifyRelayStopTypeRemoteInput(int32_t sessionId,const nlohmann::json & recMsg)549 void DistributedInputSinkTransport::NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg)
550 {
551 if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_DEVICE_ID) ||
552 !IsInt32(recMsg, DINPUT_SOFTBUS_KEY_SESSION_ID) ||
553 !IsUInt32(recMsg, DINPUT_SOFTBUS_KEY_INPUT_TYPE)) {
554 DHLOGE("The key is invaild.");
555 return;
556 }
557 std::string deviceId = recMsg[DINPUT_SOFTBUS_KEY_DEVICE_ID];
558 int32_t toSrcSessionId = recMsg[DINPUT_SOFTBUS_KEY_SESSION_ID];
559 uint32_t inputTypes = recMsg[DINPUT_SOFTBUS_KEY_INPUT_TYPE];
560 DHLOGI("OnBytesReceived cmdType is TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL deviceId:%{public}s.",
561 GetAnonyString(deviceId).c_str());
562 if (callback_ == nullptr) {
563 DHLOGE("callback_ is nullptr.");
564 return;
565 }
566 callback_->OnRelayStopTypeRemoteInput(toSrcSessionId, sessionId, deviceId, inputTypes);
567 }
568
NotifySessionClosed(int32_t sessionId)569 void DistributedInputSinkTransport::DInputTransbaseSinkListener::NotifySessionClosed(int32_t sessionId)
570 {
571 DistributedInputSinkSwitch::GetInstance().RemoveSession(sessionId);
572 }
573
HandleSessionData(int32_t sessionId,const std::string & message)574 void DistributedInputSinkTransport::DInputTransbaseSinkListener::HandleSessionData(int32_t sessionId,
575 const std::string &message)
576 {
577 DistributedInputSinkTransport::GetInstance().HandleData(sessionId, message);
578 }
579
HandleEventInner(int32_t sessionId,const nlohmann::json & recMsg)580 void DistributedInputSinkTransport::HandleEventInner(int32_t sessionId, const nlohmann::json &recMsg)
581 {
582 uint32_t cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE];
583 switch (cmdType) {
584 case TRANS_SOURCE_MSG_PREPARE_FOR_REL:
585 NotifyRelayPrepareRemoteInput(sessionId, recMsg);
586 break;
587 case TRANS_SOURCE_MSG_UNPREPARE_FOR_REL:
588 NotifyRelayUnprepareRemoteInput(sessionId, recMsg);
589 break;
590 case TRANS_SOURCE_MSG_START_DHID_FOR_REL:
591 NotifyRelayStartDhidRemoteInput(sessionId, recMsg);
592 break;
593 case TRANS_SOURCE_MSG_STOP_DHID_FOR_REL:
594 NotifyRelayStopDhidRemoteInput(sessionId, recMsg);
595 break;
596 case TRANS_SOURCE_MSG_START_TYPE_FOR_REL:
597 NotifyRelayStartTypeRemoteInput(sessionId, recMsg);
598 break;
599 case TRANS_SOURCE_MSG_STOP_TYPE_FOR_REL:
600 NotifyRelayStopTypeRemoteInput(sessionId, recMsg);
601 break;
602 default:
603 DHLOGE("OnBytesReceived cmdType %{public}u is undefined.", cmdType);
604 }
605 }
606
HandleData(int32_t sessionId,const std::string & message)607 void DistributedInputSinkTransport::HandleData(int32_t sessionId, const std::string &message)
608 {
609 if (callback_ == nullptr) {
610 DHLOGE("OnBytesReceived the callback_ is null, the message:%{public}s abort.", SetAnonyId(message).c_str());
611 return;
612 }
613
614 nlohmann::json recMsg = nlohmann::json::parse(message, nullptr, false);
615 if (recMsg.is_discarded()) {
616 DHLOGE("recMsg parse failed!");
617 return;
618 }
619 if (!IsUInt32(recMsg, DINPUT_SOFTBUS_KEY_CMD_TYPE)) {
620 DHLOGE("softbus cmd key is invalid");
621 return;
622 }
623 uint32_t cmdType = recMsg[DINPUT_SOFTBUS_KEY_CMD_TYPE];
624 switch (cmdType) {
625 case TRANS_SOURCE_MSG_PREPARE:
626 NotifyPrepareRemoteInput(sessionId, recMsg);
627 break;
628 case TRANS_SOURCE_MSG_UNPREPARE:
629 NotifyUnprepareRemoteInput(sessionId, recMsg);
630 break;
631 case TRANS_SOURCE_MSG_START_TYPE:
632 NotifyStartRemoteInput(sessionId, recMsg);
633 break;
634 case TRANS_SOURCE_MSG_STOP_TYPE:
635 NotifyStopRemoteInput(sessionId, recMsg);
636 break;
637 case TRANS_SOURCE_MSG_LATENCY:
638 NotifyLatency(sessionId, recMsg);
639 break;
640 case TRANS_SOURCE_MSG_START_DHID:
641 NotifyStartRemoteInputDhid(sessionId, recMsg);
642 break;
643 case TRANS_SOURCE_MSG_STOP_DHID:
644 NotifyStopRemoteInputDhid(sessionId, recMsg);
645 break;
646 default:
647 HandleEventInner(sessionId, recMsg);
648 }
649 }
650
CloseAllSession()651 void DistributedInputSinkTransport::CloseAllSession()
652 {
653 DistributedInputTransportBase::GetInstance().StopAllSession();
654 // clear session data
655 DistributedInputSinkSwitch::GetInstance().InitSwitch();
656 }
657
RecordEventLog(const std::shared_ptr<nlohmann::json> & events)658 void DistributedInputSinkTransport::DInputSinkEventHandler::RecordEventLog(
659 const std::shared_ptr<nlohmann::json> &events)
660 {
661 for (nlohmann::json::const_iterator iter = events->cbegin(); iter != events->cend(); ++iter) {
662 nlohmann::json event = *iter;
663 if (!IsInt32(event, INPUT_KEY_TYPE) || !IsInt64(event, INPUT_KEY_WHEN) ||
664 !IsUInt32(event, INPUT_KEY_CODE) || !IsInt32(event, INPUT_KEY_VALUE) ||
665 !IsString(event, INPUT_KEY_PATH)) {
666 DHLOGE("The key is invaild.");
667 continue;
668 }
669 std::string eventType = "";
670 int32_t evType = event[INPUT_KEY_TYPE];
671 switch (evType) {
672 case EV_KEY:
673 eventType = "EV_KEY";
674 break;
675 case EV_REL:
676 eventType = "EV_REL";
677 break;
678 case EV_ABS:
679 eventType = "EV_ABS";
680 break;
681 default:
682 eventType = "other type " + std::to_string(evType);
683 break;
684 }
685 int64_t when = event[INPUT_KEY_WHEN];
686 int32_t code = event[INPUT_KEY_CODE];
687 int32_t value = event[INPUT_KEY_VALUE];
688 std::string path = event[INPUT_KEY_PATH];
689 DHLOGD("2.E2E-Test Sink softBus send, EventType: %{public}s, Code: %{public}d, Value: %{public}d, "
690 "Path: %{public}s, When: %{public}" PRId64 "", eventType.c_str(), code, value, path.c_str(), when);
691 }
692 }
693 } // namespace DistributedInput
694 } // namespace DistributedHardware
695 } // namespace OHOS
696