nimble/sm: Use packed structures for random

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

Branch: refs/heads/develop
Commit: 09431b86782a37754a5d66cf7f7584d2ec2c51ee
Parents: bc0e759
Author: Szymon Janc <[email protected]>
Authored: Tue Jan 17 19:24:30 2017 +0100
Committer: Szymon Janc <[email protected]>
Committed: Wed Jan 25 15:44:15 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_sm.c                | 11 ++++++-----
 net/nimble/host/src/ble_sm_cmd.c            |  6 +++---
 net/nimble/host/src/ble_sm_lgcy.c           | 15 ++++++++++++---
 net/nimble/host/src/ble_sm_priv.h           |  3 +--
 net/nimble/host/src/ble_sm_sc.c             | 15 ++++++++++++---
 net/nimble/host/test/src/ble_sm_test_util.c |  6 +++---
 6 files changed, 37 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 974df60..b57edfc 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -1257,25 +1257,26 @@ static void
 ble_sm_random_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
                  struct ble_sm_result *res)
 {
-    struct ble_sm_pair_random cmd;
+    struct ble_sm_pair_random *cmd;
     struct ble_sm_proc *proc;
 
-    res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_PAIR_RANDOM_SZ);
+    res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd));
     if (res->app_status != 0) {
         res->sm_err = BLE_SM_ERR_UNSPECIFIED;
         res->enc_cb = 1;
         return;
     }
 
-    ble_sm_pair_random_parse((*om)->om_data, (*om)->om_len, &cmd);
-    BLE_SM_LOG_CMD(0, "random", conn_handle, ble_sm_pair_random_log, &cmd);
+    cmd = (struct ble_sm_pair_random *)(*om)->om_data;
+
+    BLE_SM_LOG_CMD(0, "random", conn_handle, ble_sm_pair_random_log, cmd);
 
     ble_hs_lock();
     proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_RANDOM, -1, NULL);
     if (proc == NULL) {
         res->app_status = BLE_HS_ENOENT;
     } else {
-        memcpy(ble_sm_peer_pair_rand(proc), cmd.value, 16);
+        memcpy(ble_sm_peer_pair_rand(proc), cmd->value, 16);
 
         if (proc->flags & BLE_SM_PROC_F_SC) {
             ble_sm_sc_random_rx(proc, res);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/src/ble_sm_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c
index 6ec7def..12ffe09 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -215,7 +215,7 @@ void
 ble_sm_pair_random_parse(void *payload, int len,
                          struct ble_sm_pair_random *cmd)
 {
-    BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_RANDOM_SZ);
+    BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_pair_random));
     memcpy(cmd->value, payload, sizeof cmd->value);
 }
 
@@ -226,7 +226,7 @@ ble_sm_pair_random_write(void *payload, int len,
     uint8_t *u8ptr;
 
     BLE_HS_DBG_ASSERT(len >=
-                      sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ);
+                      sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_pair_random));
 
     u8ptr = payload;
 
@@ -240,7 +240,7 @@ ble_sm_pair_random_tx(uint16_t conn_handle, struct 
ble_sm_pair_random *cmd)
     struct os_mbuf *txom;
     int rc;
 
-    rc = ble_sm_init_req(BLE_SM_PAIR_RANDOM_SZ, &txom);
+    rc = ble_sm_init_req(sizeof(struct ble_sm_pair_random), &txom);
     if (rc != 0) {
         return BLE_HS_ENOMEM;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/src/ble_sm_lgcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_lgcy.c 
b/net/nimble/host/src/ble_sm_lgcy.c
index d94d945..1447028 100644
--- a/net/nimble/host/src/ble_sm_lgcy.c
+++ b/net/nimble/host/src/ble_sm_lgcy.c
@@ -177,12 +177,21 @@ ble_sm_gen_stk(struct ble_sm_proc *proc)
 void
 ble_sm_lgcy_random_exec(struct ble_sm_proc *proc, struct ble_sm_result *res)
 {
-    struct ble_sm_pair_random cmd;
+    struct ble_sm_pair_random *cmd;
+    struct os_mbuf *txom;
     int rc;
 
-    memcpy(cmd.value, ble_sm_our_pair_rand(proc), 16);
+    cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_RANDOM, sizeof(*cmd), &txom);
+    if (cmd == NULL) {
+        res->app_status = BLE_HS_ENOMEM;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        return;
+    }
+
+    memcpy(cmd->value, ble_sm_our_pair_rand(proc), 16);
 
-    rc = ble_sm_pair_random_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_tx(proc->conn_handle, txom);
     if (rc != 0) {
         res->app_status = rc;
         res->enc_cb = 1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h 
b/net/nimble/host/src/ble_sm_priv.h
index e9ecd90..e20ce1a 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -93,10 +93,9 @@ struct ble_sm_pair_confirm {
  * | (Code=0x04)                        | 1                 |
  * | Random Value                       | 16                |
  */
-#define BLE_SM_PAIR_RANDOM_SZ       16
 struct ble_sm_pair_random {
     uint8_t value[16];
-};
+} __attribute__((packed));
 
 /**
  * | Parameter                          | Size (octets)     |

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/src/ble_sm_sc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c
index e440a85..a88ffc2 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -371,13 +371,22 @@ ble_sm_sc_random_advance(struct ble_sm_proc *proc)
 void
 ble_sm_sc_random_exec(struct ble_sm_proc *proc, struct ble_sm_result *res)
 {
-    struct ble_sm_pair_random cmd;
+    struct ble_sm_pair_random *cmd;
+    struct os_mbuf *txom;
     uint8_t ioact;
     int rc;
 
-    memcpy(cmd.value, ble_sm_our_pair_rand(proc), 16);
+    cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_RANDOM, sizeof(*cmd), &txom);
+    if (cmd == NULL) {
+        rc = BLE_HS_ENOMEM;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        return;
+    }
 
-    rc = ble_sm_pair_random_tx(proc->conn_handle, &cmd);
+    memcpy(cmd->value, ble_sm_our_pair_rand(proc), 16);
+
+    rc = ble_sm_tx(proc->conn_handle, txom);
     if (rc != 0) {
         res->app_status = rc;
         res->enc_cb = 1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/09431b86/net/nimble/host/test/src/ble_sm_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_test_util.c 
b/net/nimble/host/test/src/ble_sm_test_util.c
index 2e3eecd..4ebced2 100644
--- a/net/nimble/host/test/src/ble_sm_test_util.c
+++ b/net/nimble/host/test/src/ble_sm_test_util.c
@@ -351,12 +351,12 @@ ble_sm_test_util_rx_random(uint16_t conn_handle,
 
     hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
         2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ);
+        BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_pair_random));
 
     om = ble_hs_mbuf_l2cap_pkt();
     TEST_ASSERT_FATAL(om != NULL);
 
-    payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ;
+    payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_pair_random);
 
     v = os_mbuf_extend(om, payload_len);
     TEST_ASSERT_FATAL(v != NULL);
@@ -679,7 +679,7 @@ ble_sm_test_util_verify_tx_pair_random(
     struct os_mbuf *om;
 
     om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_RANDOM,
-                                        BLE_SM_PAIR_RANDOM_SZ);
+                                        sizeof(struct ble_sm_pair_random));
     ble_sm_pair_random_parse(om->om_data, om->om_len, &cmd);
 
     TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);

Reply via email to