From c3d25a88bebf9ad308192ec0a23b755fdfef1213 Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Tue, 3 Dec 2024 17:24:18 -0500 Subject: [PATCH] chore: make atclient support multiple json providers --- .../include/atclient/atnotification.h | 1 - .../include/atclient/{cjson.h => json.h} | 7 +- packages/atclient/include/atclient/metadata.h | 10 - packages/atclient/src/atnotification.c | 1035 +++++----- packages/atclient/src/metadata.c | 1825 +++++++++-------- packages/atcommons/src/enroll_namespace.c | 2 +- packages/atcommons/src/enroll_params.c | 2 +- 7 files changed, 1445 insertions(+), 1437 deletions(-) rename packages/atclient/include/atclient/{cjson.h => json.h} (58%) diff --git a/packages/atclient/include/atclient/atnotification.h b/packages/atclient/include/atclient/atnotification.h index d2edad92..a0e3ffa1 100644 --- a/packages/atclient/include/atclient/atnotification.h +++ b/packages/atclient/include/atclient/atnotification.h @@ -96,7 +96,6 @@ void atclient_atnotification_init(atclient_atnotification *notification); void atclient_atnotification_free(atclient_atnotification *notification); int atclient_atnotification_from_json_str(atclient_atnotification *notification, const char *json_str); -int atclient_atnotification_from_cjson_node(atclient_atnotification *notification, const cJSON *root); // Check if a field is initialized bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification); diff --git a/packages/atclient/include/atclient/cjson.h b/packages/atclient/include/atclient/json.h similarity index 58% rename from packages/atclient/include/atclient/cjson.h rename to packages/atclient/include/atclient/json.h index bcb7bf68..33e4eb0b 100644 --- a/packages/atclient/include/atclient/cjson.h +++ b/packages/atclient/include/atclient/json.h @@ -1,9 +1,12 @@ -#ifndef ATCLIENT_CJSON_H -#define ATCLIENT_CJSON_H +#ifndef ATCLIENT_JSON_H +#define ATCLIENT_JSON_H #ifdef __cplusplus extern "C" { #endif +#define ATCLIENT_JSON_PROVIDER_CJSON 1 +#define ATCLIENT_JSON_PROVIDER ATCLIENT_JSON_PROVIDER_CJSON + #if defined(CONFIG_IDF_TARGET_ESP32) #include // IWYU pragma: export #else diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index fb880b9b..f8c8875b 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -286,16 +286,6 @@ int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_a */ int atclient_atkey_metadata_from_json_str(atclient_atkey_metadata *metadata, const char *metadata_str); -/** - * @brief Populates the metadata struct from a cJSON pointer. This function is good for debugging. - * - * @param metadata the metadata struct to populate - * @param json the json object to populate from - * @return int 0 on success - */ - -int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json); - /** * @brief Reads metadata struct and converts it to a json formatted string. This function should mostly be used for * debugging only. See atclient_atkey_metadata_to_protocolstr for a more useful function when working with atProtocol diff --git a/packages/atclient/src/atnotification.c b/packages/atclient/src/atnotification.c index f86b1a5a..b702a2f3 100644 --- a/packages/atclient/src/atnotification.c +++ b/packages/atclient/src/atnotification.c @@ -5,6 +5,11 @@ #define TAG "atnotification" +// Json library specific +#if ATCLIENT_JSON_PROVIDER == ATCLIENT_JSON_PROVIDER_CJSON +static int atclient_atnotification_from_cjson_node(atclient_atnotification *notification, const cJSON *root); +#endif + static void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized); static void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, const bool initialized); static void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, const bool initialized); @@ -84,340 +89,165 @@ void atclient_atnotification_free(atclient_atnotification *notification) { } } -int atclient_atnotification_from_json_str(atclient_atnotification *notification, const char *json_str) { - int ret = 1; +bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ if (notification == NULL) { - ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return ret; + return false; } - if (json_str == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "JSON string is NULL\n"); - return ret; + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); +} + +bool atclient_atnotification_is_from_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } /* - * 2. Variables + * 2. Return is initialized */ - cJSON *root = NULL; + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & + ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); +} +bool atclient_atnotification_is_to_initialized(const atclient_atnotification *notification) { /* - * 3. Parse JSON + * 1. Validate arguments */ - if ((root = cJSON_Parse(json_str)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse JSON\n"); - goto exit; + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } /* - * 4. Extract values + * 2. Return is initialized */ - if ((ret = atclient_atnotification_from_cjson_node(notification, root)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to extract values from JSON\n"); - goto exit; + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); +} + +bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - ret = 0; - goto exit; -exit: { - cJSON_Delete(root); - return ret; -} + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & + ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); } -int atclient_atnotification_from_cjson_node(atclient_atnotification *notification, const cJSON *root) { - int ret = 1; +bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ if (notification == NULL) { - ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return ret; + return false; } - if (root == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "JSON node is NULL\n"); - return ret; - } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & + ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); +} +bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification) { /* - * 2. Variables + * 1. Validate arguments */ - cJSON *id = cJSON_GetObjectItem(root, "id"); - if (id != NULL) { - if (id->type != cJSON_NULL) { - if (id->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification id is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_id(notification, id->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification id\n"); - goto exit; - } - } else { - atclient_atnotification_unset_id(notification); - atclient_atnotification_id_set_initialized(notification, true); // set it to initialized, but it is kept null - notification->id = NULL; - } + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - cJSON *from = cJSON_GetObjectItem(root, "from"); - if (from != NULL) { - if (from->type != cJSON_NULL) { - if (from->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification from is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_from(notification, from->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification from\n"); - goto exit; - } - } else { - atclient_atnotification_unset_from(notification); - atclient_atnotification_from_set_initialized(notification, true); // set it to initialized, but it is kept null - notification->from = NULL; - } - } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & + ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); +} - cJSON *to = cJSON_GetObjectItem(root, "to"); - if (to != NULL) { - if (to->type != cJSON_NULL) { - if ((to->type != cJSON_String)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification to is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_to(notification, to->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification to\n"); - goto exit; - } - } else { - atclient_atnotification_unset_to(notification); - atclient_atnotification_to_set_initialized(notification, true); // set it to initialized, but it is kept null - notification->to = NULL; - } +bool atclient_atnotification_is_epoch_millis_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - cJSON *key = cJSON_GetObjectItem(root, "key"); - if (key != NULL) { - if (key->type != cJSON_NULL) { - if (key->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification key is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_key(notification, key->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification key\n"); - goto exit; - } - } else { - atclient_atnotification_unset_key(notification); - atclient_atnotification_key_set_initialized(notification, true); // set it to initialized, but it is kept null - notification->key = NULL; - } - } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & + ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); +} - cJSON *value = cJSON_GetObjectItem(root, "value"); - if (value != NULL) { - if (value->type != cJSON_NULL) { - if (value->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification value is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_value(notification, value->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification value\n"); - goto exit; - } - } else { - atclient_atnotification_unset_value(notification); - atclient_atnotification_value_set_initialized(notification, true); // set it to initialized, but it is kept null - notification->value = NULL; - } +bool atclient_atnotification_is_message_type_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - cJSON *operation = cJSON_GetObjectItem(root, "operation"); - if (operation != NULL) { - if (operation->type != cJSON_NULL) { - if (operation->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification operation is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_operation(notification, operation->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification operation\n"); - goto exit; - } - } else { - atclient_atnotification_unset_operation(notification); - atclient_atnotification_operation_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->operation = NULL; - } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & + ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); +} + +bool atclient_atnotification_is_is_encrypted_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - cJSON *epoch_millis = cJSON_GetObjectItem(root, "epochMillis"); - if (epoch_millis != NULL) { - if (epoch_millis->type != cJSON_Number) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification epoch_millis is not an integer\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_epoch_millis(notification, epoch_millis->valueint)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epoch_millis\n"); - goto exit; - } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & + ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); +} + +bool atclient_atnotification_is_enc_key_name_initialized(const atclient_atnotification *notification) { + /* + * 1. Validate arguments + */ + if (notification == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return false; } - cJSON *message_type = cJSON_GetObjectItem(root, "messageType"); - if (message_type != NULL) { - if (message_type->type != cJSON_NULL) { - if (message_type->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification message_type is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_message_type(notification, message_type->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification message_type\n"); - goto exit; - } - } else { - atclient_atnotification_unset_message_type(notification); - atclient_atnotification_message_type_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->message_type = NULL; - } - } - - cJSON *is_encrypted = cJSON_GetObjectItem(root, "isEncrypted"); - if (is_encrypted != NULL) { - if (is_encrypted->type == cJSON_True) { - if ((ret = atclient_atnotification_set_is_encrypted(notification, true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); - goto exit; - } - } else if (is_encrypted->type == cJSON_False) { - if ((ret = atclient_atnotification_set_is_encrypted(notification, false)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); - goto exit; - } - } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is_encrypted is not a boolean\n"); - goto exit; - } - } - - cJSON *metadata = cJSON_GetObjectItem(root, "metadata"); - if (metadata != NULL) { - // get enc_key_name - cJSON *enc_key_name = cJSON_GetObjectItem(metadata, "encKeyName"); - if (enc_key_name != NULL) { - if (enc_key_name->type != cJSON_NULL) { - if (enc_key_name->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_key_name is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_enc_key_name(notification, enc_key_name->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_key_name\n"); - goto exit; - } - } else { - atclient_atnotification_unset_enc_key_name(notification); - atclient_atnotification_enc_key_name_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->enc_key_name = NULL; - } - } - - // get enc_algo - cJSON *enc_algo = cJSON_GetObjectItem(metadata, "encAlgo"); - if (enc_algo != NULL) { - if (enc_algo->type != cJSON_NULL) { - if (enc_algo->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_algo is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_enc_algo(notification, enc_algo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_algo\n"); - goto exit; - } - } else { - atclient_atnotification_unset_enc_algo(notification); - atclient_atnotification_enc_algo_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->enc_algo = NULL; - } - } - - // get iv_nonce - cJSON *iv_nonce = cJSON_GetObjectItem(metadata, "ivNonce"); - if (iv_nonce != NULL) { - if (iv_nonce->type != cJSON_NULL) { - if (iv_nonce->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification iv_nonce is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_iv_nonce(notification, iv_nonce->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification iv_nonce\n"); - goto exit; - } - } else { - atclient_atnotification_unset_iv_nonce(notification); - atclient_atnotification_iv_nonce_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->iv_nonce = NULL; - } - } - - // get ske_enc_key_name - cJSON *ske_enc_key_name = cJSON_GetObjectItem(metadata, "skeEncKeyName"); - if (ske_enc_key_name != NULL) { - if (ske_enc_key_name->type != cJSON_NULL) { - if (ske_enc_key_name->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_key_name is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_ske_enc_key_name(notification, ske_enc_key_name->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_key_name\n"); - goto exit; - } - } else { - atclient_atnotification_unset_ske_enc_key_name(notification); - atclient_atnotification_ske_enc_key_name_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->ske_enc_key_name = NULL; - } - } - - // get ske_enc_algo - cJSON *ske_enc_algo = cJSON_GetObjectItem(metadata, "skeEncAlgo"); - if (ske_enc_algo != NULL) { - if (ske_enc_algo->type != cJSON_NULL) { - if (ske_enc_algo->type != cJSON_String) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_algo is not a string\n"); - goto exit; - } - if ((ret = atclient_atnotification_set_ske_enc_algo(notification, ske_enc_algo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_algo\n"); - goto exit; - } - } else { - atclient_atnotification_unset_ske_enc_algo(notification); - atclient_atnotification_ske_enc_algo_set_initialized(notification, - true); // set it to initialized, but it is kept null - notification->ske_enc_algo = NULL; - } - } - } - - ret = 0; - goto exit; -exit: { return ret; } + /* + * 2. Return is initialized + */ + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & + ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_enc_algo_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ @@ -429,10 +259,11 @@ bool atclient_atnotification_is_id_initialized(const atclient_atnotification *no /* * 2. Return is initialized */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & + ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); } -bool atclient_atnotification_is_from_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_iv_nonce_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ @@ -444,11 +275,11 @@ bool atclient_atnotification_is_from_initialized(const atclient_atnotification * /* * 2. Return is initialized */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & - ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & + ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); } -bool atclient_atnotification_is_to_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_ske_enc_key_name_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ @@ -460,10 +291,11 @@ bool atclient_atnotification_is_to_initialized(const atclient_atnotification *no /* * 2. Return is initialized */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & + ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_ske_enc_algo_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ @@ -475,11 +307,11 @@ bool atclient_atnotification_is_key_initialized(const atclient_atnotification *n /* * 2. Return is initialized */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & - ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & + ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); } -bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_decrypted_value_initialized(const atclient_atnotification *notification) { /* * 1. Validate arguments */ @@ -491,286 +323,126 @@ bool atclient_atnotification_is_value_initialized(const atclient_atnotification /* * 2. Return is initialized */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & - ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & + ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); } -bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized + * 2. Set the initialized bit flag */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & - ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); + if (initialized) { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + } else { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + } } -bool atclient_atnotification_is_epoch_millis_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, + const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized + * 2. Set the initialized bit flag */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & - ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); + if (initialized) { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + } else { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + } } -bool atclient_atnotification_is_message_type_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized + * 2. Set the initialized bit flag */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & - ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); + if (initialized) { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + } else { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + } } -bool atclient_atnotification_is_is_encrypted_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized + * 2. Set the initialized bit flag */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & - ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); + if (initialized) { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + } else { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + } } -bool atclient_atnotification_is_enc_key_name_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, + const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized + * 2. Set the initialized bit flag */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & - ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); + if (initialized) { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + } else { + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + } } -bool atclient_atnotification_is_enc_algo_initialized(const atclient_atnotification *notification) { +static void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, + const bool initialized) { + /* * 1. Validate arguments */ if (notification == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; + return; } /* - * 2. Return is initialized - */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & - ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); -} - -bool atclient_atnotification_is_iv_nonce_initialized(const atclient_atnotification *notification) { - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; - } - - /* - * 2. Return is initialized - */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & - ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); -} - -bool atclient_atnotification_is_ske_enc_key_name_initialized(const atclient_atnotification *notification) { - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; - } - - /* - * 2. Return is initialized - */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & - ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); -} - -bool atclient_atnotification_is_ske_enc_algo_initialized(const atclient_atnotification *notification) { - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; - } - - /* - * 2. Return is initialized - */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & - ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); -} - -bool atclient_atnotification_is_decrypted_value_initialized(const atclient_atnotification *notification) { - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return false; - } - - /* - * 2. Return is initialized - */ - return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & - ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); -} - -static void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag - */ - if (initialized) { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; - } else { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; - } -} - -static void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, - const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag - */ - if (initialized) { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; - } else { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; - } -} - -static void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag - */ - if (initialized) { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; - } else { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; - } -} - -static void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag - */ - if (initialized) { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; - } else { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; - } -} - -static void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, - const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag - */ - if (initialized) { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; - } else { - notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] &= - ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; - } -} - -static void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, - const bool initialized) { - - /* - * 1. Validate arguments - */ - if (notification == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); - return; - } - - /* - * 2. Set the initialized bit flag + * 2. Set the initialized bit flag */ if (initialized) { notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] |= @@ -1907,3 +1579,340 @@ int atclient_atnotification_set_decrypted_value(atclient_atnotification *notific goto exit; exit: { return ret; } } + +// Json library specific +#if ATCLIENT_JSON_PROVIDER == ATCLIENT_JSON_PROVIDER_CJSON +int atclient_atnotification_from_json_str(atclient_atnotification *notification, const char *json_str) { + int ret = 1; + /* + * 1. Validate arguments + */ + if (notification == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return ret; + } + + if (json_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "JSON string is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ + cJSON *root = NULL; + + /* + * 3. Parse JSON + */ + if ((root = cJSON_Parse(json_str)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse JSON\n"); + goto exit; + } + + /* + * 4. Extract values + */ + if ((ret = atclient_atnotification_from_cjson_node(notification, root)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to extract values from JSON\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { + cJSON_Delete(root); + return ret; +} +} + +static int atclient_atnotification_from_cjson_node(atclient_atnotification *notification, const cJSON *root) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (notification == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is NULL\n"); + return ret; + } + + if (root == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "JSON node is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ + cJSON *id = cJSON_GetObjectItem(root, "id"); + if (id != NULL) { + if (id->type != cJSON_NULL) { + if (id->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification id is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_id(notification, id->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification id\n"); + goto exit; + } + } else { + atclient_atnotification_unset_id(notification); + atclient_atnotification_id_set_initialized(notification, true); // set it to initialized, but it is kept null + notification->id = NULL; + } + } + + cJSON *from = cJSON_GetObjectItem(root, "from"); + if (from != NULL) { + if (from->type != cJSON_NULL) { + if (from->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification from is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_from(notification, from->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification from\n"); + goto exit; + } + } else { + atclient_atnotification_unset_from(notification); + atclient_atnotification_from_set_initialized(notification, true); // set it to initialized, but it is kept null + notification->from = NULL; + } + } + + cJSON *to = cJSON_GetObjectItem(root, "to"); + if (to != NULL) { + if (to->type != cJSON_NULL) { + if ((to->type != cJSON_String)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification to is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_to(notification, to->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification to\n"); + goto exit; + } + } else { + atclient_atnotification_unset_to(notification); + atclient_atnotification_to_set_initialized(notification, true); // set it to initialized, but it is kept null + notification->to = NULL; + } + } + + cJSON *key = cJSON_GetObjectItem(root, "key"); + if (key != NULL) { + if (key->type != cJSON_NULL) { + if (key->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification key is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_key(notification, key->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification key\n"); + goto exit; + } + } else { + atclient_atnotification_unset_key(notification); + atclient_atnotification_key_set_initialized(notification, true); // set it to initialized, but it is kept null + notification->key = NULL; + } + } + + cJSON *value = cJSON_GetObjectItem(root, "value"); + if (value != NULL) { + if (value->type != cJSON_NULL) { + if (value->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification value is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_value(notification, value->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification value\n"); + goto exit; + } + } else { + atclient_atnotification_unset_value(notification); + atclient_atnotification_value_set_initialized(notification, true); // set it to initialized, but it is kept null + notification->value = NULL; + } + } + + cJSON *operation = cJSON_GetObjectItem(root, "operation"); + if (operation != NULL) { + if (operation->type != cJSON_NULL) { + if (operation->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification operation is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_operation(notification, operation->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification operation\n"); + goto exit; + } + } else { + atclient_atnotification_unset_operation(notification); + atclient_atnotification_operation_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->operation = NULL; + } + } + + cJSON *epoch_millis = cJSON_GetObjectItem(root, "epochMillis"); + if (epoch_millis != NULL) { + if (epoch_millis->type != cJSON_Number) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification epoch_millis is not an integer\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_epoch_millis(notification, epoch_millis->valueint)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epoch_millis\n"); + goto exit; + } + } + + cJSON *message_type = cJSON_GetObjectItem(root, "messageType"); + if (message_type != NULL) { + if (message_type->type != cJSON_NULL) { + if (message_type->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification message_type is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_message_type(notification, message_type->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification message_type\n"); + goto exit; + } + } else { + atclient_atnotification_unset_message_type(notification); + atclient_atnotification_message_type_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->message_type = NULL; + } + } + + cJSON *is_encrypted = cJSON_GetObjectItem(root, "isEncrypted"); + if (is_encrypted != NULL) { + if (is_encrypted->type == cJSON_True) { + if ((ret = atclient_atnotification_set_is_encrypted(notification, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); + goto exit; + } + } else if (is_encrypted->type == cJSON_False) { + if ((ret = atclient_atnotification_set_is_encrypted(notification, false)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); + goto exit; + } + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification is_encrypted is not a boolean\n"); + goto exit; + } + } + + cJSON *metadata = cJSON_GetObjectItem(root, "metadata"); + if (metadata != NULL) { + // get enc_key_name + cJSON *enc_key_name = cJSON_GetObjectItem(metadata, "encKeyName"); + if (enc_key_name != NULL) { + if (enc_key_name->type != cJSON_NULL) { + if (enc_key_name->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_key_name is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_enc_key_name(notification, enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_key_name\n"); + goto exit; + } + } else { + atclient_atnotification_unset_enc_key_name(notification); + atclient_atnotification_enc_key_name_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->enc_key_name = NULL; + } + } + + // get enc_algo + cJSON *enc_algo = cJSON_GetObjectItem(metadata, "encAlgo"); + if (enc_algo != NULL) { + if (enc_algo->type != cJSON_NULL) { + if (enc_algo->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_algo is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_enc_algo(notification, enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_algo\n"); + goto exit; + } + } else { + atclient_atnotification_unset_enc_algo(notification); + atclient_atnotification_enc_algo_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->enc_algo = NULL; + } + } + + // get iv_nonce + cJSON *iv_nonce = cJSON_GetObjectItem(metadata, "ivNonce"); + if (iv_nonce != NULL) { + if (iv_nonce->type != cJSON_NULL) { + if (iv_nonce->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification iv_nonce is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_iv_nonce(notification, iv_nonce->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification iv_nonce\n"); + goto exit; + } + } else { + atclient_atnotification_unset_iv_nonce(notification); + atclient_atnotification_iv_nonce_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->iv_nonce = NULL; + } + } + + // get ske_enc_key_name + cJSON *ske_enc_key_name = cJSON_GetObjectItem(metadata, "skeEncKeyName"); + if (ske_enc_key_name != NULL) { + if (ske_enc_key_name->type != cJSON_NULL) { + if (ske_enc_key_name->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_key_name is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_ske_enc_key_name(notification, ske_enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_key_name\n"); + goto exit; + } + } else { + atclient_atnotification_unset_ske_enc_key_name(notification); + atclient_atnotification_ske_enc_key_name_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->ske_enc_key_name = NULL; + } + } + + // get ske_enc_algo + cJSON *ske_enc_algo = cJSON_GetObjectItem(metadata, "skeEncAlgo"); + if (ske_enc_algo != NULL) { + if (ske_enc_algo->type != cJSON_NULL) { + if (ske_enc_algo->type != cJSON_String) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_algo is not a string\n"); + goto exit; + } + if ((ret = atclient_atnotification_set_ske_enc_algo(notification, ske_enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_algo\n"); + goto exit; + } + } else { + atclient_atnotification_unset_ske_enc_algo(notification); + atclient_atnotification_ske_enc_algo_set_initialized(notification, + true); // set it to initialized, but it is kept null + notification->ske_enc_algo = NULL; + } + } + } + + ret = 0; + goto exit; +exit: { return ret; } +} +#endif diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 8b75d796..b69e6ee6 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1,5 +1,5 @@ #include "atclient/metadata.h" -#include "atclient/cjson.h" +#include "atclient/json.h" #include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include @@ -11,6 +11,11 @@ #define TAG "metadata" +// Json library specific +#if ATCLIENT_JSON_PROVIDER == ATCLIENT_JSON_PROVIDER_CJSON +static int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json); +#endif + static bool is_created_by_initialized(const atclient_atkey_metadata *metadata); static bool is_updated_by_initialized(const atclient_atkey_metadata *metadata); static bool is_status_initialized(const atclient_atkey_metadata *metadata); @@ -318,1114 +323,521 @@ int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_a exit: { return ret; } } -int atclient_atkey_metadata_from_json_str(atclient_atkey_metadata *metadata, const char *metadata_str) { - int ret = 1; - +size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *metadata) { /* * 1. Validate arguments */ if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - return ret; - } - - if (metadata_str == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); - return ret; + return 0; } /* - * 2. Parse JSON string + * 2. Calculate length */ - cJSON *root = cJSON_Parse(metadata_str); - if (root == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n"); - goto exit; + int64_t len = 0; + if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { + len += atclient_atkey_metadata_ttl_strlen(metadata); } - if ((ret = atclient_atkey_metadata_from_cjson_node(metadata, root)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_cjson_node: %d\n", ret); - goto exit; + if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { + len += atclient_atkey_metadata_ttb_strlen(metadata); } - ret = 0; - goto exit; -exit: { - cJSON_Delete(root); - return ret; -} -} - -int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json) { - int ret = 1; + if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { + len += atclient_atkey_metadata_ttr_strlen(metadata); + } - /* - * 1. Validate arguments - */ + if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { + len += atclient_atkey_metadata_ccd_strlen(metadata); + } - if (json == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "json is NULL\n"); - return ret; + if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + len += atclient_atkey_metadata_is_binary_strlen(metadata); } - if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - return ret; + if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + len += atclient_atkey_metadata_is_encrypted_strlen(metadata); } - /* - * 2. Parse JSON node - */ + if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + len += atclient_atkey_metadata_data_signature_strlen(metadata); + } - cJSON *created_by = cJSON_GetObjectItem(json, "createdBy"); - if (created_by != NULL) { - if (created_by->type != cJSON_NULL) { - if ((ret = set_created_by(metadata, created_by->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_created_by(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + len += atclient_atkey_metadata_shared_key_status_strlen(metadata); } - cJSON *updated_by = cJSON_GetObjectItem(json, "updatedBy"); - if (updated_by != NULL) { - if (updated_by->type != cJSON_NULL) { - if ((ret = set_updated_by(metadata, updated_by->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_updated_by(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + len += atclient_atkey_metadata_shared_key_enc_strlen(metadata); } - cJSON *status = cJSON_GetObjectItem(json, "status"); - if (status != NULL) { - if (status->type != cJSON_NULL) { - if ((ret = set_status(metadata, status->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_status(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + len += atclient_atkey_metadata_pub_key_hash_strlen(metadata); } - cJSON *version = cJSON_GetObjectItem(json, "version"); - if (version != NULL) { - if (version->type != cJSON_NULL) { - set_version(metadata, version->valueint); - } else { - set_version(metadata, 0); - } + if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + len += atclient_atkey_metadata_pub_key_algo_strlen(metadata); } - cJSON *expires_at = cJSON_GetObjectItem(json, "expiresAt"); - if (expires_at != NULL) { - if (expires_at->type != cJSON_NULL) { - if ((ret = set_expires_at(metadata, expires_at->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_expires_at(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { + len += atclient_atkey_metadata_encoding_strlen(metadata); } - cJSON *available_at = cJSON_GetObjectItem(json, "availableAt"); - if (available_at != NULL) { - if (available_at->type != cJSON_NULL) { - if ((ret = set_available_at(metadata, available_at->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_available_at(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + len += atclient_atkey_metadata_enc_key_name_strlen(metadata); } - cJSON *refresh_at = cJSON_GetObjectItem(json, "refreshAt"); - if (refresh_at != NULL) { - if (refresh_at->type != cJSON_NULL) { - if ((ret = set_refresh_at(metadata, refresh_at->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_refresh_at(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + len += atclient_atkey_metadata_enc_algo_strlen(metadata); } - cJSON *created_at = cJSON_GetObjectItem(json, "createdAt"); - if (created_at != NULL) { - if (created_at->type != cJSON_NULL) { - if ((ret = set_created_at(metadata, created_at->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_created_at(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + len += atclient_atkey_metadata_iv_nonce_strlen(metadata); } - cJSON *updated_at = cJSON_GetObjectItem(json, "updatedAt"); - if (updated_at != NULL) { - if (updated_at->type != cJSON_NULL) { - if ((ret = set_updated_at(metadata, updated_at->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_updated_at(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); - goto exit; - } - } + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + len += atclient_atkey_metadata_ske_enc_key_name_strlen(metadata); } - // I don't think this field exists when reading metadata from atServer - // cJSON *is_public = cJSON_GetObjectItem(root, "isPublic"); - // if(is_public != NULL) { - // set_is_public(metadata, is_public->valueint); - // } + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + len += atclient_atkey_metadata_ske_enc_algo_strlen(metadata); + } - // I don't think this field exists when reading metadata from atServer - // cJSON *is_cached = cJSON_GetObjectItem(root, "isCached - // if(is_cached != NULL) { - // set_is_cached(metadata, is_cached->valueint); - // } + return len; +} - cJSON *ttl = cJSON_GetObjectItem(json, "ttl"); - if (ttl != NULL) { - if (ttl->type != cJSON_NULL) { - set_ttl(metadata, ttl->valueint); - } else { - set_ttl(metadata, 0); - } +size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *ttb = cJSON_GetObjectItem(json, "ttb"); - if (ttb != NULL) { - if (ttb->type != cJSON_NULL) { - set_ttb(metadata, ttb->valueint); - } else { - set_ttb(metadata, 0); - } + if (!atclient_atkey_metadata_is_ttl_initialized(metadata)) { + return 0; } + return strlen(":ttl:") // :ttl: + + atclient_string_utils_int64_strlen(metadata->ttl); +} - cJSON *ttr = cJSON_GetObjectItem(json, "ttr"); - if (ttr != NULL) { - if (ttr->type != cJSON_NULL) { - set_ttr(metadata, ttr->valueint); - } else { - set_ttr(metadata, 0); - } +size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *ccd = cJSON_GetObjectItem(json, "ccd"); - if (ccd != NULL) { - if (ccd->type != cJSON_NULL) { - set_ccd(metadata, ccd->valueint); - } else { - set_ccd(metadata, false); - } + if (!atclient_atkey_metadata_is_ttb_initialized(metadata)) { + return 0; } + return strlen(":ttb:") // :ttb: + + atclient_string_utils_int64_strlen(metadata->ttb); +} - cJSON *is_binary = cJSON_GetObjectItem(json, "isBinary"); - if (is_binary != NULL) { - if (is_binary->type != cJSON_NULL) { - set_is_binary(metadata, is_binary->valueint); - } else { - set_is_binary(metadata, false); - } +size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *is_encrypted = cJSON_GetObjectItem(json, "isEncrypted"); - if (is_encrypted != NULL) { - if (is_encrypted->type != cJSON_NULL) { - set_is_encrypted(metadata, is_encrypted->valueint); - } else { - set_is_encrypted(metadata, false); - } + if (!atclient_atkey_metadata_is_ttr_initialized(metadata)) { + return 0; } + return strlen(":ttr:") // :ttr: + + atclient_string_utils_int64_strlen(metadata->ttr); +} - cJSON *data_signature = cJSON_GetObjectItem(json, "dataSignature"); - if (data_signature != NULL) { - if (data_signature->type != cJSON_NULL) { - if ((ret = set_data_signature(metadata, data_signature->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_data_signature(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); - goto exit; - } - } +size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *shared_key_status = cJSON_GetObjectItem(json, "sharedKeyStatus"); - if (shared_key_status != NULL) { - if (shared_key_status->type != cJSON_NULL) { - if ((ret = set_shared_key_status(metadata, shared_key_status->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_shared_key_status(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); - goto exit; - } - } + if (!atclient_atkey_metadata_is_ccd_initialized(metadata)) { + return 0; } - - cJSON *shared_key_enc = cJSON_GetObjectItem(json, "sharedKeyEnc"); - if (shared_key_enc != NULL) { - if (shared_key_enc->type != cJSON_NULL) { - if ((ret = set_shared_key_enc(metadata, shared_key_enc->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_shared_key_enc(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); - goto exit; - } - } + if (metadata->ccd) { + return strlen(":ccd:true"); // :ccd:true + } else { + return strlen(":ccd:false"); // :ccd:false } + return 0; +} - cJSON *pub_key_hash = cJSON_GetObjectItem(json, "pubKeyHash"); - if (pub_key_hash != NULL) { - if (pub_key_hash->type != cJSON_NULL) { - if ((ret = set_pub_key_hash(metadata, pub_key_hash->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_pub_key_hash(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); - goto exit; - } - } +size_t atclient_atkey_metadata_is_binary_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *pub_key_algo = cJSON_GetObjectItem(json, "pubKeyAlgo"); - if (pub_key_algo != NULL) { - if (pub_key_algo->type != cJSON_NULL) { - if ((ret = set_pubkeyalgo(metadata, pub_key_algo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_pubkeyalgo(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); - goto exit; - } - } + if (!atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + return 0; } - - cJSON *encoding = cJSON_GetObjectItem(json, "encoding"); - if (encoding != NULL) { - if (encoding->type != cJSON_NULL) { - if ((ret = set_encoding(metadata, encoding->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_encoding(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); - goto exit; - } - } + if (metadata->is_binary) { + return strlen(":isBinary:true"); + } else { + return strlen(":isBinary:false"); } +} - cJSON *enc_key_name = cJSON_GetObjectItem(json, "encKeyName"); - if (enc_key_name != NULL) { - if (enc_key_name->type != cJSON_NULL) { - if ((ret = set_enc_key_name(metadata, enc_key_name->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_enc_key_name(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); - goto exit; - } - } +size_t atclient_atkey_metadata_is_encrypted_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *enc_algo = cJSON_GetObjectItem(json, "encAlgo"); - if (enc_algo != NULL) { - if (enc_algo->type != cJSON_NULL) { - if ((ret = set_enc_algo(metadata, enc_algo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_enc_algo(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); - goto exit; - } - } + if (!atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + return 0; } - - cJSON *iv_nonce = cJSON_GetObjectItem(json, "ivNonce"); - if (iv_nonce != NULL) { - if (iv_nonce->type != cJSON_NULL) { - if ((ret = set_iv_nonce(metadata, iv_nonce->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_iv_nonce(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); - goto exit; - } - } + if (metadata->is_encrypted) { + return strlen(":isEncrypted:true"); + } else { + return strlen(":isEncrypted:false"); } +} - cJSON *ske_enc_key_name = cJSON_GetObjectItem(json, "skeEncKeyName"); - if (ske_enc_key_name != NULL) { - if (ske_enc_key_name->type != cJSON_NULL) { - if ((ret = set_ske_enc_key_name(metadata, ske_enc_key_name->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_ske_enc_key_name(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); - goto exit; - } - } +size_t atclient_atkey_metadata_data_signature_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - cJSON *ske_enc_algo = cJSON_GetObjectItem(json, "skeEncAlgo"); - if (ske_enc_algo != NULL) { - if (ske_enc_algo->type != cJSON_NULL) { - if ((ret = set_ske_enc_algo(metadata, ske_enc_algo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); - goto exit; - } - } else { - if ((ret = set_ske_enc_algo(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); - goto exit; - } - } + if (!atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + return 0; } - - ret = 0; - goto exit; -exit: { return ret; } + return strlen(":dataSignature:") + strlen(metadata->data_signature); } -int atclient_atkey_metadata_to_json_str(const atclient_atkey_metadata *metadata, char **metadata_str) { - int ret = 1; - - /* - * 1. Validate arguments - */ +size_t atclient_atkey_metadata_shared_key_status_strlen(const atclient_atkey_metadata *metadata) { if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - return ret; - } - - if (metadata_str == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); - return ret; + return 0; } - - /* - * 2. Create JSON string - */ - char *json_str = NULL; - cJSON *root = cJSON_CreateObject(); - if (root == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_CreateObject failed\n"); - goto exit; + if (!atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + return 0; } + return strlen(":sharedKeyStatus:") + strlen(metadata->shared_key_status); +} - if (atclient_atkey_metadata_is_created_by_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdBy", metadata->created_by); +size_t atclient_atkey_metadata_shared_key_enc_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_updated_by_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedBy", metadata->updated_by); + if (!atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + return 0; } + return strlen(":sharedKeyEnc:") + strlen(metadata->shared_key_enc); +} - if (atclient_atkey_metadata_is_status_initialized(metadata)) { - cJSON_AddStringToObject(root, "status", metadata->status); - } - - if (atclient_atkey_metadata_is_version_initialized(metadata)) { - cJSON_AddNumberToObject(root, "version", metadata->version); - } - - if (atclient_atkey_metadata_is_expires_at_initialized(metadata)) { - cJSON_AddStringToObject(root, "expiresAt", metadata->expires_at); - } - - if (atclient_atkey_metadata_is_available_at_initialized(metadata)) { - cJSON_AddStringToObject(root, "availableAt", metadata->available_at); - } - - if (atclient_atkey_metadata_is_refresh_at_initialized(metadata)) { - cJSON_AddStringToObject(root, "refreshAt", metadata->refresh_at); - } - - if (atclient_atkey_metadata_is_created_at_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdAt", metadata->created_at); - } - - if (atclient_atkey_metadata_is_updated_at_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedAt", metadata->updated_at); +size_t atclient_atkey_metadata_pub_key_hash_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_is_public_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isPublic", metadata->is_public); + if (!atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + return 0; } + return strlen(":hash:") + strlen(metadata->pub_key_hash); +} - if (atclient_atkey_metadata_is_is_cached_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isCached", metadata->is_cached); +size_t atclient_atkey_metadata_pub_key_algo_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { - cJSON_AddNumberToObject(root, "ttl", metadata->ttl); + if (!atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + return 0; } + return strlen(":algo:") + strlen(metadata->pub_key_algo); +} - if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { - cJSON_AddNumberToObject(root, "ttb", metadata->ttb); +size_t atclient_atkey_metadata_encoding_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { - cJSON_AddNumberToObject(root, "ttr", metadata->ttr); + if (!atclient_atkey_metadata_is_encoding_initialized(metadata)) { + return 0; } + return strlen(":encoding:") + strlen(metadata->encoding); +} - if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { - cJSON_AddBoolToObject(root, "ccd", metadata->ccd); +size_t atclient_atkey_metadata_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isBinary", metadata->is_binary); + if (!atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + return 0; } + return strlen(":encKeyName:") + strlen(metadata->enc_key_name); +} - if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isEncrypted", metadata->is_encrypted); +size_t atclient_atkey_metadata_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { - cJSON_AddStringToObject(root, "dataSignature", metadata->data_signature); + if (!atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + return 0; } + return strlen(":encAlgo:") + strlen(metadata->enc_algo); +} - if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->shared_key_status); +size_t atclient_atkey_metadata_iv_nonce_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->shared_key_enc); + if (!atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + return 0; } + return strlen(":ivNonce:") + strlen(metadata->iv_nonce); +} - if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyHash", metadata->pub_key_hash); +size_t atclient_atkey_metadata_ske_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pub_key_algo); + if (!atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + return 0; } + return strlen(":skeEncKeyName:") + strlen(metadata->ske_enc_key_name); +} - if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - cJSON_AddStringToObject(root, "encoding", metadata->encoding); +size_t atclient_atkey_metadata_ske_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; } - - if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { - cJSON_AddStringToObject(root, "encKeyName", metadata->enc_key_name); + if (!atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + return 0; } + return strlen(":skeEncAlgo:") + strlen(metadata->ske_enc_algo); +} - if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { - cJSON_AddStringToObject(root, "encAlgo", metadata->enc_algo); - } +int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadata_str) { + int ret = 1; - if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { - cJSON_AddStringToObject(root, "ivNonce", metadata->iv_nonce); + /* + * 1. Validate arguments + */ + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; } - if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncKeyName", metadata->ske_enc_key_name); + if (metadata_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); + return ret; } - if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncAlgo", metadata->ske_enc_algo); - } + /* + * 2. Create protocol string + */ + const size_t expected_metadatastr_len = atclient_atkey_metadata_protocol_strlen(metadata); + const size_t metadata_str_size = expected_metadatastr_len + 1; - json_str = cJSON_Print(root); - if (json_str == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print failed\n"); - goto exit; - } + size_t pos = 0; - const size_t metadata_str_size = strlen(json_str) + 1; - *metadata_str = (char *)malloc(sizeof(char) * metadata_str_size); - if (*metadata_str == NULL) { + if ((*metadata_str = malloc(sizeof(char) * metadata_str_size)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } - memcpy(*metadata_str, json_str, strlen(json_str)); - (*metadata_str)[strlen(json_str)] = '\0'; - - ret = 0; - goto exit; -exit: { - free(json_str); - cJSON_Delete(root); - return ret; -} -} - -size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *metadata) { - /* - * 1. Validate arguments - */ - if (metadata == NULL) { - return 0; - } + memset(*metadata_str, 0, sizeof(char) * metadata_str_size); - /* - * 2. Calculate length - */ - int64_t len = 0; if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { - len += atclient_atkey_metadata_ttl_strlen(metadata); + sprintf((*metadata_str) + pos, ":ttl:%" PRId64, metadata->ttl); + pos += atclient_atkey_metadata_ttl_strlen(metadata); } if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { - len += atclient_atkey_metadata_ttb_strlen(metadata); + sprintf((*metadata_str) + pos, ":ttb:%" PRId64, metadata->ttb); + pos += atclient_atkey_metadata_ttb_strlen(metadata); } if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { - len += atclient_atkey_metadata_ttr_strlen(metadata); + sprintf((*metadata_str) + pos, ":ttr:%" PRId64, metadata->ttr); + pos += atclient_atkey_metadata_ttr_strlen(metadata); } if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { - len += atclient_atkey_metadata_ccd_strlen(metadata); + if (metadata->ccd) { + sprintf((*metadata_str) + pos, ":ccd:true"); + } else { + sprintf((*metadata_str) + pos, ":ccd:false"); + } + pos += atclient_atkey_metadata_ccd_strlen(metadata); } if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { - len += atclient_atkey_metadata_is_binary_strlen(metadata); + if (metadata->is_binary) { + sprintf((*metadata_str) + pos, ":isBinary:true"); + } else { + sprintf((*metadata_str) + pos, ":isBinary:false"); + } + pos += atclient_atkey_metadata_is_binary_strlen(metadata); } if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { - len += atclient_atkey_metadata_is_encrypted_strlen(metadata); + if (metadata->is_encrypted) { + sprintf((*metadata_str) + pos, ":isEncrypted:true"); + } else { + sprintf((*metadata_str) + pos, ":isEncrypted:false"); + } + pos += atclient_atkey_metadata_is_encrypted_strlen(metadata); } if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { - len += atclient_atkey_metadata_data_signature_strlen(metadata); + sprintf((*metadata_str) + pos, ":dataSignature:%s", metadata->data_signature); + pos += atclient_atkey_metadata_data_signature_strlen(metadata); } if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { - len += atclient_atkey_metadata_shared_key_status_strlen(metadata); + sprintf((*metadata_str) + pos, ":sharedKeyStatus:%s", metadata->shared_key_status); + pos += atclient_atkey_metadata_shared_key_status_strlen(metadata); } if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { - len += atclient_atkey_metadata_shared_key_enc_strlen(metadata); + sprintf((*metadata_str) + pos, ":sharedKeyEnc:%s", metadata->shared_key_enc); + pos += atclient_atkey_metadata_shared_key_enc_strlen(metadata); } if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { - len += atclient_atkey_metadata_pub_key_hash_strlen(metadata); + sprintf((*metadata_str) + pos, ":hash:%s", metadata->pub_key_hash); + pos += atclient_atkey_metadata_pub_key_hash_strlen(metadata); } if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { - len += atclient_atkey_metadata_pub_key_algo_strlen(metadata); + sprintf((*metadata_str) + pos, ":algo:%s", metadata->pub_key_algo); + pos += atclient_atkey_metadata_pub_key_algo_strlen(metadata); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - len += atclient_atkey_metadata_encoding_strlen(metadata); + sprintf((*metadata_str) + pos, ":encoding:%s", metadata->encoding); + pos += atclient_atkey_metadata_encoding_strlen(metadata); } if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { - len += atclient_atkey_metadata_enc_key_name_strlen(metadata); + sprintf((*metadata_str) + pos, ":encKeyName:%s", metadata->enc_key_name); + pos += atclient_atkey_metadata_enc_key_name_strlen(metadata); } if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { - len += atclient_atkey_metadata_enc_algo_strlen(metadata); + sprintf((*metadata_str) + pos, ":encAlgo:%s", metadata->enc_algo); + pos += atclient_atkey_metadata_enc_algo_strlen(metadata); } if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { - len += atclient_atkey_metadata_iv_nonce_strlen(metadata); + sprintf((*metadata_str) + pos, ":ivNonce:%s", metadata->iv_nonce); + pos += atclient_atkey_metadata_iv_nonce_strlen(metadata); } if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { - len += atclient_atkey_metadata_ske_enc_key_name_strlen(metadata); + sprintf((*metadata_str) + pos, ":skeEncKeyName:%s", metadata->ske_enc_key_name); + pos += atclient_atkey_metadata_ske_enc_key_name_strlen(metadata); } if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { - len += atclient_atkey_metadata_ske_enc_algo_strlen(metadata); + sprintf((*metadata_str) + pos, ":skeEncAlgo:%s", metadata->ske_enc_algo); + pos += atclient_atkey_metadata_ske_enc_algo_strlen(metadata); } + /* + * 3. Do a sanity check + */ - return len; -} + if (strlen(*metadata_str) != expected_metadatastr_len) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "metadata_str length mismatch: %lu != %lu\nmetadata:", strlen(*metadata_str), + (expected_metadatastr_len)); -size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ttl_initialized(metadata)) { - return 0; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str: %s\n", (*metadata_str)); + goto exit; } - return strlen(":ttl:") // :ttl: - + atclient_string_utils_int64_strlen(metadata->ttl); + + ret = 0; + goto exit; +exit: { return ret; } } -size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ttb_initialized(metadata)) { - return 0; - } - return strlen(":ttb:") // :ttb: - + atclient_string_utils_int64_strlen(metadata->ttb); +bool atclient_atkey_metadata_is_created_by_initialized(const atclient_atkey_metadata *metadata) { + return is_created_by_initialized(metadata); } -size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ttr_initialized(metadata)) { - return 0; - } - return strlen(":ttr:") // :ttr: - + atclient_string_utils_int64_strlen(metadata->ttr); +bool atclient_atkey_metadata_is_updated_by_initialized(const atclient_atkey_metadata *metadata) { + return is_updated_by_initialized(metadata); } -size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ccd_initialized(metadata)) { - return 0; - } - if (metadata->ccd) { - return strlen(":ccd:true"); // :ccd:true - } else { - return strlen(":ccd:false"); // :ccd:false - } - return 0; +bool atclient_atkey_metadata_is_status_initialized(const atclient_atkey_metadata *metadata) { + return is_status_initialized(metadata); } -size_t atclient_atkey_metadata_is_binary_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_is_binary_initialized(metadata)) { - return 0; - } - if (metadata->is_binary) { - return strlen(":isBinary:true"); - } else { - return strlen(":isBinary:false"); - } +bool atclient_atkey_metadata_is_version_initialized(const atclient_atkey_metadata *metadata) { + return is_version_initialized(metadata); } -size_t atclient_atkey_metadata_is_encrypted_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { - return 0; - } - if (metadata->is_encrypted) { - return strlen(":isEncrypted:true"); - } else { - return strlen(":isEncrypted:false"); - } +bool atclient_atkey_metadata_is_available_at_initialized(const atclient_atkey_metadata *metadata) { + return is_available_at_initialized(metadata); } -size_t atclient_atkey_metadata_data_signature_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_data_signature_initialized(metadata)) { - return 0; - } - return strlen(":dataSignature:") + strlen(metadata->data_signature); +bool atclient_atkey_metadata_is_expires_at_initialized(const atclient_atkey_metadata *metadata) { + return is_expires_at_initialized(metadata); } -size_t atclient_atkey_metadata_shared_key_status_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { - return 0; - } - return strlen(":sharedKeyStatus:") + strlen(metadata->shared_key_status); +bool atclient_atkey_metadata_is_refresh_at_initialized(const atclient_atkey_metadata *metadata) { + return is_refresh_at_initialized(metadata); } -size_t atclient_atkey_metadata_shared_key_enc_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { - return 0; - } - return strlen(":sharedKeyEnc:") + strlen(metadata->shared_key_enc); +bool atclient_atkey_metadata_is_created_at_initialized(const atclient_atkey_metadata *metadata) { + return is_created_at_initialized(metadata); } -size_t atclient_atkey_metadata_pub_key_hash_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { - return 0; - } - return strlen(":hash:") + strlen(metadata->pub_key_hash); +bool atclient_atkey_metadata_is_updated_at_initialized(const atclient_atkey_metadata *metadata) { + return is_updated_at_initialized(metadata); } -size_t atclient_atkey_metadata_pub_key_algo_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { - return 0; - } - return strlen(":algo:") + strlen(metadata->pub_key_algo); +bool atclient_atkey_metadata_is_is_public_initialized(const atclient_atkey_metadata *metadata) { + return is_is_public_initialized(metadata); } -size_t atclient_atkey_metadata_encoding_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_encoding_initialized(metadata)) { - return 0; - } - return strlen(":encoding:") + strlen(metadata->encoding); +bool atclient_atkey_metadata_is_is_cached_initialized(const atclient_atkey_metadata *metadata) { + return is_is_cached_initialized(metadata); } -size_t atclient_atkey_metadata_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { - return 0; - } - return strlen(":encKeyName:") + strlen(metadata->enc_key_name); +bool atclient_atkey_metadata_is_ttl_initialized(const atclient_atkey_metadata *metadata) { + return is_ttl_initialized(metadata); } -size_t atclient_atkey_metadata_enc_algo_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { - return 0; - } - return strlen(":encAlgo:") + strlen(metadata->enc_algo); +bool atclient_atkey_metadata_is_ttb_initialized(const atclient_atkey_metadata *metadata) { + return is_ttb_initialized(metadata); } -size_t atclient_atkey_metadata_iv_nonce_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { - return 0; - } - return strlen(":ivNonce:") + strlen(metadata->iv_nonce); +bool atclient_atkey_metadata_is_ttr_initialized(const atclient_atkey_metadata *metadata) { + return is_ttr_initialized(metadata); } -size_t atclient_atkey_metadata_ske_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { - return 0; - } - return strlen(":skeEncKeyName:") + strlen(metadata->ske_enc_key_name); +bool atclient_atkey_metadata_is_ccd_initialized(const atclient_atkey_metadata *metadata) { + return is_ccd_initialized(metadata); } -size_t atclient_atkey_metadata_ske_enc_algo_strlen(const atclient_atkey_metadata *metadata) { - if (metadata == NULL) { - return 0; - } - if (!atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { - return 0; - } - return strlen(":skeEncAlgo:") + strlen(metadata->ske_enc_algo); +bool atclient_atkey_metadata_is_is_binary_initialized(const atclient_atkey_metadata *metadata) { + return is_is_binary_initialized(metadata); } -int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadata_str) { - int ret = 1; +bool atclient_atkey_metadata_is_is_encrypted_initialized(const atclient_atkey_metadata *metadata) { + return is_is_encrypted_initialized(metadata); +} - /* - * 1. Validate arguments - */ - if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - return ret; - } +bool atclient_atkey_metadata_is_data_signature_initialized(const atclient_atkey_metadata *metadata) { + return is_data_signature_initialized(metadata); +} - if (metadata_str == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); - return ret; - } +bool atclient_atkey_metadata_is_shared_key_status_initialized(const atclient_atkey_metadata *metadata) { + return is_shared_key_status_initialized(metadata); +} - /* - * 2. Create protocol string - */ - const size_t expected_metadatastr_len = atclient_atkey_metadata_protocol_strlen(metadata); - const size_t metadata_str_size = expected_metadatastr_len + 1; +bool atclient_atkey_metadata_is_shared_key_enc_initialized(const atclient_atkey_metadata *metadata) { + return is_shared_key_enc_initialized(metadata); +} - size_t pos = 0; +bool atclient_atkey_metadata_is_pub_key_hash_initialized(const atclient_atkey_metadata *metadata) { + return is_pub_key_hash_initialized(metadata); +} - if ((*metadata_str = malloc(sizeof(char) * metadata_str_size)) == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - memset(*metadata_str, 0, sizeof(char) * metadata_str_size); +bool atclient_atkey_metadata_is_pub_key_algo_initialized(const atclient_atkey_metadata *metadata) { + return is_pub_key_algo_initialized(metadata); +} - if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":ttl:%" PRId64, metadata->ttl); - pos += atclient_atkey_metadata_ttl_strlen(metadata); - } +bool atclient_atkey_metadata_is_encoding_initialized(const atclient_atkey_metadata *metadata) { + return is_encoding_initialized(metadata); +} - if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":ttb:%" PRId64, metadata->ttb); - pos += atclient_atkey_metadata_ttb_strlen(metadata); - } +bool atclient_atkey_metadata_is_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { + return is_enc_key_name_initialized(metadata); +} - if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":ttr:%" PRId64, metadata->ttr); - pos += atclient_atkey_metadata_ttr_strlen(metadata); - } - - if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { - if (metadata->ccd) { - sprintf((*metadata_str) + pos, ":ccd:true"); - } else { - sprintf((*metadata_str) + pos, ":ccd:false"); - } - pos += atclient_atkey_metadata_ccd_strlen(metadata); - } - - if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { - if (metadata->is_binary) { - sprintf((*metadata_str) + pos, ":isBinary:true"); - } else { - sprintf((*metadata_str) + pos, ":isBinary:false"); - } - pos += atclient_atkey_metadata_is_binary_strlen(metadata); - } - - if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { - if (metadata->is_encrypted) { - sprintf((*metadata_str) + pos, ":isEncrypted:true"); - } else { - sprintf((*metadata_str) + pos, ":isEncrypted:false"); - } - pos += atclient_atkey_metadata_is_encrypted_strlen(metadata); - } - - if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":dataSignature:%s", metadata->data_signature); - pos += atclient_atkey_metadata_data_signature_strlen(metadata); - } - - if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":sharedKeyStatus:%s", metadata->shared_key_status); - pos += atclient_atkey_metadata_shared_key_status_strlen(metadata); - } - - if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":sharedKeyEnc:%s", metadata->shared_key_enc); - pos += atclient_atkey_metadata_shared_key_enc_strlen(metadata); - } - - if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":hash:%s", metadata->pub_key_hash); - pos += atclient_atkey_metadata_pub_key_hash_strlen(metadata); - } - - if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":algo:%s", metadata->pub_key_algo); - pos += atclient_atkey_metadata_pub_key_algo_strlen(metadata); - } - - if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":encoding:%s", metadata->encoding); - pos += atclient_atkey_metadata_encoding_strlen(metadata); - } - - if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":encKeyName:%s", metadata->enc_key_name); - pos += atclient_atkey_metadata_enc_key_name_strlen(metadata); - } - - if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":encAlgo:%s", metadata->enc_algo); - pos += atclient_atkey_metadata_enc_algo_strlen(metadata); - } - - if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":ivNonce:%s", metadata->iv_nonce); - pos += atclient_atkey_metadata_iv_nonce_strlen(metadata); - } - - if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":skeEncKeyName:%s", metadata->ske_enc_key_name); - pos += atclient_atkey_metadata_ske_enc_key_name_strlen(metadata); - } - - if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { - sprintf((*metadata_str) + pos, ":skeEncAlgo:%s", metadata->ske_enc_algo); - pos += atclient_atkey_metadata_ske_enc_algo_strlen(metadata); - } - /* - * 3. Do a sanity check - */ - - if (strlen(*metadata_str) != expected_metadatastr_len) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "metadata_str length mismatch: %lu != %lu\nmetadata:", strlen(*metadata_str), - (expected_metadatastr_len)); - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str: %s\n", (*metadata_str)); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} - -bool atclient_atkey_metadata_is_created_by_initialized(const atclient_atkey_metadata *metadata) { - return is_created_by_initialized(metadata); -} - -bool atclient_atkey_metadata_is_updated_by_initialized(const atclient_atkey_metadata *metadata) { - return is_updated_by_initialized(metadata); -} - -bool atclient_atkey_metadata_is_status_initialized(const atclient_atkey_metadata *metadata) { - return is_status_initialized(metadata); -} - -bool atclient_atkey_metadata_is_version_initialized(const atclient_atkey_metadata *metadata) { - return is_version_initialized(metadata); -} - -bool atclient_atkey_metadata_is_available_at_initialized(const atclient_atkey_metadata *metadata) { - return is_available_at_initialized(metadata); -} - -bool atclient_atkey_metadata_is_expires_at_initialized(const atclient_atkey_metadata *metadata) { - return is_expires_at_initialized(metadata); -} - -bool atclient_atkey_metadata_is_refresh_at_initialized(const atclient_atkey_metadata *metadata) { - return is_refresh_at_initialized(metadata); -} - -bool atclient_atkey_metadata_is_created_at_initialized(const atclient_atkey_metadata *metadata) { - return is_created_at_initialized(metadata); -} - -bool atclient_atkey_metadata_is_updated_at_initialized(const atclient_atkey_metadata *metadata) { - return is_updated_at_initialized(metadata); -} - -bool atclient_atkey_metadata_is_is_public_initialized(const atclient_atkey_metadata *metadata) { - return is_is_public_initialized(metadata); -} - -bool atclient_atkey_metadata_is_is_cached_initialized(const atclient_atkey_metadata *metadata) { - return is_is_cached_initialized(metadata); -} - -bool atclient_atkey_metadata_is_ttl_initialized(const atclient_atkey_metadata *metadata) { - return is_ttl_initialized(metadata); -} - -bool atclient_atkey_metadata_is_ttb_initialized(const atclient_atkey_metadata *metadata) { - return is_ttb_initialized(metadata); -} - -bool atclient_atkey_metadata_is_ttr_initialized(const atclient_atkey_metadata *metadata) { - return is_ttr_initialized(metadata); -} - -bool atclient_atkey_metadata_is_ccd_initialized(const atclient_atkey_metadata *metadata) { - return is_ccd_initialized(metadata); -} - -bool atclient_atkey_metadata_is_is_binary_initialized(const atclient_atkey_metadata *metadata) { - return is_is_binary_initialized(metadata); -} - -bool atclient_atkey_metadata_is_is_encrypted_initialized(const atclient_atkey_metadata *metadata) { - return is_is_encrypted_initialized(metadata); -} - -bool atclient_atkey_metadata_is_data_signature_initialized(const atclient_atkey_metadata *metadata) { - return is_data_signature_initialized(metadata); -} - -bool atclient_atkey_metadata_is_shared_key_status_initialized(const atclient_atkey_metadata *metadata) { - return is_shared_key_status_initialized(metadata); -} - -bool atclient_atkey_metadata_is_shared_key_enc_initialized(const atclient_atkey_metadata *metadata) { - return is_shared_key_enc_initialized(metadata); -} - -bool atclient_atkey_metadata_is_pub_key_hash_initialized(const atclient_atkey_metadata *metadata) { - return is_pub_key_hash_initialized(metadata); -} - -bool atclient_atkey_metadata_is_pub_key_algo_initialized(const atclient_atkey_metadata *metadata) { - return is_pub_key_algo_initialized(metadata); -} - -bool atclient_atkey_metadata_is_encoding_initialized(const atclient_atkey_metadata *metadata) { - return is_encoding_initialized(metadata); -} - -bool atclient_atkey_metadata_is_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { - return is_enc_key_name_initialized(metadata); -} - -bool atclient_atkey_metadata_is_enc_algo_initialized(const atclient_atkey_metadata *metadata) { - return is_enc_algo_initialized(metadata); -} +bool atclient_atkey_metadata_is_enc_algo_initialized(const atclient_atkey_metadata *metadata) { + return is_enc_algo_initialized(metadata); +} bool atclient_atkey_metadata_is_iv_nonce_initialized(const atclient_atkey_metadata *metadata) { return is_iv_nonce_initialized(metadata); @@ -2399,7 +1811,6 @@ exit: { return ret; } static int set_created_at(atclient_atkey_metadata *metadata, const char *created_at) { int ret = 1; const size_t created_at_len = strlen(created_at); - const size_t created_at_size = created_at_len + 1; if ((metadata->created_at = malloc(sizeof(char) * (created_at_len + 1))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at malloc failed\n"); @@ -2656,3 +2067,599 @@ static int set_ske_enc_algo(atclient_atkey_metadata *metadata, const char *ske_e goto exit; exit: { return ret; } } + +// Json library specific +#if ATCLIENT_JSON_PROVIDER == ATCLIENT_JSON_PROVIDER_CJSON +int atclient_atkey_metadata_from_json_str(atclient_atkey_metadata *metadata, const char *metadata_str) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; + } + + if (metadata_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); + return ret; + } + + /* + * 2. Parse JSON string + */ + cJSON *root = cJSON_Parse(metadata_str); + if (root == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n"); + goto exit; + } + + if ((ret = atclient_atkey_metadata_from_cjson_node(metadata, root)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_cjson_node: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { + cJSON_Delete(root); + return ret; +} +} + +int atclient_atkey_metadata_to_json_str(const atclient_atkey_metadata *metadata, char **metadata_str) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; + } + + if (metadata_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); + return ret; + } + + /* + * 2. Create JSON string + */ + char *json_str = NULL; + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_CreateObject failed\n"); + goto exit; + } + + if (atclient_atkey_metadata_is_created_by_initialized(metadata)) { + cJSON_AddStringToObject(root, "createdBy", metadata->created_by); + } + + if (atclient_atkey_metadata_is_updated_by_initialized(metadata)) { + cJSON_AddStringToObject(root, "updatedBy", metadata->updated_by); + } + + if (atclient_atkey_metadata_is_status_initialized(metadata)) { + cJSON_AddStringToObject(root, "status", metadata->status); + } + + if (atclient_atkey_metadata_is_version_initialized(metadata)) { + cJSON_AddNumberToObject(root, "version", metadata->version); + } + + if (atclient_atkey_metadata_is_expires_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "expiresAt", metadata->expires_at); + } + + if (atclient_atkey_metadata_is_available_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "availableAt", metadata->available_at); + } + + if (atclient_atkey_metadata_is_refresh_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "refreshAt", metadata->refresh_at); + } + + if (atclient_atkey_metadata_is_created_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "createdAt", metadata->created_at); + } + + if (atclient_atkey_metadata_is_updated_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "updatedAt", metadata->updated_at); + } + + if (atclient_atkey_metadata_is_is_public_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isPublic", metadata->is_public); + } + + if (atclient_atkey_metadata_is_is_cached_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isCached", metadata->is_cached); + } + + if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { + cJSON_AddNumberToObject(root, "ttl", metadata->ttl); + } + + if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { + cJSON_AddNumberToObject(root, "ttb", metadata->ttb); + } + + if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { + cJSON_AddNumberToObject(root, "ttr", metadata->ttr); + } + + if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { + cJSON_AddBoolToObject(root, "ccd", metadata->ccd); + } + + if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isBinary", metadata->is_binary); + } + + if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isEncrypted", metadata->is_encrypted); + } + + if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + cJSON_AddStringToObject(root, "dataSignature", metadata->data_signature); + } + + if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->shared_key_status); + } + + if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->shared_key_enc); + } + + if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + cJSON_AddStringToObject(root, "pubKeyHash", metadata->pub_key_hash); + } + + if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pub_key_algo); + } + + if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { + cJSON_AddStringToObject(root, "encoding", metadata->encoding); + } + + if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + cJSON_AddStringToObject(root, "encKeyName", metadata->enc_key_name); + } + + if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "encAlgo", metadata->enc_algo); + } + + if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + cJSON_AddStringToObject(root, "ivNonce", metadata->iv_nonce); + } + + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + cJSON_AddStringToObject(root, "skeEncKeyName", metadata->ske_enc_key_name); + } + + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "skeEncAlgo", metadata->ske_enc_algo); + } + + json_str = cJSON_Print(root); + if (json_str == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print failed\n"); + goto exit; + } + + const size_t metadata_str_size = strlen(json_str) + 1; + *metadata_str = (char *)malloc(sizeof(char) * metadata_str_size); + if (*metadata_str == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + memcpy(*metadata_str, json_str, strlen(json_str)); + (*metadata_str)[strlen(json_str)] = '\0'; + + ret = 0; + goto exit; +exit: { + free(json_str); + cJSON_Delete(root); + return ret; +} +} + +static int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json) { + int ret = 1; + + /* + * 1. Validate arguments + */ + + if (json == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "json is NULL\n"); + return ret; + } + + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; + } + + /* + * 2. Parse JSON node + */ + + cJSON *created_by = cJSON_GetObjectItem(json, "createdBy"); + if (created_by != NULL) { + if (created_by->type != cJSON_NULL) { + if ((ret = set_created_by(metadata, created_by->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_created_by(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); + goto exit; + } + } + } + + cJSON *updated_by = cJSON_GetObjectItem(json, "updatedBy"); + if (updated_by != NULL) { + if (updated_by->type != cJSON_NULL) { + if ((ret = set_updated_by(metadata, updated_by->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_updated_by(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); + goto exit; + } + } + } + + cJSON *status = cJSON_GetObjectItem(json, "status"); + if (status != NULL) { + if (status->type != cJSON_NULL) { + if ((ret = set_status(metadata, status->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_status(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); + goto exit; + } + } + } + + cJSON *version = cJSON_GetObjectItem(json, "version"); + if (version != NULL) { + if (version->type != cJSON_NULL) { + set_version(metadata, version->valueint); + } else { + set_version(metadata, 0); + } + } + + cJSON *expires_at = cJSON_GetObjectItem(json, "expiresAt"); + if (expires_at != NULL) { + if (expires_at->type != cJSON_NULL) { + if ((ret = set_expires_at(metadata, expires_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_expires_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); + goto exit; + } + } + } + + cJSON *available_at = cJSON_GetObjectItem(json, "availableAt"); + if (available_at != NULL) { + if (available_at->type != cJSON_NULL) { + if ((ret = set_available_at(metadata, available_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_available_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); + goto exit; + } + } + } + + cJSON *refresh_at = cJSON_GetObjectItem(json, "refreshAt"); + if (refresh_at != NULL) { + if (refresh_at->type != cJSON_NULL) { + if ((ret = set_refresh_at(metadata, refresh_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_refresh_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); + goto exit; + } + } + } + + cJSON *created_at = cJSON_GetObjectItem(json, "createdAt"); + if (created_at != NULL) { + if (created_at->type != cJSON_NULL) { + if ((ret = set_created_at(metadata, created_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_created_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); + goto exit; + } + } + } + + cJSON *updated_at = cJSON_GetObjectItem(json, "updatedAt"); + if (updated_at != NULL) { + if (updated_at->type != cJSON_NULL) { + if ((ret = set_updated_at(metadata, updated_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_updated_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); + goto exit; + } + } + } + + // I don't think this field exists when reading metadata from atServer + // cJSON *is_public = cJSON_GetObjectItem(root, "isPublic"); + // if(is_public != NULL) { + // set_is_public(metadata, is_public->valueint); + // } + + // I don't think this field exists when reading metadata from atServer + // cJSON *is_cached = cJSON_GetObjectItem(root, "isCached + // if(is_cached != NULL) { + // set_is_cached(metadata, is_cached->valueint); + // } + + cJSON *ttl = cJSON_GetObjectItem(json, "ttl"); + if (ttl != NULL) { + if (ttl->type != cJSON_NULL) { + set_ttl(metadata, ttl->valueint); + } else { + set_ttl(metadata, 0); + } + } + + cJSON *ttb = cJSON_GetObjectItem(json, "ttb"); + if (ttb != NULL) { + if (ttb->type != cJSON_NULL) { + set_ttb(metadata, ttb->valueint); + } else { + set_ttb(metadata, 0); + } + } + + cJSON *ttr = cJSON_GetObjectItem(json, "ttr"); + if (ttr != NULL) { + if (ttr->type != cJSON_NULL) { + set_ttr(metadata, ttr->valueint); + } else { + set_ttr(metadata, 0); + } + } + + cJSON *ccd = cJSON_GetObjectItem(json, "ccd"); + if (ccd != NULL) { + if (ccd->type != cJSON_NULL) { + set_ccd(metadata, ccd->valueint); + } else { + set_ccd(metadata, false); + } + } + + cJSON *is_binary = cJSON_GetObjectItem(json, "isBinary"); + if (is_binary != NULL) { + if (is_binary->type != cJSON_NULL) { + set_is_binary(metadata, is_binary->valueint); + } else { + set_is_binary(metadata, false); + } + } + + cJSON *is_encrypted = cJSON_GetObjectItem(json, "isEncrypted"); + if (is_encrypted != NULL) { + if (is_encrypted->type != cJSON_NULL) { + set_is_encrypted(metadata, is_encrypted->valueint); + } else { + set_is_encrypted(metadata, false); + } + } + + cJSON *data_signature = cJSON_GetObjectItem(json, "dataSignature"); + if (data_signature != NULL) { + if (data_signature->type != cJSON_NULL) { + if ((ret = set_data_signature(metadata, data_signature->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_data_signature(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); + goto exit; + } + } + } + + cJSON *shared_key_status = cJSON_GetObjectItem(json, "sharedKeyStatus"); + if (shared_key_status != NULL) { + if (shared_key_status->type != cJSON_NULL) { + if ((ret = set_shared_key_status(metadata, shared_key_status->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_shared_key_status(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); + goto exit; + } + } + } + + cJSON *shared_key_enc = cJSON_GetObjectItem(json, "sharedKeyEnc"); + if (shared_key_enc != NULL) { + if (shared_key_enc->type != cJSON_NULL) { + if ((ret = set_shared_key_enc(metadata, shared_key_enc->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_shared_key_enc(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); + goto exit; + } + } + } + + cJSON *pub_key_hash = cJSON_GetObjectItem(json, "pubKeyHash"); + if (pub_key_hash != NULL) { + if (pub_key_hash->type != cJSON_NULL) { + if ((ret = set_pub_key_hash(metadata, pub_key_hash->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_pub_key_hash(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); + goto exit; + } + } + } + + cJSON *pub_key_algo = cJSON_GetObjectItem(json, "pubKeyAlgo"); + if (pub_key_algo != NULL) { + if (pub_key_algo->type != cJSON_NULL) { + if ((ret = set_pubkeyalgo(metadata, pub_key_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_pubkeyalgo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); + goto exit; + } + } + } + + cJSON *encoding = cJSON_GetObjectItem(json, "encoding"); + if (encoding != NULL) { + if (encoding->type != cJSON_NULL) { + if ((ret = set_encoding(metadata, encoding->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_encoding(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); + goto exit; + } + } + } + + cJSON *enc_key_name = cJSON_GetObjectItem(json, "encKeyName"); + if (enc_key_name != NULL) { + if (enc_key_name->type != cJSON_NULL) { + if ((ret = set_enc_key_name(metadata, enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_enc_key_name(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); + goto exit; + } + } + } + + cJSON *enc_algo = cJSON_GetObjectItem(json, "encAlgo"); + if (enc_algo != NULL) { + if (enc_algo->type != cJSON_NULL) { + if ((ret = set_enc_algo(metadata, enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_enc_algo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); + goto exit; + } + } + } + + cJSON *iv_nonce = cJSON_GetObjectItem(json, "ivNonce"); + if (iv_nonce != NULL) { + if (iv_nonce->type != cJSON_NULL) { + if ((ret = set_iv_nonce(metadata, iv_nonce->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_iv_nonce(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); + goto exit; + } + } + } + + cJSON *ske_enc_key_name = cJSON_GetObjectItem(json, "skeEncKeyName"); + if (ske_enc_key_name != NULL) { + if (ske_enc_key_name->type != cJSON_NULL) { + if ((ret = set_ske_enc_key_name(metadata, ske_enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_ske_enc_key_name(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); + goto exit; + } + } + } + + cJSON *ske_enc_algo = cJSON_GetObjectItem(json, "skeEncAlgo"); + if (ske_enc_algo != NULL) { + if (ske_enc_algo->type != cJSON_NULL) { + if ((ret = set_ske_enc_algo(metadata, ske_enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); + goto exit; + } + } else { + if ((ret = set_ske_enc_algo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); + goto exit; + } + } + } + + ret = 0; + goto exit; +exit: { return ret; } +} +#endif diff --git a/packages/atcommons/src/enroll_namespace.c b/packages/atcommons/src/enroll_namespace.c index 3f943308..ca579b71 100644 --- a/packages/atcommons/src/enroll_namespace.c +++ b/packages/atcommons/src/enroll_namespace.c @@ -1,6 +1,6 @@ #include "atcommons/enroll_namespace.h" -#include "atclient/cjson.h" +#include "atclient/json.h" #include #include diff --git a/packages/atcommons/src/enroll_params.c b/packages/atcommons/src/enroll_params.c index 80005022..c1ef0f5a 100644 --- a/packages/atcommons/src/enroll_params.c +++ b/packages/atcommons/src/enroll_params.c @@ -1,6 +1,6 @@ #include "atcommons/enroll_params.h" -#include "atclient/cjson.h" +#include "atclient/json.h" #include #include