core.c
Go to the documentation of this file.
1 
9 #include "mcl_core/mcl_core.h"
10 #include "core.h"
11 #include "definitions.h"
12 #include "mcl_core/mcl_memory.h"
13 #include "string_util.h"
14 
15 #define USER_AGENT_HEADER_FORMAT "MCL/" MCL_VERSION_STRING " (%s)"
16 
18 {
19  mcl_error_t code;
20 
21  MCL_DEBUG_ENTRY("mcl_core_configuration_t *configuration = <%p>, mcl_core_t **core = <%p>", configuration, core);
22 
23  // Null check of configuration.
24  MCL_ASSERT_NOT_NULL(configuration, code);
25 
26  // Null check for input argument **core.
27  MCL_ASSERT_NOT_NULL(core, code);
28 
29  // Validate core configuration.
30  code = core_configuration_validate(configuration);
31 
32  if (MCL_OK == code)
33  {
34  // Allocate memory for mcl handle.
35  MCL_NEW(*core);
36  MCL_ASSERT_CODE_MESSAGE(MCL_NULL != *core, MCL_OUT_OF_MEMORY, "Memory can not be allocated for core object.");
37 
38  // Set core configuration.
39  (*core)->configuration = configuration;
40 
41  // Initialize core processor.
42  code = core_processor_initialize((*core)->configuration, &((*core)->core_processor));
43  MCL_ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, mcl_core_destroy(core), code, "Core processor initialization failed.");
44 
45  MCL_INFO("Core structure is initialized.");
46  core_configuration_log(configuration);
47  }
48 
50  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_OK);
51  return code;
52 }
53 
55 {
56  mcl_error_t result;
57  mcl_bool_t critical_section_callbacks_are_used;
58 
59  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
60 
61  // Null check.
62  MCL_ASSERT_NOT_NULL(core, result);
63 
64  critical_section_callbacks_are_used = (MCL_NULL != core->configuration->critical_section_enter_callback) &&
65  (MCL_NULL != core->configuration->critical_section_leave_callback);
66  if (MCL_FALSE != critical_section_callbacks_are_used)
67  {
68  result = core->configuration->critical_section_enter_callback();
69  MCL_ASSERT_OK(result);
70  }
71 
72  if (MCL_FALSE == mcl_core_is_onboarded(core))
73  {
74  // Start the onboarding procedure.
75  result = core_processor_register(core->core_processor);
76  if (MCL_OK == result)
77  {
78  MCL_INFO("Agent is successfully onboarded.");
79  }
80  else
81  {
82  MCL_ERROR("Onboarding of agent failed.");
83  }
84  }
85  else
86  {
87  MCL_INFO("Agent is already onboarded.");
88  result = MCL_ALREADY_ONBOARDED;
89  }
90 
91  if (MCL_FALSE != critical_section_callbacks_are_used)
92  {
93  core->configuration->critical_section_leave_callback();
94  }
95 
97  MCL_DEBUG_LEAVE("retVal = <%d>", result);
98  return result;
99 }
100 
102 {
103  mcl_error_t result;
104  mcl_bool_t critical_section_callbacks_are_used;
105 
106  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
107 
108  // Null check.
109  MCL_ASSERT_NOT_NULL(core, result);
110 
111  critical_section_callbacks_are_used = (MCL_NULL != core->configuration->critical_section_enter_callback) &&
112  (MCL_NULL != core->configuration->critical_section_leave_callback);
113  if (MCL_FALSE != critical_section_callbacks_are_used)
114  {
115  result = core->configuration->critical_section_enter_callback();
116  MCL_ASSERT_OK(result);
117  }
118 
119  // Check if the agent is onboarded or not.
120  if (MCL_FALSE != mcl_core_is_onboarded(core))
121  {
122  // Perform key rotation.
123  MCL_INFO("Key rotation started.");
124  result = core_processor_register(core->core_processor);
125  if (MCL_OK == result)
126  {
127  MCL_INFO("Key successfully rotated.");
128  }
129  else
130  {
131  MCL_ERROR("Key rotation failed.");
132  }
133  }
134  else
135  {
136  result = MCL_NOT_ONBOARDED;
137  }
138 
139  if (MCL_FALSE != critical_section_callbacks_are_used)
140  {
141  core->configuration->critical_section_leave_callback();
142  }
143 
145  MCL_DEBUG_LEAVE("retVal = <%d>", result);
146  return result;
147 }
148 
150 {
151  mcl_error_t result;
152  mcl_bool_t critical_section_callbacks_are_used;
153 
154  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
155 
156  // Null check.
157  MCL_ASSERT_NOT_NULL(core, result);
158 
159  critical_section_callbacks_are_used = (MCL_NULL != core->configuration->critical_section_enter_callback) &&
160  (MCL_NULL != core->configuration->critical_section_leave_callback);
161  if (MCL_TRUE == critical_section_callbacks_are_used)
162  {
163  result = core->configuration->critical_section_enter_callback();
164  MCL_ASSERT_OK(result);
165  }
166 
167  // Check if the agent is onboarded.
168  if (MCL_TRUE == mcl_core_is_onboarded(core))
169  {
170  // Update credentials.
171  result = core_processor_update_credentials(core->core_processor);
172  }
173  else
174  {
175  result = MCL_NOT_ONBOARDED;
176  }
177 
178  if (MCL_TRUE == critical_section_callbacks_are_used)
179  {
180  core->configuration->critical_section_leave_callback();
181  }
182 
184  MCL_DEBUG_LEAVE("retVal = <%d>", result);
185  return result;
186 }
187 
189 {
190  mcl_error_t result;
191 
192  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
193 
194  // Null check.
195  MCL_ASSERT_NOT_NULL(core, result);
196 
197  if (MCL_TRUE == mcl_core_is_onboarded(core))
198  {
199  result = core_processor_get_access_token(core->core_processor);
200 
201  if (MCL_OK == result)
202  {
203  MCL_INFO("Access token received.");
204  }
205  else
206  {
207  MCL_ERROR("Failed to get access token.");
208  }
209  }
210  else
211  {
212  result = MCL_NOT_ONBOARDED;
213  }
214 
216  MCL_DEBUG_LEAVE("retVal = <%d>", result);
217  return result;
218 }
219 
221 {
222  mcl_bool_t is_onboarded = MCL_FALSE;
223 
224  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
225 
226  if (MCL_NULL != core)
227  {
228  is_onboarded = (MCL_NULL == core->core_processor->security_handler->registration_access_token) ? MCL_FALSE : MCL_TRUE;
229  }
230 
231  MCL_DEBUG_LEAVE("retVal = <%d>", is_onboarded);
232  return is_onboarded;
233 }
234 
236 {
237  mcl_error_t code = MCL_OK;
238 
239  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>, char **token = <%p>", core, token);
240 
241  // Null check.
242  MCL_ASSERT_NOT_NULL(core, code);
243  MCL_ASSERT_NOT_NULL(token, code);
244 
245  if (MCL_TRUE != mcl_core_is_onboarded(core))
246  {
247  code = MCL_NOT_ONBOARDED;
248  }
249  else if(MCL_NULL == core->core_processor->security_handler->access_token)
250  {
252  }
253  else
254  {
255  mcl_size_t token_length;
256  token_length = string_util_strlen(core->core_processor->security_handler->access_token) + 1;
257  *token = MCL_CALLOC(token_length, 1);
258 
259  if (MCL_NULL == *token)
260  {
261  code = MCL_OUT_OF_MEMORY;
262  }
263  else
264  {
265  string_util_memcpy(*token, core->core_processor->security_handler->access_token, token_length);
266  }
267  }
268 
270  MCL_DEBUG_LEAVE("retVal = <%d>", code);
271  return code;
272 }
273 
275 {
276  mcl_error_t code = MCL_OK;
277 
278  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>, char **token_time = <%p>", core, token_time);
279 
280  // Null check.
281  MCL_ASSERT_NOT_NULL(core, code);
282  MCL_ASSERT_NOT_NULL(token_time, code);
283  *token_time = MCL_NULL;
284 
285  if (MCL_NULL == core->core_processor->security_handler->last_token_time)
286  {
287  code = MCL_NO_SERVER_TIME;
288  }
289  else
290  {
291  mcl_size_t token_time_length;
292  token_time_length = string_util_strlen(core->core_processor->security_handler->last_token_time) + 1;
293  *token_time = MCL_CALLOC(token_time_length, 1);
294 
295  if (MCL_NULL == *token_time)
296  {
297  code = MCL_OUT_OF_MEMORY;
298  }
299  else
300  {
301  string_util_memcpy(*token_time, core->core_processor->security_handler->last_token_time, token_time_length);
302  }
303  }
304 
306  MCL_DEBUG_LEAVE("retVal = <%d>", code);
307  return code;
308 }
309 
311 {
312  mcl_http_client_t *http_client = MCL_NULL;
313 
314  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
315 
316  // Validate core before getting http client.
317  if (MCL_NULL != core)
318  {
319  http_client = core->core_processor->http_client;
320  }
321 
322  MCL_DEBUG_LEAVE("retVal = <%p>", http_client);
323  return http_client;
324 }
325 
327 {
328  char *host_name = MCL_NULL;
329 
330  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
331 
332  // Validate core before getting host name.
333  if (MCL_NULL != core)
334  {
335  host_name = core->configuration->mindsphere_hostname;
336  }
337 
338  MCL_DEBUG_LEAVE("retVal = <%p>", host_name);
339  return host_name;
340 }
341 
343 {
344  const char *client_id = MCL_NULL;
345 
346  MCL_DEBUG_ENTRY("mcl_core_t *core = <%p>", core);
347 
348  // Validate core before getting client id.
349  if ((MCL_NULL != core) && (MCL_TRUE == mcl_core_is_onboarded(core)))
350  {
351  client_id = (const char *)core->core_processor->security_handler->client_id;
352  }
353 
354  MCL_DEBUG_LEAVE("retVal = <%p>", client_id);
355  return client_id;
356 }
357 
359 {
360  MCL_DEBUG_ENTRY("mcl_core_t **core = <%p>", core);
361 
362  // Free data structure.
363  if ((MCL_NULL != core) && (MCL_NULL != *core))
364  {
365  // Destroy core processor.
366  core_processor_destroy(&((*core)->core_processor));
367 
368  // Free MCL handle.
369  MCL_FREE(*core);
370 
371  MCL_DEBUG("Core handle is destroyed.");
372  }
373 
374  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_OK);
375  return MCL_OK;
376 }
#define MCL_FUNCTION_LEAVE_LABEL
mcl_error_t mcl_core_initialize(mcl_core_configuration_t *configuration, mcl_core_t **core)
Definition: core.c:17
size_t mcl_size_t
void core_configuration_log(core_configuration_t *configuration)
Agent is already onboarded to the server, hence the library did not try to onboard again...
Success.
#define MCL_DEBUG(...)
Definition: mcl_log_util.h:114
#define MCL_CALLOC(count, bytes)
Definition: mcl_memory.h:56
mcl_int32_t mcl_error_t
Core module header file.
#define MCL_DEBUG_ENTRY(...)
Definition: mcl_log_util.h:115
#define MCL_FALSE
mcl_error_t mcl_core_get_last_token_time(mcl_core_t *core, char **token_time)
Definition: core.c:274
String utility module header file.
mcl_error_t core_configuration_validate(core_configuration_t *configuration)
#define MCL_ASSERT_CODE_MESSAGE(condition, return_code,...)
Definition: mcl_assert.h:77
mcl_error_t mcl_core_get_last_access_token(mcl_core_t *core, char **token)
Definition: core.c:235
No access token exists in mcl_core_t handle.
#define MCL_NEW(p)
Definition: mcl_memory.h:55
#define MCL_NULL
#define MCL_ERROR(...)
Definition: mcl_log_util.h:142
mcl_http_client_t * mcl_core_get_http_client(mcl_core_t *core)
Definition: core.c:310
struct mcl_core_configuration_t mcl_core_configuration_t
struct mcl_core_t mcl_core_t
Definition: mcl_core.h:33
mcl_error_t mcl_core_rotate_key(mcl_core_t *core)
Definition: core.c:101
#define MCL_FREE(p)
Definition: mcl_memory.h:59
void string_util_memcpy(void *destination, const void *source, mcl_size_t count)
Definition: string_util.c:229
Agent is not onboarded to the server yet and does not possess an authentication key.
Definitions module header file.
#define MCL_ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
Definition: mcl_assert.h:93
#define MCL_ASSERT_NOT_NULL(argument, return_variable)
Definition: mcl_assert.h:38
const char * mcl_core_get_client_id(mcl_core_t *core)
Definition: core.c:342
const char * mcl_core_get_host_name(mcl_core_t *core)
Definition: core.c:326
mcl_bool_t mcl_core_is_onboarded(mcl_core_t *core)
Definition: core.c:220
Server time is not received from the server.
mcl_error_t core_processor_initialize(core_configuration_t *configuration, core_processor_t **core_processor)
void core_processor_destroy(core_processor_t **core_processor)
mcl_uint8_t mcl_bool_t
mcl_error_t mcl_core_onboard(mcl_core_t *core)
Definition: core.c:54
mcl_error_t mcl_core_get_access_token(mcl_core_t *core)
Definition: core.c:188
mcl_error_t core_processor_get_access_token(core_processor_t *core_processor)
Memory allocation fail.
Core module interface header file.
#define MCL_ASSERT_OK(code)
Definition: mcl_assert.h:31
mcl_error_t mcl_core_update_credentials(mcl_core_t *core)
Definition: core.c:149
mcl_error_t core_processor_update_credentials(core_processor_t *core_processor)
mcl_error_t mcl_core_destroy(mcl_core_t **core)
Definition: core.c:358
#define MCL_DEBUG_LEAVE(...)
Definition: mcl_log_util.h:116
#define MCL_TRUE
mcl_size_t string_util_strlen(const char *buffer)
Definition: string_util.c:35
#define MCL_INFO(...)
Definition: mcl_log_util.h:126
mcl_error_t core_processor_register(core_processor_t *core_processor)
Memory module interface header file.