You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/10/23 09:01:07 UTC

[GitHub] andrzej-kaczmarek closed pull request #198: nimble/host: Fixes for UUID handling

andrzej-kaczmarek closed pull request #198: nimble/host: Fixes for UUID handling
URL: https://github.com/apache/mynewt-nimble/pull/198
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/nimble/host/src/ble_att_clt.c b/nimble/host/src/ble_att_clt.c
index e1eae9f0..b33f8375 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 f37c01ca..5db8fdbf 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 193e1178..2a55b9d2 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 677f427f..16352cf6 100644
--- a/nimble/host/src/ble_uuid.c
+++ b/nimble/host/src/ble_uuid.c
@@ -33,21 +33,12 @@ static uint8_t ble_uuid_base[16] = {
 };
 
 #if MYNEWT_VAL(BLE_HS_DEBUG)
-static int verify_uuid(const ble_uuid_t *uuid)
-{
-    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;
-    }
-
-    return BLE_HS_EBADDATA;
-}
+#define VERIFY_UUID(uuid)                       \
+    assert((uuid->type == BLE_UUID_TYPE_16) ||  \
+           (uuid->type == BLE_UUID_TYPE_32) ||  \
+           (uuid->type == BLE_UUID_TYPE_128))
+#else
+#define VERIFY_UUID(uuid)
 #endif
 
 int
@@ -74,8 +65,8 @@ 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)
 {
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid1) == 0);
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid2) == 0);
+    VERIFY_UUID(uuid1);
+    VERIFY_UUID(uuid2);
 
     if (uuid1->type != uuid2->type) {
       return uuid1->type - uuid2->type;
@@ -92,12 +83,14 @@ ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2)
 
     BLE_HS_DBG_ASSERT(0);
 
-    return 0;
+    return -1;
 }
 
 void
 ble_uuid_copy(ble_uuid_any_t *dst, const ble_uuid_t *src)
 {
+    VERIFY_UUID(src);
+
     switch (src->type) {
     case BLE_UUID_TYPE_16:
         dst->u16 = *(const ble_uuid16_t *)src;
@@ -147,7 +140,7 @@ ble_uuid_to_str(const ble_uuid_t *uuid, char *dst)
 uint16_t
 ble_uuid_u16(const ble_uuid_t *uuid)
 {
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0);
+    VERIFY_UUID(uuid);
 
     return uuid->type == BLE_UUID_TYPE_16 ? BLE_UUID16(uuid)->value : 0;
 }
@@ -155,8 +148,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 +159,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;
 }
@@ -174,7 +185,7 @@ ble_uuid_init_from_mbuf(ble_uuid_any_t *uuid, struct os_mbuf *om, int off,
 int
 ble_uuid_to_any(const ble_uuid_t *uuid, ble_uuid_any_t *uuid_any)
 {
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0);
+    VERIFY_UUID(uuid);
 
     uuid_any->u.type = uuid->type;
 
@@ -203,7 +214,7 @@ ble_uuid_to_mbuf(const ble_uuid_t *uuid, struct os_mbuf *om)
     int len;
     void *buf;
 
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0);
+    VERIFY_UUID(uuid);
 
     len = ble_uuid_length(uuid);
 
@@ -220,7 +231,7 @@ ble_uuid_to_mbuf(const ble_uuid_t *uuid, struct os_mbuf *om)
 int
 ble_uuid_flat(const ble_uuid_t *uuid, void *dst)
 {
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0);
+    VERIFY_UUID(uuid);
 
     switch (uuid->type) {
     case BLE_UUID_TYPE_16:
@@ -243,7 +254,7 @@ ble_uuid_flat(const ble_uuid_t *uuid, void *dst)
 int
 ble_uuid_length(const ble_uuid_t *uuid)
 {
-    BLE_HS_DBG_ASSERT(verify_uuid(uuid) == 0);
+    VERIFY_UUID(uuid);
 
     return uuid->type >> 3;
 }
diff --git a/nimble/host/src/ble_uuid_priv.h b/nimble/host/src/ble_uuid_priv.h
index d3345161..3dbcc6b8 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 cf16a826..afd7a7d4 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);
         }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services