Skip to content

Commit

Permalink
Merge pull request #9 from mindsphere/dev
Browse files Browse the repository at this point in the history
cmake build related issues resolved.
  • Loading branch information
mclib-dev authored May 17, 2021
2 parents fce6a7a + d7357b1 commit 92ab765
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 92 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [4.3.1] (2021-05-15)

## Bug Fixes

* Cmake build related issues reported from github repository are fixed.

# [4.3.0] (2021-03-27)

## Features
Expand Down
256 changes: 205 additions & 51 deletions mcl_connectivity/examples/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,235 @@

#include "callbacks.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include "mcl_core/mcl_file_util.h"
#include "mcl_core/mcl_string_util.h"
#include "mcl_core/mcl_memory.h"

static const char *credentials_file_name = "credentialsFile.txt";
static const char *credentials_file_name = "credentials.txt";

mcl_error_t custom_load_function(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri)
#define MAX_DIGIT_FOR_PARAMETER_LENGTH 4

static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor);
static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor);

mcl_error_t custom_load_function_shared_secret(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri)
{
// It is strongly recommended to use callback functions with encryption for loading/saving credentials.
void *file_descriptor = NULL;
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor);

if (MCL_OK == code)
{
code = read_parameter_from_file(client_id, file_descriptor);
}

if (MCL_OK == code)
{
code = read_parameter_from_file(client_secret, file_descriptor);
}

if (MCL_OK == code)
{
code = read_parameter_from_file(registration_access_token, file_descriptor);
}

if (MCL_OK == code)
{
code = read_parameter_from_file(registration_uri, file_descriptor);
}

if (MCL_OK != code)
{
MCL_FREE(*client_id);
MCL_FREE(*client_secret);
MCL_FREE(*registration_access_token);
MCL_FREE(*registration_uri);

code = MCL_CREDENTIALS_NOT_LOADED;
}

(void) mcl_file_util_fclose(file_descriptor);

return code;
}

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)
{
void *file_descriptor = NULL;
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor);

if (MCL_OK == code)
{
save_parameter_to_file(client_id, file_descriptor);
}

if (MCL_OK == code)
{
save_parameter_to_file(client_secret, file_descriptor);
}

if (MCL_OK == code)
{
save_parameter_to_file(registration_access_token, file_descriptor);
}

if (MCL_OK == code)
{
save_parameter_to_file(registration_uri, file_descriptor);
}

if (MCL_OK != code)
{
// In real application, user may try to save more aggressively instead of giving up.
code = MCL_CREDENTIALS_NOT_SAVED;
}

(void) mcl_file_util_fclose(file_descriptor);

return code;
}

mcl_error_t custom_load_function_rsa(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri)
{
// It is strongly recommended to use callback functions with encryption for loading/saving credentials.
FILE *fd = fopen(credentials_file_name, "r");
void *file_descriptor = NULL;
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "r", &file_descriptor);

if (NULL == fd)
if (MCL_OK == code)
{
*client_id = NULL;
*client_secret = NULL;
*registration_access_token = NULL;
*registration_uri = NULL;
printf("[INFO] : Absence of the file means we are not onboarded yet, initial access token will be used to onboard.\n");
return MCL_CREDENTIALS_NOT_LOADED;
code = read_parameter_from_file(client_id, file_descriptor);
}

char string_length[5] = {0};
int size_to_allocate;
if (MCL_OK == code)
{
code = read_parameter_from_file(public_key, file_descriptor);
}

fgets(string_length, 5, fd);
size_to_allocate = atoi(string_length) + 1;
*client_id = calloc(size_to_allocate, 1);
fgets(*client_id, size_to_allocate, fd);
if (MCL_OK == code)
{
code = read_parameter_from_file(private_key, file_descriptor);
}

fgets(string_length, 5, fd);
size_to_allocate = atoi(string_length) + 1;
*client_secret = calloc(size_to_allocate, 1);
fgets(*client_secret, size_to_allocate, fd);
if (MCL_OK == code)
{
code = read_parameter_from_file(registration_access_token, file_descriptor);
}

fgets(string_length, 5, fd);
size_to_allocate = atoi(string_length) + 1;
*registration_access_token = calloc(size_to_allocate, 1);
fgets(*registration_access_token, size_to_allocate, fd);
if (MCL_OK == code)
{
code = read_parameter_from_file(registration_uri, file_descriptor);
}

fgets(string_length, 5, fd);
size_to_allocate = atoi(string_length) + 1;
*registration_uri = calloc(size_to_allocate, 1);
fgets(*registration_uri, size_to_allocate, fd);
if (MCL_OK != code)
{
MCL_FREE(*client_id);
MCL_FREE(*public_key);
MCL_FREE(*private_key);
MCL_FREE(*registration_access_token);
MCL_FREE(*registration_uri);

fclose(fd);
return MCL_OK;
code = MCL_CREDENTIALS_NOT_LOADED;
}

(void) mcl_file_util_fclose(file_descriptor);

return code;
}

