From d5e23e86d23888d2da703b611c1a8415533ce45c Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Wed, 27 Jan 2016 22:43:34 -0500 Subject: [PATCH] Bluetooth: UUID: Introduce new API Introduce new UUID API with bt_uuid, bt_uuid_16 and bt_uuid_32 structs. The specific size structs are derived from the common bt_uuid struct to make it possible to use CONTAINER_OF(). Change-Id: I9cb03c73406acb7768d410fdf29eae75d252163c Signed-off-by: Johan Hedberg --- include/bluetooth/uuid.h | 77 ++++++++-------- net/bluetooth/att.c | 56 ++++++------ net/bluetooth/gatt.c | 115 +++++++++++++----------- net/bluetooth/uuid.c | 68 +++++++------- samples/bluetooth/central_hr/src/main.c | 22 ++--- samples/bluetooth/peripheral/src/main.c | 56 +++++------- samples/bluetooth/shell/src/main.c | 9 +- samples/bluetooth/tester/src/gatt.c | 53 ++++++----- 8 files changed, 226 insertions(+), 230 deletions(-) diff --git a/include/bluetooth/uuid.h b/include/bluetooth/uuid.h index edbb6f8af5e..a1f3e93bfd9 100644 --- a/include/bluetooth/uuid.h +++ b/include/bluetooth/uuid.h @@ -20,10 +20,52 @@ #ifndef __BT_UUID_H #define __BT_UUID_H +#include + #ifdef __cplusplus extern "C" { #endif +/** @brief Bluetooth UUID types */ +enum { + BT_UUID_TYPE_16, + BT_UUID_TYPE_128, +}; + +struct bt_uuid { + uint8_t type; +}; + +struct bt_uuid_16 { + struct bt_uuid uuid; + uint16_t val; +}; + +struct bt_uuid_128 { + struct bt_uuid uuid; + uint8_t val[16]; +}; + +#define BT_UUID_INIT_16(value) \ +{ \ + .uuid.type = BT_UUID_TYPE_16, \ + .val = (value), \ +} + +#define BT_UUID_INIT_128(value...) \ +{ \ + .uuid.type = BT_UUID_TYPE_128, \ + .val = { value }, \ +} + +#define BT_UUID_DECLARE_16(value) \ + ((struct bt_uuid *) (&(struct bt_uuid_16) BT_UUID_INIT_16(value))) +#define BT_UUID_DECLARE_128(value...) \ + ((struct bt_uuid *) (&(struct bt_uuid_128) BT_UUID_INIT_128(value))) + +#define BT_UUID_16(__u) CONTAINER_OF(__u, struct bt_uuid_16, uuid) +#define BT_UUID_128(__u) CONTAINER_OF(__u, struct bt_uuid_128, uuid) + /** @def BBT_UUID_GAP * @brief Generic Access */ @@ -185,41 +227,6 @@ extern "C" { #define BT_UUID_HRS_CONTROL_POINT BT_UUID_DECLARE_16(0x2a39) #define BT_UUID_HRS_CONTROL_POINT_VAL 0x2a39 -/** @brief Bluetooth UUID types */ -enum bt_uuid_type { - BT_UUID_16, - BT_UUID_128, -}; - -#define BT_UUID_DECLARE_16(value) \ - ((struct bt_uuid *) (&(struct __bt_uuid_16) { .type = BT_UUID_16, \ - .u16 = value })) -#define BT_UUID_DECLARE_128(value) \ - ((struct bt_uuid *) (&(struct __bt_uuid_128) { .type = BT_UUID_128, \ - .u128 = value })) - -struct __bt_uuid_16 { - uint8_t type; - uint16_t u16; -}; - -struct __bt_uuid_128 { - uint8_t type; - uint8_t u128[16]; -}; - -/** @brief Bluetooth UUID structure */ -struct bt_uuid { - /** UUID type */ - uint8_t type; - union { - /** UUID 16 bits value */ - uint16_t u16; - /** UUID 128 bits value */ - uint8_t u128[16]; - }; -}; - /** @brief Compare Bluetooth UUIDs. * * Compares 2 Bluetooth UUIDs, if the types are different both UUIDs are diff --git a/net/bluetooth/att.c b/net/bluetooth/att.c index 401242c8e7f..059c3ba555b 100644 --- a/net/bluetooth/att.c +++ b/net/bluetooth/att.c @@ -274,20 +274,20 @@ static uint8_t find_info_cb(const struct bt_gatt_attr *attr, void *user_data) /* Initialize rsp at first entry */ if (!data->rsp) { data->rsp = net_buf_add(data->buf, sizeof(*data->rsp)); - data->rsp->format = (attr->uuid->type == BT_UUID_16) ? + data->rsp->format = (attr->uuid->type == BT_UUID_TYPE_16) ? BT_ATT_INFO_16 : BT_ATT_INFO_128; } switch (data->rsp->format) { case BT_ATT_INFO_16: - if (attr->uuid->type != BT_UUID_16) { + if (attr->uuid->type != BT_UUID_TYPE_16) { return BT_GATT_ITER_STOP; } /* Fast foward to next item position */ data->info16 = net_buf_add(data->buf, sizeof(*data->info16)); data->info16->handle = sys_cpu_to_le16(attr->handle); - data->info16->uuid = sys_cpu_to_le16(attr->uuid->u16); + data->info16->uuid = sys_cpu_to_le16(BT_UUID_16(attr->uuid)->val); if (att->chan.tx.mtu - data->buf->len > sizeof(*data->info16)) { return BT_GATT_ITER_CONTINUE; @@ -295,14 +295,14 @@ static uint8_t find_info_cb(const struct bt_gatt_attr *attr, void *user_data) break; case BT_ATT_INFO_128: - if (attr->uuid->type != BT_UUID_128) { + if (attr->uuid->type != BT_UUID_TYPE_128) { return BT_GATT_ITER_STOP; } /* Fast foward to next item position */ data->info128 = net_buf_add(data->buf, sizeof(*data->info128)); data->info128->handle = sys_cpu_to_le16(attr->handle); - memcpy(data->info128->uuid, attr->uuid->u128, + memcpy(data->info128->uuid, BT_UUID_128(attr->uuid)->val, sizeof(data->info128->uuid)); if (att->chan.tx.mtu - data->buf->len > @@ -511,18 +511,14 @@ static uint8_t att_find_type_req(struct bt_att *att, struct net_buf *buf) static bool uuid_create(struct bt_uuid *uuid, struct net_buf *buf) { - if (buf->len > sizeof(uuid->u128)) { - return false; - } - switch (buf->len) { case 2: - uuid->type = BT_UUID_16; - uuid->u16 = net_buf_pull_le16(buf); + uuid->type = BT_UUID_TYPE_16; + BT_UUID_16(uuid)->val = net_buf_pull_le16(buf); return true; case 16: - uuid->type = BT_UUID_128; - memcpy(uuid->u128, buf->data, buf->len); + uuid->type = BT_UUID_TYPE_128; + memcpy(BT_UUID_128(uuid)->val, buf->data, buf->len); return true; } @@ -704,11 +700,14 @@ static uint8_t att_read_type_req(struct bt_att *att, struct net_buf *buf) struct bt_conn *conn = att->chan.conn; struct bt_att_read_type_req *req; uint16_t start_handle, end_handle, err_handle; - struct bt_uuid uuid; + union { + struct bt_uuid uuid; + struct bt_uuid_16 u16; + struct bt_uuid_128 u128; + } u; /* Type can only be UUID16 or UUID128 */ - if (buf->len != sizeof(*req) + sizeof(uuid.u16) && - buf->len != sizeof(*req) + sizeof(uuid.u128)) { + if (buf->len != sizeof(*req) + 2 && buf->len != sizeof(*req) + 16) { return BT_ATT_ERR_INVALID_PDU; } @@ -718,12 +717,12 @@ static uint8_t att_read_type_req(struct bt_att *att, struct net_buf *buf) end_handle = sys_le16_to_cpu(req->end_handle); net_buf_pull(buf, sizeof(*req)); - if (!uuid_create(&uuid, buf)) { + if (!uuid_create(&u.uuid, buf)) { return BT_ATT_ERR_UNLIKELY; } BT_DBG("start_handle 0x%04x end_handle 0x%04x type %s", - start_handle, end_handle, bt_uuid_str(&uuid)); + start_handle, end_handle, bt_uuid_str(&u.uuid)); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, err_handle, @@ -731,7 +730,7 @@ static uint8_t att_read_type_req(struct bt_att *att, struct net_buf *buf) return 0; } - return att_read_type_rsp(att, &uuid, start_handle, end_handle); + return att_read_type_rsp(att, &u.uuid, start_handle, end_handle); } struct read_data { @@ -1006,11 +1005,14 @@ static uint8_t att_read_group_req(struct bt_att *att, struct net_buf *buf) struct bt_conn *conn = att->chan.conn; struct bt_att_read_group_req *req; uint16_t start_handle, end_handle, err_handle; - struct bt_uuid uuid; + union { + struct bt_uuid uuid; + struct bt_uuid_16 u16; + struct bt_uuid_128 u128; + } u; /* Type can only be UUID16 or UUID128 */ - if (buf->len != sizeof(*req) + sizeof(uuid.u16) && - buf->len != sizeof(*req) + sizeof(uuid.u128)) { + if (buf->len != sizeof(*req) + 2 && buf->len != sizeof(*req) + 16) { return BT_ATT_ERR_INVALID_PDU; } @@ -1020,12 +1022,12 @@ static uint8_t att_read_group_req(struct bt_att *att, struct net_buf *buf) end_handle = sys_le16_to_cpu(req->end_handle); net_buf_pull(buf, sizeof(*req)); - if (!uuid_create(&uuid, buf)) { + if (!uuid_create(&u.uuid, buf)) { return BT_ATT_ERR_UNLIKELY; } BT_DBG("start_handle 0x%04x end_handle 0x%04x type %s", - start_handle, end_handle, bt_uuid_str(&uuid)); + start_handle, end_handle, bt_uuid_str(&u.uuid)); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, err_handle, @@ -1040,14 +1042,14 @@ static uint8_t att_read_group_req(struct bt_att *att, struct net_buf *buf) * Request. The «Characteristic» grouping type shall not be used in * the ATT Read By Group Type Request. */ - if (bt_uuid_cmp(&uuid, BT_UUID_GATT_PRIMARY) && - bt_uuid_cmp(&uuid, BT_UUID_GATT_SECONDARY)) { + if (bt_uuid_cmp(&u.uuid, BT_UUID_GATT_PRIMARY) && + bt_uuid_cmp(&u.uuid, BT_UUID_GATT_SECONDARY)) { send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, start_handle, BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE); return 0; } - return att_read_group_rsp(att, &uuid, start_handle, end_handle); + return att_read_group_rsp(att, &u.uuid, start_handle, end_handle); } struct write_data { diff --git a/net/bluetooth/gatt.c b/net/bluetooth/gatt.c index b3c8be7506e..59ea056054d 100644 --- a/net/bluetooth/gatt.c +++ b/net/bluetooth/gatt.c @@ -146,15 +146,15 @@ int bt_gatt_attr_read_service(struct bt_conn *conn, { struct bt_uuid *uuid = attr->user_data; - if (uuid->type == BT_UUID_16) { - uint16_t uuid16 = sys_cpu_to_le16(uuid->u16); + if (uuid->type == BT_UUID_TYPE_16) { + uint16_t uuid16 = sys_cpu_to_le16(BT_UUID_16(uuid)->val); - return bt_gatt_attr_read(conn, attr, buf, len, offset, &uuid16, - sizeof(uuid16)); + return bt_gatt_attr_read(conn, attr, buf, len, offset, + &uuid16, 2); } - return bt_gatt_attr_read(conn, attr, buf, len, offset, uuid->u128, - sizeof(uuid->u128)); + return bt_gatt_attr_read(conn, attr, buf, len, offset, + BT_UUID_128(uuid)->val, 16); } struct gatt_incl { @@ -180,8 +180,8 @@ int bt_gatt_attr_read_included(struct bt_conn *conn, * The Service UUID shall only be present when the UUID is a 16-bit * Bluetooth UUID. */ - if (incl->uuid->type == BT_UUID_16) { - pdu.uuid16 = sys_cpu_to_le16(incl->uuid->u16); + if (incl->uuid->type == BT_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BT_UUID_16(incl->uuid)->val); value_len += sizeof(pdu.uuid16); } @@ -223,12 +223,12 @@ int bt_gatt_attr_read_chrc(struct bt_conn *conn, } value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle); - if (chrc->uuid->type == BT_UUID_16) { - pdu.uuid16 = sys_cpu_to_le16(chrc->uuid->u16); - value_len += sizeof(pdu.uuid16); + if (chrc->uuid->type == BT_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BT_UUID_16(chrc->uuid)->val); + value_len += 2; } else { - memcpy(pdu.uuid, chrc->uuid->u128, sizeof(chrc->uuid->u128)); - value_len += sizeof(chrc->uuid->u128); + memcpy(pdu.uuid, BT_UUID_128(chrc->uuid)->val, 16); + value_len += 16; } return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len); @@ -746,7 +746,6 @@ static int att_find_type(struct bt_conn *conn, { struct net_buf *buf; struct bt_att_find_type_req *req; - uint16_t *value; buf = bt_att_create_pdu(conn, BT_ATT_OP_FIND_TYPE_REQ, sizeof(*req)); if (!buf) { @@ -768,14 +767,12 @@ static int att_find_type(struct bt_conn *conn, params->end_handle); switch (params->uuid->type) { - case BT_UUID_16: - value = net_buf_add(buf, sizeof(*value)); - *value = sys_cpu_to_le16(params->uuid->u16); + case BT_UUID_TYPE_16: + net_buf_add_le16(buf, BT_UUID_16(params->uuid)->val); break; - case BT_UUID_128: - net_buf_add(buf, sizeof(params->uuid->u128)); - memcpy(req->value, params->uuid->u128, - sizeof(params->uuid->u128)); + case BT_UUID_TYPE_128: + memcpy(net_buf_add(buf, 16), + BT_UUID_128(params->uuid)->val, 16); break; default: BT_ERR("Unknown UUID type %u", params->uuid->type); @@ -791,21 +788,25 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu, uint16_t length) { const struct bt_att_read_type_rsp *rsp = pdu; - struct bt_uuid uuid; uint16_t handle = 0; struct bt_gatt_include value; + union { + struct bt_uuid uuid; + struct bt_uuid_16 u16; + struct bt_uuid_128 u128; + } u; /* Data can be either in UUID16 or UUID128 */ switch (rsp->len) { case 8: /* UUID16 */ - uuid.type = BT_UUID_16; + u.uuid.type = BT_UUID_TYPE_16; break; case 6: /* UUID128 */ /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 550 * To get the included service UUID when the included service * uses a 128-bit UUID, the Read Request is used. */ - uuid.type = BT_UUID_128; + u.uuid.type = BT_UUID_TYPE_128; break; default: BT_ERR("Invalid data len %u", rsp->len); @@ -832,22 +833,22 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu, value.start_handle = incl->start_handle; value.end_handle = incl->end_handle; - switch(uuid.type) { - case BT_UUID_16: - value.uuid = &uuid; - uuid.u16 = sys_le16_to_cpu(incl->uuid16); + switch (u.uuid.type) { + case BT_UUID_TYPE_16: + value.uuid = &u.uuid; + u.u16.val = sys_le16_to_cpu(incl->uuid16); break; - case BT_UUID_128: + case BT_UUID_TYPE_128: /* Data is not available at this point */ break; } BT_DBG("handle 0x%04x uuid %s start_handle 0x%04x " - "end_handle 0x%04x\n", handle, bt_uuid_str(&uuid), + "end_handle 0x%04x\n", handle, bt_uuid_str(&u.uuid), value.start_handle, value.end_handle); /* Skip if UUID is set but doesn't match */ - if (params->uuid && bt_uuid_cmp(&uuid, params->uuid)) { + if (params->uuid && bt_uuid_cmp(&u.uuid, params->uuid)) { continue; } @@ -874,16 +875,20 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu, uint16_t length) { const struct bt_att_read_type_rsp *rsp = pdu; - struct bt_uuid uuid; uint16_t handle = 0; + union { + struct bt_uuid uuid; + struct bt_uuid_16 u16; + struct bt_uuid_128 u128; + } u; /* Data can be either in UUID16 or UUID128 */ switch (rsp->len) { case 7: /* UUID16 */ - uuid.type = BT_UUID_16; + u.uuid.type = BT_UUID_TYPE_16; break; case 21: /* UUID128 */ - uuid.type = BT_UUID_128; + u.uuid.type = BT_UUID_TYPE_128; break; default: BT_ERR("Invalid data len %u", rsp->len); @@ -903,24 +908,24 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu, goto done; } - switch(uuid.type) { - case BT_UUID_16: - uuid.u16 = sys_le16_to_cpu(chrc->uuid16); + switch (u.uuid.type) { + case BT_UUID_TYPE_16: + u.u16.val = sys_le16_to_cpu(chrc->uuid16); break; - case BT_UUID_128: - memcpy(uuid.u128, chrc->uuid, sizeof(chrc->uuid)); + case BT_UUID_TYPE_128: + memcpy(u.u128.val, chrc->uuid, sizeof(chrc->uuid)); break; } BT_DBG("handle 0x%04x uuid %s properties 0x%02x", handle, - bt_uuid_str(&uuid), chrc->properties); + bt_uuid_str(&u.uuid), chrc->properties); /* Skip if UUID is set but doesn't match */ - if (params->uuid && bt_uuid_cmp(&uuid, params->uuid)) { + if (params->uuid && bt_uuid_cmp(&u.uuid, params->uuid)) { continue; } - attr = (&(struct bt_gatt_attr)BT_GATT_CHARACTERISTIC(&uuid, + attr = (&(struct bt_gatt_attr)BT_GATT_CHARACTERISTIC(&u.uuid, chrc->properties)); attr->handle = handle; @@ -1018,13 +1023,17 @@ static void att_find_info_rsp(struct bt_conn *conn, uint8_t err, { const struct bt_att_find_info_rsp *rsp = pdu; struct bt_gatt_discover_params *params = user_data; - struct bt_uuid uuid; uint16_t handle = 0; uint8_t len; union { const struct bt_att_info_16 *i16; const struct bt_att_info_128 *i128; } info; + union { + struct bt_uuid uuid; + struct bt_uuid_16 u16; + struct bt_uuid_128 u128; + } u; BT_DBG("err 0x%02x", err); @@ -1035,11 +1044,11 @@ static void att_find_info_rsp(struct bt_conn *conn, uint8_t err, /* Data can be either in UUID16 or UUID128 */ switch (rsp->format) { case BT_ATT_INFO_16: - uuid.type = BT_UUID_16; + u.uuid.type = BT_UUID_TYPE_16; len = sizeof(*info.i16); break; case BT_ATT_INFO_128: - uuid.type = BT_UUID_128; + u.uuid.type = BT_UUID_TYPE_128; len = sizeof(*info.i128); break; default: @@ -1055,24 +1064,24 @@ static void att_find_info_rsp(struct bt_conn *conn, uint8_t err, info.i16 = pdu; handle = sys_le16_to_cpu(info.i16->handle); - switch (uuid.type) { - case BT_UUID_16: - uuid.u16 = sys_le16_to_cpu(info.i16->uuid); + switch (u.uuid.type) { + case BT_UUID_TYPE_16: + u.u16.val = sys_le16_to_cpu(info.i16->uuid); break; - case BT_UUID_128: - memcpy(uuid.u128, info.i128->uuid, sizeof(uuid.u128)); + case BT_UUID_TYPE_128: + memcpy(u.u128.val, info.i128->uuid, 16); break; } - BT_DBG("handle 0x%04x uuid %s", handle, bt_uuid_str(&uuid)); + BT_DBG("handle 0x%04x uuid %s", handle, bt_uuid_str(&u.uuid)); /* Skip if UUID is set but doesn't match */ - if (params->uuid && bt_uuid_cmp(&uuid, params->uuid)) { + if (params->uuid && bt_uuid_cmp(&u.uuid, params->uuid)) { continue; } attr = (&(struct bt_gatt_attr) - BT_GATT_DESCRIPTOR(&uuid, 0, NULL, NULL, NULL)); + BT_GATT_DESCRIPTOR(&u.uuid, 0, NULL, NULL, NULL)); attr->handle = handle; if (params->func(conn, attr, params) == BT_GATT_ITER_STOP) { diff --git a/net/bluetooth/uuid.c b/net/bluetooth/uuid.c index f96c1814f0e..6302edc342c 100644 --- a/net/bluetooth/uuid.c +++ b/net/bluetooth/uuid.c @@ -20,48 +20,40 @@ #include #include #include +#include #include #if defined(CONFIG_BLUETOOTH_DEBUG) #include -#include #endif /* CONFIG_BLUETOOTH_DEBUG */ #define UUID_16_BASE_OFFSET 12 /* TODO: Decide wether to continue using BLE format or switch to RFC 4122 */ -static const struct bt_uuid uuid128_base = { - .type = BT_UUID_128, - .u128 = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - +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 }, }; -static inline void cpu_to_uuid128(void *dst, const void *src, int len) +static inline void u16_to_uuid128(void *dst, uint16_t u16) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - int i; - - for (i = 0; i < len; i++) { - ((uint8_t *)dst)[i] = ((uint8_t *)src)[(len - 1) - i]; - } -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - memcpy(dst, src, len); -#else -#error "Unknown byte order" + u16 = bswap_16(u16); #endif + memcpy(dst, &u16, 2); } -static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid *dst) +static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid_128 *dst) { switch (src->type) { - case BT_UUID_16: + case BT_UUID_TYPE_16: *dst = uuid128_base; - cpu_to_uuid128(&dst->u128[UUID_16_BASE_OFFSET], &src->u16, - sizeof(src->u16)); + u16_to_uuid128(&dst->val[UUID_16_BASE_OFFSET], + BT_UUID_16(src)->val); return; - case BT_UUID_128: + case BT_UUID_TYPE_128: memcpy(dst, src, sizeof(*dst)); return; } @@ -69,12 +61,12 @@ static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid *dst) static int uuid128_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2) { - struct bt_uuid uuid1, uuid2; + struct bt_uuid_128 uuid1, uuid2; uuid_to_uuid128(u1, &uuid1); uuid_to_uuid128(u2, &uuid2); - return memcmp(uuid1.u128, uuid2.u128, sizeof(uuid1.u128)); + return memcmp(uuid1.val, uuid2.val, 16); } int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2) @@ -84,10 +76,10 @@ int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2) return uuid128_cmp(u1, u2); switch (u1->type) { - case BT_UUID_16: - return (int)u1->u16 - (int)u2->u16; - case BT_UUID_128: - return memcmp(u1->u128, u2->u128, sizeof(u1->u128)); + case BT_UUID_TYPE_16: + return (int)BT_UUID_16(u1)->val - (int)BT_UUID_16(u2)->val; + case BT_UUID_TYPE_128: + return memcmp(BT_UUID_128(u1)->val, BT_UUID_128(u2)->val, 16); } return -EINVAL; @@ -96,9 +88,9 @@ int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2) void bt_uuid_copy(struct bt_uuid *dst, const struct bt_uuid *src) { switch (src->type) { - case BT_UUID_16: + case BT_UUID_TYPE_16: /* Copy only the necessary data */ - memcpy(dst, src, sizeof(struct __bt_uuid_16)); + memcpy(dst, src, sizeof(struct bt_uuid_16)); return; default: memcpy(dst, src, sizeof(*dst)); @@ -112,16 +104,16 @@ void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len) uint16_t tmp0, tmp2, tmp3, tmp4; switch (uuid->type) { - case BT_UUID_16: - snprintf(str, len, "%.4x", uuid->u16); + case BT_UUID_TYPE_16: + snprintf(str, len, "%.4x", BT_UUID_16(uuid)->val); break; - case BT_UUID_128: - memcpy(&tmp0, &uuid->u128[0], sizeof(tmp0)); - memcpy(&tmp1, &uuid->u128[2], sizeof(tmp1)); - memcpy(&tmp2, &uuid->u128[6], sizeof(tmp2)); - memcpy(&tmp3, &uuid->u128[8], sizeof(tmp3)); - memcpy(&tmp4, &uuid->u128[10], sizeof(tmp4)); - memcpy(&tmp5, &uuid->u128[12], sizeof(tmp5)); + case BT_UUID_TYPE_128: + memcpy(&tmp0, &BT_UUID_128(uuid)->val[0], sizeof(tmp0)); + memcpy(&tmp1, &BT_UUID_128(uuid)->val[2], sizeof(tmp1)); + memcpy(&tmp2, &BT_UUID_128(uuid)->val[6], sizeof(tmp2)); + memcpy(&tmp3, &BT_UUID_128(uuid)->val[8], sizeof(tmp3)); + memcpy(&tmp4, &BT_UUID_128(uuid)->val[10], sizeof(tmp4)); + memcpy(&tmp5, &BT_UUID_128(uuid)->val[12], sizeof(tmp5)); snprintf(str, len, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", tmp5, tmp4, tmp3, tmp2, tmp1, tmp0); diff --git a/samples/bluetooth/central_hr/src/main.c b/samples/bluetooth/central_hr/src/main.c index 7d81438fb91..463a51d68ff 100644 --- a/samples/bluetooth/central_hr/src/main.c +++ b/samples/bluetooth/central_hr/src/main.c @@ -34,7 +34,7 @@ static struct bt_conn *default_conn; -static struct bt_uuid uuid; +static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; @@ -56,9 +56,9 @@ static uint8_t discover_func(struct bt_conn *conn, printk("[ATTRIBUTE] handle %u\n", attr->handle); - if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_HRS)) { - bt_uuid_copy(&uuid, BT_UUID_HRS_MEASUREMENT); - discover_params.uuid = &uuid; + if (BT_UUID_16(discover_params.uuid)->val == BT_UUID_HRS_VAL) { + uuid.val = BT_UUID_HRS_MEASUREMENT_VAL; + discover_params.uuid = &uuid.uuid; discover_params.start_handle = attr->handle + 1; discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; @@ -66,10 +66,10 @@ static uint8_t discover_func(struct bt_conn *conn, if (err) { printk("Discover failed (err %d)\n", err); } - } else if (!bt_uuid_cmp(discover_params.uuid, - BT_UUID_HRS_MEASUREMENT)) { - bt_uuid_copy(&uuid, BT_UUID_GATT_CCC); - discover_params.uuid = &uuid; + } else if (BT_UUID_16(discover_params.uuid)->val == + BT_UUID_HRS_MEASUREMENT_VAL) { + uuid.val = BT_UUID_GATT_CCC_VAL; + discover_params.uuid = &uuid.uuid; discover_params.start_handle = attr->handle + 2; discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; subscribe_params.value_handle = attr->handle + 1; @@ -109,8 +109,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) printk("Connected: %s\n", addr); if (conn == default_conn) { - bt_uuid_copy(&uuid, BT_UUID_HRS); - discover_params.uuid = &uuid; + uuid.val = BT_UUID_HRS_VAL; + discover_params.uuid = &uuid.uuid; discover_params.func = discover_func; discover_params.start_handle = 0x0001; discover_params.end_handle = 0xffff; @@ -145,7 +145,7 @@ static bool eir_found(uint8_t type, const uint8_t *data, uint8_t data_len, int err; memcpy(&u16, &data[i], sizeof(u16)); - if (sys_le16_to_cpu(u16) != BT_UUID_HRS->u16) { + if (sys_le16_to_cpu(u16) != BT_UUID_HRS_VAL) { continue; } diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index cb482a70130..6ae93716b10 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -168,23 +168,17 @@ static int read_manuf(struct bt_conn *conn, const struct bt_gatt_attr *attr, } /* Custom Service Variables */ -static struct bt_uuid vnd_uuid = { - .type = BT_UUID_128, - .u128 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, -}; +static struct bt_uuid_128 vnd_uuid = BT_UUID_INIT_128( + 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); -static struct bt_uuid vnd_enc_uuid = { - .type = BT_UUID_128, - .u128 = { 0xf1, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, -}; +static struct bt_uuid_128 vnd_enc_uuid = BT_UUID_INIT_128( + 0xf1, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); -static struct bt_uuid vnd_auth_uuid = { - .type = BT_UUID_128, - .u128 = { 0xf2, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, -}; +static struct bt_uuid_128 vnd_auth_uuid = BT_UUID_INIT_128( + 0xf2, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static uint8_t vnd_value[] = { 'V', 'e', 'n', 'd', 'o', 'r' }; @@ -266,11 +260,9 @@ static int flush_long_vnd(struct bt_conn *conn, return -EINVAL; } -static const struct bt_uuid vnd_long_uuid = { - .type = BT_UUID_128, - .u128 = { 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, -}; +static const struct bt_uuid_128 vnd_long_uuid = BT_UUID_INIT_128( + 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static struct bt_gatt_cep vnd_long_cep = { .properties = BT_GATT_CEP_RELIABLE_WRITE, @@ -301,11 +293,9 @@ static int write_signed(struct bt_conn *conn, const struct bt_gatt_attr *attr, return len; } -static const struct bt_uuid vnd_signed_uuid = { - .type = BT_UUID_128, - .u128 = { 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x13, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x13 }, -}; +static const struct bt_uuid_128 vnd_signed_uuid = BT_UUID_INIT_128( + 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x13, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x13); static struct bt_gatt_attr gap_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP), @@ -370,28 +360,28 @@ static struct bt_gatt_attr dis_attrs[] = { static struct bt_gatt_attr vnd_attrs[] = { /* Vendor Primary Service Declaration */ BT_GATT_PRIMARY_SERVICE(&vnd_uuid), - BT_GATT_CHARACTERISTIC(&vnd_enc_uuid, + BT_GATT_CHARACTERISTIC(&vnd_enc_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE), - BT_GATT_DESCRIPTOR(&vnd_enc_uuid, + BT_GATT_DESCRIPTOR(&vnd_enc_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE | BT_GATT_PERM_WRITE_ENCRYPT, read_vnd, write_vnd, vnd_value), - BT_GATT_CHARACTERISTIC(&vnd_auth_uuid, + BT_GATT_CHARACTERISTIC(&vnd_auth_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE), - BT_GATT_DESCRIPTOR(&vnd_auth_uuid, + BT_GATT_DESCRIPTOR(&vnd_auth_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE | BT_GATT_PERM_WRITE_AUTHEN, read_vnd, write_vnd, vnd_value), - BT_GATT_CHARACTERISTIC(&vnd_long_uuid, BT_GATT_CHRC_READ | + BT_GATT_CHARACTERISTIC(&vnd_long_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_EXT_PROP), - BT_GATT_LONG_DESCRIPTOR(&vnd_long_uuid, + BT_GATT_LONG_DESCRIPTOR(&vnd_long_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_long_vnd, write_long_vnd, flush_long_vnd, &vnd_long_value), BT_GATT_CEP(&vnd_long_cep), - BT_GATT_CHARACTERISTIC(&vnd_signed_uuid, BT_GATT_CHRC_READ | + BT_GATT_CHARACTERISTIC(&vnd_signed_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_AUTH), - BT_GATT_DESCRIPTOR(&vnd_signed_uuid, + BT_GATT_DESCRIPTOR(&vnd_signed_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_signed, write_signed, &signed_value), }; diff --git a/samples/bluetooth/shell/src/main.c b/samples/bluetooth/shell/src/main.c index b5710f077d7..efd6134e2fd 100644 --- a/samples/bluetooth/shell/src/main.c +++ b/samples/bluetooth/shell/src/main.c @@ -626,7 +626,7 @@ fail: } static struct bt_gatt_discover_params discover_params; -static struct bt_uuid uuid; +static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); static void print_chrc_props(uint8_t properties) { @@ -740,10 +740,9 @@ static void cmd_gatt_discover(int argc, char *argv[]) } /* Only set the UUID if the value is valid (non zero) */ - uuid.u16 = strtoul(argv[1], NULL, 16); - if (uuid.u16) { - uuid.type = BT_UUID_16; - discover_params.uuid = &uuid; + uuid.val = strtoul(argv[1], NULL, 16); + if (uuid.val) { + discover_params.uuid = &uuid.uuid; } if (argc > 2) { diff --git a/samples/bluetooth/tester/src/gatt.c b/samples/bluetooth/tester/src/gatt.c index a23c7f9d6da..b2f5c56d565 100644 --- a/samples/bluetooth/tester/src/gatt.c +++ b/samples/bluetooth/tester/src/gatt.c @@ -122,18 +122,17 @@ static struct bt_gatt_attr *gatt_db_add(const struct bt_gatt_attr *pattern) static uint8_t btp2bt_uuid(const uint8_t *uuid, uint8_t len, struct bt_uuid *bt_uuid) { - switch (len) { - case 0x02: { /* UUID 16 */ - uint16_t u16; + uint16_t le16; - bt_uuid->type = BT_UUID_16; - memcpy(&u16, uuid, sizeof(u16)); - bt_uuid->u16 = sys_le16_to_cpu(u16); + switch (len) { + case 0x02: /* UUID 16 */ + bt_uuid->type = BT_UUID_TYPE_16; + memcpy(&le16, uuid, sizeof(le16)); + BT_UUID_16(bt_uuid)->val = sys_le16_to_cpu(le16); break; - } case 0x10: /* UUID 128*/ - bt_uuid->type = BT_UUID_128; - memcpy(&bt_uuid->u128, uuid, sizeof(bt_uuid->u128)); + bt_uuid->type = BT_UUID_TYPE_128; + memcpy(BT_UUID_128(bt_uuid)->val, uuid, 16); break; default: return BTP_STATUS_FAILED; @@ -839,8 +838,7 @@ static uint8_t disc_prim_uuid_cb(struct bt_conn *conn, struct gatt_service *service; uint8_t uuid_length; - uuid_length = data->uuid->type == BT_UUID_16 ? sizeof(data->uuid->u16) : - sizeof(data->uuid->u128); + uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; service = gatt_buf_reserve(sizeof(*service) + uuid_length); if (!service) { @@ -858,12 +856,13 @@ static uint8_t disc_prim_uuid_cb(struct bt_conn *conn, service->end_handle = sys_cpu_to_le16(data->end_handle); service->uuid_length = uuid_length; - if (data->uuid->type == BT_UUID_16) { - uint16_t u16 = sys_cpu_to_le16(data->uuid->u16); + if (data->uuid->type == BT_UUID_TYPE_16) { + uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); memcpy(service->uuid, &u16, uuid_length); } else { - memcpy(service->uuid, &data->uuid->u128, uuid_length); + memcpy(service->uuid, BT_UUID_128(data->uuid)->val, + uuid_length); } rp->services_count++; @@ -936,8 +935,7 @@ static uint8_t find_included_cb(struct bt_conn *conn, struct gatt_included *included; uint8_t uuid_length; - uuid_length = data->uuid->type == BT_UUID_16 ? sizeof(data->uuid->u16) : - sizeof(data->uuid->u128); + uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; included = gatt_buf_reserve(sizeof(*included) + uuid_length); if (!included) { @@ -956,8 +954,8 @@ static uint8_t find_included_cb(struct bt_conn *conn, included->service.end_handle = sys_cpu_to_le16(data->end_handle); included->service.uuid_length = uuid_length; - if (data->uuid->type == BT_UUID_16) { - uint16_t u16 = sys_cpu_to_le16(data->uuid->u16); + if (data->uuid->type == BT_UUID_TYPE_16) { + uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); memcpy(included->service.uuid, &u16, uuid_length); } else { @@ -1030,8 +1028,7 @@ static uint8_t disc_chrc_cb(struct bt_conn *conn, struct gatt_characteristic *chrc; uint8_t uuid_length; - uuid_length = data->uuid->type == BT_UUID_16 ? sizeof(data->uuid->u16) : - sizeof(data->uuid->u128); + uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; chrc = gatt_buf_reserve(sizeof(*chrc) + uuid_length); if (!chrc) { @@ -1050,12 +1047,12 @@ static uint8_t disc_chrc_cb(struct bt_conn *conn, chrc->value_handle = sys_cpu_to_le16(attr->handle + 1); chrc->uuid_length = uuid_length; - if (data->uuid->type == BT_UUID_16) { - uint16_t u16 = sys_cpu_to_le16(data->uuid->u16); + if (data->uuid->type == BT_UUID_TYPE_16) { + uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); memcpy(chrc->uuid, &u16, uuid_length); } else { - memcpy(chrc->uuid, &data->uuid->u128, uuid_length); + memcpy(chrc->uuid, BT_UUID_128(data->uuid)->val, uuid_length); } rp->characteristics_count++; @@ -1178,8 +1175,7 @@ static uint8_t disc_all_desc_cb(struct bt_conn *conn, struct gatt_descriptor *descriptor; uint8_t uuid_length; - uuid_length = attr->uuid->type == BT_UUID_16 ? sizeof(attr->uuid->u16) : - sizeof(attr->uuid->u128); + uuid_length = attr->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; descriptor = gatt_buf_reserve(sizeof(*descriptor) + uuid_length); if (!descriptor) { @@ -1196,12 +1192,13 @@ static uint8_t disc_all_desc_cb(struct bt_conn *conn, descriptor->descriptor_handle = sys_cpu_to_le16(attr->handle); descriptor->uuid_length = uuid_length; - if (attr->uuid->type == BT_UUID_16) { - uint16_t u16 = sys_cpu_to_le16(attr->uuid->u16); + if (attr->uuid->type == BT_UUID_TYPE_16) { + uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(attr->uuid)->val); memcpy(descriptor->uuid, &u16, uuid_length); } else { - memcpy(descriptor->uuid, &attr->uuid->u128, uuid_length); + memcpy(descriptor->uuid, BT_UUID_128(attr->uuid)->val, + uuid_length); } rp->descriptors_count++;