• 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), isGetSinkScreenInfosCbReg(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     CheckKeyStateCallback();
164 }
165 
CheckSinkRegisterCallback()166 void DistributedInputClient::CheckSinkRegisterCallback()
167 {
168     DHLOGI("CheckSinkRegisterCallback called, isSharingDhIdsReg[%d]", isSharingDhIdsReg.load());
169     CheckSharingDhIdsCallback();
170     CheckSinkScreenInfoCallback();
171 }
172 
CheckSharingDhIdsCallback()173 void DistributedInputClient::CheckSharingDhIdsCallback()
174 {
175     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
176         DHLOGE("CheckWhiteListCallback client get source proxy fail");
177         return;
178     }
179     if (!isSharingDhIdsReg) {
180         sptr<ISharingDhIdListener> listener(new (std::nothrow) SharingDhIdListenerCb());
181         int32_t ret =
182             DInputSAManager::GetInstance().dInputSinkProxy_->RegisterSharingDhIdListener(listener);
183         if (ret == DH_SUCCESS) {
184             isSharingDhIdsReg = true;
185             std::lock_guard<std::mutex> lock(operationMutex_);
186             sharingDhIdListeners_.insert(listener);
187         } else {
188             DHLOGE("CheckSharingDhIdsCallback client RegisterSharingDhIdListener fail");
189         }
190     }
191 }
192 
CheckWhiteListCallback()193 void DistributedInputClient::CheckWhiteListCallback()
194 {
195     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
196         DHLOGE("CheckWhiteListCallback client get source proxy fail");
197         return;
198     }
199     if (!isAddWhiteListCbReg) {
200         sptr<AddWhiteListInfosCb> addCallback(new (std::nothrow) AddWhiteListInfosCb());
201         int32_t ret =
202             DInputSAManager::GetInstance().dInputSourceProxy_->RegisterAddWhiteListCallback(addCallback);
203         if (ret == DH_SUCCESS) {
204             isAddWhiteListCbReg = true;
205             std::lock_guard<std::mutex> lock(operationMutex_);
206             addWhiteListCallbacks_.insert(addCallback);
207         } else {
208             DHLOGE("CheckWhiteListCallback client RegisterAddWhiteListCallback fail");
209         }
210     }
211     if (!isDelWhiteListCbReg) {
212         sptr<DelWhiteListInfosCb> delCallback(new (std::nothrow) DelWhiteListInfosCb());
213         int32_t ret =
214             DInputSAManager::GetInstance().dInputSourceProxy_->RegisterDelWhiteListCallback(delCallback);
215         if (ret == DH_SUCCESS) {
216             isDelWhiteListCbReg = true;
217             std::lock_guard<std::mutex> lock(operationMutex_);
218             delWhiteListCallbacks_.insert(delCallback);
219         } else {
220             DHLOGE("CheckWhiteListCallback client RegisterDelWhiteListCallback fail");
221         }
222     }
223 }
224 
CheckKeyStateCallback()225 void DistributedInputClient::CheckKeyStateCallback()
226 {
227     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
228         DHLOGE("CheckKeyStateCallback client get source proxy fail");
229         return;
230     }
231     if (!isSimulationEventCbReg && regSimulationEventListener_ != nullptr) {
232         DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(regSimulationEventListener_);
233         isSimulationEventCbReg = true;
234     }
235 }
236 
CheckSinkScreenInfoCallback()237 void DistributedInputClient::CheckSinkScreenInfoCallback()
238 {
239     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
240         DHLOGE("get sink proxy fail");
241         return;
242     }
243     if (!isGetSinkScreenInfosCbReg) {
244         sptr<GetSinkScreenInfosCb> callback(new (std::nothrow) GetSinkScreenInfosCb());
245         int32_t ret =
246             DInputSAManager::GetInstance().dInputSinkProxy_->RegisterGetSinkScreenInfosCallback(callback);
247         if (ret == DH_SUCCESS) {
248             isGetSinkScreenInfosCbReg = true;
249             std::lock_guard<std::mutex> lock(operationMutex_);
250             getSinkScreenInfosCallbacks_.insert(callback);
251         } else {
252             DHLOGE("RegisterAddWhiteListCallback fail");
253         }
254     }
255 }
256 
InitSource()257 int32_t DistributedInputClient::InitSource()
258 {
259     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
260         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
261     }
262     return DInputSAManager::GetInstance().dInputSourceProxy_->Init();
263 }
264 
InitSink()265 int32_t DistributedInputClient::InitSink()
266 {
267     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
268         return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL;
269     }
270     return DInputSAManager::GetInstance().dInputSinkProxy_->Init();
271 }
272 
ReleaseSource()273 int32_t DistributedInputClient::ReleaseSource()
274 {
275     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
276         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
277     }
278 
279     serverType = DInputServerType::NULL_SERVER_TYPE;
280     inputTypes_ = DInputDeviceType::NONE;
281     regNodeListener_ = nullptr;
282     unregNodeListener_ = nullptr;
283     regSimulationEventListener_ = nullptr;
284     unregSimulationEventListener_ = nullptr;
285     WhiteListUtil::GetInstance().ClearWhiteList();
286     {
287         std::lock_guard<std::mutex> lock(operationMutex_);
288         addWhiteListCallbacks_.clear();
289         delWhiteListCallbacks_.clear();
290     }
291     return DInputSAManager::GetInstance().dInputSourceProxy_->Release();
292 }
293 
ReleaseSink()294 int32_t DistributedInputClient::ReleaseSink()
295 {
296     if (!DInputSAManager::GetInstance().GetDInputSinkProxy()) {
297         return ERR_DH_INPUT_CLIENT_GET_SINK_PROXY_FAIL;
298     }
299     serverType = DInputServerType::NULL_SERVER_TYPE;
300     inputTypes_ = DInputDeviceType::NONE;
301     {
302         std::lock_guard<std::mutex> lock(operationMutex_);
303         getSinkScreenInfosCallbacks_.clear();
304         sharingDhIdListeners_.clear();
305     }
306     WhiteListUtil::GetInstance().ClearWhiteList();
307     return DInputSAManager::GetInstance().dInputSinkProxy_->Release();
308 }
309 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & parameters,const std::shared_ptr<RegisterCallback> & callback)310 int32_t DistributedInputClient::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
311     const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback)
312 {
313     DHLOGI("DinputRegister called, deviceId: %s,  dhId: %s,  parameters: %s.",
314         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str());
315     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
316         DHLOGE("DinputRegister client fail.");
317         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
318     }
319     if (!DInputCheckParam::GetInstance().CheckRegisterParam(devId, dhId, parameters, callback)) {
320         return ERR_DH_INPUT_CLIENT_REGISTER_FAIL;
321     }
322     {
323         std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
324         for (auto iter : dHardWareFwkRstInfos) {
325             if (iter.devId == devId && iter.dhId == dhId) {
326                 return ERR_DH_INPUT_CLIENT_REGISTER_FAIL;
327             }
328         }
329         DHardWareFwkRegistInfo info {devId, dhId, callback};
330         dHardWareFwkRstInfos.push_back(info);
331     }
332 
333     return DInputSAManager::GetInstance().dInputSourceProxy_->RegisterDistributedHardware(devId, dhId, parameters,
334         new(std::nothrow) RegisterDInputCb());
335 }
336 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::shared_ptr<UnregisterCallback> & callback)337 int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
338     const std::shared_ptr<UnregisterCallback> &callback)
339 {
340     DHLOGI("DinputUnregister called, deviceId: %s,  dhId: %s.",
341         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
342     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
343         DHLOGE("DinputUnregister client fail.");
344         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
345     }
346     if (!DInputCheckParam::GetInstance().CheckUnregisterParam(devId, dhId, callback)) {
347         return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL;
348     }
349     {
350         std::lock_guard<std::mutex> lock(DistributedInputClient::GetInstance().operationMutex_);
351         for (auto iter : dHardWareFwkUnRstInfos) {
352             if (iter.devId == devId && iter.dhId == dhId) {
353                 return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL;
354             }
355         }
356         DHardWareFwkUnRegistInfo info {devId, dhId, callback};
357         dHardWareFwkUnRstInfos.push_back(info);
358     }
359 
360     return DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterDistributedHardware(devId, dhId,
361         new(std::nothrow) UnregisterDInputCb());
362 }
363 
PrepareRemoteInput(const std::string & deviceId,sptr<IPrepareDInputCallback> callback)364 int32_t DistributedInputClient::PrepareRemoteInput(const std::string &deviceId, sptr<IPrepareDInputCallback> callback)
365 {
366     DHLOGI("DinputPrepare called, deviceId: %s.", GetAnonyString(deviceId).c_str());
367     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
368         DHLOGE("DinputPrepare client fail.");
369         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
370     }
371     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, callback)) {
372         return ERR_DH_INPUT_CLIENT_PREPARE_FAIL;
373     }
374     return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(deviceId, callback);
375 }
376 
UnprepareRemoteInput(const std::string & deviceId,sptr<IUnprepareDInputCallback> callback)377 int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &deviceId,
378     sptr<IUnprepareDInputCallback> callback)
379 {
380     DHLOGI("DinputUnprepare called, deviceId: %s.", GetAnonyString(deviceId).c_str());
381     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
382         DHLOGE("DinputUnprepare 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_UNPREPARE_FAIL;
387     }
388     return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(deviceId, callback);
389 }
390 
StartRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)391 int32_t DistributedInputClient::StartRemoteInput(
392     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
393 {
394     DHLOGI("DinputStart called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes);
395     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
396         DHLOGE("DinputStart client fail.");
397         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
398     }
399     if (!DInputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) {
400         return ERR_DH_INPUT_CLIENT_START_FAIL;
401     }
402     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(deviceId, inputTypes, callback);
403 }
404 
StopRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)405 int32_t DistributedInputClient::StopRemoteInput(const std::string &deviceId, const uint32_t &inputTypes,
406     sptr<IStopDInputCallback> callback)
407 {
408     DHLOGI("DinputStop called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes);
409     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
410         DHLOGE("DinputStop 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_STOP_FAIL;
415     }
416     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(deviceId, inputTypes, callback);
417 }
418 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)419 int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
420     const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
421 {
422     DHLOGI("DinputStart called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(),
423         GetAnonyString(sinkId).c_str(), inputTypes);
424 
425     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
426         DHLOGE("DinputStart relay type client fail.");
427         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
428     }
429     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
430         return ERR_DH_INPUT_CLIENT_START_FAIL;
431     }
432     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, inputTypes, callback);
433 }
434 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)435 int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
436     const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
437 {
438     DHLOGI("DinputStop called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(),
439         GetAnonyString(sinkId).c_str(), inputTypes);
440     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
441         DHLOGE("DinputStop relay type client fail.");
442         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
443     }
444     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) {
445         return ERR_DH_INPUT_CLIENT_STOP_FAIL;
446     }
447     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, inputTypes, callback);
448 }
449 
PrepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IPrepareDInputCallback> callback)450 int32_t DistributedInputClient::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
451     sptr<IPrepareDInputCallback> callback)
452 {
453     DHLOGI("DinputPrepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(),
454         GetAnonyString(sinkId).c_str());
455     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
456         DHLOGE("DinputPrepare relay proxy error, client fail.");
457         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
458     }
459     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
460         return ERR_DH_INPUT_CLIENT_PREPARE_FAIL;
461     }
462     return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(srcId, sinkId, callback);
463 }
464 
UnprepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IUnprepareDInputCallback> callback)465 int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
466     sptr<IUnprepareDInputCallback> callback)
467 {
468     DHLOGI("DinputUnprepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(),
469         GetAnonyString(sinkId).c_str());
470     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
471         DHLOGE("DinputUnprepare relay proxy error, client fail.");
472         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
473     }
474     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) {
475         return ERR_DH_INPUT_CLIENT_UNPREPARE_FAIL;
476     }
477     return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(srcId, sinkId, callback);
478 }
479 
StartRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)480 int32_t DistributedInputClient::StartRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
481     sptr<IStartStopDInputsCallback> callback)
482 {
483     DHLOGI("DinputStart called, sinkId: %s.", GetAnonyString(sinkId).c_str());
484     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
485         DHLOGE("DinputStart dhid proxy error, client fail.");
486         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
487     }
488     if (!DInputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) {
489         return ERR_DH_INPUT_CLIENT_START_FAIL;
490     }
491     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(sinkId, dhIds, callback);
492 }
493 
StopRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)494 int32_t DistributedInputClient::StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
495     sptr<IStartStopDInputsCallback> callback)
496 {
497     DHLOGI("DinputStop called, sinkId: %s.", GetAnonyString(sinkId).c_str());
498     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
499         DHLOGE("DinputStop 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_STOP_FAIL;
504     }
505     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(sinkId, dhIds, callback);
506 }
507 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)508 int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
509     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
510 {
511     DHLOGI("DinputStart called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
512     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
513         DHLOGE("DinputStart proxy error, client fail.");
514         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
515     }
516     if (!DInputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) {
517         return ERR_DH_INPUT_CLIENT_START_FAIL;
518     }
519     return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, dhIds, callback);
520 }
521 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)522 int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
523     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
524 {
525     DHLOGI("DinputStop called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str());
526     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
527         DHLOGE("DinputStop 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_STOP_FAIL;
532     }
533     return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, dhIds, callback);
534 }
535 
IsNeedFilterOut(const std::string & deviceId,const BusinessEvent & event)536 bool DistributedInputClient::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event)
537 {
538     DHLOGI("IsNeedFilterOut called, deviceId: %s", GetAnonyString(deviceId).c_str());
539     if (deviceId.empty() || (deviceId.size() > DEV_ID_LENGTH_MAX)) {
540         DHLOGE("IsNeedFilterOut param deviceId is empty.");
541         return false;
542     }
543     return WhiteListUtil::GetInstance().IsNeedFilterOut(deviceId, event);
544 }
545 
IsTouchEventNeedFilterOut(const TouchScreenEvent & event)546 bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &event)
547 {
548     std::lock_guard<std::mutex> lock(operationMutex_);
549     for (const auto &info : screenTransInfos) {
550         DHLOGI("sinkProjPhyWidth: %d sinkProjPhyHeight: %d", info.sinkProjPhyWidth, info.sinkProjPhyHeight);
551         if ((event.absX >= info.sinkWinPhyX) && (event.absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth))
552             && (event.absY >= info.sinkWinPhyY)  && (event.absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) {
553             return true;
554         }
555     }
556     return false;
557 }
558 
IsStartDistributedInput(const std::string & dhId)559 bool DistributedInputClient::IsStartDistributedInput(const std::string &dhId)
560 {
561     std::lock_guard<std::mutex> lock(sharingDhIdsMtx_);
562     if (dhId.empty() || (dhId.size() > DH_ID_LENGTH_MAX)) {
563         DHLOGE("IsStartDistributedInput param dhid is error.");
564         return false;
565     }
566     return sharingDhIds_.find(dhId) != sharingDhIds_.end();
567 }
568 
RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)569 int32_t DistributedInputClient::RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)
570 {
571     DHLOGI("RegisterSimulationEventListener called Simulation Event Listener Register.");
572     if (listener == nullptr) {
573         DHLOGE("RegisterSimulationEventListener param error");
574         return ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL;
575     }
576     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
577         DHLOGE("RegisterSimulationEventListener proxy error, client fail");
578         isSimulationEventCbReg = false;
579         regSimulationEventListener_ = listener;
580         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
581     }
582 
583     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(listener);
584     if (ret == DH_SUCCESS) {
585         isSimulationEventCbReg = true;
586     } else {
587         isSimulationEventCbReg = false;
588         regSimulationEventListener_ = listener;
589         DHLOGE("RegisterSimulationEventListener Failed, ret = %d", ret);
590     }
591     return ret;
592 }
593 
UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)594 int32_t DistributedInputClient::UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)
595 {
596     DHLOGI("UnregisterSimulationEventListener called Simulation Event Listener UnRegister.");
597     if (listener == nullptr) {
598         DHLOGE("UnregisterSimulationEventListener param error");
599         return ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL;
600     }
601     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
602         DHLOGE("UnregisterSimulationEventListener proxy error, client fail");
603         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
604     }
605 
606     int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterSimulationEventListener(listener);
607     if (ret != DH_SUCCESS) {
608         DHLOGE("UnregisterSimulationEventListener Failed, ret = %d", ret);
609     }
610     return ret;
611 }
612 
IsJsonData(std::string strData) const613 bool DistributedInputClient::IsJsonData(std::string strData) const
614 {
615     if (strData[0] != '{') {
616         return false;
617     }
618 
619     int num = 1;
620     for (size_t i = 1; i < strData.length(); ++i) {
621         if (strData[i] == '{') {
622             ++num;
623         } else if (strData[i] == '}') {
624             --num;
625         }
626         if (num == 0) {
627             return true;
628         }
629     }
630 
631     return false;
632 }
633 
AddWhiteListInfos(const std::string & deviceId,const std::string & strJson) const634 void DistributedInputClient::AddWhiteListInfos(const std::string &deviceId, const std::string &strJson) const
635 {
636     nlohmann::json inputData = nlohmann::json::parse(strJson, nullptr, false);
637     if (inputData.is_discarded()) {
638         DHLOGE("InputData parse failed!");
639         return;
640     }
641     if (!inputData.is_array()) {
642         DHLOGE("inputData not vector!");
643         return;
644     }
645     size_t jsonSize = inputData.size();
646     DHLOGI("AddWhiteListInfosCb OnResult deviceId: %s, json str: %s, json size:%zu.\n",
647         GetAnonyString(deviceId).c_str(), GetAnonyString(strJson).c_str(), jsonSize);
648     TYPE_WHITE_LIST_VEC vecWhiteList = inputData;
649     WhiteListUtil::GetInstance().SyncWhiteList(deviceId, vecWhiteList);
650 }
651 
DelWhiteListInfos(const std::string & deviceId) const652 void DistributedInputClient::DelWhiteListInfos(const std::string &deviceId) const
653 {
654     WhiteListUtil::GetInstance().ClearWhiteList(deviceId);
655 }
656 
UpdateSinkScreenInfos(const std::string & strJson)657 void DistributedInputClient::UpdateSinkScreenInfos(const std::string &strJson)
658 {
659     std::lock_guard<std::mutex> lock(operationMutex_);
660     screenTransInfos.clear();
661     nlohmann::json inputData = nlohmann::json::parse(strJson, nullptr, false);
662     if (inputData.is_discarded()) {
663         DHLOGE("InputData parse failed!");
664         return;
665     }
666     if (!inputData.is_array()) {
667         DHLOGE("inputData not vector!");
668         return;
669     }
670     size_t jsonSize = inputData.size();
671     DHLOGI("OnResult json str: %s, json size:%zu.\n", GetAnonyString(strJson).c_str(), jsonSize);
672     std::vector<std::vector<uint32_t>> transInfos = inputData;
673     for (auto info : transInfos) {
674         if (info.size() != SINK_SCREEN_INFO_SIZE) {
675             DHLOGE("get sinkScreenInfo failed, info size is %d", info.size());
676             continue;
677         }
678         TransformInfo tmp{info[0], info[1], info[2], info[3]};
679         screenTransInfos.emplace_back(tmp);
680         DHLOGI("screenTransInfos size %d", screenTransInfos.size());
681     }
682 }
683 
NotifyStartDScreen(const std::string & sinkDevId,const std::string & srcDevId,const uint64_t srcWinId)684 int32_t DistributedInputClient::NotifyStartDScreen(const std::string &sinkDevId, const std::string &srcDevId,
685     const uint64_t srcWinId)
686 {
687     sptr<IDistributedSinkInput> remoteDInput = GetRemoteDInput(sinkDevId);
688     if (remoteDInput == nullptr || !remoteDInput->AsObject()) {
689         DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(sinkDevId).c_str());
690         return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL;
691     }
692     std::string srcScreenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId, srcWinId);
693     SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(srcScreenInfoKey);
694     DHLOGI("DistributedInputSinkProxy the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d,"
695         "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d",
696         GetAnonyString(srcScreenInfo.devId).c_str(), srcScreenInfo.sourceWinId, srcScreenInfo.sourceWinWidth,
697         srcScreenInfo.sourceWinHeight, GetAnonyString(srcScreenInfo.sourcePhyId).c_str(), srcScreenInfo.sourcePhyFd,
698         srcScreenInfo.sourcePhyWidth, srcScreenInfo.sourcePhyHeight);
699     auto ret = remoteDInput->NotifyStartDScreen(srcScreenInfo);
700     DHLOGI("NotifyStartDScreen, retCode = %d", ret);
701     if (ret != DH_SUCCESS) {
702         DHLOGE("NotifyStartDScreen failed, errCode = %d", ret);
703     }
704     return ret;
705 }
706 
NotifyStopDScreen(const std::string & networkId,const std::string & srcScreenInfoKey)707 int32_t DistributedInputClient::NotifyStopDScreen(const std::string &networkId, const std::string &srcScreenInfoKey)
708 {
709     sptr<IDistributedSinkInput> remoteDInput = GetRemoteDInput(networkId);
710     if (remoteDInput == nullptr || !remoteDInput->AsObject()) {
711         DHLOGE("GetRemoteDInput failed, networkId = %s", GetAnonyString(networkId).c_str());
712         return ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL;
713     }
714     auto ret = remoteDInput->NotifyStopDScreen(srcScreenInfoKey);
715     DHLOGI("NotifyStopDScreen, retCode = %d", ret);
716     if (ret != DH_SUCCESS) {
717         DHLOGE("NotifyStopDScreen failed, errCode = %d", ret);
718     }
719     return ret;
720 }
721 
GetRemoteDInput(const std::string & networkId) const722 sptr<IDistributedSinkInput> DistributedInputClient::GetRemoteDInput(const std::string &networkId) const
723 {
724     DHLOGI("GetRemoteDInput start, networkId = %s", GetAnonyString(networkId).c_str());
725     if (networkId.empty()) {
726         DHLOGE("networkId is empty");
727         return nullptr;
728     }
729     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
730     if (samgr == nullptr) {
731         DHLOGE("GetSystemAbilityManager failed");
732         return nullptr;
733     }
734     auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID, networkId);
735     if (object == nullptr) {
736         DHLOGE("CheckSystemAbility failed");
737         return nullptr;
738     }
739     return iface_cast<IDistributedSinkInput>(object);
740 }
741 
RegisterSessionStateCb(sptr<ISessionStateCallback> callback)742 int32_t DistributedInputClient::RegisterSessionStateCb(sptr<ISessionStateCallback> callback)
743 {
744     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
745         DHLOGE("DinputStart client fail.");
746         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
747     }
748     if (callback == nullptr) {
749         DHLOGE("RegisterSessionStateCb callback is null.");
750         return ERR_DH_INPUT_CLIENT_REGISTER_SESSION_STATE_FAIL;
751     }
752     return DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSessionStateCb(callback);
753 }
754 
UnregisterSessionStateCb()755 int32_t DistributedInputClient::UnregisterSessionStateCb()
756 {
757     if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) {
758         DHLOGE("DinputStart client fail.");
759         return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL;
760     }
761     return DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterSessionStateCb();
762 }
763 } // namespace DistributedInput
764 } // namespace DistributedHardware
765 } // namespace OHOS
766