forked from gladish/rtMessage_original
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtMessage.c
488 lines (456 loc) · 10.9 KB
/
rtMessage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* Copyright [2017] [Comcast, Corp.]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rtError.h"
#include "rtMessage.h"
#include <cJSON.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdatomic.h>
struct _rtMessage
{
cJSON* json;
atomic_int count;
};
/**
* Allocate storage and initializes it as new message
* @param pointer to the new message
* @return rtError
**/
rtError
rtMessage_Create(rtMessage* message)
{
*message = (rtMessage) malloc(sizeof(struct _rtMessage));
if (message)
{
(*message)->json = cJSON_CreateObject();
(*message)->count = 1;
return RT_OK;
}
return RT_FAIL;
}
/**
* Copies only the data within the fields of the message
* @param message to be copied
* @param pointer to new copy of the message
* @return rtError
*/
rtError
rtMessage_Clone(rtMessage const message, rtMessage* copy)
{
*copy = (rtMessage) malloc(sizeof(struct _rtMessage));
if (copy)
{
(*copy)->json = cJSON_Duplicate(message->json, 1);
(*copy)->count = 1;
return RT_OK;
}
return RT_FAIL;
}
/* Allocates storage and initializes it as new message
* @param pointer to the new message
* @param fill the new message with this data
* @return rtError
**/
rtError
rtMessage_FromBytes(rtMessage* message, uint8_t const* bytes, int n)
{
(void) n;
#if 0
printf("------------------------------------------\n")
for (i = 0; i < 256; ++i)
{
if (i > 0)
printf(" ");
if (i % 16 == 0)
printf("\n");
printf("0x%02x", bytes[i]);
}
printf("\n\n");
#endif
*message = (rtMessage) malloc(sizeof(struct _rtMessage));
if (message)
{
(*message)->json = cJSON_Parse((char *) bytes);
return RT_OK;
}
return RT_FAIL;
}
/**
* Destroy a message; free the storage that it occupies.
* @param pointer to message to be destroyed
* @return rtError
**/
rtError
rtMessage_Destroy(rtMessage message)
{
if ((message) && ((message)->count == 0))
{
if (message->json)
cJSON_Delete(message->json);
free(message);
return RT_OK;
}
return RT_FAIL;
}
/**
* Extract the data from a message as a byte sequence.
* @param extract the data bytes from this message.
* @param pointer to the byte sequence location
* @param pointer to number of bytes in the message
* @return rtErro
**/
rtError
rtMessage_ToByteArray(rtMessage message, uint8_t** buff, uint32_t *n)
{
return rtMessage_ToString(message, (char **) buff, n);
}
/**
* Format message as string
* @param message to be converted to string
* @param pointer to a string where message is to be stored
* @param pointer to number of bytes in the message
* @return rtStatus
**/
rtError
rtMessage_ToString(rtMessage const m, char** s, uint32_t* n)
{
*s = cJSON_PrintUnformatted(m->json);
*n = strlen(*s);
return RT_OK;
}
/**
* Add string field to the message
* @param message to be modified
* @param name of the field to be added
* @param value of the field to be added
* @return void
**/
void
rtMessage_SetString(rtMessage message, char const* name, char const* value)
{
cJSON_AddItemToObject(message->json, name, cJSON_CreateString(value));
}
/**
* Add integer field to the message
* @param message to be modified
* @param name of the field to be added
* @param integer value of the field to be added
* @return void
**/
void
rtMessage_SetInt32(rtMessage message, char const* name, int32_t value)
{
cJSON_AddNumberToObject(message->json, name, value);
}
/**
* Add double field to the message
* @param message to be modified
* @param name of the field to be added
* @param double value of the field to be added
* @return void
**/
void
rtMessage_SetDouble(rtMessage message, char const* name, double value)
{
cJSON_AddItemToObject(message->json, name, cJSON_CreateNumber(value));
}
/**
* Add sub message field to the message
* @param message to be modified
* @param name of the field to be added
* @param new message item to be added
* @return rtError
**/
rtError
rtMessage_SetMessage(rtMessage message, char const* name, rtMessage item)
{
if (!message || !item)
return RT_ERROR_INVALID_ARG;
if (item->json)
{
cJSON* obj = cJSON_Duplicate(item->json, 1);
cJSON_AddItemToObject(message->json, name, obj);
}
return RT_OK;
}
/**
* Get field value of type string using field name.
* @param message to get field
* @param name of the field
* @param pointer to string value obtained.
* @return rtError
**/
rtError
rtMessage_GetString(rtMessage const message, const char* name, char const** value)
{
cJSON* p = cJSON_GetObjectItem(message->json, name);
if (p)
{
*value = p->valuestring;
return RT_OK;
}
return RT_FAIL;
}
/**
* Get field value of type string using field name.
* @param message to get field
* @param name of the field
* @param pointer to string value obtained.
* @param size of value obtained
* @return rtError
**/
rtError
rtMessage_GetStringValue(rtMessage const message, char const* name, char* fieldvalue, int n)
{
cJSON* p = cJSON_GetObjectItem(message->json, name);
if (p)
{
char *value = p->valuestring;
int const len = (int) strlen(value);
if (len <= n)
{
snprintf(fieldvalue, n, "%s", value);
return RT_OK;
}
return RT_FAIL;
}
return RT_FAIL;
}
/**
* Get field value of type integer using field name.
* @param message to get field
* @param name of the field
* @param pointer to integer value obtained.
* @return rtError
**/
rtError
rtMessage_GetInt32(rtMessage const message,const char* name, int32_t* value)
{
cJSON* p = cJSON_GetObjectItem(message->json, name);
if (p)
{
*value = p->valueint;
return RT_OK;
}
return RT_FAIL;
}
/**
* Get field value of type double using field name.
* @param message to get field
* @param name of the field
* @param pointer to double value obtained.
* @return rtError
**/
rtError
rtMessage_GetDouble(rtMessage const message, char const* name,double* value)
{
cJSON* p = cJSON_GetObjectItem(message->json, name);
if (p)
{
*value = p->valuedouble;
return RT_OK;
}
return RT_FAIL;
}
/**
* Get field value of type message using name
* @param message to get field
* @param name of the field
* @param message obtained
* @return rtError
**/
rtError
rtMessage_GetMessage(rtMessage const message, char const* name, rtMessage* clone)
{
*clone = (rtMessage) malloc(sizeof(struct _rtMessage));
cJSON* p = cJSON_GetObjectItem(message->json, name);
if (p)
{
(*clone)->json = cJSON_Duplicate(p, cJSON_True);
(*clone)->count = 1;
return RT_OK;
}
return RT_FAIL;
}
/**
* Get topic of message to be sent
* @param message to get topic
* @param name of the topic
* @return rtError
**/
rtError
rtMessage_GetSendTopic(rtMessage const m, char* topic)
{
rtError err = RT_OK;
cJSON* obj = cJSON_GetObjectItem(m->json, "_topic");
if (obj)
strcpy(topic, obj->valuestring);
else
err = RT_FAIL;
return err;
}
/**
* Set topic of message to be sent
* @param message to set topic
* @param name of the topic
* @return rtError
**/
rtError
rtMessage_SetSendTopic(rtMessage const m, char const* topic)
{
cJSON* obj = cJSON_GetObjectItem(m->json, "_topic");
if (obj)
cJSON_ReplaceItemInObject(m->json, "_topic", cJSON_CreateString(topic));
else
cJSON_AddItemToObject(m->json, "_topic", cJSON_CreateString(topic));
if (obj)
cJSON_Delete(obj);
return RT_OK;
}
/**
* Add string field to array in message
* @param message to be modified
* @param name of the field to be added
* @param value of the field to be added
* @return rtError
**/
rtError
rtMessage_AddString(rtMessage m, char const* name, char const* value)
{
cJSON* obj = cJSON_GetObjectItem(m->json, name);
if (!obj)
{
obj = cJSON_CreateArray();
cJSON_AddItemToObject(m->json, name, obj);
}
cJSON_AddItemToArray(obj, cJSON_CreateString(value));
return RT_OK;
}
/**
* Add message field to array in message
* @param message to be modified
* @param name of the field to be added
* @param message to be added
* @return rtError
**/
rtError
rtMessage_AddMessage(rtMessage m, char const* name, rtMessage const item)
{
if (!m || !item)
return RT_ERROR_INVALID_ARG;
cJSON* obj = cJSON_GetObjectItem(m->json, name);
if (!obj)
{
obj = cJSON_CreateArray();
cJSON_AddItemToObject(m->json, name, obj);
}
if (item->json)
{
cJSON* item_obj = cJSON_Duplicate(item->json, 1);
cJSON_AddItemToArray(obj, item_obj);
}
return RT_OK;
}
/**
* Get length of array from message
* @param message to get array length from
* @param name of the array
* @param fill length of array
* @return rtError
**/
rtError
rtMessage_GetArrayLength(rtMessage const m, char const* name, int32_t* length)
{
cJSON* obj = cJSON_GetObjectItem(m->json, name);
if (!obj)
*length = 0;
else
*length = cJSON_GetArraySize(obj);
return RT_OK;
}
/**
* Get string item from array in message
* @param message to get string item from
* @param name of the string item
* @param index of array
* @param value obtained
* @param length of string item
* @return rtError
**/
rtError
rtMessage_GetStringItem(rtMessage const m, char const* name, int32_t idx, char* value, int len)
{
cJSON* obj = cJSON_GetObjectItem(m->json, name);
if (!obj)
return RT_PROPERTY_NOT_FOUND;
if (idx >= cJSON_GetArraySize(obj))
return RT_FAIL;
cJSON* item = cJSON_GetArrayItem(obj, idx);
if (item)
{
strncpy(value, item->valuestring, len);
}
return RT_OK;
}
/**
* Get message item from array in parent message
* @param message to get message item from
* @param name of message item
* @param index of array
* @param message obtained
* @return rtError
**/
rtError
rtMessage_GetMessageItem(rtMessage const m, char const* name, int32_t idx, rtMessage* msg)
{
cJSON* obj = cJSON_GetObjectItem(m->json, name);
if (!obj)
return RT_PROPERTY_NOT_FOUND;
if (idx >= cJSON_GetArraySize(obj))
return RT_FAIL;
*msg = (rtMessage) malloc(sizeof(struct _rtMessage));
if (msg)
{
(*msg)->json = cJSON_GetArrayItem(obj, idx);
return RT_OK;
}
return RT_OK;
}
/**
* Increase reference count of message by 1
* @param message
* @return rtError
**/
rtError
rtMessage_Retain(rtMessage m)
{
__atomic_fetch_add(&m->count, 1, __ATOMIC_SEQ_CST);
return 0;
}
/**
* Decrease reference count of message by 1 and destroy message if count is 0
* @param message
* @return rtError
**/
rtError
rtMessage_Release(rtMessage m)
{
__atomic_fetch_sub(&m->count, 1, __ATOMIC_SEQ_CST);
if (m->count == 0)
rtMessage_Destroy(m);
return 0;
}