1 /*
2 * Copyright (c) 2022 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 "input_parse.h"
17
18 #include <sstream>
19
20 #include "cJSON.h"
21 #include "define_multimodal.h"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "GetDeviceNode" };
27 struct JsonParser {
28 JsonParser() = default;
~JsonParserOHOS::MMI::__anon5e64fa040111::JsonParser29 ~JsonParser()
30 {
31 if (json_ != nullptr) {
32 cJSON_Delete(json_);
33 }
34 }
operator cJSON*OHOS::MMI::__anon5e64fa040111::JsonParser35 operator cJSON *()
36 {
37 return json_;
38 }
39 cJSON *json_ { nullptr };
40 };
41
GetJsonData(cJSON * json,const std::string & key,std::string & val)42 void GetJsonData(cJSON *json, const std::string& key, std::string& val)
43 {
44 if (!cJSON_IsObject(json)) {
45 MMI_HILOGE("The json is not object");
46 return;
47 }
48 if (cJSON_HasObjectItem(json, key.c_str())) {
49 cJSON* rawValue = cJSON_GetObjectItem(json, key.c_str());
50 if (cJSON_IsString(rawValue)) {
51 val = rawValue->valuestring;
52 }
53 }
54 return;
55 }
56
57 template <class T>
GetJsonData(cJSON * json,const std::string & key,T & val)58 void GetJsonData(cJSON *json, const std::string& key, T& val)
59 {
60 if (!cJSON_IsObject(json)) {
61 MMI_HILOGE("The json is not object");
62 return;
63 }
64 if (cJSON_HasObjectItem(json, key.c_str())) {
65 cJSON* rawNum = cJSON_GetObjectItem(json, key.c_str());
66 if (cJSON_IsNumber(rawNum)) {
67 val = rawNum->valueint;
68 }
69 }
70 return;
71 }
72
GetJsonData(cJSON * json,const std::string & key,std::vector<int32_t> & vals)73 void GetJsonData(cJSON *json, const std::string& key, std::vector<int32_t>& vals)
74 {
75 if (!cJSON_IsObject(json)) {
76 MMI_HILOGE("The json is not object");
77 return;
78 }
79 if (!cJSON_HasObjectItem(json, key.c_str())) {
80 MMI_HILOGE("The json is not data:%{public}s", key.c_str());
81 return;
82 }
83 cJSON* rawVal = cJSON_GetObjectItem(json, key.c_str());
84 if (!cJSON_IsArray(rawVal)) {
85 MMI_HILOGE("The rawVal is not Array");
86 return;
87 }
88 int32_t rawValSize = cJSON_GetArraySize(rawVal);
89 for (int32_t i = 0; i < rawValSize; ++i) {
90 cJSON* val = cJSON_GetArrayItem(rawVal, i);
91 if (cJSON_IsNumber(val)) {
92 vals.push_back(val->valueint);
93 }
94 }
95 return;
96 }
97
ParseEvents(cJSON * eventInfo,DeviceEvent & event)98 bool ParseEvents(cJSON* eventInfo, DeviceEvent& event)
99 {
100 if (!cJSON_IsArray(eventInfo)) {
101 MMI_HILOGE("The eventInfo is not array");
102 return false;
103 }
104 int32_t eventSize = cJSON_GetArraySize(eventInfo);
105 for (int32_t i = 0; i < eventSize; ++i) {
106 cJSON* eventArray = cJSON_GetArrayItem(eventInfo, i);
107 if (cJSON_IsArray(eventArray)) {
108 cJSON* xPos = cJSON_GetArrayItem(eventArray, 0);
109 if (!cJSON_IsNumber(xPos)) {
110 MMI_HILOGE("The xPos is not number");
111 return false;
112 }
113 Pos pos;
114 pos.xPos = xPos->valueint;
115 cJSON* yPos = cJSON_GetArrayItem(eventArray, 1);
116 if (!cJSON_IsNumber(yPos)) {
117 MMI_HILOGE("The yPos is not number");
118 return false;
119 }
120 pos.yPos = yPos->valueint;
121 event.posXY.push_back(pos);
122 }
123 }
124 return true;
125 }
126
ParseEventsObj(cJSON * eventInfo,DeviceEvent & event)127 void ParseEventsObj(cJSON* eventInfo, DeviceEvent& event)
128 {
129 if (!cJSON_IsObject(eventInfo)) {
130 MMI_HILOGE("The eventInfo is not object");
131 return;
132 }
133 GetJsonData(eventInfo, "eventType", event.eventType);
134 GetJsonData(eventInfo, "event", event.event);
135 GetJsonData(eventInfo, "keyValue", event.keyValue);
136 GetJsonData(eventInfo, "blockTime", event.blockTime);
137 GetJsonData(eventInfo, "ringEvents", event.ringEvents);
138 GetJsonData(eventInfo, "direction", event.direction);
139 GetJsonData(eventInfo, "distance", event.distance);
140 GetJsonData(eventInfo, "xPos", event.xPos);
141 GetJsonData(eventInfo, "yPos", event.yPos);
142 GetJsonData(eventInfo, "tiltX", event.tiltX);
143 GetJsonData(eventInfo, "tiltY", event.tiltY);
144 GetJsonData(eventInfo, "pressure", event.pressure);
145 GetJsonData(eventInfo, "trackingId", event.trackingId);
146 GetJsonData(eventInfo, "reportType", event.reportType);
147 GetJsonData(eventInfo, "keyStatus", event.keyStatus);
148 return;
149 }
150
ParseData(cJSON * events,std::vector<DeviceEvent> & eventData)151 bool ParseData(cJSON* events, std::vector<DeviceEvent>& eventData)
152 {
153 if (!cJSON_IsArray(events)) {
154 MMI_HILOGE("The events is not array");
155 return false;
156 }
157 int32_t eventsSize = cJSON_GetArraySize(events);
158 for (int32_t i = 0; i < eventsSize; ++i) {
159 cJSON* eventInfo = cJSON_GetArrayItem(events, i);
160 DeviceEvent event;
161 if (cJSON_IsArray(eventInfo)) {
162 if (!ParseEvents(eventInfo, event)) {
163 MMI_HILOGE("Failed to parse events");
164 return false;
165 }
166 } else if (cJSON_IsObject(eventInfo)) {
167 ParseEventsObj(eventInfo, event);
168 } else {
169 MMI_HILOGE("Events is error");
170 return false;
171 }
172 eventData.push_back(event);
173 }
174 return true;
175 }
176 } // namespace
177
ToString() const178 std::string Pos::ToString() const
179 {
180 std::ostringstream ss;
181 ss << "pos(" << xPos << "," << yPos << ")";
182 return ss.str();
183 }
184
ToString() const185 std::string DeviceEvent::ToString() const
186 {
187 std::ostringstream ss;
188 ss << "{eventType:" << eventType
189 << ",event:[";
190 for (const auto &item : event) {
191 ss << item << ",";
192 }
193 ss << "],keyValue:" << keyValue
194 << ",blockTime:" << blockTime
195 << ",ringEvents:[";
196 for (const auto &item : ringEvents) {
197 ss << item << ",";
198 }
199 ss << "],direction:" << direction
200 << ",distance:" << distance
201 << ",xPos:" << xPos
202 << ",yPos:" << yPos
203 << ",tiltX:" << tiltX
204 << ",tiltY:" << tiltY
205 << ",pressure:" << pressure
206 << ",trackingId:" << trackingId
207 << ",reportType:" << reportType
208 << ",keyStatus:" << keyStatus
209 << ",posXY:";
210 for (const auto &item : posXY) {
211 ss << item.ToString() << ",";
212 }
213 ss << "}" << std::endl;
214 return ss.str();
215 }
216
ToString() const217 std::string DeviceItem::ToString() const
218 {
219 std::ostringstream ss;
220 ss << "{deviceName:" << deviceName
221 << ",deviceIndex:" << deviceIndex
222 << ",events:[";
223 for (const auto &item : events) {
224 ss << item.ToString() << ",";
225 }
226 ss << "]" << std::endl;
227 return ss.str();
228 }
229
DataInit(const std::string & fileData,bool logStatus)230 DeviceItems DataInit(const std::string& fileData, bool logStatus)
231 {
232 CALL_DEBUG_ENTER;
233 JsonParser parser;
234 parser.json_ = cJSON_Parse(fileData.c_str());
235 if (!cJSON_IsArray(parser.json_)) {
236 MMI_HILOGE("The parser is not array");
237 return {};
238 }
239 int32_t arraysSize = cJSON_GetArraySize(parser.json_);
240 DeviceItems deviceItems;
241 for (int32_t i = 0; i < arraysSize; ++i) {
242 cJSON* deviceInfo = cJSON_GetArrayItem(parser.json_, i);
243 if (!cJSON_IsObject(deviceInfo)) {
244 MMI_HILOGE("The deviceInfo is not Object");
245 return {};
246 }
247 cJSON* deviceName = cJSON_GetObjectItem(deviceInfo, "deviceName");
248 if (!cJSON_IsString(deviceName)) {
249 MMI_HILOGE("The deviceName is not string");
250 return {};
251 }
252 DeviceItem deviceItem;
253 deviceItem.deviceName = deviceName->valuestring;
254 GetJsonData(deviceInfo, "deviceIndex", deviceItem.deviceIndex);
255 cJSON *events = nullptr;
256 if (cJSON_HasObjectItem(deviceInfo, "events")) {
257 events = cJSON_GetObjectItem(deviceInfo, "events");
258 } else if (cJSON_HasObjectItem(deviceInfo, "singleEvent")) {
259 events = cJSON_GetObjectItem(deviceInfo, "singleEvent");
260 }
261 if (!cJSON_IsArray(events)) {
262 MMI_HILOGE("The events is not array");
263 return {};
264 }
265 if (!ParseData(events, deviceItem.events)) {
266 MMI_HILOGE("Failed to parse data");
267 return {};
268 }
269 deviceItems.push_back(deviceItem);
270 if (logStatus) {
271 MMI_HILOGW("deviceItem[%{public}d]:%{public}s", i, deviceItem.ToString().c_str());
272 }
273 }
274 return deviceItems;
275 }
276 } // namespace MMI
277 } // namespace OHOS