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