mcl_error_t custom_save_function(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri)
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)
{
FILE *fd = fopen(credentials_file_name, "w");
void *file_descriptor = NULL;
mcl_error_t code = mcl_file_util_fopen(credentials_file_name, "w", &file_descriptor);

if (MCL_OK == code)
{
save_parameter_to_file(client_id, file_descriptor);
}

if (MCL_OK == code)
{
save_parameter_to_file(public_key, file_descriptor);
}

if (MCL_OK == code)
{
save_parameter_to_file(private_key, file_descriptor);
}

if (NULL == fd)
if (MCL_OK == code)
{
printf("[ERROR] : File cannot be opened or truncated to save the credentials.\n");
return MCL_CREDENTIALS_NOT_SAVED;
save_parameter_to_file(registration_access_token, file_descriptor);
}

char string_length[5] = {0};
if (MCL_OK == code)
{
save_parameter_to_file(registration_uri, file_descriptor);
}

snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_id));
fputs(string_length, fd);
fputs(client_id, fd);
if (MCL_OK != code)
{
// In real application, user may try to save more aggressively instead of giving up.
code = MCL_CREDENTIALS_NOT_SAVED;
}

snprintf(string_length, 5, "%04u", (unsigned int) strlen(client_secret));
fputs(string_length, fd);
fputs(client_secret, fd);
(void) mcl_file_util_fclose(file_descriptor);

snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_access_token));
fputs(string_length, fd);
fputs(registration_access_token, fd);
return code;
}

static mcl_error_t read_parameter_from_file(char **parameter, void *file_descriptor)
{
mcl_error_t code;
char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0};
int length;

*parameter = NULL;
code = mcl_file_util_fgets(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, file_descriptor);

if (MCL_OK == code)
{
length = atoi(length_string);
*parameter = MCL_CALLOC(length + 1, 1);

if (NULL != *parameter)
{
mcl_size_t actual_count = 0;
mcl_file_util_fread(*parameter, 1, length, file_descriptor, &actual_count);

if (actual_count != (unsigned) length)
{
code = MCL_FAIL;
MCL_FREE(*parameter);
}
}
else
{
code = MCL_OUT_OF_MEMORY;
}
}

snprintf(string_length, 5, "%04u", (unsigned int) strlen(registration_uri));
fputs(string_length, fd);
fputs(registration_uri, fd);
return code;
}

static mcl_error_t save_parameter_to_file(const char *parameter, void *file_descriptor)
{
mcl_error_t code;
char length_string[MAX_DIGIT_FOR_PARAMETER_LENGTH + 1] = {0};
mcl_size_t parameter_length = mcl_string_util_strlen(parameter);

code = mcl_string_util_snprintf(length_string, MAX_DIGIT_FOR_PARAMETER_LENGTH + 1, "%04u", (unsigned int) parameter_length);

if (MCL_OK == code)
{
code = mcl_file_util_fwrite(length_string, 1, MAX_DIGIT_FOR_PARAMETER_LENGTH, file_descriptor);
}

if (MCL_OK == code)
{
code = mcl_file_util_fwrite(parameter, 1, parameter_length, file_descriptor);
}

fclose(fd);
return MCL_OK;
return code;
}
7 changes: 5 additions & 2 deletions mcl_connectivity/examples/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ extern "C"
{
#endif

mcl_error_t custom_load_function(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri);
mcl_error_t custom_save_function(const char *client_id, const char *client_secret, const char *registration_access_token, const char *registration_uri);
mcl_error_t custom_load_function_shared_secret(char **client_id, char **client_secret, char **registration_access_token, char **registration_uri);
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);

mcl_error_t custom_load_function_rsa(char **client_id, char **public_key, char **private_key, char **registration_access_token, char **registration_uri);
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);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/create_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/dsc_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_TYPE, &mcl_proxy);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/event_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_USER_AGENT, "my user agent");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/file_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/store_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_SECURITY_PROFILE, &security_profile);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_IAT, initial_access_token);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
4 changes: 2 additions & 2 deletions mcl_connectivity/examples/timeseries_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ int main(void)
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_PORT, &proxy_port);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_PROXY_TYPE, &mcl_proxy);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_TENANT, "mclibiot");
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_LOAD_CALLBACK, custom_load_function_shared_secret);
mcl_core_configuration_set_parameter(core_configuration, MCL_CORE_CONFIGURATION_PARAMETER_CREDENTIALS_SAVE_CALLBACK, custom_save_function_shared_secret);

// Initialize mcl core with the core configuration.
code = mcl_core_initialize(core_configuration, &core);
Expand Down
2 changes: 1 addition & 1 deletion mcl_core/examples/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "mcl_core/mcl_string_util.h"
#include "mcl_core/mcl_memory.h"

static const char *credentials_file_name = "registrationInformationFile.txt";
static const char *credentials_file_name = "credentials.txt";

#define MAX_DIGIT_FOR_PARAMETER_LENGTH 4

Expand Down
4 changes: 2 additions & 2 deletions mcl_core/include/mcl_core/mcl_core_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ typedef enum E_MCL_CORE_RETURN_CODE
MCL_NOT_FOUND, //!< If the response of server is HTTP 404.
MCL_CONFLICT, //!< If the response of server is HTTP 409.
MCL_PRECONDITION_FAIL, //!< If the response of server is HTTP 412.
MCL_REQUEST_PAYLOAD_TOO_LARGE, //!> If the response of server is HTTP 413.
MCL_TOO_MANY_REQUESTS, //!> If the response of server is HTTP 429.
MCL_REQUEST_PAYLOAD_TOO_LARGE, //!< If the response of server is HTTP 413.
MCL_TOO_MANY_REQUESTS, //!< If the response of server is HTTP 429.
MCL_UNEXPECTED_RESULT_CODE, //!< If the response of server is unexpected.

// Status return codes.
Expand Down
Loading

0 comments on commit 92ab765

Please sign in to comment.