diff --git a/include/bluetooth/uuid.h b/include/bluetooth/uuid.h index 8bf3ed01cf3..6576f700180 100644 --- a/include/bluetooth/uuid.h +++ b/include/bluetooth/uuid.h @@ -79,6 +79,51 @@ struct bt_uuid_128 { #define BT_UUID_32(__u) CONTAINER_OF(__u, struct bt_uuid_32, uuid) #define BT_UUID_128(__u) CONTAINER_OF(__u, struct bt_uuid_128, uuid) + +/** + * @brief Encode 128 bit UUID into an array values + * + * Helper macro to initialize a 128-bit UUID value from the UUID format. + * Can be combined with BT_UUID_DECLARE_128 to declare a 128-bit UUID from + * the readable form of UUIDs. + * + * Example for how to declare the UUID `6E400001-B5A3-F393-E0A9-E50E24DCCA9E` + * + * @code + * BT_UUID_128_DECL( + * BT_UUID_128_ENC(0x6E400001, 0xB5A3, 0xF393, 0xE0A9, 0xE50E24DCCA9E)) + * @endcode + * + * Just replace the hyphen by the comma and add `0x` prefixes. + * + * @param w32 First part of the UUID (32 bits) + * @param w1 Second part of the UUID (16 bits) + * @param w2 Third part of the UUID (16 bits) + * @param w3 Fourth part of the UUID (16 bits) + * @param w48 Fifth part of the UUID (48 bits) + * + * @return The comma separated values for UUID 128 initializer that + * may be used directly as an argument for + * @ref BT_UUID_INIT_128 or @ref BT_UUID_DECLARE_128 + */ +#define BT_UUID_128_ENCODE(w32, w1, w2, w3, w48) \ + (((w48) >> 0) & 0xFF), \ + (((w48) >> 8) & 0xFF), \ + (((w48) >> 16) & 0xFF), \ + (((w48) >> 24) & 0xFF), \ + (((w48) >> 32) & 0xFF), \ + (((w48) >> 40) & 0xFF), \ + (((w3) >> 0) & 0xFF), \ + (((w3) >> 8) & 0xFF), \ + (((w2) >> 0) & 0xFF), \ + (((w2) >> 8) & 0xFF), \ + (((w1) >> 0) & 0xFF), \ + (((w1) >> 8) & 0xFF), \ + (((w32) >> 0) & 0xFF), \ + (((w32) >> 8) & 0xFF), \ + (((w32) >> 16) & 0xFF), \ + (((w32) >> 24) & 0xFF) + /** @def BT_UUID_GAP * @brief Generic Access */ diff --git a/subsys/bluetooth/host/uuid.c b/subsys/bluetooth/host/uuid.c index 10bb1d71f43..30eaccebd8a 100644 --- a/subsys/bluetooth/host/uuid.c +++ b/subsys/bluetooth/host/uuid.c @@ -17,19 +17,15 @@ /* TODO: Decide whether to continue using BLE format or switch to RFC 4122 */ -/* Base UUID : 0000[0000]-0000-1000-8000-00805F9B34FB -> - * { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - * 0x00, 0x10, 0x00, 0x00, [0x00, 0x00], 0x00, 0x00 } - * 0x2800 : 0000[2800]-0000-1000-8000-00805F9B34FB -> - * { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - * 0x00, 0x10, 0x00, 0x00, [0x00, 0x28], 0x00, 0x00 } +/* Base UUID : 0000[0000]-0000-1000-8000-00805F9B34FB + * 0x2800 : 0000[2800]-0000-1000-8000-00805F9B34FB * little endian 0x2800 : [00 28] -> no swapping required * big endian 0x2800 : [28 00] -> swapping required */ static const struct bt_uuid_128 uuid128_base = { .uuid.type = BT_UUID_TYPE_128, - .val = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + .val = { BT_UUID_128_ENCODE( + 0x00000000, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB) } }; static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid_128 *dst)