data_source_configuration.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2017 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file data_source_configuration.c
9 * @date Nov 13, 2017
10 * @brief Data source configuration module implementation file.
11 *
12 ************************************************************************/
13 
16 #include "log_util.h"
17 #include "memory.h"
18 #include "random.h"
19 #include "json_util.h"
20 #include "definitions.h"
21 
22 // Private Function Prototypes:
23 static E_MCL_ERROR_CODE _initialize_meta(const char *version, data_source_configuration_t *data_source_configuration);
24 static E_MCL_ERROR_CODE _initialize_payload(data_source_configuration_t *data_source_configuration);
25 static void _destroy_data_source(data_source_t **data_source);
26 static void _destroy_data_point(data_point_t **data_point);
27 
29 {
30  DEBUG_ENTRY("const char *version = <%p>, data_source_configuration_t **data_source_configuration = <%p>", version, data_source_configuration)
31 
32  E_MCL_ERROR_CODE code;
33 
34  // New data_source_configuration:
35  MCL_NEW(*data_source_configuration);
36  ASSERT_CODE_MESSAGE(MCL_NULL != *data_source_configuration, MCL_OUT_OF_MEMORY, "Not enough memory left to create the data_source_configuration handle!");
37 
38  (*data_source_configuration)->meta.content_id = MCL_NULL;
39  (*data_source_configuration)->meta.type = MCL_NULL;
40  (*data_source_configuration)->meta.version = MCL_NULL;
41  (*data_source_configuration)->meta.details.routing = MCL_NULL;
42  (*data_source_configuration)->meta.payload.type = MCL_NULL;
43  (*data_source_configuration)->meta.payload.version = MCL_NULL;
44  (*data_source_configuration)->payload.configuration_id = MCL_NULL;
45  (*data_source_configuration)->payload.data_sources = MCL_NULL;
46 
47  code = _initialize_meta(version, *data_source_configuration);
48 
49  (MCL_OK == code) && (code = _initialize_payload(*data_source_configuration));
50 
51  if (MCL_OK != code)
52  {
53  data_source_configuration_destroy(data_source_configuration);
54  }
55 
56  DEBUG_LEAVE("retVal = <%d>", code);
57  return code;
58 }
59 
60 E_MCL_ERROR_CODE mcl_data_source_configuration_add_data_source(mcl_data_source_configuration_t *data_source_configuration, const char *name, const char *description,
61  mcl_json_t *custom_data, mcl_data_source_t **data_source)
62 {
63  DEBUG_ENTRY("mcl_data_source_configuration_t *data_source_configuration = <%p>, const char *name = <%p>, const char *description = <%p>, mcl_json_t *custom_data = <%p>, mcl_data_source_t **data_source = <%p>",
64  data_source_configuration, name, description, custom_data, data_source)
65 
66  E_MCL_ERROR_CODE code;
67  mcl_bool_t ok;
68 
69  // Description and custom_data parameters are optional fields. Thats why there are no null checks for them.
70  ASSERT_NOT_NULL(data_source_configuration);
71  ASSERT_NOT_NULL(name);
72  ASSERT_NOT_NULL(data_source);
73 
74  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(*data_source), MCL_OUT_OF_MEMORY, "Not enough memory to allocate new data_source.");
75 
76  (*data_source)->name = MCL_NULL;
77  (*data_source)->description = MCL_NULL;
78  (*data_source)->data_points = MCL_NULL;
79  (*data_source)->custom_data = MCL_NULL;
80 
81  // Initialize name.
82  ok = MCL_OK == (code = string_initialize_new(name, 0, &(*data_source)->name));
83 
84  // Initialize description.
85  if (MCL_NULL != description)
86  {
87  ok = ok && (MCL_OK == (code = string_initialize_new(description, 0, &(*data_source)->description)));
88  }
89 
90  // Initialize custom_data.
91  if (MCL_NULL != custom_data)
92  {
93  ok = ok && (MCL_OK == (code = json_util_duplicate(custom_data, MCL_TRUE, &(*data_source)->custom_data)));
94  }
95 
96  // Initialize data_points.
97  ok = ok && (MCL_OK == (code = list_initialize(&(*data_source)->data_points)));
98 
99  // Add data_source to the list.
100  ok = ok && (MCL_OK == (code = list_add(data_source_configuration->payload.data_sources, *data_source)));
101 
102  // error check:
103  if (MCL_FALSE == ok)
104  {
105  MCL_DEBUG("String or list initialize operation(s) has been failed! Clearing strings.");
106  _destroy_data_source(data_source);
107  }
108 
109  DEBUG_LEAVE("retVal = <%d>", code);
110  return code;
111 }
112 
113 E_MCL_ERROR_CODE mcl_data_source_configuration_add_data_point(mcl_data_source_t *data_source, const char *id, const char *name, const char *description, const char *type,
114  const char *unit, mcl_json_t *custom_data)
115 {
116  DEBUG_ENTRY("mcl_data_source_t *data_source = <%p>, const char *id = <%p>, const char *name = <%p>, const char *description = <%p>, const char *type = <%p>, const char *unit = <%p>, mcl_json_t *custom_data = <%p>",
117  data_source, id, name, description, type, unit, custom_data)
118 
119  E_MCL_ERROR_CODE code;
120  data_point_t *data_point = MCL_NULL;
121  mcl_bool_t ok;
122 
123  // Description and custom_data parameters are optional fields. Thats why there are no null checks for them.
124  ASSERT_NOT_NULL(data_source);
125  ASSERT_NOT_NULL(id);
126  ASSERT_NOT_NULL(name);
127  ASSERT_NOT_NULL(type);
128  ASSERT_NOT_NULL(unit);
129 
130  ASSERT_CODE_MESSAGE(MCL_NULL != MCL_NEW(data_point), MCL_OUT_OF_MEMORY, "Not enough memory to allocate new data_point.");
131 
132  data_point->id = MCL_NULL;
133  data_point->name = MCL_NULL;
134  data_point->description = MCL_NULL;
135  data_point->type = MCL_NULL;
136  data_point->unit = MCL_NULL;
137  data_point->custom_data = MCL_NULL;
138 
139  // Initialize id.
140  ok = MCL_OK == (code = string_initialize_new(id, 0, &data_point->id));
141 
142  // Initialize name.
143  ok = ok && (MCL_OK == (code = string_initialize_new(name, 0, &data_point->name)));
144 
145  // Initialize description.
146  if (MCL_NULL != description)
147  {
148  ok = ok && (MCL_OK == (code = string_initialize_new(description, 0, &data_point->description)));
149  }
150 
151  // Initialize type.
152  ok = ok && (MCL_OK == (code = string_initialize_new(type, 0, &data_point->type)));
153 
154  // Initialize unit.
155  ok = ok && (MCL_OK == (code = string_initialize_new(unit, 0, &data_point->unit)));
156 
157  // Initialize custom_data.
158  if (MCL_NULL != custom_data)
159  {
160  ok = ok && (MCL_OK == (code = json_util_duplicate(custom_data, MCL_TRUE, &data_point->custom_data)));
161  }
162 
163  // Add data_point to the list.
164  ok = ok && (MCL_OK == (code = list_add(data_source->data_points, data_point)));
165 
166  // error check:
167  if (MCL_FALSE == ok)
168  {
169  MCL_DEBUG("String or list initialize operation(s) has been failed! Clearing strings.");
170  _destroy_data_point(&data_point);
171  }
172 
173  DEBUG_LEAVE("retVal = <%d>", code);
174  return code;
175 }
176 
178 {
179  DEBUG_ENTRY("mcl_data_source_configuration_t *data_source_configuration = <%p>, char **id = <%p>", data_source_configuration, id)
180 
181  E_MCL_ERROR_CODE code;
182  string_t *configuration_id_local = MCL_NULL;
183 
184  ASSERT_NOT_NULL(data_source_configuration);
185  ASSERT_NOT_NULL(id);
186 
187  code = string_initialize(data_source_configuration->payload.configuration_id, &configuration_id_local);
188  ASSERT_CODE_MESSAGE(MCL_OK == code, MCL_OUT_OF_MEMORY, "Not enough memory left to create configuration_id!");
189 
190  *id = configuration_id_local->buffer;
191 
192  // Free string and remain buffer
193  MCL_FREE(configuration_id_local);
194 
195  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
196  return MCL_OK;
197 }
198 
200 {
201  DEBUG_ENTRY("data_source_configuration_t **data_source_configuration = <%p>", data_source_configuration)
202 
203  if (MCL_NULL != *data_source_configuration)
204  {
205  string_destroy(&((*data_source_configuration)->meta.content_id));
206  string_destroy(&((*data_source_configuration)->meta.type));
207  string_destroy(&((*data_source_configuration)->meta.version));
208  string_destroy(&((*data_source_configuration)->meta.payload.type));
209  string_destroy(&((*data_source_configuration)->meta.payload.version));
210  string_destroy(&((*data_source_configuration)->payload.configuration_id));
211  list_destroy_with_content(&(*data_source_configuration)->payload.data_sources, (list_item_destroy_callback)_destroy_data_source);
212  MCL_FREE(*data_source_configuration);
213  }
214 
215  DEBUG_LEAVE("retVal = void");
216 }
217 
218 static E_MCL_ERROR_CODE _initialize_meta(const char *version, data_source_configuration_t *data_source_configuration)
219 {
220  DEBUG_ENTRY("const char *version = <%p>, data_source_configuration_t *data_source_configuration = <%p>", version, data_source_configuration)
221 
222  // Set meta.type.
224 
225  // Set meta.version.
227 
228  // Set meta.payload.type.
230  &data_source_configuration->meta.payload.type));
231 
232  // Set meta.payload.version.
233  (MCL_OK == code) && (code = string_initialize_new(version, 0, &data_source_configuration->meta.payload.version));
234 
235  DEBUG_LEAVE("retVal = <%d>", code);
236  return code;
237 }
238 
240 {
241  DEBUG_ENTRY("data_source_configuration_t *data_source_configuration = <%p>", data_source_configuration)
242 
243  // Set configuration_id.
244  E_MCL_ERROR_CODE code = random_generate_guid(&data_source_configuration->payload.configuration_id);
245 
246  // Initialize data sources.
247  (MCL_OK == code) && (code = list_initialize(&data_source_configuration->payload.data_sources));
248 
249  DEBUG_LEAVE("retVal = <%d>", code);
250  return code;
251 }
252 
253 static void _destroy_data_source(data_source_t **data_source)
254 {
255  DEBUG_ENTRY("data_source_t **data_source = <%p>", data_source)
256 
257  string_destroy(&(*data_source)->name);
258  string_destroy(&(*data_source)->description);
260  json_util_destroy(&(*data_source)->custom_data);
261  MCL_FREE((*data_source));
262 
263  DEBUG_LEAVE("retVal = void");
264 }
265 
266 static void _destroy_data_point(data_point_t **data_point)
267 {
268  DEBUG_ENTRY("data_point_t **data_point = <%p>", data_point)
269 
270  string_destroy(&(*data_point)->id);
271  string_destroy(&(*data_point)->name);
272  string_destroy(&(*data_point)->description);
273  string_destroy(&(*data_point)->type);
274  string_destroy(&(*data_point)->unit);
275  json_util_destroy(&(*data_point)->custom_data);
276  MCL_FREE(*data_point);
277 
278  DEBUG_LEAVE("retVal = void");
279 }
struct mcl_json_t mcl_json_t
This struct is used for json handling.
void string_destroy(string_t **string)
Destroys the allocated resources of the string.
Definition: string_type.c:326
Data source configuration module header file.
void data_source_configuration_destroy(data_source_configuration_t **data_source_configuration)
string_t * version
Version of payload.
Definition: data_types.h:77
string_t * type
Type of payload.
Definition: data_types.h:76
Memory module header file.
string_t * name
Name of the data point.
Definition: data_types.h:178
struct mcl_data_source_configuration_t mcl_data_source_configuration_t
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
Json util module header file.
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_NEW(p)
Definition: memory.h:121
list_t * data_sources
List of data sources definitions.
Definition: data_types.h:206
E_MCL_ERROR_CODE string_initialize_new(const char *value, mcl_size_t value_length, string_t **string)
Initializes a new string_t object with the given value and length.
Definition: string_type.c:46
#define MCL_TRUE
Definition: mcl_common.h:54
char * buffer
Buffer of string handle.
Definition: string_type.h:45
string_t * type
Type of meta.
Definition: data_types.h:87
This struct is used for building data_source_configuration.data_points structure. ...
Definition: data_types.h:175
#define MCL_DEBUG(...)
Definition: log_util.h:70
#define ASSERT_NOT_NULL(argument)
Definition: definitions.h:129
#define MCL_FALSE
MCL bool type.
Definition: mcl_common.h:53
Log utility module header file.
Data source configuration module interface header file.
E_MCL_ERROR_CODE list_initialize(list_t **list)
Initializes the list.
Definition: list.c:139
Current version of meta field.
Definition: data_types.h:284
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
void json_util_destroy(json_t **root)
This function destroys root.
Definition: json_util.c:863
E_MCL_ERROR_CODE mcl_data_source_configuration_add_data_point(mcl_data_source_t *data_source, const char *id, const char *name, const char *description, const char *type, const char *unit, mcl_json_t *custom_data)
#define MCL_FREE(p)
Definition: memory.h:125
data_source_configuration_payload_t payload
Payload of data_source_configuration.
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
static void _destroy_data_point(data_point_t **data_point)
E_MCL_ERROR_CODE mcl_data_source_configuration_get_id(mcl_data_source_configuration_t *data_source_configuration, char **id)
E_MCL_ERROR_CODE random_generate_guid(string_t **guid)
Generates random guid.
Definition: random.c:86
#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
static void _destroy_data_source(data_source_t **data_source)
static E_MCL_ERROR_CODE _initialize_payload(data_source_configuration_t *data_source_configuration)
E_MCL_ERROR_CODE string_initialize(const string_t *other, string_t **string)
Initializes an string_t object with another one.
Definition: string_type.c:24
Item type of meta field.
Definition: data_types.h:283
item_meta_t meta
Meta of data source configuration.
mcl_json_t * custom_data
Custom data.
Definition: data_types.h:182
string_t meta_field_values[META_FIELD_VALUES_END]
Definition: data_types.c:36
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
void list_destroy_with_content(list_t **list, list_item_destroy_callback callback)
To destroy the list and its items with a given callback function.
Definition: list.c:373
E_MCL_ERROR_CODE string_initialize_static(const char *value, mcl_size_t value_length, string_t **string)
Initializes a static string_t object with the given value and length.
Definition: string_type.c:90
E_MCL_ERROR_CODE data_source_configuration_initialize(const char *version, data_source_configuration_t **data_source_configuration)
string_t * unit
Measurement unit of the data point.
Definition: data_types.h:181
Random module header file.
E_MCL_ERROR_CODE list_add(list_t *list, void *data)
Adds a new list item.
Definition: list.c:159
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
string_t * configuration_id
Unique identifier of the configuration.
Definition: data_types.h:203
mcl_list_item_destroy_callback list_item_destroy_callback
Definition: list.h:24
Data source configuration type of meta field payload.
Definition: data_types.h:289
static E_MCL_ERROR_CODE _initialize_meta(const char *version, data_source_configuration_t *data_source_configuration)
struct mcl_data_source_t mcl_data_source_t
E_MCL_ERROR_CODE mcl_data_source_configuration_add_data_source(mcl_data_source_configuration_t *data_source_configuration, const char *name, const char *description, mcl_json_t *custom_data, mcl_data_source_t **data_source)
string_t * id
Agent-unique identifier of the data point.
Definition: data_types.h:177