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:47:18 UTC

[26/50] incubator-mynewt-core git commit: nimble/att: Use packed struct for receiving Read By Type Request

nimble/att: Use packed struct for receiving Read By Type Request

Use packed structure to map it to received mbuf instead of using
dedicated parsing function. Modern compilers generate effective code
in such case anyway.


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

Branch: refs/heads/master
Commit: e15ad929887f470e3bd39535666bb66a077fd4ac
Parents: f6800fc
Author: Szymon Janc <sz...@codecoup.pl>
Authored: Fri Mar 24 15:50:03 2017 +0100
Committer: Szymon Janc <sz...@codecoup.pl>
Committed: Mon Apr 10 11:31:32 2017 +0200

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_cmd_priv.h |  7 +++
 net/nimble/host/src/ble_att_svr.c      | 66 ++++++++++++++++-------------
 2 files changed, 44 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e15ad929/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 4a7e491..c0bef79 100644
--- a/net/nimble/host/src/ble_att_cmd_priv.h
+++ b/net/nimble/host/src/ble_att_cmd_priv.h
@@ -151,8 +151,15 @@ struct ble_att_read_type_req {
  * | Attribute Data List                | 2 to (ATT_MTU-2)  |
  */
 #define BLE_ATT_READ_TYPE_RSP_BASE_SZ       2
+
+struct ble_att_attr_data_list {
+    uint16_t handle;
+    uint8_t value[0];
+} __attribute__((packed));
+
 struct ble_att_read_type_rsp {
     uint8_t batp_length;
+    struct ble_att_attr_data_list batp_list[0];
 } __attribute__((packed));
 
 #define BLE_ATT_READ_TYPE_ADATA_BASE_SZ     2

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e15ad929/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 8a6acc3..fffb9dd 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -1293,20 +1293,20 @@ done:
 
 static int
 ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
-                                struct ble_att_read_type_req *req,
+                                uint16_t start_handle, uint16_t end_handle,
                                 const ble_uuid_t *uuid,
                                 struct os_mbuf **rxom,
                                 struct os_mbuf **out_txom,
                                 uint8_t *att_err,
                                 uint16_t *err_handle)
 {
-    struct ble_att_read_type_rsp rsp;
+    struct ble_att_attr_data_list *data;
+    struct ble_att_read_type_rsp *rsp;
     struct ble_att_svr_entry *entry;
     struct os_mbuf *txom;
     uint16_t attr_len;
     uint16_t mtu;
     uint8_t buf[19];
-    uint8_t *dptr;
     int entry_written;
     int txomlen;
     int prev_attr_len;
@@ -1314,7 +1314,7 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
 
     *att_err = 0;    /* Silence unnecessary warning. */
 
-    *err_handle = req->batq_start_handle;
+    *err_handle = start_handle;
     entry_written = 0;
     prev_attr_len = 0;
 
@@ -1326,8 +1326,9 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
     /* Allocate space for the respose base, but don't fill in the fields.  They
      * get filled in at the end, when we know the value of the length field.
      */
-    dptr = os_mbuf_extend(txom, BLE_ATT_READ_TYPE_RSP_BASE_SZ);
-    if (dptr == NULL) {
+
+    rsp = ble_att_cmd_prepare(BLE_ATT_OP_READ_TYPE_RSP, sizeof(*rsp), txom);
+    if (rsp == NULL) {
         *att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
         *err_handle = 0;
         rc = BLE_HS_ENOMEM;
@@ -1339,13 +1340,13 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
     /* Find all matching attributes, writing a record for each. */
     entry = NULL;
     while (1) {
-        entry = ble_att_svr_find_by_uuid(entry, uuid, req->batq_end_handle);
+        entry = ble_att_svr_find_by_uuid(entry, uuid, end_handle);
         if (entry == NULL) {
             rc = BLE_HS_ENOENT;
             break;
         }
 
-        if (entry->ha_handle_id >= req->batq_start_handle) {
+        if (entry->ha_handle_id >= start_handle) {
             rc = ble_att_svr_read_flat(conn_handle, entry, 0, sizeof buf, buf,
                                        &attr_len, att_err);
             if (rc != 0) {
@@ -1368,16 +1369,16 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
                 break;
             }
 
-            dptr = os_mbuf_extend(txom, 2 + attr_len);
-            if (dptr == NULL) {
+            data = os_mbuf_extend(txom, 2 + attr_len);
+            if (data == NULL) {
                 *att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
                 *err_handle = entry->ha_handle_id;
                 rc = BLE_HS_ENOMEM;
                 goto done;
             }
 
-            put_le16(dptr + 0, entry->ha_handle_id);
-            memcpy(dptr + 2, buf, attr_len);
+            data->handle = htole16(entry->ha_handle_id);
+            memcpy(data->value, buf, attr_len);
             entry_written = 1;
         }
     }
@@ -1397,10 +1398,9 @@ done:
         *att_err = 0;
 
         /* Fill the response base. */
-        rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ + prev_attr_len;
-        ble_att_read_type_rsp_write(txom->om_data, txom->om_len, &rsp);
+        rsp->batp_length = htole16(sizeof(*data) + prev_attr_len);
         BLE_ATT_LOG_CMD(1, "read type rsp", conn_handle,
-                        ble_att_read_type_rsp_log, &rsp);
+                        ble_att_read_type_rsp_log, rsp);
     }
 
     *out_txom = txom;
@@ -1415,7 +1415,8 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
     return BLE_HS_ENOTSUP;
 #endif
 
-    struct ble_att_read_type_req req;
+    struct ble_att_read_type_req *req;
+    uint16_t start_handle, end_handle;
     struct os_mbuf *txom;
     uint16_t err_handle;
     uint16_t pktlen;
@@ -1423,14 +1424,18 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
     uint8_t att_err;
     int rc;
 
+    /* TODO move this to common part
+     * Strip L2CAP ATT header from the front of the mbuf.
+     */
+    os_mbuf_adj(*rxom, 1);
+
     /* Initialize some values in case of early error. */
     txom = NULL;
 
     pktlen = OS_MBUF_PKTLEN(*rxom);
-    if (pktlen != BLE_ATT_READ_TYPE_REQ_SZ_16 &&
-        pktlen != BLE_ATT_READ_TYPE_REQ_SZ_128) {
-
-        /* Malformed packet; discard. */
+    if (pktlen != sizeof(*req) + 2 && pktlen != sizeof(*req) + 16) {
+        /* Malformed packet */
+        err_handle = 0;
         rc = BLE_HS_EBADDATA;
         goto done;
     }
@@ -1441,21 +1446,23 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
         goto done;
     }
 
-    ble_att_read_type_req_parse((*rxom)->om_data, (*rxom)->om_len, &req);
-    BLE_ATT_LOG_CMD(0, "read type req", conn_handle, ble_att_read_type_req_log,
-                    &req);
+    req = (struct ble_att_read_type_req *)(*rxom)->om_data;
 
+    BLE_ATT_LOG_CMD(0, "read type req", conn_handle, ble_att_read_type_req_log,
+                    req);
 
-    if (req.batq_start_handle > req.batq_end_handle ||
-        req.batq_start_handle == 0) {
+    start_handle = le16toh(req->batq_start_handle);
+    end_handle = le16toh(req->batq_end_handle);
 
+    if (start_handle > end_handle || start_handle == 0) {
         att_err = BLE_ATT_ERR_INVALID_HANDLE;
-        err_handle = req.batq_start_handle;
+        err_handle = start_handle;
         rc = BLE_HS_EBADDATA;
         goto done;
     }
 
-    rc = ble_uuid_init_from_mbuf(&uuid, *rxom, 5, (*rxom)->om_len - 5);
+    rc = ble_uuid_init_from_mbuf(&uuid, *rxom, sizeof(*req),
+                                 pktlen - sizeof(*req));
     if (rc != 0) {
         att_err = BLE_ATT_ERR_INVALID_PDU;
         err_handle = 0;
@@ -1463,8 +1470,9 @@ ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
         goto done;
     }
 
-    rc = ble_att_svr_build_read_type_rsp(conn_handle, &req, &uuid.u,
-                                         rxom, &txom, &att_err, &err_handle);
+    rc = ble_att_svr_build_read_type_rsp(conn_handle, start_handle, end_handle,
+                                         &uuid.u, rxom, &txom, &att_err,
+                                         &err_handle);
     if (rc != 0) {
         goto done;
     }