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