Skip to content

Commit 92ab765

Browse files
authored
Merge pull request #9 from mindsphere/dev
cmake build related issues resolved.
2 parents fce6a7a + d7357b1 commit 92ab765

File tree

19 files changed

+268
-92
lines changed

19 files changed

+268
-92
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# [4.3.1] (2021-05-15)
2+
3+
## Bug Fixes
4+
5+
* Cmake build related issues reported from github repository are fixed.
6+
17
# [4.3.0] (2021-03-27)
28

39
## Features

mcl_connectivity/examples/callbacks.c

Lines changed: 205 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,235 @@
88

99
#include "callbacks.h"
1010
#include <stdio.h>
11+
#include <stdlib.h>
1112
#include <string.h>
12-
#include <stdlib.h>
13+
#include "mcl_core/mcl_file_util.h"
14+
#include "mcl_core/mcl_string_util.h"
15+
#include "mcl_core/mcl_memory.h"
1316

14-
static const char *credentials_file_name = "credentialsFile.txt";
17+
static const char *credentials_file_name = "credentials.txt";
1518

16-
mcl_error_t custom_load_function(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri)
19+
#define MAX_DIGIT_FOR_PARAMETER_LENGTH 4
20+
21+
static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor);
22+
static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor);
23+
24+
mcl_error_t custom_load_function_shared_secret(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri)
25+
{
26+
// It is strongly recommended to use callback functions with encryption for loading/saving credentials.
27+
void *file_descriptor = NULL;
28+
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor);
29+
30+
if (MCL_OK == code)
31+
{
32+
code = read_parameter_from_file(client_id, file_descriptor);
33+
}
34+
35+
if (MCL_OK == code)
36+
{
37+
code = read_parameter_from_file(client_secret, file_descriptor);
38+
}
39+
40+
if (MCL_OK == code)
41+
{
42+
code = read_parameter_from_file(registration_access_token, file_descriptor);
43+
}
44+
45+
if (MCL_OK == code)
46+
{
47+
code = read_parameter_from_file(registration_uri, file_descriptor);
48+
}
49+
50+
if (MCL_OK != code)
51+
{
52+
MCL_FREE(*client_id);
53+
MCL_FREE(*client_secret);
54+
MCL_FREE(*registration_access_token);
55+
MCL_FREE(*registration_uri);
56+
57+
code = MCL_CREDENTIALS_NOT_LOADED;
58+
}
59+
60+
(void) mcl_file_util_fclose(file_descriptor);
61+
62+
return code;
63+
}
64+
65+
mcl_error_t custom_save_function_shared_secret(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri)
66+
{
67+
void *file_descriptor = NULL;
68+
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor);
69+
70+
if (MCL_OK == code)
71+
{
72+
save_parameter_to_file(client_id, file_descriptor);
73+
}
74+
75+
if (MCL_OK == code)
76+
{
77+
save_parameter_to_file(client_secret, file_descriptor);
78+
}
79+
80+
if (MCL_OK == code)
81+
{
82+
save_parameter_to_file(registration_access_token, file_descriptor);
83+
}
84+
85+
if (MCL_OK == code)
86+
{
87+
save_parameter_to_file(registration_uri, file_descriptor);
88+
}
89+
90+
if (MCL_OK != code)
91+
{
92+
// In real application, user may try to save more aggressively instead of giving up.
93+
code = MCL_CREDENTIALS_NOT_SAVED;
94+
}
95+
96+
(void) mcl_file_util_fclose(file_descriptor);
97+
98+
return code;
99+
}
100+
101+
mcl_error_t custom_load_function_rsa(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri)
17102
{
18103
// It is strongly recommended to use callback functions with encryption for loading/saving credentials.
19-
FILE *fd = fopen(credentials_file_name, "r");
104+
void *file_descriptor = NULL;
105+
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor);
20106

21-
if (NULL == fd)
107+
if (MCL_OK == code)
22108
{
23-
*client_id = NULL;
24-
*client_secret = NULL;
25-
*registration_access_token = NULL;
26-
*registration_uri = NULL;
27-
printf("[INFO] : Absence of the file means we are not onboarded yet, initial access token will be used to onboard.\n");
28-
return MCL_CREDENTIALS_NOT_LOADED;
109+
code = read_parameter_from_file(client_id, file_descriptor);
29110
}
30111

31-
char string_length[5] = {0};
32-
int size_to_allocate;
112+
if (MCL_OK == code)
113+
{
114+
code = read_parameter_from_file(public_key, file_descriptor);
115+
}
33116

