Skip to content

Commit

Permalink
Rename the original UUID into ASTARTE_UUID
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <[email protected]>
  • Loading branch information
sorru94 committed Feb 19, 2025
1 parent e786c6f commit 597814b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ idf_component_register(
"./src/astarte_pairing.c"
"./src/astarte_storage.c"
"./src/astarte_nvs_key_value.c"
"./src/astarte_uuid.c"
"./src/astarte_zlib.c"
"./src/uuid.c"
"./src/bson_deserializer.c"
"./src/bson_serializer.c"
"./src/data_validation.c"
Expand Down
8 changes: 4 additions & 4 deletions include/astarte_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ extern "C" {
*
* Example:
*
* uuid_t uuid_ns;
* if (uuid_from_string("de40ff58-5696-4b35-a6d6-0cc7280bcd56", uuid_ns) != 0) {
* astarte_uuid_t uuid_ns;
* if (astarte_uuid_from_string("de40ff58-5696-4b35-a6d6-0cc7280bcd56", uuid_ns) != 0) {
* ESP_LOGE(TAG, "Error while parsing namespace UUID");
* }
*
* uuid_t device_uuid;
* astarte_uuid_t device_uuid;
* const char *unique_data = "my_unique_data"
* uuid_generate_v5(uuid_ns, unique_data, strlen(unique_data), device_uuid);
* astarte_uuid_generate_v5(uuid_ns, unique_data, strlen(unique_data), device_uuid);
*
* char hwid[32] = { 0 };
* astarte_hwid_encode(hwid, sizeof(hwid), device_uuid);
Expand Down
18 changes: 9 additions & 9 deletions include/uuid.h → include/astarte_uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/

#ifndef _UUID_H_
#define _UUID_H_
#ifndef _ASTARTE_UUID_H_
#define _ASTARTE_UUID_H_

#include <stddef.h>
#include <stdint.h>

#include "astarte.h"

#define UUID_LEN 16
#define ASTARTE_UUID_LEN 16

typedef uint8_t uuid_t[UUID_LEN];
typedef uint8_t astarte_uuid_t[ASTARTE_UUID_LEN];

#ifdef __cplusplus
extern "C" {
Expand All @@ -31,7 +31,7 @@ extern "C" {
* @param out The UUID where the result will be written.
* @return ASTARTE_OK when successfull, ASTARTE_ERR otherwise.
*/
astarte_err_t uuid_generate_v5(const uuid_t namespace, const void *data, size_t len, uuid_t out);
astarte_err_t astarte_uuid_generate_v5(const astarte_uuid_t namespace, const void *data, size_t len, astarte_uuid_t out);

/**
* @brief convert a UUID to its string representation.
Expand All @@ -42,7 +42,7 @@ astarte_err_t uuid_generate_v5(const uuid_t namespace, const void *data, size_t
* Should be at least 37 bytes large.
* @return ASTARTE_ERR upon failure, ASTARTE_OK otherwise.
*/
astarte_err_t uuid_to_string(const uuid_t uuid, char *out);
astarte_err_t astarte_uuid_to_string(const astarte_uuid_t uuid, char *out);

/**
* @brief parse a UUID from its string representation.
Expand All @@ -52,18 +52,18 @@ astarte_err_t uuid_to_string(const uuid_t uuid, char *out);
* @param out The UUID where the result will be written.
* @return 0 if the parsing was succesfull, -1 otherwise.
*/
int uuid_from_string(const char *input, uuid_t out);
int astarte_uuid_from_string(const char *input, astarte_uuid_t out);

/**
* @brief generate a UUIDv4.
*
* @details This function computes a random UUID using hardware RNG.
* @param out The UUID where the result will be written.
*/
void uuid_generate_v4(uuid_t out);
void astarte_uuid_generate_v4(astarte_uuid_t out);

#ifdef __cplusplus
}
#endif

#endif // _UUID_H_
#endif // _ASTARTE_UUID_H_
10 changes: 5 additions & 5 deletions src/astarte_hwid.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <astarte_hwid.h>

#include <string.h>
#include <uuid.h>
#include <astarte_uuid.h>

#include <esp_log.h>
#include <esp_system.h>
Expand Down Expand Up @@ -69,12 +69,12 @@ astarte_err_t astarte_hwid_get_id(uint8_t *hardware_id)
ESP_LOGD(TAG, "Astarte Device SDK running on: %s", info_string);

#ifdef CONFIG_ASTARTE_HWID_ENABLE_UUID
uuid_t namespace_uuid;
uuid_from_string(CONFIG_ASTARTE_HWID_UUID_NAMESPACE, namespace_uuid);
astarte_uuid_t namespace_uuid;
astarte_uuid_from_string(CONFIG_ASTARTE_HWID_UUID_NAMESPACE, namespace_uuid);

uuid_t device_uuid;
astarte_uuid_t device_uuid;
astarte_err_t uuid_err
= uuid_generate_v5(namespace_uuid, info_string, strlen(info_string), device_uuid);
= astarte_uuid_generate_v5(namespace_uuid, info_string, strlen(info_string), device_uuid);
if (uuid_err != ASTARTE_OK) {
ESP_LOGE(TAG, "HWID generation failed.");
return uuid_err;
Expand Down
20 changes: 10 additions & 10 deletions src/uuid.c → src/astarte_uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/

#include "uuid.h"
#include "astarte_uuid.h"

#include <esp_log.h>
#include <esp_system.h>
Expand Down Expand Up @@ -77,7 +77,7 @@ struct uuid
uint8_t node[UUID_LEN_NODE];
};

static void uuid_from_struct(const struct uuid *input, uuid_t out)
static void uuid_from_struct(const struct uuid *input, astarte_uuid_t out)
{
uint32_t tmp32 = 0U;
uint16_t tmp16 = 0U;
Expand All @@ -98,7 +98,7 @@ static void uuid_from_struct(const struct uuid *input, uuid_t out)
memcpy(out_p + UUID_OFFSET_NODE, input->node, UUID_LEN_NODE);
}

static void uuid_to_struct(const uuid_t input, struct uuid *out)
static void uuid_to_struct(const astarte_uuid_t input, struct uuid *out)
{
uint32_t tmp32 = 0U;
uint16_t tmp16 = 0U;
Expand All @@ -119,7 +119,7 @@ static void uuid_to_struct(const uuid_t input, struct uuid *out)
memcpy(out->node, in_p + UUID_OFFSET_NODE, UUID_LEN_NODE);
}

astarte_err_t uuid_generate_v5(const uuid_t namespace, const void *data, size_t length, uuid_t out)
astarte_err_t astarte_uuid_generate_v5(const astarte_uuid_t namespace, const void *data, size_t length, astarte_uuid_t out)
{
const size_t sha_256_bytes = 32;
uint8_t sha_result[sha_256_bytes];
Expand All @@ -131,7 +131,7 @@ astarte_err_t uuid_generate_v5(const uuid_t namespace, const void *data, size_t
int mbedtls_err = mbedtls_md_setup(&ctx, md_info, 0);
// NOLINTBEGIN(hicpp-signed-bitwise) Only using the mbedtls_err to check if zero
mbedtls_err |= mbedtls_md_starts(&ctx);
mbedtls_err |= mbedtls_md_update(&ctx, namespace, UUID_LEN);
mbedtls_err |= mbedtls_md_update(&ctx, namespace, ASTARTE_UUID_LEN);
mbedtls_err |= mbedtls_md_update(&ctx, data, length);
mbedtls_err |= mbedtls_md_finish(&ctx, sha_result);
// NOLINTEND(hicpp-signed-bitwise)
Expand All @@ -156,7 +156,7 @@ astarte_err_t uuid_generate_v5(const uuid_t namespace, const void *data, size_t
return ASTARTE_OK;
}

astarte_err_t uuid_to_string(const uuid_t uuid, char *out)
astarte_err_t astarte_uuid_to_string(const astarte_uuid_t uuid, char *out)
{
struct uuid uuid_struct;

Expand All @@ -175,7 +175,7 @@ astarte_err_t uuid_to_string(const uuid_t uuid, char *out)
return ASTARTE_OK;
}

int uuid_from_string(const char *input, uuid_t out)
int astarte_uuid_from_string(const char *input, astarte_uuid_t out)
{
// Length check
if (strlen(input) != UUID_STR_LEN) {
Expand Down Expand Up @@ -231,10 +231,10 @@ int uuid_from_string(const char *input, uuid_t out)
return 0;
}

void uuid_generate_v4(uuid_t out)
void astarte_uuid_generate_v4(astarte_uuid_t out)
{
uint8_t random_result[UUID_LEN];
esp_fill_random(random_result, UUID_LEN);
uint8_t random_result[ASTARTE_UUID_LEN];
esp_fill_random(random_result, ASTARTE_UUID_LEN);

struct uuid random_uuid_struct;
uuid_to_struct(random_result, &random_uuid_struct);
Expand Down
52 changes: 26 additions & 26 deletions tests/host/test_uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "unity.h"

#include "test_uuid.h"
#include "uuid.h"
#include "astarte_uuid.h"

#include <esp_log.h>

Expand All @@ -36,35 +36,35 @@
void test_uuid_from_string(void)
{
const char *first_uuid_v4_string = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
uuid_t first_uuid_v4;
int res = uuid_from_string(first_uuid_v4_string, first_uuid_v4);
astarte_uuid_t first_uuid_v4;
int res = astarte_uuid_from_string(first_uuid_v4_string, first_uuid_v4);
TEST_ASSERT_EQUAL(0, res);
uint8_t expected_first_uuid_v4_byte_array[16u] = { 0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43,
0xb4, 0x8f, 0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f };
for (size_t i = 0; i < 16; i++)
TEST_ASSERT_EQUAL_HEX8(expected_first_uuid_v4_byte_array[i], first_uuid_v4[i]);

const char *second_uuid_v4_string = "6f2fd4cb-94a0-41c7-8d27-864c6b13b8c0";
uuid_t second_uuid_v4;
res = uuid_from_string(second_uuid_v4_string, second_uuid_v4);
astarte_uuid_t second_uuid_v4;
res = astarte_uuid_from_string(second_uuid_v4_string, second_uuid_v4);
TEST_ASSERT_EQUAL(0, res);
uint8_t expected_second_uuid_v4_byte_array[16u] = { 0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41,
0xc7, 0x8d, 0x27, 0x86, 0x4c, 0x6b, 0x13, 0xb8, 0xc0 };
for (size_t i = 0; i < 16; i++)
TEST_ASSERT_EQUAL_HEX8(expected_second_uuid_v4_byte_array[i], second_uuid_v4[i]);

const char *third_uuid_v4_string = "8f65dbbc-5868-4015-8523-891cc0bffa58";
uuid_t third_uuid_v4;
res = uuid_from_string(third_uuid_v4_string, third_uuid_v4);
astarte_uuid_t third_uuid_v4;
res = astarte_uuid_from_string(third_uuid_v4_string, third_uuid_v4);
TEST_ASSERT_EQUAL(0, res);
uint8_t expected_third_uuid_v4_byte_array[16u] = { 0x8f, 0x65, 0xdb, 0xbc, 0x58, 0x68, 0x40,
0x15, 0x85, 0x23, 0x89, 0x1c, 0xc0, 0xbf, 0xfa, 0x58 };
for (size_t i = 0; i < 16; i++)
TEST_ASSERT_EQUAL_HEX8(expected_third_uuid_v4_byte_array[i], third_uuid_v4[i]);

const char *first_uuid_v5_string = "0575a569-51eb-575c-afe4-ce7fc03bcdc5";
uuid_t first_uuid_v5;
res = uuid_from_string(first_uuid_v5_string, first_uuid_v5);
astarte_uuid_t first_uuid_v5;
res = astarte_uuid_from_string(first_uuid_v5_string, first_uuid_v5);
TEST_ASSERT_EQUAL(0, res);
uint8_t expected_first_uuid_v5_byte_array[16u] = { 0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57,
0x5c, 0xaf, 0xe4, 0xce, 0x7f, 0xc0, 0x3b, 0xcd, 0xc5 };
Expand All @@ -75,33 +75,33 @@ void test_uuid_from_string(void)
void test_uuid_to_string(void)
{
char first_uuid_v4_byte_array[37];
const uuid_t first_uuid_v4 = { 0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4, 0x8f, 0xef, 0xca,
const astarte_uuid_t first_uuid_v4 = { 0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4, 0x8f, 0xef, 0xca,
0x7b, 0xae, 0xa1, 0x37, 0x5f };
astarte_err_t err = uuid_to_string(first_uuid_v4, first_uuid_v4_byte_array);
astarte_err_t err = astarte_uuid_to_string(first_uuid_v4, first_uuid_v4_byte_array);
TEST_ASSERT_EQUAL(ASTARTE_OK, err);
const char *expected_first_uuid_v4_string = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
TEST_ASSERT_EQUAL_STRING(expected_first_uuid_v4_string, first_uuid_v4_byte_array);

char second_uuid_v4_byte_array[37];
const uuid_t second_uuid_v4 = { 0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d, 0x27,
const astarte_uuid_t second_uuid_v4 = { 0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d, 0x27,
0x86, 0x4c, 0x6b, 0x13, 0xb8, 0xc0 };
err = uuid_to_string(second_uuid_v4, second_uuid_v4_byte_array);
err = astarte_uuid_to_string(second_uuid_v4, second_uuid_v4_byte_array);
TEST_ASSERT_EQUAL(ASTARTE_OK, err);
const char *expected_second_uuid_v4_string = "6f2fd4cb-94a0-41c7-8d27-864c6b13b8c0";
TEST_ASSERT_EQUAL_STRING(expected_second_uuid_v4_string, second_uuid_v4_byte_array);

char first_uuid_v5_byte_array[37];
const uuid_t first_uuid_v5 = { 0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57, 0x5c, 0xaf, 0xe4, 0xce,
const astarte_uuid_t first_uuid_v5 = { 0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57, 0x5c, 0xaf, 0xe4, 0xce,
0x7f, 0xc0, 0x3b, 0xcd, 0xc5 };
err = uuid_to_string(first_uuid_v5, first_uuid_v5_byte_array);
err = astarte_uuid_to_string(first_uuid_v5, first_uuid_v5_byte_array);
TEST_ASSERT_EQUAL(ASTARTE_OK, err);
const char *expected_first_uuid_v5_string = "0575a569-51eb-575c-afe4-ce7fc03bcdc5";
TEST_ASSERT_EQUAL_STRING(expected_first_uuid_v5_string, first_uuid_v5_byte_array);
}

void test_uuid_generate_v5(void)
{
uuid_t namespace = { 0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d, 0x27, 0x86, 0x4c,
astarte_uuid_t namespace = { 0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d, 0x27, 0x86, 0x4c,
0x6b, 0x13, 0xb8, 0xc0 };
const char *unique_data = "some unique data";

Expand All @@ -117,10 +117,10 @@ void test_uuid_generate_v5(void)
mbedtls_md_finish_ReturnArrayThruPtr_output(sha_result, 32);
mbedtls_md_free_ExpectAnyArgs();

uuid_t out;
uuid_generate_v5(namespace, unique_data, strlen(unique_data), out);
astarte_uuid_t out;
astarte_uuid_generate_v5(namespace, unique_data, strlen(unique_data), out);

uuid_t expected_out = { 0x3e, 0x73, 0xe1, 0xf2, 0x03, 0x63, 0x54, 0xe5, 0xb5, 0x96, 0x23, 0x66,
astarte_uuid_t expected_out = { 0x3e, 0x73, 0xe1, 0xf2, 0x03, 0x63, 0x54, 0xe5, 0xb5, 0x96, 0x23, 0x66,
0x17, 0x0f, 0xa3, 0xe5 };
for (size_t i = 0; i < 16; i++)
TEST_ASSERT_EQUAL_HEX8(expected_out[i], out[i]);
Expand All @@ -130,22 +130,22 @@ void test_uuid_generate_v4(void)
{
esp_fill_random_Expect(NULL, 16);
esp_fill_random_IgnoreArg_buf();
uuid_t returned_random = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
astarte_uuid_t returned_random = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
esp_fill_random_ReturnArrayThruPtr_buf(returned_random, 16);

uuid_t out;
uuid_generate_v4(out);
astarte_uuid_t out;
astarte_uuid_generate_v4(out);

uuid_t expected_out = { 1, 2, 3, 4, 5, 6, 0x40 + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
astarte_uuid_t expected_out = { 1, 2, 3, 4, 5, 6, 0x40 + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_out, out, 16);

esp_fill_random_Expect(NULL, 16);
esp_fill_random_IgnoreArg_buf();
uuid_t returned_random_empty = { 0 };
astarte_uuid_t returned_random_empty = { 0 };
esp_fill_random_ReturnArrayThruPtr_buf(returned_random_empty, 16);

uuid_generate_v4(out);
astarte_uuid_generate_v4(out);

uuid_t expected_out_empty = { 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
astarte_uuid_t expected_out_empty = { 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_out_empty, out, 16);
}

0 comments on commit 597814b

Please sign in to comment.