Bluetooth: UUID: Implement UUID 128 encoder

This commit adds an macro to allow UUID 128
to be written in more user-friendly form.
UUID in 128 bit form requires an array creation.
To complicate the whole thing - it requires the array to start from LSB,
so using the readable form, we have to write it down backwards.

Old way to declare example UUID 6E400001-B5A3-F393-E0A9-E50E24DCCA9E:
 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E

A form provided by this commit:
 BT_UUID_128_ENCODE(0x6E400001, 0xB5A3, 0xF393, 0xE0A9, 0xE50E24DCCA9E)

Signed-off-by: Radoslaw Koppel <radoslaw.koppel@nordicsemi.no>
This commit is contained in:
Radoslaw Koppel 2019-10-15 11:58:11 +02:00 committed by Johan Hedberg
commit ecf06debe6
2 changed files with 49 additions and 8 deletions

View file

@ -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
*/

View file

@ -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)