34-
fgets(string_length, 5, fd);
35-
size_to_allocate = atoi(string_length) + 1;
36-
*client_id = calloc(size_to_allocate, 1);
37-
fgets(*client_id, size_to_allocate, fd);
117+
if (MCL_OK == code)
118+
{
119+
code = read_parameter_from_file(private_key, file_descriptor);
120+
}
38121

39-
fgets(string_length, 5, fd);
40-
size_to_allocate = atoi(string_length) + 1;
41-
*client_secret = calloc(size_to_allocate, 1);
42-
fgets(*client_secret, size_to_allocate, fd);
122+
if (MCL_OK == code)
123+
{
124+
code = read_parameter_from_file(registration_access_token, file_descriptor);
125+
}
43126

44-
fgets(string_length, 5, fd);
45-
size_to_allocate = atoi(string_length) + 1;
46-
*registration_access_token = calloc(size_to_allocate, 1);
47-
fgets(*registration_access_token, size_to_allocate, fd);
127+
if (MCL_OK == code)
128+
{
129+
code = read_parameter_from_file(registration_uri, file_descriptor);
130+
}
48131

49-
fgets(string_length, 5, fd);
50-
size_to_allocate = atoi(string_length) + 1;
51-
*registration_uri = calloc(size_to_allocate, 1);
52-
fgets(*registration_uri, size_to_allocate, fd);
132+
if (MCL_OK != code)
133+
{
134+
MCL_FREE(*client_id);
135+
MCL_FREE(*public_key);
136+
MCL_FREE(*private_key);
137+
MCL_FREE(*registration_access_token);
138+
MCL_FREE(*registration_uri);
53139

54-
fclose(fd);
55-
return MCL_OK;
140+
code = MCL_CREDENTIALS_NOT_LOADED;
141+
}
142+
143+
(void) mcl_file_util_fclose(file_descriptor);
144+
145+
return code;
56146
}
57147

58-
mcl_error_t custom_save_function(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri)
148+
mcl_error_t custom_save_function_rsa(const char *client_id, const char *public_key, const char *private_key, const char *registration_access_token, const char *registration_uri)
59149
{
60-
FILE *fd = fopen(credentials_file_name, "w");
150+
void *file_descriptor = NULL;
151+
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor);
152+
153+
if (MCL_OK == code)
154+
{
155+
save_parameter_to_file(client_id, file_descriptor);
156+
}
157+
158+
if (MCL_OK == code)
159+
{
160+
save_parameter_to_file(public_key, file_descriptor);
161+
}
162+
163+
if (MCL_OK == code)
164+
{
165+
save_parameter_to_file(private_key, file_descriptor);
166+
}
61167

62-
if (NULL == fd)
168+
if (MCL_OK == code)
63169
{
64-
printf("[ERROR] : File cannot be opened or truncated to save the credentials.\n");
65-
return MCL_CREDENTIALS_NOT_SAVED;
170+
save_parameter_to_file(registration_access_token, file_descriptor);
66171
}
67172

68-
char string_length[5] = {0};
173+
if (MCL_OK == code)
174+
{
175+
save_parameter_to_file(registration_uri, file_descriptor);
176+
}
69177

70-
snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_id));
71-
fputs(string_length, fd);
72-
fputs(client_id, fd);
178+
if (MCL_OK != code)
179+
{
180+
// In real application, user may try to save more aggressively instead of giving up.
181+
code = MCL_CREDENTIALS_NOT_SAVED;
182+
}
73183

74-
snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_secret));
75-
fputs(string_length, fd);
76-
fputs(client_secret, fd);
184+
(void) mcl_file_util_fclose(file_descriptor);
77185

78-
snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_access_token));
79-
fputs(string_length, fd);
80-
fputs(registration_access_token, fd);
186+
return code;
187+
}
188+
189+
static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor)
190+
{
191+
mcl_error_t code;
192+
char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0};
193+
int length;
194+
195+
*parameter = NULL;
196+
code = mcl_file_util_fgets(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, file_descriptor);
197+
198+
if (MCL_OK == code)
199+
{
200+
length = atoi(length_string);
201+
*parameter = MCL_CALLOC(length + 1, 1);
202+
203+
if (NULL != *parameter)
204+
{
205+
mcl_size_t actual_count = 0;
206+
mcl_file_util_fread(*parameter, 1, length, file_descriptor, &actual_count);
207+
208+
if (actual_count != (unsigned) length)
209+
{
210+
code = MCL_FAIL;
211+
MCL_FREE(*parameter);
212+
}
213+
}
214+
else
215+
{
216+
code = MCL_OUT_OF_MEMORY;
217+
}
218+
}
81219

