• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "distributed_input_client.h"
17 
18 #include "iservice_registry.h"
19 #include "nlohmann/json.hpp"
20 #include "system_ability_definition.h"
21 
22 #include "constants_dinput.h"
23 #include "dinput_context.h"
24 #include "dinput_errcode.h"
25 #include "dinput_log.h"
26 #include "dinput_utils_tool.h"
27 #include "distributed_input_source_proxy.h"
28 #include "input_check_param.h"
29 #include "softbus_bus_center.h"
30 #include "white_list_util.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace DistributedInput {
35 std::shared_ptr<DistributedInputClient> DistributedInputClient::instance(new DistributedInputClient());
DistributedInputClient()36 DistributedInputClient::DistributedInputClient() : isAddWhiteListCbReg(false), isDelWhiteListCbReg(false),
37     isNodeMonitorCbReg(false), isSimulationEventCbReg(false), isSharingDhIdsReg(false)
38 {
39     DHLOGI("DistributedInputClient init start");
40     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
41     eventHandler_ = std::make_shared<DistributedInputClient::DInputClientEventHandler>(runner);
42     DInputSAManager::GetInstance().RegisterEventHandler(eventHandler_);
43     DInputSAManager::GetInstance().Init();
44     DHLOGI("DistributedInputClient init end.");
45 }
46 
GetInstance()47 DistributedInputClient &DistributedInputClient::GetInstance()
48 {
49     return *instance.get();
50 }
51 
OnResult(const std::string & devId,const std::string & dhId,const int32_t & status)52 void DistributedInputClient::RegisterDInputCb::OnResult(
53     const std::string &devId, const std::string &dhId, const int32_t &status)
54 {
55     std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
56     for (std::vector<DHardWareFwkRegistInfo>::iterator iter =
57         DistributedInputClient::GetInstance().dHardWareFwkRstInfos.begin();
58         iter != DistributedInputClient::GetInstance().dHardWareFwkRstInfos.end();
59         ++iter) {
60         if (iter->devId == devId && iter->dhId == dhId) {
61             iter->callback->OnRegisterResult(devId, dhId, status, "");
62             DistributedInputClient::GetInstance().dHardWareFwkRstInfos.erase(iter);
63             return;
64         }
65     }
66 }
67 
OnResult(const std::string & devId,const std::string & dhId,const int32_t & status)68 void DistributedInputClient::UnregisterDInputCb::OnResult(
69     const std::string &devId, const std::string &dhId, const int32_t &status)
70 {
71     std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
72     for (std::vector<DHardWareFwkUnRegistInfo>::iterator iter =
73         DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.begin();
74         iter != DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.end();
75         ++iter) {
76         if (iter->devId == devId && iter->dhId == dhId) {
77             iter->callback->OnUnregisterResult(devId, dhId, status, "");
78             DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.erase(iter);
79             return;
80         }
81     }
82 }
83 
OnResult(const std::string & deviceId,const std::string & strJson)84 void DistributedInputClient::AddWhiteListInfosCb::OnResult(const std::string &deviceId, const std::string &strJson)
85 {
86     if (!strJson.empty()) {
87         DistributedInputClient::GetInstance().AddWhiteListInfos(deviceId, strJson);
88     }
89 }
90 
OnResult(const std::string & deviceId)91 void DistributedInputClient::DelWhiteListInfosCb::OnResult(const std::string &deviceId)
92 {
93     DistributedInputClient::GetInstance().DelWhiteListInfos(deviceId);
94 }
95 
OnResult(const std::string & strJson)96 void DistributedInputClient::GetSinkScreenInfosCb::OnResult(const std::string &strJson)
97 {
98     if (!strJson.empty()) {
99         DistributedInputClient::GetInstance().UpdateSinkScreenInfos(strJson);
100     }
101 }
102 
OnSharing(std::string dhId)103 int32_t DistributedInputClient::SharingDhIdListenerCb::OnSharing(std::string dhId)
104 {
105     std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().sharingDhIdsMtx_);
106     DHLOGI("Add Sharing Local dhId: %s", GetAnonyString(dhId).c_str());
107     DistributedInputClient::GetInstance().sharingDhIds_.insert(dhId);
108     return DH_SUCCESS;
109 }
110 
OnNoSharing(std::string dhId)111 int32_t DistributedInputClient::SharingDhIdListenerCb::OnNoSharing(std::string dhId)
112 {
113     std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().sharingDhIdsMtx_);
114     DHLOGI("Remove No Sharing Local dhId: %s", GetAnonyString(dhId).c_str());
115     DistributedInputClient::GetInstance().sharingDhIds_.erase(dhId);
116     return DH_SUCCESS;
117 }
118 
DInputClientEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)119 DistributedInputClient::DInputClientEventHandler::DInputClientEventHandler(
120     const std::shared_ptr<AppExecFwk::EventRunner> &runner)
121     : AppExecFwk::EventHandler(runner)
122 {
123 }
124 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)125 void DistributedInputClient::DInputClientEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
126 {
127     uint32_t eventId = event->GetInnerEventId();
128     DHLOGI("DInputClientEventHandler ProcessEvent start eventId:%d.", eventId);
129     if (eventId == DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG) {
130         DistributedInputClient::GetInstance().CheckSourceRegisterCallback();
131         return;
132     }
133 
134     if (eventId == DINPUT_CLIENT_CHECK_SINK_CALLBACK_REGISTER_MSG) {
135         DistributedInputClient::GetInstance().CheckSinkRegisterCallback();
136         return;
137     }
138 
139     if (eventId == DINPUT_CLIENT_CLEAR_SOURCE_CALLBACK_REGISTER_MSG) {
140         DHLOGI("Source SA exit, clear callback flag");
141         DistributedInputClient::GetInstance().isAddWhiteListCbReg = false;
142         DistributedInputClient::GetInstance().isDelWhiteListCbReg = false;
143         DistributedInputClient::GetInstance().isNodeMonitorCbReg = false;
144         DistributedInputClient::GetInstance().isSimulationEventCbReg = false;
145         return;
146     }
147 
148     if (eventId == DINPUT_CLIENT_CLEAR_SINK_CALLBACK_REGISTER_MSG) {
149         DHLOGI("Sink SA exit, clear callback flag");
150         DistributedInputClient::GetInstance().isSharingDhIdsReg = false;
151         return;
152     }
153 }
154 
CheckSourceRegisterCallback()155 void DistributedInputClient::CheckSourceRegisterCallback()
156 {
157     DHLOGI("CheckSourceRegisterCallback called, isAddWhiteListCbReg[%d], isDelWhiteListCbReg[%d],"
158         "isNodeMonitorCbReg[%d], isSimulationEventCbReg[%d]",
159         isAddWhiteListCbReg.load(), isDelWhiteListCbReg.load(), isNodeMonitorCbReg.load(),
160         isSimulationEventCbReg.load());
161 
162     CheckWhiteListCallback();
163     CheckNodeMonitorCallback();
164     CheckKeyStateCallback();
165 }
166 
CheckSinkRegisterCallback()167 void DistributedInputClient::CheckSinkRegisterCallback()
168 {
169     DHLOGI("CheckSinkRegisterCallback called, isSharingDhIdsReg[%d]", isSharingDhIdsReg.load());
170     CheckSharingDhIdsCallback();
171     CheckSinkScreenInfoCallback();
172 }
173 
CheckSharingDhIdsCallback()174 void DistributedInputClient::CheckSharingDhIdsCallback()
175 {
176     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
177         DHLOGE("CheckWhiteListCallback client get source proxy fail");
178         return;
179     }
180     if (!isSharingDhIdsReg) {
181         sptr<ISharingDhIdListener> listener(new (std::nothrow) SharingDhIdListenerCb());
182         int32_t ret =
183             DInputSAManager::GetInstance().dInputSinkProxy_->RegisterSharingDhIdListener(listener);
184         if (ret == DH_SUCCESS) {
185             isSharingDhIdsReg = true;
186             std::lock_guard<std::mutex> lock(operationMutex_);
187             sharingDhIdListeners_.insert(listener);
188         } else {
189             DHLOGE("CheckSharingDhIdsCallback client RegisterSharingDhIdListener fail");
190         }
191     }
192 }
193 
CheckWhiteListCallback()194 void DistributedInputClient::CheckWhiteListCallback()
195 {
196     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
197         DHLOGE("CheckWhiteListCallback client get source proxy fail");
198         return;
199     }
200     if (!isAddWhiteListCbReg) {
201         sptr<AddWhiteListInfosCb> addCallback(new (std::nothrow) AddWhiteListInfosCb());
202         int32_t ret =
203             DInputSAManager::GetInstance().dInputSourceProxy_->RegisterAddWhiteListCallback(addCallback);
204         if (ret == DH_SUCCESS) {
205             isAddWhiteListCbReg = true;
206             std::lock_guard<std::mutex> lock(operationMutex_);
207             addWhiteListCallbacks_.insert(addCallback);
208         } else {
209             DHLOGE("CheckWhiteListCallback client RegisterAddWhiteListCallback fail");
210         }
211     }
212     if (!isDelWhiteListCbReg) {
213         sptr<DelWhiteListInfosCb> delCallback(new (std::nothrow) DelWhiteListInfosCb());
214         int32_t ret =
215             DInputSAManager::GetInstance().dInputSourceProxy_->RegisterDelWhiteListCallback(delCallback);
216         if (ret == DH_SUCCESS) {
217             isDelWhiteListCbReg = true;
218             std::lock_guard<std::mutex> lock(operationMutex_);
219             delWhiteListCallbacks_.insert(delCallback);
220         } else {
221             DHLOGE("CheckWhiteListCallback client RegisterDelWhiteListCallback fail");
222         }
223     }
224 }
225 
CheckNodeMonitorCallback()226 void DistributedInputClient::CheckNodeMonitorCallback()
227 {
228     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
229         DHLOGE("CheckNodeMonitorCallback client get source proxy fail");
230         return;
231     }
232     if (!isNodeMonitorCbReg && regNodeListener_ != nullptr) {
233         DHLOGI("CheckNodeMonitorCallback need continue register regNodeListener_.");
234         DInputSAManager::GetInstance().dInputSourceProxy_->RegisterInputNodeListener(regNodeListener_);
235         isNodeMonitorCbReg = true;
236     }
237 }
238 
CheckKeyStateCallback()239 void DistributedInputClient::CheckKeyStateCallback()
240 {
241     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
242         DHLOGE("CheckKeyStateCallback client get source proxy fail");
243         return;
244     }
245     if (!isSimulationEventCbReg && regSimulationEventListener_ != nullptr) {
246         DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(regSimulationEventListener_);
247         isSimulationEventCbReg = true;
248     }
249 }
250 
CheckSinkScreenInfoCallback()251 void DistributedInputClient::CheckSinkScreenInfoCallback()
252 {
253     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
254         DHLOGE("get sink proxy fail");
255         return;
256     }
257     if (!isGetSinkScreenInfosCbReg) {
258         sptr<GetSinkScreenInfosCb> callback(new (std::nothrow) GetSinkScreenInfosCb());
259         int32_t ret =
260             DInputSAManager::GetInstance().dInputSinkProxy_->RegisterGetSinkScreenInfosCallback(callback);
261         if (ret == DH_SUCCESS) {
262             isGetSinkScreenInfosCbReg = true;
263             std::lock_guard<std::mutex> lock(operationMutex_);
264             getSinkScreenInfosCallbacks_.insert(callback);
265         } else {
266             DHLOGE("RegisterAddWhiteListCallback fail");
267         }
268     }
269 }
270 
InitSource()271 int32_t DistributedInputClient::InitSource()
272 {
273     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
274         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
275     }
276     return DInputSAManager::GetInstance().dInputSourceProxy_->Init();
277 }
278 
InitSink()279 int32_t DistributedInputClient::InitSink()
280 {
281     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
282         return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL;
283     }
284     return DInputSAManager::GetInstance().dInputSinkProxy_->Init();
285 }
286 
ReleaseSource()287 int32_t DistributedInputClient::ReleaseSource()
288 {
289     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
290         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
291     }
292 
293     serverType = DInputServerType::NULL_SERVER_TYPE;
294     inputTypes_ = DInputDeviceType::NONE;
295     regNodeListener_ = nullptr;
296     unregNodeListener_ = nullptr;
297     regSimulationEventListener_ = nullptr;
298     unregSimulationEventListener_ = nullptr;
299     WhiteListUtil::GetInstance().ClearWhiteList();
300     {
301         std::lock_guard<std::mutex> lock(operationMutex_);
302         addWhiteListCallbacks_.clear();
303         delWhiteListCallbacks_.clear();
304     }
305     return DInputSAManager::GetInstance().dInputSourceProxy_->Release();
306 }
307 
ReleaseSink()308 int32_t DistributedInputClient::ReleaseSink()
309 {
310     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
311         return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL;
312     }
313     serverType = DInputServerType::NULL_SERVER_TYPE;
314     inputTypes_ = DInputDeviceType::NONE;
315     {
316         std::lock_guard<std::mutex> lock(operationMutex_);
317         getSinkScreenInfosCallbacks_.clear();
318         sharingDhIdListeners_.clear();
319     }
320     WhiteListUtil::GetInstance().ClearWhiteList();
321     return DInputSAManager::GetInstance().dInputSinkProxy_->Release();
322 }
323 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & parameters,const std::shared_ptr<RegisterCallback> & callback)324 int32_t DistributedInputClient::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
325     const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback)
326 {
327     DHLOGI("DinputRegister called, deviceId: %s,  dhId: %s,  parameters: %s.",
328         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str());
329     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
330         DHLOGE("DinputRegister client fail.");
331         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
332     }
333     if (!DInputCheckParam::GetInstance().CheckRegisterParam(devId, dhId, parameters, callback)) {
334         return ERR_DH_INPUT_CLIENT_REGISTER_FAIL;
335     }
336     {
337         std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
338         for (auto iter : dHardWareFwkRstInfos) {
339             if (iter.devId == devId && iter.dhId == dhId) {
340                 return ERR_DH_INPUT_CLIENT_REGISTER_FAIL;
341             }
342         }
343         DHardWareFwkRegistInfo info {devId, dhId, callback};
344         dHardWareFwkRstInfos.push_back(info);
345     }
346 
347     return DInputSAManager::GetInstance().dInputSourceProxy_->RegisterDistributedHardware(devId, dhId, parameters,
348         new(std::nothrow) RegisterDInputCb());
349 }
350 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::shared_ptr<UnregisterCallback> & callback)351 int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
352     const std::shared_ptr<UnregisterCallback> &callback)
353 {
354     DHLOGI("DinputUnregister called, deviceId: %s,  dhId: %s.",
355         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
356     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
357         DHLOGE("DinputUnregister client fail.");
358         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
359     }
360     if (!DInputCheckParam::GetInstance().CheckUnregisterParam(devId, dhId, callback)) {
361         return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL;
362     }
363     {
364         std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
365         for (auto iter : dHardWareFwkUnRstInfos) {
366             if (iter.devId == devId && iter.dhId == dhId) {
367                 return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL;
368             }
369         }
370         DHardWareFwkUnRegistInfo info {devId, dhId, callback};
371         dHardWareFwkUnRstInfos.push_back(info);
372     }
373 
374     return DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterDistributedHardware(devId, dhId,
375         new(std::nothrow) UnregisterDInputCb());
376 }
377 
PrepareRemoteInput(const std::string & deviceId,sptr<IPrepareDInputCallback> callback)378 int32_t DistributedInputClient::PrepareRemoteInput(const std::string &deviceId, sptr<IPrepareDInputCallback> callback)
379 {
380     DHLOGI("DinputPrepare called, deviceId: %s.", GetAnonyString(deviceId).c_str());
381     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
382         DHLOGE("DinputPrepare client fail.");
383         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
384     }
385     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, callback)) {
386         return ERR_DH_INPUT_CLIENT_PREPARE_FAIL;
387     }
388     return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(deviceId, callback);
389 }
390 
UnprepareRemoteInput(const std::string & deviceId,sptr<IUnprepareDInputCallback> callback)391 int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &deviceId,
392     sptr<IUnprepareDInputCallback> callback)
393 {
394     DHLOGI("DinputUnprepare called, deviceId: %s.", GetAnonyString(deviceId).c_str());
395     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
396         DHLOGE("DinputUnprepare client fail.");
397         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
398     }
399     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, callback)) {
400         return ERR_DH_INPUT_CLIENT_UNPREPARE_FAIL;
401     }
402     return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(deviceId, callback);
403 }
404 
StartRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)405 int32_t DistributedInputClient::StartRemoteInput(
406     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
407 {
408     DHLOGI("DinputStart called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes);
409     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
410         DHLOGE("DinputStart client fail.");
411         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
412     }
413     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) {
414         return ERR_DH_INPUT_CLIENT_START_FAIL;
415     }
416     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(deviceId, inputTypes, callback);
417 }
418 
StopRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)419 int32_t DistributedInputClient::StopRemoteInput(const std::string &deviceId, const uint32_t &inputTypes,
420     sptr<IStopDInputCallback> callback)
421 {
422     DHLOGI("DinputStop called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes);
423     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
424         DHLOGE("DinputStop client fail.");
425         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
426     }
427     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) {
428         return ERR_DH_INPUT_CLIENT_STOP_FAIL;
429     }
430     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(deviceId, inputTypes, callback);
431 }
432 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)433 int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
434     const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
435 {
436     DHLOGI("DinputStart called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(),
437         GetAnonyString(sinkId).c_str(), inputTypes);
438 
439     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
440         DHLOGE("DinputStart relay type client fail.");
441         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
442     }
443     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
444         return ERR_DH_INPUT_CLIENT_START_FAIL;
445     }
446     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, inputTypes, callback);
447 }
448 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)449 int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
450     const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
451 {
452     DHLOGI("DinputStop called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(),
453         GetAnonyString(sinkId).c_str(), inputTypes);
454     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
455         DHLOGE("DinputStop relay type client fail.");
456         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
457     }
458     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
459         return ERR_DH_INPUT_CLIENT_STOP_FAIL;
460     }
461     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, inputTypes, callback);
462 }
463 
PrepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IPrepareDInputCallback> callback)464 int32_t DistributedInputClient::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
465     sptr<IPrepareDInputCallback> callback)
466 {
467     DHLOGI("DinputPrepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(),
468         GetAnonyString(sinkId).c_str());
469     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
470         DHLOGE("DinputPrepare relay proxy error, client fail.");
471         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
472     }
473     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
474         return ERR_DH_INPUT_CLIENT_PREPARE_FAIL;
475     }
476     return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(srcId, sinkId, callback);
477 }
478 
UnprepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IUnprepareDInputCallback> callback)479 int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
480     sptr<IUnprepareDInputCallback> callback)
481 {
482     DHLOGI("DinputUnprepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(),
483         GetAnonyString(sinkId).c_str());
484     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
485         DHLOGE("DinputUnprepare relay proxy error, client fail.");
486         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
487     }
488     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
489         return ERR_DH_INPUT_CLIENT_UNPREPARE_FAIL;
490     }
491     return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(srcId, sinkId, callback);
492 }
493 
StartRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)494 int32_t DistributedInputClient::StartRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
495     sptr<IStartStopDInputsCallback> callback)
496 {
497     DHLOGI("DinputStart called, sinkId: %s.", GetAnonyString(sinkId).c_str());
498     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
499         DHLOGE("DinputStart dhid proxy error, client fail.");
500         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
501     }
502     if (!DInputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) {
503         return ERR_DH_INPUT_CLIENT_START_FAIL;
504     }
505     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(sinkId, dhIds, callback);
506 }
507 
StopRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)508 int32_t DistributedInputClient::StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
509     sptr<IStartStopDInputsCallback> callback)
510 {
511     DHLOGI("DinputStop called, sinkId: %s.", GetAnonyString(sinkId).c_str());
512     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
513         DHLOGE("DinputStop dhid proxy error, client fail.");
514         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
515     }
516     if (!DInputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) {
517         return ERR_DH_INPUT_CLIENT_STOP_FAIL;
518     }
519     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(sinkId, dhIds, callback);
520 }
521 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)522 int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
523     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
524 {
525     DHLOGI("DinputStart called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
526     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
527         DHLOGE("DinputStart proxy error, client fail.");
528         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
529     }
530     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) {
531         return ERR_DH_INPUT_CLIENT_START_FAIL;
532     }
533     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, dhIds, callback);
534 }
535 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)536 int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
537     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
538 {
539     DHLOGI("DinputStop called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
540     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
541         DHLOGE("DinputStop proxy error, client fail.");
542         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
543     }
544     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) {
545         return ERR_DH_INPUT_CLIENT_STOP_FAIL;
546     }
547     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, dhIds, callback);
548 }
549 
IsNeedFilterOut(const std::string & deviceId,const BusinessEvent & event)550 bool DistributedInputClient::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event)
551 {
552     DHLOGI("IsNeedFilterOut called, deviceId: %s", GetAnonyString(deviceId).c_str());
553     if (deviceId.empty() || (deviceId.size() > DEV_ID_LENGTH_MAX)) {
554         DHLOGE("IsNeedFilterOut param deviceId is empty.");
555         return false;
556     }
557     return WhiteListUtil::GetInstance().IsNeedFilterOut(deviceId, event);
558 }
559 
IsTouchEventNeedFilterOut(const TouchScreenEvent & event)560 bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &event)
561 {
562     std::lock_guard<std::mutex> lock(operationMutex_);
563     for (const auto& info : screenTransInfos) {
564         DHLOGI("sinkProjPhyWidth: %d sinkProjPhyHeight: %d", info.sinkProjPhyWidth, info.sinkProjPhyHeight);
565         if ((event.absX >= info.sinkWinPhyX) && (event.absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth))
566             && (event.absY >= info.sinkWinPhyY)  && (event.absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) {
567             return true;
568         }
569     }
570     return false;
571 }
572 
IsStartDistributedInput(const std::string & dhId)573 bool DistributedInputClient::IsStartDistributedInput(const std::string &dhId)
574 {
575     std::lock_guard<std::mutex> lock(sharingDhIdsMtx_);
576     if (dhId.empty() || (dhId.size() > DH_ID_LENGTH_MAX)) {
577         DHLOGE("IsStartDistributedInput param dhid is error.");
578         return false;
579     }
580     return sharingDhIds_.find(dhId) != sharingDhIds_.end();
581 }
582 
RegisterInputNodeListener(sptr<InputNodeListener> listener)583 int32_t DistributedInputClient::RegisterInputNodeListener(sptr<InputNodeListener> listener)
584 {
585     DHLOGI("RegisterInputNodeListener called");
586     if (listener == nullptr) {
587         DHLOGE("RegisterInputNodeListener param error, client fail");
588         return ERR_DH_INPUT_CLIENT_REG_NODE_CB_FAIL;
589     }
590     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
591         DHLOGE("RegisterInputNodeListener proxy error, client fail");
592         isNodeMonitorCbReg = false;
593         regNodeListener_ = listener;
594         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
595     }
596 
597     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterInputNodeListener(listener);
598     if (ret == DH_SUCCESS) {
599         isNodeMonitorCbReg = true;
600     } else {
601         isNodeMonitorCbReg = false;
602         regNodeListener_ = listener;
603         DHLOGE("RegisterInputNodeListener Failed, ret = %d", ret);
604     }
605     return ret;
606 }
607 
UnregisterInputNodeListener(sptr<InputNodeListener> listener)608 int32_t DistributedInputClient::UnregisterInputNodeListener(sptr<InputNodeListener> listener)
609 {
610     DHLOGI("UnregisterInputNodeListener called");
611     if (listener == nullptr) {
612         DHLOGE("UnregisterInputNodeListener param error, client fail");
613         return ERR_DH_INPUT_CLIENT_UNREG_NODE_CB_FAIL;
614     }
615     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
616         DHLOGE("UnregisterInputNodeListener proxy error, client fail");
617         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
618     }
619 
620     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterInputNodeListener(listener);
621     if (ret != DH_SUCCESS) {
622         DHLOGE("DInputSAManager UnregisterInputNodeListener Failed, ret = %d", ret);
623     }
624     return ret;
625 }
626 
RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)627 int32_t DistributedInputClient::RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)
628 {
629     DHLOGI("RegisterSimulationEventListener called Simulation Event Listener Register.");
630     if (listener == nullptr) {
631         DHLOGE("RegisterSimulationEventListener param error");
632         return ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL;
633     }
634     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
635         DHLOGE("RegisterSimulationEventListener proxy error, client fail");
636         isSimulationEventCbReg = false;
637         regSimulationEventListener_ = listener;
638         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
639     }
640 
641     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(listener);
642     if (ret == DH_SUCCESS) {
643         isSimulationEventCbReg = true;
644     } else {
645         isSimulationEventCbReg = false;
646         regSimulationEventListener_ = listener;
647         DHLOGE("RegisterSimulationEventListener Failed, ret = %d", ret);
648     }
649     return ret;
650 }
651 
UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)652 int32_t DistributedInputClient::UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)
653 {
654     DHLOGI("UnregisterSimulationEventListener called Simulation Event Listener UnRegister.");
655     if (listener == nullptr) {
656         DHLOGE("UnregisterSimulationEventListener param error");
657         return ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL;
658     }
659     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
660         DHLOGE("UnregisterSimulationEventListener proxy error, client fail");
661         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
662     }
663 
664     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterSimulationEventListener(listener);
665     if (ret != DH_SUCCESS) {
666         DHLOGE("UnregisterSimulationEventListener Failed, ret = %d", ret);
667     }
668     return ret;
669 }
670 
IsJsonData(std::string strData) const671 bool DistributedInputClient::IsJsonData(std::string strData) const
672 {
673     if (strData[0] != '{') {
674         return false;
675     }
676 
677     int num = 1;
678     for (size_t i = 1; i < strData.length(); ++i) {
679         if (strData[i] == '{') {
680             ++num;
681         } else if (strData[i] == '}') {
682             --num;
683         }
684         if (num == 0) {
685             return true;
686         }
687     }
688 
689     return false;
690 }
691 
AddWhiteListInfos(const std::string & deviceId,const std::string & strJson) const692 void DistributedInputClient::AddWhiteListInfos(const std::string &deviceId, const std::string &strJson) const
693 {
694     nlohmann::json inputData = nlohmann::json::parse(strJson, nullptr, false);
695     if (inputData.is_discarded()) {
696         DHLOGE("InputData parse failed!");
697         return;
698     }
699     if (!inputData.is_array()) {
700         DHLOGE("inputData not vector!");
701         return;
702     }
703     size_t jsonSize = inputData.size();
704     DHLOGI("AddWhiteListInfosCb OnResult deviceId: %s, json str: %s, json size:%d.\n",
705         GetAnonyString(deviceId).c_str(), GetAnonyString(strJson).c_str(), jsonSize);
706     TYPE_WHITE_LIST_VEC vecWhiteList = inputData;
707     WhiteListUtil::GetInstance().SyncWhiteList(deviceId, vecWhiteList);
708 }
709 
DelWhiteListInfos(const std::string & deviceId) const710 void DistributedInputClient::DelWhiteListInfos(const std::string &deviceId) const
711 {
712     WhiteListUtil::GetInstance().ClearWhiteList(deviceId);
713 }
714 
UpdateSinkScreenInfos(const std::string & strJson)715 void DistributedInputClient::UpdateSinkScreenInfos(const std::string &strJson)
716 {
717     std::lock_guard<std::mutex> lock(operationMutex_);
718     screenTransInfos.clear();
719     nlohmann::json inputData = nlohmann::json::parse(strJson, nullptr, false);
720     if (inputData.is_discarded()) {
721         DHLOGE("InputData parse failed!");
722         return;
723     }
724     if (!inputData.is_array()) {
725         DHLOGE("inputData not vector!");
726         return;
727     }
728     size_t jsonSize = inputData.size();
729     DHLOGI("OnResult json str: %s, json size:%d.\n", GetAnonyString(strJson).c_str(), jsonSize);
730     std::vector<std::vector<uint32_t>> transInfos = inputData;
731     for (auto info : transInfos) {
732         if (info.size() != SINK_SCREEN_INFO_SIZE) {
733             DHLOGE("get sinkScreenInfo failed, info size is %d", info.size());
734             continue;
735         }
736         TransformInfo tmp{info[0], info[1], info[2], info[3]};
737         screenTransInfos.emplace_back(tmp);
738         DHLOGI("screenTransInfos size %d", screenTransInfos.size());
739     }
740 }
741 
NotifyStartDScreen(const std::string & sinkDevId,const std::string & srcDevId,const uint64_t srcWinId)742 int32_t DistributedInputClient::NotifyStartDScreen(const std::string &sinkDevId, const std::string &srcDevId,
743     const uint64_t srcWinId)
744 {
745     sptr<IDistributedSinkInput> remoteDInput = GetRemoteDInput(sinkDevId);
746     if (remoteDInput == nullptr || !remoteDInput->AsObject()) {
747         DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(sinkDevId).c_str());
748         return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL;
749     }
750     std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, srcWinId);
751     SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(srcScreenInfoKey);
752     DHLOGI("DistributedInputSinkProxy the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d,"
753         "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d",
754         GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth,
755         srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd,
756         srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight);
757     auto ret = remoteDInput->NotifyStartDScreen(srcScreenInfo);
758     DHLOGI("NotifyStartDScreen, retCode = %d", ret);
759     if (ret != DH_SUCCESS) {
760         DHLOGE("NotifyStartDScreen failed, errCode = %d", ret);
761     }
762     return ret;
763 }
764 
NotifyStopDScreen(const std::string & networkId,const std::string & srcScreenInfoKey)765 int32_t DistributedInputClient::NotifyStopDScreen(const std::string &networkId, const std::string &srcScreenInfoKey)
766 {
767     sptr<IDistributedSinkInput> remoteDInput = GetRemoteDInput(networkId);
768     if (remoteDInput == nullptr || !remoteDInput->AsObject()) {
769         DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(networkId).c_str());
770         return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL;
771     }
772     auto ret = remoteDInput->NotifyStopDScreen(srcScreenInfoKey);
773     DHLOGI("NotifyStopDScreen, retCode = %d", ret);
774     if (ret != DH_SUCCESS) {
775         DHLOGE("NotifyStopDScreen failed, errCode = %d", ret);
776     }
777     return ret;
778 }
779 
GetRemoteDInput(const std::string & networkId) const780 sptr<IDistributedSinkInput> DistributedInputClient::GetRemoteDInput(const std::string &networkId) const
781 {
782     DHLOGI("GetRemoteDInput start, networkId = %s", GetAnonyString(networkId).c_str());
783     if (networkId.empty()) {
784         DHLOGE("networkId is empty");
785         return nullptr;
786     }
787     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
788     if (samgr == nullptr) {
789         DHLOGE("GetSystemAbilityManager failed");
790         return nullptr;
791     }
792     auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, networkId);
793     if (object == nullptr) {
794         DHLOGE("CheckSystemAbility failed");
795         return nullptr;
796     }
797     return iface_cast<IDistributedSinkInput>(object);
798 }
799 } // namespace DistributedInput
800 } // namespace DistributedHardware
801 } // namespace OHOS
802