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