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 "nlohmann/json.hpp"
17 
18 #include "constants_dinput.h"
19 #include "dinput_errcode.h"
20 #include "dinput_log.h"
21 #include "distributed_input_client.h"
22 #include "white_list_util.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
GetInstance()27 DistributedInputClient &DistributedInputClient::GetInstance()
28 {
29     static DistributedInputClient instance;
30     return instance;
31 }
32 
OnResult(const std::string & devId,const std::string & dhId,const int32_t & status)33 void DistributedInputClient::RegisterDInputCb::OnResult(
34     const std::string &devId, const std::string &dhId, const int32_t &status)
35 {
36     auto iter = DistributedInputClient::GetInstance().dHardWareFwkRstInfos_.begin();
37     for (; iter != DistributedInputClient::GetInstance().dHardWareFwkRstInfos_.end(); ++iter) {
38         if (iter->devId == devId && iter->dhId == dhId) {
39             iter->callback->OnRegisterResult(devId, dhId, status, "");
40             DistributedInputClient::GetInstance().dHardWareFwkRstInfos_.erase(iter);
41             return;
42         }
43     }
44 }
45 
OnResult(const std::string & devId,const std::string & dhId,const int32_t & status)46 void DistributedInputClient::UnregisterDInputCb::OnResult(
47     const std::string &devId, const std::string &dhId, const int32_t &status)
48 {
49     auto iter = DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos_.begin();
50     for (; iter != DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos_.end(); ++iter) {
51         if (iter->devId == devId && iter->dhId == dhId) {
52             iter->callback->OnUnregisterResult(devId, dhId, status, "");
53             DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos_.erase(iter);
54             return;
55         }
56     }
57 }
58 
OnResult(const std::string & deviceId,const std::string & strJson)59 void DistributedInputClient::AddWhiteListInfosCb::OnResult(const std::string &deviceId, const std::string &strJson)
60 {
61     nlohmann::json inputData = nlohmann::json::parse(strJson, nullptr, false);
62     if (inputData.is_discarded()) {
63         DHLOGE("inputData parse error.");
64         return;
65     }
66     if (!inputData.is_array()) {
67         DHLOGE("inputData not vector!");
68         return;
69     }
70     size_t jsonSize = inputData.size();
71     DHLOGI("AddWhiteListInfosCb OnResult json size:%{public}zu.\n", jsonSize);
72     TYPE_WHITE_LIST_VEC vecWhiteList = inputData;
73     WhiteListUtil::GetInstance().SyncWhiteList(deviceId, vecWhiteList);
74 }
75 
OnResult(const std::string & deviceId)76 void DistributedInputClient::DelWhiteListInfosCb::OnResult(const std::string &deviceId)
77 {
78     WhiteListUtil::GetInstance().ClearWhiteList(deviceId);
79 }
80 
InitSource()81 int32_t DistributedInputClient::InitSource()
82 {
83     return DH_SUCCESS;
84 }
85 
InitSink()86 int32_t DistributedInputClient::InitSink()
87 {
88     return DH_SUCCESS;
89 }
90 
ReleaseSource()91 int32_t DistributedInputClient::ReleaseSource()
92 {
93     return DH_SUCCESS;
94 }
95 
ReleaseSink()96 int32_t DistributedInputClient::ReleaseSink()
97 {
98     return DH_SUCCESS;
99 }
100 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & parameters,const std::shared_ptr<RegisterCallback> & callback)101 int32_t DistributedInputClient::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
102     const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback)
103 {
104     return DH_SUCCESS;
105 }
106 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::shared_ptr<UnregisterCallback> & callback)107 int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
108     const std::shared_ptr<UnregisterCallback> &callback)
109 {
110     return DH_SUCCESS;
111 }
112 
PrepareRemoteInput(const std::string & deviceId,sptr<IPrepareDInputCallback> callback)113 int32_t DistributedInputClient::PrepareRemoteInput(
114     const std::string &deviceId, sptr<IPrepareDInputCallback> callback)
115 {
116     return DH_SUCCESS;
117 }
118 
UnprepareRemoteInput(const std::string & deviceId,sptr<IUnprepareDInputCallback> callback)119 int32_t DistributedInputClient::UnprepareRemoteInput(
120     const std::string &deviceId, sptr<IUnprepareDInputCallback> callback)
121 {
122     return DH_SUCCESS;
123 }
124 
StartRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)125 int32_t DistributedInputClient::StartRemoteInput(
126     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
127 {
128     return DH_SUCCESS;
129 }
130 
StopRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)131 int32_t DistributedInputClient::StopRemoteInput(
132     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
133 {
134     return DH_SUCCESS;
135 }
136 
IsNeedFilterOut(const std::string & deviceId,const BusinessEvent & event)137 bool DistributedInputClient::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event)
138 {
139     return true;
140 }
141 
IsJsonData(std::string strData) const142 bool DistributedInputClient::IsJsonData(std::string strData) const
143 {
144     return true;
145 }
146 } // namespace DistributedInput
147 } // namespace DistributedHardware
148 } // namespace OHOS
149