1 /*----------------------------------------------------------------------------
2 * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 * 1. Redistributions of source code must retain the above copyright notice, this list of
7 * conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
9 * of conditions and the following disclaimer in the documentation and/or other materials
10 * provided with the distribution.
11 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
12 * to endorse or promote products derived from this software without specific prior written
13 * permission.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *---------------------------------------------------------------------------*/
26 /*----------------------------------------------------------------------------
27 * Notice of Export Control Law
28 * ===============================================
29 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
30 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
31 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
32 * applicable export control laws and regulations.
33 *---------------------------------------------------------------------------*/
34 /**
35 * DATE AUTHOR INSTRUCTION
36 * 2020-02-06 14:36 zhangqianfu The first version
37 *
38 */
39 ////< this file used to package the data for the profile
40 #include <oc_mqtt.h>
41 #include <oc_mqtt_profile_package.h>
42
43 #include <cJSON.h>
44
45
cJSON_Delete_root(cJSON * root)46 static void cJSON_Delete_root(cJSON *root)
47 {
48 if (root != NULL) {
49 cJSON_Delete(root);
50 root = NULL;
51 }
52 }
53
54 ///< format the report data to json string mode
profile_fmtvalue(oc_mqtt_profile_kv_t * kv)55 static cJSON *profile_fmtvalue(oc_mqtt_profile_kv_t *kv)
56 {
57 cJSON *ret = NULL;
58 switch (kv->type) {
59 case EN_OC_MQTT_PROFILE_VALUE_INT:
60 ret = cJSON_CreateNumber((double)(*(int *)kv->value));
61 break;
62 case EN_OC_MQTT_PROFILE_VALUE_LONG:
63 ret = cJSON_CreateNumber((double)(*(long *)kv->value));
64 break;
65 case EN_OC_MQTT_PROFILE_VALUE_FLOAT:
66 ret = cJSON_CreateNumber((*(double *)kv->value));
67 break;
68 case EN_OC_MQTT_PROFILE_VALUE_STRING:
69 ret = cJSON_CreateString((const char *)kv->value);
70 break;
71 default:
72 break;
73 }
74
75 return ret;
76 }
77
78 #define CN_OC_MQTT_PROFILE_MSGUP_KEY_DEVICEID "object_device_id"
79 #define CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGNAME "name"
80 #define CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGID "id"
81 #define CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGCONTENT "content"
oc_mqtt_profile_package_msgup(oc_mqtt_profile_msgup_t * payload)82 char *oc_mqtt_profile_package_msgup(oc_mqtt_profile_msgup_t *payload)
83 {
84 char *ret = NULL;
85 cJSON *root = NULL;
86 cJSON *device_id = NULL;
87 cJSON *msg_name;
88 cJSON *msg_id;
89 cJSON *msg_conntent;
90
91 root = cJSON_CreateObject();
92 if (root == NULL) {
93 cJSON_Delete_root(root);
94 return ret;
95 }
96
97 if (payload->device_id != NULL) {
98 device_id = cJSON_CreateString(payload->device_id);
99 if (device_id == NULL) {
100 cJSON_Delete_root(root);
101 return ret;
102 }
103 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_MSGUP_KEY_DEVICEID, device_id);
104 }
105
106 if (payload->name != NULL) {
107 msg_name = cJSON_CreateString(payload->name);
108 if (msg_name == NULL) {
109 cJSON_Delete_root(root);
110 return ret;
111 }
112 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGNAME, msg_name);
113 }
114
115 if (payload->id != NULL) {
116 msg_id = cJSON_CreateString(payload->id);
117 if (msg_id == NULL) {
118 cJSON_Delete_root(root);
119 return ret;
120 }
121 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGID, msg_id);
122 }
123
124 msg_conntent = cJSON_CreateString(payload->msg);
125 if (msg_conntent == NULL) {
126 cJSON_Delete_root(root);
127 return ret;
128 }
129 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_MSGUP_KEY_MSGCONTENT, msg_conntent);
130
131 ///< OK, now we make it to a buffer
132 ret = cJSON_PrintUnformatted(root);
133 cJSON_Delete(root);
134 return ret;
135 }
136
make_kvs(oc_mqtt_profile_kv_t * kvlst)137 static cJSON *make_kvs(oc_mqtt_profile_kv_t *kvlst)
138 {
139 cJSON *root;
140 cJSON *kv;
141 oc_mqtt_profile_kv_t *kv_info;
142
143 ///< build a root node
144 root = cJSON_CreateObject();
145 if (root == NULL) {
146 cJSON_Delete_root(root);
147 return root;
148 }
149
150 ///< add all the property to the properties
151 kv_info = kvlst;
152 while (kv_info != NULL) {
153 kv = profile_fmtvalue(kv_info);
154 if (kv == NULL) {
155 cJSON_Delete_root(root);
156 return root;
157 }
158
159 cJSON_AddItemToObject(root, kv_info->key, kv);
160 kv_info = kv_info->nxt;
161 }
162
163 ///< OK, now we return it
164 return root;
165 }
166
167 #define CN_OC_MQTT_PROFILE_SERVICE_KEY_SERVICEID "service_id"
168 #define CN_OC_MQTT_PROFILE_SERVICE_KEY_PROPERTIES "properties"
169 #define CN_OC_MQTT_PROFILE_SERVICE_KEY_EVENTTIME "event_time"
170
make_service(oc_mqtt_profile_service_t * service_info)171 static cJSON *make_service(oc_mqtt_profile_service_t *service_info)
172 {
173 cJSON *root;
174 cJSON *service_id;
175 cJSON *properties;
176 cJSON *event_time;
177
178 ///< build a root node
179 root = cJSON_CreateObject();
180 if (root == NULL) {
181 cJSON_Delete_root(root);
182 return root;
183 }
184
185 ///< add the service_id node to the root node
186 service_id = cJSON_CreateString(service_info->service_id);
187 if (service_id == NULL) {
188 cJSON_Delete_root(root);
189 return root;
190 }
191 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SERVICE_KEY_SERVICEID, service_id);
192
193 ///< add the properties node to the root
194 properties = make_kvs(service_info->service_property);
195 if (properties == NULL) {
196 cJSON_Delete_root(root);
197 return root;
198 }
199 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SERVICE_KEY_PROPERTIES, properties);
200
201 ///< add the event time (optional) to the root
202 if (service_info->event_time != NULL) {
203 event_time = cJSON_CreateString(service_info->event_time);
204 if (event_time == NULL) {
205 cJSON_Delete_root(root);
206 return root;
207 }
208 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SERVICE_KEY_EVENTTIME, event_time);
209 }
210
211 ///< OK, now we return it
212 return root;
213 }
214
215 #define CN_OC_MQTT_PROFILE_SERVICES_KEY "services"
make_services(oc_mqtt_profile_service_t * service_info)216 static cJSON *make_services(oc_mqtt_profile_service_t *service_info)
217 {
218 cJSON *services = NULL;
219 cJSON *service;
220 oc_mqtt_profile_service_t *service_tmp;
221
222 ///< create the services array node
223 services = cJSON_CreateArray();
224 if (services == NULL) {
225 if (services != NULL) {
226 cJSON_Delete(services);
227 services = NULL;
228 }
229 return services;
230 }
231
232 service_tmp = service_info;
233 while (service_tmp != NULL) {
234 service = make_service(service_tmp);
235 if (service == NULL) {
236 if (services != NULL) {
237 cJSON_Delete(services);
238 services = NULL;
239 }
240 return services;
241 }
242
243 cJSON_AddItemToArray(services, service);
244 service_tmp = service_tmp->nxt;
245 }
246
247 ///< now we return the services
248 return services;
249 }
250
oc_mqtt_profile_package_propertyreport(oc_mqtt_profile_service_t * payload)251 char *oc_mqtt_profile_package_propertyreport(oc_mqtt_profile_service_t *payload)
252 {
253 char *ret = NULL;
254 cJSON *root;
255 cJSON *services;
256
257 ///< create the root node
258 root = cJSON_CreateObject();
259 if (root == NULL) {
260 cJSON_Delete_root(root);
261 return ret;
262 }
263
264 ///< create the services array node to the root
265 services = make_services(payload);
266 if (services == NULL) {
267 cJSON_Delete_root(root);
268 return ret;
269 }
270 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SERVICES_KEY, services);
271
272 ///< OK, now we make it to a buffer
273 ret = cJSON_PrintUnformatted(root);
274 cJSON_Delete(root);
275 return ret;
276 }
277
278 #define CN_OC_MQTT_PROFILE_DEVICES_KEY_DEVICES "devices"
279 #define CN_OC_MQTT_PROFILE_DEVICES_KEY_DEVICEID "device_id"
280
oc_mqtt_profile_package_gwpropertyreport(oc_mqtt_profile_device_t * payload)281 char *oc_mqtt_profile_package_gwpropertyreport(oc_mqtt_profile_device_t *payload)
282 {
283 char *ret = NULL;
284 cJSON *root;
285 cJSON *services;
286 cJSON *devices;
287 cJSON *device;
288 cJSON *device_id;
289
290 oc_mqtt_profile_device_t *device_info;
291
292 ///< create the root node
293 root = cJSON_CreateObject();
294 if (root == NULL) {
295 cJSON_Delete_root(root);
296 return ret;
297 }
298
299 ///< create the devices node and add it to root
300 devices = cJSON_CreateArray();
301 if (devices == NULL) {
302 cJSON_Delete_root(root);
303 return ret;
304 }
305 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_DEVICES_KEY_DEVICES, devices);
306
307 ///< loop all the devices and add it to devices
308 device_info = payload;
309 while (device_info != NULL) {
310 device = cJSON_CreateObject();
311 if (device == NULL) {
312 cJSON_Delete_root(root);
313 return ret;
314 }
315 cJSON_AddItemToArray(devices, device);
316
317 device_id = cJSON_CreateString(device_info->subdevice_id);
318 if (device_id == NULL) {
319 cJSON_Delete_root(root);
320 return ret;
321 }
322 cJSON_AddItemToObjectCS(device, CN_OC_MQTT_PROFILE_DEVICES_KEY_DEVICEID, device_id);
323
324 services = make_services(device_info->subdevice_property);
325 if (services == NULL) {
326 cJSON_Delete_root(root);
327 return ret;
328 }
329 cJSON_AddItemToObjectCS(device, CN_OC_MQTT_PROFILE_SERVICES_KEY, services);
330
331 device_info = device_info->nxt;
332 }
333
334 ///< OK, now we make it to a buffer
335 ret = cJSON_PrintUnformatted(root);
336 cJSON_Delete(root);
337 return ret;
338 }
339
340 #define CN_OC_MQTT_PROFILE_SETPROPERTYRESP_KEY_RETCODE "result_code"
341 #define CN_OC_MQTT_PROFILE_SETPROPERTYRESP_KEY_RETDESC "result_desc"
oc_mqtt_profile_package_propertysetresp(oc_mqtt_profile_propertysetresp_t * payload)342 char *oc_mqtt_profile_package_propertysetresp(oc_mqtt_profile_propertysetresp_t *payload)
343 {
344 char *ret = NULL;
345 cJSON *root;
346 cJSON *ret_code;
347 cJSON *ret_desc;
348
349 ///< create the root node
350 root = cJSON_CreateObject();
351 if (root == NULL) {
352 cJSON_Delete_root(root);
353 return ret;
354 }
355
356 ///< create retcode and retdesc and add it to the root
357 if (payload != NULL) {
358 ret_code = cJSON_CreateNumber(payload->ret_code);
359 if (ret_code == NULL) {
360 cJSON_Delete_root(root);
361 return ret;
362 }
363 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SETPROPERTYRESP_KEY_RETCODE, ret_code);
364
365 if (payload->ret_description != NULL) {
366 ret_desc = cJSON_CreateString(payload->ret_description);
367 if (ret_desc == NULL) {
368 cJSON_Delete_root(root);
369 return ret;
370 }
371 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SETPROPERTYRESP_KEY_RETDESC, ret_desc);
372 }
373 }
374 ///< OK, now we make it to a buffer
375 ret = cJSON_PrintUnformatted(root);
376 cJSON_Delete(root);
377 return ret;
378 }
379
oc_mqtt_profile_package_propertygetresp(oc_mqtt_profile_propertygetresp_t * payload)380 char *oc_mqtt_profile_package_propertygetresp(oc_mqtt_profile_propertygetresp_t *payload)
381 {
382 char *ret = NULL;
383 cJSON *root;
384 cJSON *services;
385
386 ///< create the root node
387 root = cJSON_CreateObject();
388 if (root == NULL) {
389 cJSON_Delete_root(root);
390 return ret;
391 }
392
393 ///< create the services array node to the root
394 services = make_services(payload->services);
395 if (services == NULL) {
396 cJSON_Delete_root(root);
397 return ret;
398 }
399 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SERVICES_KEY, services);
400
401 ///< OK, now we make it to a buffer
402 ret = cJSON_PrintUnformatted(root);
403 cJSON_Delete(root);
404 return ret;
405 }
406
407 #define CN_OC_MQTT_PROFILE_CMDRESP_KEY_RETCODE "result_code"
408 #define CN_OC_MQTT_PROFILE_CMDRESP_KEY_RESPNAME "response_name"
409 #define CN_OC_MQTT_PROFILE_CMDRESP_KEY_PARAS "paras"
oc_mqtt_profile_package_cmdresp(oc_mqtt_profile_cmdresp_t * payload)410 char *oc_mqtt_profile_package_cmdresp(oc_mqtt_profile_cmdresp_t *payload)
411 {
412 char *ret = NULL;
413 cJSON *root;
414 cJSON *ret_code;
415 cJSON *ret_name;
416 cJSON *paras;
417
418 ///< create the root node
419 root = cJSON_CreateObject();
420 if (root == NULL) {
421 cJSON_Delete_root(root);
422 return ret;
423 }
424
425 ///< create retcode and retdesc and add it to the root
426 ret_code = cJSON_CreateNumber(payload->ret_code);
427 if (ret_code == NULL) {
428 cJSON_Delete_root(root);
429 return ret;
430 }
431 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_CMDRESP_KEY_RETCODE, ret_code);
432
433 if (payload->ret_name != NULL) {
434 ret_name = cJSON_CreateString(payload->ret_name);
435 if (ret_name == NULL) {
436 cJSON_Delete_root(root);
437 return ret;
438 }
439 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_SETPROPERTYRESP_KEY_RETDESC, ret_name);
440 }
441
442 if (payload->paras != NULL) {
443 paras = make_kvs(payload->paras);
444 if (paras == NULL) {
445 cJSON_Delete_root(root);
446 return ret;
447 }
448 cJSON_AddItemToObjectCS(root, CN_OC_MQTT_PROFILE_CMDRESP_KEY_PARAS, paras);
449 }
450
451 ///< OK, now we make it to a buffer
452 ret = cJSON_PrintUnformatted(root);
453 cJSON_Delete(root);
454 return ret;
455 }
456