diff --git a/nimble/host/include/host/ble_gatt.h b/nimble/host/include/host/ble_gatt.h index 5055beb89..50b3aade7 100644 --- a/nimble/host/include/host/ble_gatt.h +++ b/nimble/host/include/host/ble_gatt.h @@ -22,6 +22,7 @@ #include #include "host/ble_att.h" +#include "host/ble_uuid.h" #ifdef __cplusplus extern "C" { #endif @@ -80,7 +81,7 @@ struct ble_gatt_error { struct ble_gatt_svc { uint16_t start_handle; uint16_t end_handle; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; }; struct ble_gatt_attr { @@ -93,12 +94,12 @@ struct ble_gatt_chr { uint16_t def_handle; uint16_t val_handle; uint8_t properties; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; }; struct ble_gatt_dsc { uint16_t handle; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; }; typedef int ble_gatt_mtu_fn(uint16_t conn_handle, @@ -143,7 +144,7 @@ int ble_gattc_exchange_mtu(uint16_t conn_handle, ble_gatt_mtu_fn *cb, void *cb_arg); int ble_gattc_disc_all_svcs(uint16_t conn_handle, ble_gatt_disc_svc_fn *cb, void *cb_arg); -int ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const void *svc_uuid128, +int ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid, ble_gatt_disc_svc_fn *cb, void *cb_arg); int ble_gattc_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, @@ -152,7 +153,7 @@ int ble_gattc_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, ble_gatt_chr_fn *cb, void *cb_arg); int ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, - uint16_t end_handle, const void *uuid128, + uint16_t end_handle, const ble_uuid_t *uuid, ble_gatt_chr_fn *cb, void *cb_arg); int ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t chr_val_handle, uint16_t chr_end_handle, @@ -160,7 +161,7 @@ int ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t chr_val_handle, int ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle, ble_gatt_attr_fn *cb, void *cb_arg); int ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, - uint16_t end_handle, const void *uuid128, + uint16_t end_handle, const ble_uuid_t *uuid, ble_gatt_attr_fn *cb, void *cb_arg); int ble_gattc_read_long(uint16_t conn_handle, uint16_t handle, uint16_t offset, ble_gatt_attr_fn *cb, void *cb_arg); @@ -201,10 +202,10 @@ typedef uint16_t ble_gatt_chr_flags; struct ble_gatt_chr_def { /** - * Pointer to first element in a uint8_t[16]; use the BLE_UUID16 macro for - * 16-bit UUIDs; NULL if there are no more characteristics in the service. + * Pointer to characteristic UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. */ - const uint8_t *uuid128; + const ble_uuid_t *uuid; /** * Callback that gets executed when this characteristic is read or @@ -245,10 +246,10 @@ struct ble_gatt_svc_def { uint8_t type; /** - * Pointer to first element in a uint8_t[16]; use the BLE_UUID16 macro for - * 16-bit UUIDs. + * Pointer to service UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. */ - const uint8_t *uuid128; + const ble_uuid_t *uuid; /** * Array of pointers to other service definitions. These services are @@ -266,10 +267,10 @@ struct ble_gatt_svc_def { struct ble_gatt_dsc_def { /** - * The first element in a uint8_t[16]; use the BLE_UUID16 macro for 16-bit - * UUIDs; NULL if there are no more descriptors in the characteristic. + * Pointer to descriptor UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. */ - uint8_t *uuid128; + const ble_uuid_t *uuid; /** Specifies the set of permitted operations for this descriptor. */ uint8_t att_flags; @@ -450,11 +451,11 @@ int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs); void ble_gatts_chr_updated(uint16_t chr_def_handle); -int ble_gatts_find_svc(const void *uuid128, uint16_t *out_handle); -int ble_gatts_find_chr(const void *svc_uuid128, const void *chr_uuid128, +int ble_gatts_find_svc(const ble_uuid_t *uuid, uint16_t *out_handle); +int ble_gatts_find_chr(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, uint16_t *out_def_handle, uint16_t *out_val_handle); -int ble_gatts_find_dsc(const void *svc_uuid128, const void *chr_uuid128, - const void *dsc_uuid128, uint16_t *out_dsc_handle); +int ble_gatts_find_dsc(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, + const ble_uuid_t *dsc_uuid, uint16_t *out_dsc_handle); #ifdef __cplusplus } diff --git a/nimble/host/include/host/ble_uuid.h b/nimble/host/include/host/ble_uuid.h index 2a7d53db7..a2be09541 100644 --- a/nimble/host/include/host/ble_uuid.h +++ b/nimble/host/include/host/ble_uuid.h @@ -21,30 +21,90 @@ #define H_BLE_UUID_ #include +#include + #ifdef __cplusplus extern "C" { #endif struct os_mbuf; -uint16_t ble_uuid_128_to_16(const void *uuid128); -int ble_uuid_16_to_128(uint16_t uuid16, void *dst); +enum { + BLE_UUID_TYPE_16 = 16, + BLE_UUID_TYPE_32 = 32, + BLE_UUID_TYPE_128 = 128, +}; -#define BLE_UUID16_ARR(uuid16) { \ - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, \ - 0x00, 0x10, 0x00, 0x00, (uuid16) & 0xff, (((uuid16) & 0xff00) >> 8), \ - 0x00, 0x00 \ -} +/* Generic UUID type, to be used only as a pointer */ +typedef struct { + uint8_t type; +} ble_uuid_t; -/** - * Expands to a designated initializer byte array containing the 128-bit - * representation of the specified 16-bit UUID. - */ -#define BLE_UUID16(uuid16) ((uint8_t[16]) { \ - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, \ - 0x00, 0x10, 0x00, 0x00, (uuid16) & 0xff, (((uuid16) & 0xff00) >> 8), \ - 0x00, 0x00 \ -}) +typedef struct { + ble_uuid_t u; + uint16_t value; +} ble_uuid16_t; + +typedef struct { + ble_uuid_t u; + uint32_t value; +} ble_uuid32_t; + +typedef struct { + ble_uuid_t u; + uint8_t value[16]; +} ble_uuid128_t; + +/* Universal UUID type, to be used for any-UUID static allocation */ +typedef union { + ble_uuid_t u; + ble_uuid16_t u16; + ble_uuid32_t u32; + ble_uuid128_t u128; +} ble_uuid_any_t; + +#define BLE_UUID16_INIT(uuid16) \ + { \ + .u.type = BLE_UUID_TYPE_16, \ + .value = (uuid16), \ + } + +#define BLE_UUID32_INIT(uuid32) \ + { \ + .u.type = BLE_UUID_TYPE_32, \ + .value = (uuid32), \ + } + +#define BLE_UUID128_INIT(uuid128...) \ + { \ + .u.type = BLE_UUID_TYPE_128, \ + .value = { uuid128 }, \ + } + +#define BLE_UUID16_DECLARE(uuid16) \ + ((ble_uuid_t *) (&(ble_uuid16_t) BLE_UUID16_INIT(uuid16))) + +#define BLE_UUID32_DECLARE(uuid32) \ + ((ble_uuid_t *) (&(ble_uuid32_t) BLE_UUID32_INIT(uuid32))) + +#define BLE_UUID128_DECLARE(uuid128...) \ + ((ble_uuid_t *) (&(ble_uuid128_t) BLE_UUID128_INIT(uuid128))) + +#define BLE_UUID16(u) \ + ((ble_uuid16_t *) (u)) + +#define BLE_UUID32(u) \ + ((ble_uuid32_t *) (u)) + +#define BLE_UUID128(u) \ + ((ble_uuid128_t *) (u)) + +#define BLE_UUID_STR_LEN (37) + +int ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len); +int ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2); +char *ble_uuid_to_str(const ble_uuid_t *uuid, char *dst); +uint16_t ble_uuid_u16(const ble_uuid_t *uuid); #ifdef __cplusplus } diff --git a/nimble/host/profiles/lls/src/ble_svc_lls.c b/nimble/host/profiles/lls/src/ble_svc_lls.c index dca7ee81d..c896c80c2 100644 --- a/nimble/host/profiles/lls/src/ble_svc_lls.c +++ b/nimble/host/profiles/lls/src/ble_svc_lls.c @@ -42,10 +42,10 @@ static const struct ble_gatt_svc_def ble_svc_lls_defs[] = { { /*** Service: Link Loss Service (LLS). */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_LLS_UUID16), + .uuid = BLE_UUID16(BLE_SVC_LLS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Alert Level. */ - .uuid128 = BLE_UUID16(BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL), + .uuid = BLE_UUID16(BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL), .access_cb = ble_svc_lls_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, }, { diff --git a/nimble/host/services/ans/src/ble_svc_ans.c b/nimble/host/services/ans/src/ble_svc_ans.c index ca4ee8a02..f3386a5d9 100644 --- a/nimble/host/services/ans/src/ble_svc_ans.c +++ b/nimble/host/services/ans/src/ble_svc_ans.c @@ -81,14 +81,14 @@ static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { { /*** Alert Notification Service. */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_ANS_UUID16), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /** Supported New Alert Catagory * * This characteristic exposes what categories of new * alert are supported in the server. */ - .uuid128 = BLE_UUID16(BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT), .access_cb = ble_svc_ans_access, .flags = BLE_GATT_CHR_F_READ, }, { @@ -97,7 +97,7 @@ static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { * This characteristic exposes information about * the count of new alerts (for a given category). */ - .uuid128 = BLE_UUID16(BLE_SVC_ANS_CHR_UUID16_NEW_ALERT), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_NEW_ALERT), .access_cb = ble_svc_ans_access, .val_handle = &ble_svc_ans_new_alert_val_handle, .flags = BLE_GATT_CHR_F_NOTIFY, @@ -107,7 +107,7 @@ static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { * This characteristic exposes what categories of * unread alert are supported in the server. */ - .uuid128 = BLE_UUID16(BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT), .access_cb = ble_svc_ans_access, .flags = BLE_GATT_CHR_F_READ, }, { @@ -116,7 +116,7 @@ static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { * This characteristic exposes the count of unread * alert events existing in the server. */ - .uuid128 = BLE_UUID16(BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT), .access_cb = ble_svc_ans_access, .val_handle = &ble_svc_ans_unr_alert_val_handle, .flags = BLE_GATT_CHR_F_NOTIFY, @@ -130,7 +130,7 @@ static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { * Client Characteristic Configuration for each alert * characteristic. */ - .uuid128 = BLE_UUID16(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT), .access_cb = ble_svc_ans_access, .flags = BLE_GATT_CHR_F_WRITE, }, { @@ -160,7 +160,7 @@ ble_svc_ans_access(uint16_t conn_handle, uint16_t attr_handle, uint8_t cat_bit_mask; int i; - uuid16 = ble_uuid_128_to_16(ctxt->chr->uuid128); + uuid16 = ble_uuid_u16(ctxt->chr->uuid); assert(uuid16 != 0); switch (uuid16) { diff --git a/nimble/host/services/bleuart/include/bleuart/bleuart.h b/nimble/host/services/bleuart/include/bleuart/bleuart.h index a8c29c15a..ab64b9c07 100644 --- a/nimble/host/services/bleuart/include/bleuart/bleuart.h +++ b/nimble/host/services/bleuart/include/bleuart/bleuart.h @@ -33,7 +33,7 @@ bleuart_gatt_svr_init(void); void bleuart_set_conn_handle(uint16_t conn_handle); -extern const uint8_t gatt_svr_svc_uart[16]; +extern const ble_uuid128_t gatt_svr_svc_uart_uuid; #ifdef __cplusplus } diff --git a/nimble/host/services/bleuart/src/bleuart.c b/nimble/host/services/bleuart/src/bleuart.c index 3c26ffc31..d481a8641 100644 --- a/nimble/host/services/bleuart/src/bleuart.c +++ b/nimble/host/services/bleuart/src/bleuart.c @@ -23,6 +23,7 @@ #include "sysinit/sysinit.h" #include "host/ble_hs.h" +#include "host/ble_uuid.h" #include "bleuart/bleuart.h" #include "os/endian.h" #include "console/console.h" @@ -47,23 +48,20 @@ uint16_t g_console_conn_handle; */ /* {6E400001-B5A3-F393-E0A9-E50E24DCCA9E} */ -const uint8_t gatt_svr_svc_uart[16] = { - 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, - 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e -}; +const ble_uuid128_t gatt_svr_svc_uart_uuid = + BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e); /* {6E400002-B5A3-F393-E0A9-E50E24DCCA9E} */ -const uint8_t gatt_svr_chr_uart_write[16] = { - 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, - 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e -}; +const ble_uuid128_t gatt_svr_chr_uart_write_uuid = + BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e); /* {6E400003-B5A3-F393-E0A9-E50E24DCCA9E} */ -const uint8_t gatt_svr_chr_uart_read[16] = { - 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, - 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e -}; +const ble_uuid128_t gatt_svr_chr_uart_read_uuid = + BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e); static int gatt_svr_chr_access_uart_write(uint16_t conn_handle, uint16_t attr_handle, @@ -73,15 +71,15 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = { { /* Service: uart */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = (void *)gatt_svr_svc_uart, + .uuid = &gatt_svr_svc_uart_uuid.u, .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = gatt_svr_chr_uart_read, + .uuid = &gatt_svr_chr_uart_read_uuid.u, .val_handle = &g_bleuart_attr_read_handle, .access_cb = gatt_svr_chr_access_uart_write, .flags = BLE_GATT_CHR_F_NOTIFY, }, { /* Characteristic: Write */ - .uuid128 = (void *)gatt_svr_chr_uart_write, + .uuid = &gatt_svr_chr_uart_write_uuid.u, .access_cb = gatt_svr_chr_access_uart_write, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP, .val_handle = &g_bleuart_attr_write_handle, diff --git a/nimble/host/services/gap/src/ble_svc_gap.c b/nimble/host/services/gap/src/ble_svc_gap.c index e0ccd7ca1..87de6fb1e 100644 --- a/nimble/host/services/gap/src/ble_svc_gap.c +++ b/nimble/host/services/gap/src/ble_svc_gap.c @@ -41,31 +41,31 @@ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = { { /*** Service: GAP. */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_GAP_UUID16), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Device Name. */ - .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME), .access_cb = ble_svc_gap_access, .flags = BLE_GATT_CHR_F_READ, }, { /*** Characteristic: Appearance. */ - .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_APPEARANCE), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_APPEARANCE), .access_cb = ble_svc_gap_access, .flags = BLE_GATT_CHR_F_READ, }, { /*** Characteristic: Peripheral Privacy Flag. */ - .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_PERIPH_PRIV_FLAG), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_PERIPH_PRIV_FLAG), .access_cb = ble_svc_gap_access, .flags = BLE_GATT_CHR_F_READ, }, { /*** Characteristic: Reconnection Address. */ - .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_RECONNECT_ADDR), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_RECONNECT_ADDR), .access_cb = ble_svc_gap_access, .flags = BLE_GATT_CHR_F_WRITE, }, { /*** Characteristic: Peripheral Preferred Connection Parameters. */ - .uuid128 = - BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS), + .uuid = + BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS), .access_cb = ble_svc_gap_access, .flags = BLE_GATT_CHR_F_READ, }, { @@ -85,7 +85,7 @@ ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle, uint16_t uuid16; int rc; - uuid16 = ble_uuid_128_to_16(ctxt->chr->uuid128); + uuid16 = ble_uuid_u16(ctxt->chr->uuid); assert(uuid16 != 0); switch (uuid16) { diff --git a/nimble/host/services/gatt/src/ble_svc_gatt.c b/nimble/host/services/gatt/src/ble_svc_gatt.c index 666e67244..8b7139dfe 100644 --- a/nimble/host/services/gatt/src/ble_svc_gatt.c +++ b/nimble/host/services/gatt/src/ble_svc_gatt.c @@ -35,9 +35,9 @@ static const struct ble_gatt_svc_def ble_svc_gatt_defs[] = { { /*** Service: GATT */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_GATT_SVC_UUID16), + .uuid = BLE_UUID16_DECLARE(BLE_GATT_SVC_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16), + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16), .access_cb = ble_svc_gatt_access, .val_handle = &ble_svc_gatt_changed_val_handle, .flags = BLE_GATT_CHR_F_INDICATE, diff --git a/nimble/host/services/ias/src/ble_svc_ias.c b/nimble/host/services/ias/src/ble_svc_ias.c index 5644ccd30..74c9778fa 100644 --- a/nimble/host/services/ias/src/ble_svc_ias.c +++ b/nimble/host/services/ias/src/ble_svc_ias.c @@ -44,10 +44,10 @@ static const struct ble_gatt_svc_def ble_svc_ias_defs[] = { { /*** Service: Immediate Alert Service (IAS). */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_IAS_UUID16), + .uuid = BLE_UUID16(BLE_SVC_IAS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Alert Level. */ - .uuid128 = BLE_UUID16(BLE_SVC_IAS_CHR_UUID16_ALERT_LEVEL), + .uuid = BLE_UUID16(BLE_SVC_IAS_CHR_UUID16_ALERT_LEVEL), .access_cb = ble_svc_ias_access, .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, }, { diff --git a/nimble/host/services/lls/src/ble_svc_lls.c b/nimble/host/services/lls/src/ble_svc_lls.c index 9c7b90078..434814f34 100644 --- a/nimble/host/services/lls/src/ble_svc_lls.c +++ b/nimble/host/services/lls/src/ble_svc_lls.c @@ -42,10 +42,10 @@ static const struct ble_gatt_svc_def ble_svc_lls_defs[] = { { /*** Service: Link Loss Service (LLS). */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_LLS_UUID16), + .uuid = BLE_UUID16(BLE_SVC_LLS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Alert Level. */ - .uuid128 = BLE_UUID16(BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL), + .uuid = BLE_UUID16(BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL), .access_cb = ble_svc_lls_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, }, { diff --git a/nimble/host/services/tps/src/ble_svc_tps.c b/nimble/host/services/tps/src/ble_svc_tps.c index 74c2df46d..fe71b7921 100644 --- a/nimble/host/services/tps/src/ble_svc_tps.c +++ b/nimble/host/services/tps/src/ble_svc_tps.c @@ -41,10 +41,10 @@ static const struct ble_gatt_svc_def ble_svc_tps_defs[] = { { /*** Service: Tx Power Service. */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(BLE_SVC_TPS_UUID16), + .uuid = BLE_UUID16(BLE_SVC_TPS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Tx Power Level. */ - .uuid128 = BLE_UUID16(BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL), + .uuid = BLE_UUID16(BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL), .access_cb = ble_svc_tps_access, .flags = BLE_GATT_CHR_F_READ, }, { diff --git a/nimble/host/src/ble_att_clt.c b/nimble/host/src/ble_att_clt.c index fe57e8d11..8b3a95dd0 100644 --- a/nimble/host/src/ble_att_clt.c +++ b/nimble/host/src/ble_att_clt.c @@ -238,7 +238,6 @@ static int ble_att_clt_parse_find_info_entry(struct os_mbuf **rxom, uint8_t rsp_format, struct ble_att_find_info_idata *idata) { - uint16_t uuid16; int entry_len; int rc; @@ -264,15 +263,14 @@ ble_att_clt_parse_find_info_entry(struct os_mbuf **rxom, uint8_t rsp_format, switch (rsp_format) { case BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT: - uuid16 = le16toh((*rxom)->om_data + 2); - rc = ble_uuid_16_to_128(uuid16, idata->uuid128); + rc = ble_uuid_init_from_mbuf(&idata->uuid, *rxom, 2, 2); if (rc != 0) { return BLE_HS_EBADDATA; } break; case BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT: - rc = os_mbuf_copydata(*rxom, 2, 16, idata->uuid128); + rc = ble_uuid_init_from_mbuf(&idata->uuid, *rxom, 2, 16); if (rc != 0) { return BLE_HS_EBADDATA; } @@ -441,7 +439,7 @@ ble_att_clt_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom) int ble_att_clt_tx_read_type(uint16_t conn_handle, const struct ble_att_read_type_req *req, - const void *uuid128) + const ble_uuid_t *uuid) { #if !NIMBLE_BLE_ATT_CLT_READ_TYPE return BLE_HS_ENOTSUP; @@ -465,7 +463,7 @@ ble_att_clt_tx_read_type(uint16_t conn_handle, } ble_att_read_type_req_write(txom->om_data, txom->om_len, req); - rc = ble_uuid_append(txom, uuid128); + rc = ble_uuid_to_mbuf(uuid, txom); if (rc != 0) { rc = BLE_HS_ENOMEM; goto err; @@ -750,7 +748,7 @@ ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom) int ble_att_clt_tx_read_group_type(uint16_t conn_handle, const struct ble_att_read_group_type_req *req, - const void *uuid128) + const ble_uuid_t *uuid) { #if !NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE return BLE_HS_ENOTSUP; @@ -774,7 +772,7 @@ ble_att_clt_tx_read_group_type(uint16_t conn_handle, } ble_att_read_group_type_req_write(txom->om_data, txom->om_len, req); - rc = ble_uuid_append(txom, uuid128); + rc = ble_uuid_to_mbuf(uuid, txom); if (rc != 0) { goto err; } diff --git a/nimble/host/src/ble_att_priv.h b/nimble/host/src/ble_att_priv.h index 44e4d23f9..ed696df95 100644 --- a/nimble/host/src/ble_att_priv.h +++ b/nimble/host/src/ble_att_priv.h @@ -24,6 +24,7 @@ #include "stats/stats.h" #include "os/queue.h" #include "host/ble_att.h" +#include "host/ble_uuid.h" #ifdef __cplusplus extern "C" { #endif @@ -139,17 +140,14 @@ typedef int ble_att_svr_access_fn(uint16_t conn_handle, uint16_t attr_handle, uint8_t op, uint16_t offset, struct os_mbuf **om, void *arg); -int ble_att_svr_register(const uint8_t *uuid, uint8_t flags, +int ble_att_svr_register(const ble_uuid_t *uuid, uint8_t flags, uint8_t min_key_size, uint16_t *handle_id, ble_att_svr_access_fn *cb, void *cb_arg); -int ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags, - uint8_t min_key_size, uint16_t *handle_id, - ble_att_svr_access_fn *cb, void *cb_arg); struct ble_att_svr_entry { STAILQ_ENTRY(ble_att_svr_entry) ha_next; - uint8_t ha_uuid[16]; + const ble_uuid_t *ha_uuid; uint8_t ha_flags; uint8_t ha_min_key_size; uint16_t ha_handle_id; @@ -182,7 +180,7 @@ int ble_att_svr_start(void); struct ble_att_svr_entry * ble_att_svr_find_by_uuid(struct ble_att_svr_entry *start_at, - const uint8_t *uuid, + const ble_uuid_t *uuid, uint16_t end_handle); uint16_t ble_att_svr_prev_handle(void); int ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom); @@ -225,7 +223,7 @@ int ble_att_svr_init(void); /** An information-data entry in a find information response. */ struct ble_att_find_info_idata { uint16_t attr_handle; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; }; /** A handles-information entry in a find by type value response. */ @@ -265,11 +263,11 @@ int ble_att_clt_tx_read_mult(uint16_t conn_handle, int ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom); int ble_att_clt_tx_read_type(uint16_t conn_handle, const struct ble_att_read_type_req *req, - const void *uuid128); + const ble_uuid_t *uuid); int ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom); int ble_att_clt_tx_read_group_type( uint16_t conn_handle, const struct ble_att_read_group_type_req *req, - const void *uuid128); + const ble_uuid_t *uuid128); int ble_att_clt_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom); int ble_att_clt_tx_find_info(uint16_t conn_handle, diff --git a/nimble/host/src/ble_att_svr.c b/nimble/host/src/ble_att_svr.c index cdd02a827..332f09740 100644 --- a/nimble/host/src/ble_att_svr.c +++ b/nimble/host/src/ble_att_svr.c @@ -97,9 +97,9 @@ ble_att_svr_next_id(void) * @return 0 on success, non-zero error code on failure. */ int -ble_att_svr_register(const uint8_t *uuid, uint8_t flags, uint8_t min_key_size, - uint16_t *handle_id, ble_att_svr_access_fn *cb, - void *cb_arg) +ble_att_svr_register(const ble_uuid_t *uuid, uint8_t flags, + uint8_t min_key_size, uint16_t *handle_id, + ble_att_svr_access_fn *cb, void *cb_arg) { struct ble_att_svr_entry *entry; @@ -108,7 +108,7 @@ ble_att_svr_register(const uint8_t *uuid, uint8_t flags, uint8_t min_key_size, return BLE_HS_ENOMEM; } - memcpy(&entry->ha_uuid, uuid, sizeof entry->ha_uuid); + entry->ha_uuid = uuid; entry->ha_flags = flags; entry->ha_min_key_size = min_key_size; entry->ha_handle_id = ble_att_svr_next_id(); @@ -124,28 +124,6 @@ ble_att_svr_register(const uint8_t *uuid, uint8_t flags, uint8_t min_key_size, return 0; } -int -ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags, - uint8_t min_key_size, uint16_t *handle_id, - ble_att_svr_access_fn *cb, void *cb_arg) -{ - uint8_t uuid128[16]; - int rc; - - rc = ble_uuid_16_to_128(uuid16, uuid128); - if (rc != 0) { - return rc; - } - - rc = ble_att_svr_register(uuid128, flags, min_key_size, handle_id, cb, - cb_arg); - if (rc != 0) { - return rc; - } - - return 0; -} - uint16_t ble_att_svr_prev_handle(void) { @@ -198,7 +176,7 @@ ble_att_svr_find_by_handle(uint16_t handle_id) * @return 0 on success; BLE_HS_ENOENT on not found. */ struct ble_att_svr_entry * -ble_att_svr_find_by_uuid(struct ble_att_svr_entry *prev, const uint8_t *uuid, +ble_att_svr_find_by_uuid(struct ble_att_svr_entry *prev, const ble_uuid_t *uuid, uint16_t end_handle) { struct ble_att_svr_entry *entry; @@ -213,7 +191,7 @@ ble_att_svr_find_by_uuid(struct ble_att_svr_entry *prev, const uint8_t *uuid, entry != NULL && entry->ha_handle_id <= end_handle; entry = STAILQ_NEXT(entry, ha_next)) { - if (memcmp(entry->ha_uuid, uuid, sizeof entry->ha_uuid) == 0) { + if (ble_uuid_cmp(entry->ha_uuid, uuid) == 0) { return entry; } } @@ -819,7 +797,6 @@ ble_att_svr_fill_info(struct ble_att_find_info_req *req, struct os_mbuf *om, uint16_t mtu, uint8_t *format) { struct ble_att_svr_entry *ha; - uint16_t uuid16; uint8_t *buf; int num_entries; int entry_sz; @@ -835,9 +812,7 @@ ble_att_svr_fill_info(struct ble_att_find_info_req *req, struct os_mbuf *om, goto done; } if (ha->ha_handle_id >= req->bafq_start_handle) { - uuid16 = ble_uuid_128_to_16(ha->ha_uuid); - - if (uuid16 != 0) { + if (ha->ha_uuid->type == BLE_UUID_TYPE_16) { if (*format == 0) { *format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT; } else if (*format != BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT) { @@ -869,19 +844,7 @@ ble_att_svr_fill_info(struct ble_att_find_info_req *req, struct os_mbuf *om, htole16(buf + 0, ha->ha_handle_id); - switch (*format) { - case BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT: - htole16(buf + 2, uuid16); - break; - - case BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT: - memcpy(buf + 2, ha->ha_uuid, sizeof ha->ha_uuid); - break; - - default: - BLE_HS_DBG_ASSERT(0); - break; - } + ble_uuid_flat(ha->ha_uuid, buf + 2); num_entries++; } @@ -1047,25 +1010,48 @@ ble_att_svr_fill_type_value_entry(struct os_mbuf *om, uint16_t first, } static int -ble_att_svr_is_valid_find_group_type(uint16_t uuid16) +ble_att_svr_is_valid_find_group_type(const ble_uuid_t *uuid) { + uint16_t uuid16; + + uuid16 = ble_uuid_u16(uuid); + return uuid16 == BLE_ATT_UUID_PRIMARY_SERVICE || uuid16 == BLE_ATT_UUID_SECONDARY_SERVICE || uuid16 == BLE_ATT_UUID_CHARACTERISTIC; } static int -ble_att_svr_is_valid_group_end(uint16_t uuid16_group, uint16_t uuid16) +ble_att_svr_is_valid_group_end(const ble_uuid_t *uuid_group, + const ble_uuid_t *uuid) { - switch (uuid16_group) { + uint16_t uuid16; + + /* Grouping is defined only for 16-bit UUIDs, so any attribute ends group + * for non-16-bit UUIDs. + */ + if (uuid_group->type != BLE_UUID_TYPE_16) { + return 1; + } + + /* Grouping is defined only for 16-bit UUIDs, so non-16-bit UUID attribute + * cannot end group. + */ + if (uuid->type != BLE_UUID_TYPE_16) { + return 0; + } + + switch (ble_uuid_u16(uuid_group)) { case BLE_ATT_UUID_PRIMARY_SERVICE: case BLE_ATT_UUID_SECONDARY_SERVICE: + uuid16 = ble_uuid_u16(uuid); + /* Only Primary or Secondary Service types end service group. */ return uuid16 == BLE_ATT_UUID_PRIMARY_SERVICE || uuid16 == BLE_ATT_UUID_SECONDARY_SERVICE; case BLE_ATT_UUID_CHARACTERISTIC: /* Any valid grouping type ends characteristic group */ - return ble_att_svr_is_valid_find_group_type(uuid16); + return ble_att_svr_is_valid_find_group_type(uuid); default: /* Any attribute type ends group of non-grouping type */ return 1; @@ -1097,14 +1083,15 @@ ble_att_svr_fill_type_value(uint16_t conn_handle, struct ble_att_svr_entry *ha; uint8_t buf[16]; uint16_t attr_len; - uint16_t uuid16; uint16_t first; uint16_t prev; + ble_uuid16_t attr_type; int any_entries; int rc; first = 0; prev = 0; + attr_type = (ble_uuid16_t) BLE_UUID16_INIT(req->bavq_attr_type); rc = 0; /* Iterate through the attribute list, keeping track of the current @@ -1121,11 +1108,9 @@ ble_att_svr_fill_type_value(uint16_t conn_handle, break; } - uuid16 = ble_uuid_128_to_16(ha->ha_uuid); - /* With group in progress, check if current attribute ends it. */ if (first) { - if (!ble_att_svr_is_valid_group_end(req->bavq_attr_type, uuid16)) { + if (!ble_att_svr_is_valid_group_end(&attr_type.u, ha->ha_uuid)) { prev = ha->ha_handle_id; continue; } @@ -1149,7 +1134,7 @@ ble_att_svr_fill_type_value(uint16_t conn_handle, /* Compare the attribute type and value to the request fields to * determine if this attribute matches. */ - if (uuid16 == req->bavq_attr_type) { + if (ble_uuid_cmp(ha->ha_uuid, &attr_type.u) == 0) { rc = ble_att_svr_read_flat(conn_handle, ha, 0, sizeof buf, buf, &attr_len, out_att_err); if (rc != 0) { @@ -1292,7 +1277,7 @@ done: static int ble_att_svr_build_read_type_rsp(uint16_t conn_handle, struct ble_att_read_type_req *req, - uint8_t *uuid128, + const ble_uuid_t *uuid, struct os_mbuf **rxom, struct os_mbuf **out_txom, uint8_t *att_err, @@ -1337,7 +1322,7 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle, /* Find all matching attributes, writing a record for each. */ entry = NULL; while (1) { - entry = ble_att_svr_find_by_uuid(entry, uuid128, req->batq_end_handle); + entry = ble_att_svr_find_by_uuid(entry, uuid, req->batq_end_handle); if (entry == NULL) { rc = BLE_HS_ENOENT; break; @@ -1416,9 +1401,8 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom) struct ble_att_read_type_req req; struct os_mbuf *txom; uint16_t err_handle; - uint16_t uuid16; uint16_t pktlen; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; uint8_t att_err; int rc; @@ -1453,30 +1437,15 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom) goto done; } - switch ((*rxom)->om_len) { - case BLE_ATT_READ_TYPE_REQ_SZ_16: - uuid16 = le16toh((*rxom)->om_data + 5); - rc = ble_uuid_16_to_128(uuid16, uuid128); - if (rc != 0) { - att_err = BLE_ATT_ERR_ATTR_NOT_FOUND; - err_handle = 0; - rc = BLE_HS_EBADDATA; - goto done; - } - break; - - case BLE_ATT_READ_TYPE_REQ_SZ_128: - memcpy(uuid128, (*rxom)->om_data + 5, 16); - break; - - default: + rc = ble_uuid_init_from_mbuf(&uuid, *rxom, 5, (*rxom)->om_len - 5); + if (rc != 0) { att_err = BLE_ATT_ERR_INVALID_PDU; err_handle = 0; rc = BLE_HS_EMSGSIZE; goto done; } - rc = ble_att_svr_build_read_type_rsp(conn_handle, &req, uuid128, + rc = ble_att_svr_build_read_type_rsp(conn_handle, &req, &uuid.u, rxom, &txom, &att_err, &err_handle); if (rc != 0) { goto done; @@ -1714,57 +1683,45 @@ done: } static int -ble_att_svr_is_valid_read_group_type(uint8_t *uuid128) +ble_att_svr_is_valid_read_group_type(const ble_uuid_t *uuid) { uint16_t uuid16; - uuid16 = ble_uuid_128_to_16(uuid128); + uuid16 = ble_uuid_u16(uuid); return uuid16 == BLE_ATT_UUID_PRIMARY_SERVICE || uuid16 == BLE_ATT_UUID_SECONDARY_SERVICE; } static int -ble_att_svr_service_uuid(struct ble_att_svr_entry *entry, uint16_t *uuid16, - uint8_t *uuid128, uint8_t *out_att_err) +ble_att_svr_service_uuid(struct ble_att_svr_entry *entry, + ble_uuid_any_t *uuid, uint8_t *out_att_err) { + uint8_t val[16]; uint16_t attr_len; int rc; - rc = ble_att_svr_read_flat(BLE_HS_CONN_HANDLE_NONE, entry, 0, 16, uuid128, + rc = ble_att_svr_read_flat(BLE_HS_CONN_HANDLE_NONE, entry, 0, sizeof(val), val, &attr_len, out_att_err); if (rc != 0) { return rc; } - switch (attr_len) { - case 16: - *uuid16 = 0; - return 0; + rc = ble_uuid_init_from_buf(uuid, val, attr_len); - case 2: - *uuid16 = le16toh(uuid128); - if (*uuid16 == 0) { - return BLE_HS_EINVAL; - } - return 0; - - default: - return BLE_HS_EINVAL; - } + return rc; } static int ble_att_svr_read_group_type_entry_write(struct os_mbuf *om, uint16_t mtu, uint16_t start_group_handle, uint16_t end_group_handle, - uint16_t service_uuid16, - uint8_t *service_uuid128) + const ble_uuid_t *service_uuid) { uint8_t *buf; int len; - if (service_uuid16 != 0) { + if (service_uuid->type == BLE_UUID_TYPE_16) { len = BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16; } else { len = BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_128; @@ -1780,11 +1737,7 @@ ble_att_svr_read_group_type_entry_write(struct os_mbuf *om, uint16_t mtu, htole16(buf + 0, start_group_handle); htole16(buf + 2, end_group_handle); - if (service_uuid16 != 0) { - htole16(buf + 4, service_uuid16); - } else { - memcpy(buf + 4, service_uuid128, 16); - } + ble_uuid_flat(service_uuid, buf + 4); return 0; } @@ -1795,7 +1748,7 @@ ble_att_svr_read_group_type_entry_write(struct os_mbuf *om, uint16_t mtu, static int ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, struct ble_att_read_group_type_req *req, - uint8_t *group_uuid128, + const ble_uuid_t *group_uuid, struct os_mbuf **rxom, struct os_mbuf **out_txom, uint8_t *att_err, @@ -1806,15 +1759,13 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, struct os_mbuf *txom; uint16_t start_group_handle; uint16_t end_group_handle; - uint16_t service_uuid16; uint16_t mtu; - uint8_t service_uuid128[16]; + ble_uuid_any_t service_uuid; void *rsp_buf; int rc; /* Silence warnings. */ rsp_buf = NULL; - service_uuid16 = 0; end_group_handle = 0; *att_err = 0; @@ -1858,7 +1809,7 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, */ rc = ble_att_svr_read_group_type_entry_write( txom, mtu, start_group_handle, end_group_handle, - service_uuid16, service_uuid128); + &service_uuid.u); start_group_handle = 0; end_group_handle = 0; if (rc != 0) { @@ -1875,10 +1826,9 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, if (start_group_handle == 0) { /* We are looking for the start of a group. */ - if (memcmp(entry->ha_uuid, group_uuid128, 16) == 0) { + if (ble_uuid_cmp(entry->ha_uuid, group_uuid) == 0) { /* Found a group start. Read the group UUID. */ - rc = ble_att_svr_service_uuid(entry, &service_uuid16, - service_uuid128, att_err); + rc = ble_att_svr_service_uuid(entry, &service_uuid, att_err); if (rc != 0) { *err_handle = entry->ha_handle_id; goto done; @@ -1890,7 +1840,7 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, */ switch (rsp.bagp_length) { case 0: - if (service_uuid16 != 0) { + if (service_uuid.u.type == BLE_UUID_TYPE_16) { rsp.bagp_length = BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16; } else { rsp.bagp_length = BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_128; @@ -1898,14 +1848,14 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle, break; case BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16: - if (service_uuid16 == 0) { + if (service_uuid.u.type != BLE_UUID_TYPE_16) { rc = 0; goto done; } break; case BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_128: - if (service_uuid16 != 0) { + if (service_uuid.u.type == BLE_UUID_TYPE_16) { rc = 0; goto done; } @@ -1939,9 +1889,10 @@ done: end_group_handle = 0xffff; } - rc = ble_att_svr_read_group_type_entry_write( - txom, mtu, start_group_handle, end_group_handle, - service_uuid16, service_uuid128); + rc = ble_att_svr_read_group_type_entry_write(txom, mtu, + start_group_handle, + end_group_handle, + &service_uuid.u); if (rc == BLE_HS_ENOMEM) { *att_err = BLE_ATT_ERR_INSUFFICIENT_RES; } @@ -1975,10 +1926,11 @@ ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom) struct ble_att_read_group_type_req req; struct os_mbuf *txom; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; uint16_t err_handle; uint16_t pktlen; uint8_t att_err; + int om_uuid_len; int rc; /* Initialize some values in case of early error. */ @@ -2011,8 +1963,11 @@ ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom) goto done; } - rc = ble_uuid_extract(*rxom, BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, - uuid128); + om_uuid_len = OS_MBUF_PKTHDR(*rxom)->omp_len - + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ; + rc = ble_uuid_init_from_mbuf(&uuid, *rxom, + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, + om_uuid_len); if (rc != 0) { att_err = BLE_ATT_ERR_INVALID_PDU; err_handle = req.bagq_start_handle; @@ -2020,14 +1975,14 @@ ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom) goto done; } - if (!ble_att_svr_is_valid_read_group_type(uuid128)) { + if (!ble_att_svr_is_valid_read_group_type(&uuid.u)) { att_err = BLE_ATT_ERR_UNSUPPORTED_GROUP; err_handle = req.bagq_start_handle; rc = BLE_HS_ENOTSUP; goto done; } - rc = ble_att_svr_build_read_group_type_rsp(conn_handle, &req, uuid128, + rc = ble_att_svr_build_read_group_type_rsp(conn_handle, &req, &uuid.u, rxom, &txom, &att_err, &err_handle); if (rc != 0) { diff --git a/nimble/host/src/ble_gattc.c b/nimble/host/src/ble_gattc.c index 7cdee07f7..e448c8b96 100644 --- a/nimble/host/src/ble_gattc.c +++ b/nimble/host/src/ble_gattc.c @@ -113,7 +113,7 @@ struct ble_gattc_proc { } disc_all_svcs; struct { - uint8_t service_uuid[16]; + ble_uuid_any_t service_uuid; uint16_t prev_handle; ble_gatt_disc_svc_fn *cb; void *cb_arg; @@ -138,7 +138,7 @@ struct ble_gattc_proc { } disc_all_chrs; struct { - uint8_t chr_uuid[16]; + ble_uuid_any_t chr_uuid; uint16_t prev_handle; uint16_t end_handle; ble_gatt_chr_fn *cb; @@ -160,7 +160,7 @@ struct ble_gattc_proc { } read; struct { - uint8_t chr_uuid[16]; + ble_uuid_any_t chr_uuid; uint16_t start_handle; uint16_t end_handle; ble_gatt_attr_fn *cb; @@ -491,24 +491,20 @@ ble_gattc_log_proc_init(char *name) } static void -ble_gattc_log_uuid(const void *uuid128) +ble_gattc_log_uuid(const ble_uuid_t *uuid) { - const uint8_t *u8p; + char buf[BLE_UUID_STR_LEN]; - u8p = uuid128; - BLE_HS_LOG(INFO, "%02x%02x%02x%02x-%02x%02x-%02x%02x-" - "%02x%02x%02x%02x%02x%02x%02x%02x", - u8p[15], u8p[14], u8p[13], u8p[12], - u8p[11], u8p[10], u8p[9], u8p[8], - u8p[7], u8p[6], u8p[5], u8p[4], - u8p[3], u8p[2], u8p[1], u8p[0]); + ble_uuid_to_str(uuid, buf); + + BLE_HS_LOG(INFO, "%s", buf); } static void ble_gattc_log_disc_svc_uuid(struct ble_gattc_proc *proc) { ble_gattc_log_proc_init("discover service by uuid; uuid="); - ble_gattc_log_uuid(proc->disc_svc_uuid.service_uuid); + ble_gattc_log_uuid(&proc->disc_svc_uuid.service_uuid.u); BLE_HS_LOG(INFO, "\n"); } @@ -537,7 +533,7 @@ ble_gattc_log_disc_chr_uuid(struct ble_gattc_proc *proc) BLE_HS_LOG(INFO, "start_handle=%d end_handle=%d uuid=", proc->disc_chr_uuid.prev_handle + 1, proc->disc_chr_uuid.end_handle); - ble_gattc_log_uuid(proc->disc_chr_uuid.chr_uuid); + ble_gattc_log_uuid(&proc->disc_chr_uuid.chr_uuid.u); BLE_HS_LOG(INFO, "\n"); } @@ -559,20 +555,12 @@ ble_gattc_log_read(uint16_t att_handle) static void ble_gattc_log_read_uuid(uint16_t start_handle, uint16_t end_handle, - const uint8_t *uuid128) + const ble_uuid_t *uuid) { - uint16_t uuid16; - ble_gattc_log_proc_init("read by uuid; "); BLE_HS_LOG(INFO, "start_handle=%d end_handle=%d uuid=", start_handle, end_handle); - - uuid16 = ble_uuid_128_to_16(uuid128); - if (uuid16 != 0) { - BLE_HS_LOG(INFO, "0x%04x", uuid16); - } else { - ble_gattc_log_uuid(uuid128); - } + ble_gattc_log_uuid(uuid); BLE_HS_LOG(INFO, "\n"); } @@ -1394,17 +1382,14 @@ static int ble_gattc_disc_all_svcs_tx(struct ble_gattc_proc *proc) { struct ble_att_read_group_type_req req; - uint8_t uuid128[16]; + ble_uuid16_t uuid = BLE_UUID16_INIT(BLE_ATT_UUID_PRIMARY_SERVICE); int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); - rc = ble_uuid_16_to_128(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - req.bagq_start_handle = proc->disc_all_svcs.prev_handle + 1; req.bagq_end_handle = 0xffff; - rc = ble_att_clt_tx_read_group_type(proc->conn_handle, &req, uuid128); + rc = ble_att_clt_tx_read_group_type(proc->conn_handle, &req, &uuid.u); if (rc != 0) { return rc; } @@ -1455,7 +1440,6 @@ ble_gattc_disc_all_svcs_rx_adata(struct ble_gattc_proc *proc, struct ble_att_read_group_type_adata *adata) { struct ble_gatt_svc service; - uint16_t uuid16; int cbrc; int rc; @@ -1463,17 +1447,14 @@ ble_gattc_disc_all_svcs_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case 2: - uuid16 = le16toh(adata->value); - rc = ble_uuid_16_to_128(uuid16, service.uuid128); + case 16: + rc = ble_uuid_init_from_buf(&service.uuid, adata->value, adata->value_len); if (rc != 0) { + rc = BLE_HS_EBADDATA; goto done; } break; - case 16: - memcpy(service.uuid128, adata->value, 16); - break; - default: rc = BLE_HS_EBADDATA; goto done; @@ -1636,7 +1617,7 @@ static int ble_gattc_disc_svc_uuid_tx(struct ble_gattc_proc *proc) { struct ble_att_find_type_value_req req; - uint16_t uuid16; + uint8_t val[16]; int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); @@ -1645,17 +1626,9 @@ ble_gattc_disc_svc_uuid_tx(struct ble_gattc_proc *proc) req.bavq_end_handle = 0xffff; req.bavq_attr_type = BLE_ATT_UUID_PRIMARY_SERVICE; - - uuid16 = ble_uuid_128_to_16(proc->disc_svc_uuid.service_uuid); - if (uuid16 != 0){ - rc = ble_att_clt_tx_find_type_value(proc->conn_handle, &req, - &uuid16, 2); - } else { - rc = ble_att_clt_tx_find_type_value(proc->conn_handle, &req, - proc->disc_svc_uuid.service_uuid, - 16); - } - + ble_uuid_flat(&proc->disc_svc_uuid.service_uuid.u, val); + rc = ble_att_clt_tx_find_type_value(proc->conn_handle, &req, val, + ble_uuid_length(&proc->disc_svc_uuid.service_uuid.u)); if (rc != 0) { return rc; } @@ -1721,7 +1694,7 @@ ble_gattc_disc_svc_uuid_rx_hinfo(struct ble_gattc_proc *proc, service.start_handle = hinfo->attr_handle; service.end_handle = hinfo->group_end_handle; - memcpy(service.uuid128, proc->disc_svc_uuid.service_uuid, 16); + service.uuid = proc->disc_svc_uuid.service_uuid; rc = 0; @@ -1779,7 +1752,7 @@ ble_gattc_disc_svc_uuid_rx_complete(struct ble_gattc_proc *proc, int status) * @return 0 on success; nonzero on failure. */ int -ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const void *svc_uuid128, +ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid, ble_gatt_disc_svc_fn *cb, void *cb_arg) { #if !MYNEWT_VAL(BLE_GATT_DISC_SVC_UUID) @@ -1799,7 +1772,7 @@ ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const void *svc_uuid128, proc->op = BLE_GATT_OP_DISC_SVC_UUID; proc->conn_handle = conn_handle; - memcpy(proc->disc_svc_uuid.service_uuid, svc_uuid128, 16); + ble_uuid_to_any(uuid, &proc->disc_svc_uuid.service_uuid); proc->disc_svc_uuid.prev_handle = 0x0000; proc->disc_svc_uuid.cb = cb; proc->disc_svc_uuid.cb_arg = cb_arg; @@ -1874,7 +1847,7 @@ ble_gattc_find_inc_svcs_tx(struct ble_gattc_proc *proc) { struct ble_att_read_type_req read_type_req; struct ble_att_read_req read_req; - uint8_t uuid128[16]; + ble_uuid16_t uuid = BLE_UUID16_INIT(BLE_ATT_UUID_INCLUDE); int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); @@ -1885,11 +1858,8 @@ ble_gattc_find_inc_svcs_tx(struct ble_gattc_proc *proc) proc->find_inc_svcs.prev_handle + 1; read_type_req.batq_end_handle = proc->find_inc_svcs.end_handle; - rc = ble_uuid_16_to_128(BLE_ATT_UUID_INCLUDE, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - rc = ble_att_clt_tx_read_type(proc->conn_handle, - &read_type_req, uuid128); + &read_type_req, &uuid.u); if (rc != 0) { return rc; } @@ -1950,16 +1920,15 @@ ble_gattc_find_inc_svcs_rx_read_rsp(struct ble_gattc_proc *proc, int status, struct os_mbuf **om) { struct ble_gatt_svc service; - uint16_t om_len; int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); - rc = ble_hs_mbuf_to_flat(*om, service.uuid128, 16, &om_len); + rc = ble_uuid_init_from_mbuf(&service.uuid, *om, 0, 16); os_mbuf_free_chain(*om); *om = NULL; - if (rc != 0 || om_len != 16) { + if (rc != 0) { /* Invalid UUID. */ rc = BLE_HS_EBADDATA; goto err; @@ -2009,7 +1978,6 @@ ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc, struct ble_att_read_type_adata *adata) { struct ble_gatt_svc service; - uint16_t uuid16; int call_cb; int cbrc; int rc; @@ -2043,13 +2011,7 @@ ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc, case BLE_GATTS_INC_SVC_LEN_UUID: service.start_handle = le16toh(adata->value + 0); service.end_handle = le16toh(adata->value + 2); - uuid16 = le16toh(adata->value + 4); - rc = ble_uuid_16_to_128(uuid16, service.uuid128); - if (rc != 0) { - rc = BLE_HS_EBADDATA; - goto done; - } - + ble_uuid_init_from_buf(&service.uuid, adata->value + 4, 2); break; default: @@ -2218,18 +2180,15 @@ static int ble_gattc_disc_all_chrs_tx(struct ble_gattc_proc *proc) { struct ble_att_read_type_req req; - uint8_t uuid128[16]; + ble_uuid16_t uuid = BLE_UUID16_INIT(BLE_ATT_UUID_CHARACTERISTIC); int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); - rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - req.batq_start_handle = proc->disc_all_chrs.prev_handle + 1; req.batq_end_handle = proc->disc_all_chrs.end_handle; - rc = ble_att_clt_tx_read_type(proc->conn_handle, &req, uuid128); + rc = ble_att_clt_tx_read_type(proc->conn_handle, &req, &uuid.u); if (rc != 0) { return rc; } @@ -2280,7 +2239,6 @@ ble_gattc_disc_all_chrs_rx_adata(struct ble_gattc_proc *proc, struct ble_att_read_type_adata *adata) { struct ble_gatt_chr chr; - uint16_t uuid16; int cbrc; int rc; @@ -2291,18 +2249,15 @@ ble_gattc_disc_all_chrs_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case BLE_GATT_CHR_DECL_SZ_16: - uuid16 = le16toh(adata->value + 3); - rc = ble_uuid_16_to_128(uuid16, chr.uuid128); + case BLE_GATT_CHR_DECL_SZ_128: + rc = ble_uuid_init_from_buf(&chr.uuid, adata->value + 3, + adata->value_len - 3); if (rc != 0) { rc = BLE_HS_EBADDATA; goto done; } break; - case BLE_GATT_CHR_DECL_SZ_128: - memcpy(chr.uuid128, adata->value + 3, 16); - break; - default: rc = BLE_HS_EBADDATA; goto done; @@ -2471,18 +2426,15 @@ static int ble_gattc_disc_chr_uuid_tx(struct ble_gattc_proc *proc) { struct ble_att_read_type_req req; - uint8_t uuid128[16]; + ble_uuid16_t uuid = BLE_UUID16_INIT(BLE_ATT_UUID_CHARACTERISTIC); int rc; ble_gattc_dbg_assert_proc_not_inserted(proc); - rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - req.batq_start_handle = proc->disc_chr_uuid.prev_handle + 1; req.batq_end_handle = proc->disc_chr_uuid.end_handle; - rc = ble_att_clt_tx_read_type(proc->conn_handle, &req, uuid128); + rc = ble_att_clt_tx_read_type(proc->conn_handle, &req, &uuid.u); if (rc != 0) { return rc; } @@ -2533,7 +2485,6 @@ ble_gattc_disc_chr_uuid_rx_adata(struct ble_gattc_proc *proc, struct ble_att_read_type_adata *adata) { struct ble_gatt_chr chr; - uint16_t uuid16; int cbrc; int rc; @@ -2544,18 +2495,15 @@ ble_gattc_disc_chr_uuid_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case BLE_GATT_CHR_DECL_SZ_16: - uuid16 = le16toh(adata->value + 3); - rc = ble_uuid_16_to_128(uuid16, chr.uuid128); + case BLE_GATT_CHR_DECL_SZ_128: + rc = ble_uuid_init_from_buf(&chr.uuid, adata->value + 3, + adata->value_len - 3); if (rc != 0) { rc = BLE_HS_EBADDATA; goto done; } break; - case BLE_GATT_CHR_DECL_SZ_128: - memcpy(chr.uuid128, adata->value + 3, 16); - break; - default: rc = BLE_HS_EBADDATA; goto done; @@ -2578,7 +2526,7 @@ done: if (rc != 0) { /* Failure. */ cbrc = ble_gattc_disc_chr_uuid_cb(proc, rc, 0, NULL); - } else if (memcmp(chr.uuid128, proc->disc_chr_uuid.chr_uuid, 16) == 0) { + } else if (ble_uuid_cmp(&chr.uuid.u, &proc->disc_chr_uuid.chr_uuid.u) == 0) { /* Requested characteristic discovered. */ cbrc = ble_gattc_disc_chr_uuid_cb(proc, 0, 0, &chr); } else { @@ -2643,7 +2591,7 @@ ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_proc *proc, int status) */ int ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, - uint16_t end_handle, const void *uuid128, + uint16_t end_handle, const ble_uuid_t *uuid, ble_gatt_chr_fn *cb, void *cb_arg) { #if !MYNEWT_VAL(BLE_GATT_DISC_CHR_UUID) @@ -2663,7 +2611,7 @@ ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, proc->op = BLE_GATT_OP_DISC_CHR_UUID; proc->conn_handle = conn_handle; - memcpy(proc->disc_chr_uuid.chr_uuid, uuid128, 16); + ble_uuid_to_any(uuid, &proc->disc_chr_uuid.chr_uuid); proc->disc_chr_uuid.prev_handle = start_handle - 1; proc->disc_chr_uuid.end_handle = end_handle; proc->disc_chr_uuid.cb = cb; @@ -2812,7 +2760,7 @@ ble_gattc_disc_all_dscs_rx_idata(struct ble_gattc_proc *proc, done: dsc.handle = idata->attr_handle; - memcpy(dsc.uuid128, idata->uuid128, 16); + dsc.uuid = idata->uuid; cbrc = ble_gattc_disc_all_dscs_cb(proc, rc, 0, &dsc); if (rc != 0 || cbrc != 0) { @@ -3185,7 +3133,7 @@ ble_gattc_read_uuid_tx(struct ble_gattc_proc *proc) req.batq_end_handle = proc->read_uuid.end_handle; rc = ble_att_clt_tx_read_type(proc->conn_handle, &req, - proc->read_uuid.chr_uuid); + &proc->read_uuid.chr_uuid.u); if (rc != 0) { return rc; } @@ -3211,7 +3159,7 @@ ble_gattc_read_uuid_tx(struct ble_gattc_proc *proc) */ int ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, - uint16_t end_handle, const void *uuid128, + uint16_t end_handle, const ble_uuid_t *uuid, ble_gatt_attr_fn *cb, void *cb_arg) { #if !MYNEWT_VAL(BLE_GATT_READ_UUID) @@ -3231,13 +3179,13 @@ ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, proc->op = BLE_GATT_OP_READ_UUID; proc->conn_handle = conn_handle; - memcpy(proc->read_uuid.chr_uuid, uuid128, 16); + ble_uuid_to_any(uuid, &proc->read_uuid.chr_uuid); proc->read_uuid.start_handle = start_handle; proc->read_uuid.end_handle = end_handle; proc->read_uuid.cb = cb; proc->read_uuid.cb_arg = cb_arg; - ble_gattc_log_read_uuid(start_handle, end_handle, uuid128); + ble_gattc_log_read_uuid(start_handle, end_handle, uuid); rc = ble_gattc_read_uuid_tx(proc); if (rc != 0) { goto done; diff --git a/nimble/host/src/ble_gatts.c b/nimble/host/src/ble_gatts.c index 4a5ddfb57..58ca63174 100644 --- a/nimble/host/src/ble_gatts.c +++ b/nimble/host/src/ble_gatts.c @@ -28,6 +28,17 @@ #define BLE_GATTS_INCLUDE_SZ 6 #define BLE_GATTS_CHR_MAX_SZ 19 +static const ble_uuid_t *uuid_pri = + BLE_UUID16_DECLARE(BLE_ATT_UUID_PRIMARY_SERVICE); +static const ble_uuid_t *uuid_sec = + BLE_UUID16_DECLARE(BLE_ATT_UUID_SECONDARY_SERVICE); +static const ble_uuid_t *uuid_inc = + BLE_UUID16_DECLARE(BLE_ATT_UUID_INCLUDE); +static const ble_uuid_t *uuid_chr = + BLE_UUID16_DECLARE(BLE_ATT_UUID_CHARACTERISTIC); +static const ble_uuid_t *uuid_ccc = + BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16); + static const struct ble_gatt_svc_def **ble_gatts_svc_defs; static int ble_gatts_num_svc_defs; @@ -73,9 +84,7 @@ ble_gatts_svc_access(uint16_t conn_handle, uint16_t attr_handle, void *arg) { const struct ble_gatt_svc_def *svc; - uint16_t uuid16; uint8_t *buf; - int rc; STATS_INC(ble_gatts_stats, svc_def_reads); @@ -83,20 +92,13 @@ ble_gatts_svc_access(uint16_t conn_handle, uint16_t attr_handle, svc = arg; - uuid16 = ble_uuid_128_to_16(svc->uuid128); - if (uuid16 != 0) { - buf = os_mbuf_extend(*om, 2); - if (buf == NULL) { - return BLE_ATT_ERR_INSUFFICIENT_RES; - } - htole16(buf, uuid16); - } else { - rc = os_mbuf_append(*om, svc->uuid128, 16); - if (rc != 0) { - return BLE_ATT_ERR_INSUFFICIENT_RES; - } + buf = os_mbuf_extend(*om, ble_uuid_length(svc->uuid)); + if (buf == NULL) { + return BLE_ATT_ERR_INSUFFICIENT_RES; } + ble_uuid_flat(svc->uuid, buf); + return 0; } @@ -123,7 +125,7 @@ ble_gatts_inc_access(uint16_t conn_handle, uint16_t attr_handle, htole16(buf + 2, entry->end_group_handle); /* Only include the service UUID if it has a 16-bit representation. */ - uuid16 = ble_uuid_128_to_16(entry->svc->uuid128); + uuid16 = ble_uuid_u16(entry->svc->uuid); if (uuid16 != 0) { buf = os_mbuf_extend(*om, 2); if (buf == NULL) { @@ -228,7 +230,6 @@ ble_gatts_chr_def_access(uint16_t conn_handle, uint16_t attr_handle, void *arg) { const struct ble_gatt_chr_def *chr; - uint16_t uuid16; uint8_t *buf; STATS_INC(ble_gatts_stats, chr_def_reads); @@ -247,28 +248,20 @@ ble_gatts_chr_def_access(uint16_t conn_handle, uint16_t attr_handle, /* The value attribute is always immediately after the declaration. */ htole16(buf + 1, attr_handle + 1); - uuid16 = ble_uuid_128_to_16(chr->uuid128); - if (uuid16 != 0) { - buf = os_mbuf_extend(*om, 2); - if (buf == NULL) { - return BLE_ATT_ERR_INSUFFICIENT_RES; - } - htole16(buf, uuid16); - } else { - buf = os_mbuf_extend(*om, 16); - if (buf == NULL) { - return BLE_ATT_ERR_INSUFFICIENT_RES; - } - memcpy(buf, chr->uuid128, 16); + buf = os_mbuf_extend(*om, ble_uuid_length(chr->uuid)); + if (buf == NULL) { + return BLE_ATT_ERR_INSUFFICIENT_RES; } + ble_uuid_flat(chr->uuid, buf); + return 0; } static int ble_gatts_chr_is_sane(const struct ble_gatt_chr_def *chr) { - if (chr->uuid128 == NULL) { + if (chr->uuid == NULL) { return 0; } @@ -423,8 +416,8 @@ ble_gatts_register_inc(struct ble_gatts_svc_entry *entry) BLE_HS_DBG_ASSERT(entry->handle != 0); BLE_HS_DBG_ASSERT(entry->end_group_handle != 0xffff); - rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_INCLUDE, BLE_ATT_F_READ, 0, - &handle, ble_gatts_inc_access, entry); + rc = ble_att_svr_register(uuid_inc, BLE_ATT_F_READ, 0, &handle, + ble_gatts_inc_access, entry); if (rc != 0) { return rc; } @@ -490,7 +483,7 @@ ble_gatts_dsc_access(uint16_t conn_handle, uint16_t attr_handle, static int ble_gatts_dsc_is_sane(const struct ble_gatt_dsc_def *dsc) { - if (dsc->uuid128 == NULL) { + if (dsc->uuid == NULL) { return 0; } @@ -516,7 +509,7 @@ ble_gatts_register_dsc(const struct ble_gatt_svc_def *svc, return BLE_HS_EINVAL; } - rc = ble_att_svr_register(dsc->uuid128, dsc->att_flags, dsc->min_key_size, + rc = ble_att_svr_register(dsc->uuid, dsc->att_flags, dsc->min_key_size, &dsc_handle, ble_gatts_dsc_access, (void *)dsc); if (rc != 0) { return rc; @@ -744,15 +737,9 @@ ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t attr_handle, static int ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle) { - uint8_t uuid128[16]; int rc; - rc = ble_uuid_16_to_128(BLE_GATT_DSC_CLT_CFG_UUID16, uuid128); - if (rc != 0) { - return rc; - } - - rc = ble_att_svr_register(uuid128, BLE_ATT_F_READ | BLE_ATT_F_WRITE, 0, + rc = ble_att_svr_register(uuid_ccc, BLE_ATT_F_READ | BLE_ATT_F_WRITE, 0, att_handle, ble_gatts_clt_cfg_access, NULL); if (rc != 0) { return rc; @@ -790,9 +777,8 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc, /* Register characteristic definition attribute (cast away const on * callback arg). */ - rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_CHARACTERISTIC, - BLE_ATT_F_READ, 0, &def_handle, - ble_gatts_chr_def_access, (void *)chr); + rc = ble_att_svr_register(uuid_chr, BLE_ATT_F_READ, 0, &def_handle, + ble_gatts_chr_def_access, (void *)chr); if (rc != 0) { return rc; } @@ -801,7 +787,7 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc, * arg). */ att_flags = ble_gatts_att_flags_from_chr_flags(chr->flags); - rc = ble_att_svr_register(chr->uuid128, att_flags, chr->min_key_size, + rc = ble_att_svr_register(chr->uuid, att_flags, chr->min_key_size, &val_handle, ble_gatts_chr_val_access, (void *)chr); if (rc != 0) { @@ -832,7 +818,7 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc, /* Register each descriptor. */ if (chr->descriptors != NULL) { - for (dsc = chr->descriptors; dsc->uuid128 != NULL; dsc++) { + for (dsc = chr->descriptors; dsc->uuid != NULL; dsc++) { rc = ble_gatts_register_dsc(svc, chr, dsc, def_handle, register_cb, cb_arg); if (rc != 0) { @@ -847,15 +833,15 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc, } static int -ble_gatts_svc_type_to_uuid(uint8_t svc_type, uint16_t *out_uuid16) +ble_gatts_svc_type_to_uuid(uint8_t svc_type, const ble_uuid_t **uuid) { switch (svc_type) { case BLE_GATT_SVC_TYPE_PRIMARY: - *out_uuid16 = BLE_ATT_UUID_PRIMARY_SERVICE; + *uuid = uuid_pri; return 0; case BLE_GATT_SVC_TYPE_SECONDARY: - *out_uuid16 = BLE_ATT_UUID_SECONDARY_SERVICE; + *uuid = uuid_sec; return 0; default: @@ -872,7 +858,7 @@ ble_gatts_svc_is_sane(const struct ble_gatt_svc_def *svc) return 0; } - if (svc->uuid128 == NULL) { + if (svc->uuid == NULL) { return 0; } @@ -886,7 +872,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc, { const struct ble_gatt_chr_def *chr; struct ble_gatt_register_ctxt register_ctxt; - uint16_t uuid16; + const ble_uuid_t *uuid; int idx; int rc; int i; @@ -900,15 +886,15 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc, } /* Prevent spurious maybe-uninitialized gcc warning. */ - uuid16 = 0; + uuid = NULL; - rc = ble_gatts_svc_type_to_uuid(svc->type, &uuid16); + rc = ble_gatts_svc_type_to_uuid(svc->type, &uuid); BLE_HS_DBG_ASSERT_EVAL(rc == 0); /* Register service definition attribute (cast away const on callback * arg). */ - rc = ble_att_svr_register_uuid16(uuid16, BLE_ATT_F_READ, 0, out_handle, + rc = ble_att_svr_register(uuid, BLE_ATT_F_READ, 0, out_handle, ble_gatts_svc_access, (void *)svc); if (rc != 0) { return rc; @@ -936,7 +922,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc, /* Register each characteristic. */ if (svc->characteristics != NULL) { - for (chr = svc->characteristics; chr->uuid128 != NULL; chr++) { + for (chr = svc->characteristics; chr->uuid != NULL; chr++) { rc = ble_gatts_register_chr(svc, chr, register_cb, cb_arg); if (rc != 0) { return rc; @@ -1137,7 +1123,7 @@ ble_gatts_start(void) struct ble_att_svr_entry *ha; struct ble_gatt_chr_def *chr; uint16_t allowed_flags; - uint8_t uuid128[16]; + ble_uuid16_t uuid = BLE_UUID16_INIT(BLE_ATT_UUID_CHARACTERISTIC); int num_elems; int idx; int rc; @@ -1200,11 +1186,9 @@ ble_gatts_start(void) } /* Fill the cache. */ - rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); idx = 0; ha = NULL; - while ((ha = ble_att_svr_find_by_uuid(ha, uuid128, 0xffff)) != NULL) { + while ((ha = ble_att_svr_find_by_uuid(ha, &uuid.u, 0xffff)) != NULL) { chr = ha->ha_cb_arg; allowed_flags = ble_gatts_chr_clt_cfg_allowed(chr); if (allowed_flags != 0) { @@ -1710,14 +1694,14 @@ ble_gatts_bonding_restored(uint16_t conn_handle) } static struct ble_gatts_svc_entry * -ble_gatts_find_svc_entry(const void *uuid128) +ble_gatts_find_svc_entry(const ble_uuid_t *uuid) { struct ble_gatts_svc_entry *entry; int i; for (i = 0; i < ble_gatts_num_svc_entries; i++) { entry = ble_gatts_svc_entries + i; - if (memcmp(uuid128, entry->svc->uuid128, 16) == 0) { + if (ble_uuid_cmp(uuid, entry->svc->uuid) == 0) { return entry; } } @@ -1726,7 +1710,7 @@ ble_gatts_find_svc_entry(const void *uuid128) } static int -ble_gatts_find_svc_chr_attr(const void *svc_uuid128, const void *chr_uuid128, +ble_gatts_find_svc_chr_attr(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, struct ble_gatts_svc_entry **out_svc_entry, struct ble_att_svr_entry **out_att_chr) { @@ -1734,9 +1718,8 @@ ble_gatts_find_svc_chr_attr(const void *svc_uuid128, const void *chr_uuid128, struct ble_att_svr_entry *att_svc; struct ble_att_svr_entry *next; struct ble_att_svr_entry *cur; - uint16_t uuid16; - svc_entry = ble_gatts_find_svc_entry(svc_uuid128); + svc_entry = ble_gatts_find_svc_entry(svc_uuid); if (svc_entry == NULL) { return BLE_HS_ENOENT; } @@ -1759,10 +1742,9 @@ ble_gatts_find_svc_chr_attr(const void *svc_uuid128, const void *chr_uuid128, return BLE_HS_ENOENT; } - uuid16 = ble_uuid_128_to_16(cur->ha_uuid); - if (uuid16 == BLE_ATT_UUID_CHARACTERISTIC && + if (ble_uuid_u16(cur->ha_uuid) == BLE_ATT_UUID_CHARACTERISTIC && next != NULL && - memcmp(next->ha_uuid, chr_uuid128, 16) == 0) { + ble_uuid_cmp(next->ha_uuid, chr_uuid) == 0) { if (out_svc_entry != NULL) { *out_svc_entry = svc_entry; @@ -1790,11 +1772,11 @@ ble_gatts_find_svc_chr_attr(const void *svc_uuid128, const void *chr_uuid128, * not be found. */ int -ble_gatts_find_svc(const void *uuid128, uint16_t *out_handle) +ble_gatts_find_svc(const ble_uuid_t *uuid, uint16_t *out_handle) { struct ble_gatts_svc_entry *entry; - entry = ble_gatts_find_svc_entry(uuid128); + entry = ble_gatts_find_svc_entry(uuid); if (entry == NULL) { return BLE_HS_ENOENT; } @@ -1823,13 +1805,13 @@ ble_gatts_find_svc(const void *uuid128, uint16_t *out_handle) * characteristic could not be found. */ int -ble_gatts_find_chr(const void *svc_uuid128, const void *chr_uuid128, +ble_gatts_find_chr(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, uint16_t *out_def_handle, uint16_t *out_val_handle) { struct ble_att_svr_entry *att_chr; int rc; - rc = ble_gatts_find_svc_chr_attr(svc_uuid128, chr_uuid128, NULL, &att_chr); + rc = ble_gatts_find_svc_chr_attr(svc_uuid, chr_uuid, NULL, &att_chr); if (rc != 0) { return rc; } @@ -1859,8 +1841,8 @@ ble_gatts_find_chr(const void *svc_uuid128, const void *chr_uuid128, * found. */ int -ble_gatts_find_dsc(const void *svc_uuid128, const void *chr_uuid128, - const void *dsc_uuid128, uint16_t *out_handle) +ble_gatts_find_dsc(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, + const ble_uuid_t *dsc_uuid, uint16_t *out_handle) { struct ble_gatts_svc_entry *svc_entry; struct ble_att_svr_entry *att_chr; @@ -1868,7 +1850,7 @@ ble_gatts_find_dsc(const void *svc_uuid128, const void *chr_uuid128, uint16_t uuid16; int rc; - rc = ble_gatts_find_svc_chr_attr(svc_uuid128, chr_uuid128, &svc_entry, + rc = ble_gatts_find_svc_chr_attr(svc_uuid, chr_uuid, &svc_entry, &att_chr); if (rc != 0) { return rc; @@ -1886,13 +1868,13 @@ ble_gatts_find_dsc(const void *svc_uuid128, const void *chr_uuid128, return BLE_HS_ENOENT; } - uuid16 = ble_uuid_128_to_16(cur->ha_uuid); + uuid16 = ble_uuid_u16(cur->ha_uuid); if (uuid16 == BLE_ATT_UUID_CHARACTERISTIC) { /* Reached end of characteristic without a match. */ return BLE_HS_ENOENT; } - if (memcmp(cur->ha_uuid, dsc_uuid128, 16) == 0) { + if (ble_uuid_cmp(cur->ha_uuid, dsc_uuid) == 0) { if (out_handle != NULL) { *out_handle = cur->ha_handle_id; return 0; @@ -1988,7 +1970,7 @@ ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs, } if (svc->characteristics != NULL) { - for (c = 0; svc->characteristics[c].uuid128 != NULL; c++) { + for (c = 0; svc->characteristics[c].uuid != NULL; c++) { chr = svc->characteristics + c; if (!ble_gatts_chr_is_sane(chr)) { @@ -2020,7 +2002,7 @@ ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs, } if (chr->descriptors != NULL) { - for (d = 0; chr->descriptors[d].uuid128 != NULL; d++) { + for (d = 0; chr->descriptors[d].uuid != NULL; d++) { if (!ble_gatts_dsc_is_sane(chr->descriptors + d)) { BLE_HS_DBG_ASSERT(0); return BLE_HS_EINVAL; diff --git a/nimble/host/src/ble_uuid.c b/nimble/host/src/ble_uuid.c index 1640e9c44..2399018d2 100644 --- a/nimble/host/src/ble_uuid.c +++ b/nimble/host/src/ble_uuid.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include "os/os_mbuf.h" @@ -30,127 +31,194 @@ static uint8_t ble_uuid_base[16] = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -/** - * Attempts to convert the supplied 128-bit UUID into its shortened 16-bit - * form. - * - * @param uuid128 The 128-bit UUID to attempt to convert. - * This must point to 16 contiguous bytes. - * - * @return A positive 16-bit unsigned integer on - * success; - * 0 if the UUID cannot be represented in 16 - * bits. - */ -uint16_t -ble_uuid_128_to_16(const void *uuid128) +#if MYNEWT_VAL(BLE_HS_DEBUG) +static int verify_uuid(const ble_uuid_t *uuid) { - const uint8_t *u8ptr; - uint16_t uuid16; - int rc; + switch (uuid->type) { + case BLE_UUID_TYPE_16: + case BLE_UUID_TYPE_32: + return 0; + case BLE_UUID_TYPE_128: + if (memcmp(BLE_UUID128(uuid)->value, ble_uuid_base, 12) != 0) { + return 0; + } + break; + } - u8ptr = uuid128; + return BLE_HS_EBADDATA; +} +#endif - /* The UUID can only be converted if the final 96 bits of its big endian - * representation are equal to the base UUID. - */ - rc = memcmp(u8ptr, ble_uuid_base, sizeof ble_uuid_base - 4); - if (rc != 0) { +int +ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len) +{ + switch (len) { + case 2: + uuid->u.type = BLE_UUID_TYPE_16; + uuid->u16.value = le16toh(buf); + return 0; + case 4: + uuid->u.type = BLE_UUID_TYPE_32; + uuid->u32.value = le32toh(buf); + return 0; + case 16: + uuid->u.type = BLE_UUID_TYPE_128; + memcpy(uuid->u128.value, buf, 16); return 0; } - if (u8ptr[14] != 0 || u8ptr[15] != 0) { - /* This UUID has a 32-bit form, but not a 16-bit form. */ - return 0; - } - - uuid16 = le16toh(u8ptr + 12); - if (uuid16 == 0) { - return 0; - } - - return uuid16; + return BLE_HS_EINVAL; } -/** - * Expands a 16-bit UUID into its 128-bit form. - * - * @param uuid16 The 16-bit UUID to convert. - * @param out_uuid128 On success, the resulting 128-bit UUID gets - * written here. - * - * @return 0 on success; - * BLE_HS_EINVAL if uuid16 is not a valid 16-bit - * UUID. - */ int -ble_uuid_16_to_128(uint16_t uuid16, void *out_uuid128) +ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2) { - uint8_t *u8ptr; + BLE_HS_DBG_ASSERT(verify_uuid(uuid1) == 0); + BLE_HS_DBG_ASSERT(verify_uuid(uuid2) == 0); - if (uuid16 == 0) { + switch (uuid1->type) { + case BLE_UUID_TYPE_16: + return (int) BLE_UUID16(uuid1)->value - (int) BLE_UUID16(uuid2)->value; + case BLE_UUID_TYPE_32: + return (int) BLE_UUID32(uuid1)->value - (int) BLE_UUID32(uuid2)->value; + case BLE_UUID_TYPE_128: + return memcmp(BLE_UUID128(uuid1)->value, BLE_UUID128(uuid2)->value, 16); + } + + BLE_HS_DBG_ASSERT(0); + + return 0; +} + +char * +ble_uuid_to_str(const ble_uuid_t *uuid, char *dst) +{ + const uint8_t *u8p; + + switch (uuid->type) { + case BLE_UUID_TYPE_16: + sprintf(dst, "0x%04" PRIx16, BLE_UUID16(uuid)->value); + break; + case BLE_UUID_TYPE_32: + sprintf(dst, "0x%08" PRIx32, BLE_UUID32(uuid)->value); + break; + case BLE_UUID_TYPE_128: + u8p = BLE_UUID128(uuid)->value; + + sprintf(dst, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x", + u8p[15], u8p[14], u8p[13], u8p[12], + u8p[11], u8p[10], u8p[9], u8p[8], + u8p[7], u8p[6], u8p[5], u8p[4], + u8p[3], u8p[2], u8p[1], u8p[0]); + break; + default: + dst[0] = '\0'; + break; + } + + return dst; +} + +uint16_t +ble_uuid_u16(const ble_uuid_t *uuid) +{ + BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0); + + return uuid->type == BLE_UUID_TYPE_16 ? BLE_UUID16(uuid)->value : 0; +} + +/* APIs below are private (ble_uuid_priv.h) */ + +int +ble_uuid_init_from_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off, + int len) +{ + uint8_t val[16]; + int rc; + + rc = os_mbuf_copydata(om, off, len, val); + if (rc != 0) { + return rc; + } + + rc = ble_uuid_init_from_buf(uuid, val, len); + + return rc; +} + +int +ble_uuid_to_any(const ble_uuid_t *uuid, ble_uuid_any_t *uuid_any) +{ + BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0); + + uuid_any->u.type = uuid->type; + + switch (uuid->type) { + case BLE_UUID_TYPE_16: + uuid_any->u16.value = BLE_UUID16(uuid)->value; + break; + + case BLE_UUID_TYPE_32: + uuid_any->u32.value = BLE_UUID32(uuid)->value; + break; + + case BLE_UUID_TYPE_128: + memcpy(uuid_any->u128.value, BLE_UUID128(uuid)->value, 16); + break; + default: return BLE_HS_EINVAL; } - u8ptr = out_uuid128; - - memcpy(u8ptr, ble_uuid_base, 16); - htole16(u8ptr + 12, uuid16); - return 0; } int -ble_uuid_append(struct os_mbuf *om, const void *uuid128) +ble_uuid_to_mbuf(const ble_uuid_t *uuid, struct os_mbuf *om) { - uint16_t uuid16; + int len; void *buf; - int rc; - uuid16 = ble_uuid_128_to_16(uuid128); - if (uuid16 != 0) { - buf = os_mbuf_extend(om, 2); - if (buf == NULL) { - return BLE_HS_ENOMEM; - } + BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0); - htole16(buf, uuid16); - } else { - rc = os_mbuf_append(om, uuid128, 16); - if (rc != 0) { - return BLE_HS_ENOMEM; - } + len = ble_uuid_length(uuid); + + buf = os_mbuf_extend(om, len); + if (buf == NULL) { + return BLE_HS_ENOMEM; + } + + ble_uuid_flat(uuid, buf); + + return 0; +} + +int +ble_uuid_flat(const ble_uuid_t *uuid, void *dst) +{ + BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0); + + switch (uuid->type) { + case BLE_UUID_TYPE_16: + htole16(dst, BLE_UUID16(uuid)->value); + break; + case BLE_UUID_TYPE_32: + memcpy(dst, ble_uuid_base, 16); + htole32(dst + 12, BLE_UUID32(uuid)->value); + break; + case BLE_UUID_TYPE_128: + memcpy(dst, BLE_UUID128(uuid)->value, 16); + break; + default: + return BLE_HS_EINVAL; } return 0; } int -ble_uuid_extract(struct os_mbuf *om, int off, void *uuid128) +ble_uuid_length(const ble_uuid_t *uuid) { - uint16_t uuid16; - int remlen; - int rc; + BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0); - remlen = OS_MBUF_PKTHDR(om)->omp_len - off; - switch (remlen) { - case 2: - rc = os_mbuf_copydata(om, off, 2, &uuid16); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - - uuid16 = le16toh(&uuid16); - rc = ble_uuid_16_to_128(uuid16, uuid128); - if (rc != 0) { - return rc; - } - return 0; - - case 16: - rc = os_mbuf_copydata(om, off, 16, uuid128); - BLE_HS_DBG_ASSERT_EVAL(rc == 0); - return 0; - - default: - return BLE_HS_EMSGSIZE; - } + return uuid->type >> 3; } diff --git a/nimble/host/src/ble_uuid_priv.h b/nimble/host/src/ble_uuid_priv.h index bd01388b4..d33451613 100644 --- a/nimble/host/src/ble_uuid_priv.h +++ b/nimble/host/src/ble_uuid_priv.h @@ -20,14 +20,19 @@ #ifndef H_BLE_UUID_PRIV_ #define H_BLE_UUID_PRIV_ +#include "host/ble_uuid.h" + #ifdef __cplusplus extern "C" { #endif struct os_mbuf; -int ble_uuid_append(struct os_mbuf *om, const void *uuid128); -int ble_uuid_extract(struct os_mbuf *om, int off, void *uuid128); +int ble_uuid_init_from_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off, int len); +int ble_uuid_to_any(const ble_uuid_t *uuid, ble_uuid_any_t *uuid_any); +int ble_uuid_to_mbuf(const ble_uuid_t *uuid, struct os_mbuf *om); +int ble_uuid_flat(const ble_uuid_t *uuid, void *dst); +int ble_uuid_length(const ble_uuid_t *uuid); #ifdef __cplusplus } diff --git a/nimble/host/test/src/ble_att_svr_test.c b/nimble/host/test/src/ble_att_svr_test.c index c24693fbf..839b2b496 100644 --- a/nimble/host/test/src/ble_att_svr_test.c +++ b/nimble/host/test/src/ble_att_svr_test.c @@ -206,33 +206,18 @@ ble_att_svr_test_misc_attr_fn_r_group(uint16_t conn_handle, } static void -ble_att_svr_test_misc_register_uuid128(uint8_t *uuid128, uint8_t flags, +ble_att_svr_test_misc_register_uuid(const ble_uuid_t *uuid, uint8_t flags, uint16_t expected_handle, ble_att_svr_access_fn *fn) { uint16_t handle; int rc; - rc = ble_att_svr_register(uuid128, flags, 0, &handle, fn, NULL); + rc = ble_att_svr_register(uuid, flags, 0, &handle, fn, NULL); TEST_ASSERT_FATAL(rc == 0); TEST_ASSERT_FATAL(handle == expected_handle); } -static void -ble_att_svr_test_misc_register_uuid16(uint16_t uuid16, uint8_t flags, - uint16_t expected_handle, - ble_att_svr_access_fn *fn) -{ - uint8_t uuid128[16]; - int rc; - - rc = ble_uuid_16_to_128(uuid16, uuid128); - TEST_ASSERT_FATAL(rc == 0); - - ble_att_svr_test_misc_register_uuid128(uuid128, flags, expected_handle, - fn); -} - static void ble_att_svr_test_misc_register_group_attrs(void) { @@ -240,46 +225,48 @@ ble_att_svr_test_misc_register_group_attrs(void) /* Service 0x2233 from 6 to 10 */ /* Service 010203...0f from 11 to 24 */ + static const ble_uuid16_t uuid_svc = + BLE_UUID16_INIT(BLE_ATT_UUID_PRIMARY_SERVICE); + static const ble_uuid16_t uuid_inc = + BLE_UUID16_INIT(BLE_ATT_UUID_INCLUDE); + static const ble_uuid16_t uuid_chr = + BLE_UUID16_INIT(BLE_ATT_UUID_CHARACTERISTIC); + static ble_uuid16_t uuids[24]; + int i; /* Service 0x1122 from 1 to 5 */ - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 1, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_svc.u, HA_FLAG_PERM_RW, 1, + ble_att_svr_test_misc_attr_fn_r_group); for (i = 2; i <= 5; i++) { if ((i - 2) % 2 == 0) { - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_chr.u, HA_FLAG_PERM_RW, i, + ble_att_svr_test_misc_attr_fn_r_group); } else { - ble_att_svr_test_misc_register_uuid16( - i, HA_FLAG_PERM_RW, i, + uuids[i] = *BLE_UUID16(BLE_UUID16_DECLARE(i)); + ble_att_svr_test_misc_register_uuid(&uuids[i].u, HA_FLAG_PERM_RW, i, ble_att_svr_test_misc_attr_fn_r_group); } } /* Service 0x2233 from 6 to 10 */ - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 6, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_svc.u, HA_FLAG_PERM_RW, 6, + ble_att_svr_test_misc_attr_fn_r_group); for (i = 7; i <= 10; i++) { - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_INCLUDE, HA_FLAG_PERM_RW, i, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_inc.u, HA_FLAG_PERM_RW, i, + ble_att_svr_test_misc_attr_fn_r_group); } /* Service 010203...0f from 11 to 24 */ - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 11, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_svc.u, HA_FLAG_PERM_RW, 11, + ble_att_svr_test_misc_attr_fn_r_group); for (i = 12; i <= 24; i++) { if ((i - 12) % 2 == 0) { - ble_att_svr_test_misc_register_uuid16( - BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i, - ble_att_svr_test_misc_attr_fn_r_group); + ble_att_svr_test_misc_register_uuid(&uuid_chr.u, HA_FLAG_PERM_RW, i, + ble_att_svr_test_misc_attr_fn_r_group); } else { - ble_att_svr_test_misc_register_uuid16( - i, HA_FLAG_PERM_RW, i, + uuids[i] = *BLE_UUID16(BLE_UUID16_DECLARE(i)); + ble_att_svr_test_misc_register_uuid(&uuids[i].u, HA_FLAG_PERM_RW, i, ble_att_svr_test_misc_attr_fn_r_group); } } @@ -788,8 +775,10 @@ TEST_CASE(ble_att_svr_test_read) struct os_mbuf *om; uint16_t attr_handle; uint16_t conn_handle; - uint8_t uuid_sec[16] = {1}; - uint8_t uuid[16] = {0}; + const ble_uuid_t *uuid_sec = BLE_UUID128_DECLARE( \ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + const ble_uuid_t *uuid = BLE_UUID128_DECLARE( \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ); int rc; conn_handle = ble_att_svr_test_misc_init(0); @@ -864,7 +853,8 @@ TEST_CASE(ble_att_svr_test_read_blob) { uint16_t attr_handle; uint16_t conn_handle; - uint8_t uuid[16] = {0}; + const ble_uuid_t *uuid = BLE_UUID128_DECLARE( \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); int rc; conn_handle = ble_att_svr_test_misc_init(0); @@ -932,12 +922,12 @@ TEST_CASE(ble_att_svr_test_read_mult) ble_att_svr_test_attr_r_2 = attrs[1].value; ble_att_svr_test_attr_r_2_len = attrs[1].value_len; - rc = ble_att_svr_register(BLE_UUID16(0x1111), HA_FLAG_PERM_RW, 0, + rc = ble_att_svr_register(BLE_UUID16_DECLARE(0x1111), HA_FLAG_PERM_RW, 0, &attrs[0].handle, ble_att_svr_test_misc_attr_fn_r_1, NULL); TEST_ASSERT(rc == 0); - rc = ble_att_svr_register(BLE_UUID16(0x2222), HA_FLAG_PERM_RW, 0, + rc = ble_att_svr_register(BLE_UUID16_DECLARE(0x2222), HA_FLAG_PERM_RW, 0, &attrs[1].handle, ble_att_svr_test_misc_attr_fn_r_2, NULL); TEST_ASSERT(rc == 0); @@ -987,9 +977,12 @@ TEST_CASE(ble_att_svr_test_write) struct ble_hs_conn *conn; uint16_t conn_handle; uint16_t attr_handle; - uint8_t uuid_sec[16] = {2}; - uint8_t uuid_rw[16] = {0}; - uint8_t uuid_r[16] = {1}; + const ble_uuid_t *uuid_sec = BLE_UUID128_DECLARE( \ + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + const ble_uuid_t *uuid_rw = BLE_UUID128_DECLARE( \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + const ble_uuid_t *uuid_r = BLE_UUID128_DECLARE( \ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); int rc; static const uint8_t attr_val[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; @@ -1076,12 +1069,11 @@ TEST_CASE(ble_att_svr_test_find_info) uint16_t handle1; uint16_t handle2; uint16_t handle3; - uint8_t uuid1[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; - uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; - uint8_t uuid3[16] = { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00 - }; + const ble_uuid_t *uuid1 = + BLE_UUID128_DECLARE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15); + const ble_uuid_t *uuid2 = + BLE_UUID128_DECLARE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + const ble_uuid_t *uuid3 = BLE_UUID16_DECLARE(0x000f); int rc; conn_handle = ble_att_svr_test_misc_init(128); @@ -1120,7 +1112,7 @@ TEST_CASE(ble_att_svr_test_find_info) ble_hs_test_util_verify_tx_find_info_rsp( ((struct ble_hs_test_util_att_info_entry[]) { { .handle = handle1, - .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .handle = 0, } })); @@ -1135,10 +1127,10 @@ TEST_CASE(ble_att_svr_test_find_info) ble_hs_test_util_verify_tx_find_info_rsp( ((struct ble_hs_test_util_att_info_entry[]) { { .handle = handle1, - .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .handle = handle2, - .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { .handle = 0, } })); @@ -1153,10 +1145,10 @@ TEST_CASE(ble_att_svr_test_find_info) ble_hs_test_util_verify_tx_find_info_rsp( ((struct ble_hs_test_util_att_info_entry[]) { { .handle = handle1, - .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .handle = handle2, - .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { .handle = 0, } })); @@ -1167,7 +1159,7 @@ TEST_CASE(ble_att_svr_test_find_info) ble_hs_test_util_verify_tx_find_info_rsp( ((struct ble_hs_test_util_att_info_entry[]) { { .handle = handle3, - .uuid16 = 0x000f, + .uuid = BLE_UUID16_DECLARE(0x000f), }, { .handle = 0, } })); @@ -1183,15 +1175,10 @@ TEST_CASE(ble_att_svr_test_find_type_value) uint16_t handle4; uint16_t handle5; uint16_t handle_desc; - uint8_t uuid1[16] = { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00 - }; /* 0x2800 (Primary Service) */ - uint8_t uuid2[16] = { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x03, 0x28, 0x00, 0x00 - }; /* 0x2803 (Characteristic) */ - uint8_t uuid3[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + const ble_uuid_t *uuid1 = BLE_UUID16_DECLARE(0x2800); + const ble_uuid_t *uuid2 = BLE_UUID16_DECLARE(0x2803); + const ble_uuid_t *uuid3 = + BLE_UUID128_DECLARE(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); int rc; conn_handle = ble_att_svr_test_misc_init(128); @@ -1530,7 +1517,7 @@ TEST_CASE(ble_att_svr_test_read_group_type) ((struct ble_hs_test_util_att_group_type_entry[]) { { .start_handle = 1, .end_handle = 5, - .uuid16 = 0x1122, + .uuid = BLE_UUID16_DECLARE(0x1122), }, { .start_handle = 0, } })); @@ -1543,11 +1530,11 @@ TEST_CASE(ble_att_svr_test_read_group_type) ((struct ble_hs_test_util_att_group_type_entry[]) { { .start_handle = 1, .end_handle = 5, - .uuid16 = 0x1122, + .uuid = BLE_UUID16_DECLARE(0x1122), }, { .start_handle = 6, .end_handle = 10, - .uuid16 = 0x2233, + .uuid = BLE_UUID16_DECLARE(0x2233), }, { .start_handle = 0, } })); @@ -1560,11 +1547,11 @@ TEST_CASE(ble_att_svr_test_read_group_type) ((struct ble_hs_test_util_att_group_type_entry[]) { { .start_handle = 1, .end_handle = 5, - .uuid16 = 0x1122, + .uuid = BLE_UUID16_DECLARE(0x1122), }, { .start_handle = 6, .end_handle = 10, - .uuid16 = 0x2233, + .uuid = BLE_UUID16_DECLARE(0x2233), }, { .start_handle = 0, } })); @@ -1577,7 +1564,7 @@ TEST_CASE(ble_att_svr_test_read_group_type) ((struct ble_hs_test_util_att_group_type_entry[]) { { .start_handle = 11, .end_handle = 0xffff, - .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { .start_handle = 0, } })); @@ -1600,27 +1587,32 @@ TEST_CASE(ble_att_svr_test_prep_write) } /* Register two writable attributes. */ - ble_att_svr_test_misc_register_uuid16(0x1234, HA_FLAG_PERM_RW, 1, + ble_att_svr_test_misc_register_uuid(BLE_UUID16_DECLARE(0x1234), + HA_FLAG_PERM_RW, 1, ble_att_svr_test_misc_attr_fn_w_1); - ble_att_svr_test_misc_register_uuid16(0x8989, HA_FLAG_PERM_RW, 2, + ble_att_svr_test_misc_register_uuid(BLE_UUID16_DECLARE(0x8989), + HA_FLAG_PERM_RW, 2, ble_att_svr_test_misc_attr_fn_w_2); /* 3: not writable. */ - ble_att_svr_test_misc_register_uuid16(0xabab, BLE_ATT_F_READ, 3, + ble_att_svr_test_misc_register_uuid(BLE_UUID16_DECLARE(0xabab), + BLE_ATT_F_READ, 3, ble_att_svr_test_misc_attr_fn_r_1); /* 4: Encryption required. */ - ble_att_svr_test_misc_register_uuid16( - 0xabac, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, 4, + ble_att_svr_test_misc_register_uuid( + BLE_UUID16_DECLARE(0xabac), BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, 4, ble_att_svr_test_misc_attr_fn_w_1); /* 5: Encryption+authentication required. */ - ble_att_svr_test_misc_register_uuid16( - 0xabad, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC | BLE_ATT_F_WRITE_AUTHEN, + ble_att_svr_test_misc_register_uuid( + BLE_UUID16_DECLARE(0xabad), + BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC | BLE_ATT_F_WRITE_AUTHEN, 5, ble_att_svr_test_misc_attr_fn_w_1); /* 6: Write callback always fails. */ - ble_att_svr_test_misc_register_uuid16( - 0xabae, BLE_ATT_F_WRITE, 6, ble_att_svr_test_misc_attr_fn_w_fail); + ble_att_svr_test_misc_register_uuid( + BLE_UUID16_DECLARE(0xabae), BLE_ATT_F_WRITE, 6, + ble_att_svr_test_misc_attr_fn_w_fail); /*** Empty write succeeds. */ ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_EXECUTE, @@ -1780,7 +1772,8 @@ TEST_CASE(ble_att_svr_test_prep_write_tmo) } /* Register a writable attribute. */ - ble_att_svr_test_misc_register_uuid16(0x1234, HA_FLAG_PERM_RW, 1, + ble_att_svr_test_misc_register_uuid(BLE_UUID16_DECLARE(0x1234), + HA_FLAG_PERM_RW, 1, ble_att_svr_test_misc_attr_fn_w_1); /* Ensure timer is not set. */ @@ -1853,9 +1846,9 @@ TEST_CASE(ble_att_svr_test_oom) conn_handle = ble_att_svr_test_misc_init(0); /* Register an attribute (primary service) for incoming read commands. */ - ble_att_svr_test_misc_register_uuid16(BLE_ATT_UUID_PRIMARY_SERVICE, - HA_FLAG_PERM_RW, 1, - ble_att_svr_test_misc_attr_fn_rw_1); + ble_att_svr_test_misc_register_uuid( + BLE_UUID16_DECLARE(BLE_ATT_UUID_PRIMARY_SERVICE), + HA_FLAG_PERM_RW, 1, ble_att_svr_test_misc_attr_fn_rw_1); ble_att_svr_test_attr_w_1_len = 2; ble_att_svr_test_attr_w_1[0] = 0x12; ble_att_svr_test_attr_w_1[1] = 0x34; @@ -1883,7 +1876,7 @@ TEST_CASE(ble_att_svr_test_oom) ble_hs_test_util_tx_all(); ble_hs_test_util_verify_tx_find_info_rsp( (struct ble_hs_test_util_att_info_entry[]) { - { .handle = 1, .uuid16 = BLE_ATT_UUID_PRIMARY_SERVICE }, + { .handle = 1, .uuid = BLE_UUID16_DECLARE(BLE_ATT_UUID_PRIMARY_SERVICE) }, { 0 }, }); diff --git a/nimble/host/test/src/ble_gatt_conn_test.c b/nimble/host/test/src/ble_gatt_conn_test.c index 4add074e2..0bb37eae2 100644 --- a/nimble/host/test/src/ble_gatt_conn_test.c +++ b/nimble/host/test/src/ble_gatt_conn_test.c @@ -389,7 +389,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect) ble_gatt_conn_test_util_init(); /*** Register an attribute to allow indicatations to be sent. */ - rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ, 0, + rc = ble_att_svr_register(BLE_UUID16_DECLARE(0x1212), BLE_ATT_F_READ, 0, &attr_handle, ble_gatt_conn_test_attr_cb, NULL); TEST_ASSERT(rc == 0); @@ -413,7 +413,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect) TEST_ASSERT_FATAL(rc == 0); disc_svc_uuid_arg.exp_conn_handle = 1; - rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16(0x1111), + rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16_DECLARE(0x1111), ble_gatt_conn_test_disc_svc_uuid_cb, &disc_svc_uuid_arg); TEST_ASSERT_FATAL(rc == 0); @@ -437,7 +437,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect) &disc_all_dscs_arg); disc_chr_uuid_arg.exp_conn_handle = 2; - rc = ble_gattc_disc_chrs_by_uuid(2, 2, 0xffff, BLE_UUID16(0x2222), + rc = ble_gattc_disc_chrs_by_uuid(2, 2, 0xffff, BLE_UUID16_DECLARE(0x2222), ble_gatt_conn_test_disc_chr_uuid_cb, &disc_chr_uuid_arg); @@ -447,7 +447,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect) TEST_ASSERT_FATAL(rc == 0); read_uuid_arg.exp_conn_handle = 2; - rc = ble_gattc_read_by_uuid(2, 1, 0xffff, BLE_UUID16(0x3333), + rc = ble_gattc_read_by_uuid(2, 1, 0xffff, BLE_UUID16_DECLARE(0x3333), ble_gatt_conn_test_read_uuid_cb, &read_uuid_arg); TEST_ASSERT_FATAL(rc == 0); @@ -614,7 +614,7 @@ TEST_CASE(ble_gatt_conn_test_timeout) TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER); /*** Register an attribute to allow indicatations to be sent. */ - rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ, 0, + rc = ble_att_svr_register(BLE_UUID16_DECLARE(0x1212), BLE_ATT_F_READ, 0, &attr_handle, ble_gatt_conn_test_attr_cb, NULL); TEST_ASSERT(rc == 0); @@ -634,7 +634,7 @@ TEST_CASE(ble_gatt_conn_test_timeout) /*** Discover services by UUID. */ ble_hs_test_util_create_conn(1, peer_addr, NULL, NULL); - rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16(0x1111), + rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16_DECLARE(0x1111), ble_gatt_conn_test_disc_svc_uuid_cb, &disc_svc_uuid_arg); TEST_ASSERT_FATAL(rc == 0); @@ -666,7 +666,7 @@ TEST_CASE(ble_gatt_conn_test_timeout) /*** Discover characteristics by UUID. */ ble_hs_test_util_create_conn(1, peer_addr, NULL, NULL); - rc = ble_gattc_disc_chrs_by_uuid(1, 2, 0xffff, BLE_UUID16(0x2222), + rc = ble_gattc_disc_chrs_by_uuid(1, 2, 0xffff, BLE_UUID16_DECLARE(0x2222), ble_gatt_conn_test_disc_chr_uuid_cb, &disc_all_dscs_arg); TEST_ASSERT_FATAL(rc == 0); @@ -681,7 +681,7 @@ TEST_CASE(ble_gatt_conn_test_timeout) /*** Read by UUID. */ ble_hs_test_util_create_conn(1, peer_addr, NULL, NULL); - rc = ble_gattc_read_by_uuid(1, 1, 0xffff, BLE_UUID16(0x3333), + rc = ble_gattc_read_by_uuid(1, 1, 0xffff, BLE_UUID16_DECLARE(0x3333), ble_gatt_conn_test_read_uuid_cb, &read_uuid_arg); TEST_ASSERT_FATAL(rc == 0); diff --git a/nimble/host/test/src/ble_gatt_disc_c_test.c b/nimble/host/test/src/ble_gatt_disc_c_test.c index 6c384cd3a..7ecb48e65 100644 --- a/nimble/host/test/src/ble_gatt_disc_c_test.c +++ b/nimble/host/test/src/ble_gatt_disc_c_test.c @@ -30,9 +30,8 @@ struct ble_gatt_disc_c_test_char { uint16_t def_handle; uint16_t val_handle; - uint16_t uuid16; /* 0 if not present. */ uint8_t properties; - uint8_t uuid128[16]; + const ble_uuid_t *uuid; }; #define BLE_GATT_DISC_C_TEST_MAX_CHARS 256 @@ -63,7 +62,7 @@ ble_gatt_disc_c_test_misc_rx_rsp_once( /* Send the pending ATT Read By Type Request. */ ble_hs_test_util_tx_all(); - if (chars[0].uuid16 != 0) { + if (chars[0].uuid->type == BLE_UUID_TYPE_16) { rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ + BLE_GATT_CHR_DECL_SZ_16; } else { @@ -81,11 +80,12 @@ ble_gatt_disc_c_test_misc_rx_rsp_once( } /* If the value length is changing, we need a separate response. */ - if (((chars[i].uuid16 == 0) ^ (chars[0].uuid16 == 0)) != 0) { + if (((chars[i].uuid->type == BLE_UUID_TYPE_16) ^ + (chars[0].uuid->type == BLE_UUID_TYPE_16)) != 0) { break; } - if (chars[i].uuid16 != 0) { + if (chars[i].uuid->type == BLE_UUID_TYPE_16) { if (off + BLE_ATT_READ_TYPE_ADATA_SZ_16 > ble_att_mtu(conn_handle)) { @@ -110,13 +110,8 @@ ble_gatt_disc_c_test_misc_rx_rsp_once( htole16(buf + off, chars[i].val_handle); off += 2; - if (chars[i].uuid16 != 0) { - htole16(buf + off, chars[i].uuid16); - off += 2; - } else { - memcpy(buf + off, chars[i].uuid128, 16); - off += 16; - } + ble_uuid_flat(chars[i].uuid, buf + off); + off += ble_uuid_length(chars[i].uuid); } rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, @@ -157,7 +152,6 @@ static void ble_gatt_disc_c_test_misc_verify_chars(struct ble_gatt_disc_c_test_char *chars, int stop_after) { - uint16_t uuid16; int i; if (stop_after == 0) { @@ -169,14 +163,8 @@ ble_gatt_disc_c_test_misc_verify_chars(struct ble_gatt_disc_c_test_char *chars, ble_gatt_disc_c_test_chars[i].def_handle); TEST_ASSERT(chars[i].val_handle == ble_gatt_disc_c_test_chars[i].val_handle); - if (chars[i].uuid16 != 0) { - uuid16 = ble_uuid_128_to_16(ble_gatt_disc_c_test_chars[i].uuid128); - TEST_ASSERT(chars[i].uuid16 == uuid16); - } else { - TEST_ASSERT(memcmp(chars[i].uuid128, - ble_gatt_disc_c_test_chars[i].uuid128, - 16) == 0); - } + TEST_ASSERT(ble_uuid_cmp(chars[i].uuid, + &ble_gatt_disc_c_test_chars[i].uuid.u) == 0); } TEST_ASSERT(i == ble_gatt_disc_c_test_num_chars); @@ -249,7 +237,7 @@ ble_gatt_disc_c_test_misc_all(uint16_t start_handle, uint16_t end_handle, static void ble_gatt_disc_c_test_misc_uuid(uint16_t start_handle, uint16_t end_handle, - int stop_after, uint8_t *uuid128, + int stop_after, const ble_uuid_t *uuid, struct ble_gatt_disc_c_test_char *rsp_chars, struct ble_gatt_disc_c_test_char *ret_chars) { @@ -261,7 +249,7 @@ ble_gatt_disc_c_test_misc_uuid(uint16_t start_handle, uint16_t end_handle, NULL, NULL); rc = ble_gattc_disc_chrs_by_uuid(2, start_handle, end_handle, - uuid128, + uuid, ble_gatt_disc_c_test_misc_cb, &stop_after); TEST_ASSERT(rc == 0); @@ -278,7 +266,7 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { 0 } }); @@ -288,11 +276,11 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 57, .val_handle = 58, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { 0 } }); @@ -302,23 +290,23 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 57, .val_handle = 58, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { .def_handle = 59, .val_handle = 60, - .uuid16 = 0x5372, + .uuid = BLE_UUID16_DECLARE(0x5372), }, { .def_handle = 61, .val_handle = 62, - .uuid16 = 0xab93, + .uuid = BLE_UUID16_DECLARE(0xab93), }, { .def_handle = 63, .val_handle = 64, - .uuid16 = 0x0023, + .uuid = BLE_UUID16_DECLARE(0x0023), }, { 0 } }); @@ -328,23 +316,25 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 83, .val_handle = 84, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 87, .val_handle = 88, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15), }, { .def_handle = 91, .val_handle = 92, - .uuid16 = 0x0003, + .uuid = BLE_UUID16_DECLARE(0x0003), }, { .def_handle = 93, .val_handle = 94, - .uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 }, + .uuid = BLE_UUID128_DECLARE(1, 0, 4, 0, 6, 9, 17, 7, + 8, 43, 7, 4, 12, 43, 19, 35), }, { .def_handle = 98, .val_handle = 99, - .uuid16 = 0xabfa, + .uuid = BLE_UUID16_DECLARE(0xabfa), }, { 0 } }); @@ -354,11 +344,11 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 99, .val_handle = 100, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { 0 } }); @@ -368,23 +358,23 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 57, .val_handle = 58, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { .def_handle = 59, .val_handle = 60, - .uuid16 = 0x5372, + .uuid = BLE_UUID16_DECLARE(0x5372), }, { .def_handle = 61, .val_handle = 62, - .uuid16 = 0xab93, + .uuid = BLE_UUID16_DECLARE(0xab93), }, { .def_handle = 63, .val_handle = 64, - .uuid16 = 0x0023, + .uuid = BLE_UUID16_DECLARE(0x0023), }, { 0 } }); } @@ -392,156 +382,156 @@ TEST_CASE(ble_gatt_disc_c_test_disc_all) TEST_CASE(ble_gatt_disc_c_test_disc_uuid) { /*** One 16-bit characteristic. */ - ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010), + ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16_DECLARE(0x2010), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { 0 } } ); /*** No matching characteristics. */ - ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010), + ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16_DECLARE(0x2010), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x1234, + .uuid = BLE_UUID16_DECLARE(0x1234), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { 0 } } ); /*** 2/5 16-bit characteristics. */ - ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010), + ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16_DECLARE(0x2010), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 57, .val_handle = 58, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { .def_handle = 59, .val_handle = 60, - .uuid16 = 0x5372, + .uuid = BLE_UUID16_DECLARE(0x5372), }, { .def_handle = 61, .val_handle = 62, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 63, .val_handle = 64, - .uuid16 = 0x0023, + .uuid = BLE_UUID16_DECLARE(0x0023), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 61, .val_handle = 62, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { 0 } } ); /*** Interleaved 16-bit and 128-bit characteristics. */ ble_gatt_disc_c_test_misc_uuid( 50, 100, 0, - ((uint8_t[]){ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }), + BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 83, .val_handle = 84, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 87, .val_handle = 88, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .def_handle = 91, .val_handle = 92, - .uuid16 = 0x0003, + .uuid = BLE_UUID16_DECLARE(0x0003), }, { .def_handle = 93, .val_handle = 94, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .def_handle = 98, .val_handle = 99, - .uuid16 = 0xabfa, + .uuid = BLE_UUID16_DECLARE(0xabfa), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 87, .val_handle = 88, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .def_handle = 93, .val_handle = 94, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { 0 } } ); /*** Ends with final handle ID. */ - ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x64ba), + ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16_DECLARE(0x64ba), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 99, .val_handle = 100, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 99, .val_handle = 100, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { 0 } } ); /*** Stop after first characteristic. */ - ble_gatt_disc_c_test_misc_uuid(50, 100, 1, BLE_UUID16(0x2010), + ble_gatt_disc_c_test_misc_uuid(50, 100, 1, BLE_UUID16_DECLARE(0x2010), (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 57, .val_handle = 58, - .uuid16 = 0x64ba, + .uuid = BLE_UUID16_DECLARE(0x64ba), }, { .def_handle = 59, .val_handle = 60, - .uuid16 = 0x5372, + .uuid = BLE_UUID16_DECLARE(0x5372), }, { .def_handle = 61, .val_handle = 62, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { .def_handle = 63, .val_handle = 64, - .uuid16 = 0x0023, + .uuid = BLE_UUID16_DECLARE(0x0023), }, { 0 } }, (struct ble_gatt_disc_c_test_char[]) { { .def_handle = 55, .val_handle = 56, - .uuid16 = 0x2010, + .uuid = BLE_UUID16_DECLARE(0x2010), }, { 0 } } ); } @@ -553,12 +543,12 @@ TEST_CASE(ble_gatt_disc_c_test_oom_all) { .def_handle = 93, .val_handle = 94, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .def_handle = 95, .val_handle = 96, - .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { 0 } }; @@ -641,12 +631,12 @@ TEST_CASE(ble_gatt_disc_c_test_oom_uuid) { .def_handle = 93, .val_handle = 94, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .def_handle = 95, .val_handle = 96, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { 0 } }; @@ -664,7 +654,7 @@ TEST_CASE(ble_gatt_disc_c_test_oom_uuid) /* Initiate a discover characteristics by UUID procedure. */ stop_after = 0; - rc = ble_gattc_disc_chrs_by_uuid(1, 1, 0xffff, chrs[0].uuid128, + rc = ble_gattc_disc_chrs_by_uuid(1, 1, 0xffff, chrs[0].uuid, ble_gatt_disc_c_test_misc_cb, &stop_after); TEST_ASSERT_FATAL(rc == 0); diff --git a/nimble/host/test/src/ble_gatt_disc_d_test.c b/nimble/host/test/src/ble_gatt_disc_d_test.c index 849f405f0..ce3fc4e04 100644 --- a/nimble/host/test/src/ble_gatt_disc_d_test.c +++ b/nimble/host/test/src/ble_gatt_disc_d_test.c @@ -30,7 +30,7 @@ struct ble_gatt_disc_d_test_dsc { uint16_t chr_val_handle; /* 0 if last entry. */ uint16_t dsc_handle; - uint8_t dsc_uuid128[16]; + ble_uuid_any_t dsc_uuid; }; #define BLE_GATT_DISC_D_TEST_MAX_DSCS 256 @@ -54,8 +54,6 @@ ble_gatt_disc_d_test_misc_rx_rsp_once( { struct ble_att_find_info_rsp rsp; uint8_t buf[1024]; - uint16_t uuid16_cur; - uint16_t uuid16_0; int off; int rc; int i; @@ -63,8 +61,7 @@ ble_gatt_disc_d_test_misc_rx_rsp_once( /* Send the pending ATT Read By Type Request. */ ble_hs_test_util_tx_all(); - uuid16_0 = ble_uuid_128_to_16(dscs[0].dsc_uuid128); - if (uuid16_0 != 0) { + if (dscs[0].dsc_uuid.u.type == BLE_UUID_TYPE_16) { rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT; } else { rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT; @@ -79,9 +76,7 @@ ble_gatt_disc_d_test_misc_rx_rsp_once( break; } - uuid16_cur = ble_uuid_128_to_16(dscs[i].dsc_uuid128); - - if (uuid16_cur != 0) { + if (dscs[i].dsc_uuid.u.type == BLE_UUID_TYPE_16) { if (off + BLE_ATT_FIND_INFO_IDATA_16_SZ > ble_att_mtu(conn_handle)) { @@ -98,20 +93,16 @@ ble_gatt_disc_d_test_misc_rx_rsp_once( } /* If the value length is changing, we need a separate response. */ - if (((uuid16_0 == 0) ^ (uuid16_cur == 0)) != 0) { + if (((dscs[0].dsc_uuid.u.type == BLE_UUID_TYPE_16) ^ + (dscs[i].dsc_uuid.u.type == BLE_UUID_TYPE_16)) != 0) { break; } htole16(buf + off, dscs[i].dsc_handle); off += 2; - if (uuid16_cur != 0) { - htole16(buf + off, uuid16_cur); - off += 2; - } else { - memcpy(buf + off, dscs[i].dsc_uuid128, 16); - off += 16; - } + ble_uuid_flat(&dscs[i].dsc_uuid.u, buf + off); + off += ble_uuid_length(&dscs[i].dsc_uuid.u); } rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, @@ -162,9 +153,8 @@ ble_gatt_disc_d_test_misc_verify_dscs(struct ble_gatt_disc_d_test_dsc *dscs, ble_gatt_disc_d_test_dscs[i].chr_val_handle); TEST_ASSERT(dscs[i].dsc_handle == ble_gatt_disc_d_test_dscs[i].dsc_handle); - TEST_ASSERT(memcmp(dscs[i].dsc_uuid128, - ble_gatt_disc_d_test_dscs[i].dsc_uuid128, - 16) == 0); + TEST_ASSERT(ble_uuid_cmp(&dscs[i].dsc_uuid.u, + &ble_gatt_disc_d_test_dscs[i].dsc_uuid.u) == 0); } TEST_ASSERT(i == ble_gatt_disc_d_test_num_dscs); @@ -194,7 +184,7 @@ ble_gatt_disc_d_test_misc_cb(uint16_t conn_handle, dst = ble_gatt_disc_d_test_dscs + ble_gatt_disc_d_test_num_dscs++; dst->chr_val_handle = chr_val_handle; dst->dsc_handle = dsc->handle; - memcpy(dst->dsc_uuid128, dsc->uuid128, 16); + dst->dsc_uuid = dsc->uuid; break; case BLE_HS_EDONE: @@ -246,7 +236,7 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 5, .dsc_handle = 6, - .dsc_uuid128 = BLE_UUID16_ARR(0x1234), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1234), }, { 0 } }) @@ -257,11 +247,11 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 50, .dsc_handle = 51, - .dsc_uuid128 = BLE_UUID16_ARR(0x1111), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1111), }, { .chr_val_handle = 50, .dsc_handle = 52, - .dsc_uuid128 = BLE_UUID16_ARR(0x2222), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x2222), }, { 0 } }) @@ -272,23 +262,23 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 50, .dsc_handle = 51, - .dsc_uuid128 = BLE_UUID16_ARR(0x1111), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1111), }, { .chr_val_handle = 50, .dsc_handle = 52, - .dsc_uuid128 = BLE_UUID16_ARR(0x2222), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x2222), }, { .chr_val_handle = 50, .dsc_handle = 53, - .dsc_uuid128 = BLE_UUID16_ARR(0x3333), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x3333), }, { .chr_val_handle = 50, .dsc_handle = 54, - .dsc_uuid128 = BLE_UUID16_ARR(0x4444), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x4444), }, { .chr_val_handle = 50, .dsc_handle = 55, - .dsc_uuid128 = BLE_UUID16_ARR(0x5555), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x5555), }, { 0 } }) @@ -299,23 +289,23 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 50, .dsc_handle = 51, - .dsc_uuid128 = BLE_UUID16_ARR(0x1111), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1111), }, { .chr_val_handle = 50, .dsc_handle = 52, - .dsc_uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .dsc_uuid.u128 = BLE_UUID128_INIT( 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .chr_val_handle = 50, .dsc_handle = 53, - .dsc_uuid128 = BLE_UUID16_ARR(0x3333), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x3333), }, { .chr_val_handle = 50, .dsc_handle = 54, - .dsc_uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 }, + .dsc_uuid.u128 = BLE_UUID128_INIT(1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35), }, { .chr_val_handle = 50, .dsc_handle = 55, - .dsc_uuid128 = BLE_UUID16_ARR(0x5555), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x5555), }, { 0 } }) @@ -326,11 +316,11 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 50, .dsc_handle = 51, - .dsc_uuid128 = BLE_UUID16_ARR(0x1111), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1111), }, { .chr_val_handle = 50, .dsc_handle = 52, - .dsc_uuid128 = BLE_UUID16_ARR(0x2222), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x2222), }, { 0 } }) @@ -341,23 +331,23 @@ TEST_CASE(ble_gatt_disc_d_test_1) ((struct ble_gatt_disc_d_test_dsc[]) { { .chr_val_handle = 50, .dsc_handle = 51, - .dsc_uuid128 = BLE_UUID16_ARR(0x1111), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x1111), }, { .chr_val_handle = 50, .dsc_handle = 52, - .dsc_uuid128 = BLE_UUID16_ARR(0x2222), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x2222), }, { .chr_val_handle = 50, .dsc_handle = 53, - .dsc_uuid128 = BLE_UUID16_ARR(0x3333), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x3333), }, { .chr_val_handle = 50, .dsc_handle = 54, - .dsc_uuid128 = BLE_UUID16_ARR(0x4444), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x4444), }, { .chr_val_handle = 50, .dsc_handle = 55, - .dsc_uuid128 = BLE_UUID16_ARR(0x5555), + .dsc_uuid.u16 = BLE_UUID16_INIT(0x5555), }, { 0 } }) @@ -370,12 +360,12 @@ TEST_CASE(ble_gatt_disc_d_test_oom_all) { .chr_val_handle = 543, .dsc_handle = 548, - .dsc_uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .dsc_uuid.u128 = BLE_UUID128_INIT(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .chr_val_handle = 543, .dsc_handle = 549, - .dsc_uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .dsc_uuid.u128 = BLE_UUID128_INIT(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { 0 } }; diff --git a/nimble/host/test/src/ble_gatt_disc_s_test.c b/nimble/host/test/src/ble_gatt_disc_s_test.c index cb3d575e1..8c8bf9afd 100644 --- a/nimble/host/test/src/ble_gatt_disc_s_test.c +++ b/nimble/host/test/src/ble_gatt_disc_s_test.c @@ -28,8 +28,7 @@ struct ble_gatt_disc_s_test_svc { uint16_t start_handle; uint16_t end_handle; - uint16_t uuid16; - uint8_t uuid128[16]; + const ble_uuid_t *uuid; }; #define BLE_GATT_DISC_S_TEST_MAX_SERVICES 256 @@ -49,7 +48,7 @@ ble_gatt_disc_s_test_init(void) static int ble_gatt_disc_s_test_misc_svc_length(struct ble_gatt_disc_s_test_svc *service) { - if (service->uuid16 != 0) { + if (service->uuid->type == BLE_UUID_TYPE_16) { return 6; } else { return 20; @@ -86,7 +85,7 @@ ble_gatt_disc_s_test_misc_rx_all_rsp_once( break; } - if (services[i].uuid16 != 0) { + if (services[i].uuid->type == BLE_UUID_TYPE_16) { if (off + BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16 > ble_att_mtu(conn_handle)) { @@ -108,13 +107,8 @@ ble_gatt_disc_s_test_misc_rx_all_rsp_once( htole16(buf + off, services[i].end_handle); off += 2; - if (services[i].uuid16 != 0) { - htole16(buf + off, services[i].uuid16); - off += 2; - } else { - memcpy(buf + off, services[i].uuid128, 16); - off += 16; - } + ble_uuid_flat(services[i].uuid, buf + off); + off += ble_uuid_length(services[i].uuid); } rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, @@ -217,8 +211,6 @@ static void ble_gatt_disc_s_test_misc_verify_services( struct ble_gatt_disc_s_test_svc *services) { - uint16_t uuid16; - uint8_t *uuid128; int i; for (i = 0; services[i].start_handle != 0; i++) { @@ -227,13 +219,8 @@ ble_gatt_disc_s_test_misc_verify_services( TEST_ASSERT(services[i].end_handle == ble_gatt_disc_s_test_svcs[i].end_handle); - uuid128 = ble_gatt_disc_s_test_svcs[i].uuid128; - uuid16 = ble_uuid_128_to_16(uuid128); - if (uuid16 != 0) { - TEST_ASSERT(services[i].uuid16 == uuid16); - } else { - TEST_ASSERT(memcmp(services[i].uuid128, uuid128, 16) == 0); - } + TEST_ASSERT(ble_uuid_cmp(services[i].uuid, + &ble_gatt_disc_s_test_svcs[i].uuid.u) == 0); } TEST_ASSERT(i == ble_gatt_disc_s_test_num_svcs); @@ -301,11 +288,7 @@ ble_gatt_disc_s_test_misc_good_uuid( ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}), NULL, NULL); - if (services[0].uuid16 != 0) { - rc = ble_uuid_16_to_128(services[0].uuid16, services[0].uuid128); - TEST_ASSERT_FATAL(rc == 0); - } - rc = ble_gattc_disc_svc_by_uuid(2, services[0].uuid128, + rc = ble_gattc_disc_svc_by_uuid(2, services[0].uuid, ble_gatt_disc_s_test_misc_disc_cb, NULL); TEST_ASSERT(rc == 0); @@ -317,38 +300,38 @@ TEST_CASE(ble_gatt_disc_s_test_disc_all) { /*** One 128-bit service. */ ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 } }); /*** Two 128-bit services. */ ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 10, 50, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 10, 50, BLE_UUID128_DECLARE(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), }, { 0 } }); /*** Five 128-bit services. */ ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 10, 50, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, - { 80, 120, 0, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, }, - { 123, 678, 0, {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }, }, - { 751, 999, 0, {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 10, 50, BLE_UUID128_DECLARE(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), }, + { 80, 120, BLE_UUID128_DECLARE(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ), }, + { 123, 678, BLE_UUID128_DECLARE(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ), }, + { 751, 999, BLE_UUID128_DECLARE(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ), }, { 0 } }); /*** One 128-bit service, one 16-bit-service. */ ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 6, 7, 0x1234 }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 6, 7, BLE_UUID16_DECLARE(0x1234) }, { 0 } }); /*** End with handle 0xffff. */ ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 7, 0xffff, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 7, 0xffff,BLE_UUID128_DECLARE(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), }, { 0 } }); } @@ -357,61 +340,61 @@ TEST_CASE(ble_gatt_disc_s_test_disc_uuid) { /*** 128-bit service; one entry. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 } }); /*** 128-bit service; two entries. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 8, 43, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 8, 43, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 } }); /*** 128-bit service; five entries. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 8, 43, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 67, 100, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 102, 103, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 262, 900, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 8, 43, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 67, 100, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 102, 103, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 262, 900, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 } }); /*** 128-bit service; end with handle 0xffff. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 7, 0xffff, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 7, 0xffff,BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 } }); /*** 16-bit service; one entry. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0x1234 }, + { 1, 5, BLE_UUID16_DECLARE(0x1234) }, { 0 } }); /*** 16-bit service; two entries. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0x1234 }, - { 85, 243, 0x1234 }, + { 1, 5, BLE_UUID16_DECLARE(0x1234) }, + { 85, 243, BLE_UUID16_DECLARE(0x1234) }, { 0 } }); /*** 16-bit service; five entries. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0x1234 }, - { 85, 243, 0x1234 }, - { 382, 383, 0x1234 }, - { 562, 898, 0x1234 }, - { 902, 984, 0x1234 }, + { 1, 5, BLE_UUID16_DECLARE(0x1234) }, + { 85, 243, BLE_UUID16_DECLARE(0x1234) }, + { 382, 383, BLE_UUID16_DECLARE(0x1234) }, + { 562, 898, BLE_UUID16_DECLARE(0x1234) }, + { 902, 984, BLE_UUID16_DECLARE(0x1234) }, { 0 } }); /*** 16-bit service; end with handle 0xffff. */ ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) { - { 1, 5, 0x1234 }, - { 9, 0xffff, 0x1234 }, + { 1, 5, BLE_UUID16_DECLARE(0x1234) }, + { 9, 0xffff,BLE_UUID16_DECLARE(0x1234) }, { 0 } }); } @@ -419,8 +402,8 @@ TEST_CASE(ble_gatt_disc_s_test_disc_uuid) TEST_CASE(ble_gatt_disc_s_test_oom_all) { struct ble_gatt_disc_s_test_svc svcs[] = { - { 1, 5, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 6, 10, 0, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 6, 10, BLE_UUID128_DECLARE(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), }, { 0 }, }; @@ -493,12 +476,12 @@ TEST_CASE(ble_gatt_disc_s_test_oom_uuid) { /* Retrieve enough services to require two transactions. */ struct ble_gatt_disc_s_test_svc svcs[] = { - { 1, 5, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 6, 10, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 11, 15, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 16, 20, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 21, 25, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 26, 30, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 6, 10, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 11, 15, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 16, 20, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 21, 25, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 26, 30, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, { 0 }, }; @@ -513,7 +496,7 @@ TEST_CASE(ble_gatt_disc_s_test_oom_uuid) NULL, NULL); /* Initiate a discover all services procedure. */ - rc = ble_gattc_disc_svc_by_uuid(1, svcs[0].uuid128, + rc = ble_gattc_disc_svc_by_uuid(1, svcs[0].uuid, ble_gatt_disc_s_test_misc_disc_cb, NULL); TEST_ASSERT_FATAL(rc == 0); @@ -573,8 +556,8 @@ TEST_CASE(ble_gatt_disc_s_test_oom_uuid) TEST_CASE(ble_gatt_disc_s_test_oom_timeout) { struct ble_gatt_disc_s_test_svc svcs[] = { - { 1, 5, 0, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 6, 10, 0, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + { 1, 5, BLE_UUID128_DECLARE(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), }, + { 6, 10, BLE_UUID128_DECLARE(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), }, { 0 }, }; diff --git a/nimble/host/test/src/ble_gatt_find_s_test.c b/nimble/host/test/src/ble_gatt_find_s_test.c index 79397d54f..d8834f4c6 100644 --- a/nimble/host/test/src/ble_gatt_find_s_test.c +++ b/nimble/host/test/src/ble_gatt_find_s_test.c @@ -33,7 +33,7 @@ struct ble_gatt_find_s_test_entry { uint16_t inc_handle; /* 0 indicates no more entries. */ uint16_t start_handle; uint16_t end_handle; - uint8_t uuid128[16]; + const ble_uuid_t *uuid; }; static void @@ -81,8 +81,8 @@ ble_gatt_find_s_test_misc_verify_incs( entries[i].start_handle); TEST_ASSERT(ble_gatt_find_s_test_svcs[i].end_handle == entries[i].end_handle); - TEST_ASSERT(memcmp(ble_gatt_find_s_test_svcs[i].uuid128, - entries[i].uuid128, 16) == 0); + TEST_ASSERT(ble_uuid_cmp(&ble_gatt_find_s_test_svcs[i].uuid.u, + entries[i].uuid) == 0); } TEST_ASSERT(i == ble_gatt_find_s_test_num_svcs); @@ -94,7 +94,6 @@ ble_gatt_find_s_test_misc_rx_read_type( uint16_t conn_handle, struct ble_gatt_find_s_test_entry *entries) { struct ble_att_read_type_rsp rsp; - uint16_t uuid16; uint8_t buf[1024]; int off; int rc; @@ -108,8 +107,7 @@ ble_gatt_find_s_test_misc_rx_read_type( break; } - uuid16 = ble_uuid_128_to_16(entries[i].uuid128); - if (uuid16 == 0) { + if (entries[i].uuid->type != BLE_UUID_TYPE_16) { if (rsp.batp_length != 0) { break; } @@ -129,8 +127,8 @@ ble_gatt_find_s_test_misc_rx_read_type( htole16(buf + off, entries[i].end_handle); off += 2; - if (uuid16 != 0) { - htole16(buf + off, uuid16); + if (entries[i].uuid->type == BLE_UUID_TYPE_16) { + htole16(buf + off, ble_uuid_u16(entries[i].uuid)); off += 2; } } @@ -151,13 +149,15 @@ ble_gatt_find_s_test_misc_rx_read_type( } static void -ble_gatt_find_s_test_misc_rx_read(uint16_t conn_handle, uint8_t *uuid128) +ble_gatt_find_s_test_misc_rx_read(uint16_t conn_handle, const ble_uuid_t *uuid) { uint8_t buf[17]; int rc; + TEST_ASSERT(uuid->type == BLE_UUID_TYPE_128); + buf[0] = BLE_ATT_OP_READ_RSP; - memcpy(buf + 1, uuid128, 16); + ble_uuid_flat(uuid, buf + 1); rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, buf, 17); @@ -229,12 +229,12 @@ ble_gatt_find_s_test_misc_find_inc(uint16_t conn_handle, break; } - if (ble_uuid_128_to_16(entries[idx].uuid128) == 0) { + if (entries[idx].uuid->type == BLE_UUID_TYPE_128) { TEST_ASSERT(num_found == 1); ble_gatt_find_s_test_misc_verify_tx_read( entries[idx].start_handle); ble_gatt_find_s_test_misc_rx_read(conn_handle, - entries[idx].uuid128); + entries[idx].uuid); } idx += num_found; @@ -248,8 +248,8 @@ ble_gatt_find_s_test_misc_find_inc(uint16_t conn_handle, entries[i].start_handle); TEST_ASSERT(ble_gatt_find_s_test_svcs[i].end_handle == entries[i].end_handle); - TEST_ASSERT(memcmp(ble_gatt_find_s_test_svcs[i].uuid128, - entries[i].uuid128, 16) == 0); + TEST_ASSERT(ble_uuid_cmp(&ble_gatt_find_s_test_svcs[i].uuid.u, + entries[i].uuid) == 0); } } @@ -264,12 +264,12 @@ TEST_CASE(ble_gatt_find_s_test_1) .inc_handle = 6, .start_handle = 35, .end_handle = 49, - .uuid128 = BLE_UUID16_ARR(0x5155), + .uuid = BLE_UUID16_DECLARE(0x5155), }, { .inc_handle = 9, .start_handle = 543, .end_handle = 870, - .uuid128 = BLE_UUID16_ARR(0x1122), + .uuid = BLE_UUID16_DECLARE(0x1122), }, { 0, } }) @@ -284,7 +284,7 @@ TEST_CASE(ble_gatt_find_s_test_1) .inc_handle = 36, .start_handle = 403, .end_handle = 859, - .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { 0, } }) @@ -299,12 +299,12 @@ TEST_CASE(ble_gatt_find_s_test_1) .inc_handle = 36, .start_handle = 403, .end_handle = 859, - .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { .inc_handle = 39, .start_handle = 900, .end_handle = 932, - .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 }, + .uuid = BLE_UUID128_DECLARE(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17), }, { 0, } }) @@ -319,27 +319,27 @@ TEST_CASE(ble_gatt_find_s_test_1) .inc_handle = 36, .start_handle = 403, .end_handle = 859, - .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { .inc_handle = 37, .start_handle = 35, .end_handle = 49, - .uuid128 = BLE_UUID16_ARR(0x5155), + .uuid = BLE_UUID16_DECLARE(0x5155), }, { .inc_handle = 38, .start_handle = 543, .end_handle = 870, - .uuid128 = BLE_UUID16_ARR(0x1122), + .uuid = BLE_UUID16_DECLARE(0x1122), }, { .inc_handle = 39, .start_handle = 900, .end_handle = 932, - .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 }, + .uuid = BLE_UUID128_DECLARE(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17), }, { .inc_handle = 40, .start_handle = 940, .end_handle = 950, - .uuid128 = { 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 }, + .uuid = BLE_UUID128_DECLARE(3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18), }, { 0, } }) @@ -354,13 +354,13 @@ TEST_CASE(ble_gatt_find_s_test_oom) .inc_handle = 21, .start_handle = 800, .end_handle = 899, - .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, + .uuid = BLE_UUID128_DECLARE(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), }, { .inc_handle = 22, .start_handle = 900, .end_handle = 999, - .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, + .uuid = BLE_UUID128_DECLARE(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), }, { 0 } }; @@ -406,7 +406,7 @@ TEST_CASE(ble_gatt_find_s_test_oom) * follow-up request, so there is always an mbuf available. */ /* XXX: Find a way to test this. */ - ble_gatt_find_s_test_misc_rx_read(1, incs[0].uuid128); + ble_gatt_find_s_test_misc_rx_read(1, incs[0].uuid); ble_hs_test_util_tx_all(); /* Exhaust the msys pool. Leave one mbuf for the forthcoming response. */ @@ -418,7 +418,7 @@ TEST_CASE(ble_gatt_find_s_test_oom) os_time_advance(ticks_until); ble_gattc_timer(); - ble_gatt_find_s_test_misc_rx_read(1, incs[1].uuid128); + ble_gatt_find_s_test_misc_rx_read(1, incs[1].uuid); ble_hs_test_util_tx_all(); ble_hs_test_util_rx_att_err_rsp(1, diff --git a/nimble/host/test/src/ble_gatt_read_test.c b/nimble/host/test/src/ble_gatt_read_test.c index 6ffdc43c0..54c8a562e 100644 --- a/nimble/host/test/src/ble_gatt_read_test.c +++ b/nimble/host/test/src/ble_gatt_read_test.c @@ -285,7 +285,7 @@ ble_gatt_read_test_misc_verify_bad(uint8_t att_status, static void ble_gatt_read_test_misc_uuid_verify_good( - uint16_t start_handle, uint16_t end_handle, void *uuid128, + uint16_t start_handle, uint16_t end_handle, const ble_uuid_t *uuid, int stop_after, struct ble_hs_test_util_flat_attr *attrs) { int num_read; @@ -297,7 +297,7 @@ ble_gatt_read_test_misc_uuid_verify_good( ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}), NULL, NULL); - rc = ble_gattc_read_by_uuid(2, start_handle, end_handle, uuid128, + rc = ble_gattc_read_by_uuid(2, start_handle, end_handle, uuid, ble_gatt_read_test_cb, &stop_after); TEST_ASSERT_FATAL(rc == 0); @@ -540,7 +540,7 @@ TEST_CASE(ble_gatt_read_test_by_handle) TEST_CASE(ble_gatt_read_test_by_uuid) { /* Read a single seven-byte attribute. */ - ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0, + ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16_DECLARE(0x1234), 0, (struct ble_hs_test_util_flat_attr[]) { { .handle = 43, .value = { 1,2,3,4,5,6,7 }, @@ -550,7 +550,7 @@ TEST_CASE(ble_gatt_read_test_by_uuid) } }); /* Read two seven-byte attributes; one response. */ - ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0, + ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16_DECLARE(0x1234), 0, (struct ble_hs_test_util_flat_attr[]) { { .handle = 43, .value = { 1,2,3,4,5,6,7 }, @@ -564,7 +564,7 @@ TEST_CASE(ble_gatt_read_test_by_uuid) } }); /* Read two attributes; two responses. */ - ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0, + ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16_DECLARE(0x1234), 0, (struct ble_hs_test_util_flat_attr[]) { { .handle = 43, .value = { 1,2,3,4,5,6,7 }, @@ -578,7 +578,7 @@ TEST_CASE(ble_gatt_read_test_by_uuid) } }); /* Stop after three reads. */ - ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 3, + ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16_DECLARE(0x1234), 3, (struct ble_hs_test_util_flat_attr[]) { { .handle = 43, .value = { 1,2,3,4,5,6,7 }, diff --git a/nimble/host/test/src/ble_gatts_notify_test.c b/nimble/host/test/src/ble_gatts_notify_test.c index a1f14699b..21f1afb02 100644 --- a/nimble/host/test/src/ble_gatts_notify_test.c +++ b/nimble/host/test/src/ble_gatts_notify_test.c @@ -43,14 +43,14 @@ ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, static const struct ble_gatt_svc_def ble_gatts_notify_test_svcs[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_1_UUID), + .uuid = BLE_UUID16_DECLARE(BLE_GATTS_NOTIFY_TEST_CHR_1_UUID), .access_cb = ble_gatts_notify_test_misc_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE, }, { - .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_2_UUID), + .uuid = BLE_UUID16_DECLARE(BLE_GATTS_NOTIFY_TEST_CHR_2_UUID), .access_cb = ble_gatts_notify_test_misc_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE, @@ -369,7 +369,7 @@ ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, uint16_t uuid16; if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) { - uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128); + uuid16 = ble_uuid_u16(ctxt->chr.chr_def->uuid); switch (uuid16) { case BLE_GATTS_NOTIFY_TEST_CHR_1_UUID: ble_gatts_notify_test_chr_1_def_handle = ctxt->chr.def_handle; @@ -979,19 +979,19 @@ TEST_CASE(ble_gatts_notify_test_disallowed) const struct ble_gatt_svc_def svcs[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(1), + .uuid = BLE_UUID16_DECLARE(1), .access_cb = ble_gatts_notify_test_misc_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, .val_handle = &chr1_val_handle, }, { - .uuid128 = BLE_UUID16(2), + .uuid = BLE_UUID16_DECLARE(2), .access_cb = ble_gatts_notify_test_misc_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE, .val_handle = &chr2_val_handle, }, { - .uuid128 = BLE_UUID16(3), + .uuid = BLE_UUID16_DECLARE(3), .access_cb = ble_gatts_notify_test_misc_access, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE, diff --git a/nimble/host/test/src/ble_gatts_read_test.c b/nimble/host/test/src/ble_gatts_read_test.c index 9f321c65b..bb5db1eaf 100644 --- a/nimble/host/test/src/ble_gatts_read_test.c +++ b/nimble/host/test/src/ble_gatts_read_test.c @@ -46,13 +46,13 @@ ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, static const struct ble_gatt_svc_def ble_gatts_read_test_svcs[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_1_UUID), + .uuid = BLE_UUID16_DECLARE(BLE_GATTS_READ_TEST_CHR_1_UUID), .access_cb = ble_gatts_read_test_util_access_1, .flags = BLE_GATT_CHR_F_READ }, { - .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_2_UUID), + .uuid = BLE_UUID16_DECLARE(BLE_GATTS_READ_TEST_CHR_2_UUID), .access_cb = ble_gatts_read_test_util_access_2, .flags = BLE_GATT_CHR_F_READ }, { @@ -103,7 +103,7 @@ ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, uint16_t uuid16; if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) { - uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128); + uuid16 = ble_uuid_u16(ctxt->chr.chr_def->uuid); switch (uuid16) { case BLE_GATTS_READ_TEST_CHR_1_UUID: ble_gatts_read_test_chr_1_def_handle = ctxt->chr.def_handle; diff --git a/nimble/host/test/src/ble_gatts_reg_test.c b/nimble/host/test/src/ble_gatts_reg_test.c index 88a9b6cf9..1486690a7 100644 --- a/nimble/host/test/src/ble_gatts_reg_test.c +++ b/nimble/host/test/src/ble_gatts_reg_test.c @@ -29,7 +29,7 @@ struct ble_gatts_reg_test_entry { uint8_t op; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; uint16_t handle; uint16_t val_handle; /* If a characteristic. */ @@ -64,13 +64,13 @@ ble_gatts_reg_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) entry->op = ctxt->op; switch (ctxt->op) { case BLE_GATT_REGISTER_OP_SVC: - memcpy(entry->uuid128, ctxt->svc.svc_def->uuid128, 16); + ble_uuid_to_any(ctxt->svc.svc_def->uuid, &entry->uuid); entry->handle = ctxt->svc.handle; entry->svc = ctxt->svc.svc_def; break; case BLE_GATT_REGISTER_OP_CHR: - memcpy(entry->uuid128, ctxt->chr.chr_def->uuid128, 16); + ble_uuid_to_any(ctxt->chr.chr_def->uuid, &entry->uuid); entry->handle = ctxt->chr.def_handle; entry->val_handle = ctxt->chr.val_handle; entry->svc = ctxt->chr.svc_def; @@ -78,7 +78,7 @@ ble_gatts_reg_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) break; case BLE_GATT_REGISTER_OP_DSC: - memcpy(entry->uuid128, ctxt->dsc.dsc_def->uuid128, 16); + ble_uuid_to_any(ctxt->dsc.dsc_def->uuid, &entry->uuid); entry->handle = ctxt->dsc.handle; entry->svc = ctxt->dsc.svc_def; entry->chr = ctxt->dsc.chr_def; @@ -102,13 +102,13 @@ ble_gatts_reg_test_misc_lookup_good(struct ble_gatts_reg_test_entry *entry) switch (entry->op) { case BLE_GATT_REGISTER_OP_SVC: - rc = ble_gatts_find_svc(entry->uuid128, &svc_handle); + rc = ble_gatts_find_svc(&entry->uuid.u, &svc_handle); TEST_ASSERT_FATAL(rc == 0); TEST_ASSERT(svc_handle == entry->handle); break; case BLE_GATT_REGISTER_OP_CHR: - rc = ble_gatts_find_chr(entry->svc->uuid128, entry->chr->uuid128, + rc = ble_gatts_find_chr(entry->svc->uuid, entry->chr->uuid, &chr_def_handle, &chr_val_handle); TEST_ASSERT_FATAL(rc == 0); TEST_ASSERT(chr_def_handle == entry->handle); @@ -116,8 +116,8 @@ ble_gatts_reg_test_misc_lookup_good(struct ble_gatts_reg_test_entry *entry) break; case BLE_GATT_REGISTER_OP_DSC: - rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128, - entry->dsc->uuid128, &dsc_handle); + rc = ble_gatts_find_dsc(entry->svc->uuid, entry->chr->uuid, + entry->dsc->uuid, &dsc_handle); break; default: @@ -130,30 +130,30 @@ static void ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) { struct ble_gatts_reg_test_entry *cur; - uint8_t wrong_uuid[16]; + ble_uuid_any_t wrong_uuid; int rc; int i; switch (entry->op) { case BLE_GATT_REGISTER_OP_SVC: /* Wrong service UUID. */ - memcpy(wrong_uuid, entry->svc->uuid128, 16); - wrong_uuid[15]++; - rc = ble_gatts_find_svc(wrong_uuid, NULL); + ble_uuid_to_any(entry->svc->uuid, &wrong_uuid); + wrong_uuid.u16.value++; + rc = ble_gatts_find_svc(&wrong_uuid.u, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); break; case BLE_GATT_REGISTER_OP_CHR: /* Correct service UUID, wrong characteristic UUID. */ - memcpy(wrong_uuid, entry->chr->uuid128, 16); - wrong_uuid[15]++; - rc = ble_gatts_find_chr(entry->svc->uuid128, wrong_uuid, NULL, NULL); + ble_uuid_to_any(entry->chr->uuid, &wrong_uuid); + wrong_uuid.u16.value++; + rc = ble_gatts_find_chr(entry->svc->uuid, &wrong_uuid.u, NULL, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); /* Incorrect service UUID, correct characteristic UUID. */ - memcpy(wrong_uuid, entry->svc->uuid128, 16); - wrong_uuid[15]++; - rc = ble_gatts_find_chr(wrong_uuid, entry->chr->uuid128, NULL, NULL); + ble_uuid_to_any(entry->svc->uuid, &wrong_uuid); + wrong_uuid.u16.value++; + rc = ble_gatts_find_chr(&wrong_uuid.u, entry->chr->uuid, NULL, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); /* Existing (but wrong) service, correct characteristic UUID. */ @@ -162,8 +162,8 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) switch (cur->op) { case BLE_GATT_REGISTER_OP_SVC: if (cur->svc != entry->svc) { - rc = ble_gatts_find_chr(cur->svc->uuid128, - entry->chr->uuid128, + rc = ble_gatts_find_chr(cur->svc->uuid, + entry->chr->uuid, NULL, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); } @@ -172,8 +172,8 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_CHR: /* Characteristic that isn't in this service. */ if (cur->svc != entry->svc) { - rc = ble_gatts_find_chr(entry->svc->uuid128, - cur->chr->uuid128, + rc = ble_gatts_find_chr(entry->svc->uuid, + cur->chr->uuid, NULL, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); } @@ -181,8 +181,8 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_DSC: /* Use descriptor UUID instead of characteristic UUID. */ - rc = ble_gatts_find_chr(entry->svc->uuid128, - cur->dsc->uuid128, + rc = ble_gatts_find_chr(entry->svc->uuid, + cur->dsc->uuid, NULL, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); break; @@ -196,17 +196,17 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_DSC: /* Correct svc/chr UUID, wrong dsc UUID. */ - memcpy(wrong_uuid, entry->dsc->uuid128, 16); - wrong_uuid[15]++; - rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128, - wrong_uuid, NULL); + ble_uuid_to_any(entry->dsc->uuid, &wrong_uuid); + wrong_uuid.u128.value[15]++; + rc = ble_gatts_find_dsc(entry->svc->uuid, entry->chr->uuid, + &wrong_uuid.u, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); /* Incorrect svc UUID, correct chr/dsc UUID. */ - memcpy(wrong_uuid, entry->svc->uuid128, 16); - wrong_uuid[15]++; - rc = ble_gatts_find_dsc(wrong_uuid, entry->chr->uuid128, - entry->dsc->uuid128, NULL); + ble_uuid_to_any(entry->svc->uuid, &wrong_uuid); + wrong_uuid.u128.value[15]++; + rc = ble_gatts_find_dsc(&wrong_uuid.u, entry->chr->uuid, + entry->dsc->uuid, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); for (i = 0; i < ble_gatts_reg_test_num_entries; i++) { @@ -215,9 +215,9 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_SVC: /* Existing (but wrong) svc, correct chr/dsc UUID. */ if (cur->svc != entry->svc) { - rc = ble_gatts_find_dsc(cur->svc->uuid128, - entry->chr->uuid128, - entry->dsc->uuid128, + rc = ble_gatts_find_dsc(cur->svc->uuid, + entry->chr->uuid, + entry->dsc->uuid, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); } @@ -226,9 +226,9 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_CHR: /* Existing (but wrong) svc/chr, correct dsc UUID. */ if (cur->chr != entry->chr) { - rc = ble_gatts_find_dsc(cur->svc->uuid128, - cur->chr->uuid128, - entry->dsc->uuid128, + rc = ble_gatts_find_dsc(cur->svc->uuid, + cur->chr->uuid, + entry->dsc->uuid, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); } @@ -237,9 +237,9 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) case BLE_GATT_REGISTER_OP_DSC: /* Descriptor that isn't in this characteristic. */ if (cur->chr != entry->chr) { - rc = ble_gatts_find_dsc(cur->svc->uuid128, - cur->chr->uuid128, - entry->dsc->uuid128, + rc = ble_gatts_find_dsc(cur->svc->uuid, + cur->chr->uuid, + entry->dsc->uuid, NULL); TEST_ASSERT(rc == BLE_HS_ENOENT); } @@ -259,14 +259,14 @@ ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry) } static void -ble_gatts_reg_test_misc_verify_entry(uint8_t op, const uint8_t *uuid128) +ble_gatts_reg_test_misc_verify_entry(uint8_t op, const ble_uuid_t *uuid) { struct ble_gatts_reg_test_entry *entry; int i; for (i = 0; i < ble_gatts_reg_test_num_entries; i++) { entry = ble_gatts_reg_test_entries + i; - if (entry->op == op && memcmp(entry->uuid128, uuid128, 16) == 0) { + if (entry->op == op && ble_uuid_cmp(&entry->uuid.u, uuid) == 0) { break; } } @@ -316,11 +316,11 @@ TEST_CASE(ble_gatts_reg_test_svc_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_circ[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 1, NULL }, }, { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 0, NULL }, }, { 0 @@ -333,11 +333,11 @@ TEST_CASE(ble_gatts_reg_test_svc_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_good[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .includes = (const struct ble_gatt_svc_def*[]) { svcs_good + 1, NULL }, }, { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), }, { 0 } }; @@ -354,9 +354,9 @@ TEST_CASE(ble_gatts_reg_test_chr_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_no_chr_cb[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .flags = BLE_GATT_CHR_F_READ, }, { 0 @@ -372,9 +372,9 @@ TEST_CASE(ble_gatts_reg_test_chr_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_good[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, }, { @@ -396,13 +396,13 @@ TEST_CASE(ble_gatts_reg_test_dsc_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_no_dsc_cb[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x8888), + .uuid = BLE_UUID16_DECLARE(0x8888), .att_flags = 5, }, { 0 @@ -421,13 +421,13 @@ TEST_CASE(ble_gatts_reg_test_dsc_return) ble_gatts_reg_test_init(); struct ble_gatt_svc_def svcs_good[] = { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x8888), + .uuid = BLE_UUID16_DECLARE(0x8888), .access_cb = ble_gatts_reg_test_misc_dummy_access, .att_flags = 5, }, { @@ -462,17 +462,17 @@ ble_gatts_reg_test_misc_svcs(struct ble_gatt_svc_def *svcs) /* Verify that the appropriate callbacks were executed. */ for (svc = svcs; svc->type != BLE_GATT_SVC_TYPE_END; svc++) { ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_SVC, - svc->uuid128); + svc->uuid); if (svc->characteristics != NULL) { - for (chr = svc->characteristics; chr->uuid128 != NULL; chr++) { + for (chr = svc->characteristics; chr->uuid != NULL; chr++) { ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_CHR, - chr->uuid128); + chr->uuid); if (chr->descriptors != NULL) { - for (dsc = chr->descriptors; dsc->uuid128 != NULL; dsc++) { + for (dsc = chr->descriptors; dsc->uuid != NULL; dsc++) { ble_gatts_reg_test_misc_verify_entry( - BLE_GATT_REGISTER_OP_DSC, dsc->uuid128); + BLE_GATT_REGISTER_OP_DSC, dsc->uuid); } } } @@ -485,7 +485,7 @@ TEST_CASE(ble_gatts_reg_test_svc_cb) /*** 1 primary. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), }, { 0 } }); @@ -493,13 +493,13 @@ TEST_CASE(ble_gatts_reg_test_svc_cb) /*** 3 primary. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), }, { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x2234), + .uuid = BLE_UUID16_DECLARE(0x2234), }, { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x3234), + .uuid = BLE_UUID16_DECLARE(0x3234), }, { 0 } }); @@ -508,10 +508,10 @@ TEST_CASE(ble_gatts_reg_test_svc_cb) /*** 1 primary, 1 secondary. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), }, { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x2222), + .uuid = BLE_UUID16_DECLARE(0x2222), }, { 0 } }); @@ -520,12 +520,12 @@ TEST_CASE(ble_gatts_reg_test_svc_cb) struct ble_gatt_svc_def svcs[] = { [0] = { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .includes = (const struct ble_gatt_svc_def*[]) { svcs + 1, NULL, }, }, [1] = { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x2222), + .uuid = BLE_UUID16_DECLARE(0x2222), }, { 0 } @@ -540,9 +540,9 @@ TEST_CASE(ble_gatts_reg_test_chr_cb) /*** 1 characteristic. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 0, @@ -556,14 +556,14 @@ TEST_CASE(ble_gatts_reg_test_chr_cb) /*** 3 characteristics. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 0, }, { - .uuid128 = BLE_UUID16(0x2222), + .uuid = BLE_UUID16_DECLARE(0x2222), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_WRITE, .val_handle = val_handles + 1, @@ -572,9 +572,9 @@ TEST_CASE(ble_gatts_reg_test_chr_cb) } }, }, { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x5678), + .uuid = BLE_UUID16_DECLARE(0x5678), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x3333), + .uuid = BLE_UUID16_DECLARE(0x3333), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 2, @@ -593,14 +593,14 @@ TEST_CASE(ble_gatts_reg_test_dsc_cb) /*** 1 descriptor. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 0, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x111a), + .uuid = BLE_UUID16_DECLARE(0x111a), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { @@ -616,21 +616,21 @@ TEST_CASE(ble_gatts_reg_test_dsc_cb) /*** 5+ descriptors. */ ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { { .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid128 = BLE_UUID16(0x1234), + .uuid = BLE_UUID16_DECLARE(0x1234), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x1111), + .uuid = BLE_UUID16_DECLARE(0x1111), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 0, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x111a), + .uuid = BLE_UUID16_DECLARE(0x111a), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { 0 } }, }, { - .uuid128 = BLE_UUID16(0x2222), + .uuid = BLE_UUID16_DECLARE(0x2222), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_WRITE, .val_handle = val_handles + 1, @@ -639,50 +639,50 @@ TEST_CASE(ble_gatts_reg_test_dsc_cb) } }, }, { .type = BLE_GATT_SVC_TYPE_SECONDARY, - .uuid128 = BLE_UUID16(0x5678), + .uuid = BLE_UUID16_DECLARE(0x5678), .characteristics = (struct ble_gatt_chr_def[]) { { - .uuid128 = BLE_UUID16(0x3333), + .uuid = BLE_UUID16_DECLARE(0x3333), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 2, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x333a), + .uuid = BLE_UUID16_DECLARE(0x333a), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x333b), + .uuid = BLE_UUID16_DECLARE(0x333b), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x333c), + .uuid = BLE_UUID16_DECLARE(0x333c), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x333e), + .uuid = BLE_UUID16_DECLARE(0x333e), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { 0 } }, }, { - .uuid128 = BLE_UUID16(0x4444), + .uuid = BLE_UUID16_DECLARE(0x4444), .access_cb = ble_gatts_reg_test_misc_dummy_access, .flags = BLE_GATT_CHR_F_READ, .val_handle = val_handles + 3, .descriptors = (struct ble_gatt_dsc_def[]) { { - .uuid128 = BLE_UUID16(0x444a), + .uuid = BLE_UUID16_DECLARE(0x444a), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x444b), + .uuid = BLE_UUID16_DECLARE(0x444b), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x444c), + .uuid = BLE_UUID16_DECLARE(0x444c), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { - .uuid128 = BLE_UUID16(0x444e), + .uuid = BLE_UUID16_DECLARE(0x444e), .att_flags = 5, .access_cb = ble_gatts_reg_test_misc_dummy_access, }, { diff --git a/nimble/host/test/src/ble_hs_test_util.c b/nimble/host/test/src/ble_hs_test_util.c index 1470fc61b..a4a57f871 100644 --- a/nimble/host/test/src/ble_hs_test_util.c +++ b/nimble/host/test/src/ble_hs_test_util.c @@ -991,11 +991,10 @@ int ble_hs_test_util_rx_att_read_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, - const void *uuid128) + const ble_uuid_t *uuid) { struct ble_att_read_type_req req; uint8_t buf[BLE_ATT_READ_TYPE_REQ_SZ_128]; - uint16_t uuid16; int req_len; int rc; @@ -1004,14 +1003,8 @@ ble_hs_test_util_rx_att_read_type_req(uint16_t conn_handle, ble_att_read_type_req_write(buf, sizeof buf, &req); - uuid16 = ble_uuid_128_to_16(uuid128); - if (uuid16 != 0) { - htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ, uuid16); - req_len = BLE_ATT_READ_TYPE_REQ_BASE_SZ + 2; - } else { - memcpy(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ, uuid128, 16); - req_len = BLE_ATT_READ_TYPE_REQ_BASE_SZ + 16; - } + ble_uuid_flat(uuid, buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ); + req_len = BLE_ATT_READ_TYPE_REQ_BASE_SZ + ble_uuid_length(uuid); rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, buf, req_len); @@ -1024,14 +1017,11 @@ ble_hs_test_util_rx_att_read_type_req16(uint16_t conn_handle, uint16_t end_handle, uint16_t uuid16) { - uint8_t uuid128[16]; int rc; - rc = ble_uuid_16_to_128(uuid16, uuid128); - TEST_ASSERT_FATAL(rc == 0); - rc = ble_hs_test_util_rx_att_read_type_req(conn_handle, start_handle, - end_handle, uuid128); + end_handle, + BLE_UUID16_DECLARE(uuid16)); return rc; } @@ -1095,25 +1085,18 @@ int ble_hs_test_util_rx_att_read_group_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, - const void *uuid128) + const ble_uuid_t *uuid) { struct ble_att_read_group_type_req req; uint8_t buf[BLE_ATT_READ_GROUP_TYPE_REQ_SZ_128]; - uint16_t uuid16; int req_len; int rc; req.bagq_start_handle = start_handle; req.bagq_end_handle = end_handle; - uuid16 = ble_uuid_128_to_16(uuid128); - if (uuid16 != 0) { - htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, uuid16); - req_len = BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ + 2; - } else { - memcpy(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, uuid128, 16); - req_len = BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ + 16; - } + ble_uuid_flat(uuid, buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ); + req_len = BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ + ble_uuid_length(uuid); ble_att_read_group_type_req_write(buf, sizeof buf, &req); rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT, @@ -1127,14 +1110,11 @@ ble_hs_test_util_rx_att_read_group_type_req16(uint16_t conn_handle, uint16_t end_handle, uint16_t uuid16) { - uint8_t uuid128[16]; int rc; - rc = ble_uuid_16_to_128(uuid16, uuid128); - TEST_ASSERT_FATAL(rc == 0); - rc = ble_hs_test_util_rx_att_read_group_type_req(conn_handle, start_handle, - end_handle, uuid128); + end_handle, + BLE_UUID16_DECLARE(uuid16)); return rc; } @@ -1538,9 +1518,8 @@ ble_hs_test_util_verify_tx_find_info_rsp( struct ble_att_find_info_rsp rsp; struct os_mbuf *om; uint16_t handle; - uint16_t uuid16; uint8_t buf[BLE_ATT_FIND_INFO_RSP_BASE_SZ]; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; int off; int rc; @@ -1564,24 +1543,23 @@ ble_hs_test_util_verify_tx_find_info_rsp( handle = le16toh((void *)&handle); TEST_ASSERT(handle == entry->handle); - if (entry->uuid16 != 0) { + if (entry->uuid->type == BLE_UUID_TYPE_16) { TEST_ASSERT(rsp.bafp_format == BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT); - rc = os_mbuf_copydata(om, off, 2, &uuid16); + + ble_uuid_init_from_mbuf(&uuid, om, off, 2); TEST_ASSERT(rc == 0); off += 2; - - uuid16 = le16toh((void *)&uuid16); - TEST_ASSERT(uuid16 == entry->uuid16); } else { TEST_ASSERT(rsp.bafp_format == BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT); - rc = os_mbuf_copydata(om, off, 16, uuid128); + + rc = ble_uuid_init_from_mbuf(&uuid, om, off, 16); TEST_ASSERT(rc == 0); off += 16; - - TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0); } + + TEST_ASSERT(ble_uuid_cmp(entry->uuid, &uuid.u) == 0); } /* Ensure there is no extra data in the response. */ @@ -1596,7 +1574,7 @@ ble_hs_test_util_verify_tx_read_group_type_rsp( struct ble_att_read_group_type_rsp rsp; struct os_mbuf *om; uint16_t u16; - uint8_t uuid128[16]; + ble_uuid_any_t uuid; int off; int rc; @@ -1608,7 +1586,7 @@ ble_hs_test_util_verify_tx_read_group_type_rsp( off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ; for (entry = entries; entry->start_handle != 0; entry++) { - if (entry->uuid16 != 0) { + if (entry->uuid->type == BLE_UUID_TYPE_16) { TEST_ASSERT(rsp.bagp_length == BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16); } else { @@ -1628,18 +1606,16 @@ ble_hs_test_util_verify_tx_read_group_type_rsp( TEST_ASSERT(u16 == entry->end_handle); off += 2; - if (entry->uuid16 != 0) { - rc = os_mbuf_copydata(om, off, 2, &u16); + if (entry->uuid->type == BLE_UUID_TYPE_16) { + rc = ble_uuid_init_from_mbuf(&uuid, om, off, 2); TEST_ASSERT(rc == 0); - htole16(&u16, u16); - TEST_ASSERT(u16 == entry->uuid16); - off += 2; } else { - rc = os_mbuf_copydata(om, off, 16, uuid128); + rc = ble_uuid_init_from_mbuf(&uuid, om, off, 16); TEST_ASSERT(rc == 0); - TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0); - off += 16; } + + TEST_ASSERT(ble_uuid_cmp(&uuid.u, entry->uuid) == 0); + off += ble_uuid_length(&uuid.u); } /* Ensure there is no extra data in the response. */ diff --git a/nimble/host/test/src/ble_hs_test_util.h b/nimble/host/test/src/ble_hs_test_util.h index 75865e24a..8f3d94e96 100644 --- a/nimble/host/test/src/ble_hs_test_util.h +++ b/nimble/host/test/src/ble_hs_test_util.h @@ -55,15 +55,13 @@ struct ble_hs_test_util_mbuf_params { struct ble_hs_test_util_att_info_entry { uint16_t handle; /* 0 on last entry */ - uint16_t uuid16; /* 0 if not present. */ - uint8_t uuid128[16]; + const ble_uuid_t *uuid; }; struct ble_hs_test_util_att_group_type_entry { uint16_t start_handle; /* 0 on last entry */ uint16_t end_handle; /* 0 on last entry */ - uint16_t uuid16; /* 0 if not present. */ - uint8_t uuid128[16]; + const ble_uuid_t *uuid; }; #define BLE_HS_TEST_UTIL_L2CAP_HCI_HDR(handle, pb, len) \ @@ -167,7 +165,7 @@ int ble_hs_test_util_rx_att_find_type_value_req(uint16_t conn_handle, int ble_hs_test_util_rx_att_read_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, - const void *uuid128); + const ble_uuid_t *uuid); int ble_hs_test_util_rx_att_read_type_req16(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, @@ -183,7 +181,7 @@ int ble_hs_test_util_rx_att_read_mult_req(uint16_t conn_handle, int ble_hs_test_util_rx_att_read_group_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, - const void *uuid128); + const ble_uuid_t *uuid); int ble_hs_test_util_rx_att_read_group_type_req16(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, diff --git a/nimble/host/test/src/ble_uuid_test.c b/nimble/host/test/src/ble_uuid_test.c index fa2245ad8..ac7d6edfc 100644 --- a/nimble/host/test/src/ble_uuid_test.c +++ b/nimble/host/test/src/ble_uuid_test.c @@ -24,64 +24,55 @@ #include "host/ble_uuid.h" #include "ble_hs_test_util.h" -TEST_CASE(ble_uuid_test_128_to_16) +TEST_CASE(ble_uuid_test) { - uint16_t uuid16; + uint8_t buf_16[2] = { 0x00, 0x18 }; + uint8_t buf_128[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; - /*** RFCOMM */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 - })); - TEST_ASSERT(uuid16 == 0x0003); + const ble_uuid_t *uuid16_1 = BLE_UUID16_DECLARE(0x1800); + const ble_uuid_t *uuid16_2 = BLE_UUID16_DECLARE(0x1801); - /*** BNEP */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00 - })); - TEST_ASSERT(uuid16 == 0x000f); + const ble_uuid_t *uuid128_1 = + BLE_UUID128_DECLARE(0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF); + const ble_uuid_t *uuid128_2 = + BLE_UUID128_DECLARE(0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xEE); - /*** L2CAP */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 - })); - TEST_ASSERT(uuid16 == 0x0100); + ble_uuid_any_t uuid; + int rc; - /*** ObEXObjectPush */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x05, 0x11, 0x00, 0x00 - })); - TEST_ASSERT(uuid16 == 0x1105); + rc = ble_uuid_init_from_buf(&uuid, buf_16, 2); + TEST_ASSERT(rc == 0); - /*** Invalid base. */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9c, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 - })); - TEST_ASSERT(uuid16 == 0); + rc = ble_uuid_cmp(&uuid.u, uuid16_1); + TEST_ASSERT(rc == 0); - /*** Invalid prefix. */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01 - })); - TEST_ASSERT(uuid16 == 0); + rc = ble_uuid_cmp(&uuid.u, uuid16_2); + TEST_ASSERT(rc != 0); - /*** 16-bit UUID of 0. */ - uuid16 = ble_uuid_128_to_16(((uint8_t[]) { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - })); + rc = ble_uuid_cmp(uuid16_1, uuid16_2); + TEST_ASSERT(rc != 0); + + rc = ble_uuid_init_from_buf(&uuid, buf_128, 16); + TEST_ASSERT(rc == 0); + + rc = ble_uuid_cmp(&uuid.u, uuid128_1); + TEST_ASSERT(rc == 0); + + rc = ble_uuid_cmp(&uuid.u, uuid128_2); + TEST_ASSERT(rc != 0); + + rc = ble_uuid_cmp(uuid128_1, uuid128_2); + TEST_ASSERT(rc != 0); } TEST_SUITE(ble_uuid_test_suite) { tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL); - ble_uuid_test_128_to_16(); + ble_uuid_test(); } int