82-
snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_uri));
83-
fputs(string_length, fd);
84-
fputs(registration_uri, fd);
220+
return code;
221+
}
222+
223+
static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor)
224+
{
225+
mcl_error_t code;
226+
char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0};
227+
mcl_size_t parameter_length = mcl_string_util_strlen(parameter);
228+
229+
code = mcl_string_util_snprintf(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, "%04u", (unsigned int) parameter_length);
230+
231+
if (MCL_OK == code)
232+
{
233+
code = mcl_file_util_fwrite(length_string, 1, MAX_DIGIT_FOR_PARAMETER_LENGTH, file_descriptor);
234+
}
235+
236+
if (MCL_OK == code)
237+
{
238+
code = mcl_file_util_fwrite(parameter, 1, parameter_length, file_descriptor);
239+
}
85240

86-
fclose(fd);
87-
return MCL_OK;
241+
return code;
88242
}

mcl_connectivity/examples/callbacks.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ extern "C"
1616
{
1717
#endif
1818

19-
mcl_error_t custom_load_function(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri);
20-
mcl_error_t custom_save_function(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri);
19+
mcl_error_t custom_load_function_shared_secret(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri);
20+
mcl_error_t custom_save_function_shared_secret(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri);
21+
22+
mcl_error_t custom_load_function_rsa(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri);
23+
mcl_error_t custom_save_function_rsa(const char *client_id, const char *public_key, const char *private_key, const char *registration_access_token, const char *registration_uri);
2124

2225
#ifdef __cplusplus
2326
}

mcl_connectivity/examples/create_mapping.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ int main(void)
9999
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
100100
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
101101
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
102-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
103-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
102+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
103+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
104104

105105
// Initialize mcl core with the core configuration.
106106
code = mcl_core_initialize(core_configuration, &core);

mcl_connectivity/examples/dsc_upload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ int main(void)
164164
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_TYPE, &mcl_proxy);
165165
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
166166
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
167-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
168-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
167+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
168+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
169169

170170
// Initialize mcl core with the core configuration.
171171
code = mcl_core_initialize(core_configuration, &core);

mcl_connectivity/examples/event_upload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ int main(void)
8787
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_USER_AGENT, "my user agent");
8888
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
8989
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
90-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
91-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
90+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
91+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
9292

9393
// Initialize mcl core with the core configuration.
9494
code = mcl_core_initialize(core_configuration, &core);

mcl_connectivity/examples/file_upload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ int main(void)
9090
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
9191
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
9292
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
93-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
94-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
93+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
94+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
9595

9696
// Initialize mcl core with the core configuration.
9797
code = mcl_core_initialize(core_configuration, &core);

mcl_connectivity/examples/store_upload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ int main(void)
165165
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
166166
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
167167
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
168-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
169-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
168+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
169+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
170170

171171
// Initialize mcl core with the core configuration.
172172
code = mcl_core_initialize(core_configuration, &core);

mcl_connectivity/examples/timeseries_upload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ int main(void)
144144
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_PORT, &proxy_port);
145145
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_TYPE, &mcl_proxy);
146146
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
147-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
148-
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
147+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
148+
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);
149149

150150
// Initialize mcl core with the core configuration.
151151
code = mcl_core_initialize(core_configuration, &core);

mcl_core/examples/callbacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "mcl_core/mcl_string_util.h"
1515
#include "mcl_core/mcl_memory.h"
1616

17-
static const char *credentials_file_name = "registrationInformationFile.txt";
17+
static const char *credentials_file_name = "credentials.txt";
1818

1919
#define MAX_DIGIT_FOR_PARAMETER_LENGTH 4
2020

mcl_core/include/mcl_core/mcl_core_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ typedef enum E_MCL_CORE_RETURN_CODE
127127
MCL_NOT_FOUND, //!< If the response of server is HTTP 404.
128128
MCL_CONFLICT, //!< If the response of server is HTTP 409.
129129
MCL_PRECONDITION_FAIL, //!< If the response of server is HTTP 412.
130-
MCL_REQUEST_PAYLOAD_TOO_LARGE, //!> If the response of server is HTTP 413.
131-
MCL_TOO_MANY_REQUESTS, //!> If the response of server is HTTP 429.
130+
MCL_REQUEST_PAYLOAD_TOO_LARGE, //!< If the response of server is HTTP 413.
131+
MCL_TOO_MANY_REQUESTS, //!< If the response of server is HTTP 429.
132132
MCL_UNEXPECTED_RESULT_CODE, //!< If the response of server is unexpected.
133133

134134
// Status return codes.

0 commit comments

Comments
 (0)