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 2016/01/09 03:09:37 UTC

[1/3] incubator-mynewt-larva git commit: Replace descriptor fields with single struct.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 8a5d285fd -> 9a869c056


Replace descriptor fields with single struct.


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/b6566c44
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/b6566c44
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/b6566c44

Branch: refs/heads/master
Commit: b6566c44b30629cdb0222ed2882832bae2bcca51
Parents: 5cc71df
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 8 17:21:05 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 8 18:09:04 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gatt.h         |  9 ++++++--
 net/nimble/host/src/ble_gattc.c                 | 22 +++++++++++---------
 net/nimble/host/src/test/ble_gatt_disc_d_test.c | 16 +++++++-------
 3 files changed, 27 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b6566c44/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 47c6d04..802e8bd 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -43,6 +43,11 @@ struct ble_gatt_chr {
     uint8_t uuid128[16];
 };
 
+struct ble_gatt_dsc {
+    uint16_t handle;
+    uint8_t uuid128[16];
+};
+
 typedef int ble_gatt_disc_svc_fn(uint16_t conn_handle, int status,
                                  struct ble_gatt_service *service,
                                  void *arg);
@@ -53,8 +58,8 @@ typedef int ble_gatt_chr_fn(uint16_t conn_handle, int status,
                             struct ble_gatt_chr *chr, void *arg);
 
 typedef int ble_gatt_dsc_fn(uint16_t conn_handle, int status,
-                            uint16_t chr_val_handle, uint16_t dsc_handle,
-                            uint8_t *dsc_uuid128, void *arg);
+                            uint16_t chr_val_handle, struct ble_gatt_dsc *dsc,
+                            void *arg);
 
 int ble_gattc_disc_all_svcs(uint16_t conn_handle,
                             ble_gatt_disc_svc_fn *cb, void *cb_arg);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b6566c44/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 97ad893..8eed257 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -580,7 +580,7 @@ ble_gattc_tx_postpone_chk(struct ble_gattc_entry *entry, int rc)
 }
 
 /*****************************************************************************
- * @mtu                                                                      *
+ * $mtu                                                                      *
  *****************************************************************************/
 
 static int
@@ -815,7 +815,7 @@ ble_gattc_disc_all_svcs(uint16_t conn_handle, ble_gatt_disc_svc_fn *cb,
 
 static int
 ble_gattc_disc_svc_uuid_cb(struct ble_gattc_entry *entry, int status,
-                              struct ble_gatt_service *service)
+                           struct ble_gatt_service *service)
 {
     int rc;
 
@@ -1485,7 +1485,7 @@ ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
 
 static int
 ble_gattc_disc_all_dscs_cb(struct ble_gattc_entry *entry, int status,
-                           uint16_t dsc_handle, uint8_t *dsc_uuid128)
+                           struct ble_gatt_dsc *dsc)
 {
     int rc;
 
@@ -1494,8 +1494,7 @@ ble_gattc_disc_all_dscs_cb(struct ble_gattc_entry *entry, int status,
     } else {
         rc = entry->disc_all_dscs.cb(entry->conn_handle, status,
                                      entry->disc_all_dscs.chr_val_handle,
-                                     dsc_handle, dsc_uuid128,
-                                     entry->disc_all_dscs.cb_arg);
+                                     dsc, entry->disc_all_dscs.cb_arg);
     }
 
     return rc;
@@ -1529,7 +1528,7 @@ err:
         return BLE_HS_EAGAIN;
     }
 
-    ble_gattc_disc_all_dscs_cb(entry, rc, 0, NULL);
+    ble_gattc_disc_all_dscs_cb(entry, rc, NULL);
     return rc;
 }
 
@@ -1541,7 +1540,7 @@ ble_gattc_disc_all_dscs_err(struct ble_gattc_entry *entry, int status)
         status = 0;
     }
 
-    ble_gattc_disc_all_dscs_cb(entry, status, 0, NULL);
+    ble_gattc_disc_all_dscs_cb(entry, status, NULL);
 }
 
 static int
