From d4871fe9a16e201890594cff0d6d3893c28423dc Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 27 Sep 2018 09:18:11 +0200 Subject: [PATCH] nimble/host: Cleanup handling UUIDs in ATT This patch adds private APIs to initialize UUIDs which should be used in ATT code - they guarantee that only 16-bit and 128-bit UUIDs are valid since public API function also allows 32-bit UUID which are not allowed in ATT. --- nimble/host/src/ble_att_clt.c | 4 ++-- nimble/host/src/ble_att_svr.c | 7 +++---- nimble/host/src/ble_gattc.c | 24 ++++++++++++++---------- nimble/host/src/ble_uuid.c | 24 +++++++++++++++++++++--- nimble/host/src/ble_uuid_priv.h | 6 +++++- nimble/host/test/src/ble_hs_test_util.c | 8 ++++---- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/nimble/host/src/ble_att_clt.c b/nimble/host/src/ble_att_clt.c index e1eae9f0f..b33f8375e 100644 --- a/nimble/host/src/ble_att_clt.c +++ b/nimble/host/src/ble_att_clt.c @@ -207,14 +207,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: - rc = ble_uuid_init_from_mbuf(&idata->uuid, *rxom, 2, 2); + rc = ble_uuid_init_from_att_mbuf(&idata->uuid, *rxom, 2, 2); if (rc != 0) { return BLE_HS_EBADDATA; } break; case BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT: - rc = ble_uuid_init_from_mbuf(&idata->uuid, *rxom, 2, 16); + rc = ble_uuid_init_from_att_mbuf(&idata->uuid, *rxom, 2, 16); if (rc != 0) { return BLE_HS_EBADDATA; } diff --git a/nimble/host/src/ble_att_svr.c b/nimble/host/src/ble_att_svr.c index f37c01cae..5db8fdbf2 100644 --- a/nimble/host/src/ble_att_svr.c +++ b/nimble/host/src/ble_att_svr.c @@ -1432,8 +1432,8 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom) goto done; } - rc = ble_uuid_init_from_mbuf(&uuid, *rxom, sizeof(*req), - pktlen - sizeof(*req)); + rc = ble_uuid_init_from_att_mbuf(&uuid, *rxom, sizeof(*req), + pktlen - sizeof(*req)); if (rc != 0) { att_err = BLE_ATT_ERR_INVALID_PDU; rc = BLE_HS_EMSGSIZE; @@ -1930,8 +1930,7 @@ ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom) } om_uuid_len = OS_MBUF_PKTHDR(*rxom)->omp_len - sizeof(*req); - rc = ble_uuid_init_from_mbuf(&uuid, *rxom, sizeof(*req), - om_uuid_len); + rc = ble_uuid_init_from_att_mbuf(&uuid, *rxom, sizeof(*req), om_uuid_len); if (rc != 0) { att_err = BLE_ATT_ERR_INVALID_PDU; err_handle = start_handle; diff --git a/nimble/host/src/ble_gattc.c b/nimble/host/src/ble_gattc.c index 193e1178e..2a55b9d23 100644 --- a/nimble/host/src/ble_gattc.c +++ b/nimble/host/src/ble_gattc.c @@ -1441,7 +1441,8 @@ ble_gattc_disc_all_svcs_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case 2: case 16: - rc = ble_uuid_init_from_buf(&service.uuid, adata->value, adata->value_len); + rc = ble_uuid_init_from_att_buf(&service.uuid, adata->value, + adata->value_len); if (rc != 0) { rc = BLE_HS_EBADDATA; goto done; @@ -1887,7 +1888,7 @@ ble_gattc_find_inc_svcs_rx_read_rsp(struct ble_gattc_proc *proc, int status, ble_gattc_dbg_assert_proc_not_inserted(proc); - rc = ble_uuid_init_from_mbuf(&service.uuid, *om, 0, 16); + rc = ble_uuid_init_from_att_mbuf(&service.uuid, *om, 0, 16); os_mbuf_free_chain(*om); *om = NULL; @@ -1964,6 +1965,8 @@ ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc, proc->find_inc_svcs.prev_handle = adata->att_handle; + rc = 0; + switch (adata->value_len) { case BLE_GATTS_INC_SVC_LEN_NO_UUID: proc->find_inc_svcs.cur_start = get_le16(adata->value + 0); @@ -1974,16 +1977,17 @@ ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc, case BLE_GATTS_INC_SVC_LEN_UUID: service.start_handle = get_le16(adata->value + 0); service.end_handle = get_le16(adata->value + 2); - ble_uuid_init_from_buf(&service.uuid, adata->value + 4, 2); + rc = ble_uuid_init_from_att_buf(&service.uuid, adata->value + 4, 2); + if (rc != 0) { + rc = BLE_HS_EBADDATA; + } break; default: rc = BLE_HS_EBADDATA; - goto done; + break; } - rc = 0; - done: if (call_cb) { cbrc = ble_gattc_find_inc_svcs_cb(proc, 0, 0, &service); @@ -2195,8 +2199,8 @@ ble_gattc_disc_all_chrs_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case BLE_GATT_CHR_DECL_SZ_16: case BLE_GATT_CHR_DECL_SZ_128: - rc = ble_uuid_init_from_buf(&chr.uuid, adata->value + 3, - adata->value_len - 3); + rc = ble_uuid_init_from_att_buf(&chr.uuid, adata->value + 3, + adata->value_len - 3); if (rc != 0) { rc = BLE_HS_EBADDATA; goto done; @@ -2423,8 +2427,8 @@ ble_gattc_disc_chr_uuid_rx_adata(struct ble_gattc_proc *proc, switch (adata->value_len) { case BLE_GATT_CHR_DECL_SZ_16: case BLE_GATT_CHR_DECL_SZ_128: - rc = ble_uuid_init_from_buf(&chr.uuid, adata->value + 3, - adata->value_len - 3); + rc = ble_uuid_init_from_att_buf(&chr.uuid, adata->value + 3, + adata->value_len - 3); if (rc != 0) { rc = BLE_HS_EBADDATA; goto done; diff --git a/nimble/host/src/ble_uuid.c b/nimble/host/src/ble_uuid.c index 677f427fa..1405d169c 100644 --- a/nimble/host/src/ble_uuid.c +++ b/nimble/host/src/ble_uuid.c @@ -155,8 +155,8 @@ ble_uuid_u16(const ble_uuid_t *uuid) /* 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) +ble_uuid_init_from_att_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off, + int len) { uint8_t val[16]; int rc; @@ -166,7 +166,25 @@ ble_uuid_init_from_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off, return rc; } - rc = ble_uuid_init_from_buf(uuid, val, len); + rc = ble_uuid_init_from_att_buf(uuid, val, len); + + return rc; +} + +int +ble_uuid_init_from_att_buf(ble_uuid_any_t *uuid, const void *buf, size_t len) +{ + int rc = 0; + + if (len == 2) { + uuid->u.type = BLE_UUID_TYPE_16; + uuid->u16.value = get_le16(buf); + } else if (len == 16) { + uuid->u.type = BLE_UUID_TYPE_128; + memcpy(uuid->u128.value, buf, 16); + } else { + rc = BLE_HS_EINVAL; + } return rc; } diff --git a/nimble/host/src/ble_uuid_priv.h b/nimble/host/src/ble_uuid_priv.h index d33451613..3dbcc6b8e 100644 --- a/nimble/host/src/ble_uuid_priv.h +++ b/nimble/host/src/ble_uuid_priv.h @@ -28,7 +28,11 @@ extern "C" { struct os_mbuf; -int ble_uuid_init_from_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off, int len); +int ble_uuid_init_from_att_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, + int off, int len); +int ble_uuid_init_from_att_buf(ble_uuid_any_t *uuid, const void *buf, + size_t 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); diff --git a/nimble/host/test/src/ble_hs_test_util.c b/nimble/host/test/src/ble_hs_test_util.c index cf16a8268..afd7a7d41 100644 --- a/nimble/host/test/src/ble_hs_test_util.c +++ b/nimble/host/test/src/ble_hs_test_util.c @@ -1269,14 +1269,14 @@ ble_hs_test_util_verify_tx_find_info_rsp( TEST_ASSERT(rsp.bafp_format == BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT); - ble_uuid_init_from_mbuf(&uuid, om, off, 2); + ble_uuid_init_from_att_mbuf(&uuid, om, off, 2); TEST_ASSERT(rc == 0); off += 2; } else { TEST_ASSERT(rsp.bafp_format == BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT); - rc = ble_uuid_init_from_mbuf(&uuid, om, off, 16); + rc = ble_uuid_init_from_att_mbuf(&uuid, om, off, 16); TEST_ASSERT(rc == 0); off += 16; } @@ -1328,10 +1328,10 @@ ble_hs_test_util_verify_tx_read_group_type_rsp( off += 2; if (entry->uuid->type == BLE_UUID_TYPE_16) { - rc = ble_uuid_init_from_mbuf(&uuid, om, off, 2); + rc = ble_uuid_init_from_att_mbuf(&uuid, om, off, 2); TEST_ASSERT(rc == 0); } else { - rc = ble_uuid_init_from_mbuf(&uuid, om, off, 16); + rc = ble_uuid_init_from_att_mbuf(&uuid, om, off, 16); TEST_ASSERT(rc == 0); }