json.c
Go to the documentation of this file.
1 /*!**********************************************************************
2  *
3  * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4  * All rights reserved.
5  *
6  *************************************************************************
7  *
8  * @file json.c
9  * @date Jul 14, 2016
10  * @brief Json module implementation file.
11  *
12  ************************************************************************/
13 
14 #include "json.h"
15 #include "json_util.h"
16 #include "memory.h"
17 #include "log_util.h"
18 #include "definitions.h"
19 #include "event.h"
20 #include "mcl/mcl_event.h"
21 
22 // Private Function Prototypes:
23 static E_MCL_ERROR_CODE _add_string_field_to_object(json_t *parent_object, char *field_name, const string_t *optional_field_to_be_added, mcl_bool_t is_mandatory);
27 static E_MCL_ERROR_CODE _add_time_series_value_sets(list_t *value_sets, json_t *value_set_array);
31 static E_MCL_ERROR_CODE _add_data_source_configuration_data_sources(list_t *data_source_list, json_t *payload_object);
32 static E_MCL_ERROR_CODE _add_data_source_configuration_data_points(list_t *data_points, json_t *data_sources_object);
33 
34 static E_MCL_ERROR_CODE _add_event_list(list_t *event_list, json_t *event_list_array);
35 static E_MCL_ERROR_CODE _add_event(event_t *event, json_t *event_list_array);
36 
38 {
39  DEBUG_ENTRY("item_meta_t *item_meta = <%p>, string_t **json_string = <%p>", item_meta, json_string)
40 
41  E_MCL_ERROR_CODE code;
42  char *json_string_local = MCL_NULL;
43  json_t *root = MCL_NULL;
44 
45  code = json_util_initialize(JSON_OBJECT, &root);
46  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Root object of item meta couldn't be allocated in memory for json_string.");
47 
48  // Add meta type.
50  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&root), code, "Type couldn't be added to meta!");
51 
52  // Add meta version.
54  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&root), code, "Version couldn't be added to meta!");
55 
56  // Add item meta details.
57  code = _add_item_meta_details(item_meta, root);
58  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&root), code, "Details couldn't be added to meta!");
59 
60  // Add item meta payload.
61  code = _add_item_meta_payload(item_meta, root);
62  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&root), code, "Payload couldn't be added to meta!");
63 
64  MCL_DEBUG("Json string will be set.");
65 
66  code = json_util_to_string(root, &json_string_local);
67  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&root), code, "Json to string conversion failed!");
68 
69  code = string_initialize_dynamic(json_string_local, 0, json_string);
70 
71  json_util_destroy(&root);
72  DEBUG_LEAVE("retVal = <%d>", code);
73  return code;
74 }
75 
77 {
78  DEBUG_ENTRY("time_series_payload_t *payload = <%p>, string_t **json_string = <%p>", payload, json_string)
79 
80  E_MCL_ERROR_CODE code;
81  char *json_string_local = MCL_NULL;
82  json_t *value_set_array = MCL_NULL;
83 
84  MCL_DEBUG("Create payload array.");
85 
86  code = json_util_initialize(JSON_ARRAY, &value_set_array);
87  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "value_set_array couldn't be allocated in memory for json_string.");
88 
89  code = _add_time_series_value_sets(payload->value_sets, value_set_array);
90  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&value_set_array), code, "Time series payload couldn't be added.");
91 
92 
93  code = json_util_to_string(value_set_array, &json_string_local);
94  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&value_set_array), code, "Json to string conversion failed!");
95 
96  code = string_initialize_dynamic(json_string_local, 0, json_string);
97 
98  MCL_DEBUG("Generated json_string = <%s>", (*json_string)->buffer);
99  json_util_destroy(&value_set_array);
100  DEBUG_LEAVE("retVal = <%d>", code);
101  return code;
102 }
103 
105 {
106  DEBUG_ENTRY("data_source_configuration_payload_t *payload = <%p>, string_t **json_string = <%p>", payload, json_string)
107 
108  E_MCL_ERROR_CODE code;
109  json_t *payload_object = MCL_NULL;
110  char *json_string_local = MCL_NULL;
111 
112  MCL_DEBUG("Create payload array.");
113 
114  code = json_util_initialize(JSON_OBJECT, &payload_object);
115 
116  // Add configuration_id to payload_object.
117  (MCL_OK == code) && (code = json_util_add_string(payload_object, payload_field_names[PAYLOAD_FIELD_CONFIGURATION_ID].buffer, payload->configuration_id->buffer));
118 
119  (MCL_OK == code) && (code =_add_data_source_configuration_data_sources(payload->data_sources, payload_object));
120 
121  (MCL_OK == code) && (code = json_util_to_string(payload_object, &json_string_local));
122  (MCL_OK == code) && (code = string_initialize_dynamic(json_string_local, 0, json_string));
123 
124  json_util_destroy(&payload_object);
125  DEBUG_LEAVE("retVal = <%d>", code);
126  return code;
127 }
128 
129 E_MCL_ERROR_CODE json_from_event_payload(list_t *event_list_payload, string_t **json_string)
130 {
131  DEBUG_ENTRY("list_t *event_list_payload = <%p>, string_t **json_string = <%p>", event_list_payload, json_string)
132 
133  // Create root_array object which includes other objects as child.
134  json_t *event_list_payload_array = MCL_NULL;
135  char *json_string_local = MCL_NULL;
136  E_MCL_ERROR_CODE code = json_util_initialize(JSON_ARRAY, &event_list_payload_array);
137  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "event_list_array couldn't be allocated in memory for json_string.");
138 
139  code = _add_event_list(event_list_payload, event_list_payload_array);
140  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&event_list_payload_array), code, "Event set payload couldn't be added.");
141 
142  code = json_util_to_string(event_list_payload_array, &json_string_local);
143  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&event_list_payload_array), code, "Json to string conversion failed!");
144 
145  code = string_initialize_dynamic(json_string_local, 0, json_string);
146 
147  MCL_DEBUG("Generated json_string = <%s>", (*json_string)->buffer);
148  json_util_destroy(&event_list_payload_array);
149 
150  DEBUG_LEAVE("retVal = <%d>", code);
151  return code;
152 }
153 
154 static E_MCL_ERROR_CODE _add_event_list(list_t *event_list, json_t *event_list_array)
155 {
156  DEBUG_ENTRY("list_t *event_list = <%p>, json_t *event_list_array = <%p>", event_list, event_list_array)
157 
158  list_node_t *current_event_node;
159  list_reset(event_list);
160  while (MCL_NULL != (current_event_node = list_next(event_list)))
161  {
162  E_MCL_ERROR_CODE code = _add_event((event_t *)(current_event_node->data), event_list_array);
163  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Event objects couldn't be added to event set array!");
164  }
165 
166  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
167  return MCL_OK;
168 }
169 
170 static E_MCL_ERROR_CODE _add_event(event_t *event, json_t *event_list_array)
171 {
172  DEBUG_ENTRY("event_t *event = <%p>, json_t *event_list_array = <%p>", event, event_list_array)
173 
174  json_t *details_json = MCL_NULL;
175 
176  // Create event_local.
177  json_t *event_local = MCL_NULL;
178 
179  E_MCL_ERROR_CODE code = json_util_initialize(JSON_OBJECT, &event_local);
180  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "event_local couldn't be allocated in memory for json_string.");
181 
182  // Add id.
183  code = json_util_add_string(event_local, payload_field_names[PAYLOAD_FIELD_ID].buffer, event->payload->id->buffer);
184 
185  // Add correlationId (optional field).
186  if (MCL_OK == code)
187  {
189  }
190 
191  // Add timestamp.
192  if (MCL_OK == code)
193  {
195  }
196 
197  // Add severity.
198  if (MCL_OK == code)
199  {
200  code = json_util_add_uint(event_local, payload_field_names[PAYLOAD_FIELD_SEVERITY].buffer, event->payload->severity);
201  }
202 
203  // Add description (optional field).
204  if (MCL_OK == code)
205  {
207  }
208 
209  // Add type.
210  if (MCL_OK == code)
211  {
212  code = json_util_add_string(event_local, payload_field_names[PAYLOAD_FIELD_TYPE].buffer, event->payload->type->buffer);
213  }
214 
215  // Add version.
216  if (MCL_OK == code)
217  {
219  }
220 
221  // Create details.
222  if (MCL_OK == code)
223  {
224  if (MCL_NULL != event->payload->details)
225  {
226  code = json_util_duplicate(event->payload->details, MCL_TRUE, &details_json);
227  }
228  else
229  {
230  code = json_util_initialize(JSON_OBJECT, &details_json);
231  }
232  }
233 
234  // Add details even it is empty.
235  if (MCL_OK == code)
236  {
237  code = json_util_add_object(event_local, payload_field_names[PAYLOAD_FIELD_DETAILS].buffer, details_json);
238 
239  if (MCL_OK != code)
240  {
241  // Destroy all the content.
242  json_util_destroy(&details_json);
243  }
244  }
245 
246  json_util_finish_object(&details_json);
247 
248  // Add event to event list array.
249  if (MCL_OK == code)
250  {
251  json_util_add_item_to_array(event_list_array, event_local);
252  json_util_finish_object(&event_local);
253  }
254  else
255  {
256  json_util_destroy(&event_local);
257  }
258 
259  DEBUG_LEAVE("retVal = <%d>", code);
260  return code;
261 }
262 
263 static E_MCL_ERROR_CODE _add_time_series_value_sets(list_t *value_sets, json_t *value_set_array)
264 {
265  DEBUG_ENTRY("list_t *value_sets = <%p>, json_t *value_set_array = <%p>", value_sets, value_set_array)
266 
267  list_node_t *current_payloads_node;
268 
269  list_reset(value_sets);
270  while (MCL_NULL != (current_payloads_node = list_next(value_sets)))
271  {
272  E_MCL_ERROR_CODE code = _add_time_series_value_set((time_series_value_set_t *)(current_payloads_node->data), value_set_array);
273  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Values objects couldn't be added to values array!");
274  }
275 
276  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
277  return MCL_OK;
278 }
279 
281 {
282  DEBUG_ENTRY("time_series_value_set_t *value_set = <%p>, json_t *value_set_array = <%p>", value_set, value_set_array)
283 
284  E_MCL_ERROR_CODE code;
285  json_t *value_set_local = MCL_NULL;
286 
287  code = json_util_initialize(JSON_OBJECT, &value_set_local);
288 
289  (MCL_OK == code) && (code = json_util_add_string(value_set_local, payload_field_names[PAYLOAD_FIELD_TIMESTAMP].buffer, value_set->timestamp->buffer));
290 
291  (MCL_OK == code) && (code = _add_time_series_payload_values_array(value_set->values, value_set_local));
292 
293  ASSERT_STATEMENT_CODE(MCL_OK == code, json_util_destroy(&value_set_local), code);
294 
295  // Add value_set_local to value_set_array.
296  json_util_add_item_to_array(value_set_array, value_set_local);
297 
298  // value_set_local is added to value_set_array. We can destroy value_set_local struct w/o destroying root_handle in it.
299  json_util_finish_object(&value_set_local);
300 
301  DEBUG_LEAVE("retVal = <%d>", code);
302  return code;
303 }
304 
306 {
307  DEBUG_ENTRY("list_t *values = <%p>, json_t *value_set = <%p>", values, value_set)
308 
309  E_MCL_ERROR_CODE code;
310  json_t *values_array = MCL_NULL;
311  list_node_t *current_value_node;
312 
313  code = json_util_start_array(value_set, payload_field_names[PAYLOAD_FIELD_VALUES].buffer, &values_array);
314  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "values_array object couldn't be allocated in memory for Json string.");
315 
316  list_reset(values);
317  while (MCL_NULL != (current_value_node = list_next(values)))
318  {
319  code = _add_time_series_payload_values_objects((time_series_value_t *)current_value_node->data, values_array);
320  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_finish_array(&values_array), code, "Values objects couldn't be added to values array!");
321  }
322 
323  json_util_finish_array(&values_array);
324 
325  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
326  return MCL_OK;
327 }
328 
330 {
331  DEBUG_ENTRY("time_series_value_t *value = <%p>, json_t *values_array = <%p>", value, values_array)
332 
333  E_MCL_ERROR_CODE code;
334  json_t *values_object = MCL_NULL;
335 
336  code = json_util_initialize(JSON_OBJECT, &values_object);
337 
338  // Add fields of values array in time series payload.
339  (MCL_OK == code) && (code = json_util_add_string(values_object, payload_field_names[PAYLOAD_FIELD_VALUES_DATA_POINT_ID].buffer, value->data_point_id->buffer));
340  (MCL_OK == code) && (code = json_util_add_string(values_object, payload_field_names[PAYLOAD_FIELD_VALUES_VALUE].buffer, value->value->buffer));
341  (MCL_OK == code) && (code = json_util_add_string(values_object, payload_field_names[PAYLOAD_FIELD_VALUES_QUALITY_CODE].buffer, value->quality_code->buffer));
342 
343  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&values_object), code, "Values objects couldn't be added to values array!");
344 
345  // Add values_object to values_array.
346  json_util_add_item_to_array(values_array, values_object);
347 
348  json_util_finish_object(&values_object);
349 
350  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
351  return MCL_OK;
352 }
353 
355 {
356  DEBUG_ENTRY("item_meta_t *item_meta = <%p>, json_t *root = <%p>", item_meta, root)
357 
358  E_MCL_ERROR_CODE code;
359  json_t *details = MCL_NULL;
360 
361  code = json_util_initialize(JSON_OBJECT, &details);
362  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Details object of item meta couldn't be allocated in memory for json_string.");
363 
364  // Check whether the following fields are relevant. if so, add to object.
366 
367  // If details has at least one field, then we add it into root_object. Otherwise it is destroyed without adding to the root.
368  if (MCL_OK == code && MCL_TRUE == json_util_has_child(details))
369  {
370  code = json_util_add_object(root, meta_field_names[META_FIELD_DETAILS].buffer, details);
371 
372  if (MCL_OK == code)
373  {
374  // details is added to root parent. We can destroy details struct w/o destroying root_handle in it.
375  json_util_finish_object(&details);
376  }
377  else
378  {
379  json_util_destroy(&details);
380  }
381  }
382  else
383  {
384  json_util_destroy(&details);
385  }
386 
387  DEBUG_LEAVE("retVal = <%d>", code);
388  return code;
389 }
390 
392 {
393  DEBUG_ENTRY("item_meta_t *item_meta = <%p>, json_t *payload = <%p>", item_meta, payload)
394 
395  E_MCL_ERROR_CODE code;
396  json_t *payload_details = MCL_NULL;
397 
398  MCL_DEBUG("Item type specific fields will be added.");
399 
401  {
402  MCL_DEBUG("Payload type is time series.");
403 
404  code = json_util_initialize(JSON_OBJECT, &payload_details);
405  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "payload_details object couldn't be allocated in memory for Json string.");
406 
408  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&payload_details), code, "configuration_id couldn't be added to item meta of time series!");
409  }
411  {
412  MCL_DEBUG("Payload type is file");
413 
414  code = json_util_initialize(JSON_OBJECT, &payload_details);
415  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "payload_details object couldn't be allocated in memory for Json string.");
416 
418  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&payload_details), code, "file_name couldn't be added to item meta of file!");
419 
420  // Add file creation date (optional field).
422  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&payload_details), code, "creation_date couldn't be added to item meta of file!");
423 
424  // Add file type (optional field).
426  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, json_util_destroy(&payload_details), code, "file_type couldn't be added to item meta of file!");
427 
428  }
430  {
431  MCL_DEBUG("Payload type is data source configuration.");
432  }
434  {
435  MCL_DEBUG("Payload type is event.");
436  }
437  else
438  {
439  MCL_DEBUG("Payload type is custom type.");
440 
442  {
443  // Add details.
444  code = json_util_duplicate(item_meta->payload.details.custom_details.details_json, MCL_TRUE, &payload_details);
445  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "details json object couldn't be added to item meta of custom data!");
446  }
447  }
448 
449  // If payload_details has at least one field, then we add it into payload. Otherwise it is destroyed without adding to the root.
450  if ((MCL_NULL != payload_details) && (MCL_TRUE == json_util_has_child(payload_details)))
451  {
452  code = json_util_add_object(payload, meta_field_names[META_FIELD_DETAILS].buffer, payload_details);
453 
454  if (MCL_OK == code)
455  {
456  // payload_details is added to payload parent. We can destroy payload_details struct w/o destroying root_handle in it.
457  json_util_finish_object(&payload_details);
458  }
459  else
460  {
461  json_util_destroy(&payload_details);
462  }
463  }
464  else
465  {
466  json_util_destroy(&payload_details);
467  }
468 
469  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
470  return MCL_OK;
471 }
472 
474 {
475  DEBUG_ENTRY("item_meta_t *item_meta = <%p>, json_t *root = <%p>", item_meta, root)
476 
477  E_MCL_ERROR_CODE code;
478  json_t *payload = MCL_NULL;
479 
480  code = json_util_start_object(root, meta_field_names[META_FIELD_PAYLOAD].buffer, &payload);
481  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "Payload object of item meta couldn't be allocated in memory for json_string.");
482 
483  // Add item meta payload type.
484  code = json_util_add_string(payload, meta_field_names[META_FIELD_PAYLOAD_TYPE].buffer, item_meta->payload.type->buffer);
485 
486  // Add item meta payload version.
487  if (MCL_OK == code)
488  {
490  }
491 
492  // Add item meta payload details.
493  if (MCL_OK == code)
494  {
495  code = _add_item_meta_payload_details(item_meta, payload);
496  }
497 
498  // Destroy payload struct w/o destroying root_handle in it.
499  json_util_finish_object(&payload);
500 
501  DEBUG_LEAVE("retVal = <%d>", code);
502  return code;
503 }
504 
506 {
507  DEBUG_ENTRY("list_t *data_source_list = <%p>, json_t *payload_object = <%p>", data_source_list, payload_object)
508 
509  E_MCL_ERROR_CODE code;
510  list_node_t *current_data_sources_node;
511  json_t *data_sources_array = MCL_NULL;
512 
513  code = json_util_start_array(payload_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES].buffer, &data_sources_array);
514  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "data_sources_array couldn't be allocated in memory for Json string.");
515 
516  list_reset(data_source_list);
517  while (MCL_NULL != (current_data_sources_node = list_next(data_source_list)))
518  {
519  // Create data_sources_object.
520  json_t *data_sources_object = MCL_NULL;
521  data_source_t *data_source;
522 
523  code = json_util_initialize(JSON_OBJECT, &data_sources_object);
524 
525  data_source = (data_source_t *)current_data_sources_node->data;
526 
527  // Add name to data source.
528  (MCL_OK == code) && (code = json_util_add_string(data_sources_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_NAME].buffer, data_source->name->buffer));
529 
530  // Add description to data source (optional field).
531  (MCL_OK == code) && (code = _add_string_field_to_object(data_sources_object, payload_field_names[PAYLOAD_FIELD_DESCRIPTION].buffer, data_source->description, MCL_FALSE));
532 
533  // Add data points to data source.
534  (MCL_OK == code) && (code = _add_data_source_configuration_data_points(data_source->data_points, data_sources_object));
535 
536  // Add custom data to data source.
537  if (MCL_NULL != data_source->custom_data)
538  {
539  json_t *duplicated_custom_data_json = MCL_NULL;
540 
541  (MCL_OK == code) && (code = json_util_duplicate(data_source->custom_data, MCL_TRUE, &duplicated_custom_data_json));
542  (MCL_OK == code) && (code = json_util_add_object(data_sources_object, payload_field_names[PAYLOAD_FIELD_CUSTOM_DATA].buffer, duplicated_custom_data_json));
543 
544  if (MCL_OK == code)
545  {
546  json_util_finish_object(&duplicated_custom_data_json);
547  }
548  else
549  {
550  json_util_destroy(&duplicated_custom_data_json);
551  }
552  }
553 
554  // Add data_sources_object to data_sources_array.
555  json_util_add_item_to_array(data_sources_array, data_sources_object);
556 
557  json_util_finish_object(&data_sources_object);
558 
559  if (MCL_OK != code)
560  {
561  break;
562  }
563  }
564 
565  json_util_finish_array(&data_sources_array);
566 
567  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
568  return MCL_OK;
569 }
570 
572 {
573  E_MCL_ERROR_CODE code;
574  json_t *data_points_array = MCL_NULL;
575  list_node_t *current_data_points_node;
576 
577  code = json_util_start_array(data_sources_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_DATA_POINTS].buffer, &data_points_array);
578  ASSERT_CODE_MESSAGE(MCL_OK == code, code, "data_points_array couldn't be allocated in memory for Json string.");
579 
580  list_reset(data_points);
581  while (MCL_NULL != (current_data_points_node = list_next(data_points)))
582  {
583  // Create data_points_object.
584  json_t *data_points_object = MCL_NULL;
585  data_point_t *data_point;
586 
587  code = json_util_initialize(JSON_OBJECT, &data_points_object);
588 
589  data_point = (data_point_t *)current_data_points_node->data;
590 
591  (MCL_OK == code) && (code = json_util_add_string(data_points_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_DATA_POINTS_ID].buffer, data_point->id->buffer));
592  (MCL_OK == code) && (code = json_util_add_string(data_points_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_DATA_POINTS_NAME].buffer, data_point->name->buffer));
593  (MCL_OK == code) && (code = _add_string_field_to_object(data_points_object, payload_field_names[PAYLOAD_FIELD_DESCRIPTION].buffer, data_point->description, MCL_FALSE));
594  (MCL_OK == code) && (code = json_util_add_string(data_points_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_DATA_POINTS_TYPE].buffer, data_point->type->buffer));
595  (MCL_OK == code) && (code = json_util_add_string(data_points_object, payload_field_names[PAYLOAD_FIELD_DATA_SOURCES_DATA_POINTS_UNIT].buffer, data_point->unit->buffer));
596 
597  // Add custom_data to data_point.
598  if (MCL_NULL != data_point->custom_data)
599  {
600  json_t *duplicated_custom_data_json = MCL_NULL;
601 
602  (MCL_OK == code) && (code = json_util_duplicate(data_point->custom_data, MCL_TRUE, &duplicated_custom_data_json));
603  (MCL_OK == code) && (code = json_util_add_object(data_points_object, payload_field_names[PAYLOAD_FIELD_CUSTOM_DATA].buffer, duplicated_custom_data_json));
604 
605  if (MCL_OK == code)
606  {
607  json_util_finish_object(&duplicated_custom_data_json);
608  }
609  else
610  {
611  json_util_destroy(&duplicated_custom_data_json);
612  }
613  }
614 
615  // Add data_points_object to data_points_array.
616  json_util_add_item_to_array(data_points_array, data_points_object);
617 
618  json_util_finish_object(&data_points_object);
619  }
620 
621  json_util_finish_array(&data_points_array);
622 
623  DEBUG_LEAVE("retVal = <%d>", code);
624  return code;
625 }
626 
627 static E_MCL_ERROR_CODE _add_string_field_to_object(json_t *parent_object, char *field_name, const string_t *field_to_be_added, mcl_bool_t is_mandatory)
628 {
629  DEBUG_ENTRY("json_t *parent_object = <%p>, char *field_name = <%s>, const string_t *field_to_be_added = <%p>, mcl_bool_t is_mandatory = <%u>", parent_object, field_name, field_to_be_added, is_mandatory)
630 
631  E_MCL_ERROR_CODE code = is_mandatory ? MCL_FAIL : MCL_OK;
632 
633  // Check whether string field is NULL or not and it is used or not before adding to object.
634  if ((MCL_NULL != field_to_be_added) && (MCL_NULL_CHAR != field_to_be_added->buffer[0]))
635  {
636  MCL_DEBUG("<%s> field will be added.", field_name);
637  code = json_util_add_string(parent_object, field_name, field_to_be_added->buffer);
638  }
639 
640  DEBUG_LEAVE("retVal = <%d>", code);
641  return code;
642 }
mcl_int32_t severity
Severity level.
Definition: data_types.h:139
Quality code of payload field values.
Definition: data_types.h:244
mcl_bool_t json_util_has_child(json_t *root)
This function checks whether root object has child object or not.
Definition: json_util.c:600
static E_MCL_ERROR_CODE _add_event_list(list_t *event_list, json_t *event_list_array)
Definition: json.c:154
string_t * routing
Information helping the server-side routing mechanism.
Definition: data_types.h:32
item_meta_payload_details_union_t details
Type and version specific meta information about the payload.
Definition: data_types.h:78
string_t * version
Version of payload.
Definition: data_types.h:77
Internal failure in MCL.
Definition: mcl_common.h:141
string_t * type
Type of payload.
Definition: data_types.h:76
string_t * data_point_id
Id of the datapoint the value is read from.
Definition: data_types.h:101
Memory module header file.
string_t * name
Name of the data point.
Definition: data_types.h:178
Version of meta field.
Definition: data_types.h:216
Type of payload field data sources data points.
Definition: data_types.h:271
E_MCL_ERROR_CODE json_util_add_uint(json_t *root, const char *object_name, const mcl_size_t number)
This function adds integer number to root which can be object or array.
Definition: json_util.c:285
void list_reset(list_t *list)
Reset the current node to head node.
Definition: list.c:329
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
mcl_json_t * details
Event/alarm details.
Definition: data_types.h:143
This struct is used for building the payload of time series which is list of value sets...
Definition: data_types.h:121
string_t * file_name
Name of the file transferred.
Definition: data_types.h:48
Json util module header file.
string_t * correlation_id
Parent event id.
Definition: data_types.h:135
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
Time series type of meta field payload.
Definition: data_types.h:286
File type of meta field payload.
Definition: data_types.h:288
list_t * data_sources
List of data sources definitions.
Definition: data_types.h:206
#define MCL_TRUE
Definition: mcl_common.h:54
Configuration id of meta field payload details.
Definition: data_types.h:222
char * buffer
Buffer of string handle.
Definition: string_type.h:45
string_t * type
Type of meta.
Definition: data_types.h:87
Business event type of meta field payload.
Definition: data_types.h:287
This struct is used for building data_source_configuration.data_points structure. ...
Definition: data_types.h:175
static E_MCL_ERROR_CODE _add_time_series_payload_values_objects(time_series_value_t *value, json_t *values_array)
Definition: json.c:329
string_t * version
Version of the event/alarm type.
Definition: data_types.h:142
E_MCL_ERROR_CODE json_util_start_array(json_t *root, const char *array_name, json_t **json_array)
This function creates an array in root.
Definition: json_util.c:100
string_t * description
Event description.
Definition: data_types.h:140
static E_MCL_ERROR_CODE _add_item_meta_payload_details(item_meta_t *item_meta, json_t *payload)
Definition: json.c:391
#define MCL_DEBUG(...)
Definition: log_util.h:70
#define MCL_FALSE
MCL bool type.
Definition: mcl_common.h:53
item_meta_payload_details_file_t file_details
File details.
Definition: data_types.h:67
static E_MCL_ERROR_CODE _add_data_source_configuration_data_sources(list_t *data_source_list, json_t *payload_object)
Definition: json.c:505
static E_MCL_ERROR_CODE _add_time_series_payload_values_array(list_t *values, json_t *value_set)
Definition: json.c:305
string_t * configuration_id
Unique identifier of the configuration.
Definition: data_types.h:40
Data point id of payload field values.
Definition: data_types.h:242
mcl_json_t * custom_data
Custom data.
Definition: data_types.h:195
Log utility module header file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
This struct is used for building item.meta structure.
Definition: data_types.h:84
Correlation id of payload field.
Definition: data_types.h:247
This struct is used for building value set of time series.
Definition: data_types.h:109
void json_util_destroy(json_t **root)
This function destroys root.
Definition: json_util.c:863
event_payload_t * payload
Payload of event.
Definition: event.h:27
Payload field data sources data points.
Definition: data_types.h:268
File type of meta field details.
Definition: data_types.h:226
string_t * type
Type of data point.
Definition: data_types.h:180
item_meta_payload_t payload
Information describing the payload part following this meta or a collection of tuples referencing it...
Definition: data_types.h:91
Json array.
Definition: json_util.h:34
Configuration id of payload field.
Definition: data_types.h:265
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
Definition: definitions.h:121
Event module interface header file.
Version of payload field.
Definition: data_types.h:254
static E_MCL_ERROR_CODE _add_data_source_configuration_data_points(list_t *data_points, json_t *data_sources_object)
Definition: json.c:571
Json module header file.
item_meta_payload_details_custom_t custom_details
Custom details.
Definition: data_types.h:68
list_t * value_sets
List of value sets.
Definition: data_types.h:124
Description of payload field.
Definition: data_types.h:251
This struct is used for building data_source_configuratio.payload structure.
Definition: data_types.h:201
string_t meta_field_names[META_FIELD_NAMES_END]
Definition: data_types.c:16
E_MCL_ERROR_CODE string_compare(const string_t *string, const string_t *other)
Compare the contents of two string_t&#39;s.
Definition: string_type.c:129
string_t * timestamp
Time of values in YYYY-MM-DDThh:mm:ss.sssZ format.
Definition: data_types.h:111
Type of meta field payload.
Definition: data_types.h:219
File name of meta field details.
Definition: data_types.h:225
Type of meta field.
Definition: data_types.h:215
void json_util_finish_array(json_t **json_array)
This function destroys json_array data struct. But the array still exists in root json object...
Definition: json_util.c:827
static E_MCL_ERROR_CODE _add_time_series_value_sets(list_t *value_sets, json_t *value_set_array)
Definition: json.c:263
Value of payload field values.
Definition: data_types.h:243
item_meta_payload_details_time_series_t time_series_details
Time series details.
Definition: data_types.h:66
Version of meta field payload.
Definition: data_types.h:220
#define ASSERT_STATEMENT_CODE(condition, statement, return_code)
Definition: definitions.h:112
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: definitions.h:105
Definitions module header file.
string_t * version
Version of meta.
Definition: data_types.h:88
Timestamp of payload field.
Definition: data_types.h:240
E_MCL_ERROR_CODE string_initialize_dynamic(const char *value, mcl_size_t value_length, string_t **string)
Initializes a dynamic string_t object with the given value and length.
Definition: string_type.c:68
E_MCL_ERROR_CODE json_from_item_meta(item_meta_t *item_meta, string_t **json_string)
Creates item meta part of all types in json format.
Definition: json.c:37
string_t * timestamp
Creation time of the event in ISO format.
Definition: data_types.h:138
static E_MCL_ERROR_CODE _add_item_meta_payload(item_meta_t *item_meta, json_t *root)
Definition: json.c:473
E_MCL_ERROR_CODE json_util_add_string(json_t *root, const char *object_name, const char *object_value)
This function adds string to root which can be object or array.
Definition: json_util.c:237
E_MCL_ERROR_CODE json_util_add_object(json_t *root, const char *object_name, json_t *object)
This function adds object to root.
Definition: json_util.c:504
Severity of payload field.
Definition: data_types.h:250
list_node_t * list_next(list_t *list)
Get the next node from the list.
Definition: list.c:309
string_t * description
Description of the data source.
Definition: data_types.h:191
mcl_json_t * custom_data
Custom data.
Definition: data_types.h:182
Routing of meta field details.
Definition: data_types.h:224
static E_MCL_ERROR_CODE _add_event(event_t *event, json_t *event_list_array)
Definition: json.c:170
string_t meta_field_values[META_FIELD_VALUES_END]
Definition: data_types.c:36
void json_util_finish_object(json_t **json_object)
This function destroys json_object data struct. But the object still exists in root json object...
Definition: json_util.c:845
static E_MCL_ERROR_CODE _add_time_series_value_set(time_series_value_set_t *value_set, json_t *value_set_array)
Definition: json.c:280
E_MCL_ERROR_CODE json_from_time_series_payload(time_series_payload_t *payload, string_t **json_string)
Creates payload part of time series in json format.
Definition: json.c:76
E_MCL_ERROR_CODE json_util_duplicate(const json_t *source_json, mcl_bool_t with_children, json_t **duplicated_json)
This function duplicates source_json as duplicated_json.
Definition: json_util.c:805
This struct is used for building data_source_configuration.data_source structure. ...
Definition: data_types.h:188
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
string_t * description
Description of the data point.
Definition: data_types.h:179
Success.
Definition: mcl_common.h:140
Event module header file.
string_t payload_field_names[PAYLOAD_FIELD_NAMES_END]
Definition: data_types.c:47
string_t * quality_code
The quality of the value provided.
Definition: data_types.h:103
Name of payload field data sources data points.
Definition: data_types.h:270
Details of payload field.
Definition: data_types.h:256
string_t * creation_date
Date and time when the file was created. ISO 8601 date and time format.
Definition: data_types.h:49
This struct is used for building time_series.payload.values structure.
Definition: data_types.h:99
mcl_json_t * details_json
Any details.
Definition: data_types.h:58
Payload field id.
Definition: data_types.h:245
E_MCL_ERROR_CODE json_util_to_string(json_t *root, char **json_string)
This function gives the string of root in json format.
Definition: json_util.c:737
static E_MCL_ERROR_CODE _add_string_field_to_object(json_t *parent_object, char *field_name, const string_t *optional_field_to_be_added, mcl_bool_t is_mandatory)
Definition: json.c:627
string_t * unit
Measurement unit of the data point.
Definition: data_types.h:181
string_t * file_type
Type of the file transferred.
Definition: data_types.h:50
Creation date of meta field details.
Definition: data_types.h:227
string_t * name
Name of the data source.
Definition: data_types.h:190
list_t * data_points
List of data points definitions.
Definition: data_types.h:194
Type of payload field.
Definition: data_types.h:253
Json object.
Definition: json_util.h:35
string_t * value
The value read.
Definition: data_types.h:102
Name of payload field data sources.
Definition: data_types.h:267
E_MCL_ERROR_CODE json_from_data_source_configuration_payload(data_source_configuration_payload_t *payload, string_t **json_string)
Creates payload part of data source configuration in json format.
Definition: json.c:104
This struct is used for building a single event.
Definition: event.h:24
Unit of payload field data sources data points.
Definition: data_types.h:272
#define MCL_NULL
Definition: definitions.h:24
string_t * configuration_id
Unique identifier of the configuration.
Definition: data_types.h:203
void json_util_add_item_to_array(json_t *root, json_t *object)
This function adds object to root array.
Definition: json_util.c:540
Data point id of payload field assets data sources data points.
Definition: data_types.h:269
Data source configuration type of meta field payload.
Definition: data_types.h:289
void * data
Data of the node.
Definition: mcl_list.h:32
E_MCL_ERROR_CODE json_from_event_payload(list_t *event_list_payload, string_t **json_string)
Creates payload part of event set in json format.
Definition: json.c:129
Custom data of payload field.
Definition: data_types.h:255
E_MCL_ERROR_CODE json_util_start_object(json_t *root, const char *object_name, json_t **json_object)
This function creates an object in root.
Definition: json_util.c:198
E_MCL_ERROR_CODE json_util_initialize(E_JSON_TYPE json_type, json_t **root)
This function initializes the given root json.
Definition: json_util.c:53
Payload field data sources.
Definition: data_types.h:266
string_t * id
Unique identifier of the event.
Definition: data_types.h:134
string_t * type
Type of the event.
Definition: data_types.h:141
Meta field details.
Definition: data_types.h:223
list_t * values
List of measurements made at the given timestamp.
Definition: data_types.h:114
Payload field values.
Definition: data_types.h:241
item_meta_details_t details
Type and version-specific meta-information.
Definition: data_types.h:90
This struct is used for json handling.
Definition: json_util.h:27
Meta field payload.
Definition: data_types.h:218
#define MCL_NULL_CHAR
Definition: definitions.h:26
static E_MCL_ERROR_CODE _add_item_meta_details(item_meta_t *item_meta, json_t *root)
Definition: json.c:354
string_t * id
Agent-unique identifier of the data point.
Definition: data_types.h:177