@@ -1549,6 +1548,7 @@ ble_gattc_disc_all_dscs_rx_idata(struct ble_gattc_entry *entry,
                                  struct ble_hs_conn *conn,
                                  struct ble_att_find_info_idata *idata)
 {
+    struct ble_gatt_dsc dsc;
     int cbrc;
     int rc;
 
@@ -1562,8 +1562,10 @@ ble_gattc_disc_all_dscs_rx_idata(struct ble_gattc_entry *entry,
     rc = 0;
 
 done:
-    cbrc = ble_gattc_disc_all_dscs_cb(entry, rc, idata->attr_handle,
-                                      idata->uuid128);
+    dsc.handle = idata->attr_handle;
+    memcpy(dsc.uuid128, idata->uuid128, 16);
+
+    cbrc = ble_gattc_disc_all_dscs_cb(entry, rc, &dsc);
     if (rc == 0) {
         rc = cbrc;
     }
@@ -1577,7 +1579,7 @@ ble_gattc_disc_all_dscs_rx_complete(struct ble_gattc_entry *entry,
     if (status != 0 || entry->disc_all_dscs.prev_handle ==
                        entry->disc_all_dscs.end_handle) {
         /* Error or all descriptors discovered. */
-        ble_gattc_disc_all_dscs_cb(entry, status, 0, NULL);
+        ble_gattc_disc_all_dscs_cb(entry, status, NULL);
         return 1;
     } else {
         /* Send follow-up request. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b6566c44/net/nimble/host/src/test/ble_gatt_disc_d_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_d_test.c b/net/nimble/host/src/test/ble_gatt_disc_d_test.c
index 5add73d..d362807 100644
--- a/net/nimble/host/src/test/ble_gatt_disc_d_test.c
+++ b/net/nimble/host/src/test/ble_gatt_disc_d_test.c
@@ -161,10 +161,10 @@ ble_gatt_disc_d_test_misc_verify_dscs(struct ble_gatt_disc_d_test_dsc *dscs,
 
 static int
 ble_gatt_disc_d_test_misc_cb(uint16_t conn_handle, int status,
-                             uint16_t chr_val_handle, uint16_t dsc_handle,
-                             uint8_t *dsc_uuid128, void *arg)
+                             uint16_t chr_val_handle, struct ble_gatt_dsc *dsc,
+                             void *arg)
 {
-    struct ble_gatt_disc_d_test_dsc *dsc;
+    struct ble_gatt_disc_d_test_dsc *dst;
     int *stop_after;
 
     TEST_ASSERT(status == 0);
@@ -172,16 +172,16 @@ ble_gatt_disc_d_test_misc_cb(uint16_t conn_handle, int status,
 
     stop_after = arg;
 
-    if (dsc_handle == 0) {
+    if (dsc == NULL) {
         ble_gatt_disc_d_test_rx_complete = 1;
     } else {
         TEST_ASSERT_FATAL(ble_gatt_disc_d_test_num_dscs <
                           BLE_GATT_DISC_D_TEST_MAX_DSCS);
 
-        dsc = ble_gatt_disc_d_test_dscs + ble_gatt_disc_d_test_num_dscs++;
-        dsc->chr_val_handle = chr_val_handle;
-        dsc->dsc_handle = dsc_handle;
-        memcpy(dsc->dsc_uuid128, dsc_uuid128, 16);
+        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);
     }
 
     if (*stop_after > 0) {


[2/3] incubator-mynewt-larva git commit: GATT Read Long Characteristic Value procedure.

Posted by cc...@apache.org.
GATT Read Long Characteristic Value procedure.


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/5cc71dff
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/5cc71dff
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/5cc71dff

Branch: refs/heads/master
Commit: 5cc71dffeffd045aba2e4b41a2b482bc76c2d522
Parents: 8a5d285
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 8 16:49:33 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 8 18:09:04 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_att.h  |  12 +-
 net/nimble/host/include/host/ble_gatt.h |   3 +
 net/nimble/host/src/ble_att_clt.c       |  14 +--
 net/nimble/host/src/ble_gatt_priv.h     |   2 +
 net/nimble/host/src/ble_gattc.c         | 175 +++++++++++++++++++++++++--
 5 files changed, 186 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/5cc71dff/net/nimble/host/include/host/ble_att.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_att.h b/net/nimble/host/include/host/ble_att.h
index 8f1899f..ee177b3 100644
--- a/net/nimble/host/include/host/ble_att.h
+++ b/net/nimble/host/include/host/ble_att.h
@@ -66,12 +66,6 @@
 
 #define BLE_ATT_ATTR_MAX_LEN                512
 
-struct ble_att_svr_access_ctxt {
-    void *attr_data;
-    uint16_t data_len;
-    uint16_t offset;
-};
-
 #define HA_FLAG_PERM_READ                   (1 << 0)
 #define HA_FLAG_PERM_WRITE                  (1 << 1)
 #define HA_FLAG_ENC_REQ                     (1 << 2)
@@ -83,6 +77,12 @@ struct ble_att_svr_access_ctxt {
 #define BLE_ATT_ACCESS_OP_READ              1
 #define BLE_ATT_ACCESS_OP_WRITE             2
 
+struct ble_att_svr_access_ctxt {
+    void *attr_data;
+    uint16_t data_len;
+    uint16_t offset; /* Only used for read-blob requests. */
+};
+
 /**
  * Handles a host attribute request.
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/5cc71dff/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 94f80b3..47c6d04 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -31,6 +31,7 @@ struct ble_gatt_service {
 
 struct ble_gatt_attr {
     uint16_t handle;
+    uint16_t offset;
     uint8_t value_len;
     void *value;
 };
@@ -76,6 +77,8 @@ int ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle,
 int ble_gattc_read_uuid(uint16_t conn_handle, uint16_t start_handle,
                         uint16_t end_handle, void *uuid128,
                         ble_gatt_attr_fn *cb, void *cb_arg);
+int ble_gattc_read_long(uint16_t conn_handle, uint16_t handle,
+                        ble_gatt_attr_fn *cb, void *cb_arg);
 int ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
                            void *value, uint16_t value_len,
                            ble_gatt_attr_fn *cb, void *cb_arg);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/5cc71dff/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_clt.c b/net/nimble/host/src/ble_att_clt.c
index 1da1008..cd37ac8 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -621,12 +621,12 @@ int
 ble_att_clt_rx_read_blob(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                          struct os_mbuf **rxom)
 {
-    //void *value;
-    //int value_len;
+    void *value;
+    int value_len;
     int rc;
 
-    //value = NULL;
-    //value_len = 0;
+    value = NULL;
+    value_len = 0;
 
     /* Reponse consists of a one-byte opcode (already verified) and a variable
      * length Attribute Value field.  Strip the opcode from the response.
@@ -639,14 +639,14 @@ ble_att_clt_rx_read_blob(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         goto done;
     }
 
-    //value_len = (*rxom)->om_len;
-    //value = (*rxom)->om_data;
+    value_len = (*rxom)->om_len;
+    value = (*rxom)->om_data;
 
     rc = 0;
 
 done:
     /* Pass the Attribute Value field to GATT. */
-    //ble_gattc_rx_read_blob_rsp(conn, rc, value, value_len);
+    ble_gattc_rx_read_blob_rsp(conn, rc, value, value_len);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/5cc71dff/net/nimble/host/src/ble_gatt_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt_priv.h b/net/nimble/host/src/ble_gatt_priv.h
index 7ec84b7..5b2c956 100644
--- a/net/nimble/host/src/ble_gatt_priv.h
+++ b/net/nimble/host/src/ble_gatt_priv.h
@@ -53,6 +53,8 @@ void ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
 void ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status);
 void ble_gattc_rx_read_rsp(struct ble_hs_conn *conn, int status, void *value,
                            int value_len);
+void ble_gattc_rx_read_blob_rsp(struct ble_hs_conn *conn, int status,
+                                void *value, int value_len);
 void ble_gattc_rx_read_group_type_adata(
     struct ble_hs_conn *conn, struct ble_att_read_group_type_adata *adata);
 void ble_gattc_rx_read_group_type_complete(struct ble_hs_conn *conn, int rc);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/5cc71dff/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index e7f89f8..97ad893 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -97,6 +97,13 @@ struct ble_gattc_entry {
         } read;
 
         struct {
+            uint16_t handle;
+            uint16_t offset;
+            ble_gatt_attr_fn *cb;
+            void *cb_arg;
+        } read_long;
+
+        struct {
             uint16_t prev_handle;
             uint16_t end_handle;
             uint8_t uuid128[16];
@@ -131,10 +138,11 @@ struct ble_gattc_entry {
 #define BLE_GATT_OP_DISC_ALL_DSCS               6
 #define BLE_GATT_OP_READ                        7
 #define BLE_GATT_OP_READ_UUID                   8
-#define BLE_GATT_OP_WRITE_NO_RSP                9
-#define BLE_GATT_OP_WRITE                       10
-#define BLE_GATT_OP_INDICATE                    11
-#define BLE_GATT_OP_MAX                         12
+#define BLE_GATT_OP_READ_LONG                   9
+#define BLE_GATT_OP_WRITE_NO_RSP                10
+#define BLE_GATT_OP_WRITE                       11
+#define BLE_GATT_OP_INDICATE                    12
+#define BLE_GATT_OP_MAX                         13
 
 static struct os_callout_func ble_gattc_heartbeat_timer;
 
@@ -150,6 +158,7 @@ static int ble_gattc_disc_chr_uuid_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_disc_all_dscs_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_read_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_read_uuid_kick(struct ble_gattc_entry *entry);
+static int ble_gattc_read_long_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_write_no_rsp_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_write_kick(struct ble_gattc_entry *entry);
 static int ble_gattc_indicate_kick(struct ble_gattc_entry *entry);
@@ -169,6 +178,7 @@ static void ble_gattc_disc_all_dscs_err(struct ble_gattc_entry *entry,
                                         int status);
 static void ble_gattc_read_err(struct ble_gattc_entry *entry, int status);
 static void ble_gattc_read_uuid_err(struct ble_gattc_entry *entry, int status);
+static void ble_gattc_read_long_err(struct ble_gattc_entry *entry, int status);
 static void ble_gattc_write_err(struct ble_gattc_entry *entry, int status);
 static void ble_gattc_indicate_err(struct ble_gattc_entry *entry, int status);
 
@@ -200,6 +210,10 @@ static int ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_entry *entry,
 static int ble_gattc_read_rx_read_rsp(struct ble_gattc_entry *entry,
                                       struct ble_hs_conn *conn, int status,
                                       void *value, int value_len);
+static int ble_gattc_read_long_rx_read_rsp(struct ble_gattc_entry *entry,
+                                           struct ble_hs_conn *conn,
+                                           int status, void *value,
+                                           int value_len);
 static int
 ble_gattc_read_uuid_rx_adata(struct ble_gattc_entry *entry,
                              struct ble_hs_conn *conn,
@@ -252,6 +266,10 @@ static const struct ble_gattc_dispatch_entry
         .kick_cb = ble_gattc_read_uuid_kick,
         .err_cb = ble_gattc_read_uuid_err,
     },
+    [BLE_GATT_OP_READ_LONG] = {
+        .kick_cb = ble_gattc_read_long_kick,
+        .err_cb = ble_gattc_read_long_err,
+    },
     [BLE_GATT_OP_WRITE_NO_RSP] = {
         .kick_cb = ble_gattc_write_no_rsp_kick,
         .err_cb = NULL,
@@ -309,8 +327,9 @@ static const struct ble_gattc_rx_complete_entry
 };
 
 static const struct ble_gattc_rx_attr_entry ble_gattc_rx_read_rsp_entries[] = {
-    { BLE_GATT_OP_READ, ble_gattc_read_rx_read_rsp },
-    { BLE_GATT_OP_FIND_INC_SVCS, ble_gattc_find_inc_svcs_rx_read_rsp },
+    { BLE_GATT_OP_READ,             ble_gattc_read_rx_read_rsp },
+    { BLE_GATT_OP_READ_LONG,        ble_gattc_read_long_rx_read_rsp },
+    { BLE_GATT_OP_FIND_INC_SVCS,    ble_gattc_find_inc_svcs_rx_read_rsp },
 };
 
 #define BLE_GATT_ENTRY_F_PENDING    0x01
@@ -1652,6 +1671,7 @@ ble_gattc_read_rx_read_rsp(struct ble_gattc_entry *entry,
     struct ble_gatt_attr attr;
 
     attr.handle = entry->read.handle;
+    attr.offset = 0;
     attr.value_len = value_len;
     attr.value = value;
 
@@ -1663,7 +1683,7 @@ ble_gattc_read_rx_read_rsp(struct ble_gattc_entry *entry,
 
 int
 ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle,
-              ble_gatt_attr_fn *cb, void *cb_arg)
+               ble_gatt_attr_fn *cb, void *cb_arg)
 {
     struct ble_gattc_entry *entry;
     int rc;
@@ -1749,6 +1769,7 @@ ble_gattc_read_uuid_rx_adata(struct ble_gattc_entry *entry,
     int rc;
 
     attr.handle = adata->att_handle;
+    attr.offset = 0;
     attr.value_len = adata->value_len;
     attr.value = adata->value;
 
@@ -1802,6 +1823,125 @@ ble_gattc_read_uuid(uint16_t conn_handle, uint16_t start_handle,
 }
 
 /*****************************************************************************
+ * $read long                                                                *
+ *****************************************************************************/
+
+static int
+ble_gattc_read_long_cb(struct ble_gattc_entry *entry, int status,
+                       struct ble_gatt_attr *attr)
+{
+    int rc;
+
+    if (entry->read_long.cb == NULL) {
+        rc = 0;
+    } else {
+        rc = entry->read_long.cb(entry->conn_handle, status, attr,
+                                 entry->read_long.cb_arg);
+    }
+
+    return rc;
+}
+
+static int
+ble_gattc_read_long_kick(struct ble_gattc_entry *entry)
+{
+    struct ble_att_read_blob_req blob_req;
+    struct ble_att_read_req read_req;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    conn = ble_hs_conn_find(entry->conn_handle);
+    if (conn == NULL) {
+        rc = BLE_HS_ENOTCONN;
+        goto err;
+    }
+
+    if (entry->read_long.offset == 0) {
+        read_req.barq_handle = entry->read_long.handle;
+        rc = ble_att_clt_tx_read(conn, &read_req);
+    } else {
+        blob_req.babq_handle = entry->read_long.handle;
+        blob_req.babq_offset = entry->read_long.offset;
+        rc = ble_att_clt_tx_read_blob(conn, &blob_req);
+    }
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    if (ble_gattc_tx_postpone_chk(entry, rc)) {
+        return BLE_HS_EAGAIN;
+    }
+
+    ble_gattc_read_long_cb(entry, rc, NULL);
+    return rc;
+}
+
+static void
+ble_gattc_read_long_err(struct ble_gattc_entry *entry, int status)
+{
+    struct ble_gatt_attr attr;
+
+    memset(&attr, 0, sizeof attr);
+    ble_gattc_read_long_cb(entry, status, &attr);
+}
+
+static int
+ble_gattc_read_long_rx_read_rsp(struct ble_gattc_entry *entry,
+                                struct ble_hs_conn *conn, int status,
+                                void *value, int value_len)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_gatt_attr attr;
+    int rc;
+
+    attr.handle = entry->read_long.handle;
+    attr.offset = entry->read_long.offset;
+    attr.value_len = value_len;
+    attr.value = value;
+
+    rc = ble_gattc_read_long_cb(entry, status, &attr);
+    if (rc != 0 || status != 0) {
+        return 1;
+    }
+
+    /* Determine if this is the end of the attribute value. */
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    assert(chan != NULL);
+
+    if (value_len < ble_l2cap_chan_mtu(chan) - 1) {
+        attr.value_len = 0;
+        attr.value = NULL;
+        ble_gattc_read_long_cb(entry, 0, &attr);
+        return 1;
+    } else {
+        entry->read_long.offset += value_len;
+        return 0;
+    }
+}
+
+int
+ble_gattc_read_long(uint16_t conn_handle, uint16_t handle,
+                    ble_gatt_attr_fn *cb, void *cb_arg)
+{
+    struct ble_gattc_entry *entry;
+    int rc;
+
+    rc = ble_gattc_new_entry(conn_handle, BLE_GATT_OP_READ_LONG, &entry);
+    if (rc != 0) {
+        return rc;
+    }
+    entry->read_long.handle = handle;
+    entry->read_long.offset = 0;
+    entry->read_long.cb = cb;
+    entry->read_long.cb_arg = cb_arg;
+
+    return 0;
+}
+
+/*****************************************************************************
  * $write no response                                                        *
  *****************************************************************************/
 
@@ -2325,6 +2465,27 @@ ble_gattc_rx_read_rsp(struct ble_hs_conn *conn, int status, void *value,
 }
 
 void
+ble_gattc_rx_read_blob_rsp(struct ble_hs_conn *conn, int status,
+                           void *value, int value_len)
+{
+    struct ble_gattc_entry *entry;
+    struct ble_gattc_entry *prev;
+    int rc;
+
+    entry = ble_gattc_find(conn->bhc_handle, BLE_GATT_OP_READ_LONG, 1, &prev);
+    if (entry == NULL) {
+        /* Not expecting a response from this device. */
+        return;
+    }
+
+    rc = ble_gattc_read_long_rx_read_rsp(entry, conn, status, value,
+                                         value_len);
+    if (rc != 0) {
+        ble_gattc_entry_remove_free(entry, prev);
+    }
+}
+
+void
 ble_gattc_rx_write_rsp(struct ble_hs_conn *conn)
 {
     struct ble_gattc_entry *entry;


[3/3] incubator-mynewt-larva git commit: Tests for GATT procedure: read long characteristic

Posted by cc...@apache.org.
Tests for GATT procedure: read long characteristic


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/9a869c05
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/9a869c05
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/9a869c05

Branch: refs/heads/master
Commit: 9a869c056d023f034a9d91ca0deba09d2002418b
Parents: b6566c4
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 8 18:08:24 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 8 18:09:05 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gattc.c               |   9 +-
 net/nimble/host/src/test/ble_gatt_read_test.c | 218 +++++++++++++++++++--
 2 files changed, 201 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/9a869c05/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 8eed257..1c59f25 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -1884,10 +1884,7 @@ err:
 static void
 ble_gattc_read_long_err(struct ble_gattc_entry *entry, int status)
 {
-    struct ble_gatt_attr attr;
-
-    memset(&attr, 0, sizeof attr);
-    ble_gattc_read_long_cb(entry, status, &attr);
+    ble_gattc_read_long_cb(entry, status, NULL);
 }
 
 static int
@@ -1914,9 +1911,7 @@ ble_gattc_read_long_rx_read_rsp(struct ble_gattc_entry *entry,
     assert(chan != NULL);
 
     if (value_len < ble_l2cap_chan_mtu(chan) - 1) {
-        attr.value_len = 0;
-        attr.value = NULL;
-        ble_gattc_read_long_cb(entry, 0, &attr);
+        ble_gattc_read_long_cb(entry, 0, NULL);
         return 1;
     } else {
         entry->read_long.offset += value_len;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/9a869c05/net/nimble/host/src/test/ble_gatt_read_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_read_test.c b/net/nimble/host/src/test/ble_gatt_read_test.c
index 2590c79..22dc1f4 100644
--- a/net/nimble/host/src/test/ble_gatt_read_test.c
+++ b/net/nimble/host/src/test/ble_gatt_read_test.c
@@ -16,6 +16,7 @@
 
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include "testutil/testutil.h"
 #include "nimble/ble.h"
 #include "host/ble_hs_test.h"
@@ -28,10 +29,9 @@
 
 struct ble_gatt_read_test_attr {
     uint16_t conn_handle;
-    int status;
     uint16_t handle;
     uint8_t value_len;
-    uint8_t value[256];
+    uint8_t value[BLE_ATT_ATTR_MAX_LEN];
 };
 
 #define BLE_GATT_READ_TEST_MAX_ATTRS    256
@@ -52,6 +52,9 @@ ble_gatt_read_test_misc_init(void)
     ble_gatt_read_test_complete = 0;
     ble_gatt_read_test_bad_conn_handle = 0;
     ble_gatt_read_test_bad_status = 0;
+
+    memset(&ble_gatt_read_test_attrs[0], 0,
+           sizeof ble_gatt_read_test_attrs[0]);
 }
 
 static int
@@ -63,13 +66,18 @@ ble_gatt_read_test_cb(uint16_t conn_handle, int status,
 
     stop_after = arg;
 
-    if (attr == NULL) {
+    if (status != 0) {
         ble_gatt_read_test_bad_conn_handle = conn_handle;
         ble_gatt_read_test_bad_status = status;
         ble_gatt_read_test_complete = 1;
         return 0;
     }
 
+    if (attr == NULL) {
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
     TEST_ASSERT_FATAL(ble_gatt_read_test_num_attrs <
                       BLE_GATT_READ_TEST_MAX_ATTRS);
     dst = ble_gatt_read_test_attrs + ble_gatt_read_test_num_attrs++;
@@ -77,14 +85,9 @@ ble_gatt_read_test_cb(uint16_t conn_handle, int status,
     TEST_ASSERT_FATAL(attr->value_len <= sizeof dst->value);
 
     dst->conn_handle = conn_handle;
-    dst->status = status;
-
-    if (status == 0) {
-        TEST_ASSERT_FATAL(attr != NULL);
-        dst->handle = attr->handle;
-        dst->value_len = attr->value_len;
-        memcpy(dst->value, attr->value, attr->value_len);
-    }
+    dst->handle = attr->handle;
+    dst->value_len = attr->value_len;
+    memcpy(dst->value, attr->value, attr->value_len);
 
     if (stop_after != NULL && *stop_after > 0) {
         (*stop_after)--;
@@ -97,31 +100,88 @@ ble_gatt_read_test_cb(uint16_t conn_handle, int status,
     return 0;
 }
 
+static int
+ble_gatt_read_test_long_cb(uint16_t conn_handle, int status,
+                           struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_read_test_attr *dst;
+    int *reads_left;
+
+    reads_left = arg;
+
+    if (status != 0) {
+        ble_gatt_read_test_bad_conn_handle = conn_handle;
+        ble_gatt_read_test_bad_status = status;
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    if (attr == NULL) {
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    dst = ble_gatt_read_test_attrs + 0;
+
+    TEST_ASSERT_FATAL(attr->value_len <= dst->value_len + sizeof dst->value);
+    TEST_ASSERT(attr->offset == dst->value_len);
+
+    if (attr->offset == 0) {
+        dst->conn_handle = conn_handle;
+        dst->handle = attr->handle;
+    } else {
+        TEST_ASSERT(conn_handle == dst->conn_handle);
+        TEST_ASSERT(attr->handle == dst->handle);
+    }
+    memcpy(dst->value + dst->value_len, attr->value, attr->value_len);
+    dst->value_len += attr->value_len;
+
+    if (reads_left != NULL && *reads_left > 0) {
+        (*reads_left)--;
+        if (*reads_left == 0) {
+            ble_gatt_read_test_complete = 1;
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
 static void
-ble_gatt_read_test_misc_rx_rsp_good(struct ble_hs_conn *conn,
-                                    struct ble_gatt_attr *attr)
+ble_gatt_read_test_misc_rx_rsp_good_raw(struct ble_hs_conn *conn,
+                                        uint8_t att_op,
+                                        void *data, int data_len)
 {
     struct ble_l2cap_chan *chan;
     uint8_t buf[1024];
     int rc;
 
-    TEST_ASSERT_FATAL(attr->value_len <= sizeof buf);
+    TEST_ASSERT_FATAL(data_len <= sizeof buf);
 
     /* Send the pending ATT Read Request. */
     ble_hs_test_util_tx_all();
 
-    buf[0] = BLE_ATT_OP_READ_RSP;
-    memcpy(buf + 1, attr->value, attr->value_len);
+    buf[0] = att_op;
+    memcpy(buf + 1, data, data_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,
-                                                1 + attr->value_len);
+                                                1 + data_len);
     TEST_ASSERT(rc == 0);
 }
 
 static void
+ble_gatt_read_test_misc_rx_rsp_good(struct ble_hs_conn *conn,
+                                    struct ble_gatt_attr *attr)
+{
+    ble_gatt_read_test_misc_rx_rsp_good_raw(conn, BLE_ATT_OP_READ_RSP,
+                                            attr->value,
+                                            attr->value_len);
+}
+
+static void
 ble_gatt_read_test_misc_rx_rsp_bad(struct ble_hs_conn *conn,
                                    struct ble_gatt_attr *attr,
                                    uint8_t att_error)
@@ -197,7 +257,6 @@ ble_gatt_read_test_misc_verify_good(struct ble_gatt_attr *attr)
 
     TEST_ASSERT(ble_gatt_read_test_num_attrs == 1);
     TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == conn->bhc_handle);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].status == 0);
     TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
     TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
     TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
@@ -265,7 +324,6 @@ ble_gatt_read_test_misc_uuid_verify_good(uint16_t start_handle,
     for (i = 0; i < idx; i++) {
         TEST_ASSERT(ble_gatt_read_test_attrs[i].conn_handle ==
                     conn->bhc_handle);
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].status == 0);
         TEST_ASSERT(ble_gatt_read_test_attrs[i].handle == attrs[i].handle);
         TEST_ASSERT(ble_gatt_read_test_attrs[i].value_len ==
                     attrs[i].value_len);
@@ -274,6 +332,81 @@ ble_gatt_read_test_misc_uuid_verify_good(uint16_t start_handle,
     }
 }
 
+static void
+ble_gatt_read_test_misc_long_verify_good(int max_reads,
+                                         struct ble_gatt_attr *attr)
+{
+    struct ble_hs_conn *conn;
+    int reads_left;
+    int chunk_sz;
+    int rem_len;
+    int att_op;
+    int off;
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    conn = ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}));
+
+    if (max_reads == 0) {
+        max_reads = INT_MAX;
+    }
+    reads_left = max_reads;
+    rc = ble_gattc_read_long(conn->bhc_handle, attr->handle,
+                             ble_gatt_read_test_long_cb, &reads_left);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    off = 0;
+    rem_len = attr->value_len;
+    do {
+        if (rem_len > BLE_ATT_MTU_DFLT - 1) {
+            chunk_sz = BLE_ATT_MTU_DFLT - 1;
+        } else {
+            chunk_sz = rem_len;
+        }
+        if (off == 0) {
+            att_op = BLE_ATT_OP_READ_RSP;
+        } else {
+            att_op = BLE_ATT_OP_READ_BLOB_RSP;
+        }
+        ble_gatt_read_test_misc_rx_rsp_good_raw(conn, att_op,
+                                                attr->value + off, chunk_sz);
+        rem_len -= chunk_sz;
+        off += chunk_sz;
+    } while (rem_len > 0 && reads_left > 0);
+
+    TEST_ASSERT(ble_gatt_read_test_complete);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == conn->bhc_handle);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
+    if (reads_left > 0) {
+        TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
+    }
+    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
+                       ble_gatt_read_test_attrs[0].value_len) == 0);
+}
+
+static void
+ble_gatt_read_test_misc_long_verify_bad(uint8_t att_status,
+                                        struct ble_gatt_attr *attr)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    conn = ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}));
+
+    rc = ble_gattc_read_long(conn->bhc_handle, attr->handle,
+                             ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_bad(conn, attr, att_status);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
+    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == conn->bhc_handle);
+    TEST_ASSERT(ble_gatt_read_test_bad_status ==
+                BLE_HS_ERR_ATT_BASE + att_status);
+}
+
 TEST_CASE(ble_gatt_read_test_by_handle)
 {
     /* Read a seven-byte attribute. */
@@ -381,10 +514,57 @@ TEST_CASE(ble_gatt_read_test_by_uuid)
         } });
 }
 
