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_source_manager.h"
17
18 #include <algorithm>
19 #include <cinttypes>
20 #include <dlfcn.h>
21 #include <fstream>
22
23 #include "dinput_softbus_define.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "nlohmann/json.hpp"
27 #include "system_ability_definition.h"
28 #include "string_ex.h"
29
30 #include "distributed_hardware_fwk_kit.h"
31 #include "ipublisher_listener.h"
32
33 #include "constants_dinput.h"
34 #include "dinput_errcode.h"
35 #include "dinput_hitrace.h"
36 #include "dinput_log.h"
37 #include "dinput_state.h"
38 #include "dinput_utils_tool.h"
39 #include "distributed_input_client.h"
40 #include "distributed_input_inject.h"
41 #include "distributed_input_source_proxy.h"
42 #include "distributed_input_source_transport.h"
43 #include "hisysevent_util.h"
44 #include "hidumper.h"
45 #include "input_check_param.h"
46 #include "white_list_util.h"
47
48 namespace OHOS {
49 namespace DistributedHardware {
50 namespace DistributedInput {
51 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSourceManager, DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, true);
52
DistributedInputSourceManager(int32_t saId,bool runOnCreate)53 DistributedInputSourceManager::DistributedInputSourceManager(int32_t saId, bool runOnCreate)
54 : SystemAbility(saId, runOnCreate)
55 {
56 DHLOGI("DistributedInputSourceManager ctor!");
57 }
58
~DistributedInputSourceManager()59 DistributedInputSourceManager::~DistributedInputSourceManager()
60 {
61 DHLOGI("DistributedInputSourceManager dtor!");
62 startDScreenListener_ = nullptr;
63 stopDScreenListener_ = nullptr;
64 }
65
DInputSourceListener(DistributedInputSourceManager * manager)66 DistributedInputSourceManager::DInputSourceListener::DInputSourceListener(DistributedInputSourceManager *manager)
67 {
68 sourceManagerObj_ = manager;
69 DHLOGI("DInputSourceListener init.");
70 }
71
~DInputSourceListener()72 DistributedInputSourceManager::DInputSourceListener::~DInputSourceListener()
73 {
74 sourceManagerObj_ = nullptr;
75 DHLOGI("DInputSourceListener destory.");
76 }
77
OnResponseRegisterDistributedHardware(const std::string deviceId,const std::string dhId,bool result)78 void DistributedInputSourceManager::DInputSourceListener::OnResponseRegisterDistributedHardware(
79 const std::string deviceId, const std::string dhId, bool result)
80 {
81 DHLOGI("OnResponseRegisterDistributedHardware called, deviceId: %s, "
82 "result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed");
83 if (sourceManagerObj_ == nullptr) {
84 DHLOGE("OnResponseRegisterDistributedHardware sourceManagerObj is null.");
85 return;
86 }
87 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
88 sourceManagerObj_->RunRegisterCallback(deviceId, dhId,
89 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
90 DHLOGE("OnResponseRegisterDistributedHardware GetCallbackEventHandler is null.");
91 return;
92 }
93
94 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
95
96 nlohmann::json tmpJson;
97 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
98 tmpJson[INPUT_SOURCEMANAGER_KEY_HWID] = dhId;
99 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
100 jsonArrayMsg->push_back(tmpJson);
101 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
102 DINPUT_SOURCE_MANAGER_RIGISTER_MSG, jsonArrayMsg, 0);
103 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
104 }
105
OnResponsePrepareRemoteInput(const std::string deviceId,bool result,const std::string & object)106 void DistributedInputSourceManager::DInputSourceListener::OnResponsePrepareRemoteInput(const std::string deviceId,
107 bool result, const std::string &object)
108 {
109 DHLOGI("OnResponsePrepareRemoteInput called, deviceId: %s, result: %s.",
110 GetAnonyString(deviceId).c_str(), result ? "success" : "failed");
111
112 if (sourceManagerObj_ == nullptr) {
113 DHLOGE("OnResponsePrepareRemoteInput sourceManagerObj is null.");
114 return;
115 }
116 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
117 sourceManagerObj_->RunPrepareCallback(deviceId,
118 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL, object);
119 DHLOGE("OnResponsePrepareRemoteInput GetCallbackEventHandler is null.");
120 return;
121 }
122 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
123 nlohmann::json tmpJson;
124 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
125 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
126 tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object;
127 jsonArrayMsg->push_back(tmpJson);
128 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
129 DINPUT_SOURCE_MANAGER_PREPARE_MSG, jsonArrayMsg, 0);
130 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
131 }
132
OnResponseUnprepareRemoteInput(const std::string deviceId,bool result)133 void DistributedInputSourceManager::DInputSourceListener::OnResponseUnprepareRemoteInput(
134 const std::string deviceId, bool result)
135 {
136 DHLOGI("OnResponseUnprepareRemoteInput called, deviceId: %s, "
137 "result: %s.", GetAnonyString(deviceId).c_str(), result ? "success" : "failed");
138
139 if (sourceManagerObj_ == nullptr) {
140 DHLOGE("OnResponseUnprepareRemoteInput sourceManagerObj is null.");
141 return;
142 }
143 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
144 sourceManagerObj_->RunUnprepareCallback(deviceId,
145 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
146 DHLOGE("OnResponseUnprepareRemoteInput GetCallbackEventHandler is null.");
147 return;
148 }
149 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
150
151 nlohmann::json tmpJson;
152 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
153 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
154 jsonArrayMsg->push_back(tmpJson);
155 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
156 DINPUT_SOURCE_MANAGER_UNPREPARE_MSG, jsonArrayMsg, 0);
157 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
158 }
159
OnResponseRelayPrepareRemoteInput(int32_t toSrcSessionId,const std::string & deviceId,bool result,const std::string & object)160 void DistributedInputSourceManager::DInputSourceListener::OnResponseRelayPrepareRemoteInput(int32_t toSrcSessionId,
161 const std::string &deviceId, bool result, const std::string &object)
162 {
163 DHLOGI("OnResponseRelayPrepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result);
164 if (sourceManagerObj_ == nullptr) {
165 DHLOGE("sourceManagerObj is null.");
166 return;
167 }
168 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
169 DHLOGE("GetCallbackEventHandler is null.");
170 return;
171 }
172 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
173 nlohmann::json tmpJson;
174 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
175 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
176 tmpJson[INPUT_SOURCEMANAGER_KEY_WHITELIST] = object;
177 tmpJson[INPUT_SOURCEMANAGER_KEY_SESSIONID] = toSrcSessionId;
178 jsonArrayMsg->push_back(tmpJson);
179 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
180 DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_TO_ORIGIN, jsonArrayMsg, 0);
181 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
182 }
183
OnResponseRelayUnprepareRemoteInput(int32_t toSrcSessionId,const std::string & deviceId,bool result)184 void DistributedInputSourceManager::DInputSourceListener::OnResponseRelayUnprepareRemoteInput(int32_t toSrcSessionId,
185 const std::string &deviceId, bool result)
186 {
187 DHLOGI("OnResponseRelayUnprepareRemoteInput deviceId: %s, result: %d.", GetAnonyString(deviceId).c_str(), result);
188 if (sourceManagerObj_ == nullptr) {
189 DHLOGE("sourceManagerObj is null.");
190 return;
191 }
192 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
193 DHLOGE("GetCallbackEventHandler is null.");
194 return;
195 }
196 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
197 nlohmann::json tmpJson;
198 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
199 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
200 tmpJson[INPUT_SOURCEMANAGER_KEY_SESSIONID] = toSrcSessionId;
201 jsonArrayMsg->push_back(tmpJson);
202 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
203 DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_TO_ORIGIN, jsonArrayMsg, 0);
204 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
205 }
206
OnResponseStartRemoteInput(const std::string deviceId,const uint32_t inputTypes,bool result)207 void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteInput(
208 const std::string deviceId, const uint32_t inputTypes, bool result)
209 {
210 DHLOGI("OnResponseStartRemoteInput called, deviceId: %s, inputTypes: %d, result: %s.",
211 GetAnonyString(deviceId).c_str(), inputTypes, result ? "success" : "failed");
212
213 if (sourceManagerObj_ == nullptr) {
214 DHLOGE("sourceManagerObj is null.");
215 return;
216 }
217 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
218 sourceManagerObj_->RunStartCallback(deviceId, inputTypes,
219 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
220 DHLOGE("GetCallbackEventHandler is null.");
221 return;
222 }
223 if (result) {
224 sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_ON);
225 }
226
227 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
228 nlohmann::json tmpJson;
229 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
230 tmpJson[INPUT_SOURCEMANAGER_KEY_ITP] = inputTypes;
231 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
232 jsonArrayMsg->push_back(tmpJson);
233 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
234 DINPUT_SOURCE_MANAGER_START_MSG, jsonArrayMsg, 0);
235 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
236 }
237
OnResponseStopRemoteInput(const std::string deviceId,const uint32_t inputTypes,bool result)238 void DistributedInputSourceManager::DInputSourceListener::OnResponseStopRemoteInput(
239 const std::string deviceId, const uint32_t inputTypes, bool result)
240 {
241 DHLOGI("OnResponseStopRemoteInput called, deviceId: %s, inputTypes: %d, result: %s.",
242 GetAnonyString(deviceId).c_str(), inputTypes, result ? "true" : "failed");
243
244 if (sourceManagerObj_ == nullptr) {
245 DHLOGE("OnResponseStopRemoteInput sourceManagerObj_ is null.");
246 return;
247 }
248 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
249 DHLOGE("OnResponseStopRemoteInput GetCallbackEventHandler is null.");
250 sourceManagerObj_->RunStopCallback(deviceId, inputTypes,
251 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
252 return;
253 }
254 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
255
256 nlohmann::json tmpJson;
257 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
258 tmpJson[INPUT_SOURCEMANAGER_KEY_ITP] = inputTypes;
259 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
260 jsonArrayMsg->push_back(tmpJson);
261 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
262 DINPUT_SOURCE_MANAGER_STOP_MSG, jsonArrayMsg, 0);
263 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
264 }
265
OnResponseStartRemoteInputDhid(const std::string deviceId,const std::string & dhids,bool result)266 void DistributedInputSourceManager::DInputSourceListener::OnResponseStartRemoteInputDhid(
267 const std::string deviceId, const std::string &dhids, bool result)
268 {
269 DHLOGI("OnResponseStartRemoteInputDhid called, deviceId: %s, result: %s.",
270 GetAnonyString(deviceId).c_str(), result ? "success" : "failed");
271
272 if (sourceManagerObj_ == nullptr) {
273 DHLOGE("OnResponseStartRemoteInputDhid sourceManagerObj_ is null.");
274 return;
275 }
276 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
277 DHLOGE("OnResponseStartRemoteInputDhid GetCallbackEventHandler is null.");
278 sourceManagerObj_->RunStartDhidCallback(deviceId, dhids,
279 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
280 return;
281 }
282 if (result) {
283 sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_ON);
284 }
285
286 std::vector<std::string> vecStr;
287 StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr);
288 DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_IN, -1);
289
290 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
291 nlohmann::json tmpJson;
292 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
293 tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhids;
294 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
295 jsonArrayMsg->push_back(tmpJson);
296 AppExecFwk::InnerEvent::Pointer msgEvent =
297 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_START_DHID_MSG, jsonArrayMsg, 0);
298 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
299 }
300
OnResponseStopRemoteInputDhid(const std::string deviceId,const std::string & dhids,bool result)301 void DistributedInputSourceManager::DInputSourceListener::OnResponseStopRemoteInputDhid(
302 const std::string deviceId, const std::string &dhids, bool result)
303 {
304 DHLOGI("OnResponseStopRemoteInputDhid called, deviceId: %s, result: %s.",
305 GetAnonyString(deviceId).c_str(), result ? "success" : "failed");
306
307 if (sourceManagerObj_ == nullptr) {
308 DHLOGE("OnResponseStopRemoteInputDhid sourceManagerObj_ is null.");
309 return;
310 }
311 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
312 DHLOGE("OnResponseStopRemoteInputDhid GetCallbackEventHandler is null.");
313 sourceManagerObj_->RunStopDhidCallback(deviceId, dhids,
314 ERR_DH_INPUT_SERVER_SOURCE_MANAGERGET_CALLBACK_HANDLER_FAIL);
315 return;
316 }
317 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
318 nlohmann::json tmpJson;
319 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
320 tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhids;
321 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = result;
322 jsonArrayMsg->push_back(tmpJson);
323 AppExecFwk::InnerEvent::Pointer msgEvent =
324 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_STOP_DHID_MSG, jsonArrayMsg, 0);
325 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
326 }
327
OnResponseKeyState(const std::string deviceId,const std::string & dhid,const uint32_t type,const uint32_t code,const uint32_t value)328 void DistributedInputSourceManager::DInputSourceListener::OnResponseKeyState(const std::string deviceId,
329 const std::string &dhid, const uint32_t type, const uint32_t code, const uint32_t value)
330 {
331 DHLOGI("OnResponseKeyState called, deviceId: %s, dhid: %s.", GetAnonyString(deviceId).c_str(),
332 GetAnonyString(dhid).c_str());
333 if (sourceManagerObj_ == nullptr) {
334 DHLOGE("sourceManagerObj is null.");
335 return;
336 }
337 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
338 DHLOGE("GetCallbackEventHandler is null.");
339 sourceManagerObj_->RunKeyStateCallback(deviceId, dhid, type, code, value);
340 return;
341 }
342 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
343 nlohmann::json tmpJson;
344 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = deviceId;
345 tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhid;
346 tmpJson[INPUT_SOURCEMANAGER_KEY_TYPE] = type;
347 tmpJson[INPUT_SOURCEMANAGER_KEY_CODE] = code;
348 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = value;
349 jsonArrayMsg->push_back(tmpJson);
350 AppExecFwk::InnerEvent::Pointer msgEvent =
351 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_KEY_STATE_MSG, jsonArrayMsg, 0);
352 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
353 }
354
OnReceivedEventRemoteInput(const std::string deviceId,const std::string & event)355 void DistributedInputSourceManager::DInputSourceListener::OnReceivedEventRemoteInput(
356 const std::string deviceId, const std::string &event)
357 {
358 nlohmann::json inputData = nlohmann::json::parse(event, nullptr, false);
359 if (inputData.is_discarded()) {
360 DHLOGE("inputData parse failed!");
361 return;
362 }
363 size_t jsonSize = inputData.size();
364 DHLOGI("OnReceivedEventRemoteInput called, deviceId: %s, json size:%d.",
365 GetAnonyString(deviceId).c_str(), jsonSize);
366
367 if (!inputData.is_array()) {
368 DHLOGE("inputData not vector!");
369 return;
370 }
371
372 RawEvent mEventBuffer[jsonSize];
373 int idx = 0;
374 for (auto it = inputData.begin(); it != inputData.end(); ++it) {
375 nlohmann::json oneData = (*it);
376 if (!IsInt64(oneData, INPUT_KEY_WHEN) || !IsUInt32(oneData, INPUT_KEY_TYPE) ||
377 !IsUInt32(oneData, INPUT_KEY_CODE) || !IsInt32(oneData, INPUT_KEY_VALUE) ||
378 !IsString(oneData, INPUT_KEY_DESCRIPTOR) || !IsString(oneData, INPUT_KEY_PATH)) {
379 DHLOGE("The key is invaild.");
380 continue;
381 }
382 mEventBuffer[idx].when = oneData[INPUT_KEY_WHEN];
383 mEventBuffer[idx].type = oneData[INPUT_KEY_TYPE];
384 mEventBuffer[idx].code = oneData[INPUT_KEY_CODE];
385 mEventBuffer[idx].value = oneData[INPUT_KEY_VALUE];
386 mEventBuffer[idx].descriptor = oneData[INPUT_KEY_DESCRIPTOR];
387 mEventBuffer[idx].path = oneData[INPUT_KEY_PATH];
388 RecordEventLog(oneData[INPUT_KEY_WHEN], oneData[INPUT_KEY_TYPE], oneData[INPUT_KEY_CODE],
389 oneData[INPUT_KEY_VALUE], oneData[INPUT_KEY_PATH]);
390 ++idx;
391 }
392 DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer, jsonSize);
393 }
394
OnReceiveRelayPrepareResult(int32_t status,const std::string & srcId,const std::string & sinkId)395 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayPrepareResult(int32_t status,
396 const std::string &srcId, const std::string &sinkId)
397 {
398 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
399 if (sourceManagerObj_ == nullptr) {
400 DHLOGE("sourceManagerObj is null.");
401 return;
402 }
403 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
404 DHLOGE("GetCallbackEventHandler is null.");
405 return;
406 }
407 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
408 nlohmann::json tmpJson;
409 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
410 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
411 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
412 jsonArrayMsg->push_back(tmpJson);
413 AppExecFwk::InnerEvent::Pointer msgEvent =
414 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_MMI, jsonArrayMsg, 0);
415 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
416 }
417
OnReceiveRelayUnprepareResult(int32_t status,const std::string & srcId,const std::string & sinkId)418 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayUnprepareResult(int32_t status,
419 const std::string &srcId, const std::string &sinkId)
420 {
421 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
422 if (sourceManagerObj_ == nullptr) {
423 DHLOGE("sourceManagerObj is null.");
424 return;
425 }
426 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
427 DHLOGE("GetCallbackEventHandler is null.");
428 return;
429 }
430 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
431 nlohmann::json tmpJson;
432 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
433 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
434 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
435 jsonArrayMsg->push_back(tmpJson);
436 AppExecFwk::InnerEvent::Pointer msgEvent =
437 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_MMI, jsonArrayMsg, 0);
438 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
439 }
440
OnReceiveRelayStartDhidResult(int32_t status,const std::string & srcId,const std::string & sinkId,const std::string & dhids)441 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayStartDhidResult(int32_t status,
442 const std::string &srcId, const std::string &sinkId, const std::string &dhids)
443 {
444 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
445 if (sourceManagerObj_ == nullptr) {
446 DHLOGE("sourceManagerObj is null.");
447 return;
448 }
449 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
450 DHLOGE("GetCallbackEventHandler is null.");
451 return;
452 }
453 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
454 nlohmann::json tmpJson;
455 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
456 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
457 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
458 tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhids;
459 jsonArrayMsg->push_back(tmpJson);
460 AppExecFwk::InnerEvent::Pointer msgEvent =
461 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI, jsonArrayMsg, 0);
462 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
463 }
464
OnReceiveRelayStopDhidResult(int32_t status,const std::string & srcId,const std::string & sinkId,const std::string & dhids)465 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayStopDhidResult(int32_t status,
466 const std::string &srcId, const std::string &sinkId, const std::string &dhids)
467 {
468 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
469 if (sourceManagerObj_ == nullptr) {
470 DHLOGE("sourceManagerObj is null.");
471 return;
472 }
473 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
474 DHLOGE("GetCallbackEventHandler is null.");
475 return;
476 }
477 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
478 nlohmann::json tmpJson;
479 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
480 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
481 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
482 tmpJson[INPUT_SOURCEMANAGER_KEY_DHID] = dhids;
483 jsonArrayMsg->push_back(tmpJson);
484 AppExecFwk::InnerEvent::Pointer msgEvent =
485 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_STOPDHID_RESULT_MMI, jsonArrayMsg, 0);
486 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
487 }
488
OnReceiveRelayStartTypeResult(int32_t status,const std::string & srcId,const std::string & sinkId,uint32_t inputTypes)489 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayStartTypeResult(int32_t status,
490 const std::string &srcId, const std::string &sinkId, uint32_t inputTypes)
491 {
492 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
493 if (sourceManagerObj_ == nullptr) {
494 DHLOGE("sourceManagerObj is null.");
495 return;
496 }
497 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
498 DHLOGE("GetCallbackEventHandler is null.");
499 return;
500 }
501 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
502 nlohmann::json tmpJson;
503 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
504 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
505 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
506 tmpJson[INPUT_SOURCEMANAGER_KEY_TYPE] = inputTypes;
507 jsonArrayMsg->push_back(tmpJson);
508 AppExecFwk::InnerEvent::Pointer msgEvent =
509 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_STARTTYPE_RESULT_MMI, jsonArrayMsg, 0);
510 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
511 }
512
OnReceiveRelayStopTypeResult(int32_t status,const std::string & srcId,const std::string & sinkId,uint32_t inputTypes)513 void DistributedInputSourceManager::DInputSourceListener::OnReceiveRelayStopTypeResult(int32_t status,
514 const std::string &srcId, const std::string &sinkId, uint32_t inputTypes)
515 {
516 DHLOGI("status:%d, srcId: %s, sinkId: %s.", status, GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
517 if (sourceManagerObj_ == nullptr) {
518 DHLOGE("sourceManagerObj is null.");
519 return;
520 }
521 if (sourceManagerObj_->GetCallbackEventHandler() == nullptr) {
522 DHLOGE("GetCallbackEventHandler is null.");
523 return;
524 }
525 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
526 nlohmann::json tmpJson;
527 tmpJson[INPUT_SOURCEMANAGER_KEY_SRC_DEVID] = srcId;
528 tmpJson[INPUT_SOURCEMANAGER_KEY_SINK_DEVID] = sinkId;
529 tmpJson[INPUT_SOURCEMANAGER_KEY_VALUE] = status;
530 tmpJson[INPUT_SOURCEMANAGER_KEY_TYPE] = inputTypes;
531 jsonArrayMsg->push_back(tmpJson);
532 AppExecFwk::InnerEvent::Pointer msgEvent =
533 AppExecFwk::InnerEvent::Get(DINPUT_SOURCE_MANAGER_RELAY_STOPTYPE_RESULT_MMI, jsonArrayMsg, 0);
534 sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
535 }
536
OnStart()537 void DistributedInputSourceManager::OnStart()
538 {
539 if (serviceRunningState_ == ServiceSourceRunningState::STATE_RUNNING) {
540 DHLOGI("dinput Manager Service has already started.");
541 return;
542 }
543 DHLOGI("dinput Manager Service started.");
544 if (!InitAuto()) {
545 DHLOGI("failed to init service.");
546 return;
547 }
548 serviceRunningState_ = ServiceSourceRunningState::STATE_RUNNING;
549 runner_->Run();
550
551 /*
552 * Publish service maybe failed, so we need call this function at the last,
553 * so it can't affect the TDD test program.
554 */
555 bool ret = Publish(this);
556 if (!ret) {
557 return;
558 }
559
560 DHLOGI("DistributedInputSourceManager start success.");
561 }
562
InitAuto()563 bool DistributedInputSourceManager::InitAuto()
564 {
565 runner_ = AppExecFwk::EventRunner::Create(true);
566 if (runner_ == nullptr) {
567 return false;
568 }
569
570 handler_ = std::make_shared<DistributedInputSourceEventHandler>(runner_);
571
572 DHLOGI("init success");
573
574 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
575 callBackHandler_ = std::make_shared<DistributedInputSourceManager::DInputSourceManagerEventHandler>(runner, this);
576
577 return true;
578 }
579
DInputSourceManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,DistributedInputSourceManager * manager)580 DistributedInputSourceManager::DInputSourceManagerEventHandler::DInputSourceManagerEventHandler(
581 const std::shared_ptr<AppExecFwk::EventRunner> &runner, DistributedInputSourceManager *manager)
582 : AppExecFwk::EventHandler(runner)
583 {
584 eventFuncMap_[DINPUT_SOURCE_MANAGER_RIGISTER_MSG] = &DInputSourceManagerEventHandler::NotifyRegisterCallback;
585 eventFuncMap_[DINPUT_SOURCE_MANAGER_UNRIGISTER_MSG] = &DInputSourceManagerEventHandler::NotifyUnregisterCallback;
586 eventFuncMap_[DINPUT_SOURCE_MANAGER_PREPARE_MSG] = &DInputSourceManagerEventHandler::NotifyPrepareCallback;
587 eventFuncMap_[DINPUT_SOURCE_MANAGER_UNPREPARE_MSG] = &DInputSourceManagerEventHandler::NotifyUnprepareCallback;
588 eventFuncMap_[DINPUT_SOURCE_MANAGER_START_MSG] = &DInputSourceManagerEventHandler::NotifyStartCallback;
589 eventFuncMap_[DINPUT_SOURCE_MANAGER_STOP_MSG] = &DInputSourceManagerEventHandler::NotifyStopCallback;
590 eventFuncMap_[DINPUT_SOURCE_MANAGER_START_DHID_MSG] = &DInputSourceManagerEventHandler::NotifyStartDhidCallback;
591 eventFuncMap_[DINPUT_SOURCE_MANAGER_STOP_DHID_MSG] = &DInputSourceManagerEventHandler::NotifyStopDhidCallback;
592 eventFuncMap_[DINPUT_SOURCE_MANAGER_KEY_STATE_MSG] = &DInputSourceManagerEventHandler::NotifyKeyStateCallback;
593 eventFuncMap_[DINPUT_SOURCE_MANAGER_STARTSERVER_MSG] = &DInputSourceManagerEventHandler::NotifyStartServerCallback;
594 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_TO_ORIGIN] =
595 &DInputSourceManagerEventHandler::NotifyRelayPrepareRemoteInput;
596 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_TO_ORIGIN] =
597 &DInputSourceManagerEventHandler::NotifyRelayUnprepareRemoteInput;
598 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_MMI] =
599 &DInputSourceManagerEventHandler::NotifyRelayPrepareCallback;
600 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_MMI] =
601 &DInputSourceManagerEventHandler::NotifyRelayUnprepareCallback;
602 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI] =
603 &DInputSourceManagerEventHandler::NotifyRelayStartDhidCallback;
604 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_STOPDHID_RESULT_MMI] =
605 &DInputSourceManagerEventHandler::NotifyRelayStopDhidCallback;
606 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_STARTTYPE_RESULT_MMI] =
607 &DInputSourceManagerEventHandler::NotifyRelayStartTypeCallback;
608 eventFuncMap_[DINPUT_SOURCE_MANAGER_RELAY_STOPTYPE_RESULT_MMI] =
609 &DInputSourceManagerEventHandler::NotifyRelayStopTypeCallback;
610
611 sourceManagerObj_ = manager;
612 }
613
~DInputSourceManagerEventHandler()614 DistributedInputSourceManager::DInputSourceManagerEventHandler::~DInputSourceManagerEventHandler()
615 {
616 eventFuncMap_.clear();
617 sourceManagerObj_ = nullptr;
618 }
619
NotifyRegisterCallback(const AppExecFwk::InnerEvent::Pointer & event)620 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRegisterCallback(
621 const AppExecFwk::InnerEvent::Pointer &event)
622 {
623 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
624 auto it = dataMsg->begin();
625 nlohmann::json innerMsg = *(it);
626 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
627 std::string dhId = innerMsg[INPUT_SOURCEMANAGER_KEY_HWID];
628 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
629
630 InputDeviceId inputDeviceId {deviceId, dhId};
631 std::vector<InputDeviceId> tmpInputDevId = sourceManagerObj_->GetInputDeviceId();
632 // Find out if the dh exists
633 auto devIt = std::find(tmpInputDevId.begin(), tmpInputDevId.end(), inputDeviceId);
634 if (devIt != tmpInputDevId.end()) {
635 if (result == false) {
636 sourceManagerObj_->RemoveInputDeviceId(deviceId, dhId);
637 }
638 } else {
639 DHLOGW("ProcessEvent DINPUT_SOURCE_MANAGER_RIGISTER_MSG the "
640 "devId: %s, dhId: %s is bad data.", GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str());
641 }
642
643 sourceManagerObj_->RunRegisterCallback(deviceId, dhId,
644 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_MSG_IS_BAD);
645 }
646
NotifyUnregisterCallback(const AppExecFwk::InnerEvent::Pointer & event)647 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnregisterCallback(
648 const AppExecFwk::InnerEvent::Pointer &event)
649 {
650 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
651 auto it = dataMsg->begin();
652 nlohmann::json innerMsg = *(it);
653 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
654 std::string dhId = innerMsg[INPUT_SOURCEMANAGER_KEY_HWID];
655 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
656 if (result) {
657 sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_OFF);
658 }
659 sourceManagerObj_->RunUnregisterCallback(deviceId, dhId,
660 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_MSG_IS_BAD);
661 }
662
NotifyPrepareCallback(const AppExecFwk::InnerEvent::Pointer & event)663 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyPrepareCallback(
664 const AppExecFwk::InnerEvent::Pointer &event)
665 {
666 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
667 auto it = dataMsg->begin();
668 nlohmann::json innerMsg = *(it);
669 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
670 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
671 std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST];
672
673 sourceManagerObj_->RunPrepareCallback(deviceId,
674 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_MSG_IS_BAD, object);
675 }
676
NotifyUnprepareCallback(const AppExecFwk::InnerEvent::Pointer & event)677 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyUnprepareCallback(
678 const AppExecFwk::InnerEvent::Pointer &event)
679 {
680 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
681 auto it = dataMsg->begin();
682 nlohmann::json innerMsg = *(it);
683 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
684 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
685 if (result) {
686 sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_OFF);
687 }
688 sourceManagerObj_->RunUnprepareCallback(deviceId,
689 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_MSG_IS_BAD);
690 }
691
NotifyStartCallback(const AppExecFwk::InnerEvent::Pointer & event)692 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartCallback(
693 const AppExecFwk::InnerEvent::Pointer &event)
694 {
695 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
696 auto it = dataMsg->begin();
697 nlohmann::json innerMsg = *(it);
698 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
699 uint32_t inputTypes = innerMsg[INPUT_SOURCEMANAGER_KEY_ITP];
700 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
701 DHLOGI("Start DInput Recv Callback ret: %s, devId: %s, inputTypes: %d",
702 result ? "true" : "false", GetAnonyString(deviceId).c_str(), inputTypes);
703 if (result) {
704 sourceManagerObj_->SetInputTypesMap(
705 deviceId, sourceManagerObj_->GetInputTypesMap(deviceId) | inputTypes);
706 }
707 sourceManagerObj_->SetStartTransFlag((result && (sourceManagerObj_->GetInputTypesMap(deviceId) > 0)) ?
708 DInputServerType::SOURCE_SERVER_TYPE : DInputServerType::NULL_SERVER_TYPE);
709 sourceManagerObj_->RunStartCallback(deviceId, inputTypes,
710 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD);
711 }
712
NotifyStopCallback(const AppExecFwk::InnerEvent::Pointer & event)713 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopCallback(
714 const AppExecFwk::InnerEvent::Pointer &event)
715 {
716 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
717 auto it = dataMsg->begin();
718 nlohmann::json innerMsg = *(it);
719 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
720 uint32_t inputTypes = innerMsg[INPUT_SOURCEMANAGER_KEY_ITP];
721 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
722
723 DHLOGI("Stop DInput Recv Callback ret: %B, devId: %s, inputTypes: %d",
724 result, GetAnonyString(deviceId).c_str(), inputTypes);
725 if (result && (sourceManagerObj_->GetInputTypesMap(deviceId) & inputTypes)) {
726 sourceManagerObj_->SetInputTypesMap(
727 deviceId, sourceManagerObj_->GetInputTypesMap(deviceId) -
728 (sourceManagerObj_->GetInputTypesMap(deviceId) & inputTypes));
729 }
730
731 if (sourceManagerObj_->GetInputTypesMap(deviceId) == 0) {
732 sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_OFF);
733 }
734
735 // DeviceMap_ all sink device switch is off,call isstart's callback
736 bool isAllDevSwitchOff = sourceManagerObj_->GetDeviceMapAllDevSwitchOff();
737 if (isAllDevSwitchOff) {
738 DHLOGI("All Dev Switch Off");
739 sourceManagerObj_->SetStartTransFlag(DInputServerType::NULL_SERVER_TYPE);
740 }
741 sourceManagerObj_->RunStopCallback(deviceId, inputTypes,
742 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_MSG_IS_BAD);
743 }
744
NotifyStartDhidCallback(const AppExecFwk::InnerEvent::Pointer & event)745 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartDhidCallback(
746 const AppExecFwk::InnerEvent::Pointer &event)
747 {
748 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
749 auto it = dataMsg->begin();
750 nlohmann::json innerMsg = *(it);
751 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
752 std::string dhidStr = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID];
753 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
754
755 sourceManagerObj_->RunStartDhidCallback(deviceId, dhidStr,
756 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_MSG_IS_BAD);
757 }
758
NotifyStopDhidCallback(const AppExecFwk::InnerEvent::Pointer & event)759 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStopDhidCallback(
760 const AppExecFwk::InnerEvent::Pointer &event)
761 {
762 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
763 auto it = dataMsg->begin();
764 nlohmann::json innerMsg = *(it);
765 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
766 std::string dhidStr = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID];
767 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
768
769 sourceManagerObj_->RunStopDhidCallback(deviceId, dhidStr,
770 result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_MSG_IS_BAD);
771 }
772
NotifyKeyStateCallback(const AppExecFwk::InnerEvent::Pointer & event)773 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyKeyStateCallback(
774 const AppExecFwk::InnerEvent::Pointer &event)
775 {
776 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
777 auto it = dataMsg->begin();
778 nlohmann::json innerMsg = *(it);
779 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
780 std::string dhid = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID];
781 uint32_t keyType = innerMsg[INPUT_SOURCEMANAGER_KEY_TYPE];
782 uint32_t keyCode = innerMsg[INPUT_SOURCEMANAGER_KEY_CODE];
783 uint32_t keyValue = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
784
785 sourceManagerObj_->RunKeyStateCallback(deviceId, dhid, keyType, keyCode, keyValue);
786 }
787
NotifyStartServerCallback(const AppExecFwk::InnerEvent::Pointer & event)788 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyStartServerCallback(
789 const AppExecFwk::InnerEvent::Pointer &event)
790 {
791 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
792 auto it = dataMsg->begin();
793 nlohmann::json innerMsg = *(it);
794 int32_t serType = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
795 DInputServerType startTransFlag = DInputServerType(serType);
796 sourceManagerObj_->SetStartTransFlag(startTransFlag);
797 }
798
799
NotifyRelayPrepareCallback(const AppExecFwk::InnerEvent::Pointer & event)800 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayPrepareCallback(
801 const AppExecFwk::InnerEvent::Pointer &event)
802 {
803 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
804 nlohmann::json::iterator it = dataMsg->begin();
805 nlohmann::json innerMsg = *(it);
806 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
807 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
808 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
809
810 sourceManagerObj_->RunRelayPrepareCallback(srcId, sinkId, status);
811 }
812
NotifyRelayUnprepareCallback(const AppExecFwk::InnerEvent::Pointer & event)813 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayUnprepareCallback(
814 const AppExecFwk::InnerEvent::Pointer &event)
815 {
816 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
817 nlohmann::json::iterator it = dataMsg->begin();
818 nlohmann::json innerMsg = *(it);
819 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
820 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
821 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
822
823 sourceManagerObj_->RunRelayUnprepareCallback(srcId, sinkId, status);
824 }
825
NotifyRelayPrepareRemoteInput(const AppExecFwk::InnerEvent::Pointer & event)826 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayPrepareRemoteInput(
827 const AppExecFwk::InnerEvent::Pointer &event)
828 {
829 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
830 nlohmann::json::iterator it = dataMsg->begin();
831 nlohmann::json innerMsg = *(it);
832 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
833 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
834 std::string object = innerMsg[INPUT_SOURCEMANAGER_KEY_WHITELIST];
835 int32_t toSrcSessionId = innerMsg[INPUT_SOURCEMANAGER_KEY_SESSIONID];
836 DHLOGI("Device whitelist object: %s", object.c_str());
837 std::string localNetworkId = GetLocalNetworkId();
838 if (localNetworkId.empty()) {
839 return;
840 }
841
842 // notify to origin sourcesa result.
843 int32_t ret = DistributedInputSourceTransport::GetInstance().NotifyOriginPrepareResult(toSrcSessionId,
844 localNetworkId, deviceId, result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_MSG_IS_BAD);
845 if (ret != DH_SUCCESS) {
846 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_TO_ORIGIN notify is fail.");
847 return;
848 }
849 sourceManagerObj_->RunWhiteListCallback(deviceId, object);
850 }
851
NotifyRelayUnprepareRemoteInput(const AppExecFwk::InnerEvent::Pointer & event)852 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayUnprepareRemoteInput(
853 const AppExecFwk::InnerEvent::Pointer &event)
854 {
855 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
856 nlohmann::json::iterator it = dataMsg->begin();
857 nlohmann::json innerMsg = *(it);
858 std::string deviceId = innerMsg[INPUT_SOURCEMANAGER_KEY_DEVID];
859 bool result = innerMsg[INPUT_SOURCEMANAGER_KEY_RESULT];
860 int32_t toSrcSessionId = innerMsg[INPUT_SOURCEMANAGER_KEY_SESSIONID];
861 std::string localNetworkId = GetLocalNetworkId();
862 if (localNetworkId.empty()) {
863 return;
864 }
865
866 // notify to origin sourcesa result.
867 int32_t ret = DistributedInputSourceTransport::GetInstance().NotifyOriginUnprepareResult(toSrcSessionId,
868 localNetworkId, deviceId, result ? DH_SUCCESS : ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_MSG_IS_BAD);
869 if (ret != DH_SUCCESS) {
870 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_TO_ORIGIN notify is fail.");
871 return;
872 }
873 }
874
NotifyRelayStartDhidCallback(const AppExecFwk::InnerEvent::Pointer & event)875 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStartDhidCallback(
876 const AppExecFwk::InnerEvent::Pointer &event)
877 {
878 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
879 nlohmann::json::iterator it = dataMsg->begin();
880 nlohmann::json innerMsg = *(it);
881 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
882 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
883 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
884 std::string dhids = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID];
885
886 sourceManagerObj_->RunRelayStartDhidCallback(srcId, sinkId, status, dhids);
887 }
888
NotifyRelayStopDhidCallback(const AppExecFwk::InnerEvent::Pointer & event)889 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStopDhidCallback(
890 const AppExecFwk::InnerEvent::Pointer &event)
891 {
892 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
893 nlohmann::json::iterator it = dataMsg->begin();
894 nlohmann::json innerMsg = *(it);
895 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
896 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
897 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
898 std::string dhids = innerMsg[INPUT_SOURCEMANAGER_KEY_DHID];
899
900 sourceManagerObj_->RunRelayStopDhidCallback(srcId, sinkId, status, dhids);
901 }
902
NotifyRelayStartTypeCallback(const AppExecFwk::InnerEvent::Pointer & event)903 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStartTypeCallback(
904 const AppExecFwk::InnerEvent::Pointer &event)
905 {
906 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
907 nlohmann::json::iterator it = dataMsg->begin();
908 nlohmann::json innerMsg = *(it);
909 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
910 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
911 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
912 uint32_t inputTypes = innerMsg[INPUT_SOURCEMANAGER_KEY_TYPE];
913
914 sourceManagerObj_->RunRelayStartTypeCallback(srcId, sinkId, status, inputTypes);
915 }
916
NotifyRelayStopTypeCallback(const AppExecFwk::InnerEvent::Pointer & event)917 void DistributedInputSourceManager::DInputSourceManagerEventHandler::NotifyRelayStopTypeCallback(
918 const AppExecFwk::InnerEvent::Pointer &event)
919 {
920 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
921 nlohmann::json::iterator it = dataMsg->begin();
922 nlohmann::json innerMsg = *(it);
923 int32_t status = innerMsg[INPUT_SOURCEMANAGER_KEY_VALUE];
924 std::string srcId = innerMsg[INPUT_SOURCEMANAGER_KEY_SRC_DEVID];
925 std::string sinkId = innerMsg[INPUT_SOURCEMANAGER_KEY_SINK_DEVID];
926 uint32_t inputTypes = innerMsg[INPUT_SOURCEMANAGER_KEY_TYPE];
927
928 sourceManagerObj_->RunRelayStopTypeCallback(srcId, sinkId, status, inputTypes);
929 }
930
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)931 void DistributedInputSourceManager::DInputSourceManagerEventHandler::ProcessEvent(
932 const AppExecFwk::InnerEvent::Pointer &event)
933 {
934 auto iter = eventFuncMap_.find(event->GetInnerEventId());
935 if (iter == eventFuncMap_.end()) {
936 DHLOGE("Event Id %d is undefined.", event->GetInnerEventId());
937 return;
938 }
939 SourceEventFunc &func = iter->second;
940 (this->*func)(event);
941 }
942
OnStop()943 void DistributedInputSourceManager::OnStop()
944 {
945 DHLOGI("stop service");
946 runner_.reset();
947 handler_.reset();
948 serviceRunningState_ = ServiceSourceRunningState::STATE_NOT_START;
949 }
950
Init()951 int32_t DistributedInputSourceManager::Init()
952 {
953 DHLOGI("enter");
954 isStartTrans_ = DInputServerType::NULL_SERVER_TYPE;
955
956 // transport init session
957 int32_t ret = DistributedInputSourceTransport::GetInstance().Init();
958 if (ret != DH_SUCCESS) {
959 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL;
960 }
961
962 statuslistener_ = std::make_shared<DInputSourceListener>(this);
963 DistributedInputSourceTransport::GetInstance().RegisterSourceRespCallback(statuslistener_);
964
965 serviceRunningState_ = ServiceSourceRunningState::STATE_RUNNING;
966
967 std::shared_ptr<DistributedHardwareFwkKit> dhFwkKit = DInputContext::GetInstance().GetDHFwkKit();
968 if (dhFwkKit == nullptr) {
969 DHLOGE("dhFwkKit obtain fail!");
970 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL;
971 }
972 startDScreenListener_ = new(std::nothrow) StartDScreenListener;
973 stopDScreenListener_ = new(std::nothrow) StopDScreenListener;
974 deviceOfflineListener_ = new(std::nothrow) DeviceOfflineListener(this);
975 dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, startDScreenListener_);
976 dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_);
977 dhFwkKit->RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, deviceOfflineListener_);
978
979 ret = DInputState::GetInstance().Init();
980 if (ret != DH_SUCCESS) {
981 DHLOGE("DInputState init fail!");
982 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INIT_FAIL;
983 }
984
985 return DH_SUCCESS;
986 }
987
Release()988 int32_t DistributedInputSourceManager::Release()
989 {
990 DHLOGI("Release source manager.");
991 for (auto iter = inputDevice_.begin(); iter != inputDevice_.end(); ++iter) {
992 std::string devId = iter->devId;
993 std::string dhId = iter->dhId;
994 DHLOGI("Release devId: %s, dhId: %s.", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
995 int32_t ret = DistributedInputInject::GetInstance().UnregisterDistributedHardware(devId, dhId);
996 if (ret != DH_SUCCESS) {
997 DHLOGW("DinputSourceManager Release called, remove node fail.");
998 }
999 }
1000 DistributedInputSourceTransport::GetInstance().Release();
1001 inputDevice_.clear();
1002 DeviceMap_.clear();
1003 InputTypesMap_.clear();
1004
1005 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
1006 nlohmann::json tmpJson;
1007 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = static_cast<int32_t>(DInputServerType::NULL_SERVER_TYPE);
1008 jsonArrayMsg->push_back(tmpJson);
1009 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
1010 DINPUT_SOURCE_MANAGER_STARTSERVER_MSG, jsonArrayMsg, 0);
1011 if (callBackHandler_ != nullptr) {
1012 DHLOGI("Sourcemanager send event success.");
1013 callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
1014 }
1015 serviceRunningState_ = ServiceSourceRunningState::STATE_NOT_START;
1016 UnregisterDHFwkPublisher();
1017
1018 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_EXIT, "dinput source sa exit success.");
1019 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1020 if (systemAbilityMgr == nullptr) {
1021 DHLOGE("Failed to get SystemAbilityManager.");
1022 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_RELEASE_FAIL;
1023 }
1024 int32_t ret = systemAbilityMgr->UnloadSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID);
1025 if (ret != DH_SUCCESS) {
1026 DHLOGE("Failed to UnloadSystemAbility service! errcode: %d.", ret);
1027 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_RELEASE_FAIL;
1028 }
1029 DHLOGI("Source unloadSystemAbility successfully.");
1030 return DH_SUCCESS;
1031 }
1032
UnregisterDHFwkPublisher()1033 void DistributedInputSourceManager::UnregisterDHFwkPublisher()
1034 {
1035 std::shared_ptr<DistributedHardwareFwkKit> dhFwkKit = DInputContext::GetInstance().GetDHFwkKit();
1036 if (dhFwkKit != nullptr && startDScreenListener_ != nullptr) {
1037 DHLOGI("UnPublish StartDScreenListener");
1038 dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, startDScreenListener_);
1039 }
1040 if (dhFwkKit != nullptr && stopDScreenListener_ != nullptr) {
1041 DHLOGI("UnPublish StopDScreenListener");
1042 dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, stopDScreenListener_);
1043 }
1044 if (dhFwkKit != nullptr && deviceOfflineListener_ != nullptr) {
1045 DHLOGI("UnPublish DeviceOfflineListener");
1046 dhFwkKit->UnregisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, deviceOfflineListener_);
1047 }
1048 if (dhFwkKit != nullptr) {
1049 DHLOGD("Disable low Latency!");
1050 dhFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, DISABLE_LOW_LATENCY.dump());
1051 }
1052 }
1053
CheckRegisterParam(const std::string & devId,const std::string & dhId,const std::string & parameters,sptr<IRegisterDInputCallback> callback)1054 bool DistributedInputSourceManager::CheckRegisterParam(const std::string &devId, const std::string &dhId,
1055 const std::string ¶meters, sptr<IRegisterDInputCallback> callback)
1056 {
1057 if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) {
1058 DHLOGE("CheckParam devId is empty or devId size too long.");
1059 return false;
1060 }
1061 if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) {
1062 DHLOGE("CheckParam dhId is empty or dhId size too long.");
1063 return false;
1064 }
1065 if (parameters.empty()) {
1066 DHLOGE("CheckParam parameters is empty.");
1067 return false;
1068 }
1069 if (callback == nullptr) {
1070 DHLOGE("CheckParam callback is null.");
1071 return false;
1072 }
1073 return true;
1074 }
1075
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & parameters,sptr<IRegisterDInputCallback> callback)1076 int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
1077 const std::string ¶meters, sptr<IRegisterDInputCallback> callback)
1078 {
1079 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_REGISTER, devId, dhId, "dinput register call.");
1080 DHLOGI("RegisterDistributedHardware called, deviceId: %s, dhId: %s, parameters: %s",
1081 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str());
1082 if (!CheckRegisterParam(devId, dhId, parameters, callback)) {
1083 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_REGISTER_FAIL, devId, dhId,
1084 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL, "Dinputregister failed callback is nullptr.");
1085 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL;
1086 }
1087 std::lock_guard<std::mutex> lock(operationMutex_);
1088 DInputClientRegistInfo info {devId, dhId, callback};
1089 regCallbacks_.push_back(info);
1090 InputDeviceId inputDeviceId {devId, dhId, GetNodeDesc(parameters)};
1091 DHLOGI("RegisterDistributedHardware deviceId: %s, dhId: %s",
1092 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
1093
1094 // 1.Find out if the dh exists
1095 auto it = std::find(inputDevice_.begin(), inputDevice_.end(), inputDeviceId);
1096 if (it != inputDevice_.end()) {
1097 callback->OnResult(devId, dhId, DH_SUCCESS);
1098 return DH_SUCCESS;
1099 }
1100
1101 // 2.create input node
1102 int32_t ret = DistributedInputInject::GetInstance().RegisterDistributedHardware(devId, dhId, parameters);
1103 if (ret != DH_SUCCESS) {
1104 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_REGISTER_FAIL, devId, dhId,
1105 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL,
1106 "dinput register distributed hardware failed in create input node.");
1107 DHLOGE("RegisterDistributedHardware called, create node fail.");
1108
1109 for (auto iter = regCallbacks_.begin(); iter != regCallbacks_.end(); ++iter) {
1110 if (iter->devId == devId && iter->dhId == dhId) {
1111 iter->callback->OnResult(iter->devId, iter->dhId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL);
1112 regCallbacks_.erase(iter);
1113 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL;
1114 }
1115 }
1116 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL;
1117 }
1118
1119 // 3.save device
1120 DHLOGI("inputDevice push deviceId: %s, dhId: %s", GetAnonyString(inputDeviceId.devId).c_str(),
1121 GetAnonyString(inputDeviceId.dhId).c_str());
1122 inputDevice_.push_back(inputDeviceId);
1123
1124 // 4.notify source distributedfwk register hardware success
1125 callback->OnResult(devId, dhId, DH_SUCCESS);
1126
1127 // 5. notify remote side that this side is registerd remote dhid
1128 sptr<IDistributedSourceInput> cli = DInputSourceSACliMgr::GetInstance().GetRemoteCli(devId);
1129 if (cli == nullptr) {
1130 DHLOGE("Get Remote DInput Source Proxy return null");
1131 return DH_SUCCESS;
1132 }
1133
1134 cli->SyncNodeInfoRemoteInput(GetLocalNetworkId(), dhId, GetNodeDesc(parameters));
1135
1136 // 6. Notify node mgr to scan vir dev node info
1137 DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(dhId);
1138 return DH_SUCCESS;
1139 }
1140
handleStartServerCallback(const std::string & devId)1141 void DistributedInputSourceManager::handleStartServerCallback(const std::string &devId)
1142 {
1143 bool isFindDevice = false;
1144 for (auto iter = inputDevice_.begin(); iter != inputDevice_.end(); ++iter) {
1145 if (devId == iter->devId) {
1146 isFindDevice = true;
1147 break;
1148 }
1149 }
1150 if (!isFindDevice) {
1151 DeviceMap_[devId] = DINPUT_SOURCE_SWITCH_OFF;
1152 // DeviceMap_ all sink device switch is off,call isstart's callback
1153 bool isAllDevSwitchOff = true;
1154 for (auto it = DeviceMap_.begin(); it != DeviceMap_.end(); ++it) {
1155 if (it->second == DINPUT_SOURCE_SWITCH_ON) {
1156 isAllDevSwitchOff = false;
1157 break;
1158 }
1159 }
1160 if (isAllDevSwitchOff) {
1161 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
1162 nlohmann::json tmpJson;
1163 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = static_cast<int32_t>(DInputServerType::NULL_SERVER_TYPE);
1164 jsonArrayMsg->push_back(tmpJson);
1165 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
1166 DINPUT_SOURCE_MANAGER_STARTSERVER_MSG, jsonArrayMsg, 0);
1167
1168 if (callBackHandler_ == nullptr) {
1169 DHLOGE("handleStartServerCallback callBackHandler_ is null.");
1170 return;
1171 }
1172 callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
1173 }
1174 }
1175 }
1176
UnregCallbackNotify(const std::string & devId,const std::string & dhId)1177 int32_t DistributedInputSourceManager::UnregCallbackNotify(const std::string &devId, const std::string &dhId)
1178 {
1179 for (auto iter = unregCallbacks_.begin(); iter != unregCallbacks_.end(); ++iter) {
1180 if (iter->devId == devId && iter->dhId == dhId) {
1181 iter->callback->OnResult(iter->devId, iter->dhId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL);
1182 unregCallbacks_.erase(iter);
1183 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1184 }
1185 }
1186 return DH_SUCCESS;
1187 }
1188
CheckUnregisterParam(const std::string & devId,const std::string & dhId,sptr<IUnregisterDInputCallback> callback)1189 bool DistributedInputSourceManager::CheckUnregisterParam(const std::string &devId, const std::string &dhId,
1190 sptr<IUnregisterDInputCallback> callback)
1191 {
1192 if (devId.empty() || devId.size() > DEV_ID_LENGTH_MAX) {
1193 DHLOGE("CheckParam devId is empty or devId size too long.");
1194 return false;
1195 }
1196 if (dhId.empty() || dhId.size() > DEV_ID_LENGTH_MAX) {
1197 DHLOGE("CheckParam dhId is empty or dhId size too long.");
1198 return false;
1199 }
1200 if (callback == nullptr) {
1201 DHLOGE("CheckParam callback is null.");
1202 return false;
1203 }
1204 return true;
1205 }
1206
CheckDeviceIsExists(const std::string & devId,const std::string & dhId,const InputDeviceId & inputDeviceId,std::vector<InputDeviceId>::iterator & it)1207 int32_t DistributedInputSourceManager::CheckDeviceIsExists(const std::string &devId, const std::string &dhId,
1208 const InputDeviceId &inputDeviceId, std::vector<InputDeviceId>::iterator &it)
1209 {
1210 for (; it != inputDevice_.end(); ++it) {
1211 if (it->devId == inputDeviceId.devId && it->dhId == inputDeviceId.dhId) {
1212 break;
1213 }
1214 }
1215
1216 if (it == inputDevice_.end()) {
1217 DHLOGE("CheckDevice called, deviceId: %s is not exist.", GetAnonyString(devId).c_str());
1218 if (UnregCallbackNotify(devId, dhId) != DH_SUCCESS) {
1219 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1220 }
1221 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1222 }
1223
1224 return DH_SUCCESS;
1225 }
1226
DeleteInputDeviceNodeInfo(const std::string & devId,const std::string & dhId,const std::vector<InputDeviceId>::iterator & it)1227 int32_t DistributedInputSourceManager::DeleteInputDeviceNodeInfo(const std::string &devId, const std::string &dhId,
1228 const std::vector<InputDeviceId>::iterator &it)
1229 {
1230 int32_t ret = DistributedInputInject::GetInstance().UnregisterDistributedHardware(devId, dhId);
1231 if (ret != DH_SUCCESS) {
1232 DHLOGE("RemoveInputNode called, remove node fail.");
1233 if (UnregCallbackNotify(devId, dhId) != DH_SUCCESS) {
1234 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1235 }
1236 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REMOVE_INPUT_NODE_FAIL;
1237 }
1238
1239 inputDevice_.erase(it);
1240 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
1241 nlohmann::json tmpJson;
1242 tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = devId;
1243 tmpJson[INPUT_SOURCEMANAGER_KEY_HWID] = dhId;
1244 tmpJson[INPUT_SOURCEMANAGER_KEY_RESULT] = true;
1245 jsonArrayMsg->push_back(tmpJson);
1246 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
1247 DINPUT_SOURCE_MANAGER_UNRIGISTER_MSG, jsonArrayMsg, 0);
1248
1249 if (callBackHandler_ == nullptr) {
1250 DHLOGE("UnregisterDistributedHardware callBackHandler_ is null.");
1251 if (UnregCallbackNotify(devId, dhId) != DH_SUCCESS) {
1252 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1253 }
1254 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_DELETE_DEVICE_FAIL;
1255 }
1256 callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
1257 return DH_SUCCESS;
1258 }
1259
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,sptr<IUnregisterDInputCallback> callback)1260 int32_t DistributedInputSourceManager::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
1261 sptr<IUnregisterDInputCallback> callback)
1262 {
1263 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_UNREGISTER, devId, dhId, "dinput unregister call");
1264 DHLOGI("Unregister called, deviceId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
1265 if (!CheckUnregisterParam(devId, dhId, callback)) {
1266 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_UNREGISTER_FAIL, devId, dhId,
1267 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL, "dinput unregister failed in callback is nullptr");
1268 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1269 }
1270
1271 std::lock_guard<std::mutex> lock(operationMutex_);
1272 DInputClientUnregistInfo info {devId, dhId, callback};
1273 unregCallbacks_.push_back(info);
1274
1275 InputDeviceId inputDeviceId {devId, dhId};
1276 DHLOGI("Unregister deviceId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
1277
1278 auto it = inputDevice_.begin();
1279 if (CheckDeviceIsExists(devId, dhId, inputDeviceId, it) != DH_SUCCESS) {
1280 DHLOGE("Unregister deviceId: %s is not exist.", GetAnonyString(devId).c_str());
1281 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_UNREGISTER_FAIL, devId, dhId,
1282 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL, "dinput unregister failed in deviceId is not exist");
1283 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1284 }
1285
1286 if (DeleteInputDeviceNodeInfo(devId, dhId, it) != DH_SUCCESS) {
1287 DHLOGE("Unregister deviceId: %s, delete device node failed", GetAnonyString(devId).c_str());
1288 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_UNREGISTER_FAIL, devId, dhId,
1289 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL, "dinput unregister failed in delete input node");
1290 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL;
1291 }
1292
1293 HiDumper::GetInstance().DeleteNodeInfo(devId, dhId);
1294
1295 // isstart callback
1296 handleStartServerCallback(devId);
1297 return DH_SUCCESS;
1298 }
1299
PrepareRemoteInput(const std::string & deviceId,sptr<IPrepareDInputCallback> callback)1300 int32_t DistributedInputSourceManager::PrepareRemoteInput(
1301 const std::string &deviceId, sptr<IPrepareDInputCallback> callback)
1302 {
1303 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
1304 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_PREPARE, deviceId, "Dinput prepare call.");
1305 if (!DInputCheckParam::GetInstance().CheckParam(deviceId, callback)) {
1306 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1307 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare param is failed.");
1308 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
1309 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1310 }
1311
1312 DHLOGI("Prepare called, deviceId: %s", GetAnonyString(deviceId).c_str());
1313 for (auto iter : preCallbacks_) {
1314 if (iter.devId == deviceId) {
1315 callback->OnResult(deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL);
1316 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1317 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare failed in already prepared.");
1318 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
1319 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1320 }
1321 }
1322
1323 int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(deviceId, false);
1324 if (ret != DH_SUCCESS) {
1325 DHLOGE("Open softbus session fail.");
1326 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1327 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare failed in open softbus");
1328 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
1329 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1330 }
1331
1332 DInputClientPrepareInfo info {deviceId, callback};
1333 preCallbacks_.push_back(info);
1334 ret = DistributedInputSourceTransport::GetInstance().PrepareRemoteInput(deviceId);
1335 if (ret != DH_SUCCESS) {
1336 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1337 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare failed in transport prepare");
1338 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
1339 DHLOGE("Can not send message by softbus, prepare fail.");
1340 for (auto iter = preCallbacks_.begin(); iter != preCallbacks_.end(); ++iter) {
1341 if (iter->devId == deviceId) {
1342 iter->preCallback->OnResult(iter->devId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL);
1343 preCallbacks_.erase(iter);
1344 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1345 }
1346 }
1347 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1348 }
1349
1350 return DH_SUCCESS;
1351 }
1352
UnprepareRemoteInput(const std::string & deviceId,sptr<IUnprepareDInputCallback> callback)1353 int32_t DistributedInputSourceManager::UnprepareRemoteInput(
1354 const std::string &deviceId, sptr<IUnprepareDInputCallback> callback)
1355 {
1356 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
1357 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_UNPREPARE, deviceId, "Dinput unprepare call.");
1358 if (!DInputCheckParam::GetInstance().CheckParam(deviceId, callback)) {
1359 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1360 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare param is failed.");
1361 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
1362 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1363 }
1364 DHLOGI("Unprepare called, deviceId: %s", GetAnonyString(deviceId).c_str());
1365 for (auto iter : unpreCallbacks_) {
1366 if (iter.devId == deviceId) {
1367 callback->OnResult(deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL);
1368 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1369 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare failed in already unprepared.");
1370 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
1371 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1372 }
1373 }
1374
1375 DInputClientUnprepareInfo info {deviceId, callback};
1376 unpreCallbacks_.push_back(info);
1377 int32_t ret = DistributedInputSourceTransport::GetInstance().UnprepareRemoteInput(deviceId);
1378 if (ret != DH_SUCCESS) {
1379 DHLOGE("Can not send message by softbus, unprepare fail.");
1380 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1381 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare failed in transport unprepare.");
1382 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
1383 for (auto iter = unpreCallbacks_.begin(); iter != unpreCallbacks_.end(); ++iter) {
1384 if (iter->devId == deviceId) {
1385 iter->unpreCallback->OnResult(iter->devId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL);
1386 unpreCallbacks_.erase(iter);
1387 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1388 }
1389 }
1390 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1391 }
1392
1393 return DH_SUCCESS;
1394 }
1395
StartRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)1396 int32_t DistributedInputSourceManager::StartRemoteInput(
1397 const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
1398 {
1399 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1400 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, deviceId, "Dinput start use call.");
1401 if (!DInputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) {
1402 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1403 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is failed.");
1404 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1405 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1406 }
1407
1408 DHLOGI("Start called, deviceId: %s, inputTypes: %d", GetAnonyString(deviceId).c_str(), inputTypes);
1409 for (auto iter : staCallbacks_) {
1410 if (iter.devId == deviceId && iter.inputTypes == inputTypes) {
1411 callback->OnResult(deviceId, inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL);
1412 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1413 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in already started.");
1414 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1415 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1416 }
1417 }
1418
1419 DInputClientStartInfo info {deviceId, inputTypes, callback};
1420 staCallbacks_.push_back(info);
1421 DeviceMap_[deviceId] = DINPUT_SOURCE_SWITCH_OFF;
1422 int32_t ret = DistributedInputSourceTransport::GetInstance().StartRemoteInput(deviceId, inputTypes);
1423 if (ret != DH_SUCCESS) {
1424 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1425 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in transport start");
1426 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1427 DHLOGE("Start fail.");
1428 for (auto iter = staCallbacks_.begin(); iter != staCallbacks_.end(); ++iter) {
1429 if (iter->devId == deviceId && iter->inputTypes == inputTypes) {
1430 iter->callback->OnResult(iter->devId, iter->inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL);
1431 staCallbacks_.erase(iter);
1432 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1433 }
1434 }
1435 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1436 }
1437
1438 return DH_SUCCESS;
1439 }
1440
StopRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)1441 int32_t DistributedInputSourceManager::StopRemoteInput(
1442 const std::string &deviceId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
1443 {
1444 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1445 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, deviceId, "Dinput stop use call");
1446 if (!DInputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) {
1447 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1448 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is failed.");
1449 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1450 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1451 }
1452
1453 DHLOGI("Stop called, deviceId: %s, inputTypes: %d", GetAnonyString(deviceId).c_str(), inputTypes);
1454 for (auto iter : stpCallbacks_) {
1455 if (iter.devId == deviceId && iter.inputTypes == inputTypes) {
1456 callback->OnResult(deviceId, inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL);
1457 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1458 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in already stoped.");
1459 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1460 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1461 }
1462 }
1463
1464 DInputClientStopInfo info {deviceId, inputTypes, callback};
1465 stpCallbacks_.push_back(info);
1466 int32_t ret = DistributedInputSourceTransport::GetInstance().StopRemoteInput(deviceId, inputTypes);
1467 if (ret != DH_SUCCESS) {
1468 DHLOGE("Stop fail.");
1469 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId,
1470 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in transport stop.");
1471 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1472 for (auto iter = stpCallbacks_.begin(); iter != stpCallbacks_.end(); ++iter) {
1473 if (iter->devId == deviceId && iter->inputTypes == inputTypes) {
1474 iter->callback->OnResult(iter->devId, iter->inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL);
1475 stpCallbacks_.erase(iter);
1476 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1477 }
1478 }
1479 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1480 }
1481
1482 return DH_SUCCESS;
1483 }
1484
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)1485 int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
1486 const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
1487 {
1488 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1489 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "Dinput start use call.");
1490 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
1491 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1492 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is failed.");
1493 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1494 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1495 }
1496
1497 DHLOGI("StartRemoteInput called, srcId: %s, sinkId: %s, inputTypes: %d", GetAnonyString(srcId).c_str(),
1498 GetAnonyString(sinkId).c_str(), inputTypes);
1499 std::string localNetworkId = GetLocalNetworkId();
1500 if (localNetworkId.empty()) {
1501 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1502 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in get local networkId error.");
1503 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1504 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1505 }
1506 if (srcId != localNetworkId) {
1507 return RelayStartRemoteInputByType(srcId, sinkId, inputTypes, callback);
1508 }
1509 for (auto iter : staCallbacks_) {
1510 if (iter.devId == sinkId && iter.inputTypes == inputTypes) {
1511 DHLOGE("StartRemoteInput called, repeat call.");
1512 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1513 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in already started.");
1514 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1515 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1516 }
1517 }
1518
1519 DInputClientStartInfo info {sinkId, inputTypes, callback};
1520 staCallbacks_.push_back(info);
1521 DeviceMap_[sinkId] = DINPUT_SOURCE_SWITCH_OFF; // when sink device start success,set DINPUT_SOURCE_SWITCH_ON
1522 int32_t ret = DistributedInputSourceTransport::GetInstance().StartRemoteInput(sinkId, inputTypes);
1523 if (ret != DH_SUCCESS) {
1524 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1525 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in transport start.");
1526 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1527 DHLOGE("StartRemoteInput called, start fail.");
1528 for (auto it = staCallbacks_.begin(); it != staCallbacks_.end(); ++it) {
1529 if (it->devId == sinkId && it->inputTypes == inputTypes) {
1530 staCallbacks_.erase(it);
1531 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1532 }
1533 }
1534 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1535 }
1536 return DH_SUCCESS;
1537 }
1538
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)1539 int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
1540 const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
1541 {
1542 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1543 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "Dinput stop use call.");
1544 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
1545 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1546 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is failed.");
1547 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1548 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1549 }
1550 DHLOGI("StopRemoteInput called, srcId: %s, sinkId: %s, inputTypes: %d", GetAnonyString(srcId).c_str(),
1551 GetAnonyString(sinkId).c_str(), inputTypes);
1552 std::string localNetworkId = GetLocalNetworkId();
1553 if (localNetworkId.empty()) {
1554 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1555 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in get networkId.");
1556 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1557 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1558 }
1559 if (srcId != localNetworkId) {
1560 return RelayStopRemoteInputByType(srcId, sinkId, inputTypes, callback);
1561 }
1562 for (auto iter : stpCallbacks_) {
1563 if (iter.devId == sinkId && iter.inputTypes == inputTypes) {
1564 DHLOGE("StopRemoteInput called, repeat call.");
1565 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1566 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in already stoped.");
1567 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1568 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1569 }
1570 }
1571
1572 DInputClientStopInfo info {sinkId, inputTypes, callback};
1573 stpCallbacks_.push_back(info);
1574 int32_t ret = DistributedInputSourceTransport::GetInstance().StopRemoteInput(sinkId, inputTypes);
1575 if (ret != DH_SUCCESS) {
1576 DHLOGE("StopRemoteInput called, stop fail.");
1577 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1578 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in transport stop.");
1579 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1580 for (auto it = stpCallbacks_.begin(); it != stpCallbacks_.end(); ++it) {
1581 if (it->devId == sinkId && it->inputTypes == inputTypes) {
1582 stpCallbacks_.erase(it);
1583 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1584 }
1585 }
1586 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1587 }
1588 return DH_SUCCESS;
1589 }
1590
RelayStartRemoteInputByType(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)1591 int32_t DistributedInputSourceManager::RelayStartRemoteInputByType(const std::string &srcId, const std::string &sinkId,
1592 const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
1593 {
1594 std::lock_guard<std::mutex> lock(mutex_);
1595 for (auto iter : relayStaTypeCallbacks_) {
1596 if (iter.srcId == srcId && iter.sinkId == sinkId && iter.inputTypes == inputTypes) {
1597 DHLOGE("Repeat call.");
1598 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1599 }
1600 }
1601
1602 DInputClientStartTypeInfo info(srcId, sinkId, inputTypes, callback);
1603 relayStaTypeCallbacks_.push_back(info);
1604
1605 int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStartTypeRequest(srcId, sinkId, inputTypes);
1606 if (ret != DH_SUCCESS) {
1607 DHLOGE("Can not send message by softbus, start fail.");
1608 for (auto iter = relayStaTypeCallbacks_.begin(); iter != relayStaTypeCallbacks_.end(); ++iter) {
1609 if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) {
1610 relayStaTypeCallbacks_.erase(iter);
1611 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1612 }
1613 }
1614 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1615 }
1616
1617 return DH_SUCCESS;
1618 }
1619
RelayStopRemoteInputByType(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)1620 int32_t DistributedInputSourceManager::RelayStopRemoteInputByType(
1621 const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
1622 {
1623 std::lock_guard<std::mutex> lock(mutex_);
1624 for (auto iter : relayStpTypeCallbacks_) {
1625 if (iter.srcId == srcId && iter.sinkId == sinkId && iter.inputTypes == inputTypes) {
1626 DHLOGE("Repeat call.");
1627 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1628 }
1629 }
1630
1631 DInputClientStopTypeInfo info(srcId, sinkId, inputTypes, callback);
1632 relayStpTypeCallbacks_.push_back(info);
1633
1634 int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStopTypeRequest(srcId, sinkId, inputTypes);
1635 if (ret != DH_SUCCESS) {
1636 DHLOGE("Can not send message by softbus, start fail.");
1637 for (auto iter = relayStpTypeCallbacks_.begin(); iter != relayStpTypeCallbacks_.end(); ++iter) {
1638 if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) {
1639 relayStpTypeCallbacks_.erase(iter);
1640 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1641 }
1642 }
1643 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1644 }
1645
1646 return DH_SUCCESS;
1647 }
1648
PrepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IPrepareDInputCallback> callback)1649 int32_t DistributedInputSourceManager::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
1650 sptr<IPrepareDInputCallback> callback)
1651 {
1652 DHLOGI("Dinput prepare, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
1653 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
1654 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1655 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare param is failed.");
1656 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1657 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1658 }
1659 std::string localNetworkId = GetLocalNetworkId();
1660 if (localNetworkId.empty()) {
1661 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1662 }
1663 if (srcId != localNetworkId) {
1664 return RelayPrepareRemoteInput(srcId, sinkId, callback);
1665 }
1666 // current device is source device
1667 for (auto iter : preCallbacks_) {
1668 if (iter.devId == sinkId) {
1669 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1670 }
1671 }
1672 int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(sinkId, false);
1673 if (ret != DH_SUCCESS) {
1674 DHLOGE("Open softbus session fail ret=%d.", ret);
1675 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1676 }
1677 DInputClientPrepareInfo info {sinkId, callback};
1678 preCallbacks_.push_back(info);
1679
1680 ret = DistributedInputSourceTransport::GetInstance().PrepareRemoteInput(sinkId);
1681 if (ret != DH_SUCCESS) {
1682 DHLOGE("Can not send message by softbus, prepare fail.");
1683 for (auto iter = preCallbacks_.begin(); iter != preCallbacks_.end(); ++iter) {
1684 if (iter->devId == sinkId) {
1685 preCallbacks_.erase(iter);
1686 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1687 }
1688 }
1689 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
1690 }
1691
1692 return DH_SUCCESS;
1693 }
1694
UnprepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IUnprepareDInputCallback> callback)1695 int32_t DistributedInputSourceManager::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
1696 sptr<IUnprepareDInputCallback> callback)
1697 {
1698 DHLOGI("Dinput unprepare, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
1699 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
1700 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1701 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare param is failed.");
1702 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1703 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1704 }
1705 std::string localNetworkId = GetLocalNetworkId();
1706 if (localNetworkId.empty()) {
1707 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1708 }
1709 if (srcId != localNetworkId) {
1710 return RelayUnprepareRemoteInput(srcId, sinkId, callback);
1711 }
1712
1713 // current device is source device
1714 for (auto iter : unpreCallbacks_) {
1715 if (iter.devId == sinkId) {
1716 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1717 }
1718 }
1719
1720 DInputClientUnprepareInfo info {sinkId, callback};
1721 unpreCallbacks_.push_back(info);
1722 int32_t ret = DistributedInputSourceTransport::GetInstance().UnprepareRemoteInput(sinkId);
1723 if (ret != DH_SUCCESS) {
1724 DHLOGE("Can not send message by softbus, unprepare fail.");
1725 for (auto iter = unpreCallbacks_.begin(); iter != unpreCallbacks_.end(); ++iter) {
1726 if (iter->devId == sinkId) {
1727 unpreCallbacks_.erase(iter);
1728 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1729 }
1730 }
1731 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
1732 }
1733 return DH_SUCCESS;
1734 }
1735
IsStringDataSame(const std::vector<std::string> & oldDhIds,std::vector<std::string> newDhIds)1736 bool DistributedInputSourceManager::IsStringDataSame(const std::vector<std::string> &oldDhIds,
1737 std::vector<std::string> newDhIds)
1738 {
1739 if (oldDhIds.size() != newDhIds.size()) {
1740 DHLOGI("Size is not same, return false.");
1741 return false;
1742 }
1743 bool isSame = true;
1744 for (auto oDhid : oldDhIds) {
1745 auto it = find(newDhIds.begin(), newDhIds.end(), oDhid);
1746 if (it == newDhIds.end()) {
1747 isSame = false;
1748 break;
1749 }
1750 }
1751 DHLOGI("IsSame: %d.", isSame);
1752 return isSame;
1753 }
1754
StartRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)1755 int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &sinkId,
1756 const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
1757 {
1758 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1759 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "dinput start use call");
1760 DHLOGI("Dinput start, sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size());
1761 if (!DInputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) {
1762 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1763 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is failed.");
1764 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1765 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1766 }
1767 std::string localNetworkId = GetLocalNetworkId();
1768 if (localNetworkId.empty()) {
1769 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1770 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "dinput start use failed in get networkId");
1771 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1772 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1773 }
1774
1775 // current device is source device
1776 for (auto iter : staStringCallbacks_) {
1777 if (iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
1778 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1779 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "dinput start use failed in already started");
1780 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1781 DHLOGE("sinkId: %s, repeat call.", GetAnonyString(sinkId).c_str());
1782 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1783 }
1784 }
1785
1786 DInputClientStartDhidInfo info {localNetworkId, sinkId, dhIds, callback};
1787 staStringCallbacks_.push_back(info);
1788 DeviceMap_[sinkId] = DINPUT_SOURCE_SWITCH_OFF; // when sink device start success,set DINPUT_SOURCE_SWITCH_ON
1789 int32_t ret = DistributedInputSourceTransport::GetInstance().StartRemoteInput(sinkId, dhIds);
1790 if (ret != DH_SUCCESS) {
1791 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1792 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "dinput start use failed in transport start");
1793 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1794 DHLOGE("StartRemoteInput start fail.");
1795 for (auto iter = staStringCallbacks_.begin(); iter != staStringCallbacks_.end(); ++iter) {
1796 if (iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
1797 staStringCallbacks_.erase(iter);
1798 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1799 }
1800 }
1801 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1802 }
1803 return DH_SUCCESS;
1804 }
1805
StopRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)1806 int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
1807 sptr<IStartStopDInputsCallback> callback)
1808 {
1809 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1810 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "dinput stop use call");
1811 DHLOGI("Dinput stop, sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size());
1812 if (!DInputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) {
1813 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1814 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is failed.");
1815 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1816 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1817 }
1818 std::string localNetworkId = GetLocalNetworkId();
1819 if (localNetworkId.empty()) {
1820 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1821 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "dinput stop use failed in get networkId");
1822 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1823 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1824 }
1825 for (auto iter : stpStringCallbacks_) {
1826 if (iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
1827 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1828 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "dinput stop use failed in already stop");
1829 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1830 DHLOGE("sinkId: %s, repeat call.", GetAnonyString(sinkId).c_str());
1831 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1832 }
1833 }
1834
1835 DInputClientStopDhidInfo info {localNetworkId, sinkId, dhIds, callback};
1836 stpStringCallbacks_.push_back(info);
1837 int32_t ret = DistributedInputSourceTransport::GetInstance().StopRemoteInput(sinkId, dhIds);
1838 if (ret != DH_SUCCESS) {
1839 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1840 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "dinput stop use failed in transport stop");
1841 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1842 DHLOGE("StopRemoteInput stop fail.");
1843 for (auto iter = stpStringCallbacks_.begin(); iter != stpStringCallbacks_.end(); ++iter) {
1844 if (iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
1845 stpStringCallbacks_.erase(iter);
1846 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1847 }
1848 }
1849 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1850 }
1851 return DH_SUCCESS;
1852 }
1853
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)1854 int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
1855 const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
1856 {
1857 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1858 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "Dinput start use call.");
1859 DHLOGI("Dinput start, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
1860 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) {
1861 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1862 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is failed.");
1863 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1864 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1865 }
1866 std::string localNetworkId = GetLocalNetworkId();
1867 if (localNetworkId.empty()) {
1868 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1869 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in get networkId.");
1870 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1871 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1872 }
1873 if (srcId != localNetworkId) {
1874 return RelayStartRemoteInputByDhid(srcId, sinkId, dhIds, callback);
1875 }
1876 for (auto iter : staStringCallbacks_) {
1877 if (iter.srcId == srcId && iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
1878 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1879 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in already start.");
1880 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1881 DHLOGE("sinkId: %s, repeat call.", GetAnonyString(sinkId).c_str());
1882 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1883 }
1884 }
1885
1886 DInputClientStartDhidInfo info {srcId, sinkId, dhIds, callback};
1887 staStringCallbacks_.push_back(info);
1888 DeviceMap_[sinkId] = DINPUT_SOURCE_SWITCH_OFF;
1889 int32_t ret = DistributedInputSourceTransport::GetInstance().StartRemoteInput(sinkId, dhIds);
1890 if (ret != DH_SUCCESS) {
1891 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1892 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in transport start.");
1893 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
1894 DHLOGE("StartRemoteInput start fail.");
1895 for (auto iter = staStringCallbacks_.begin(); iter != staStringCallbacks_.end(); ++iter) {
1896 if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
1897 staStringCallbacks_.erase(iter);
1898 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1899 }
1900 }
1901 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
1902 }
1903 return DH_SUCCESS;
1904 }
1905
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)1906 int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
1907 const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
1908 {
1909 StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1910 HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "Dinput stop use call.");
1911 DHLOGI("Dinput stop, srcId: %s, sinkId: %s, vector.string.size: %d",
1912 GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), dhIds.size());
1913 if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) {
1914 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1915 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is failed.");
1916 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1917 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1918 }
1919 std::string localNetworkId = GetLocalNetworkId();
1920 if (localNetworkId.empty()) {
1921 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1922 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in get networkId.");
1923 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1924 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1925 }
1926 if (srcId != localNetworkId) {
1927 return RelayStopRemoteInputByDhid(srcId, sinkId, dhIds, callback);
1928 }
1929 for (auto iter : stpStringCallbacks_) {
1930 if (iter.srcId == srcId && iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
1931 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1932 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in already stop.");
1933 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1934 DHLOGE("sinkId: %s, repeat call.", GetAnonyString(sinkId).c_str());
1935 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1936 }
1937 }
1938
1939 DInputClientStopDhidInfo info {srcId, sinkId, dhIds, callback};
1940 stpStringCallbacks_.push_back(info);
1941 int32_t ret = DistributedInputSourceTransport::GetInstance().StopRemoteInput(sinkId, dhIds);
1942 if (ret != DH_SUCCESS) {
1943 HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId,
1944 ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in transport stop.");
1945 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
1946 DHLOGE("StopRemoteInput stop fail.");
1947 for (auto iter = stpStringCallbacks_.begin(); iter != stpStringCallbacks_.end(); ++iter) {
1948 if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
1949 stpStringCallbacks_.erase(iter);
1950 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1951 }
1952 }
1953 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
1954 }
1955 return DH_SUCCESS;
1956 }
1957
RegisterAddWhiteListCallback(sptr<IAddWhiteListInfosCallback> callback)1958 int32_t DistributedInputSourceManager::RegisterAddWhiteListCallback(sptr<IAddWhiteListInfosCallback> callback)
1959 {
1960 DHLOGI("RegisterAddWhiteListCallback called.");
1961 if (callback == nullptr) {
1962 DHLOGE("RegisterAddWhiteListCallback callback is null.");
1963 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REG_CALLBACK_ERR;
1964 }
1965 std::lock_guard<std::mutex> lock(valMutex_);
1966 addWhiteListCallbacks_.insert(callback);
1967 return DH_SUCCESS;
1968 }
1969
RegisterDelWhiteListCallback(sptr<IDelWhiteListInfosCallback> callback)1970 int32_t DistributedInputSourceManager::RegisterDelWhiteListCallback(sptr<IDelWhiteListInfosCallback> callback)
1971 {
1972 DHLOGI("RegisterDelWhiteListCallback called.");
1973 if (callback == nullptr) {
1974 DHLOGE("RegisterDelWhiteListCallback callback is null.");
1975 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REG_CALLBACK_ERR;
1976 }
1977 std::lock_guard<std::mutex> lock(valMutex_);
1978 delWhiteListCallbacks_.insert(callback);
1979 return DH_SUCCESS;
1980 }
1981
RegisterInputNodeListener(sptr<InputNodeListener> listener)1982 int32_t DistributedInputSourceManager::RegisterInputNodeListener(sptr<InputNodeListener> listener)
1983 {
1984 DHLOGI("RegisterInputNodeListener.");
1985 if (listener == nullptr) {
1986 DHLOGE("RegisterInputNodeListener callback is null.");
1987 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_NODE_LISTENER_CALLBACK_ERR;
1988 }
1989 DistributedInputInject::GetInstance().RegisterInputNodeListener(listener);
1990 SendExistVirNodeInfos(listener);
1991 return DH_SUCCESS;
1992 }
1993
UnregisterInputNodeListener(sptr<InputNodeListener> listener)1994 int32_t DistributedInputSourceManager::UnregisterInputNodeListener(sptr<InputNodeListener> listener)
1995 {
1996 DHLOGI("UnregisterInputNodeListener.");
1997 if (listener == nullptr) {
1998 DHLOGE("UnregisterInputNodeListener callback is null.");
1999 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_NODE_LISTENER_CALLBACK_ERR;
2000 }
2001 DistributedInputInject::GetInstance().UnregisterInputNodeListener(listener);
2002 return DH_SUCCESS;
2003 }
2004
SendExistVirNodeInfos(sptr<InputNodeListener> listener)2005 void DistributedInputSourceManager::SendExistVirNodeInfos(sptr<InputNodeListener> listener)
2006 {
2007 DHLOGI("SendExistVirNodeInfos call");
2008 std::lock_guard<std::mutex> lock(operationMutex_);
2009 DevInfo localDevInfo = GetLocalDeviceInfo();
2010 for (const auto &node : inputDevice_) {
2011 DHLOGI("Send Exist Vir Node: srcId: %s, sinkId: %s, dhId: %s", GetAnonyString(localDevInfo.networkId).c_str(),
2012 GetAnonyString(node.devId).c_str(), GetAnonyString(node.dhId).c_str());
2013 listener->OnNodeOnLine(localDevInfo.networkId, node.devId, node.dhId, node.nodeDesc);
2014 }
2015 }
2016
RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)2017 int32_t DistributedInputSourceManager::RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)
2018 {
2019 DHLOGI("RegisterSimulationEventListener called.");
2020 if (listener == nullptr) {
2021 DHLOGE("RegisterSimulationEventListener callback is null.");
2022 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_SIMULATION_EVENT_CALLBACK_ERR;
2023 }
2024 std::lock_guard<std::mutex> lock(valMutex_);
2025 this->simulationEventCallbacks_.insert(listener);
2026 return DH_SUCCESS;
2027 }
2028
UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)2029 int32_t DistributedInputSourceManager::UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)
2030 {
2031 DHLOGI("UnregisterSimulationEventListener called.");
2032 if (listener == nullptr) {
2033 DHLOGE("UnregisterSimulationEventListener callback is null.");
2034 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_SIMULATION_EVENT_CALLBACK_ERR;
2035 }
2036 std::lock_guard<std::mutex> lock(valMutex_);
2037 this->simulationEventCallbacks_.erase(listener);
2038 return DH_SUCCESS;
2039 }
2040
SyncNodeInfoRemoteInput(const std::string & userDevId,const std::string & dhId,const std::string & nodeDesc)2041 int32_t DistributedInputSourceManager::SyncNodeInfoRemoteInput(const std::string &userDevId, const std::string &dhId,
2042 const std::string &nodeDesc)
2043 {
2044 // store info
2045 UpdateSyncNodeInfo(userDevId, dhId, nodeDesc);
2046 // notify multimodal
2047 DistributedInputInject::GetInstance().SyncNodeOnlineInfo(userDevId, GetLocalNetworkId(), dhId, nodeDesc);
2048 return DH_SUCCESS;
2049 }
2050
RelayPrepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IPrepareDInputCallback> callback)2051 int32_t DistributedInputSourceManager::RelayPrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
2052 sptr<IPrepareDInputCallback> callback)
2053 {
2054 std::lock_guard<std::mutex> lock(mutex_);
2055 for (auto iter : relayPreCallbacks_) {
2056 if (iter.srcId == srcId && iter.sinkId == sinkId) {
2057 DHLOGE("Repeat call.");
2058 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
2059 }
2060 }
2061
2062 int32_t ret = DistributedInputSourceTransport::GetInstance().OpenInputSoftbus(srcId, true);
2063 if (ret != DH_SUCCESS) {
2064 DHLOGE("Open softbus session fail.");
2065 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
2066 }
2067 DInputClientRelayPrepareInfo info(srcId, sinkId, callback);
2068 relayPreCallbacks_.push_back(info);
2069
2070 ret = DistributedInputSourceTransport::GetInstance().SendRelayPrepareRequest(srcId, sinkId);
2071 if (ret != DH_SUCCESS) {
2072 DHLOGE("Can not send message by softbus, prepare fail.");
2073 for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); ++iter) {
2074 if (iter->srcId == srcId && iter->sinkId == sinkId) {
2075 relayPreCallbacks_.erase(iter);
2076 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
2077 }
2078 }
2079 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL;
2080 }
2081
2082 return DH_SUCCESS;
2083 }
2084
RelayUnprepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IUnprepareDInputCallback> callback)2085 int32_t DistributedInputSourceManager::RelayUnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
2086 sptr<IUnprepareDInputCallback> callback)
2087 {
2088 std::lock_guard<std::mutex> lock(mutex_);
2089 for (auto iter : relayUnpreCallbacks_) {
2090 if (iter.srcId == srcId && iter.sinkId == sinkId) {
2091 DHLOGE("Repeat call.");
2092 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
2093 }
2094 }
2095
2096 DInputClientRelayUnprepareInfo info(srcId, sinkId, callback);
2097 relayUnpreCallbacks_.push_back(info);
2098
2099 int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayUnprepareRequest(srcId, sinkId);
2100 if (ret != DH_SUCCESS) {
2101 DHLOGE("Can not send message by softbus, prepare fail.");
2102 for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); ++iter) {
2103 if (iter->srcId == srcId && iter->sinkId == sinkId) {
2104 relayUnpreCallbacks_.erase(iter);
2105 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
2106 }
2107 }
2108 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL;
2109 }
2110
2111 return DH_SUCCESS;
2112 }
2113
RelayStartRemoteInputByDhid(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)2114 int32_t DistributedInputSourceManager::RelayStartRemoteInputByDhid(const std::string &srcId, const std::string &sinkId,
2115 const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
2116 {
2117 std::lock_guard<std::mutex> lock(mutex_);
2118 for (auto iter : relayStaDhidCallbacks_) {
2119 if (iter.srcId == srcId && iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
2120 DHLOGE("Repeat call.");
2121 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
2122 }
2123 }
2124
2125 DInputClientStartDhidInfo info{srcId, sinkId, dhIds, callback};
2126 relayStaDhidCallbacks_.push_back(info);
2127
2128 int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStartDhidRequest(srcId, sinkId, dhIds);
2129 if (ret != DH_SUCCESS) {
2130 DHLOGE("Can not send message by softbus, start fail.");
2131 for (auto iter = relayStaDhidCallbacks_.begin(); iter != relayStaDhidCallbacks_.end(); ++iter) {
2132 if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
2133 relayStaDhidCallbacks_.erase(iter);
2134 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
2135 }
2136 }
2137 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL;
2138 }
2139
2140 return DH_SUCCESS;
2141 }
2142
RelayStopRemoteInputByDhid(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)2143 int32_t DistributedInputSourceManager::RelayStopRemoteInputByDhid(const std::string &srcId, const std::string &sinkId,
2144 const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
2145 {
2146 std::lock_guard<std::mutex> lock(mutex_);
2147 for (auto iter : relayStpDhidCallbacks_) {
2148 if (iter.srcId == srcId && iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) {
2149 DHLOGE("Repeat call.");
2150 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
2151 }
2152 }
2153
2154 DInputClientStopDhidInfo info{srcId, sinkId, dhIds, callback};
2155 relayStpDhidCallbacks_.push_back(info);
2156
2157 int32_t ret = DistributedInputSourceTransport::GetInstance().SendRelayStopDhidRequest(srcId, sinkId, dhIds);
2158 if (ret != DH_SUCCESS) {
2159 DHLOGE("Can not send message by softbus, stop fail.");
2160 for (auto iter = relayStpDhidCallbacks_.begin(); iter != relayStpDhidCallbacks_.end(); ++iter) {
2161 if (iter->srcId == srcId && iter->sinkId == sinkId && IsStringDataSame(iter->dhIds, dhIds)) {
2162 relayStpDhidCallbacks_.erase(iter);
2163 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
2164 }
2165 }
2166 return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL;
2167 }
2168
2169 return DH_SUCCESS;
2170 }
2171
RunRegisterCallback(const std::string & devId,const std::string & dhId,const int32_t & status)2172 void DistributedInputSourceManager::RunRegisterCallback(
2173 const std::string &devId, const std::string &dhId, const int32_t &status)
2174 {
2175 std::lock_guard<std::mutex> lock(operationMutex_);
2176 for (auto iter = regCallbacks_.begin(); iter != regCallbacks_.end(); ++iter) {
2177 if (iter->devId == devId && iter->dhId == dhId) {
2178 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RIGISTER_MSG");
2179 iter->callback->OnResult(devId, dhId, status);
2180 regCallbacks_.erase(iter);
2181 return;
2182 }
2183 }
2184
2185 DHLOGE("ProcessEvent registerCallback is null.");
2186 }
2187
RunUnregisterCallback(const std::string & devId,const std::string & dhId,const int32_t & status)2188 void DistributedInputSourceManager::RunUnregisterCallback(
2189 const std::string &devId, const std::string &dhId, const int32_t &status)
2190 {
2191 std::lock_guard<std::mutex> lock(operationMutex_);
2192 for (auto iter = unregCallbacks_.begin(); iter != unregCallbacks_.end(); ++iter) {
2193 if (iter->devId == devId && iter->dhId == dhId) {
2194 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_UNRIGISTER_MSG");
2195 iter->callback->OnResult(devId, dhId, status);
2196 unregCallbacks_.erase(iter);
2197 return;
2198 }
2199 }
2200
2201 DHLOGE("ProcessEvent unregisterCallback is null.");
2202 }
2203
RunPrepareCallback(const std::string & devId,const int32_t & status,const std::string & object)2204 void DistributedInputSourceManager::RunPrepareCallback(
2205 const std::string &devId, const int32_t &status, const std::string &object)
2206 {
2207 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
2208 for (auto iter = preCallbacks_.begin(); iter != preCallbacks_.end(); ++iter) {
2209 if (iter->devId == devId) {
2210 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_PREPARE_MSG");
2211 iter->preCallback->OnResult(devId, status);
2212 preCallbacks_.erase(iter);
2213 RunWhiteListCallback(devId, object);
2214 return;
2215 }
2216 }
2217
2218 DHLOGE("ProcessEvent parepareCallback is null.");
2219 }
2220
RunWhiteListCallback(const std::string & devId,const std::string & object)2221 void DistributedInputSourceManager::RunWhiteListCallback(const std::string &devId, const std::string &object)
2222 {
2223 std::lock_guard<std::mutex> lock(valMutex_);
2224 if (addWhiteListCallbacks_.size() == 0) {
2225 DHLOGE("addWhiteListCallbacks_ is empty.");
2226 return;
2227 }
2228 for (const auto& it : addWhiteListCallbacks_) {
2229 it->OnResult(devId, object);
2230 }
2231 }
2232
RunRelayPrepareCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status)2233 void DistributedInputSourceManager::RunRelayPrepareCallback(const std::string &srcId, const std::string &sinkId,
2234 const int32_t &status)
2235 {
2236 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK);
2237 for (auto iter = relayPreCallbacks_.begin(); iter != relayPreCallbacks_.end(); ++iter) {
2238 if (iter->srcId == srcId && iter->sinkId == sinkId) {
2239 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_MMI");
2240 iter->preCallback->OnResult(sinkId, status);
2241 relayPreCallbacks_.erase(iter);
2242 return;
2243 }
2244 }
2245 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_PREPARE_RESULT_MMI relayPreCallbacks_ is null.");
2246 }
2247
RunRelayUnprepareCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status)2248 void DistributedInputSourceManager::RunRelayUnprepareCallback(const std::string &srcId, const std::string &sinkId,
2249 const int32_t &status)
2250 {
2251 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
2252 for (auto iter = relayUnpreCallbacks_.begin(); iter != relayUnpreCallbacks_.end(); ++iter) {
2253 if (iter->srcId == srcId && iter->sinkId == sinkId) {
2254 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_MMI");
2255 iter->unpreCallback->OnResult(sinkId, status);
2256 relayUnpreCallbacks_.erase(iter);
2257 return;
2258 }
2259 }
2260 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_UNPREPARE_RESULT_MMI relayUnpreCallbacks_ is null.");
2261 }
2262
RunUnprepareCallback(const std::string & devId,const int32_t & status)2263 void DistributedInputSourceManager::RunUnprepareCallback(const std::string &devId, const int32_t &status)
2264 {
2265 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK);
2266 for (auto iter = unpreCallbacks_.begin(); iter != unpreCallbacks_.end(); ++iter) {
2267 if (iter->devId == devId) {
2268 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_UNPREPARE_MSG");
2269 iter->unpreCallback->OnResult(devId, status);
2270 unpreCallbacks_.erase(iter);
2271 std::lock_guard<std::mutex> lock(valMutex_);
2272 if (delWhiteListCallbacks_.size() == 0) {
2273 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_UNPREPARE_MSG delWhiteListCallback is null.");
2274 return;
2275 }
2276 for (const auto& it : delWhiteListCallbacks_) {
2277 it->OnResult(devId);
2278 }
2279 return;
2280 }
2281 }
2282
2283 DHLOGE("ProcessEvent unparepareCallback is null.");
2284 }
2285
RunStartCallback(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)2286 void DistributedInputSourceManager::RunStartCallback(
2287 const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
2288 {
2289 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
2290 for (auto iter = staCallbacks_.begin(); iter != staCallbacks_.end(); ++iter) {
2291 if (iter->devId == devId && iter->inputTypes == inputTypes) {
2292 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_MSG");
2293 iter->callback->OnResult(devId, inputTypes, status);
2294 staCallbacks_.erase(iter);
2295 break;
2296 }
2297 }
2298 }
2299
RunStopCallback(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)2300 void DistributedInputSourceManager::RunStopCallback(
2301 const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
2302 {
2303 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
2304 for (auto iter = stpCallbacks_.begin(); iter != stpCallbacks_.end(); ++iter) {
2305 if (iter->devId == devId && iter->inputTypes == inputTypes) {
2306 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_STOP_MSG");
2307 iter->callback->OnResult(devId, inputTypes, status);
2308 stpCallbacks_.erase(iter);
2309 break;
2310 }
2311 }
2312 }
2313
RunStartDhidCallback(const std::string & sinkId,const std::string & dhIds,const int32_t & status)2314 void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sinkId, const std::string &dhIds,
2315 const int32_t &status)
2316 {
2317 std::vector<std::string> dhidsVec;
2318 StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec);
2319 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_DHID_MSG dhIds:%s, vec-size:%d", GetAnonyString(dhIds).c_str(),
2320 dhidsVec.size());
2321 std::string localNetWorkId = GetLocalNetworkId();
2322 if (localNetWorkId.empty()) {
2323 return;
2324 }
2325
2326 for (auto iter = staStringCallbacks_.begin(); iter != staStringCallbacks_.end(); ++iter) {
2327 if (iter->sinkId != sinkId || !IsStringDataSame(iter->dhIds, dhidsVec)) {
2328 continue;
2329 }
2330 iter->callback->OnResultDhids(sinkId, status);
2331 staStringCallbacks_.erase(iter);
2332 break;
2333 }
2334 }
2335
RunStopDhidCallback(const std::string & sinkId,const std::string & dhIds,const int32_t & status)2336 void DistributedInputSourceManager::RunStopDhidCallback(const std::string &sinkId, const std::string &dhIds,
2337 const int32_t &status)
2338 {
2339 std::vector<std::string> dhidsVec;
2340 StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec);
2341 std::string localNetworkId = GetLocalNetworkId();
2342 if (localNetworkId.empty()) {
2343 return;
2344 }
2345
2346 for (auto iter = stpStringCallbacks_.begin();
2347 iter != stpStringCallbacks_.end(); ++iter) {
2348 if (iter->sinkId != sinkId || !IsStringDataSame(iter->dhIds, dhidsVec)) {
2349 continue;
2350 }
2351 iter->callback->OnResultDhids(sinkId, status);
2352 stpStringCallbacks_.erase(iter);
2353 break;
2354 }
2355 }
2356
RunRelayStartDhidCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status,const std::string & dhids)2357 void DistributedInputSourceManager::RunRelayStartDhidCallback(const std::string &srcId, const std::string &sinkId,
2358 const int32_t &status, const std::string &dhids)
2359 {
2360 std::vector<std::string> dhidsVec;
2361 StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec);
2362 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI dhIds:%s, vec-size:%d",
2363 dhids.c_str(), dhidsVec.size());
2364 bool isCbRun = false;
2365 for (std::vector<DInputClientStartDhidInfo>::iterator iter = relayStaDhidCallbacks_.begin();
2366 iter != relayStaDhidCallbacks_.end(); ++iter) {
2367 if (iter->srcId != srcId || iter->sinkId != sinkId || !IsStringDataSame(iter->dhIds, dhidsVec)) {
2368 continue;
2369 }
2370 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI call OnResultDhids");
2371 iter->callback->OnResultDhids(sinkId, status);
2372 relayStaDhidCallbacks_.erase(iter);
2373 isCbRun = true;
2374 break;
2375 }
2376 if (!isCbRun) {
2377 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI relayStaDhidCallbacks_ is null.");
2378 }
2379 }
2380
RunRelayStopDhidCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status,const std::string & dhids)2381 void DistributedInputSourceManager::RunRelayStopDhidCallback(const std::string &srcId, const std::string &sinkId,
2382 const int32_t &status, const std::string &dhids)
2383 {
2384 std::vector<std::string> dhidsVec;
2385 StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec);
2386 bool isCbRun = false;
2387 for (std::vector<DInputClientStopDhidInfo>::iterator iter = relayStpDhidCallbacks_.begin();
2388 iter != relayStpDhidCallbacks_.end(); ++iter) {
2389 if (iter->srcId != srcId || iter->sinkId != sinkId || !IsStringDataSame(iter->dhIds, dhidsVec)) {
2390 continue;
2391 }
2392 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STOPDHID_RESULT_MMI call OnResultDhids");
2393 iter->callback->OnResultDhids(sinkId, status);
2394 relayStpDhidCallbacks_.erase(iter);
2395 isCbRun = true;
2396 break;
2397 }
2398 if (!isCbRun) {
2399 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STOPDHID_RESULT_MMI relayStpDhidCallbacks_ is null.");
2400 }
2401 }
2402
RunRelayStartTypeCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status,uint32_t inputTypes)2403 void DistributedInputSourceManager::RunRelayStartTypeCallback(const std::string &srcId, const std::string &sinkId,
2404 const int32_t &status, uint32_t inputTypes)
2405 {
2406 bool isCbRun = false;
2407 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
2408 for (std::vector<DInputClientStartTypeInfo>::iterator iter =
2409 relayStaTypeCallbacks_.begin(); iter != relayStaTypeCallbacks_.end(); ++iter) {
2410 if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) {
2411 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTTYPE_RESULT_MMI");
2412 iter->callback->OnResult(sinkId, inputTypes, status);
2413 relayStaTypeCallbacks_.erase(iter);
2414 isCbRun = true;
2415 break;
2416 }
2417 }
2418
2419 if (!isCbRun) {
2420 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTTYPE_RESULT_MMI relayStaTypeCallbacks_ is null.");
2421 }
2422 }
2423
RunRelayStopTypeCallback(const std::string & srcId,const std::string & sinkId,const int32_t & status,uint32_t inputTypes)2424 void DistributedInputSourceManager::RunRelayStopTypeCallback(const std::string &srcId, const std::string &sinkId,
2425 const int32_t &status, uint32_t inputTypes)
2426 {
2427 bool isCbRun = false;
2428 FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
2429 for (std::vector<DInputClientStopTypeInfo>::iterator iter =
2430 relayStpTypeCallbacks_.begin(); iter != relayStpTypeCallbacks_.end(); ++iter) {
2431 if (iter->srcId == srcId && iter->sinkId == sinkId && iter->inputTypes == inputTypes) {
2432 DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STOPTYPE_RESULT_MMI");
2433 iter->callback->OnResult(sinkId, inputTypes, status);
2434 relayStpTypeCallbacks_.erase(iter);
2435 isCbRun = true;
2436 break;
2437 }
2438 }
2439
2440 if (!isCbRun) {
2441 DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STOPTYPE_RESULT_MMI relayStpTypeCallbacks_ is null.");
2442 }
2443 }
RunKeyStateCallback(const std::string & sinkId,const std::string & dhId,const uint32_t type,const uint32_t code,const uint32_t value)2444 void DistributedInputSourceManager::RunKeyStateCallback(const std::string &sinkId, const std::string &dhId,
2445 const uint32_t type, const uint32_t code, const uint32_t value)
2446 {
2447 // 1.notify multiinput
2448 std::lock_guard<std::mutex> lock(valMutex_);
2449 for (const auto &cb : simulationEventCallbacks_) {
2450 cb->OnSimulationEvent(type, code, value);
2451 }
2452
2453 DHLOGI("ProcessEvent notify multimodal OnSimulationEvent success.");
2454 // 2.if return success, write to virtulnode
2455 RawEvent mEventBuffer;
2456 mEventBuffer.type = type;
2457 mEventBuffer.code = code;
2458 mEventBuffer.value = value;
2459 mEventBuffer.descriptor = dhId;
2460 DistributedInputInject::GetInstance().RegisterDistributedEvent(&mEventBuffer, DINPUT_SOURCE_WRITE_EVENT_SIZE);
2461 return;
2462 }
2463
GetStartTransFlag()2464 DInputServerType DistributedInputSourceManager::GetStartTransFlag()
2465 {
2466 return isStartTrans_;
2467 }
2468
SetStartTransFlag(const DInputServerType flag)2469 void DistributedInputSourceManager::SetStartTransFlag(const DInputServerType flag)
2470 {
2471 DHLOGI("Set Source isStartTrans_ %d", static_cast<int32_t>(flag));
2472 isStartTrans_ = flag;
2473 }
2474
GetInputDeviceId()2475 std::vector<DistributedInputSourceManager::InputDeviceId> DistributedInputSourceManager::GetInputDeviceId()
2476 {
2477 return inputDevice_;
2478 }
2479
RemoveInputDeviceId(const std::string deviceId,const std::string dhId)2480 void DistributedInputSourceManager::RemoveInputDeviceId(const std::string deviceId, const std::string dhId)
2481 {
2482 InputDeviceId inputDeviceId {deviceId, dhId};
2483
2484 auto it = std::find(inputDevice_.begin(), inputDevice_.end(), inputDeviceId);
2485 if (it == inputDevice_.end()) {
2486 return;
2487 }
2488
2489 // delete device
2490 DHLOGI("inputDevice erase deviceId: %s, dhId: %s", GetAnonyString(it->devId).c_str(),
2491 GetAnonyString(it->dhId).c_str());
2492 inputDevice_.erase(it);
2493 }
2494
GetDeviceMapAllDevSwitchOff()2495 bool DistributedInputSourceManager::GetDeviceMapAllDevSwitchOff()
2496 {
2497 bool isAllDevSwitchOff = true;
2498 for (auto it = DeviceMap_.begin(); it != DeviceMap_.end(); ++it) {
2499 if (it->second == DINPUT_SOURCE_SWITCH_ON) {
2500 isAllDevSwitchOff = false;
2501 break;
2502 }
2503 }
2504 return isAllDevSwitchOff;
2505 }
2506
SetDeviceMapValue(const std::string deviceId,int32_t value)2507 void DistributedInputSourceManager::SetDeviceMapValue(const std::string deviceId, int32_t value)
2508 {
2509 DeviceMap_[deviceId] = value;
2510 }
2511
GetInputTypesMap(const std::string deviceId)2512 uint32_t DistributedInputSourceManager::GetInputTypesMap(const std::string deviceId)
2513 {
2514 auto key = InputTypesMap_.find(deviceId);
2515 if (key != InputTypesMap_.end()) {
2516 return InputTypesMap_[deviceId];
2517 }
2518 return static_cast<uint32_t>(DInputDeviceType::NONE);
2519 }
2520
GetAllInputTypesMap()2521 uint32_t DistributedInputSourceManager::GetAllInputTypesMap()
2522 {
2523 uint32_t rInputTypes = static_cast<uint32_t>(DInputDeviceType::NONE);
2524 for (auto iter = InputTypesMap_.begin(); iter != InputTypesMap_.end(); ++iter) {
2525 rInputTypes |= iter->second;
2526 }
2527 return rInputTypes;
2528 }
2529
SetInputTypesMap(const std::string deviceId,uint32_t value)2530 void DistributedInputSourceManager::SetInputTypesMap(const std::string deviceId, uint32_t value)
2531 {
2532 if (value == static_cast<uint32_t>(DInputDeviceType::NONE)) {
2533 auto key = InputTypesMap_.find(deviceId);
2534 if (key != InputTypesMap_.end()) {
2535 InputTypesMap_.erase(key);
2536 return;
2537 }
2538 }
2539 InputTypesMap_[deviceId] = value;
2540 }
2541
GetSyncNodeInfo(const std::string & devId)2542 std::set<BeRegNodeInfo> DistributedInputSourceManager::GetSyncNodeInfo(const std::string &devId)
2543 {
2544 std::lock_guard<std::mutex> lock(syncNodeInfoMutex_);
2545 if (syncNodeInfoMap_.find(devId) == syncNodeInfoMap_.end()) {
2546 DHLOGI("syncNodeInfoMap find not the key: %s", GetAnonyString(devId).c_str());
2547 return {};
2548 }
2549 return syncNodeInfoMap_[devId];
2550 }
2551
UpdateSyncNodeInfo(const std::string & userDevId,const std::string & dhId,const std::string & nodeDesc)2552 void DistributedInputSourceManager::UpdateSyncNodeInfo(const std::string &userDevId, const std::string &dhId,
2553 const std::string &nodeDesc)
2554 {
2555 std::lock_guard<std::mutex> lock(syncNodeInfoMutex_);
2556 if (syncNodeInfoMap_.find(userDevId) == syncNodeInfoMap_.end()) {
2557 DHLOGI("syncNodeInfoMap has not the key: %s, So create this entry", GetAnonyString(userDevId).c_str());
2558 std::set<BeRegNodeInfo> syncNodeInfo;
2559 syncNodeInfoMap_[userDevId] = syncNodeInfo;
2560 }
2561 syncNodeInfoMap_[userDevId].insert({userDevId, dhId, nodeDesc});
2562 }
2563
DeleteSyncNodeInfo(const std::string & devId)2564 void DistributedInputSourceManager::DeleteSyncNodeInfo(const std::string &devId)
2565 {
2566 std::lock_guard<std::mutex> lock(syncNodeInfoMutex_);
2567 syncNodeInfoMap_.erase(devId);
2568 }
2569
RecordEventLog(int64_t when,int32_t type,int32_t code,int32_t value,const std::string & path)2570 void DistributedInputSourceManager::DInputSourceListener::RecordEventLog(int64_t when, int32_t type, int32_t code,
2571 int32_t value, const std::string &path)
2572 {
2573 std::string eventType = "";
2574 switch (type) {
2575 case EV_KEY:
2576 eventType = "EV_KEY";
2577 break;
2578 case EV_REL:
2579 eventType = "EV_REL";
2580 break;
2581 case EV_ABS:
2582 eventType = "EV_ABS";
2583 break;
2584 default:
2585 eventType = "other type";
2586 break;
2587 }
2588 DHLOGD("3.E2E-Test Source softBus receive event, EventType: %s, Code: %d, Value: %d, Path: %s, When: %" PRId64 "",
2589 eventType.c_str(), code, value, path.c_str(), when);
2590 }
2591
StartDScreenListener()2592 DistributedInputSourceManager::StartDScreenListener::StartDScreenListener()
2593 {
2594 DHLOGI("StartDScreenListener ctor!");
2595 }
2596
~StartDScreenListener()2597 DistributedInputSourceManager::StartDScreenListener::~StartDScreenListener()
2598 {
2599 DHLOGI("StartDScreenListener dtor!");
2600 }
2601
OnMessage(const DHTopic topic,const std::string & message)2602 void DistributedInputSourceManager::StartDScreenListener::OnMessage(const DHTopic topic, const std::string &message)
2603 {
2604 DHLOGI("StartDScreenListener OnMessage!");
2605 if (topic != DHTopic::TOPIC_START_DSCREEN) {
2606 DHLOGE("this topic is wrong, %d", static_cast<uint32_t>(topic));
2607 return;
2608 }
2609 if (message.size() > SCREEN_MSG_MAX) {
2610 DHLOGE("StartDScreenListener message size too long.");
2611 return;
2612 }
2613 std::string sinkDevId = "";
2614 SrcScreenInfo srcScreenInfo = {};
2615 int32_t parseRes = ParseMessage(message, sinkDevId, srcScreenInfo);
2616 if (parseRes != DH_SUCCESS) {
2617 DHLOGE("Parse message failed!");
2618 return;
2619 }
2620
2621 std::string srcDevId = GetLocalNetworkId();
2622 std::string virtualTouchScreenDHId = DistributedInputInject::GetInstance().GenerateVirtualTouchScreenDHId(
2623 srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, srcScreenInfo.sourceWinHeight);
2624 int32_t createNodeRes = DistributedInputInject::GetInstance().CreateVirtualTouchScreenNode(srcDevId,
2625 virtualTouchScreenDHId, srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth,
2626 srcScreenInfo.sourceWinHeight);
2627 if (createNodeRes != DH_SUCCESS) {
2628 DHLOGE("Create virtual touch screen Node failed!");
2629 return;
2630 }
2631
2632 int32_t cacheRes = UpdateSrcScreenInfoCache(srcScreenInfo);
2633 if (cacheRes != DH_SUCCESS) {
2634 DHLOGE("Update SrcScreenInfo cache failed!");
2635 return;
2636 }
2637
2638 int32_t rpcRes = DistributedInputClient::GetInstance().NotifyStartDScreen(sinkDevId, srcDevId,
2639 srcScreenInfo.sourceWinId);
2640 if (rpcRes != DH_SUCCESS) {
2641 DHLOGE("Rpc invoke failed!");
2642 return;
2643 }
2644
2645 sptr<IRemoteObject> dScreenSrcSA =
2646 DInputContext::GetInstance().GetRemoteObject(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID);
2647 sptr<DScreenSourceSvrRecipient> dScreenSrcDeathRecipient(new(std::nothrow) DScreenSourceSvrRecipient(srcDevId,
2648 sinkDevId, srcScreenInfo.sourceWinId));
2649 dScreenSrcSA->AddDeathRecipient(dScreenSrcDeathRecipient);
2650 DInputContext::GetInstance().AddRemoteObject(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, dScreenSrcSA);
2651 }
2652
ParseMessage(const std::string & message,std::string & sinkDevId,SrcScreenInfo & srcScreenInfo)2653 int32_t DistributedInputSourceManager::StartDScreenListener::ParseMessage(const std::string &message,
2654 std::string &sinkDevId, SrcScreenInfo &srcScreenInfo)
2655 {
2656 nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false);
2657 if (jsonObj.is_discarded()) {
2658 DHLOGE("jsonObj parse failed!");
2659 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2660 }
2661 if (!IsString(jsonObj, SINK_DEVICE_ID)) {
2662 DHLOGE("devId key is invalid");
2663 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2664 }
2665 sinkDevId = jsonObj[SINK_DEVICE_ID].get<std::string>();
2666 if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) {
2667 DHLOGE("sourceWinId key is invalid");
2668 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2669 }
2670 srcScreenInfo.sourceWinId = jsonObj[SOURCE_WINDOW_ID].get<uint64_t>();
2671 if (!IsUInt32(jsonObj, SOURCE_WINDOW_WIDTH)) {
2672 DHLOGE("sourceWinWidth key is invalid");
2673 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2674 }
2675 srcScreenInfo.sourceWinWidth = jsonObj[SOURCE_WINDOW_WIDTH].get<std::uint32_t>();
2676 if (!IsUInt32(jsonObj, SOURCE_WINDOW_HEIGHT)) {
2677 DHLOGE("sourceWinHeight key is invalid");
2678 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2679 }
2680 srcScreenInfo.sourceWinHeight = jsonObj[SOURCE_WINDOW_HEIGHT].get<std::uint32_t>();
2681 return DH_SUCCESS;
2682 }
2683
UpdateSrcScreenInfoCache(const SrcScreenInfo & tmpInfo)2684 int32_t DistributedInputSourceManager::StartDScreenListener::UpdateSrcScreenInfoCache(const SrcScreenInfo &tmpInfo)
2685 {
2686 std::string srcDevId = GetLocalNetworkId();
2687 std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, tmpInfo.sourceWinId);
2688 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(srcScreenInfoKey);
2689 srcScreenInfo.devId = srcDevId;
2690 srcScreenInfo.sessionId = DistributedInputSourceTransport::GetInstance().GetCurrentSessionId();
2691 srcScreenInfo.uuid = GetUUIDBySoftBus(srcDevId);
2692 srcScreenInfo.sourceWinId = tmpInfo.sourceWinId;
2693 srcScreenInfo.sourceWinWidth = tmpInfo.sourceWinWidth;
2694 srcScreenInfo.sourceWinHeight = tmpInfo.sourceWinHeight;
2695 srcScreenInfo.sourcePhyId = DistributedInputInject::GetInstance().GenerateVirtualTouchScreenDHId(
2696 srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth, srcScreenInfo.sourceWinHeight);
2697 int32_t virtualScreenFd = DistributedInputInject::GetInstance().GetVirtualTouchScreenFd();
2698 if (virtualScreenFd < 0) {
2699 DHLOGE("virtualScreenFd is invalid");
2700 return ERR_DH_INPUT_SERVER_SOURCE_VIRTUAL_SCREEN_NODE_IS_INVALID;
2701 }
2702 srcScreenInfo.sourcePhyFd = static_cast<uint32_t>(virtualScreenFd);
2703 srcScreenInfo.sourcePhyWidth = tmpInfo.sourceWinWidth;
2704 srcScreenInfo.sourcePhyHeight = tmpInfo.sourceWinHeight;
2705 DHLOGI("StartDScreenListener UpdateSrcScreenInfo the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d,"
2706 "sourceWinHeight: %d, sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d",
2707 GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth,
2708 srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd,
2709 srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight);
2710 return DInputContext::GetInstance().UpdateSrcScreenInfo(srcScreenInfoKey, srcScreenInfo);
2711 }
2712
StopDScreenListener()2713 DistributedInputSourceManager::StopDScreenListener::StopDScreenListener()
2714 {
2715 DHLOGI("StopDScreenListener ctor!");
2716 }
2717
~StopDScreenListener()2718 DistributedInputSourceManager::StopDScreenListener::~StopDScreenListener()
2719 {
2720 DHLOGI("StopDScreenListener dtor!");
2721 }
2722
OnMessage(const DHTopic topic,const std::string & message)2723 void DistributedInputSourceManager::StopDScreenListener::OnMessage(const DHTopic topic, const std::string &message)
2724 {
2725 DHLOGI("StopDScreenListener OnMessage!");
2726 if (topic != DHTopic::TOPIC_STOP_DSCREEN) {
2727 DHLOGE("this topic is wrong, %d", static_cast<uint32_t>(topic));
2728 return;
2729 }
2730 std::string sinkDevId = "";
2731 uint64_t sourceWinId = 0;
2732 int32_t parseRes = ParseMessage(message, sinkDevId, sourceWinId);
2733 if (parseRes != DH_SUCCESS) {
2734 DHLOGE("Message parse failed!");
2735 return;
2736 }
2737
2738 std::string sourceDevId = GetLocalNetworkId();
2739 std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(sourceDevId, sourceWinId);
2740 DHLOGI("screenInfoKey: %s", GetAnonyString(screenInfoKey).c_str());
2741 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey);
2742
2743 int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(
2744 srcScreenInfo.sourcePhyId);
2745 if (removeNodeRes != DH_SUCCESS) {
2746 DHLOGE("Remove virtual touch screen node failed!");
2747 return;
2748 }
2749
2750 int32_t removeCacheRes = DInputContext::GetInstance().RemoveSrcScreenInfo(screenInfoKey);
2751 if (removeCacheRes != DH_SUCCESS) {
2752 DHLOGE("Remove src cache failed!");
2753 return;
2754 }
2755
2756 int32_t rpcRes = DistributedInputClient::GetInstance().NotifyStopDScreen(sinkDevId, screenInfoKey);
2757 if (rpcRes != DH_SUCCESS) {
2758 DHLOGE("Rpc invoke failed!");
2759 return;
2760 }
2761 }
2762
ParseMessage(const std::string & message,std::string & sinkDevId,uint64_t & sourceWinId)2763 int32_t DistributedInputSourceManager::StopDScreenListener::ParseMessage(const std::string &message,
2764 std::string &sinkDevId, uint64_t &sourceWinId)
2765 {
2766 nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false);
2767 if (jsonObj.is_discarded()) {
2768 DHLOGE("jsonObj parse failed!");
2769 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2770 }
2771 if (!IsString(jsonObj, SINK_DEVICE_ID)) {
2772 DHLOGE("sourceWinId key is invalid");
2773 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2774 }
2775 sinkDevId = jsonObj[SINK_DEVICE_ID].get<std::string>();
2776 if (!IsUInt64(jsonObj, SOURCE_WINDOW_ID)) {
2777 DHLOGE("sourceWinId key is invalid");
2778 return ERR_DH_INPUT_JSON_PARSE_FAIL;
2779 }
2780 sourceWinId = jsonObj[SOURCE_WINDOW_ID].get<uint64_t>();
2781 return DH_SUCCESS;
2782 }
2783
DeviceOfflineListener(DistributedInputSourceManager * srcManagerContext)2784 DistributedInputSourceManager::DeviceOfflineListener::DeviceOfflineListener(
2785 DistributedInputSourceManager* srcManagerContext)
2786 {
2787 DHLOGI("DeviceOfflineListener ctor!");
2788 sourceManagerContext_ = srcManagerContext;
2789 }
2790
~DeviceOfflineListener()2791 DistributedInputSourceManager::DeviceOfflineListener::~DeviceOfflineListener()
2792 {
2793 DHLOGI("DeviceOfflineListener dtor!");
2794 }
2795
OnMessage(const DHTopic topic,const std::string & message)2796 void DistributedInputSourceManager::DeviceOfflineListener::OnMessage(const DHTopic topic, const std::string &message)
2797 {
2798 DHLOGI("DeviceOfflineListener OnMessage!");
2799 if (topic != DHTopic::TOPIC_DEV_OFFLINE) {
2800 DHLOGE("this topic is wrong, %d", static_cast<uint32_t>(topic));
2801 return;
2802 }
2803 if (message.empty()) {
2804 DHLOGE("this message is empty");
2805 return;
2806 }
2807 DeleteNodeInfoAndNotify(message);
2808 }
2809
DeleteNodeInfoAndNotify(const std::string & offlineDevId)2810 void DistributedInputSourceManager::DeviceOfflineListener::DeleteNodeInfoAndNotify(const std::string &offlineDevId)
2811 {
2812 DHLOGI("DeviceOfflineListener DeleteNodeInfoAndNotify!");
2813 if (sourceManagerContext_ == nullptr) {
2814 DHLOGE("sourceManagerContext is nullptr!");
2815 return;
2816 }
2817 std::set<BeRegNodeInfo> nodeSet = sourceManagerContext_->GetSyncNodeInfo(offlineDevId);
2818 std::string localNetWorkId = GetLocalNetworkId();
2819 for (const auto& node : nodeSet) {
2820 DHLOGI("DeleteNodeInfoAndNotify device: %s, dhId: %s", GetAnonyString(offlineDevId).c_str(),
2821 GetAnonyString(node.dhId).c_str());
2822 // Notify multimodal
2823 DistributedInputInject::GetInstance().SyncNodeOfflineInfo(offlineDevId, localNetWorkId, node.dhId);
2824 }
2825 // Delete info
2826 sourceManagerContext_->DeleteSyncNodeInfo(offlineDevId);
2827 }
2828
DScreenSourceSvrRecipient(const std::string & srcDevId,const std::string & sinkDevId,const uint64_t srcWinId)2829 DistributedInputSourceManager::DScreenSourceSvrRecipient::DScreenSourceSvrRecipient(const std::string &srcDevId,
2830 const std::string &sinkDevId, const uint64_t srcWinId)
2831 {
2832 DHLOGI("DScreenStatusListener ctor!");
2833 this->srcDevId_ = srcDevId;
2834 this->sinkDevId_ = sinkDevId;
2835 this->srcWinId_ = srcWinId;
2836 }
2837
~DScreenSourceSvrRecipient()2838 DistributedInputSourceManager::DScreenSourceSvrRecipient::~DScreenSourceSvrRecipient()
2839 {
2840 DHLOGI("DScreenStatusListener dtor!");
2841 }
2842
OnRemoteDied(const wptr<IRemoteObject> & remote)2843 void DistributedInputSourceManager::DScreenSourceSvrRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
2844 {
2845 DHLOGI("DScreenStatusListener OnRemoveSystemAbility");
2846 sptr<IRemoteObject> remoteObject = remote.promote();
2847 if (!remoteObject) {
2848 DHLOGE("OnRemoteDied remote promoted failed");
2849 return;
2850 }
2851 std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId_, srcWinId_);
2852 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey);
2853
2854 int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(
2855 srcScreenInfo.sourcePhyId);
2856 if (removeNodeRes != DH_SUCCESS) {
2857 DHLOGE("Remove virtual touch screen node failed!");
2858 return;
2859 }
2860
2861 int32_t removeCacheRes = DInputContext::GetInstance().RemoveSrcScreenInfo(screenInfoKey);
2862 if (removeCacheRes != DH_SUCCESS) {
2863 DHLOGE("Remove src cache failed!");
2864 return;
2865 }
2866
2867 int32_t rpcRes = DistributedInputClient::GetInstance().NotifyStopDScreen(sinkDevId_, screenInfoKey);
2868 if (rpcRes != DH_SUCCESS) {
2869 DHLOGE("Rpc invoke failed!");
2870 return;
2871 }
2872 DInputContext::GetInstance().RemoveRemoteObject(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID);
2873 }
2874
Dump(int32_t fd,const std::vector<std::u16string> & args)2875 int32_t DistributedInputSourceManager::Dump(int32_t fd, const std::vector<std::u16string> &args)
2876 {
2877 DHLOGI("DistributedInputSourceManager Dump.");
2878 std::vector<std::string> argsStr(args.size());
2879 std::transform(args.begin(), args.end(), argsStr.begin(), [](const auto &item) {return Str16ToStr8(item);});
2880 std::string result("");
2881 if (!HiDumper::GetInstance().HiDump(argsStr, result)) {
2882 DHLOGI("Hidump error.");
2883 return ERR_DH_INPUT_HIDUMP_DUMP_PROCESS_FAIL;
2884 }
2885
2886 int ret = dprintf(fd, "%s\n", result.c_str());
2887 if (ret < 0) {
2888 DHLOGE("dprintf error.");
2889 return ERR_DH_INPUT_HIDUMP_DPRINTF_FAIL;
2890 }
2891 return DH_SUCCESS;
2892 }
2893 } // namespace DistributedInput
2894 } // namespace DistributedHardware
2895 } // namespace OHOS
2896