From 0a4ac85042a1cdcfedf4cb782edc9bd1c814f3a7 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Wed, 2 Dec 2015 10:49:12 +0200 Subject: lib/uuid: Make bt_uuid_to_string always use the same format The convention has been to use 128 Bits UUID strings so other types must be converted first. --- lib/uuid.c | 55 ++++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/lib/uuid.c b/lib/uuid.c index 046b52131..20b67d037 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -138,46 +138,35 @@ int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2) */ int bt_uuid_to_string(const bt_uuid_t *uuid, char *str, size_t n) { - if (!uuid) { + bt_uuid_t tmp; + unsigned int data0; + unsigned short data1; + unsigned short data2; + unsigned short data3; + unsigned int data4; + unsigned short data5; + const uint8_t *data; + + if (!uuid || uuid->type == BT_UUID_UNSPEC) { snprintf(str, n, "NULL"); return -EINVAL; } - switch (uuid->type) { - case BT_UUID16: - snprintf(str, n, "%.4x", uuid->value.u16); - break; - case BT_UUID32: - snprintf(str, n, "%.8x", uuid->value.u32); - break; - case BT_UUID128: { - unsigned int data0; - unsigned short data1; - unsigned short data2; - unsigned short data3; - unsigned int data4; - unsigned short data5; - - const uint8_t *data = (uint8_t *) &uuid->value.u128; - - memcpy(&data0, &data[0], 4); - memcpy(&data1, &data[4], 2); - memcpy(&data2, &data[6], 2); - memcpy(&data3, &data[8], 2); - memcpy(&data4, &data[10], 4); - memcpy(&data5, &data[14], 2); - - snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", + /* Convert to 128 Bit format */ + bt_uuid_to_uuid128(uuid, &tmp); + data = (uint8_t *) &tmp.value.u128; + + memcpy(&data0, &data[0], 4); + memcpy(&data1, &data[4], 2); + memcpy(&data2, &data[6], 2); + memcpy(&data3, &data[8], 2); + memcpy(&data4, &data[10], 4); + memcpy(&data5, &data[14], 2); + + snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", ntohl(data0), ntohs(data1), ntohs(data2), ntohs(data3), ntohl(data4), ntohs(data5)); - } - break; - case BT_UUID_UNSPEC: - default: - snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); - return -EINVAL; /* Enum type of UUID not set */ - } return 0; } -- cgit v1.2.1