+TEST_CASE(ble_gatt_read_test_long)
+{
+    uint8_t data512[512];
+    int i;
+
+    for (i = 0; i < sizeof data512; i++) {
+        data512[i] = i;
+    }
+
+    /* Read a seven-byte attribute. */
+    ble_gatt_read_test_misc_long_verify_good(0, (struct ble_gatt_attr[]) { {
+        .handle = 43,
+        .value = data512,
+        .value_len = 7
+    } });
+
+    /* Read a zero-byte attribute. */
+    ble_gatt_read_test_misc_long_verify_good(0, (struct ble_gatt_attr[]) { {
+        .handle = 43,
+        .value = NULL,
+        .value_len = 0
+    } });
+
+    /* Read a 60-byte attribute; three requests. */
+    ble_gatt_read_test_misc_long_verify_good(0, (struct ble_gatt_attr[]) { {
+        .handle = 34,
+        .value = data512,
+        .value_len = 60
+    } });
+
+    /* Stop after two reads. */
+    ble_gatt_read_test_misc_long_verify_good(2, (struct ble_gatt_attr[]) { {
+        .handle = 34,
+        .value = data512,
+        .value_len = 60
+    } });
+
+    /* Fail due to attribute not found. */
+    ble_gatt_read_test_misc_long_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND,
+        (struct ble_gatt_attr[]) { {
+            .handle = 719,
+            .value = (uint8_t[]){ 1,2,3,4,5,6,7 },
+            .value_len = 7
+        } });
+}
+
 TEST_SUITE(ble_gatt_read_test_suite)
 {
     ble_gatt_read_test_by_handle();
     ble_gatt_read_test_by_uuid();
+    ble_gatt_read_test_long();
 }
 
 int