You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2017/04/10 11:46:58 UTC

[06/50] incubator-mynewt-core git commit: nimble/att: Use new helpers for sending Write Request and Command

nimble/att: Use new helpers for sending Write Request and Command

This constructs response directly on mbuf reducing number of memcopies.


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

Branch: refs/heads/master
Commit: a9549fdbc17383c00851dde7bd555cf628d3876b
Parents: 191acab
Author: Szymon Janc <sz...@codecoup.pl>
Authored: Fri Mar 24 15:34:01 2017 +0100
Committer: Szymon Janc <sz...@codecoup.pl>
Committed: Mon Apr 10 11:31:31 2017 +0200

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_clt.c           | 65 +++++++++---------------
 net/nimble/host/src/ble_att_cmd.c           | 10 +++-
 net/nimble/host/src/ble_att_cmd_priv.h      | 17 ++++++-
 net/nimble/host/src/ble_att_priv.h          |  6 +--
 net/nimble/host/src/ble_att_svr.c           |  4 +-
 net/nimble/host/src/ble_gattc.c             |  8 +--
 net/nimble/host/test/src/ble_att_clt_test.c | 14 ++---
 7 files changed, 59 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/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 0464821..114abea 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -789,62 +789,41 @@ done:
  * $write                                                                    *
  *****************************************************************************/
 
