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