You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2015/12/05 03:09:16 UTC

[5/5] incubator-mynewt-larva git commit: Add tests for discover characteristics GATT proc

Add tests for discover characteristics GATT proc


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/b4241d69
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/b4241d69
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/b4241d69

Branch: refs/heads/master
Commit: b4241d69a5ad1f495893eb4fcbb1167ba81e0770
Parents: 090d9fb
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Dec 4 18:06:21 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Dec 4 18:08:25 2015 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_hs_test.h      |   1 +
 net/nimble/host/src/ble_att_cmd.h               |   2 +-
 net/nimble/host/src/ble_gatt.c                  |   2 +
 net/nimble/host/src/ble_hs.c                    |  14 +-
 net/nimble/host/src/test/ble_gatt_disc_c_test.c | 271 +++++++++++++++++++
 net/nimble/host/src/test/ble_gatt_disc_s_test.c |   9 +-
 net/nimble/host/src/test/ble_hs_test.c          |   1 +
 7 files changed, 288 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/include/host/ble_hs_test.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs_test.h b/net/nimble/host/include/host/ble_hs_test.h
index 03c81aa..f832c0e 100644
--- a/net/nimble/host/include/host/ble_hs_test.h
+++ b/net/nimble/host/include/host/ble_hs_test.h
@@ -29,5 +29,6 @@ int ble_hs_conn_test_all(void);
 int ble_gap_test_all(void);
 int ble_hs_uuid_test_all(void);
 int ble_gatt_disc_s_test_all(void);