-static int
-ble_att_clt_tx_write_req_or_cmd(uint16_t conn_handle,
-                                const struct ble_att_write_req *req,
-                                struct os_mbuf *txom, int is_req)
-{
-    int rc;
-
-    txom = os_mbuf_prepend_pullup(txom, BLE_ATT_WRITE_REQ_BASE_SZ);
-    if (txom == NULL) {
-        return BLE_HS_ENOMEM;
-    }
-
-    if (is_req) {
-        ble_att_write_req_write(txom->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
-    } else {
-        ble_att_write_cmd_write(txom->om_data, BLE_ATT_WRITE_REQ_BASE_SZ, req);
-    }
-
-    rc = ble_att_clt_tx_req(conn_handle, txom);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
 int
-ble_att_clt_tx_write_req(uint16_t conn_handle,
-                         const struct ble_att_write_req *req,
+ble_att_clt_tx_write_req(uint16_t conn_handle, uint16_t handle,
                          struct os_mbuf *txom)
 {
 #if !NIMBLE_BLE_ATT_CLT_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
-    int rc;
+    struct ble_att_write_req *req;
+    struct os_mbuf *txom2;
 
-    rc = ble_att_clt_tx_write_req_or_cmd(conn_handle, req, txom, 1);
-    if (rc != 0) {
-        return rc;
+    req = ble_att_cmd_get(BLE_ATT_OP_WRITE_REQ, sizeof(*req), &txom2);
+    if (req == NULL) {
+        os_mbuf_free_chain(txom);
+        return BLE_HS_ENOMEM;
     }
 
-    BLE_ATT_LOG_CMD(1, "write req", conn_handle, ble_att_write_cmd_log, req);
+    req->bawq_handle = htole16(handle);
+    os_mbuf_concat(txom2, txom);
 
-    return 0;
+    BLE_ATT_LOG_CMD(1, "write req", conn_handle, ble_att_write_req_log, req);
+
+    return ble_att_tx(conn_handle, txom2);
 }
 
 int
-ble_att_clt_tx_write_cmd(uint16_t conn_handle,
-                         const struct ble_att_write_req *req,
+ble_att_clt_tx_write_cmd(uint16_t conn_handle, uint16_t handle,
                          struct os_mbuf *txom)
 {
 #if !NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP
     return BLE_HS_ENOTSUP;
 #endif
 
+    struct ble_att_write_cmd *cmd;
+    struct os_mbuf *txom2;
     uint8_t b;
     int rc;
     int i;
@@ -860,14 +839,18 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
     }
     
 
-    rc = ble_att_clt_tx_write_req_or_cmd(conn_handle, req, txom, 0);
-    if (rc != 0) {
-        return rc;
+    cmd = ble_att_cmd_get(BLE_ATT_OP_WRITE_CMD, sizeof(*cmd), &txom2);
+    if (cmd == NULL) {
+        os_mbuf_free_chain(txom);
+        return BLE_HS_ENOMEM;
     }
 
-    BLE_ATT_LOG_CMD(1, "write cmd", conn_handle, ble_att_write_cmd_log, req);
+    cmd->handle = htole16(handle);
+    os_mbuf_concat(txom2, txom);
 
-    return 0;
+    BLE_ATT_LOG_CMD(1, "write cmd", conn_handle, ble_att_write_cmd_log, cmd);
+
+    return ble_att_tx(conn_handle, txom2);
 }
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/net/nimble/host/src/ble_att_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_cmd.c b/net/nimble/host/src/ble_att_cmd.c
index 7997ff1..9fb81d1 100644
--- a/net/nimble/host/src/ble_att_cmd.c
+++ b/net/nimble/host/src/ble_att_cmd.c
@@ -610,9 +610,15 @@ ble_att_write_cmd_write(void *payload, int len,
 }
 
 void
-ble_att_write_cmd_log(const struct ble_att_write_req *cmd)
+ble_att_write_cmd_log(const struct ble_att_write_cmd *cmd)
 {
-    BLE_HS_LOG(DEBUG, "handle=0x%04x", cmd->bawq_handle);
+    BLE_HS_LOG(DEBUG, "handle=0x%04x", cmd->handle);
+}
+
+void
+ble_att_write_req_log(const struct ble_att_write_req *req)
+{
+    BLE_HS_LOG(DEBUG, "handle=0x%04x", req->bawq_handle);
 }
 
 static void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/net/nimble/host/src/ble_att_cmd_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_cmd_priv.h b/net/nimble/host/src/ble_att_cmd_priv.h
index 80fe713..491dc99 100644
--- a/net/nimble/host/src/ble_att_cmd_priv.h
+++ b/net/nimble/host/src/ble_att_cmd_priv.h
@@ -252,6 +252,7 @@ struct ble_att_read_group_type_rsp {
 #define BLE_ATT_WRITE_REQ_BASE_SZ       3
 struct ble_att_write_req {
     uint16_t bawq_handle;
+    uint8_t value[0];
 } __attribute__((packed));
 
 #define BLE_ATT_WRITE_RSP_SZ            1
@@ -322,6 +323,19 @@ struct ble_att_indicate_req {
  */
 #define BLE_ATT_INDICATE_RSP_SZ         1
 
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Attribute Handle                   | 2                 |
+ * | Attribute Value                    | 0 to (ATT_MTU-3)  |
+ */
+#define BLE_ATT_WRITE_CMD_BASE_SZ       3
+struct ble_att_write_cmd {
+    uint16_t handle;
+    uint8_t value[0];
+} __attribute__((packed));
+
 void ble_att_error_rsp_parse(const void *payload, int len,
                              struct ble_att_error_rsp *rsp);
 void ble_att_error_rsp_write(void *payload, int len,
@@ -396,7 +410,8 @@ void ble_att_write_cmd_parse(const void *payload, int len,
                              struct ble_att_write_req *req);
 void ble_att_write_cmd_write(void *payload, int len,
                              const struct ble_att_write_req *req);
-void ble_att_write_cmd_log(const struct ble_att_write_req *cmd);
+void ble_att_write_cmd_log(const struct ble_att_write_cmd *cmd);
+void ble_att_write_req_log(const struct ble_att_write_req *req);
 void ble_att_prep_write_req_parse(const void *payload, int len,
                                   struct ble_att_prep_write_cmd *cmd);
 void ble_att_prep_write_req_write(void *payload, int len,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h b/net/nimble/host/src/ble_att_priv.h
index a51f7cc..b7def41 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -276,11 +276,9 @@ int ble_att_clt_tx_find_type_value(uint16_t conn_handle, uint16_t start_handle,
                                    const void *attribute_value, int value_len);
 int ble_att_clt_rx_find_type_value(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
-int ble_att_clt_tx_write_req(uint16_t conn_handle,
-                             const struct ble_att_write_req *req,
+int ble_att_clt_tx_write_req(uint16_t conn_handle, uint16_t handle,
                              struct os_mbuf *txom);
-int ble_att_clt_tx_write_cmd(uint16_t conn_handle,
-                             const struct ble_att_write_req *req,
+int ble_att_clt_tx_write_cmd(uint16_t conn_handle, uint16_t handle,
                              struct os_mbuf *txom);
 int ble_att_clt_tx_prep_write(uint16_t conn_handle,
                               const struct ble_att_prep_write_cmd *req,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index ac6fa55..3aea795 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -2055,7 +2055,7 @@ ble_att_svr_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 
     ble_att_write_req_parse((*rxom)->om_data, (*rxom)->om_len, &req);
     BLE_ATT_LOG_CMD(0, "write req", conn_handle,
-                    ble_att_write_cmd_log, &req);
+                    ble_att_write_req_log, &req);
 
     err_handle = req.bawq_handle;
 
@@ -2105,7 +2105,7 @@ ble_att_svr_rx_write_no_rsp(uint16_t conn_handle, struct os_mbuf **rxom)
 
     ble_att_write_cmd_parse((*rxom)->om_data, (*rxom)->om_len, &req);
     BLE_ATT_LOG_CMD(0, "write cmd", conn_handle,
-                    ble_att_write_cmd_log, &req);
+                    ble_att_write_req_log, &req);
 
     /* Strip the request base from the front of the mbuf. */
     os_mbuf_adj(*rxom, BLE_ATT_WRITE_REQ_BASE_SZ);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/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 6aa8bb1..7e57433 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3560,15 +3560,13 @@ ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct ble_att_write_req req;
     int rc;
 
     STATS_INC(ble_gattc_stats, write_no_rsp);
 
     ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(txom), 0);
 
-    req.bawq_handle = attr_handle;
-    rc = ble_att_clt_tx_write_cmd(conn_handle, &req, txom);
+    rc = ble_att_clt_tx_write_cmd(conn_handle, attr_handle, txom);
     if (rc != 0) {
         STATS_INC(ble_gattc_stats, write);
     }
@@ -3692,7 +3690,6 @@ ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct ble_att_write_req req;
     struct ble_gattc_proc *proc;
     int rc;
 
@@ -3712,8 +3709,7 @@ ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
 
     ble_gattc_log_write(attr_handle, OS_MBUF_PKTLEN(txom), 1);
 
-    req.bawq_handle = attr_handle;
-    rc = ble_att_clt_tx_write_req(conn_handle, &req, txom);
+    rc = ble_att_clt_tx_write_req(conn_handle, attr_handle, txom);
     txom = NULL;
     if (rc != 0) {
         goto done;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a9549fdb/net/nimble/host/test/src/ble_att_clt_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_att_clt_test.c b/net/nimble/host/test/src/ble_att_clt_test.c
index dec4695..97a1a44 100644
--- a/net/nimble/host/test/src/ble_att_clt_test.c
+++ b/net/nimble/host/test/src/ble_att_clt_test.c
@@ -59,8 +59,7 @@ ble_att_clt_test_misc_verify_tx_write(uint16_t handle_id, void *value,
 }
 
 static void
-ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle,
-                                     struct ble_att_write_req *req,
+ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle, uint16_t handle,
                                      void *value, int value_len, int is_req)
 {
     struct os_mbuf *om;
@@ -68,9 +67,9 @@ ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle,
 
     om = ble_hs_test_util_om_from_flat(value, value_len);
     if (is_req) {
-        rc = ble_att_clt_tx_write_req(conn_handle, req, om);
+        rc = ble_att_clt_tx_write_req(conn_handle, handle, om);
     } else {
-        rc = ble_att_clt_tx_write_cmd(conn_handle, req, om);
+        rc = ble_att_clt_tx_write_cmd(conn_handle, handle, om);
     }
     TEST_ASSERT(rc == 0);
 }
@@ -167,7 +166,6 @@ TEST_CASE(ble_att_clt_test_rx_find_info)
 static void
 ble_att_clt_test_case_tx_write_req_or_cmd(int is_req)
 {
-    struct ble_att_write_req req;
     uint16_t conn_handle;
     uint8_t value300[500] = { 0 };
     uint8_t value5[5] = { 6, 7, 54, 34, 8 };
@@ -175,16 +173,14 @@ ble_att_clt_test_case_tx_write_req_or_cmd(int is_req)
     conn_handle = ble_att_clt_test_misc_init();
 
     /*** 5-byte write. */
-    req.bawq_handle = 0x1234;
-    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value5,
+    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, 0x1234, value5,
                                          sizeof value5, is_req);
     ble_hs_test_util_tx_all();
     ble_att_clt_test_misc_verify_tx_write(0x1234, value5, sizeof value5,
                                           is_req);
 
     /*** Overlong write; verify command truncated to ATT MTU. */
-    req.bawq_handle = 0xab83;
-    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value300,
+    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, 0xab83, value300,
                                          sizeof value300, is_req);
     ble_hs_test_util_tx_all();
     ble_att_clt_test_misc_verify_tx_write(0xab83, value300,