nimble/sm: Use packed structures for id addr info
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/b371c867 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b371c867 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b371c867 Branch: refs/heads/develop Commit: b371c867b1bec20ebb421d39b3116b73ab73cdda Parents: b9abdc4 Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:38 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:15 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 40 ++++++++++++++---------- net/nimble/host/src/ble_sm_cmd.c | 4 +-- net/nimble/host/src/ble_sm_priv.h | 3 +- net/nimble/host/test/src/ble_sm_test_util.c | 6 ++-- 4 files changed, 30 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b371c867/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 74ecc39..6ca3ef4 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -1718,7 +1718,7 @@ static void ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg) { - struct ble_sm_id_addr_info addr_info; + struct ble_sm_id_addr_info *addr_info; struct ble_hs_conn_addrs addrs; struct ble_sm_sign_info sign_info; struct ble_sm_master_id *master_id; @@ -1808,20 +1808,28 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, } /* Send identity address information. */ - conn = ble_hs_conn_find_assert(proc->conn_handle); - - ble_hs_conn_addrs(conn, &addrs); - addr_info.addr_type = addrs.our_id_addr_type; - memcpy(addr_info.bd_addr, addrs.our_id_addr, 6); - rc = ble_sm_id_addr_info_tx(proc->conn_handle, &addr_info); - if (rc != 0) { + addr_info = ble_sm_cmd_get(BLE_SM_OP_IDENTITY_ADDR_INFO, + sizeof(*addr_info), &txom); + if (!addr_info) { + rc = BLE_HS_ENOMEM; goto err; } + conn = ble_hs_conn_find_assert(proc->conn_handle); + ble_hs_conn_addrs(conn, &addrs); + + addr_info->addr_type = addrs.our_id_addr_type; + memcpy(addr_info->bd_addr, addrs.our_id_addr, 6); + proc->our_keys.addr_valid = 1; memcpy(proc->our_keys.irk, irk, 16); - proc->our_keys.addr_type = addr_info.addr_type; - memcpy(proc->our_keys.addr, addr_info.bd_addr, 6); + proc->our_keys.addr_type = addr_info->addr_type; + memcpy(proc->our_keys.addr, addr_info->bd_addr, 6); + + rc = ble_sm_tx(proc->conn_handle, txom); + if (rc != 0) { + goto err; + } } if (our_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) { @@ -1977,19 +1985,19 @@ static void ble_sm_id_addr_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_id_addr_info cmd; + struct ble_sm_id_addr_info *cmd; struct ble_sm_proc *proc; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_ID_ADDR_INFO_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_id_addr_info_parse((*om)->om_data, (*om)->om_len, &cmd); + cmd = (struct ble_sm_id_addr_info *)(*om)->om_data; BLE_SM_LOG_CMD(0, "id addr info", conn_handle, ble_sm_id_addr_info_log, - &cmd); + cmd); ble_hs_lock(); @@ -2000,8 +2008,8 @@ ble_sm_id_addr_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, } else { proc->rx_key_flags &= ~BLE_SM_KE_F_ADDR_INFO; proc->peer_keys.addr_valid = 1; - proc->peer_keys.addr_type = cmd.addr_type; - memcpy(proc->peer_keys.addr, cmd.bd_addr, 6); + proc->peer_keys.addr_type = cmd->addr_type; + memcpy(proc->peer_keys.addr, cmd->bd_addr, 6); ble_sm_key_rxed(proc, res); } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b371c867/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 626010c..a3b693c 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -493,7 +493,7 @@ ble_sm_id_addr_info_write(void *payload, int len, { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_ID_ADDR_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_id_addr_info)); u8ptr = payload; @@ -511,7 +511,7 @@ ble_sm_id_addr_info_tx(uint16_t conn_handle, struct ble_sm_id_addr_info *cmd) BLE_SM_LOG_CMD(1, "id addr info", conn_handle, ble_sm_id_addr_info_log, cmd); - rc = ble_sm_init_req(BLE_SM_ID_ADDR_INFO_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_id_addr_info), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b371c867/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 95ec4ff..45fd484 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -146,11 +146,10 @@ struct ble_sm_id_info { * | addr_type | 1 | * | address | 6 | */ -#define BLE_SM_ID_ADDR_INFO_SZ 7 struct ble_sm_id_addr_info { uint8_t addr_type; uint8_t bd_addr[6]; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b371c867/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 99f7c89..ad72efa 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -559,12 +559,12 @@ ble_sm_test_util_rx_id_addr_info(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_ID_ADDR_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_id_addr_info)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_ID_ADDR_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_id_addr_info); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -781,7 +781,7 @@ ble_sm_test_util_verify_tx_id_addr_info(struct ble_sm_id_addr_info *exp_cmd) ble_hs_test_util_tx_all(); om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_ADDR_INFO, - BLE_SM_ID_ADDR_INFO_SZ); + sizeof(struct ble_sm_id_addr_info)); ble_sm_id_addr_info_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(cmd.addr_type == exp_cmd->addr_type);
