uuid: add uuid_to_string()
This commit is contained in:
parent
71455b692f
commit
db4ce5eff2
@ -780,6 +780,7 @@ endif
|
|||||||
ifneq (,$(filter uuid,$(USEMODULE)))
|
ifneq (,$(filter uuid,$(USEMODULE)))
|
||||||
USEMODULE += hashes
|
USEMODULE += hashes
|
||||||
USEMODULE += random
|
USEMODULE += random
|
||||||
|
USEMODULE += fmt
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Enable periph_gpio when periph_gpio_irq is enabled
|
# Enable periph_gpio when periph_gpio_irq is enabled
|
||||||
|
|||||||
@ -38,6 +38,8 @@ extern "C" {
|
|||||||
|
|
||||||
#define UUID_NODE_LEN (6U) /**< Size of the node identifier in bytes */
|
#define UUID_NODE_LEN (6U) /**< Size of the node identifier in bytes */
|
||||||
|
|
||||||
|
#define UUID_STR_LEN (36U) /**< Size of a string UUID without null character */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name UUID version identifiers
|
* @name UUID version identifiers
|
||||||
* @{
|
* @{
|
||||||
@ -140,6 +142,14 @@ static inline bool uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
|
|||||||
return (memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0);
|
return (memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Generate an UUID string from an UUID structure
|
||||||
|
*
|
||||||
|
* @param[in] uuid UUID
|
||||||
|
* @param[out] str null-terminated UUID string, must be at least UUID_STR_LEN + 1 bytes
|
||||||
|
*/
|
||||||
|
void uuid_to_string(const uuid_t *uuid, char *str);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -23,6 +23,7 @@
|
|||||||
#include "hashes/sha1.h"
|
#include "hashes/sha1.h"
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
#include "fmt.h"
|
||||||
|
|
||||||
const uuid_t uuid_namespace_dns = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
|
const uuid_t uuid_namespace_dns = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
|
||||||
.time_low.u8 = { 0x6b, 0xa7, 0xb8, 0x10 },
|
.time_low.u8 = { 0x6b, 0xa7, 0xb8, 0x10 },
|
||||||
@ -110,3 +111,20 @@ void uuid_v5(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len)
|
|||||||
_set_version(uuid, UUID_V5);
|
_set_version(uuid, UUID_V5);
|
||||||
_set_reserved(uuid);
|
_set_reserved(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void uuid_to_string(const uuid_t *uuid, char *str)
|
||||||
|
{
|
||||||
|
char *p = str;
|
||||||
|
p += fmt_u32_hex(p, byteorder_ntohl(uuid->time_low));
|
||||||
|
p += fmt_char(p, '-');
|
||||||
|
p += fmt_u16_hex(p, byteorder_ntohs(uuid->time_mid));
|
||||||
|
p += fmt_char(p, '-');
|
||||||
|
p += fmt_u16_hex(p, byteorder_ntohs(uuid->time_hi));
|
||||||
|
p += fmt_char(p, '-');
|
||||||
|
p += fmt_byte_hex(p, uuid->clk_seq_hi_res);
|
||||||
|
p += fmt_byte_hex(p, uuid->clk_seq_low);
|
||||||
|
p += fmt_char(p, '-');
|
||||||
|
p += fmt_bytes_hex(p, uuid->node, UUID_NODE_LEN);
|
||||||
|
*p = '\0';
|
||||||
|
fmt_to_lower(str, str);
|
||||||
|
}
|
||||||
|
|||||||
@ -95,12 +95,33 @@ void test_uuid_v5(void)
|
|||||||
TEST_ASSERT_EQUAL_INT(uuid_version(&uuid_next), UUID_V5);
|
TEST_ASSERT_EQUAL_INT(uuid_version(&uuid_next), UUID_V5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_uuid_str(void)
|
||||||
|
{
|
||||||
|
char str[40];
|
||||||
|
const char dns[] = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
uuid_to_string(&uuid_namespace_dns, str);
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, memcmp(dns, str, sizeof(dns)));
|
||||||
|
|
||||||
|
const char url[] = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
uuid_to_string(&uuid_namespace_url, str);
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, memcmp(url, str, sizeof(dns)));
|
||||||
|
|
||||||
|
const char iso[] = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
uuid_to_string(&uuid_namespace_iso, str);
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, memcmp(iso, str, sizeof(dns)));
|
||||||
|
|
||||||
|
const char x500[] = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
|
||||||
|
uuid_to_string(&uuid_namespace_x500, str);
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, memcmp(x500, str, sizeof(dns)));
|
||||||
|
}
|
||||||
|
|
||||||
Test *tests_uuid_all(void)
|
Test *tests_uuid_all(void)
|
||||||
{
|
{
|
||||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||||
new_TestFixture(test_uuid_v3),
|
new_TestFixture(test_uuid_v3),
|
||||||
new_TestFixture(test_uuid_v4),
|
new_TestFixture(test_uuid_v4),
|
||||||
new_TestFixture(test_uuid_v5),
|
new_TestFixture(test_uuid_v5),
|
||||||
|
new_TestFixture(test_uuid_str),
|
||||||
};
|
};
|
||||||
|
|
||||||
EMB_UNIT_TESTCALLER(uuid_tests, NULL, NULL, fixtures);
|
EMB_UNIT_TESTCALLER(uuid_tests, NULL, NULL, fixtures);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user