1 /*
2 * Copyright (c) 2023-2025 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 "dsoftbus_handler.h"
17
18 #include "ipc_skeleton.h"
19 #include "token_setproc.h"
20
21 #include "cooperate_hisysevent.h"
22 #include "device.h"
23 #include "devicestatus_define.h"
24 #include "utility.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "DSoftbusHandler"
28
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 namespace Cooperate {
33 constexpr int32_t MAX_INPUT_DEV_NUM { 100 };
34 constexpr int32_t INVALID_DEVICE_ID { -1 };
35
DSoftbusHandler(IContext * env)36 DSoftbusHandler::DSoftbusHandler(IContext *env)
37 : env_(env)
38 {
39 handles_ = {
40 { static_cast<int32_t>(MessageId::DSOFTBUS_START_COOPERATE),
41 [this] (const std::string &networkId, NetPacket &packet) {
42 this->OnStartCooperate(networkId, packet);}},
43 { static_cast<int32_t>(MessageId::DSOFTBUS_STOP_COOPERATE),
44 [this] (const std::string &networkId, NetPacket &packet) {
45 this->OnStopCooperate(networkId, packet);}},
46 { static_cast<int32_t>(MessageId::DSOFTBUS_COME_BACK),
47 [this] (const std::string &networkId, NetPacket &packet) {
48 this->OnComeBack(networkId, packet);}},
49 { static_cast<int32_t>(MessageId::DSOFTBUS_COME_BACK_WITH_OPTIONS),
50 [this] (const std::string &networkId, NetPacket &packet) {
51 this->OnComeBackWithOptions(networkId, packet);}},
52 { static_cast<int32_t>(MessageId::DSOFTBUS_RELAY_COOPERATE),
53 [this] (const std::string &networkId, NetPacket &packet) {
54 this->OnRelayCooperate(networkId, packet);}},
55 { static_cast<int32_t>(MessageId::DSOFTBUS_RELAY_COOPERATE_FINISHED),
56 [this] (const std::string &networkId, NetPacket &packet) {
57 this->OnRelayCooperateFinish(networkId, packet);}},
58 { static_cast<int32_t>(MessageId::DSOFTBUS_SUBSCRIBE_MOUSE_LOCATION),
59 [this] (const std::string &networkId, NetPacket &packet) {
60 this->OnSubscribeMouseLocation(networkId, packet);}},
61 { static_cast<int32_t>(MessageId::DSOFTBUS_UNSUBSCRIBE_MOUSE_LOCATION),
62 [this] (const std::string &networkId, NetPacket &packet) {
63 this->OnUnSubscribeMouseLocation(networkId, packet);}},
64 { static_cast<int32_t>(MessageId::DSOFTBUS_REPLY_SUBSCRIBE_MOUSE_LOCATION),
65 [this] (const std::string &networkId, NetPacket &packet) {
66 this->OnReplySubscribeLocation(networkId, packet);}},
67 { static_cast<int32_t>(MessageId::DSOFTBUS_REPLY_UNSUBSCRIBE_MOUSE_LOCATION),
68 [this] (const std::string &networkId, NetPacket &packet) {
69 this->OnReplyUnSubscribeLocation(networkId, packet);}},
70 { static_cast<int32_t>(MessageId::DSOFTBUS_MOUSE_LOCATION),
71 [this] (const std::string &networkId, NetPacket &packet) {
72 this->OnRemoteMouseLocation(networkId, packet);}},
73 { static_cast<int32_t>(MessageId::DSOFTBUS_INPUT_DEV_SYNC),
74 [this] (const std::string &networkId, NetPacket &packet) {
75 this->OnRemoteInputDevice(networkId, packet);}},
76 { static_cast<int32_t>(MessageId::DSOFTBUS_INPUT_DEV_HOT_PLUG),
77 [this] (const std::string &networkId, NetPacket &packet) {
78 this->OnRemoteHotPlug(networkId, packet);}},
79 { static_cast<int32_t>(MessageId::DSOFTBUS_COOPERATE_WITH_OPTIONS),
80 [this] (const std::string &networkId, NetPacket &packet) {
81 this->OnStartCooperateWithOptions(networkId, packet);}},
82 { static_cast<int32_t>(MessageId::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS),
83 [this] (const std::string &networkId, NetPacket &packet) {
84 this->OnRelayCooperateWithOptions(networkId, packet);}},
85 { static_cast<int32_t>(MessageId::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS_FINISHED),
86 [this] (const std::string &networkId, NetPacket &packet) {
87 this->OnRelayCooperateWithOptionsFinish(networkId, packet);}}
88 };
89 observer_ = std::make_shared<DSoftbusObserver>(*this);
90 CHKPV(env_);
91 env_->GetDSoftbus().AddObserver(observer_);
92 }
93
~DSoftbusHandler()94 DSoftbusHandler::~DSoftbusHandler()
95 {
96 CHKPV(env_);
97 env_->GetDSoftbus().RemoveObserver(observer_);
98 }
99
AttachSender(Channel<CooperateEvent>::Sender sender)100 void DSoftbusHandler::AttachSender(Channel<CooperateEvent>::Sender sender)
101 {
102 CALL_DEBUG_ENTER;
103 std::lock_guard guard(lock_);
104 sender_ = sender;
105 }
106
OpenSession(const std::string & networkId)107 int32_t DSoftbusHandler::OpenSession(const std::string &networkId)
108 {
109 CALL_INFO_TRACE;
110 auto tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
111 int ret = SetFirstCallerTokenID(tokenId);
112 if (ret != RET_OK) {
113 FI_HILOGW("Failed to SetFirstCallerTokenID, ret:%{public}d", ret);
114 }
115 ret = env_->GetDSoftbus().OpenSession(networkId);
116 if (ret == RET_OK) {
117 env_->GetDSoftbus().StartHeartBeat(networkId);
118 }
119 return ret;
120 }
121
CloseSession(const std::string & networkId)122 void DSoftbusHandler::CloseSession(const std::string &networkId)
123 {
124 CALL_INFO_TRACE;
125 env_->GetDSoftbus().CloseSession(networkId);
126 }
127
CloseAllSessions()128 void DSoftbusHandler::CloseAllSessions()
129 {
130 CALL_INFO_TRACE;
131 env_->GetDSoftbus().CloseAllSessions();
132 }
133
StartCooperate(const std::string & networkId,const DSoftbusStartCooperate & event)134 int32_t DSoftbusHandler::StartCooperate(const std::string &networkId, const DSoftbusStartCooperate &event)
135 {
136 CALL_INFO_TRACE;
137 NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
138 packet << event.originNetworkId << event.cursorPos.x
139 << event.cursorPos.y << event.success << event.extra.priv << event.pointerSpeed
140 << event.touchPadSpeed;
141 if (packet.ChkRWError()) {
142 FI_HILOGE("Failed to write data packet");
143 CooperateRadarInfo radarInfo {
144 .funcName = __FUNCTION__,
145 .bizState = static_cast<int32_t> (BizState::STATE_END),
146 .bizStage = static_cast<int32_t> (BizCooperateStage::STAGE_SERIALIZE_INSTRUCTION),
147 .stageRes = static_cast<int32_t> (BizCooperateStageRes::RES_FAIL),
148 .bizScene = static_cast<int32_t> (BizCooperateScene::SCENE_ACTIVE),
149 .errCode = static_cast<int32_t> (CooperateRadarErrCode::SERIALIZE_INSTRUCTION_FAILED),
150 .hostName = "",
151 .localNetId = Utility::DFXRadarAnonymize(event.originNetworkId.c_str()),
152 .peerNetId = Utility::DFXRadarAnonymize(networkId.c_str())
153 };
154 CooperateRadar::ReportCooperateRadarInfo(radarInfo);
155 return RET_ERR;
156 }
157 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
158 if (ret != RET_OK) {
159 OnCommunicationFailure(networkId);
160 CooperateRadarInfo radarInfo {
161 .funcName = __FUNCTION__,
162 .bizState = static_cast<int32_t> (BizState::STATE_END),
163 .bizStage = static_cast<int32_t> (BizCooperateStage::STAGE_SEND_INSTRUCTION_TO_REMOTE),
164 .stageRes = static_cast<int32_t> (BizCooperateStageRes::RES_FAIL),
165 .bizScene = static_cast<int32_t> (BizCooperateScene::SCENE_ACTIVE),
166 .errCode = static_cast<int32_t> (CooperateRadarErrCode::SEND_INSTRUCTION_TO_REMOTE_FAILED),
167 .hostName = "",
168 .localNetId = Utility::DFXRadarAnonymize(event.originNetworkId.c_str()),
169 .peerNetId = Utility::DFXRadarAnonymize(networkId.c_str())
170 };
171 CooperateRadar::ReportCooperateRadarInfo(radarInfo);
172 }
173 return ret;
174 }
175
StartCooperateWithOptions(const std::string & networkId,const DSoftbusCooperateOptions & event)176 int32_t DSoftbusHandler::StartCooperateWithOptions(const std::string &networkId, const DSoftbusCooperateOptions &event)
177 {
178 CALL_INFO_TRACE;
179 NetPacket packet(MessageId::DSOFTBUS_COOPERATE_WITH_OPTIONS);
180 packet << event.originNetworkId << event.cooperateOptions.displayX
181 << event.cooperateOptions.displayY << event.cooperateOptions.displayId
182 << event.success << event.extra.priv;
183 if (packet.ChkRWError()) {
184 FI_HILOGE("Failed to write data packet");
185 return RET_ERR;
186 }
187 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
188 if (ret != RET_OK) {
189 OnCommunicationFailure(networkId);
190 }
191 return ret;
192 }
193
StopCooperate(const std::string & networkId,const DSoftbusStopCooperate & event)194 int32_t DSoftbusHandler::StopCooperate(const std::string &networkId, const DSoftbusStopCooperate &event)
195 {
196 CALL_INFO_TRACE;
197 NetPacket packet(MessageId::DSOFTBUS_STOP_COOPERATE);
198 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
199 if (ret != RET_OK) {
200 OnCommunicationFailure(networkId);
201 }
202 return ret;
203 }
204
ComeBack(const std::string & networkId,const DSoftbusComeBack & event)205 int32_t DSoftbusHandler::ComeBack(const std::string &networkId, const DSoftbusComeBack &event)
206 {
207 CALL_INFO_TRACE;
208 NetPacket packet(MessageId::DSOFTBUS_COME_BACK);
209 packet << event.originNetworkId << event.cursorPos.x << event.cursorPos.y << event.extra.priv;
210 if (packet.ChkRWError()) {
211 FI_HILOGE("Failed to write data packet");
212 return RET_ERR;
213 }
214 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
215 if (ret != RET_OK) {
216 OnCommunicationFailure(networkId);
217 }
218 return ret;
219 }
220
ComeBackWithOptions(const std::string & networkId,const DSoftbusComeBackWithOptions & event)221 int32_t DSoftbusHandler::ComeBackWithOptions(const std::string &networkId, const DSoftbusComeBackWithOptions &event)
222 {
223 CALL_INFO_TRACE;
224 NetPacket packet(MessageId::DSOFTBUS_COME_BACK_WITH_OPTIONS);
225 packet << event.originNetworkId << event.cooperateOptions.displayX << event.cooperateOptions.displayY
226 << event.cooperateOptions.displayId << event.extra.priv;
227 if (packet.ChkRWError()) {
228 FI_HILOGE("Failed to write data packet");
229 return RET_ERR;
230 }
231 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
232 if (ret != RET_OK) {
233 OnCommunicationFailure(networkId);
234 }
235 return ret;
236 }
237
RelayCooperate(const std::string & networkId,const DSoftbusRelayCooperate & event)238 int32_t DSoftbusHandler::RelayCooperate(const std::string &networkId, const DSoftbusRelayCooperate &event)
239 {
240 CALL_INFO_TRACE;
241 NetPacket packet(MessageId::DSOFTBUS_RELAY_COOPERATE);
242 packet << event.targetNetworkId << event.pointerSpeed << event.touchPadSpeed;
243 if (packet.ChkRWError()) {
244 FI_HILOGE("Failed to write data packet");
245 return RET_ERR;
246 }
247 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
248 if (ret != RET_OK) {
249 OnCommunicationFailure(networkId);
250 }
251 return ret;
252 }
253
RelayCooperateFinish(const std::string & networkId,const DSoftbusRelayCooperateFinished & event)254 int32_t DSoftbusHandler::RelayCooperateFinish(const std::string &networkId, const DSoftbusRelayCooperateFinished &event)
255 {
256 CALL_INFO_TRACE;
257 NetPacket packet(MessageId::DSOFTBUS_RELAY_COOPERATE_FINISHED);
258 packet << event.targetNetworkId << event.normal;
259 if (packet.ChkRWError()) {
260 FI_HILOGE("Failed to write data packet");
261 return RET_ERR;
262 }
263 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
264 if (ret != RET_OK) {
265 OnCommunicationFailure(networkId);
266 }
267 return ret;
268 }
269
RelayCooperateWithOptions(const std::string & networkId,const DSoftbusRelayCooperate & event)270 int32_t DSoftbusHandler::RelayCooperateWithOptions(const std::string &networkId, const DSoftbusRelayCooperate &event)
271 {
272 CALL_INFO_TRACE;
273 NetPacket packet(MessageId::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS);
274 packet << event.targetNetworkId << event.pointerSpeed << event.touchPadSpeed;
275 if (packet.ChkRWError()) {
276 FI_HILOGE("Failed to write data packet");
277 return RET_ERR;
278 }
279 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
280 if (ret != RET_OK) {
281 OnCommunicationFailure(networkId);
282 }
283 return ret;
284 }
285
RelayCooperateWithOptionsFinish(const std::string & networkId,const DSoftbusRelayCooperateFinished & event)286 int32_t DSoftbusHandler::RelayCooperateWithOptionsFinish(const std::string &networkId,
287 const DSoftbusRelayCooperateFinished &event)
288 {
289 CALL_INFO_TRACE;
290 NetPacket packet(MessageId::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS_FINISHED);
291 packet << event.targetNetworkId << event.normal;
292 if (packet.ChkRWError()) {
293 FI_HILOGE("Failed to write data packet");
294 return RET_ERR;
295 }
296 int32_t ret = env_->GetDSoftbus().SendPacket(networkId, packet);
297 if (ret != RET_OK) {
298 OnCommunicationFailure(networkId);
299 }
300 return ret;
301 }
302
GetLocalNetworkId()303 std::string DSoftbusHandler::GetLocalNetworkId()
304 {
305 return IDSoftbusAdapter::GetLocalNetworkId();
306 }
307
OnBind(const std::string & networkId)308 void DSoftbusHandler::OnBind(const std::string &networkId)
309 {
310 FI_HILOGI("Bind to \'%{public}s\'", Utility::Anonymize(networkId).c_str());
311 SendEvent(CooperateEvent(
312 CooperateEventType::DSOFTBUS_SESSION_OPENED,
313 DSoftbusSessionOpened {
314 .networkId = networkId
315 }));
316 }
317
OnShutdown(const std::string & networkId)318 void DSoftbusHandler::OnShutdown(const std::string &networkId)
319 {
320 FI_HILOGI("Connection with \'%{public}s\' shutdown", Utility::Anonymize(networkId).c_str());
321 SendEvent(CooperateEvent(
322 CooperateEventType::DSOFTBUS_SESSION_CLOSED,
323 DSoftbusSessionClosed {
324 .networkId = networkId
325 }));
326 }
327
OnConnected(const std::string & networkId)328 void DSoftbusHandler::OnConnected(const std::string &networkId)
329 {
330 FI_HILOGI("Connection to \'%{public}s\' successfully", Utility::Anonymize(networkId).c_str());
331 SendEvent(CooperateEvent(
332 CooperateEventType::DSOFTBUS_SESSION_OPENED,
333 DSoftbusSessionOpened {
334 .networkId = networkId
335 }));
336 }
337
OnPacket(const std::string & networkId,NetPacket & packet)338 bool DSoftbusHandler::OnPacket(const std::string &networkId, NetPacket &packet)
339 {
340 CALL_DEBUG_ENTER;
341 int32_t messageId = static_cast<int32_t>(packet.GetMsgId());
342 auto it = handles_.find(messageId);
343 if (it != handles_.end()) {
344 (it->second)(networkId, packet);
345 return true;
346 }
347 FI_HILOGD("Unsupported messageId: %{public}d from %{public}s", messageId,
348 Utility::Anonymize(networkId).c_str());
349 return false;
350 }
351
SendEvent(const CooperateEvent & event)352 void DSoftbusHandler::SendEvent(const CooperateEvent &event)
353 {
354 std::lock_guard guard(lock_);
355 auto ret = sender_.Send(event);
356 if (ret != Channel<CooperateEvent>::NO_ERROR) {
357 FI_HILOGE("Failed to send event via channel, error:%{public}d", ret);
358 }
359 }
360
OnCommunicationFailure(const std::string & networkId)361 void DSoftbusHandler::OnCommunicationFailure(const std::string &networkId)
362 {
363 env_->GetDSoftbus().CloseSession(networkId);
364 FI_HILOGI("Notify communication failure with peer(%{public}s)", Utility::Anonymize(networkId).c_str());
365 SendEvent(CooperateEvent(
366 CooperateEventType::DSOFTBUS_SESSION_CLOSED,
367 DSoftbusSessionClosed {
368 .networkId = networkId
369 }));
370 }
371
OnStartCooperate(const std::string & networkId,NetPacket & packet)372 void DSoftbusHandler::OnStartCooperate(const std::string &networkId, NetPacket &packet)
373 {
374 CALL_INFO_TRACE;
375 DSoftbusStartCooperate event {
376 .networkId = networkId,
377 };
378 packet >> event.originNetworkId >> event.cursorPos.x
379 >> event.cursorPos.y >> event.success;
380 CooperateRadarInfo radarInfo {
381 .funcName = __FUNCTION__,
382 .bizState = static_cast<int32_t> (BizState::STATE_BEGIN),
383 .bizScene = static_cast<int32_t> (BizCooperateScene::SCENE_PASSIVE),
384 .hostName = "",
385 .localNetId = Utility::DFXRadarAnonymize(event.originNetworkId.c_str()),
386 .peerNetId = Utility::DFXRadarAnonymize(networkId.c_str())
387 };
388 if (packet.ChkRWError()) {
389 FI_HILOGE("Failed to read data packet");
390 radarInfo.bizStage = static_cast<int32_t> (BizCooperateStage::STAGE_PASSIVE_DEASERIALIZATION);
391 radarInfo.stageRes = static_cast<int32_t> (BizCooperateStageRes::RES_FAIL);
392 radarInfo.errCode = static_cast<int32_t> (CooperateRadarErrCode::PASSIVE_DEASERIALIZATION_FAILED);
393 CooperateRadar::ReportCooperateRadarInfo(radarInfo);
394 return;
395 }
396 packet >> event.extra.priv;
397 if (packet.ChkRWError()) {
398 event.extra.priv = 0;
399 }
400 packet >> event.pointerSpeed;
401 if (packet.ChkRWError()) {
402 event.pointerSpeed = -1;
403 }
404 packet >> event.touchPadSpeed;
405 if (packet.ChkRWError()) {
406 event.touchPadSpeed = -1;
407 }
408 FI_HILOGI("Cur pointerSpeed:%{public}d, touchPadSpeed:%{public}d,", event.pointerSpeed, event.touchPadSpeed);
409 SendEvent(CooperateEvent(
410 CooperateEventType::DSOFTBUS_START_COOPERATE,
411 event));
412 radarInfo.bizStage = static_cast<int32_t> (BizCooperateStage::STAGE_PASSIVE_DEASERIALIZATION);
413 radarInfo.stageRes = static_cast<int32_t> (BizCooperateStageRes::RES_SUCCESS);
414 radarInfo.errCode = static_cast<int32_t> (CooperateRadarErrCode::CALLING_COOPERATE_SUCCESS);
415 CooperateRadar::ReportCooperateRadarInfo(radarInfo);
416 auto motionDrag = env_->GetPluginManager().LoadMotionDrag();
417 if (motionDrag == nullptr) {
418 FI_HILOGE("Failed to load motion drag");
419 return;
420 }
421 motionDrag->OnRemoteStartCooperateSetPointerButtonDown();
422 }
423
OnStopCooperate(const std::string & networkId,NetPacket & packet)424 void DSoftbusHandler::OnStopCooperate(const std::string &networkId, NetPacket &packet)
425 {
426 CALL_INFO_TRACE;
427 DSoftbusStopCooperate event {
428 .networkId = networkId,
429 .normal = true,
430 };
431 SendEvent(CooperateEvent(
432 CooperateEventType::DSOFTBUS_STOP_COOPERATE,
433 event));
434 }
435
OnStartCooperateWithOptions(const std::string & networkId,NetPacket & packet)436 void DSoftbusHandler::OnStartCooperateWithOptions(const std::string &networkId, NetPacket &packet)
437 {
438 CALL_INFO_TRACE;
439 DSoftbusCooperateOptions event {
440 .networkId = networkId,
441 };
442 packet >> event.originNetworkId >> event.cooperateOptions.displayX
443 >> event.cooperateOptions.displayY >> event.cooperateOptions.displayId
444 >> event.success;
445 if (packet.ChkRWError()) {
446 FI_HILOGE("Failed to read data packet");
447 return;
448 }
449 packet >> event.extra.priv;
450 if (packet.ChkRWError()) {
451 event.extra.priv = 0;
452 }
453 SendEvent(CooperateEvent(
454 CooperateEventType::DSOFTBUS_COOPERATE_WITH_OPTIONS,
455 event));
456 }
457
OnComeBack(const std::string & networkId,NetPacket & packet)458 void DSoftbusHandler::OnComeBack(const std::string &networkId, NetPacket &packet)
459 {
460 CALL_INFO_TRACE;
461 DSoftbusComeBack event {
462 .networkId = networkId,
463 .success = true,
464 };
465 packet >> event.originNetworkId >> event.cursorPos.x >> event.cursorPos.y;
466 if (packet.ChkRWError()) {
467 FI_HILOGE("Failed to read data packet");
468 return;
469 }
470 packet >> event.extra.priv;
471 if (packet.ChkRWError()) {
472 event.extra.priv = 0;
473 }
474 SendEvent(CooperateEvent(
475 CooperateEventType::DSOFTBUS_COME_BACK,
476 event));
477 }
478
OnComeBackWithOptions(const std::string & networkId,NetPacket & packet)479 void DSoftbusHandler::OnComeBackWithOptions(const std::string &networkId, NetPacket &packet)
480 {
481 CALL_INFO_TRACE;
482 DSoftbusComeBackWithOptions event {
483 .networkId = networkId,
484 .success = true,
485 };
486 packet >> event.originNetworkId >> event.cooperateOptions.displayX >> event.cooperateOptions.displayY;
487 if (packet.ChkRWError()) {
488 FI_HILOGE("Failed to read data packet");
489 return;
490 }
491 packet >> event.extra.priv;
492 if (packet.ChkRWError()) {
493 event.extra.priv = 0;
494 }
495 SendEvent(CooperateEvent(
496 CooperateEventType::DSOFTBUS_COME_BACK_WITH_OPTIONS,
497 event));
498 }
499
OnRelayCooperate(const std::string & networkId,NetPacket & packet)500 void DSoftbusHandler::OnRelayCooperate(const std::string &networkId, NetPacket &packet)
501 {
502 CALL_INFO_TRACE;
503 DSoftbusRelayCooperate event {
504 .networkId = networkId,
505 .normal = true,
506 };
507 packet >> event.targetNetworkId;
508 if (packet.ChkRWError()) {
509 FI_HILOGE("Failed to read data packet");
510 return;
511 }
512 packet >> event.pointerSpeed;
513 if (packet.ChkRWError()) {
514 event.pointerSpeed = -1;
515 }
516 FI_HILOGI("Cur pointerSpeed:%{public}d", event.pointerSpeed);
517 packet >> event.touchPadSpeed;
518 if (packet.ChkRWError()) {
519 event.touchPadSpeed = -1;
520 }
521 FI_HILOGI("Cur touchPadSpeed:%{public}d", event.touchPadSpeed);
522 SendEvent(CooperateEvent(
523 CooperateEventType::DSOFTBUS_RELAY_COOPERATE,
524 event));
525 }
526
OnRelayCooperateFinish(const std::string & networkId,NetPacket & packet)527 void DSoftbusHandler::OnRelayCooperateFinish(const std::string &networkId, NetPacket &packet)
528 {
529 CALL_INFO_TRACE;
530 DSoftbusRelayCooperate event {
531 .networkId = networkId,
532 };
533 packet >> event.targetNetworkId >> event.normal;
534 if (packet.ChkRWError()) {
535 FI_HILOGE("Failed to read data packet");
536 return;
537 }
538 SendEvent(CooperateEvent(
539 CooperateEventType::DSOFTBUS_RELAY_COOPERATE_FINISHED,
540 event));
541 }
542
OnRelayCooperateWithOptions(const std::string & networkId,NetPacket & packet)543 void DSoftbusHandler::OnRelayCooperateWithOptions(const std::string &networkId, NetPacket &packet)
544 {
545 CALL_INFO_TRACE;
546 DSoftbusRelayCooperate event {
547 .networkId = networkId,
548 .normal = true,
549 };
550 packet >> event.targetNetworkId;
551 if (packet.ChkRWError()) {
552 FI_HILOGE("Failed to read data packet");
553 return;
554 }
555 packet >> event.pointerSpeed;
556 if (packet.ChkRWError()) {
557 event.pointerSpeed = -1;
558 }
559 FI_HILOGI("Cur pointerSpeed:%{public}d", event.pointerSpeed);
560 packet >> event.touchPadSpeed;
561 if (packet.ChkRWError()) {
562 event.touchPadSpeed = -1;
563 }
564 FI_HILOGI("Cur touchPadSpeed:%{public}d", event.touchPadSpeed);
565 SendEvent(CooperateEvent(
566 CooperateEventType::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS,
567 event));
568 }
569
OnRelayCooperateWithOptionsFinish(const std::string & networkId,NetPacket & packet)570 void DSoftbusHandler::OnRelayCooperateWithOptionsFinish(const std::string &networkId, NetPacket &packet)
571 {
572 CALL_INFO_TRACE;
573 DSoftbusRelayCooperate event {
574 .networkId = networkId,
575 };
576 packet >> event.targetNetworkId >> event.normal;
577 if (packet.ChkRWError()) {
578 FI_HILOGE("Failed to read data packet");
579 return;
580 }
581 SendEvent(CooperateEvent(
582 CooperateEventType::DSOFTBUS_RELAY_COOPERATE_WITHOPTIONS_FINISHED,
583 event));
584 }
585
OnSubscribeMouseLocation(const std::string & networKId,NetPacket & packet)586 void DSoftbusHandler::OnSubscribeMouseLocation(const std::string &networKId, NetPacket &packet)
587 {
588 CALL_INFO_TRACE;
589 DSoftbusSubscribeMouseLocation event;
590 packet >> event.networkId >> event.remoteNetworkId;
591 if (packet.ChkRWError()) {
592 FI_HILOGE("Failed to read data packet");
593 return;
594 }
595 SendEvent(CooperateEvent(
596 CooperateEventType::DSOFTBUS_SUBSCRIBE_MOUSE_LOCATION,
597 event));
598 }
599
OnUnSubscribeMouseLocation(const std::string & networKId,NetPacket & packet)600 void DSoftbusHandler::OnUnSubscribeMouseLocation(const std::string& networKId, NetPacket &packet)
601 {
602 CALL_INFO_TRACE;
603 DSoftbusUnSubscribeMouseLocation event;
604 packet >> event.networkId >> event.remoteNetworkId;
605 if (packet.ChkRWError()) {
606 FI_HILOGE("Failed to read data packet");
607 return;
608 }
609 SendEvent(CooperateEvent(
610 CooperateEventType::DSOFTBUS_UNSUBSCRIBE_MOUSE_LOCATION,
611 event));
612 }
613
OnReplySubscribeLocation(const std::string & networKId,NetPacket & packet)614 void DSoftbusHandler::OnReplySubscribeLocation(const std::string& networKId, NetPacket &packet)
615 {
616 CALL_INFO_TRACE;
617 DSoftbusReplySubscribeMouseLocation event;
618 packet >> event.networkId >> event.remoteNetworkId >> event.result;
619 if (packet.ChkRWError()) {
620 FI_HILOGE("Failed to read data packet");
621 return;
622 }
623 SendEvent(CooperateEvent(
624 CooperateEventType::DSOFTBUS_REPLY_SUBSCRIBE_MOUSE_LOCATION,
625 event));
626 }
627
OnReplyUnSubscribeLocation(const std::string & networKId,NetPacket & packet)628 void DSoftbusHandler::OnReplyUnSubscribeLocation(const std::string& networKId, NetPacket &packet)
629 {
630 CALL_INFO_TRACE;
631 DSoftbusReplyUnSubscribeMouseLocation event;
632 packet >> event.networkId >> event.remoteNetworkId >> event.result;
633 if (packet.ChkRWError()) {
634 FI_HILOGE("Failed to read data packet");
635 return;
636 }
637 SendEvent(CooperateEvent(
638 CooperateEventType::DSOFTBUS_REPLY_UNSUBSCRIBE_MOUSE_LOCATION,
639 event));
640 }
641
OnRemoteMouseLocation(const std::string & networKId,NetPacket & packet)642 void DSoftbusHandler::OnRemoteMouseLocation(const std::string& networKId, NetPacket &packet)
643 {
644 CALL_DEBUG_ENTER;
645 DSoftbusSyncMouseLocation event;
646 packet >> event.networkId >> event.remoteNetworkId >> event.mouseLocation.displayX >>
647 event.mouseLocation.displayY >> event.mouseLocation.displayWidth >> event.mouseLocation.displayHeight;
648 if (packet.ChkRWError()) {
649 FI_HILOGE("Failed to read data packet");
650 return;
651 }
652 SendEvent(CooperateEvent(
653 CooperateEventType::DSOFTBUS_MOUSE_LOCATION,
654 event));
655 }
656
OnRemoteInputDevice(const std::string & networkId,NetPacket & packet)657 void DSoftbusHandler::OnRemoteInputDevice(const std::string& networkId, NetPacket &packet)
658 {
659 CALL_INFO_TRACE;
660 DSoftbusSyncInputDevice event;
661 int32_t devNum { -1 };
662 packet >> devNum;
663 event.networkId = networkId;
664 FI_HILOGI("devNum:%{public}d", devNum);
665 if (devNum <= 0 || devNum >= MAX_INPUT_DEV_NUM) {
666 FI_HILOGE("Invalid devNum:%{public}d", devNum);
667 return;
668 }
669 for (int32_t i = 0; i < devNum; i++) {
670 auto device = std::make_shared<Device>(INVALID_DEVICE_ID);
671 if (DeserializeDevice(device, packet) != RET_OK) {
672 FI_HILOGE("DeserializeDevice failed");
673 return;
674 }
675 event.devices.push_back(device);
676 }
677 SendEvent(CooperateEvent(
678 CooperateEventType::DSOFTBUS_INPUT_DEV_SYNC,
679 event));
680 }
681
OnRemoteHotPlug(const std::string & networkId,NetPacket & packet)682 void DSoftbusHandler::OnRemoteHotPlug(const std::string &networkId, NetPacket &packet)
683 {
684 CALL_INFO_TRACE;
685 DSoftbusHotPlugEvent event;
686 packet >> event.type;
687 FI_HILOGI("Hot plug type:%{public}d", event.type);
688 auto device = std::make_shared<Device>(INVALID_DEVICE_ID);
689 if (DeserializeDevice(device, packet) != RET_OK) {
690 FI_HILOGE("DeserializeDevice failed");
691 return;
692 }
693 event.device = device;
694 SendEvent(CooperateEvent(
695 CooperateEventType::DSOFTBUS_INPUT_DEV_HOT_PLUG,
696 event));
697 }
698
DeserializeDevice(std::shared_ptr<IDevice> device,NetPacket & packet)699 int32_t DSoftbusHandler::DeserializeDevice(std::shared_ptr<IDevice> device, NetPacket &packet)
700 {
701 CALL_DEBUG_ENTER;
702 CHKPR(device, RET_ERR);
703 int32_t data;
704 std::string str;
705 packet >> data;
706 device->SetId(data);
707 packet >> str;
708 device->SetDevPath(str);
709 packet >> str;
710 device->SetSysPath(str);
711 packet >> data;
712 device->SetBus(data);
713 packet >> data;
714 device->SetVendor(data);
715 packet >> data;
716 device->SetProduct(data);
717 packet >> data;
718 device->SetVersion(data);
719 packet >> str;
720 device->SetName(str);
721 packet >> str;
722 device->SetPhys(str);
723 packet >> str;
724 device->SetUniq(str);
725 bool isPointerDevice { false };
726 packet >> isPointerDevice;
727 if (isPointerDevice) {
728 device->AddCapability(IDevice::Capability::DEVICE_CAP_POINTER);
729 }
730 bool isKeyboard { false };
731 packet >> isKeyboard;
732 if (isKeyboard) {
733 device->AddCapability(IDevice::Capability::DEVICE_CAP_KEYBOARD);
734 }
735 int32_t keyboardType { static_cast<int32_t> (IDevice::KeyboardType::KEYBOARD_TYPE_NONE) };
736 packet >> keyboardType;
737 device->SetKeyboardType(static_cast<IDevice::KeyboardType>(keyboardType));
738 if (packet.ChkRWError()) {
739 FI_HILOGE("Packet read type failed");
740 return RET_ERR;
741 }
742 return RET_OK;
743 }
744 } // namespace Cooperate
745 } // namespace DeviceStatus
746 } // namespace Msdp
747 } // namespace OHOS
748