+int ble_gatt_disc_c_test_all(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/ble_att_cmd.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_cmd.h b/net/nimble/host/src/ble_att_cmd.h
index b68713a..ea9c907 100644
--- a/net/nimble/host/src/ble_att_cmd.h
+++ b/net/nimble/host/src/ble_att_cmd.h
@@ -141,7 +141,7 @@ struct ble_att_read_type_rsp {
     uint8_t batp_length;
 };
 
-#define BLE_ATT_READ_TYPE_ADATA_BASE_SZ     4
+#define BLE_ATT_READ_TYPE_ADATA_BASE_SZ     2
 
 /**
  * | Parameter                          | Size (octets)     |

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/ble_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt.c b/net/nimble/host/src/ble_gatt.c
index cb45ad9..82e8577 100644
--- a/net/nimble/host/src/ble_gatt.c
+++ b/net/nimble/host/src/ble_gatt.c
@@ -746,6 +746,8 @@ ble_gatt_rx_read_type_adata(struct ble_hs_conn *conn,
         return;
     }
 
+    entry->disc_all_chars.prev_handle = adata->att_handle;
+
     attr.handle = adata->att_handle;
     attr.value_len = adata->value_len;
     attr.value = adata->value;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 8a9f1e5..ff31260 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 #include <assert.h>
 #include <errno.h>
 #include "util/tpq.h"
@@ -36,13 +35,14 @@
 #define BLE_HS_STACK_SIZE   (256)
 #endif
 
-struct os_task ble_hs_task;
-os_stack_t ble_hs_stack[BLE_HS_STACK_SIZE];
+static struct os_task ble_hs_task;
+static os_stack_t ble_hs_stack[BLE_HS_STACK_SIZE];
 
 #define HCI_CMD_BUFS        (8)
 #define HCI_CMD_BUF_SIZE    (260)       /* XXX: temporary, Fix later */
 struct os_mempool g_hci_cmd_pool;
-os_membuf_t g_hci_cmd_buf[OS_MEMPOOL_BYTES(HCI_CMD_BUFS, HCI_CMD_BUF_SIZE)];
+static os_membuf_t g_hci_cmd_buf[OS_MEMPOOL_BYTES(HCI_CMD_BUFS,
+                                                  HCI_CMD_BUF_SIZE)];
 
 /* XXX: this might be transport layer*/
 #define HCI_NUM_OS_EVENTS       (32)
@@ -60,11 +60,11 @@ os_membuf_t g_hci_cmd_buf[OS_MEMPOOL_BYTES(HCI_CMD_BUFS, HCI_CMD_BUF_SIZE)];
 #define BLE_HS_PKT_MAX              BLE_HS_NUM_MBUFS
 
 struct os_mempool g_hci_os_event_pool;
-os_membuf_t g_hci_os_event_buf[OS_MEMPOOL_BYTES(HCI_NUM_OS_EVENTS,
-                                                HCI_OS_EVENT_BUF_SIZE)];
+static os_membuf_t g_hci_os_event_buf[OS_MEMPOOL_BYTES(HCI_NUM_OS_EVENTS,
+                                                       HCI_OS_EVENT_BUF_SIZE)];
 
 static os_membuf_t *ble_hs_mbuf_mem;
-struct os_mempool ble_hs_mbuf_mempool;
+static struct os_mempool ble_hs_mbuf_mempool;
 struct os_mbuf_pool ble_hs_mbuf_pool;
 
 /* Host HCI Task Events */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/test/ble_gatt_disc_c_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_c_test.c b/net/nimble/host/src/test/ble_gatt_disc_c_test.c
new file mode 100644
index 0000000..31cb4a7
--- /dev/null
+++ b/net/nimble/host/src/test/ble_gatt_disc_c_test.c
@@ -0,0 +1,271 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_gatt.h"
+#include "ble_hs_uuid.h"
+#include "ble_att_cmd.h"
+#include "ble_hs_conn.h"
+#include "ble_hs_test_util.h"
+
+struct ble_gatt_disc_c_test_char {
+    uint16_t handle;
+    uint8_t *value;
+    int value_len;
+};
+
+#define BLE_GATT_DISC_C_TEST_MAX_CHARS  256
+static struct ble_gatt_attr
+    ble_gatt_disc_c_test_chars[BLE_GATT_DISC_C_TEST_MAX_CHARS];
+static int ble_gatt_disc_c_test_num_chars;
+static int ble_gatt_disc_c_test_rx_complete;
+
+static void
+ble_gatt_disc_c_test_init(void)
+{
+    int i;
+
+    ble_hs_test_util_init();
+
+    for (i = 0; i < ble_gatt_disc_c_test_num_chars; i++) {
+        free(ble_gatt_disc_c_test_chars[i].value);
+    }
+
+    ble_gatt_disc_c_test_num_chars = 0;
+    ble_gatt_disc_c_test_rx_complete = 0;
+}
+
+static void
+ble_gatt_disc_c_test_misc_rx_err_rsp(struct ble_hs_conn *conn, uint8_t req_op,
+                                     uint8_t error_code)
+{
+    struct ble_att_error_rsp rsp;
+    struct ble_l2cap_chan *chan;
+    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
+    int rc;
+
+    /* Send the pending ATT Request. */
+    ble_gatt_wakeup();
+
+    rsp.baep_req_op = req_op;
+    rsp.baep_handle = conn->bhc_handle;
+    rsp.baep_error_code = error_code;
+
+    rc = ble_att_error_rsp_write(buf, sizeof buf, &rsp);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    TEST_ASSERT_FATAL(chan != NULL);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn, chan, buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+}
+
+static int
+ble_gatt_disc_c_test_misc_rx_all_rsp_once(
+    struct ble_hs_conn *conn, struct ble_gatt_disc_c_test_char *chars)
+{
+    struct ble_att_read_type_rsp rsp;
+    struct ble_l2cap_chan *chan;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+    int i;
+
+    /* Send the pending ATT Read By Type Request. */
+    ble_gatt_wakeup();
+
+    rsp.batp_length = 2 + chars[0].value_len;
+    rc = ble_att_read_type_rsp_write(buf, BLE_ATT_READ_TYPE_RSP_BASE_SZ, &rsp);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
+    for (i = 0; ; i++) {
+        if (chars[i].handle == 0) {
+            /* No more services. */
+            break;
+        }
+
+        if (chars[i].value_len != rsp.batp_length - 2) {
+            /* Value length is changing; Need a separate response. */
+            break;
+        }
+
+        htole16(buf + off, chars[i].handle);
+        off += 2;
+
+        memcpy(buf + off, chars[i].value, chars[i].value_len);
+        off += chars[i].value_len;
+    }
+
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    TEST_ASSERT_FATAL(chan != NULL);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn, chan, buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_disc_c_test_misc_rx_all_rsp(struct ble_hs_conn *conn,
+                                     uint16_t end_handle,
+                                     struct ble_gatt_disc_c_test_char *chars)
+{
+    int count;
+    int idx;
+
+    idx = 0;
+    while (chars[idx].handle != 0) {
+        count = ble_gatt_disc_c_test_misc_rx_all_rsp_once(conn, chars + idx);
+        idx += count;
+    }
+
+    if (chars[idx - 1].handle != end_handle) {
+        ble_gatt_disc_c_test_misc_rx_err_rsp(conn,
+                                             BLE_ATT_OP_READ_TYPE_REQ,
+                                             BLE_ATT_ERR_ATTR_NOT_FOUND);
+    }
+}
+
+static void
+ble_gatt_disc_c_test_misc_verify_chars(struct ble_gatt_disc_c_test_char *chars)
+{
+    int i;
+
+    for (i = 0; chars[i].handle != 0; i++) {
+        TEST_ASSERT(chars[i].handle == ble_gatt_disc_c_test_chars[i].handle);
+        TEST_ASSERT(chars[i].value_len ==
+                    ble_gatt_disc_c_test_chars[i].value_len);
+        TEST_ASSERT(memcmp(chars[i].value,
+                           ble_gatt_disc_c_test_chars[i].value,
+                           chars[i].value_len) == 0);
+    }
+
+    TEST_ASSERT(i == ble_gatt_disc_c_test_num_chars);
+    TEST_ASSERT(ble_gatt_disc_c_test_rx_complete);
+}
+
+static int
+ble_gatt_disc_c_test_misc_cb(uint16_t conn_handle, int status,
+                             struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_attr *dst;
+
+    TEST_ASSERT(status == 0);
+    TEST_ASSERT(!ble_gatt_disc_c_test_rx_complete);
+
+    if (attr == NULL) {
+        ble_gatt_disc_c_test_rx_complete = 1;
+    } else {
+        TEST_ASSERT_FATAL(ble_gatt_disc_c_test_num_chars <
+                          BLE_GATT_DISC_C_TEST_MAX_CHARS);
+
+        dst = ble_gatt_disc_c_test_chars + ble_gatt_disc_c_test_num_chars++;
+
+        dst->handle = attr->handle;
+        dst->value_len = attr->value_len;
+        dst->value = malloc(dst->value_len);
+        TEST_ASSERT_FATAL(dst->value != NULL);
+        memcpy(dst->value, attr->value, dst->value_len);
+    }
+
+    return 0;
+}
+
+static void
+ble_gatt_disc_c_test_misc_all(uint16_t start_handle, uint16_t end_handle,
+                              struct ble_gatt_disc_c_test_char *chars)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_gatt_disc_c_test_init();
+
+    conn = ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}));
+
+    rc = ble_gatt_disc_all_chars(2, start_handle, end_handle,
+                                 ble_gatt_disc_c_test_misc_cb, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_c_test_misc_rx_all_rsp(conn, end_handle, chars);
+    ble_gatt_disc_c_test_misc_verify_chars(chars);
+}
+
+TEST_CASE(ble_gatt_disc_c_test_disc_all)
+{
+    /*** One 16-bit characteristic. */
+    ble_gatt_disc_c_test_misc_all(50, 100,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        { 55, (uint8_t[]) { 0x10, 0x20 }, 2 },
+        { 0 }
+    });
+
+    /*** Two 16-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        { 55, (uint8_t[]) { 0x10, 0x20 }, 2 },
+        { 56, (uint8_t[]) { 0x32, 0x55 }, 2 },
+        { 0 }
+    });
+
+    /*** Five 16-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        { 55, (uint8_t[]) { 0x10, 0x20 }, 2 },
+        { 56, (uint8_t[]) { 0x32, 0x55 }, 2 },
+        { 58, (uint8_t[]) { 0xfa, 0xc4 }, 2 },
+        { 63, (uint8_t[]) { 0x43, 0x2e }, 2 },
+        { 77, (uint8_t[]) { 0x83, 0x36 }, 2 },
+        { 0 }
+    });
+
+    /*** Interleaved 16-bit and 128-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        { 55, (uint8_t[]) { 0x10, 0x20 }, 2 },
+        { 56, (uint8_t[]) { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }, 16 },
+        { 58, (uint8_t[]) { 0xfa, 0xc4 }, 2 },
+        { 63, (uint8_t[]) { 5,2,7,5,4,8,7,6,2,2,40,64,85,62,50,49 }, 16 },
+        { 77, (uint8_t[]) { 0x83, 0x36 }, 2 },
+        { 0 }
+    });
+
+    /*** Ends with final handle ID. */
+    ble_gatt_disc_c_test_misc_all(50, 100,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        { 55, (uint8_t[]) { 0x10, 0x20 }, 2 },
+        { 100, (uint8_t[]) { 0x32, 0x55 }, 2 },
+        { 0 }
+    });
+}
+
+TEST_SUITE(gle_gatt_disc_c_test_suite)
+{
+    ble_gatt_disc_c_test_disc_all();
+}
+
+int
+ble_gatt_disc_c_test_all(void)
+{
+    gle_gatt_disc_c_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/test/ble_gatt_disc_s_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_s_test.c b/net/nimble/host/src/test/ble_gatt_disc_s_test.c
index be5f9e8..b724a24 100644
--- a/net/nimble/host/src/test/ble_gatt_disc_s_test.c
+++ b/net/nimble/host/src/test/ble_gatt_disc_s_test.c
@@ -304,7 +304,7 @@ ble_gatt_disc_s_test_misc_good_uuid(
     ble_gatt_disc_s_test_misc_verify_services(services);
 }
 
-TEST_CASE(ble_gatt_disc_s_test_disc_all_services)
+TEST_CASE(ble_gatt_disc_s_test_disc_all)
 {
     /*** One 128-bit service. */
     ble_gatt_disc_s_test_misc_all((struct ble_gatt_disc_s_test_svc[]) {
@@ -340,6 +340,7 @@ TEST_CASE(ble_gatt_disc_s_test_disc_all_services)
     ble_gatt_disc_s_test_misc_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 }, },
+        { 0 }
     });
 }
 
@@ -406,16 +407,16 @@ TEST_CASE(ble_gatt_disc_s_test_disc_service_uuid)
     });
 }
 
-TEST_SUITE(ble_gatt_suite)
+TEST_SUITE(gle_gatt_disc_s_test_suite)
 {
-    ble_gatt_disc_s_test_disc_all_services();
+    ble_gatt_disc_s_test_disc_all();
     ble_gatt_disc_s_test_disc_service_uuid();
 }
 
 int
 ble_gatt_disc_s_test_all(void)
 {
-    ble_gatt_suite();
+    gle_gatt_disc_s_test_suite();
 
     return tu_any_failed;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b4241d69/net/nimble/host/src/test/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test.c b/net/nimble/host/src/test/ble_hs_test.c
index 9424b4a..d3b6bd6 100644
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ b/net/nimble/host/src/test/ble_hs_test.c
@@ -47,6 +47,7 @@ main(void)
     ble_gap_test_all();
     ble_hs_uuid_test_all();
     ble_gatt_disc_s_test_all();
+    ble_gatt_disc_c_test_all();
 
     return tu_any_failed;
 }