You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2016/08/11 21:27:01 UTC

[01/50] [abbrv] incubator-mynewt-core git commit: olimex-e407 - hal_uart_close().

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/phyrx_no_mbuf a621643fd -> 6a5c8ae64


olimex-e407 - hal_uart_close().


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 2a569b8bb759e154d006a93b32ef00f8967f5d89
Parents: d92d63b
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 2 12:40:46 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 hw/mcu/stm/stm32f4xx/src/hal_uart.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2a569b8b/hw/mcu/stm/stm32f4xx/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_uart.c b/hw/mcu/stm/stm32f4xx/src/hal_uart.c
index 3b2868f..43d83eb 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_uart.c
+++ b/hw/mcu/stm/stm32f4xx/src/hal_uart.c
@@ -371,3 +371,19 @@ hal_uart_config(int port, int32_t baudrate, uint8_t databits, uint8_t stopbits,
 
     return 0;
 }
+
+int
+hal_uart_close(int port)
+{
+    struct hal_uart *u;
+
+    if (port >= UART_CNT) {
+        return -1;
+    }
+    u = &uarts[port];
+
+    u->u_open = 0;
+    u->u_regs->CR1 = 0;
+
+    return 0;
+}


[31/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Add missing break statement.

Posted by we...@apache.org.
BLE Host - Add missing break statement.

This fixes a minor bug - the sending of notifications / indications
would cause the tx and rx queues to be processed unnecessarily.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 2c3618de6307f44c5c4c9943f025019e92af6e93
Parents: 768e635
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:50:21 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs.c | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2c3618de/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 02db879..231dce3 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -373,6 +373,7 @@ ble_hs_event_handle(void *unused)
         case BLE_HS_EVENT_TX_NOTIFICATIONS:
             BLE_HS_DBG_ASSERT(ev == &ble_hs_event_tx_notifications);
             ble_gatts_tx_notifications();
+            break;
 
         case OS_EVENT_T_MQUEUE_DATA:
             ble_hs_process_tx_data_queue();


[39/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Set state before tx HCI enable.

Posted by we...@apache.org.
BLE Host - Set state before tx HCI enable.

Prior to this change, for advertising, connecting, and scanning, the
host didn't update its GAP state until after all HCI commands were sent
to the controller.  This creates a race condition, because the
controller may act on the enable command before the host GAP state has
been updated.

For example, as soon as the host tells the controlller to
start scanning, the controller may send an advertisement report event.
If the host has not updated its state yet, the advertising report gets
dropped.

Now, the host updates its state before sending the enable command to the
controller.  Unfortunately, this means the host needs to undo the state
update if the enable command fails to be sent.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: b607ac065a18aacc7f642e0f3ea9a22f24d69034
Parents: 34ee3db
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 16:36:02 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b607ac06/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 49d678e..9717125 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1733,8 +1733,11 @@ ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
         }
     }
 
+    ble_gap_slave.op = BLE_GAP_OP_S_ADV;
+
     rc = ble_gap_adv_enable_tx(1);
     if (rc != 0) {
+        ble_gap_slave_reset_state();
         goto done;
     }
 
@@ -1742,7 +1745,6 @@ ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
         ble_gap_slave_set_timer(duration_ticks);
     }
 
-    ble_gap_slave.op = BLE_GAP_OP_S_ADV;
     rc = 0;
 
 done:
@@ -2094,8 +2096,11 @@ ble_gap_disc(uint8_t own_addr_type, int32_t duration_ms,
         goto done;
     }
 
+    ble_gap_master.op = BLE_GAP_OP_M_DISC;
+
     rc = ble_gap_disc_enable_tx(1, params.filter_duplicates);
     if (rc != 0) {
+        ble_gap_master_reset_state();
         goto done;
     }
 
@@ -2103,8 +2108,6 @@ ble_gap_disc(uint8_t own_addr_type, int32_t duration_ms,
         ble_gap_master_set_timer(duration_ticks);
     }
 
-    ble_gap_master.op = BLE_GAP_OP_M_DISC;
-
     rc = 0;
 
 done:
@@ -2292,9 +2295,12 @@ ble_gap_connect(uint8_t own_addr_type,
     ble_gap_master.conn.using_wl = peer_addr_type == BLE_GAP_ADDR_TYPE_WL;
     ble_gap_master.conn.our_addr_type = own_addr_type;
 
+    ble_gap_master.op = BLE_GAP_OP_M_CONN;
+
     rc = ble_gap_conn_create_tx(own_addr_type, peer_addr_type, peer_addr,
                                 conn_params);
     if (rc != 0) {
+        ble_gap_master_reset_state();
         goto done;
     }
 
@@ -2302,8 +2308,6 @@ ble_gap_connect(uint8_t own_addr_type,
         ble_gap_master_set_timer(duration_ticks);
     }
 
-    ble_gap_master.op = BLE_GAP_OP_M_CONN;
-
     rc = 0;
 
 done:


[33/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Check conn state once before proc.

Posted by we...@apache.org.
BLE Host - Check conn state once before proc.

Much of the host code repeatedly checks if a peer is connected while it
processes incoming data packets.  This is not necessary, because:

1. The peer's connected state is checked at the top level before the
   packet is processed.
2. Peer removal and packet processing is done in the same task.

This commit removes a bunch of redundant connectivity checks.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 8e95fbe714876dbd5a6f95593d995bdf9e0749fb
Parents: 9f944af
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 10 13:45:12 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c          |   9 +-
 net/nimble/host/src/ble_att_clt.c      |  22 ++--
 net/nimble/host/src/ble_att_priv.h     |   4 +-
 net/nimble/host/src/ble_att_svr.c      | 179 +++++++++-------------------
 net/nimble/host/src/ble_gattc.c        |  10 +-
 net/nimble/host/src/ble_hs_conn.c      |  11 ++
 net/nimble/host/src/ble_hs_conn_priv.h |   1 +
 net/nimble/host/src/ble_hs_misc.c      |   6 +-
 net/nimble/host/src/ble_hs_priv.h      |   6 +-
 net/nimble/host/src/ble_l2cap_sig.c    |  45 +++----
 net/nimble/host/src/ble_sm.c           |  68 +++--------
 net/nimble/host/src/ble_sm_cmd.c       |   9 +-
 net/nimble/host/src/ble_sm_lgcy.c      |   7 +-
 net/nimble/host/src/ble_sm_priv.h      |   8 +-
 net/nimble/host/src/ble_sm_sc.c        |  43 ++-----
 15 files changed, 143 insertions(+), 285 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_att.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att.c b/net/nimble/host/src/ble_att.c
index 1743a4f..d5160d0 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -143,15 +143,12 @@ ble_att_rx_dispatch_entry_find(uint8_t op)
     return NULL;
 }
 
-int
+void
 ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
                        struct ble_l2cap_chan **out_chan)
 {
-    int rc;
-
-    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_ATT,
-                                         out_conn, out_chan);
-    return rc;
+    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_ATT,
+                                    out_conn, out_chan);
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 87e2f44..ed36b7f 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -70,11 +70,9 @@ ble_att_clt_tx_req(uint16_t conn_handle, struct os_mbuf *txom)
 
     ble_hs_lock();
 
-    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    if (rc == 0) {
-        ble_att_truncate_to_mtu(chan, txom);
-        rc = ble_l2cap_tx(conn, chan, txom);
-    }
+    ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    ble_att_truncate_to_mtu(chan, txom);
+    rc = ble_l2cap_tx(conn, chan, txom);
 
     ble_hs_unlock();
 
@@ -139,10 +137,8 @@ ble_att_clt_tx_mtu(uint16_t conn_handle, const struct ble_att_mtu_cmd *req)
 
     ble_hs_lock();
 
-    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
-    if (rc == 0) {
-        chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
-    }
+    ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
 
     ble_hs_unlock();
 
@@ -166,11 +162,9 @@ ble_att_clt_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom)
 
         ble_hs_lock();
 
-        rc = ble_att_conn_chan_find(conn_handle, NULL, &chan);
-        if (rc == 0) {
-            ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
-            mtu = ble_l2cap_chan_mtu(chan);
-        }
+        ble_att_conn_chan_find(conn_handle, NULL, &chan);
+        ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
+        mtu = ble_l2cap_chan_mtu(chan);
 
         ble_hs_unlock();
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 edf9dce..ee9a9e9 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -158,8 +158,8 @@ SLIST_HEAD(ble_att_clt_entry_list, ble_att_clt_entry);
 /*** @gen */
 
 struct ble_l2cap_chan *ble_att_create_chan(void);
-int ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
-                           struct ble_l2cap_chan **out_chan);
+void ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
+                            struct ble_l2cap_chan **out_chan);
 void ble_att_inc_tx_stat(uint8_t att_op);
 void ble_att_truncate_to_mtu(const struct ble_l2cap_chan *att_chan,
                              struct os_mbuf *txom);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 93c57ff..f224094 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -216,24 +216,18 @@ ble_att_svr_pullup_req_base(struct os_mbuf **om, int base_len,
     return rc;
 }
 
-static int
+static void
 ble_att_svr_get_sec_state(uint16_t conn_handle,
                           struct ble_gap_sec_state *out_sec_state)
 {
     struct ble_hs_conn *conn;
 
     ble_hs_lock();
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn != NULL) {
-        *out_sec_state = conn->bhc_sec_state;
-    }
-    ble_hs_unlock();
 
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    } else {
-        return 0;
-    }
+    conn = ble_hs_conn_find_assert(conn_handle);
+    *out_sec_state = conn->bhc_sec_state;
+
+    ble_hs_unlock();
 }
 
 static int
@@ -245,7 +239,6 @@ ble_att_svr_check_perms(uint16_t conn_handle, int is_read,
     int author;
     int authen;
     int enc;
-    int rc;
 
     if (is_read) {
         if (!(entry->ha_flags & BLE_ATT_F_READ)) {
@@ -272,13 +265,7 @@ ble_att_svr_check_perms(uint16_t conn_handle, int is_read,
         return 0;
     }
 
-    rc = ble_att_svr_get_sec_state(conn_handle, &sec_state);
-    if (rc != 0) {
-        /* Peer no longer connected. */
-        *out_att_err = 0;
-        return rc;
-    }
-
+    ble_att_svr_get_sec_state(conn_handle, &sec_state);
     if (enc && !sec_state.encrypted) {
         /* XXX: Check security database; if required key present, respond with
          * insufficient encryption error code.
@@ -542,10 +529,10 @@ err:
 /**
  * Transmits a response or error message over the specified connection.
  *
- * The specified rc value controls what gets sent as follows:
+ * The specified rc and err_status values control what gets sent as follows:
  *     o If rc == 0: tx an affirmative response.
- *     o If rc == BLE_HS_ENOTCONN: tx nothing.
- *     o Else: tx an error response.
+ *     o Else if err_status != 0: tx an error response.
+ *     o Else: tx nothing.
  *
  * In addition, if transmission of an affirmative response fails, an error is
  * sent instead.
@@ -570,10 +557,7 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
     struct ble_hs_conn *conn;
     int do_tx;
 
-    if (rc == BLE_HS_ENOTCONN) {
-        /* No connection; tx is not possible. */
-        do_tx = 0;
-    } else if (rc != 0 && err_status == 0) {
+    if (rc != 0 && err_status == 0) {
         /* Processing failed, but err_status of 0 means don't send error. */
         do_tx = 0;
     } else {
@@ -584,35 +568,33 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
         ble_hs_lock();
 
         ble_att_conn_chan_find(conn_handle, &conn, &chan);
-        if (chan == NULL) {
-            rc = BLE_HS_ENOTCONN;
-        } else {
-            if (rc == 0) {
-                BLE_HS_DBG_ASSERT(om != NULL);
+        BLE_HS_DBG_ASSERT(chan != NULL);
 
-                ble_att_inc_tx_stat(om->om_data[0]);
-                ble_att_truncate_to_mtu(chan, om);
-                rc = ble_l2cap_tx(conn, chan, om);
-                om = NULL;
-                if (rc != 0) {
-                    err_status = BLE_ATT_ERR_UNLIKELY;
-                }
-            }
+        if (rc == 0) {
+            BLE_HS_DBG_ASSERT(om != NULL);
 
+            ble_att_inc_tx_stat(om->om_data[0]);
+            ble_att_truncate_to_mtu(chan, om);
+            rc = ble_l2cap_tx(conn, chan, om);
+            om = NULL;
             if (rc != 0) {
-                STATS_INC(ble_att_stats, error_rsp_tx);
+                err_status = BLE_ATT_ERR_UNLIKELY;
+            }
+        }
 
-                /* Reuse om for error response. */
-                if (om == NULL) {
-                    om = ble_hs_mbuf_l2cap_pkt();
-                } else {
-                    os_mbuf_adj(om, OS_MBUF_PKTLEN(om));
-                }
-                if (om != NULL) {
-                    ble_att_svr_tx_error_rsp(conn, chan, om, att_op,
-                                             err_handle, err_status);
-                    om = NULL;
-                }
+        if (rc != 0) {
+            STATS_INC(ble_att_stats, error_rsp_tx);
+
+            /* Reuse om for error response. */
+            if (om == NULL) {
+                om = ble_hs_mbuf_l2cap_pkt();
+            } else {
+                os_mbuf_adj(om, OS_MBUF_PKTLEN(om));
+            }
+            if (om != NULL) {
+                ble_att_svr_tx_error_rsp(conn, chan, om, att_op,
+                                         err_handle, err_status);
+                om = NULL;
             }
         }
 
@@ -640,16 +622,10 @@ ble_att_svr_build_mtu_rsp(uint16_t conn_handle, struct os_mbuf **out_txom,
     txom = NULL;
 
     ble_hs_lock();
-    rc = ble_att_conn_chan_find(conn_handle, NULL, &chan);
-    if (rc == 0) {
-        mtu = chan->blc_my_mtu;
-    }
+    ble_att_conn_chan_find(conn_handle, NULL, &chan);
+    mtu = chan->blc_my_mtu;
     ble_hs_unlock();
 
-    if (rc != 0) {
-        goto done;
-    }
-
     txom = ble_hs_mbuf_l2cap_pkt();
     if (txom == NULL) {
         *att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
@@ -708,11 +684,11 @@ done:
                             att_err, 0);
     if (rc == 0) {
         ble_hs_lock();
+
         ble_att_conn_chan_find(conn_handle, &conn, &chan);
-        if (chan != NULL) {
-            ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
-            chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
-        }
+        ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
+        chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+
         ble_hs_unlock();
     }
     return rc;
@@ -830,10 +806,6 @@ ble_att_svr_build_find_info_rsp(uint16_t conn_handle,
     txom = NULL;
 
     mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
-    }
 
     txom = ble_hs_mbuf_l2cap_pkt();
     if (txom == NULL) {
@@ -1170,10 +1142,6 @@ ble_att_svr_build_find_type_value_rsp(uint16_t conn_handle,
 
     /* Write the variable length Information Data field. */
     mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
-    }
 
     rc = ble_att_svr_fill_type_value(conn_handle, req, rxom, txom, mtu,
                                      out_att_err);
@@ -1274,9 +1242,6 @@ ble_att_svr_build_read_type_rsp(uint16_t conn_handle,
     prev_attr_len = 0;
 
     mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        return BLE_HS_ENOTCONN;
-    }
 
     txom = ble_hs_mbuf_l2cap_pkt();
     if (txom == NULL) {
@@ -1519,7 +1484,6 @@ ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
     struct ble_att_read_blob_req req;
     struct os_mbuf *txom;
     uint16_t err_handle;
-    uint16_t mtu;
     uint8_t *dptr;
     uint8_t att_err;
     int rc;
@@ -1529,12 +1493,6 @@ ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
     att_err = 0;
     err_handle = 0;
 
-    mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
-    }
-
     rc = ble_att_svr_pullup_req_base(rxom, BLE_ATT_READ_BLOB_REQ_SZ, &att_err);
     if (rc != 0) {
         err_handle = 0;
@@ -1590,13 +1548,7 @@ ble_att_svr_build_read_mult_rsp(uint16_t conn_handle,
     uint8_t *dptr;
     int rc;
 
-    txom = NULL;
-
     mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
-    }
 
     txom = ble_hs_mbuf_l2cap_pkt();
     if (txom == NULL) {
@@ -1801,13 +1753,7 @@ ble_att_svr_build_read_group_type_rsp(uint16_t conn_handle,
     *att_err = 0;
     *err_handle = req->bagq_start_handle;
 
-    txom = NULL;
-
     mtu = ble_att_mtu(conn_handle);
-    if (mtu == 0) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
-    }
 
     txom = ble_hs_mbuf_l2cap_pkt();
     if (txom == NULL) {
@@ -2373,11 +2319,7 @@ ble_att_svr_insert_prep_entry(uint16_t conn_handle,
     struct ble_hs_conn *conn;
     int rc;
 
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        *out_att_err = 0;
-        return BLE_HS_ENOTCONN;
-    }
+    conn = ble_hs_conn_find_assert(conn_handle);
 
     prep_entry = ble_att_svr_prep_alloc();
     if (prep_entry == NULL) {
@@ -2566,33 +2508,28 @@ ble_att_svr_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 done:
     if (rc == 0) {
         ble_hs_lock();
-        conn = ble_hs_conn_find(conn_handle);
-        if (conn == NULL) {
-            rc = BLE_HS_ENOTCONN;
-        } else {
-            /* Extract the list of prepared writes from the connection so
-             * that they can be processed after the mutex is unlocked.  They
-             * aren't processed now because attribute writes involve executing
-             * an application callback.
-             */
-            prep_list = conn->bhc_att_svr.basc_prep_list;
-            SLIST_INIT(&conn->bhc_att_svr.basc_prep_list);
-        }
+        conn = ble_hs_conn_find_assert(conn_handle);
+
+        /* Extract the list of prepared writes from the connection so
+         * that they can be processed after the mutex is unlocked.  They
+         * aren't processed now because attribute writes involve executing
+         * an application callback.
+         */
+        prep_list = conn->bhc_att_svr.basc_prep_list;
+        SLIST_INIT(&conn->bhc_att_svr.basc_prep_list);
         ble_hs_unlock();
 
-        if (conn != NULL) {
-            if (req.baeq_flags & BLE_ATT_EXEC_WRITE_F_CONFIRM) {
-                /* Perform attribute writes. */
-                att_err = ble_att_svr_prep_write(conn_handle, &prep_list,
-                                                 &err_handle);
-                if (att_err != 0) {
-                    rc = BLE_HS_EAPP;
-                }
+        if (req.baeq_flags & BLE_ATT_EXEC_WRITE_F_CONFIRM) {
+            /* Perform attribute writes. */
+            att_err = ble_att_svr_prep_write(conn_handle, &prep_list,
+                                             &err_handle);
+            if (att_err != 0) {
+                rc = BLE_HS_EAPP;
             }
-
-            /* Free the prep entries. */
-            ble_att_svr_prep_clear(&prep_list);
         }
+
+        /* Free the prep entries. */
+        ble_att_svr_prep_clear(&prep_list);
     }
 
     rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_EXEC_WRITE_REQ,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 43d2134..3dee679 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -1042,16 +1042,10 @@ ble_gattc_exchange_mtu(uint16_t conn_handle, ble_gatt_mtu_fn *cb, void *cb_arg)
     ble_gattc_log_proc_init("exchange mtu\n");
 
     ble_hs_lock();
-    rc = ble_att_conn_chan_find(proc->conn_handle, &conn, &chan);
-    if (rc == 0) {
-        req.bamc_mtu = chan->blc_my_mtu;
-    }
+    ble_att_conn_chan_find(proc->conn_handle, &conn, &chan);
+    req.bamc_mtu = chan->blc_my_mtu;
     ble_hs_unlock();
 
-    if (rc != 0) {
-        goto done;
-    }
-
     rc = ble_att_clt_tx_mtu(proc->conn_handle, &req);
     if (rc != 0) {
         goto done;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 76196ef..34d9590 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -245,6 +245,17 @@ ble_hs_conn_find(uint16_t conn_handle)
 }
 
 struct ble_hs_conn *
+ble_hs_conn_find_assert(uint16_t conn_handle)
+{
+    struct ble_hs_conn *conn;
+
+    conn = ble_hs_conn_find(conn_handle);
+    BLE_HS_DBG_ASSERT(conn != NULL);
+
+    return conn;
+}
+
+struct ble_hs_conn *
 ble_hs_conn_find_by_addr(uint8_t addr_type, uint8_t *addr)
 {
 #if !NIMBLE_OPT(CONNECT)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_hs_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn_priv.h b/net/nimble/host/src/ble_hs_conn_priv.h
index cdcb363..da8d2fc 100644
--- a/net/nimble/host/src/ble_hs_conn_priv.h
+++ b/net/nimble/host/src/ble_hs_conn_priv.h
@@ -79,6 +79,7 @@ void ble_hs_conn_free(struct ble_hs_conn *conn);
 void ble_hs_conn_insert(struct ble_hs_conn *conn);
 void ble_hs_conn_remove(struct ble_hs_conn *conn);
 struct ble_hs_conn *ble_hs_conn_find(uint16_t conn_handle);
+struct ble_hs_conn *ble_hs_conn_find_assert(uint16_t conn_handle);
 struct ble_hs_conn *ble_hs_conn_find_by_addr(uint8_t addr_type, uint8_t *addr);
 struct ble_hs_conn *ble_hs_conn_find_by_idx(int idx);
 int ble_hs_conn_exists(uint16_t conn_handle);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_hs_misc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_misc.c b/net/nimble/host/src/ble_hs_misc.c
index 216196b..649fc05 100644
--- a/net/nimble/host/src/ble_hs_misc.c
+++ b/net/nimble/host/src/ble_hs_misc.c
@@ -77,7 +77,7 @@ ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
     return rc;
 }
 
-int
+void
 ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
                                 struct ble_hs_conn **out_conn,
                                 struct ble_l2cap_chan **out_chan)
@@ -87,7 +87,7 @@ ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
     int rc;
 
     rc = ble_hs_misc_conn_chan_find(conn_handle, cid, &conn, &chan);
-    BLE_HS_DBG_ASSERT(conn == NULL || chan != NULL);
+    BLE_HS_DBG_ASSERT_EVAL(rc == 0);
 
     if (out_conn != NULL) {
         *out_conn = conn;
@@ -95,8 +95,6 @@ ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
     if (out_chan != NULL) {
         *out_chan = chan;
     }
-
-    return rc;
 }
 
 uint8_t

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_priv.h b/net/nimble/host/src/ble_hs_priv.h
index d61f0ba..6bebec9 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -91,9 +91,9 @@ int ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
 int ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
                                struct ble_hs_conn **out_conn,
                                struct ble_l2cap_chan **out_chan);
-int ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
-                                    struct ble_hs_conn **out_conn,
-                                    struct ble_l2cap_chan **out_chan);
+void ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
+                                     struct ble_hs_conn **out_conn,
+                                     struct ble_l2cap_chan **out_chan);
 uint8_t ble_hs_misc_addr_type_to_id(uint8_t addr_type);
 
 void ble_hs_cfg_init(struct ble_hs_cfg *cfg);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 4838067..a382b09 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -338,18 +338,16 @@ ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
 
     /* Send L2CAP response. */
     ble_hs_lock();
-    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
-                                         &conn, &chan);
-    if (rc == 0) {
-        if (!sig_err) {
-            rc = ble_l2cap_sig_update_rsp_tx(conn, chan, hdr->identifier,
-                                             l2cap_result);
-        } else {
-            ble_l2cap_sig_reject_tx(conn, chan, hdr->identifier,
-                                    BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD,
-                                    NULL, 0);
-            rc = BLE_HS_L2C_ERR(BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD);
-        }
+    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
+                                    &conn, &chan);
+    if (!sig_err) {
+        rc = ble_l2cap_sig_update_rsp_tx(conn, chan, hdr->identifier,
+                                         l2cap_result);
+    } else {
+        ble_l2cap_sig_reject_tx(conn, chan, hdr->identifier,
+                                BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD,
+                                NULL, 0);
+        rc = BLE_HS_L2C_ERR(BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD);
     }
     ble_hs_unlock();
 
@@ -421,11 +419,8 @@ ble_l2cap_sig_update(uint16_t conn_handle,
 
     ble_hs_lock();
 
-    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
-                                         &conn, &chan);
-    if (rc != 0) {
-        goto done;
-    }
+    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
+                                    &conn, &chan);
     if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
         /* Only the slave can initiate the L2CAP connection update
          * procedure.
@@ -499,16 +494,12 @@ ble_l2cap_sig_rx(uint16_t conn_handle, struct os_mbuf **om)
     rx_cb = ble_l2cap_sig_dispatch_get(hdr.op);
     if (rx_cb == NULL) {
         ble_hs_lock();
-        rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
-                                             &conn, &chan);
-        if (rc == 0) {
-            ble_l2cap_sig_reject_tx(conn, chan, hdr.identifier,
-                                    BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD,
-                                    NULL, 0);
-            rc = BLE_HS_L2C_ERR(BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD);
-        } else {
-            rc = BLE_HS_ENOTCONN;
-        }
+        ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SIG,
+                                        &conn, &chan);
+        ble_l2cap_sig_reject_tx(conn, chan, hdr.identifier,
+                                BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD,
+                                NULL, 0);
+        rc = BLE_HS_L2C_ERR(BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD);
         ble_hs_unlock();
     } else {
         rc = rx_cb(conn_handle, &hdr, om);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 f2a7b5c..4991e2e 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -494,24 +494,7 @@ ble_sm_fill_store_value(uint8_t peer_addr_type, uint8_t *peer_addr,
     }
 }
 
-int
-ble_sm_peer_addr(struct ble_sm_proc *proc,
-                 uint8_t *out_type, uint8_t **out_addr)
-{
-    struct ble_hs_conn *conn;
-
-    conn = ble_hs_conn_find(proc->conn_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    }
-
-    *out_type = conn->bhc_peer_addr_type;
-    *out_addr = conn->bhc_peer_addr;
-
-    return 0;
-}
-
-int
+void
 ble_sm_ia_ra(struct ble_sm_proc *proc,
              uint8_t *out_iat, uint8_t *out_ia,
              uint8_t *out_rat, uint8_t *out_ra)
@@ -519,10 +502,7 @@ ble_sm_ia_ra(struct ble_sm_proc *proc,
     struct ble_hs_conn_addrs addrs;
     struct ble_hs_conn *conn;
 
-    conn = ble_hs_conn_find(proc->conn_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    }
+    conn = ble_hs_conn_find_assert(proc->conn_handle);
 
     ble_hs_conn_addrs(conn, &addrs);
 
@@ -539,8 +519,6 @@ ble_sm_ia_ra(struct ble_sm_proc *proc,
         *out_rat = addrs.our_id_addr_type;
         memcpy(out_ra, addrs.our_ota_addr, 6);
     }
-
-    return 0;
 }
 
 static void
@@ -765,13 +743,16 @@ ble_sm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg)
 {
     ble_sm_state_fn *cb;
 
-    BLE_HS_DBG_ASSERT(proc->state < BLE_SM_PROC_STATE_CNT);
-    cb = ble_sm_state_dispatch[proc->state];
-    BLE_HS_DBG_ASSERT(cb != NULL);
-
     memset(res, 0, sizeof *res);
 
-    cb(proc, res, arg);
+    if (!ble_hs_conn_exists(proc->conn_handle)) {
+        res->app_status = BLE_HS_ENOTCONN;
+    } else {
+        BLE_HS_DBG_ASSERT(proc->state < BLE_SM_PROC_STATE_CNT);
+        cb = ble_sm_state_dispatch[proc->state];
+        BLE_HS_DBG_ASSERT(cb != NULL);
+        cb(proc, res, arg);
+    }
 }
 
 void
@@ -1208,13 +1189,9 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
     }
 
     if (restore) {
-        conn = ble_hs_conn_find(evt->connection_handle);
-        if (conn == NULL) {
-            res.app_status = BLE_HS_ENOTCONN;
-        } else {
-            ble_hs_conn_addrs(conn, &addrs);
-            memcpy(peer_id_addr, addrs.peer_id_addr, 6);
-        }
+        conn = ble_hs_conn_find_assert(evt->connection_handle);
+        ble_hs_conn_addrs(conn, &addrs);
+        memcpy(peer_id_addr, addrs.peer_id_addr, 6);
     }
 
     ble_hs_unlock();
@@ -1526,11 +1503,8 @@ ble_sm_pair_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
 
         proc->pair_req = req;
 
-        conn = ble_hs_conn_find(proc->conn_handle);
-        if (conn == NULL) {
-            res->sm_err = BLE_SM_ERR_UNSPECIFIED;
-            res->app_status = BLE_HS_ENOTCONN;
-        } else if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
+        conn = ble_hs_conn_find_assert(proc->conn_handle);
+        if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
             res->sm_err = BLE_SM_ERR_CMD_NOT_SUPP;
             res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_CMD_NOT_SUPP);
         } else if (!ble_sm_pair_cmd_is_valid(&req)) {
@@ -1630,10 +1604,8 @@ ble_sm_sec_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
 
     ble_hs_lock();
 
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        res->app_status = BLE_HS_ENOTCONN;
-    } else if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
+    conn = ble_hs_conn_find_assert(conn_handle);
+    if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
         res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_CMD_NOT_SUPP);
         res->sm_err = BLE_SM_ERR_CMD_NOT_SUPP;
     } else {
@@ -1769,11 +1741,7 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         proc->our_keys.irk_valid = 1;
 
         /* Send identity address information. */
-        conn = ble_hs_conn_find(proc->conn_handle);
-        if (conn == NULL) {
-            rc = BLE_HS_ENOTCONN;
-            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;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 9854bde..0283137 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -38,13 +38,8 @@ ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
 
     STATS_INC(ble_l2cap_stats, sm_tx);
 
-    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
-                                         &conn, &chan);
-    if (rc != 0) {
-        os_mbuf_free_chain(txom);
-        return rc;
-    }
-
+    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
+                                    &conn, &chan);
     rc = ble_l2cap_tx(conn, chan, txom);
     if (rc != 0) {
         return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 d844a6f..7b1673c 100644
--- a/net/nimble/host/src/ble_sm_lgcy.c
+++ b/net/nimble/host/src/ble_sm_lgcy.c
@@ -109,12 +109,7 @@ ble_sm_lgcy_confirm_prepare_args(struct ble_sm_proc *proc,
                                  uint8_t *iat, uint8_t *rat,
                                  uint8_t *ia, uint8_t *ra)
 {
-    int rc;
-
-    rc = ble_sm_ia_ra(proc, iat, ia, rat, ra);
-    if (rc != 0) {
-        return rc;
-    }
+    ble_sm_ia_ra(proc, iat, ia, rat, ra);
 
     memcpy(k, proc->tk, sizeof proc->tk);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 80798e1..d15afbc 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -448,11 +448,9 @@ int ble_sm_ioact_state(uint8_t action);
 int ble_sm_proc_can_advance(struct ble_sm_proc *proc);
 void ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res);
 void ble_sm_confirm_advance(struct ble_sm_proc *proc);
-int ble_sm_peer_addr(struct ble_sm_proc *proc,
-                     uint8_t *out_type, uint8_t **out_addr);
-int ble_sm_ia_ra(struct ble_sm_proc *proc,
-                 uint8_t *out_iat, uint8_t *out_ia,
-                 uint8_t *out_rat, uint8_t *out_ra);
+void ble_sm_ia_ra(struct ble_sm_proc *proc,
+                  uint8_t *out_iat, uint8_t *out_ia,
+                  uint8_t *out_rat, uint8_t *out_ra);
 
 int32_t ble_sm_heartbeat(void);
 void ble_sm_connection_broken(uint16_t conn_handle);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8e95fbe7/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 539d0f9..1c38d04 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -386,14 +386,7 @@ ble_sm_sc_random_rx(struct ble_sm_proc *proc, struct ble_sm_result *res)
     }
 
     /* Calculate the mac key and ltk. */
-    rc = ble_sm_ia_ra(proc, &iat, ia, &rat, ra);
-    if (rc != 0) {
-        res->app_status = rc;
-        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
-        res->enc_cb = 1;
-        return;
-    }
-
+    ble_sm_ia_ra(proc, &iat, ia, &rat, ra);
     rc = ble_sm_alg_f5(proc->dhkey, proc->randm, proc->rands,
                        iat, ia, rat, ra, proc->mackey, proc->ltk);
     if (rc != 0) {
@@ -531,7 +524,7 @@ ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
     ble_hs_unlock();
 }
 
-static int
+static void
 ble_sm_sc_dhkey_addrs(struct ble_sm_proc *proc,
                       uint8_t *out_our_id_addr_type,
                       const uint8_t **out_our_ota_addr,
@@ -541,18 +534,13 @@ ble_sm_sc_dhkey_addrs(struct ble_sm_proc *proc,
     struct ble_hs_conn_addrs addrs;
     struct ble_hs_conn *conn;
 
-    conn = ble_hs_conn_find(proc->conn_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    }
+    conn = ble_hs_conn_find_assert(proc->conn_handle);
 
     ble_hs_conn_addrs(conn, &addrs);
     *out_our_id_addr_type = addrs.our_id_addr_type;
     *out_our_ota_addr = addrs.our_ota_addr;
     *out_peer_id_addr_type = addrs.peer_id_addr_type;
     *out_peer_ota_addr = addrs.peer_ota_addr;
-
-    return 0;
 }
 
 static void
@@ -582,12 +570,9 @@ ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         ble_sm_sc_dhkey_check_iocap(&proc->pair_rsp, iocap);
     }
 
-    rc = ble_sm_sc_dhkey_addrs(proc,
-                               &our_id_addr_type, &our_ota_addr,
-                               &peer_id_addr_type, &peer_ota_addr);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_sm_sc_dhkey_addrs(proc,
+                          &our_id_addr_type, &our_ota_addr,
+                          &peer_id_addr_type, &peer_ota_addr);
 
     rc = ble_sm_alg_f6(proc->mackey, ble_sm_our_pair_rand(proc),
                        ble_sm_peer_pair_rand(proc), proc->tk, iocap,
@@ -634,17 +619,11 @@ ble_sm_dhkey_check_process(struct ble_sm_proc *proc,
         ble_sm_sc_dhkey_check_iocap(&proc->pair_req, iocap);
     }
 
-    res->app_status = ble_sm_sc_dhkey_addrs(proc,
-                                            &our_id_addr_type,
-                                            &our_ota_addr,
-                                            &peer_id_addr_type,
-                                            &peer_ota_addr);
-    if (res->app_status != 0) {
-        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
-        res->enc_cb = 1;
-        return;
-    }
-
+    ble_sm_sc_dhkey_addrs(proc,
+                          &our_id_addr_type,
+                          &our_ota_addr,
+                          &peer_id_addr_type,
+                          &peer_ota_addr);
     BLE_HS_LOG(DEBUG, "tk=");
     ble_hs_log_flat_buf(proc->tk, 32);
     BLE_HS_LOG(DEBUG, "\n");


[02/50] [abbrv] incubator-mynewt-core git commit: mempool - fn to check if block came from pool.

Posted by we...@apache.org.
mempool - fn to check if block came from pool.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: edf19dd74fe2f763960dac9fa41a8cb73c9a17a6
Parents: 93f8d4d
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 16:24:14 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 libs/os/include/os/os_mempool.h |  3 ++
 libs/os/src/os_mempool.c        | 54 ++++++++++++++++++++++++------------
 2 files changed, 40 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/edf19dd7/libs/os/include/os/os_mempool.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/os_mempool.h b/libs/os/include/os/os_mempool.h
index 6b0f24c..a731618 100644
--- a/libs/os/include/os/os_mempool.h
+++ b/libs/os/include/os/os_mempool.h
@@ -82,6 +82,9 @@ typedef uint64_t os_membuf_t;
 os_error_t os_mempool_init(struct os_mempool *mp, int blocks, int block_size, 
                            void *membuf, char *name);
 
+/* Checks if a memory block was allocated from the specified mempool. */
+int os_memblock_from(struct os_mempool *mp, void *block_addr);
+
 /* Get a memory block from the pool */
 void *os_memblock_get(struct os_mempool *mp);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/edf19dd7/libs/os/src/os_mempool.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mempool.c b/libs/os/src/os_mempool.c
index 1e7428c..04a18a1 100644
--- a/libs/os/src/os_mempool.c
+++ b/libs/os/src/os_mempool.c
@@ -86,6 +86,42 @@ os_mempool_init(struct os_mempool *mp, int blocks, int block_size, void *membuf,
 }
 
 /**
+ * Checks if a memory block was allocated from the specified mempool.
+ *
+ * @param mp                    The mempool to check as parent.
+ * @param block_addr            The memory block to check as child.
+ *
+ * @return                      0 if the block does not belong to the mempool;
+ *                              1 if the block does belong to the mempool.
+ */
+int
+os_memblock_from(struct os_mempool *mp, void *block_addr)
+{
+    uint32_t true_block_size;
+    uint32_t baddr32;
+    uint32_t end;
+
+    _Static_assert(sizeof block_addr == sizeof baddr32,
+                   "Pointer to void must be 32-bits.");
+
+    baddr32 = (uint32_t)block_addr;
+    true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size);
+    end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size);
+
+    /* Check that the block is in the memory buffer range. */
+    if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) {
+        return 0;
+    }
+
+    /* All freed blocks should be on true block size boundaries! */
+    if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) {
+        return 0;
+    }
+
+    return 1;
+}
+
+/**
  * os memblock get 
  *  
  * Get a memory block from a memory pool 
@@ -135,9 +171,6 @@ os_error_t
 os_memblock_put(struct os_mempool *mp, void *block_addr)
 {
     os_sr_t sr;
-    uint32_t end;
-    uint32_t true_block_size;
-    uint32_t baddr32;
     struct os_memblock *block;
 
     /* Make sure parameters are valid */
@@ -146,23 +179,10 @@ os_memblock_put(struct os_mempool *mp, void *block_addr)
     }
 
     /* Check that the block we are freeing is a valid block! */
-    baddr32 = (uint32_t)block_addr;
-    true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size);
-    end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size);
-    if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) {
-        return OS_INVALID_PARM;
-    }
-
-    /* All freed blocks should be on true block size boundaries! */
-    if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) {
+    if (!os_memblock_from(mp, block_addr)) {
         return OS_INVALID_PARM;
     }
 
-    /* 
-     * XXX: we should do boundary checks here! The block had better be within 
-     * the pool. If it fails, do we return an error or assert()? Add this when 
-     * we add the memory debug. 
-     */ 
     block = (struct os_memblock *)block_addr;
     OS_ENTER_CRITICAL(sr);
     


[38/50] [abbrv] incubator-mynewt-core git commit: ble - Transports return BLE_ERR_ return codes.

Posted by we...@apache.org.
ble - Transports return BLE_ERR_ return codes.

Prior to this change, the HCI transports return code behavior was
inconsistent.  Sometimes BLE_ERR codes were returned; other times plain
errno values were returned.

Now only BLE_ERR error codes are returned.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 2697a4f5cec19e334526cdb3c87c168050512b41
Parents: ef34d01
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 10:33:36 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/include/nimble/ble.h              |  2 ++
 net/nimble/src/ble_util.c                    | 43 +++++++++++++++++++++++
 net/nimble/transport/ram/src/ble_hci_ram.c   | 42 ++++++++--------------
 net/nimble/transport/uart/src/ble_hci_uart.c | 40 +++++++--------------
 4 files changed, 73 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2697a4f5/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index 4deb9f3..6c4c90e 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -227,4 +227,6 @@ enum ble_error_codes
 #define BLE_ADDR_TYPE_RPA_PUB_DEFAULT   (2)
 #define BLE_ADDR_TYPE_RPA_RND_DEFAULT   (3)
 
+int ble_err_from_os(int os_err);
+
 #endif /* H_BLE_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2697a4f5/net/nimble/src/ble_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/src/ble_util.c b/net/nimble/src/ble_util.c
new file mode 100644
index 0000000..bfe7083
--- /dev/null
+++ b/net/nimble/src/ble_util.c
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/os.h"
+#include "nimble/ble.h"
+
+/**
+ * Converts an OS error code to its equivalent BLE_ERR code.
+ *
+ * @param os_err                The OS error code to convert.
+ *
+ * @return                      The equivalent BLE_ERR code.
+ */
+int
+ble_err_from_os(int os_err)
+{
+    switch (os_err) {
+    case 0:
+        return 0;
+
+    case OS_ENOMEM:
+        return BLE_ERR_MEM_CAPACITY;
+
+    default:
+        return BLE_ERR_UNSPECIFIED;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2697a4f5/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index 23a375d..2ce5fd6 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -2,6 +2,8 @@
 #include <errno.h>
 #include <stddef.h>
 #include "os/os.h"
+#include "util/mem.h"
+#include "nimble/ble.h"
 #include "nimble/ble_hci_trans.h"
 #include "transport/ram/ble_hci_ram.h"
 
@@ -191,43 +193,29 @@ ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
 
     ble_hci_ram_free_mem();
 
-    if (cfg->num_evt_hi_bufs > 0) {
-        ble_hci_ram_evt_hi_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_hi_bufs,
-                                                         cfg->evt_buf_sz));
-        if (ble_hci_ram_evt_hi_buf == NULL) {
-            rc = ENOMEM;
-            goto err;
-        }
-    }
-
-    rc = os_mempool_init(&ble_hci_ram_evt_hi_pool, cfg->num_evt_hi_bufs,
-                         cfg->evt_buf_sz, ble_hci_ram_evt_hi_buf,
-                         "ble_hci_ram_evt_hi_pool");
+    rc = mem_malloc_mempool(&ble_hci_ram_evt_hi_pool,
+                            cfg->num_evt_hi_bufs,
+                            cfg->evt_buf_sz,
+                            "ble_hci_ram_evt_hi_pool",
+                            &ble_hci_ram_evt_hi_buf);
     if (rc != 0) {
-        rc = EINVAL;
+        rc = ble_err_from_os(rc);
         goto err;
     }
 
-    if (cfg->num_evt_lo_bufs > 0) {
-        ble_hci_ram_evt_lo_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_lo_bufs,
-                                                         cfg->evt_buf_sz));
-        if (ble_hci_ram_evt_lo_buf == NULL) {
-            rc = ENOMEM;
-            goto err;
-        }
-    }
-
-    rc = os_mempool_init(&ble_hci_ram_evt_lo_pool, cfg->num_evt_lo_bufs,
-                         cfg->evt_buf_sz, ble_hci_ram_evt_lo_buf,
-                         "ble_hci_ram_evt_lo_pool");
+    rc = mem_malloc_mempool(&ble_hci_ram_evt_lo_pool,
+                            cfg->num_evt_lo_bufs,
+                            cfg->evt_buf_sz,
+                            "ble_hci_ram_evt_lo_pool",
+                            &ble_hci_ram_evt_lo_buf);
     if (rc != 0) {
-        rc = EINVAL;
+        rc = ble_err_from_os(rc);
         goto err;
     }
 
     ble_hci_ram_hs_cmd_buf = malloc(BLE_HCI_TRANS_CMD_SZ);
     if (ble_hci_ram_hs_cmd_buf == NULL) {
-        rc = ENOMEM;
+        rc = BLE_ERR_MEM_CAPACITY;
         goto err;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2697a4f5/net/nimble/transport/uart/src/ble_hci_uart.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/src/ble_hci_uart.c b/net/nimble/transport/uart/src/ble_hci_uart.c
index 1285f56..0fa982a 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -23,7 +23,7 @@
 #include <errno.h>
 #include "bsp/bsp.h"
 #include "os/os.h"
-#include "bsp/bsp.h"
+#include "util/mem.h"
 #include "hal/hal_gpio.h"
 #include "hal/hal_cputime.h"
 #include "hal/hal_uart.h"
@@ -705,39 +705,25 @@ ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg)
 
     ble_hci_uart_cfg = *cfg;
 
-    ble_hci_uart_evt_buf = malloc(
-        OS_MEMPOOL_BYTES(ble_hci_uart_cfg.num_evt_bufs,
-                         ble_hci_uart_cfg.evt_buf_sz));
-    if (ble_hci_uart_evt_buf == NULL) {
-        rc = BLE_ERR_MEM_CAPACITY;
-        goto err;
-    }
-
     /* Create memory pool of HCI command / event buffers */
-    rc = os_mempool_init(&ble_hci_uart_evt_pool, ble_hci_uart_cfg.num_evt_bufs,
-                         ble_hci_uart_cfg.evt_buf_sz, ble_hci_uart_evt_buf,
-                         "ble_hci_uart_evt_pool");
+    rc = mem_malloc_mempool(&ble_hci_uart_evt_pool,
+                            cfg->num_evt_bufs,
+                            cfg->evt_buf_sz,
+                            "ble_hci_uart_evt_pool",
+                            &ble_hci_uart_evt_buf);
     if (rc != 0) {
-        rc = BLE_ERR_UNSPECIFIED;
-        goto err;
-    }
-
-    ble_hci_uart_pkt_buf = malloc(
-        OS_MEMPOOL_BYTES(ble_hci_uart_cfg.num_evt_bufs,
-        sizeof (struct os_event)));
-    if (ble_hci_uart_pkt_buf == NULL) {
-        rc = BLE_ERR_MEM_CAPACITY;
+        rc = ble_err_from_os(rc);
         goto err;
     }
 
     /* Create memory pool of packet list nodes. */
-    rc = os_mempool_init(&ble_hci_uart_pkt_pool,
-                         ble_hci_uart_cfg.num_evt_bufs,
-                         sizeof (struct ble_hci_uart_pkt),
-                         ble_hci_uart_pkt_buf,
-                         "ble_hci_uart_pkt_pool");
+    rc = mem_malloc_mempool(&ble_hci_uart_pkt_pool,
+                            cfg->num_evt_bufs,
+                            sizeof (struct ble_hci_uart_pkt),
+                            "ble_hci_uart_pkt_pool",
+                            &ble_hci_uart_pkt_buf);
     if (rc != 0) {
-        rc = BLE_ERR_UNSPECIFIED;
+        rc = ble_err_from_os(rc);
         goto err;
     }
 


[13/50] [abbrv] incubator-mynewt-core git commit: BLE Host - doxy comments for ble_gatts_count_cfg()

Posted by we...@apache.org.
BLE Host - doxy comments for ble_gatts_count_cfg()


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

Branch: refs/heads/phyrx_no_mbuf
Commit: ab25d8496b14943d52f0b66aafcabb601221e7e0
Parents: 4b24cca
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Aug 6 14:12:03 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gatts.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ab25d849/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 31f3a6f..4bdc6c7 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -1979,6 +1979,23 @@ ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs,
     return 0;
 }
 
+/**
+ * Adjusts a host configuration object's settings to accommodate the specified
+ * service definition array.  This function adds the counts to the appropriate
+ * fields in the supplied configuration object without clearing them first, so
+ * it can be called repeatedly with different inputs to calculate totals.  Be
+ * sure to zero the GATT server settings prior to the first call to this
+ * function.
+ *
+ * @param defs                  The service array containing the resource
+ *                                  definitions to be counted.
+ * @param cfg                   The resource counts are accumulated in this
+ *                                  configuration object.
+ *
+ * @return                      0 on success;
+ *                              BLE_HS_EINVAL if the svcs array contains an
+ *                                  invalid resource definition.
+ */
 int
 ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
                     struct ble_hs_cfg *cfg)


[15/50] [abbrv] incubator-mynewt-core git commit: BLE apps - Update for latest HCI transport changes

Posted by we...@apache.org.
BLE apps - Update for latest HCI transport changes


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 90de67d6e2842fa93eccafbf55c3561639bbd437
Parents: 1301926
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 16:31:33 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 apps/bleprph/src/main.c |  7 ++++---
 apps/bletest/src/main.c |  3 +++
 apps/bletiny/src/main.c |  4 ++--
 apps/bleuart/src/main.c | 17 ++++++++++++-----
 4 files changed, 21 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/90de67d6/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 8261de3..524690d 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -390,6 +390,10 @@ main(void)
     rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
     assert(rc == 0);
 
+    /* Initialize the RAM HCI transport. */
+    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    assert(rc == 0);
+
     /* Initialize the NimBLE host configuration. */
     cfg = ble_hs_cfg_dflt;
     cfg.max_gattc_procs = 2;
@@ -419,9 +423,6 @@ main(void)
     rc = ble_hs_init(&bleprph_evq, &cfg);
     assert(rc == 0);
 
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
-    assert(rc == 0);
-
     nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
     imgmgr_module_init();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/90de67d6/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index be0cf6d..c2635dd 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -44,6 +44,8 @@
 #include "controller/ble_ll_conn.h"
 #include "controller/ble_ll_scan.h"
 #include "controller/ble_ll_adv.h"
+
+/* RAM HCI transport. */
 #include "transport/ram/ble_hci_ram.h"
 
 /* XXX: An app should not include private headers from a library.  The bletest
@@ -1154,6 +1156,7 @@ main(void)
     rc = ble_hs_init(&g_bletest_evq, NULL);
     assert(rc == 0);
 
+    /* Initialize the RAM HCI transport. */
     rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
     assert(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/90de67d6/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 33de2f0..86b502a 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -45,6 +45,8 @@
 #include "host/ble_gatt.h"
 #include "host/ble_store.h"
 #include "host/ble_sm.h"
+
+/* RAM HCI transport. */
 #include "transport/ram/ble_hci_ram.h"
 
 /* RAM persistence layer. */
@@ -1697,13 +1699,11 @@ main(void)
 
     /* Initialize the RAM HCI transport. */
     hci_cfg = ble_hci_ram_cfg_dflt;
-    hci_cfg.num_evt_bufs = 3;
     rc = ble_hci_ram_init(&hci_cfg);
     assert(rc == 0);
 
     /* Initialize the NimBLE host configuration. */
     cfg = ble_hs_cfg_dflt;
-    cfg.max_hci_bufs = hci_cfg.num_evt_bufs;
     cfg.max_gattc_procs = 2;
     cfg.reset_cb = bletiny_on_reset;
     cfg.store_read_cb = ble_store_ram_read;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/90de67d6/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index e68d097..a9e71bd 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -39,10 +39,9 @@
 #include "host/ble_l2cap.h"
 #include "host/ble_sm.h"
 #include "controller/ble_ll.h"
-/* Newtmgr include */
-#include "newtmgr/newtmgr.h"
-#include "nmgrble/newtmgr_ble.h"
-#include "bleuart/bleuart.h"
+
+/* RAM HCI transport. */
+#include "transport/ram/ble_hci_ram.h"
 
 /* RAM persistence layer. */
 #include "store/ram/ble_store_ram.h"
@@ -51,6 +50,11 @@
 #include "services/gap/ble_svc_gap.h"
 #include "services/gatt/ble_svc_gatt.h"
 
+/* Newtmgr include */
+#include "newtmgr/newtmgr.h"
+#include "nmgrble/newtmgr_ble.h"
+#include "bleuart/bleuart.h"
+
 /** Mbuf settings. */
 #define MBUF_NUM_MBUFS      (12)
 #define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
@@ -292,9 +296,12 @@ main(void)
     rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
     assert(rc == 0);
 
+    /* Initialize the RAM HCI transport. */
+    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    assert(rc == 0);
+
     /* Initialize the BLE host. */
     cfg = ble_hs_cfg_dflt;
-    cfg.max_hci_bufs = 3;
     cfg.max_connections = 1;
     cfg.max_gattc_procs = 2;
     cfg.max_l2cap_chans = 3;


[41/50] [abbrv] incubator-mynewt-core git commit: BLE apps - Base ble_hs max_hci setting on HCI cfg.

Posted by we...@apache.org.
BLE apps - Base ble_hs max_hci setting on HCI cfg.

The host should be configured to allocate the same number of OS events
as there are HCI event buffers.  This commit changes the sample apps to
use the RAM transport's configuration to determine how many OS events to
configure the host with.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 9f944afa795a2540837e56c0b520a9ec087dab7f
Parents: 74c3c9b
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:56:52 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 apps/blecent/src/main.c | 6 ++++--
 apps/bleprph/src/main.c | 5 ++++-
 apps/bletiny/src/main.c | 1 +
 apps/bleuart/src/main.c | 6 ++++--
 4 files changed, 13 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9f944afa/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index 6cc01e2..d5f47e6 100755
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@ -508,6 +508,7 @@ blecent_task_handler(void *unused)
 int
 main(void)
 {
+    struct ble_hci_ram_cfg hci_cfg;
     struct ble_hs_cfg cfg;
     uint32_t seed;
     int rc;
@@ -567,12 +568,13 @@ main(void)
     assert(rc == 0);
 
     /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    hci_cfg = ble_hci_ram_cfg_dflt;
+    rc = ble_hci_ram_init(&hci_cfg);
     assert(rc == 0);
 
     /* Configure the host. */
     cfg = ble_hs_cfg_dflt;
-    cfg.max_hci_bufs = 3;
+    cfg.max_hci_bufs = hci_cfg.num_evt_hi_bufs + hci_cfg.num_evt_lo_bufs;
     cfg.max_gattc_procs = 5;
     cfg.sm_bonding = 1;
     cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9f944afa/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 524690d..b08b077 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -332,6 +332,7 @@ bleprph_task_handler(void *unused)
 int
 main(void)
 {
+    struct ble_hci_ram_cfg hci_cfg;
     struct ble_hs_cfg cfg;
     uint32_t seed;
     int rc;
@@ -391,11 +392,13 @@ main(void)
     assert(rc == 0);
 
     /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    hci_cfg = ble_hci_ram_cfg_dflt;
+    rc = ble_hci_ram_init(&hci_cfg);
     assert(rc == 0);
 
     /* Initialize the NimBLE host configuration. */
     cfg = ble_hs_cfg_dflt;
+    cfg.max_hci_bufs = hci_cfg.num_evt_hi_bufs + hci_cfg.num_evt_lo_bufs;
     cfg.max_gattc_procs = 2;
     cfg.sm_bonding = 1;
     cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9f944afa/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 86b502a..469944c 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1704,6 +1704,7 @@ main(void)
 
     /* Initialize the NimBLE host configuration. */
     cfg = ble_hs_cfg_dflt;
+    cfg.max_hci_bufs = hci_cfg.num_evt_hi_bufs + hci_cfg.num_evt_lo_bufs;
     cfg.max_gattc_procs = 2;
     cfg.reset_cb = bletiny_on_reset;
     cfg.store_read_cb = ble_store_ram_read;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9f944afa/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index a9e71bd..264be92 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -253,6 +253,7 @@ bleuart_task_handler(void *unused)
 int
 main(void)
 {
+    struct ble_hci_ram_cfg hci_cfg;
     struct ble_hs_cfg cfg;
     uint32_t seed;
     int rc;
@@ -297,11 +298,13 @@ main(void)
     assert(rc == 0);
 
     /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    hci_cfg = ble_hci_ram_cfg_dflt;
+    rc = ble_hci_ram_init(&hci_cfg);
     assert(rc == 0);
 
     /* Initialize the BLE host. */
     cfg = ble_hs_cfg_dflt;
+    cfg.max_hci_bufs = hci_cfg.num_evt_hi_bufs + hci_cfg.num_evt_lo_bufs;
     cfg.max_connections = 1;
     cfg.max_gattc_procs = 2;
     cfg.max_l2cap_chans = 3;
@@ -334,7 +337,6 @@ main(void)
     rc = nmgr_ble_gatt_svr_init(&bleuart_evq, &cfg);
     assert(rc == 0);
 
-
     rc = ble_hs_init(&bleuart_evq, &cfg);
     assert(rc == 0);
 


[47/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Send correct error code for unsup. n/i.

Posted by we...@apache.org.
BLE Host - Send correct error code for unsup. n/i.

Previously, if a peer attempted to enable notifications or indications
for a characteristic that did not support them, the elicited error
response had an error code of 3 (BLE_ATT_ERR_WRITE_NOT_PERMITTED).

Now, the error response has a code of 6 (BLE_ATT_ERR_REQ_NOT_SUPPORTED).

The spec does not appear to indicate the correct error t osend in this
case.  I believe req-not-supported is more correct than
write-not-permitted.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: a2e20c8bb7c228de16dd2df2d77c1d896a6b4e61
Parents: 0008660
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 11 13:31:37 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gatts.c                 |  2 +-
 net/nimble/host/src/test/ble_att_svr_test.c     | 98 ++++++-------------
 .../host/src/test/ble_gatts_notify_test.c       | 99 +++++++++++++++++++-
 net/nimble/host/src/test/ble_hs_test_util.c     | 39 ++++++++
 net/nimble/host/src/test/ble_hs_test_util.h     |  3 +
 5 files changed, 167 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2e20c8b/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 4bdc6c7..e6ff203 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -662,7 +662,7 @@ ble_gatts_clt_cfg_access_locked(struct ble_hs_conn *conn, uint16_t attr_handle,
 
         flags = le16toh(om->om_data);
         if ((flags & ~clt_cfg->allowed) != 0) {
-            return BLE_ATT_ERR_WRITE_NOT_PERMITTED;
+            return BLE_ATT_ERR_REQ_NOT_SUPPORTED;
         }
 
         if (clt_cfg->flags != flags) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2e20c8b/net/nimble/host/src/test/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_svr_test.c b/net/nimble/host/src/test/ble_att_svr_test.c
index 1509b41..d8dfb27 100644
--- a/net/nimble/host/src/test/ble_att_svr_test.c
+++ b/net/nimble/host/src/test/ble_att_svr_test.c
@@ -343,29 +343,6 @@ ble_att_svr_test_misc_verify_w_2(void *data, int data_len)
 }
 
 static void
-ble_att_svr_test_misc_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
-                                        uint8_t error_code)
-{
-    struct ble_att_error_rsp rsp;
-    struct os_mbuf *om;
-    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_error_rsp_parse(buf, sizeof buf, &rsp);
-
-    TEST_ASSERT(rsp.baep_req_op == req_op);
-    TEST_ASSERT(rsp.baep_handle == handle);
-    TEST_ASSERT(rsp.baep_error_code == error_code);
-}
-
-static void
 ble_att_svr_test_misc_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
 {
     struct os_mbuf *om;
@@ -481,23 +458,6 @@ ble_att_svr_test_misc_verify_all_read_mult(
                                                   attrs, num_attrs);
 }
 
-
-static void
-ble_att_svr_test_misc_verify_tx_write_rsp(void)
-{
-    struct os_mbuf *om;
-    uint8_t u8;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, 1, &u8);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(u8 == BLE_ATT_OP_WRITE_RSP);
-}
-
 static void
 ble_att_svr_test_misc_verify_tx_mtu_rsp(uint16_t conn_handle)
 {
@@ -845,7 +805,7 @@ ble_att_svr_test_misc_prep_write(uint16_t conn_handle, uint16_t attr_handle,
                                                        data, data_len);
     } else {
         TEST_ASSERT(rc != 0);
-        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_PREP_WRITE_REQ,
+        ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_PREP_WRITE_REQ,
                                                 attr_handle, error_code);
     }
 }
@@ -869,7 +829,7 @@ ble_att_svr_test_misc_exec_write(uint16_t conn_handle, uint8_t flags,
         ble_att_svr_test_misc_verify_tx_exec_write_rsp();
     } else {
         TEST_ASSERT(rc != 0);
-        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_EXEC_WRITE_REQ,
+        ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_EXEC_WRITE_REQ,
                                                 error_handle, error_code);
     }
 }
@@ -1022,7 +982,7 @@ TEST_CASE(ble_att_svr_test_read)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ, 0,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ, 0,
                                             BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** Successful read. */
@@ -1066,7 +1026,7 @@ TEST_CASE(ble_att_svr_test_read)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ,
                                             req.barq_handle,
                                             BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
 
@@ -1114,7 +1074,7 @@ TEST_CASE(ble_att_svr_test_read_blob)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_BLOB_REQ, 0,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_READ_BLOB_REQ, 0,
                                             BLE_ATT_ERR_INVALID_HANDLE);
 
 
@@ -1198,7 +1158,7 @@ TEST_CASE(ble_att_svr_test_read_mult)
     /*** Single nonexistent attribute. */
     ble_att_svr_test_misc_rx_read_mult_req(
         conn_handle, ((uint16_t[]){ 100 }), 1, 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
                                             100, BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** Single attribute. */
@@ -1213,7 +1173,7 @@ TEST_CASE(ble_att_svr_test_read_mult)
     /*** Second attribute nonexistent; verify only error txed. */
     ble_att_svr_test_misc_rx_read_mult_req(
         conn_handle, ((uint16_t[]){ attrs[0].handle, 100 }), 2, 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
                                             100, BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** Response too long; verify only MTU bytes sent. */
@@ -1256,7 +1216,7 @@ TEST_CASE(ble_att_svr_test_write)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_WRITE_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** Write not permitted if non-local. */
@@ -1272,7 +1232,7 @@ TEST_CASE(ble_att_svr_test_write)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc == BLE_HS_ENOTSUP);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
                                             req.bawq_handle,
                                             BLE_ATT_ERR_WRITE_NOT_PERMITTED);
 
@@ -1296,7 +1256,7 @@ TEST_CASE(ble_att_svr_test_write)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_write_rsp();
+    ble_hs_test_util_verify_tx_write_rsp();
 
     /*** Write requires encryption. */
     /* Insufficient authentication. */
@@ -1312,7 +1272,7 @@ TEST_CASE(ble_att_svr_test_write)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
+    ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
                                             req.bawq_handle,
                                             BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
 
@@ -1333,7 +1293,7 @@ TEST_CASE(ble_att_svr_test_write)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_write_rsp();
+    ble_hs_test_util_verify_tx_write_rsp();
 }
 
 TEST_CASE(ble_att_svr_test_find_info)
@@ -1363,7 +1323,7 @@ TEST_CASE(ble_att_svr_test_find_info)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_INFO_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** Start handle > end handle. */
@@ -1375,7 +1335,7 @@ TEST_CASE(ble_att_svr_test_find_info)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_INFO_REQ, 101, BLE_ATT_ERR_INVALID_HANDLE);
 
     /*** No attributes. */
@@ -1387,7 +1347,7 @@ TEST_CASE(ble_att_svr_test_find_info)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
 
     /*** Range too late. */
@@ -1403,7 +1363,7 @@ TEST_CASE(ble_att_svr_test_find_info)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
 
     /*** One 128-bit entry. */
@@ -1530,7 +1490,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 0,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1543,7 +1503,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 101,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1556,7 +1516,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
@@ -1573,7 +1533,7 @@ TEST_CASE(ble_att_svr_test_find_type_value)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
@@ -1712,7 +1672,7 @@ ble_att_svr_test_misc_read_type(uint16_t mtu)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_TYPE_REQ, 0,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1727,7 +1687,7 @@ ble_att_svr_test_misc_read_type(uint16_t mtu)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_TYPE_REQ, 101,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1742,7 +1702,7 @@ ble_att_svr_test_misc_read_type(uint16_t mtu)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_TYPE_REQ, 1,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
@@ -1758,7 +1718,7 @@ ble_att_svr_test_misc_read_type(uint16_t mtu)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_TYPE_REQ, 200,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
@@ -1924,7 +1884,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_GROUP_TYPE_REQ, 0,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1939,7 +1899,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_GROUP_TYPE_REQ, 101,
         BLE_ATT_ERR_INVALID_HANDLE);
 
@@ -1953,7 +1913,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_GROUP_TYPE_REQ, 110,
         BLE_ATT_ERR_UNSUPPORTED_GROUP);
 
@@ -1968,7 +1928,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_GROUP_TYPE_REQ, 1,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 
@@ -1984,7 +1944,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
     TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
+    ble_hs_test_util_verify_tx_err_rsp(
         BLE_ATT_OP_READ_GROUP_TYPE_REQ, 200,
         BLE_ATT_ERR_ATTR_NOT_FOUND);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2e20c8b/net/nimble/host/src/test/ble_gatts_notify_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatts_notify_test.c b/net/nimble/host/src/test/ble_gatts_notify_test.c
index 4516e66..4e2d587 100644
--- a/net/nimble/host/src/test/ble_gatts_notify_test.c
+++ b/net/nimble/host/src/test/ble_gatts_notify_test.c
@@ -127,9 +127,9 @@ ble_gatts_notify_test_misc_read_notify(uint16_t conn_handle,
 }
 
 static void
-ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
-                                         uint16_t chr_def_handle,
-                                         uint16_t flags)
+ble_gatts_notify_test_misc_try_enable_notify(uint16_t conn_handle,
+                                             uint16_t chr_def_handle,
+                                             uint16_t flags, int fail)
 {
     struct ble_att_write_req req;
     uint8_t buf[BLE_ATT_WRITE_REQ_BASE_SZ + 2];
@@ -141,7 +141,25 @@ ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
     htole16(buf + BLE_ATT_WRITE_REQ_BASE_SZ, flags);
     rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
                                                 buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
+    if (fail) {
+        TEST_ASSERT_FATAL(rc != 0);
+        ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
+                                           req.bawq_handle,
+                                           BLE_ATT_ERR_REQ_NOT_SUPPORTED);
+    } else {
+        TEST_ASSERT_FATAL(rc == 0);
+        ble_hs_test_util_verify_tx_write_rsp();
+    }
+}
+
+static void
+ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
+                                         uint16_t chr_def_handle,
+                                         uint16_t flags)
+{
+    ble_gatts_notify_test_misc_try_enable_notify(conn_handle,
+                                                 chr_def_handle,
+                                                 flags, 0);
 }
 
 static void
@@ -956,6 +974,77 @@ TEST_CASE(ble_gatts_notify_test_bonded_i_no_ack)
     TEST_ASSERT(!value_cccd.value_changed);
 }
 
+TEST_CASE(ble_gatts_notify_test_disallowed)
+{
+    uint16_t chr1_val_handle;
+    uint16_t chr2_val_handle;
+    uint16_t chr3_val_handle;
+    int rc;
+
+    const struct ble_gatt_svc_def svcs[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(1),
+            .access_cb = ble_gatts_notify_test_misc_access,
+            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
+            .val_handle = &chr1_val_handle,
+        }, {
+            .uuid128 = BLE_UUID16(2),
+            .access_cb = ble_gatts_notify_test_misc_access,
+            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE,
+            .val_handle = &chr2_val_handle,
+        }, {
+            .uuid128 = BLE_UUID16(3),
+            .access_cb = ble_gatts_notify_test_misc_access,
+            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
+                     BLE_GATT_CHR_F_INDICATE,
+            .val_handle = &chr3_val_handle,
+        }, {
+            0
+        } },
+    }, {
+        0
+    } };
+
+    ble_hs_test_util_init();
+
+    rc = ble_gatts_register_svcs(svcs, NULL, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT_FATAL(chr1_val_handle != 0);
+    TEST_ASSERT_FATAL(chr2_val_handle != 0);
+    TEST_ASSERT_FATAL(chr3_val_handle != 0);
+
+    ble_gatts_start();
+
+    ble_hs_test_util_create_conn(2, ble_gatts_notify_test_peer_addr,
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    /* Attempt to enable notifications on chr1 should succeed. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr1_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
+
+    /* Attempt to enable indications on chr1 should fail. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr1_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 1);
+
+    /* Attempt to enable notifications on chr2 should fail. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr2_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 1);
+
+    /* Attempt to enable indications on chr2 should succeed. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr2_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+
+    /* Attempt to enable notifications on chr3 should succeed. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr3_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
+
+    /* Attempt to enable indications on chr3 should succeed. */
+    ble_gatts_notify_test_misc_try_enable_notify(
+        2, chr3_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+}
+
 TEST_SUITE(ble_gatts_notify_suite)
 {
     tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
@@ -968,6 +1057,8 @@ TEST_SUITE(ble_gatts_notify_suite)
 
     ble_gatts_notify_test_bonded_i_no_ack();
 
+    ble_gatts_notify_test_disallowed();
+
     /* XXX: Test corner cases:
      *     o Bonding after CCCD configuration.
      *     o Disconnect prior to rx of indicate ack.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2e20c8b/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 30926b6..b5148e3 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1133,6 +1133,45 @@ ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
 }
 
 void
+ble_hs_test_util_verify_tx_write_rsp(void)
+{
+    struct os_mbuf *om;
+    uint8_t u8;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, 1, &u8);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(u8 == BLE_ATT_OP_WRITE_RSP);
+}
+
+void
+ble_hs_test_util_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
+                                   uint8_t error_code)
+{
+    struct ble_att_error_rsp rsp;
+    struct os_mbuf *om;
+    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_error_rsp_parse(buf, sizeof buf, &rsp);
+
+    TEST_ASSERT(rsp.baep_req_op == req_op);
+    TEST_ASSERT(rsp.baep_handle == handle);
+    TEST_ASSERT(rsp.baep_error_code == error_code);
+}
+
+void
 ble_hs_test_util_set_static_rnd_addr(void)
 {
     uint8_t addr[6] = { 1, 2, 3, 4, 5, 0xc1 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2e20c8b/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index 3320e9b..fa3b5e0 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -142,6 +142,9 @@ void ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags);
 void ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len);
 void ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data,
                                               int attr_len);
+void ble_hs_test_util_verify_tx_write_rsp(void);
+void ble_hs_test_util_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
+                                        uint8_t error_code);
 void ble_hs_test_util_set_static_rnd_addr(void);
 struct os_mbuf *ble_hs_test_util_om_from_flat(const void *buf, uint16_t len);
 int ble_hs_test_util_flat_attr_cmp(const struct ble_hs_test_util_flat_attr *a,


[30/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Cleanup att_svr rx prep / exec write.

Posted by we...@apache.org.
BLE Host - Cleanup att_svr rx prep / exec write.

While debugging an mbuf leak, I noticed this code was much more
complicated than it needs to be.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 34ee3db51c2ed62dadd8ee6130d348eac3623fd8
Parents: ebbd1bd
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 12:46:50 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c | 123 +++++++++++++++------------------
 1 file changed, 57 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34ee3db5/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 860794d..93c57ff 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -2182,8 +2182,10 @@ ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf *om)
 static void
 ble_att_svr_prep_free(struct ble_att_prep_entry *entry)
 {
-    os_mbuf_free_chain(entry->bape_value);
-    os_memblock_put(&ble_att_svr_prep_entry_pool, entry);
+    if (entry != NULL) {
+        os_mbuf_free_chain(entry->bape_value);
+        os_memblock_put(&ble_att_svr_prep_entry_pool, entry);
+    }
 }
 
 static struct ble_att_prep_entry *
@@ -2360,6 +2362,56 @@ ble_att_svr_prep_write(uint16_t conn_handle,
     return 0;
 }
 
+static int
+ble_att_svr_insert_prep_entry(uint16_t conn_handle,
+                              const struct ble_att_prep_write_cmd *req,
+                              const struct os_mbuf *rxom,
+                              uint8_t *out_att_err)
+{
+    struct ble_att_prep_entry *prep_entry;
+    struct ble_att_prep_entry *prep_prev;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn == NULL) {
+        *out_att_err = 0;
+        return BLE_HS_ENOTCONN;
+    }
+
+    prep_entry = ble_att_svr_prep_alloc();
+    if (prep_entry == NULL) {
+        *out_att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
+        return BLE_HS_ENOMEM;
+    }
+    prep_entry->bape_handle = req->bapc_handle;
+    prep_entry->bape_offset = req->bapc_offset;
+
+    /* Append attribute value from request onto prep mbuf. */
+    rc = os_mbuf_appendfrom(
+        prep_entry->bape_value,
+        rxom,
+        BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+        OS_MBUF_PKTLEN(rxom) - BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    if (rc != 0) {
+        ble_att_svr_prep_free(prep_entry);
+        *out_att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
+        return rc;
+    }
+
+    prep_prev = ble_att_svr_prep_find_prev(&conn->bhc_att_svr,
+                                           req->bapc_handle,
+                                           req->bapc_offset);
+    if (prep_prev == NULL) {
+        SLIST_INSERT_HEAD(&conn->bhc_att_svr.basc_prep_list, prep_entry,
+                          bape_next);
+    } else {
+        SLIST_INSERT_AFTER(prep_prev, prep_entry, bape_next);
+    }
+
+    return 0;
+}
+
 int
 ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
@@ -2368,17 +2420,13 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 #endif
 
     struct ble_att_prep_write_cmd req;
-    struct ble_att_prep_entry *prep_entry;
-    struct ble_att_prep_entry *prep_prev;
     struct ble_att_svr_entry *attr_entry;
-    struct ble_hs_conn *conn;
     struct os_mbuf *txom;
     uint16_t err_handle;
     uint8_t att_err;
     int rc;
 
     /* Initialize some values in case of early error. */
-    prep_entry = NULL;
     txom = NULL;
     att_err = 0;
     err_handle = 0;
@@ -2419,49 +2467,16 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
         goto done;
     }
 
-    prep_entry = ble_att_svr_prep_alloc();
-    if (prep_entry == NULL) {
-        att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
-        rc = BLE_HS_ENOMEM;
-        goto done;
-    }
-    prep_entry->bape_handle = req.bapc_handle;
-    prep_entry->bape_offset = req.bapc_offset;
-
     ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        rc = BLE_HS_ENOTCONN;
-    } else {
-        prep_prev = ble_att_svr_prep_find_prev(&conn->bhc_att_svr,
-                                               req.bapc_handle,
-                                               req.bapc_offset);
-        if (prep_prev == NULL) {
-            SLIST_INSERT_HEAD(&conn->bhc_att_svr.basc_prep_list, prep_entry,
-                              bape_next);
-        } else {
-            SLIST_INSERT_AFTER(prep_prev, prep_entry, bape_next);
-        }
-
-        /* Append attribute value from request onto prep mbuf. */
-        rc = os_mbuf_appendfrom(prep_entry->bape_value, *rxom,
-                                BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-                                OS_MBUF_PKTLEN(*rxom) -
-                                    BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-        if (rc != 0) {
-            att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
-        }
-    }
-
+    rc = ble_att_svr_insert_prep_entry(conn_handle, &req, *rxom, &att_err);
     ble_hs_unlock();
 
     if (rc != 0) {
         goto done;
     }
 
-    /* Reuse rxom for response.  Response is identical to request except for op
-     * code.
+    /* Reuse rxom for response.  Response is identical to request except for
+     * op code.
      */
     txom = *rxom;
     *rxom = NULL;
@@ -2473,32 +2488,8 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
     rc = 0;
 
 done:
-    if (rc != 0 && rc != BLE_HS_ENOTCONN) {
-        ble_hs_lock();
-
-        conn = ble_hs_conn_find(conn_handle);
-        if (conn == NULL) {
-            rc = BLE_HS_ENOTCONN;
-        } else {
-            if (prep_entry != NULL) {
-                if (prep_prev == NULL) {
-                    SLIST_REMOVE_HEAD(&conn->bhc_att_svr.basc_prep_list,
-                                      bape_next);
-                } else {
-                    SLIST_NEXT(prep_prev, bape_next) =
-                        SLIST_NEXT(prep_entry, bape_next);
-                }
-
-                ble_att_svr_prep_free(prep_entry);
-            }
-        }
-
-        ble_hs_unlock();
-    }
-
     rc = ble_att_svr_tx_rsp(conn_handle, rc, txom, BLE_ATT_OP_PREP_WRITE_REQ,
                             att_err, err_handle);
-
     return rc;
 }
 


[36/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Fix unit test SIGSEGV.

Posted by we...@apache.org.
BLE Host - Fix unit test SIGSEGV.

An array was missing a '0' terminator.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 02fa2d02029a99ec60513f67bef1b7357d1f9ce1
Parents: 16ead26
Author: Christopher Collins <cc...@apache.org>
Authored: Sun Aug 7 18:13:26 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_hs_test_util.c | 3 +++
 1 file changed, 3 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/02fa2d02/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 460642e..30926b6 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -802,6 +802,9 @@ ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
             BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
             ble_hs_test_util_exp_hci_status(3, fail_idx, hci_status),
         },
+        {
+            0
+        }
     }));
 
     rc = ble_hs_pvcy_set_our_irk(irk);


[42/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Don't reject max-size adv field.

Posted by we...@apache.org.
BLE Host - Don't reject max-size adv field.

The host would reject an advertising report if it contains a 31-byte
field (maximum size allowed).  It was incorrectly limiting fields to 30
bytes.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: bec12a6192b0297b84b3bbf832fb2f49c032f0db
Parents: b607ac0
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 16:57:35 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs_adv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bec12a61/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
index da0d5c1..305c672 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -23,7 +23,8 @@
 #include "host/ble_hs_adv.h"
 #include "ble_hs_priv.h"
 
-#define BLE_HS_ADV_MAX_FIELD_SZ     (BLE_HCI_MAX_ADV_DATA_LEN - 3)
+/** Max field paylaod size (account for 2-byte header). */
+#define BLE_HS_ADV_MAX_FIELD_SZ     (BLE_HCI_MAX_ADV_DATA_LEN - 2)
 
 static uint16_t ble_hs_adv_uuids16[BLE_HS_ADV_MAX_FIELD_SZ / 2];
 static uint32_t ble_hs_adv_uuids32[BLE_HS_ADV_MAX_FIELD_SZ / 4];


[06/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by we...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_pvcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_pvcy.c b/net/nimble/host/src/ble_hs_pvcy.c
index 94a5586..ad3fa0c 100644
--- a/net/nimble/host/src/ble_hs_pvcy.c
+++ b/net/nimble/host/src/ble_hs_pvcy.c
@@ -36,7 +36,7 @@ ble_hs_pvcy_set_addr_timeout(uint16_t timeout)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN];
     int rc;
 
-    rc = host_hci_cmd_build_set_resolv_priv_addr_timeout(
+    rc = ble_hs_hci_cmd_build_set_resolv_priv_addr_timeout(
             timeout, buf, sizeof(buf));
 
     return rc;
@@ -48,12 +48,12 @@ ble_hs_pvcy_set_resolve_enabled(int enable)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADDR_RESOL_ENA_LEN];
     int rc;
 
-    rc = host_hci_cmd_build_set_addr_res_en(enable, buf, sizeof(buf));
+    rc = ble_hs_hci_cmd_build_set_addr_res_en(enable, buf, sizeof(buf));
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    rc = ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
     if (rc != 0) {
         return rc;
     }
@@ -67,13 +67,13 @@ ble_hs_pvcy_remove_entry(uint8_t addr_type, uint8_t *addr)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_RMV_FROM_RESOLV_LIST_LEN];
     int rc;
 
-    rc = host_hci_cmd_build_remove_from_resolv_list(
+    rc = ble_hs_hci_cmd_build_remove_from_resolv_list(
         addr_type, addr, buf, sizeof(buf));
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    rc = ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
     if (rc != 0) {
         return rc;
     }
@@ -87,12 +87,12 @@ ble_hs_pvcy_clear_entries(void)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN ];
     int rc;
 
-    rc = host_hci_cmd_build_clear_resolv_list(buf, sizeof(buf));
+    rc = ble_hs_hci_cmd_build_clear_resolv_list(buf, sizeof(buf));
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    rc = ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
     if (rc != 0) {
         return rc;
     }
@@ -112,12 +112,12 @@ ble_hs_pvcy_add_entry(uint8_t *addr, uint8_t addr_type, uint8_t *irk)
     memcpy(add.local_irk, ble_hs_pvcy_irk, 16);
     memcpy(add.peer_irk, irk, 16);
 
-    rc = host_hci_cmd_build_add_to_resolv_list(&add, buf, sizeof(buf));
+    rc = ble_hs_hci_cmd_build_add_to_resolv_list(&add, buf, sizeof(buf));
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    rc = ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_startup.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_startup.c b/net/nimble/host/src/ble_hs_startup.c
index 1ce49a7..89cbd70 100644
--- a/net/nimble/host/src/ble_hs_startup.c
+++ b/net/nimble/host/src/ble_hs_startup.c
@@ -19,7 +19,6 @@
 
 #include <stddef.h>
 #include <string.h>
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 #include "ble_hs_priv.h"
 
@@ -31,8 +30,9 @@ ble_hs_startup_le_read_sup_f_tx(void)
     uint8_t ack_params_len;
     int rc;
 
-    host_hci_cmd_build_le_read_loc_supp_feat(buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, ack_params, sizeof ack_params, &ack_params_len);
+    ble_hs_hci_cmd_build_le_read_loc_supp_feat(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, ack_params, sizeof ack_params,
+                           &ack_params_len);
     if (rc != 0) {
         return rc;
     }
@@ -56,8 +56,9 @@ ble_hs_startup_le_read_buf_sz_tx(void)
     uint8_t max_pkts;
     int rc;
 
-    host_hci_cmd_build_le_read_buffer_size(buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, ack_params, sizeof ack_params, &ack_params_len);
+    ble_hs_hci_cmd_build_le_read_buffer_size(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, ack_params, sizeof ack_params,
+                           &ack_params_len);
     if (rc != 0) {
         return rc;
     }
@@ -69,7 +70,7 @@ ble_hs_startup_le_read_buf_sz_tx(void)
     pktlen = le16toh(ack_params + 0);
     max_pkts = ack_params[2];
 
-    rc = host_hci_set_buf_size(pktlen, max_pkts);
+    rc = ble_hs_hci_set_buf_sz(pktlen, max_pkts);
     if (rc != 0) {
         return rc;
     }
@@ -85,8 +86,9 @@ ble_hs_startup_read_bd_addr(void)
     uint8_t ack_params_len;
     int rc;
 
-    host_hci_cmd_build_read_bd_addr(buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, ack_params, sizeof ack_params, &ack_params_len);
+    ble_hs_hci_cmd_build_read_bd_addr(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, ack_params, sizeof ack_params,
+                           &ack_params_len);
     if (rc != 0) {
         return rc;
     }
@@ -116,8 +118,9 @@ ble_hs_startup_le_set_evmask_tx(void)
      *     0x0000000000000040 LE Data Length Change Event
      *     0x0000000000000200 LE Enhanced Connection Complete Event
      */
-    host_hci_cmd_build_le_set_event_mask(0x000000000000027f, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_set_event_mask(0x000000000000027f,
+                                           buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -173,8 +176,8 @@ ble_hs_startup_set_evmask_tx(void)
      *     0x0000800000000000 Encryption Key Refresh Complete Event
      *     0x2000000000000000 LE Meta-Event
      */
-    host_hci_cmd_build_set_event_mask(0x20009fffffffffff, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_set_event_mask(0x20009fffffffffff, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -183,8 +186,8 @@ ble_hs_startup_set_evmask_tx(void)
      * Enable the following events:
      *     0x0000000000800000 Authenticated Payload Timeout Event
      */
-    host_hci_cmd_build_set_event_mask2(0x0000000000800000, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_set_event_mask2(0x0000000000800000, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -198,8 +201,8 @@ ble_hs_startup_reset_tx(void)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN];
     int rc;
 
-    host_hci_cmd_build_reset(buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_reset(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_ibeacon.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_ibeacon.c b/net/nimble/host/src/ble_ibeacon.c
index 68c182f..5bdb99d 100644
--- a/net/nimble/host/src/ble_ibeacon.c
+++ b/net/nimble/host/src/ble_ibeacon.c
@@ -61,7 +61,7 @@ ble_ibeacon_set_adv_data(void *uuid128, uint16_t major, uint16_t minor)
 
     /** Last byte (tx power level) filled in after HCI exchange. */
 
-    rc = ble_hci_util_read_adv_tx_pwr(&tx_pwr);
+    rc = ble_hs_hci_util_read_adv_tx_pwr(&tx_pwr);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index a25c364..66bad51 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -22,7 +22,6 @@
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
-#include "host/host_hci.h"
 #include "ble_hs_priv.h"
 
 _Static_assert(sizeof (struct ble_l2cap_hdr) == BLE_L2CAP_HDR_SZ,
@@ -287,7 +286,7 @@ ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         return BLE_HS_ENOMEM;
     }
 
-    rc = host_hci_data_tx(conn, txom);
+    rc = ble_hs_hci_acl_tx(conn, txom);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/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 ae2e5f8..f2a7b5c 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -259,7 +259,7 @@ ble_sm_gen_pair_rand(uint8_t *pair_rand)
     }
 #endif
 
-    rc = ble_hci_util_rand(pair_rand, 16);
+    rc = ble_hs_hci_util_rand(pair_rand, 16);
     if (rc != 0) {
         return rc;
     }
@@ -280,7 +280,7 @@ ble_sm_gen_ediv(uint16_t *ediv)
     }
 #endif
 
-    rc = ble_hci_util_rand(ediv, sizeof *ediv);
+    rc = ble_hs_hci_util_rand(ediv, sizeof *ediv);
     if (rc != 0) {
         return rc;
     }
@@ -301,7 +301,7 @@ ble_sm_gen_master_id_rand(uint64_t *master_id_rand)
     }
 #endif
 
-    rc = ble_hci_util_rand(master_id_rand, sizeof *master_id_rand);
+    rc = ble_hs_hci_util_rand(master_id_rand, sizeof *master_id_rand);
     if (rc != 0) {
         return rc;
     }
@@ -323,7 +323,7 @@ ble_sm_gen_ltk(struct ble_sm_proc *proc, uint8_t *ltk)
     }
 #endif
 
-    rc = ble_hci_util_rand(ltk, 16);
+    rc = ble_hs_hci_util_rand(ltk, 16);
     if (rc != 0) {
         return rc;
     }
@@ -345,7 +345,7 @@ ble_sm_gen_csrk(struct ble_sm_proc *proc, uint8_t *csrk)
     }
 #endif
 
-    rc = ble_hci_util_rand(csrk, 16);
+    rc = ble_hs_hci_util_rand(csrk, 16);
     if (rc != 0) {
         return rc;
     }
@@ -875,8 +875,8 @@ ble_sm_start_encrypt_tx(struct hci_start_encrypt *cmd)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_START_ENCRYPT_LEN];
     int rc;
 
-    host_hci_cmd_build_le_start_encrypt(cmd, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_start_encrypt(cmd, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1059,9 +1059,9 @@ ble_sm_ltk_req_reply_tx(uint16_t conn_handle, uint8_t *ltk)
     cmd.conn_handle = conn_handle;
     memcpy(cmd.long_term_key, ltk, 16);
 
-    host_hci_cmd_build_le_lt_key_req_reply(&cmd, buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
-                        &ack_params_len);
+    ble_hs_hci_cmd_build_le_lt_key_req_reply(&cmd, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
+                           &ack_params_len);
     if (rc != 0) {
         return rc;
     }
@@ -1085,9 +1085,9 @@ ble_sm_ltk_req_neg_reply_tx(uint16_t conn_handle)
     uint8_t ack_params_len;
     int rc;
 
-    host_hci_cmd_build_le_lt_key_req_neg_reply(conn_handle, buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
-                        &ack_params_len);
+    ble_hs_hci_cmd_build_le_lt_key_req_neg_reply(conn_handle, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
+                           &ack_params_len);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_sm_alg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_alg.c b/net/nimble/host/src/ble_sm_alg.c
index 3aac60d..a2b0fcf 100644
--- a/net/nimble/host/src/ble_sm_alg.c
+++ b/net/nimble/host/src/ble_sm_alg.c
@@ -410,7 +410,8 @@ ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2,
 }
 
 int
-ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y, uint32_t *passkey)
+ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y,
+              uint32_t *passkey)
 {
     uint8_t m[80], xs[16];
     int rc;
@@ -476,7 +477,7 @@ ble_sm_alg_gen_key_pair(void *pub, uint32_t *priv)
     int rc;
 
     do {
-        rc = ble_hci_util_rand(random, sizeof random);
+        rc = ble_hs_hci_util_rand(random, sizeof random);
         if (rc != 0) {
             return rc;
         }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/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 455da90..539d0f9 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -221,7 +221,7 @@ ble_sm_sc_gen_ri(struct ble_sm_proc *proc)
         return 0;
 
     case BLE_SM_PAIR_ALG_OOB:
-        rc = ble_hci_util_rand(&proc->ri, 1);
+        rc = ble_hs_hci_util_rand(&proc->ri, 1);
         return rc;
 
     default:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/host_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c
deleted file mode 100644
index f5d46a3..0000000
--- a/net/nimble/host/src/host_dbg.c
+++ /dev/null
@@ -1,503 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <stdint.h>
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>
-#include "os/os.h"
-#include "console/console.h"
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "ble_hs_priv.h"
-
-static void
-host_hci_dbg_le_event_disp(uint8_t subev, uint8_t len, uint8_t *evdata)
-{
-    int8_t rssi;
-    uint8_t advlen;
-    uint8_t status;
-    int i;
-    int imax;
-    uint8_t *dptr;
-    char *adv_ptr;
-    char adv_data_buf[32];
-
-    switch (subev) {
-    case BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE:
-    case BLE_HCI_LE_SUBEV_CONN_COMPLETE:
-        status = evdata[0];
-        if (status == BLE_ERR_SUCCESS) {
-            BLE_HS_LOG(DEBUG, "LE connection complete. handle=%u role=%u "
-                              "paddrtype=%u addr=%x.%x.%x.%x.%x.%x ",
-                       le16toh(evdata + 1), evdata[3], evdata[4],
-                       evdata[10], evdata[9], evdata[8], evdata[7],
-                       evdata[6], evdata[5]);
-
-            evdata += 11;
-            if (subev == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) {
-                BLE_HS_LOG(DEBUG, "local_rpa=%x.%x.%x.%x.%x.%x "
-                                   "peer_rpa=%x.%x.%x.%x.%x.%x ",
-                           evdata[5], evdata[4], evdata[3], evdata[2],
-                           evdata[1], evdata[0],
-                           evdata[11], evdata[10], evdata[9], evdata[8],
-                           evdata[7], evdata[6]);
-
-                evdata += 12;
-            }
-            BLE_HS_LOG(DEBUG, "itvl=%u latency=%u spvn_tmo=%u mca=%u\n",
-                       le16toh(evdata), le16toh(evdata + 2),
-                       le16toh(evdata + 4), evdata[6]);
-        } else {
-            BLE_HS_LOG(DEBUG, "LE connection complete. FAIL (status=%u)\n",
-                       status);
-        }
-        break;
-    case BLE_HCI_LE_SUBEV_ADV_RPT:
-        advlen = evdata[9];
-        rssi = evdata[10 + advlen];
-        BLE_HS_LOG(DEBUG, "LE advertising report. len=%u num=%u evtype=%u "
-                          "addrtype=%u addr=%x.%x.%x.%x.%x.%x advlen=%u "
-                          "rssi=%d\n", len, evdata[0], evdata[1], evdata[2],
-                   evdata[8], evdata[7], evdata[6], evdata[5],
-                   evdata[4], evdata[3], advlen, rssi);
-        if (advlen) {
-            dptr = &evdata[10];
-            while (advlen > 0) {
-                memset(adv_data_buf, 0, 32);
-                imax = advlen;
-                if (imax > 8) {
-                    imax = 8;
-                }
-                adv_ptr = &adv_data_buf[0];
-                for (i = 0; i < imax; ++i) {
-                    snprintf(adv_ptr, 4, "%02x ", *dptr);
-                    adv_ptr += 3;
-                    ++dptr;
-                }
-                advlen -= imax;
-                BLE_HS_LOG(DEBUG, "%s\n", adv_data_buf);
-            }
-        }
-        break;
-    case BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE:
-        status = evdata[0];
-        if (status == BLE_ERR_SUCCESS) {
-            BLE_HS_LOG(DEBUG, "LE Connection Update Complete. handle=%u "
-                              "itvl=%u latency=%u timeout=%u\n",
-                       le16toh(evdata + 1), le16toh(evdata + 3),
-                       le16toh(evdata + 5), le16toh(evdata + 7));
-        } else {
-            BLE_HS_LOG(DEBUG, "LE Connection Update Complete. FAIL "
-                              "(status=%u)\n", status);
-        }
-        break;
-
-    case BLE_HCI_LE_SUBEV_DATA_LEN_CHG:
-        BLE_HS_LOG(DEBUG, "LE Data Length Change. handle=%u max_tx_bytes=%u "
-                          "max_tx_time=%u max_rx_bytes=%u max_rx_time=%u\n",
-                   le16toh(evdata), le16toh(evdata + 2),
-                   le16toh(evdata + 4), le16toh(evdata + 6),
-                   le16toh(evdata + 8));
-        break;
-    case BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ:
-        BLE_HS_LOG(DEBUG, "LE Remote Connection Parameter Request. handle=%u "
-                          "min_itvl=%u max_itvl=%u latency=%u timeout=%u\n",
-                   le16toh(evdata), le16toh(evdata + 2),
-                   le16toh(evdata + 4), le16toh(evdata + 6),
-                   le16toh(evdata + 8));
-        break;
-
-    case BLE_HCI_LE_SUBEV_RD_REM_USED_FEAT:
-        status = evdata[0];
-        if (status == BLE_ERR_SUCCESS) {
-            BLE_HS_LOG(DEBUG, "LE Remote Used Features. handle=%u feat=",
-                       le16toh(evdata + 1));
-            for (i = 0; i < BLE_HCI_RD_LOC_SUPP_FEAT_RSPLEN; ++i) {
-                BLE_HS_LOG(DEBUG, "%02x ", evdata[3 + i]);
-            }
-            BLE_HS_LOG(DEBUG, "\n");
-        } else {
-            BLE_HS_LOG(DEBUG, "LE Remote Used Features. FAIL (status=%u)\n",
-                       status);
-        }
-        break;
-
-    case BLE_HCI_LE_SUBEV_LT_KEY_REQ:
-            BLE_HS_LOG(DEBUG, "LE LTK Req. handle=%u rand=%lx%lx encdiv=%u\n",
-                       le16toh(evdata), le32toh(evdata + 6),
-                       le32toh(evdata + 2), le16toh(evdata + 10));
-        break;
-
-    default:
-        BLE_HS_LOG(DEBUG, "\tUnknown LE event\n");
-        break;
-    }
-}
-
-/**
- * Display a disconnection complete command.
- *
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_disconn_comp_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t status;
-    uint8_t reason;
-    uint16_t handle;
-
-    status = evdata[0];
-    handle = le16toh(evdata + 1);
-    /* Ignore reason if status is not success */
-    if (status != BLE_ERR_SUCCESS) {
-        reason = 0;
-    } else {
-        reason = evdata[3];
-    }
-    BLE_HS_LOG(DEBUG, "Disconnection Complete: status=%u handle=%u "
-                      "reason=%u\n", status, handle, reason);
-}
-
-/**
- * Display an encryption change event or encryption key refresh event
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_encrypt_chg_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t status;
-    uint8_t enabled;
-    uint16_t handle;
-
-    status = evdata[0];
-    handle = le16toh(evdata + 1);
-
-    /* Ignore reason if status is not success */
-    if (status != BLE_ERR_SUCCESS) {
-        enabled = 0;
-    } else {
-        enabled = evdata[3];
-    }
-    BLE_HS_LOG(DEBUG, "Encrypt change: status=%u handle=%u state=%u\n",
-               status, handle, enabled);
-}
-
-/**
- * Display an encryption encryption key refresh event
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_encrypt_refresh_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t status;
-    uint16_t handle;
-
-    status = evdata[0];
-    handle = le16toh(evdata + 1);
-
-    BLE_HS_LOG(DEBUG, "Encrypt key refresh: status=%u handle=%u\n",
-               status, handle);
-}
-
-/**
- * Display a version information event
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_rd_rem_ver_disp(uint8_t *evdata, uint8_t len)
-{
-    BLE_HS_LOG(DEBUG, "Remote Version Info: status=%u handle=%u vers_nr=%u "
-                      "compid=%u subver=%u\n",
-               evdata[0], le16toh(evdata + 1), evdata[3],
-               le16toh(evdata + 4), le16toh(evdata + 6));
-}
-
-/**
- * Display the number of completed packets event
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_num_comp_pkts_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t handles;
-    uint8_t *handle_ptr;
-    uint8_t *pkt_ptr;
-    uint16_t handle;
-    uint16_t pkts;
-
-    handles = evdata[0];
-    if (len != ((handles * 4) + 1)) {
-        BLE_HS_LOG(DEBUG, "ERR: Number of Completed Packets bad length: "
-                          "num_handles=%u len=%u\n", handles, len);
-        return;
-
-    }
-
-    BLE_HS_LOG(DEBUG, "Number of Completed Packets: num_handles=%u\n",
-               handles);
-    if (handles) {
-        handle_ptr = evdata + 1;
-        pkt_ptr = handle_ptr + (2 * handles);
-        while (handles) {
-            handle = le16toh(handle_ptr);
-            handle_ptr += 2;
-            pkts = le16toh(pkt_ptr);
-            pkt_ptr += 2;
-            BLE_HS_LOG(DEBUG, "handle:%u pkts:%u\n", handle, pkts);
-            --handles;
-        }
-    }
-}
-
-/**
- * Display the authenticated payload timeout event
- *
- * @param evdata
- * @param len
- */
-static void
-host_hci_dbg_auth_pyld_tmo_disp(uint8_t *evdata, uint8_t len)
-{
-    uint16_t handle;
-
-    if (len != sizeof(uint16_t)) {
-        BLE_HS_LOG(DEBUG, "ERR: AuthPyldTmoEvent bad length %u\n", len);
-        return;
-
-    }
-
-    handle = le16toh(evdata);
-    BLE_HS_LOG(DEBUG, "AuthPyldTmo: handle=%u\n", handle);
-}
-
-
-static void
-host_hci_dbg_cmd_comp_info_params(uint8_t status, uint8_t ocf, uint8_t *evdata)
-{
-    int i;
-    uint8_t *dptr;
-
-    if (status != BLE_ERR_SUCCESS) {
-        return;
-    }
-
-    switch (ocf) {
-    case BLE_HCI_OCF_IP_RD_LOCAL_VER:
-        BLE_HS_LOG(DEBUG, "hci_ver=%u hci_rev=%u lmp_ver=%u mfrg=%u "
-                          "lmp_subver=%u",
-                   evdata[0], le16toh(evdata + 1), evdata[3],
-                   le16toh(evdata + 4), le16toh(evdata + 6));
-        break;
-    case BLE_HCI_OCF_IP_RD_LOC_SUPP_CMD:
-        BLE_HS_LOG(DEBUG, "supp_cmds=");
-        dptr = evdata;
-        for (i = 0; i < 8; ++i) {
-            BLE_HS_LOG(DEBUG, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:",
-                       dptr[0], dptr[1], dptr[2], dptr[3],
-                       dptr[4], dptr[5], dptr[6], dptr[7]);
-            dptr += 8;
-        }
-        break;
-    case BLE_HCI_OCF_IP_RD_LOC_SUPP_FEAT:
-        BLE_HS_LOG(DEBUG, "supp_feat=0x%lx%08lx",
-                   le32toh(evdata + 4), le32toh(evdata));
-        break;
-    case BLE_HCI_OCF_IP_RD_BD_ADDR:
-        BLE_HS_LOG(DEBUG, "bd_addr=%x:%x:%x:%x:%x:%x",
-                   evdata[5], evdata[4], evdata[3],
-                   evdata[2], evdata[1], evdata[0]);
-        break;
-    default:
-        break;
-    }
-}
-
-static void
-host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t cmd_pkts;
-    uint8_t ogf;
-    uint8_t ocf;
-    uint8_t status;
-    uint16_t opcode;
-
-    if (len < 3) {
-        BLE_HS_LOG(DEBUG, "Invalid command complete: len=%d "
-                          "(expected >= 3)", len);
-        goto done;
-    }
-
-    cmd_pkts = evdata[0];
-    opcode = le16toh(evdata + 1);
-    ogf = BLE_HCI_OGF(opcode);
-    ocf = BLE_HCI_OCF(opcode);
-
-    BLE_HS_LOG(DEBUG, "Command complete: cmd_pkts=%u ogf=0x%x ocf=0x%x",
-               cmd_pkts, ogf, ocf);
-
-    if (len == 3) {
-        goto done;
-    }
-
-    status = evdata[3];
-    BLE_HS_LOG(DEBUG, " status=%u ", status);
-
-    /* Move past header and status */
-    evdata += 4;
-
-    /* Display parameters based on command. */
-    switch (ogf) {
-    case BLE_HCI_OGF_INFO_PARAMS:
-        host_hci_dbg_cmd_comp_info_params(status, ocf, evdata);
-        break;
-    case BLE_HCI_OGF_STATUS_PARAMS:
-        switch (ocf) {
-        case BLE_HCI_OCF_RD_RSSI:
-            BLE_HS_LOG(DEBUG, "handle=%u rssi=%d", le16toh(evdata),
-                       (int8_t)evdata[2]);
-            break;
-        default:
-            break;
-        }
-        break;
-    case BLE_HCI_OGF_LE:
-        switch (ocf) {
-        case BLE_HCI_OCF_LE_RD_CHAN_MAP:
-            BLE_HS_LOG(DEBUG, "handle=%u chanmap=%x.%x.%x.%x.%x",
-                       le16toh(evdata), evdata[2], evdata[3], evdata[4],
-                       evdata[5], evdata[6]);
-            break;
-        case BLE_HCI_OCF_LE_RD_MAX_DATA_LEN:
-            BLE_HS_LOG(DEBUG, "txoct=%u txtime=%u rxoct=%u rxtime=%u",
-                       le16toh(evdata), le16toh(evdata + 2),
-                       le16toh(evdata + 4), le16toh(evdata + 6));
-            break;
-        case BLE_HCI_OCF_LE_RD_SUPP_STATES:
-            BLE_HS_LOG(DEBUG, "states=0x%lx%08lx", le32toh(evdata + 4),
-                       le32toh(evdata));
-            break;
-        case BLE_HCI_OCF_LE_ENCRYPT:
-            BLE_HS_LOG(DEBUG, "encdata=0x%02x%02x%02x%02x%02x%02x%02x%02x",
-                       evdata[15], evdata[14], evdata[13], evdata[12],
-                       evdata[11], evdata[10], evdata[9], evdata[8]);
-            BLE_HS_LOG(DEBUG, "%02x%02x%02x%02x%02x%02x%02x%02x",
-                       evdata[7], evdata[6], evdata[5], evdata[4],
-                       evdata[3], evdata[2], evdata[1], evdata[0]);
-
-            break;
-        case BLE_HCI_OCF_LE_RAND:
-            BLE_HS_LOG(DEBUG, "rand=0x%02x%02x%02x%02x%02x%02x%02x%02x",
-                       evdata[0], evdata[1], evdata[2], evdata[3],
-                       evdata[4], evdata[5], evdata[6], evdata[7]);
-            break;
-        case BLE_HCI_OCF_LE_RD_SUGG_DEF_DATA_LEN:
-            BLE_HS_LOG(DEBUG, "txoct=%u txtime=%u", le16toh(evdata),
-                       le16toh(evdata + 2));
-            break;
-        case BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY:
-        case BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY:
-        case BLE_HCI_OCF_LE_SET_DATA_LEN:
-            BLE_HS_LOG(DEBUG, "handle=%u", le16toh(evdata));
-            break;
-        default:
-            break;
-        }
-        break;
-    default:
-        break;
-    }
-
-done:
-    BLE_HS_LOG(DEBUG, "\n");
-}
-
-static void
-host_hci_dbg_cmd_status_disp(uint8_t *evdata, uint8_t len)
-{
-    uint8_t ogf;
-    uint8_t ocf;
-    uint16_t opcode;
-
-    opcode = le16toh(evdata + 2);
-    ogf = BLE_HCI_OGF(opcode);
-    ocf = BLE_HCI_OCF(opcode);
-
-    BLE_HS_LOG(DEBUG, "Command Status: status=%u cmd_pkts=%u ocf=0x%x "
-                      "ogf=0x%x\n", evdata[0], evdata[1], ocf, ogf);
-}
-
-void
-host_hci_dbg_event_disp(uint8_t *evbuf)
-{
-#if LOG_LEVEL > LOG_LEVEL_DEBUG
-    return;
-#endif
-
-    uint8_t *evdata;
-    uint8_t evcode;
-    uint8_t len;
-
-    /* Extract event code and length; move pointer to event parameter data */
-    evcode = evbuf[0];
-    len = evbuf[1];
-    evdata = evbuf + BLE_HCI_EVENT_HDR_LEN;
-
-    switch (evcode) {
-    case BLE_HCI_EVCODE_DISCONN_CMP:
-        host_hci_dbg_disconn_comp_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_ENC_KEY_REFRESH:
-        host_hci_dbg_encrypt_refresh_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_ENCRYPT_CHG:
-        host_hci_dbg_encrypt_chg_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_RD_REM_VER_INFO_CMP:
-        host_hci_dbg_rd_rem_ver_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
-        host_hci_dbg_cmd_complete_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_COMMAND_STATUS:
-        host_hci_dbg_cmd_status_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_NUM_COMP_PKTS:
-        host_hci_dbg_num_comp_pkts_disp(evdata, len);
-        break;
-    case BLE_HCI_EVCODE_LE_META:
-        host_hci_dbg_le_event_disp(evdata[0], len, evdata + 1);
-        break;
-    case BLE_HCI_EVCODE_AUTH_PYLD_TMO:
-        host_hci_dbg_auth_pyld_tmo_disp(evdata, len);
-        break;
-    default:
-        BLE_HS_LOG(DEBUG, "Unknown event 0x%x len=%u\n", evcode, len);
-        break;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/host_dbg_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg_priv.h b/net/nimble/host/src/host_dbg_priv.h
deleted file mode 100644
index 8d548ca..0000000
--- a/net/nimble/host/src/host_dbg_priv.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HOST_DBG_
-#define H_HOST_DBG_
-
-void host_hci_dbg_event_disp(uint8_t *evbuf);
-
-#endif /* H_HOST_DBG_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/host_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c
deleted file mode 100644
index b9bdaf9..0000000
--- a/net/nimble/host/src/host_hci.c
+++ /dev/null
@@ -1,899 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <stdint.h>
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#include "os/os.h"
-#include "console/console.h"
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/host_hci.h"
-#include "host/ble_gap.h"
-#include "ble_hs_priv.h"
-#include "host_dbg_priv.h"
-
-_Static_assert(sizeof (struct hci_data_hdr) == BLE_HCI_DATA_HDR_SZ,
-               "struct hci_data_hdr must be 4 bytes");
-
-typedef int host_hci_event_fn(uint8_t event_code, uint8_t *data, int len);
-static host_hci_event_fn host_hci_rx_disconn_complete;
-static host_hci_event_fn host_hci_rx_encrypt_change;
-static host_hci_event_fn host_hci_rx_hw_error;
-static host_hci_event_fn host_hci_rx_num_completed_pkts;
-static host_hci_event_fn host_hci_rx_enc_key_refresh;
-static host_hci_event_fn host_hci_rx_le_meta;
-
-typedef int host_hci_le_event_fn(uint8_t subevent, uint8_t *data, int len);
-static host_hci_le_event_fn host_hci_rx_le_conn_complete;
-static host_hci_le_event_fn host_hci_rx_le_adv_rpt;
-static host_hci_le_event_fn host_hci_rx_le_conn_upd_complete;
-static host_hci_le_event_fn host_hci_rx_le_lt_key_req;
-static host_hci_le_event_fn host_hci_rx_le_conn_parm_req;
-static host_hci_le_event_fn host_hci_rx_le_dir_adv_rpt;
-
-static uint16_t host_hci_buffer_sz;
-static uint8_t host_hci_max_pkts;
-
-/* Statistics */
-struct host_hci_stats
-{
-    uint32_t events_rxd;
-    uint32_t good_acks_rxd;
-    uint32_t bad_acks_rxd;
-    uint32_t unknown_events_rxd;
-};
-
-#define HOST_HCI_TIMEOUT        50      /* Milliseconds. */
-
-/** Dispatch table for incoming HCI events.  Sorted by event code field. */
-struct host_hci_event_dispatch_entry {
-    uint8_t hed_event_code;
-    host_hci_event_fn *hed_fn;
-};
-
-static const struct host_hci_event_dispatch_entry host_hci_event_dispatch[] = {
-    { BLE_HCI_EVCODE_DISCONN_CMP, host_hci_rx_disconn_complete },
-    { BLE_HCI_EVCODE_ENCRYPT_CHG, host_hci_rx_encrypt_change },
-    { BLE_HCI_EVCODE_HW_ERROR, host_hci_rx_hw_error },
-    { BLE_HCI_EVCODE_NUM_COMP_PKTS, host_hci_rx_num_completed_pkts },
-    { BLE_HCI_EVCODE_ENC_KEY_REFRESH, host_hci_rx_enc_key_refresh },
-    { BLE_HCI_EVCODE_LE_META, host_hci_rx_le_meta },
-};
-
-#define HOST_HCI_EVENT_DISPATCH_SZ \
-    (sizeof host_hci_event_dispatch / sizeof host_hci_event_dispatch[0])
-
-/** Dispatch table for incoming LE meta events.  Sorted by subevent field. */
-struct host_hci_le_event_dispatch_entry {
-    uint8_t hmd_subevent;
-    host_hci_le_event_fn *hmd_fn;
-};
-
-static const struct host_hci_le_event_dispatch_entry
-        host_hci_le_event_dispatch[] = {
-    { BLE_HCI_LE_SUBEV_CONN_COMPLETE, host_hci_rx_le_conn_complete },
-    { BLE_HCI_LE_SUBEV_ADV_RPT, host_hci_rx_le_adv_rpt },
-    { BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE, host_hci_rx_le_conn_upd_complete },
-    { BLE_HCI_LE_SUBEV_LT_KEY_REQ, host_hci_rx_le_lt_key_req },
-    { BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ, host_hci_rx_le_conn_parm_req },
-    { BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE, host_hci_rx_le_conn_complete },
-    { BLE_HCI_LE_SUBEV_DIRECT_ADV_RPT, host_hci_rx_le_dir_adv_rpt },
-};
-
-#define HOST_HCI_LE_EVENT_DISPATCH_SZ \
-    (sizeof host_hci_le_event_dispatch / sizeof host_hci_le_event_dispatch[0])
-
-uint16_t
-host_hci_opcode_join(uint8_t ogf, uint16_t ocf)
-{
-    return (ogf << 10) | ocf;
-}
-
-uint16_t
-host_hci_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc)
-{
-    BLE_HS_DBG_ASSERT(handle <= 0x0fff);
-    BLE_HS_DBG_ASSERT(pb <= 0x03);
-    BLE_HS_DBG_ASSERT(bc <= 0x03);
-
-    return (handle  << 0)   |
-           (pb      << 12)  |
-           (bc      << 14);
-}
-
-static const struct host_hci_event_dispatch_entry *
-host_hci_dispatch_entry_find(uint8_t event_code)
-{
-    const struct host_hci_event_dispatch_entry *entry;
-    int i;
-
-    for (i = 0; i < HOST_HCI_EVENT_DISPATCH_SZ; i++) {
-        entry = host_hci_event_dispatch + i;
-        if (entry->hed_event_code == event_code) {
-            return entry;
-        }
-    }
-
-    return NULL;
-}
-
-static const struct host_hci_le_event_dispatch_entry *
-host_hci_le_dispatch_entry_find(uint8_t event_code)
-{
-    const struct host_hci_le_event_dispatch_entry *entry;
-    int i;
-
-    for (i = 0; i < HOST_HCI_LE_EVENT_DISPATCH_SZ; i++) {
-        entry = host_hci_le_event_dispatch + i;
-        if (entry->hmd_subevent == event_code) {
-            return entry;
-        }
-    }
-
-    return NULL;
-}
-
-static int
-host_hci_rx_disconn_complete(uint8_t event_code, uint8_t *data, int len)
-{
-    struct hci_disconn_complete evt;
-
-    if (len < BLE_HCI_EVENT_DISCONN_COMPLETE_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.status = data[2];
-    evt.connection_handle = le16toh(data + 3);
-    evt.reason = data[5];
-
-    ble_gap_rx_disconn_complete(&evt);
-
-    return 0;
-}
-
-static int
-host_hci_rx_encrypt_change(uint8_t event_code, uint8_t *data, int len)
-{
-    struct hci_encrypt_change evt;
-
-    if (len < BLE_HCI_EVENT_ENCRYPT_CHG_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.status = data[2];
-    evt.connection_handle = le16toh(data + 3);
-    evt.encryption_enabled = data[5];
-
-    ble_sm_enc_change_rx(&evt);
-
-    return 0;
-}
-
-static int
-host_hci_rx_hw_error(uint8_t event_code, uint8_t *data, int len)
-{
-    uint8_t hw_code;
-
-    if (len < BLE_HCI_EVENT_HW_ERROR_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    hw_code = data[0];
-    ble_hs_hw_error(hw_code);
-
-    return 0;
-}
-
-static int
-host_hci_rx_enc_key_refresh(uint8_t event_code, uint8_t *data, int len)
-{
-    struct hci_encrypt_key_refresh evt;
-
-    if (len < BLE_HCI_EVENT_ENC_KEY_REFRESH_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.status = data[2];
-    evt.connection_handle = le16toh(data + 3);
-
-    ble_sm_enc_key_refresh_rx(&evt);
-
-    return 0;
-}
-
-static int
-host_hci_rx_num_completed_pkts(uint8_t event_code, uint8_t *data, int len)
-{
-    uint16_t num_pkts;
-    uint16_t handle;
-    uint8_t num_handles;
-    int off;
-    int i;
-
-    if (len < BLE_HCI_EVENT_HDR_LEN + BLE_HCI_EVENT_NUM_COMP_PKTS_HDR_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    off = BLE_HCI_EVENT_HDR_LEN;
-    num_handles = data[off];
-    if (len < BLE_HCI_EVENT_NUM_COMP_PKTS_HDR_LEN +
-              num_handles * BLE_HCI_EVENT_NUM_COMP_PKTS_ENT_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-    off++;
-
-    for (i = 0; i < num_handles; i++) {
-        handle = le16toh(data + off + 2 * i);
-        num_pkts = le16toh(data + off + 2 * num_handles + 2 * i);
-
-        /* XXX: Do something with these values. */
-        (void)handle;
-        (void)num_pkts;
-    }
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_meta(uint8_t event_code, uint8_t *data, int len)
-{
-    const struct host_hci_le_event_dispatch_entry *entry;
-    uint8_t subevent;
-    int rc;
-
-    if (len < BLE_HCI_EVENT_HDR_LEN + BLE_HCI_LE_MIN_LEN) {
-        /* XXX: Increment stat. */
-        return BLE_HS_ECONTROLLER;
-    }
-
-    subevent = data[2];
-    entry = host_hci_le_dispatch_entry_find(subevent);
-    if (entry != NULL) {
-        rc = entry->hmd_fn(subevent, data + BLE_HCI_EVENT_HDR_LEN,
-                           len - BLE_HCI_EVENT_HDR_LEN);
-        if (rc != 0) {
-            return rc;
-        }
-    }
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_conn_complete(uint8_t subevent, uint8_t *data, int len)
-{
-    struct hci_le_conn_complete evt;
-    int extended_offset = 0;
-    int rc;
-
-    if (len < BLE_HCI_LE_CONN_COMPLETE_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    /* this code processes two different events that are really similar */
-    if ((subevent == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) &&
-        ( len < BLE_HCI_LE_ENH_CONN_COMPLETE_LEN)) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.subevent_code = data[0];
-    evt.status = data[1];
-    evt.connection_handle = le16toh(data + 2);
-    evt.role = data[4];
-    evt.peer_addr_type = data[5];
-    memcpy(evt.peer_addr, data + 6, BLE_DEV_ADDR_LEN);
-
-    /* enhanced connection event has the same information with these
-     * extra fields stuffed into the middle */
-    if (subevent == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) {
-        memcpy(evt.local_rpa, data + 12, BLE_DEV_ADDR_LEN);
-        memcpy(evt.peer_rpa, data + 18, BLE_DEV_ADDR_LEN);
-        extended_offset = 12;
-    } else {
-        memset(evt.local_rpa, 0, BLE_DEV_ADDR_LEN);
-        memset(evt.peer_rpa, 0, BLE_DEV_ADDR_LEN);
-    }
-
-    evt.conn_itvl = le16toh(data + 12 + extended_offset);
-    evt.conn_latency = le16toh(data + 14 + extended_offset);
-    evt.supervision_timeout = le16toh(data + 16 + extended_offset);
-    evt.master_clk_acc = data[18 + extended_offset];
-
-    if (evt.status == 0) {
-        if (evt.role != BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER &&
-            evt.role != BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE) {
-
-            return BLE_HS_EBADDATA;
-        }
-    }
-
-    rc = ble_gap_rx_conn_complete(&evt);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static int
-host_hci_le_adv_rpt_first_pass(uint8_t *data, int len,
-                               uint8_t *out_num_reports, int *out_rssi_off)
-{
-    uint8_t num_reports;
-    int data_len;
-    int off;
-    int i;
-
-    if (len < BLE_HCI_LE_ADV_RPT_MIN_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    num_reports = data[1];
-    if (num_reports < BLE_HCI_LE_ADV_RPT_NUM_RPTS_MIN ||
-        num_reports > BLE_HCI_LE_ADV_RPT_NUM_RPTS_MAX) {
-
-        return BLE_HS_EBADDATA;
-    }
-
-    off = 2 +       /* Subevent code and num reports. */
-          (1 +      /* Event type. */
-           1 +      /* Address type. */
-           6        /* Address. */
-          ) * num_reports;
-    if (off + num_reports >= len) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    data_len = 0;
-    for (i = 0; i < num_reports; i++) {
-        data_len += data[off];
-        off++;
-    }
-
-    off += data_len;
-
-    /* Check if RSSI fields fit in the packet. */
-    if (off + num_reports > len) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    *out_num_reports = num_reports;
-    *out_rssi_off = off;
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_adv_rpt(uint8_t subevent, uint8_t *data, int len)
-{
-    struct ble_gap_disc_desc desc;
-    uint8_t num_reports;
-    int rssi_off;
-    int data_off;
-    int suboff;
-    int off;
-    int rc;
-    int i;
-
-    rc = host_hci_le_adv_rpt_first_pass(data, len, &num_reports, &rssi_off);
-    if (rc != 0) {
-        return rc;
-    }
-
-    /* Direct address fields not present in a standard advertising report. */
-    desc.direct_addr_type = BLE_GAP_ADDR_TYPE_NONE;
-    memset(desc.direct_addr, 0, sizeof desc.direct_addr);
-
-    data_off = 0;
-    for (i = 0; i < num_reports; i++) {
-        suboff = 0;
-
-        off = 2 + suboff * num_reports + i;
-        desc.event_type = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + i;
-        desc.addr_type = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + i * 6;
-        memcpy(desc.addr, data + off, 6);
-        suboff += 6;
-
-        off = 2 + suboff * num_reports + i;
-        desc.length_data = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + data_off;
-        desc.data = data + off;
-        data_off += desc.length_data;
-
-        off = rssi_off + 1 * i;
-        desc.rssi = data[off];
-
-        ble_gap_rx_adv_report(&desc);
-    }
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_dir_adv_rpt(uint8_t subevent, uint8_t *data, int len)
-{
-    struct ble_gap_disc_desc desc;
-    uint8_t num_reports;
-    int suboff;
-    int off;
-    int i;
-
-    if (len < BLE_HCI_LE_ADV_DIRECT_RPT_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    num_reports = data[1];
-    if (len != 2 + num_reports * BLE_HCI_LE_ADV_DIRECT_RPT_SUB_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    /* Data fields not present in a direct advertising report. */
-    desc.data = NULL;
-    desc.fields = NULL;
-
-    for (i = 0; i < num_reports; i++) {
-        suboff = 0;
-
-        off = 2 + suboff * num_reports + i;
-        desc.event_type = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + i;
-        desc.addr_type = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + i * 6;
-        memcpy(desc.addr, data + off, 6);
-        suboff += 6;
-
-        off = 2 + suboff * num_reports + i;
-        desc.direct_addr_type = data[off];
-        suboff++;
-
-        off = 2 + suboff * num_reports + i * 6;
-        memcpy(desc.direct_addr, data + off, 6);
-        suboff += 6;
-
-        off = 2 + suboff * num_reports + i;
-        desc.rssi = data[off];
-        suboff++;
-
-        ble_gap_rx_adv_report(&desc);
-    }
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_conn_upd_complete(uint8_t subevent, uint8_t *data, int len)
-{
-    struct hci_le_conn_upd_complete evt;
-
-    if (len < BLE_HCI_LE_CONN_UPD_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.subevent_code = data[0];
-    evt.status = data[1];
-    evt.connection_handle = le16toh(data + 2);
-    evt.conn_itvl = le16toh(data + 4);
-    evt.conn_latency = le16toh(data + 6);
-    evt.supervision_timeout = le16toh(data + 8);
-
-    if (evt.status == 0) {
-        if (evt.conn_itvl < BLE_HCI_CONN_ITVL_MIN ||
-            evt.conn_itvl > BLE_HCI_CONN_ITVL_MAX) {
-
-            return BLE_HS_EBADDATA;
-        }
-        if (evt.conn_latency < BLE_HCI_CONN_LATENCY_MIN ||
-            evt.conn_latency > BLE_HCI_CONN_LATENCY_MAX) {
-
-            return BLE_HS_EBADDATA;
-        }
-        if (evt.supervision_timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN ||
-            evt.supervision_timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX) {
-
-            return BLE_HS_EBADDATA;
-        }
-    }
-
-    ble_gap_rx_update_complete(&evt);
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_lt_key_req(uint8_t subevent, uint8_t *data, int len)
-{
-    struct hci_le_lt_key_req evt;
-
-    if (len < BLE_HCI_LE_LT_KEY_REQ_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.subevent_code = data[0];
-    evt.connection_handle = le16toh(data + 1);
-    evt.random_number = le64toh(data + 3);
-    evt.encrypted_diversifier = le16toh(data + 11);
-
-    ble_sm_ltk_req_rx(&evt);
-
-    return 0;
-}
-
-static int
-host_hci_rx_le_conn_parm_req(uint8_t subevent, uint8_t *data, int len)
-{
-    struct hci_le_conn_param_req evt;
-
-    if (len < BLE_HCI_LE_REM_CONN_PARM_REQ_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    evt.subevent_code = data[0];
-    evt.connection_handle = le16toh(data + 1);
-    evt.itvl_min = le16toh(data + 3);
-    evt.itvl_max = le16toh(data + 5);
-    evt.latency = le16toh(data + 7);
-    evt.timeout = le16toh(data + 9);
-
-    if (evt.itvl_min < BLE_HCI_CONN_ITVL_MIN ||
-        evt.itvl_max > BLE_HCI_CONN_ITVL_MAX ||
-        evt.itvl_min > evt.itvl_max) {
-
-        return BLE_HS_EBADDATA;
-    }
-    if (evt.latency < BLE_HCI_CONN_LATENCY_MIN ||
-        evt.latency > BLE_HCI_CONN_LATENCY_MAX) {
-
-        return BLE_HS_EBADDATA;
-    }
-    if (evt.timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN ||
-        evt.timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX) {
-
-        return BLE_HS_EBADDATA;
-    }
-
-    ble_gap_rx_param_req(&evt);
-
-    return 0;
-}
-
-int
-host_hci_set_buf_size(uint16_t pktlen, uint8_t max_pkts)
-{
-    if (pktlen == 0 || max_pkts == 0) {
-        return BLE_HS_EINVAL;
-    }
-
-    host_hci_buffer_sz = pktlen;
-    host_hci_max_pkts = max_pkts;
-
-    return 0;
-}
-
-int
-host_hci_evt_process(uint8_t *data)
-{
-    const struct host_hci_event_dispatch_entry *entry;
-    uint8_t event_code;
-    uint8_t param_len;
-    int event_len;
-    int rc;
-
-    /* Count events received */
-    STATS_INC(ble_hs_stats, hci_event);
-
-    /* Display to console */
-    host_hci_dbg_event_disp(data);
-
-    /* Process the event */
-    event_code = data[0];
-    param_len = data[1];
-
-    event_len = param_len + 2;
-
-    entry = host_hci_dispatch_entry_find(event_code);
-    if (entry == NULL) {
-        STATS_INC(ble_hs_stats, hci_unknown_event);
-        rc = BLE_HS_ENOTSUP;
-    } else {
-        rc = entry->hed_fn(event_code, data, event_len);
-    }
-
-    ble_hci_trans_buf_free(data);
-
-    return rc;
-}
-
-int
-host_hci_evt_rx(uint8_t *hci_ev, void *arg)
-{
-    int enqueue;
-
-    BLE_HS_DBG_ASSERT(hci_ev != NULL);
-
-    switch (hci_ev[0]) {
-    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
-    case BLE_HCI_EVCODE_COMMAND_STATUS:
-        if (hci_ev[3] == 0 && hci_ev[4] == 0) {
-            enqueue = 1;
-        } else {
-            ble_hci_cmd_rx_ack(hci_ev);
-            enqueue = 0;
-        }
-        break;
-
-    default:
-        enqueue = 1;
-        break;
-    }
-
-    if (enqueue) {
-        ble_hs_enqueue_hci_event(hci_ev);
-    }
-
-    return 0;
-}
-
-/**
- * Called when a data packet is received from the controller.  This function
- * consumes the supplied mbuf, regardless of the outcome.
- *
- * @param om                    The incoming data packet, beginning with the
- *                                  HCI ACL data header.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-host_hci_acl_process(struct os_mbuf *om)
-{
-    struct hci_data_hdr hci_hdr;
-    struct ble_hs_conn *conn;
-    ble_l2cap_rx_fn *rx_cb;
-    struct os_mbuf *rx_buf;
-    uint16_t handle;
-    int rc;
-
-    rc = ble_hci_util_data_hdr_strip(om, &hci_hdr);
-    if (rc != 0) {
-        goto err;
-    }
-
-#if (BLETEST_THROUGHPUT_TEST == 0)
-    BLE_HS_LOG(DEBUG, "host_hci_acl_process(): handle=%u pb=%x len=%u data=",
-               BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc), 
-               BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc), 
-               hci_hdr.hdh_len);
-    ble_hs_log_mbuf(om);
-    BLE_HS_LOG(DEBUG, "\n");
-#endif
-
-    if (hci_hdr.hdh_len != OS_MBUF_PKTHDR(om)->omp_len) {
-        rc = BLE_HS_EBADDATA;
-        goto err;
-    }
-
-    handle = BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc);
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(handle);
-    if (conn == NULL) {
-        rc = BLE_HS_ENOTCONN;
-    } else {
-        rc = ble_l2cap_rx(conn, &hci_hdr, om, &rx_cb, &rx_buf);
-        om = NULL;
-    }
-
-    ble_hs_unlock();
-
-    switch (rc) {
-    case 0:
-        /* Final fragment received. */
-        BLE_HS_DBG_ASSERT(rx_cb != NULL);
-        BLE_HS_DBG_ASSERT(rx_buf != NULL);
-        rc = rx_cb(handle, &rx_buf);
-        os_mbuf_free_chain(rx_buf);
-        break;
-
-    case BLE_HS_EAGAIN:
-        /* More fragments on the way. */
-        break;
-
-    default:
-        goto err;
-    }
-
-    return 0;
-
-err:
-    os_mbuf_free_chain(om);
-    return rc;
-}
-
-static struct os_mbuf *
-host_hci_data_hdr_prepend(struct os_mbuf *om, uint16_t handle, uint8_t pb_flag)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om2;
-
-    hci_hdr.hdh_handle_pb_bc = host_hci_handle_pb_bc_join(handle, pb_flag, 0);
-    htole16(&hci_hdr.hdh_len, OS_MBUF_PKTHDR(om)->omp_len);
-
-    om2 = os_mbuf_prepend(om, sizeof hci_hdr);
-    if (om2 == NULL) {
-        return NULL;
-    }
-
-    om = om2;
-    om = os_mbuf_pullup(om, sizeof hci_hdr);
-    if (om == NULL) {
-        return NULL;
-    }
-
-    memcpy(om->om_data, &hci_hdr, sizeof hci_hdr);
-
-    BLE_HS_LOG(DEBUG, "host tx hci data; handle=%d length=%d\n", handle,
-               le16toh(&hci_hdr.hdh_len));
-
-    return om;
-}
-
-/**
- * Splits an appropriately-sized fragment from the front of an outgoing ACL
- * data packet, if necessary.  If the packet size is within the controller's
- * buffer size requirements, no splitting is performed.  The fragment data is
- * removed from the data packet mbuf.
- *
- * @param om                    The ACL data packet.
- * @param out_frag              On success, this points to the fragment to
- *                                  send.  If the entire packet can fit within
- *                                  a single fragment, this will point to the
- *                                  ACL data packet itself ('om').
- *
- * @return                      BLE_HS_EDONE: success; this is the final
- *                                  fragment.
- *                              BLE_HS_EAGAIN: success; more data remains in
- *                                  the original mbuf.
- *                              Other BLE host core return code on error.
- */
-int
-host_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
-{
-    struct os_mbuf *frag;
-    int rc;
-
-    if (OS_MBUF_PKTLEN(*om) <= host_hci_buffer_sz) {
-        /* Final fragment. */
-        *out_frag = *om;
-        *om = NULL;
-        return BLE_HS_EDONE;
-    }
-
-    frag = ble_hs_mbuf_acm_pkt();
-    if (frag == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    /* Move data from the front of the packet into the fragment mbuf. */
-    rc = os_mbuf_appendfrom(frag, *om, 0, host_hci_buffer_sz);
-    if (rc != 0) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-    os_mbuf_adj(*om, host_hci_buffer_sz);
-
-    /* More fragments to follow. */
-    *out_frag = frag;
-    return BLE_HS_EAGAIN;
-
-err:
-    os_mbuf_free_chain(frag);
-    return rc;
-}
-
-/**
- * Transmits an HCI ACL data packet.  This function consumes the supplied mbuf,
- * regardless of the outcome.
- *
- * XXX: Ensure the controller has sufficient buffer capacity for the outgoing
- * fragments.
- */
-int
-host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
-{
-    struct os_mbuf *frag;
-    uint8_t pb;
-    int done;
-    int rc;
-
-    /* The first fragment uses the first-non-flush packet boundary value.
-     * After sending the first fragment, pb gets set appropriately for all
-     * subsequent fragments in this packet.
-     */
-    pb = BLE_HCI_PB_FIRST_NON_FLUSH;
-
-    /* Send fragments until the entire packet has been sent. */
-    done = 0;
-    while (!done) {
-        rc = host_hci_split_frag(&txom, &frag);
-        switch (rc) {
-        case BLE_HS_EDONE:
-            /* This is the final fragment. */
-            done = 1;
-            break;
-
-        case BLE_HS_EAGAIN:
-            /* More fragments to follow. */
-            break;
-
-        default:
-            goto err;
-        }
-
-        frag = host_hci_data_hdr_prepend(frag, connection->bhc_handle, pb);
-        if (frag == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-        pb = BLE_HCI_PB_MIDDLE;
-
-        BLE_HS_LOG(DEBUG, "host_hci_data_tx(): ");
-        ble_hs_log_mbuf(frag);
-        BLE_HS_LOG(DEBUG, "\n");
-
-        /* XXX: Try to pullup the entire fragment.  The controller currently
-         * requires the entire fragment to fit in a single buffer.  When this
-         * restriction is removed from the controller, this operation can be
-         * removed.
-         */
-        frag = os_mbuf_pullup(frag, OS_MBUF_PKTLEN(frag));
-        if (frag == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-
-        rc = ble_hs_tx_data(frag);
-        if (rc != 0) {
-            goto err;
-        }
-
-        connection->bhc_outstanding_pkts++;
-    }
-
-    return 0;
-
-err:
-    BLE_HS_DBG_ASSERT(rc != 0);
-
-    os_mbuf_free_chain(txom);
-    return rc;
-}


[03/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Use os_eventq_poll()

Posted by we...@apache.org.
BLE Host - Use os_eventq_poll()

Before, the host code implemented an eventq "peek" operation:
* disable interrupts
* check whether anything is on the eventq
* enable interrupts
* if something was on the queue, call os_eventq_get().

Unlike os_eventq_get(), os_eventq_poll() takes a timeout parameter.  Now
the host calls os_eventq_poll() with a timeout of 0 to perform a
non-blocking read of the eventq.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 93f8d4daf7594f35420f7eea4d44137ee1e94bd5
Parents: 7defc1f
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 13:39:25 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93f8d4da/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 8035b49..225b26c 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -330,17 +330,19 @@ static void
 ble_hs_event_handle(void *unused)
 {
     struct os_callout_func *cf;
+    struct os_eventq *evqp;
     struct os_event *ev;
     uint8_t *hci_evt;
-    os_sr_t sr;
     int rc;
     int i;
 
+    evqp = &ble_hs_evq;
+
     i = 0;
     while (1) {
         /* If the host has already processed several consecutive events, stop
          * and return control to the parent task.  Put an event on the parent
-         * task's eventq so indicate that more host events are enqueued.
+         * task's eventq to indicate that more host events are enqueued.
          */
         if (i >= BLE_HS_MAX_EVS_IN_A_ROW) {
             os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
@@ -348,15 +350,11 @@ ble_hs_event_handle(void *unused)
         }
         i++;
 
-        OS_ENTER_CRITICAL(sr);
-        ev = STAILQ_FIRST(&ble_hs_evq.evq_list);
-        OS_EXIT_CRITICAL(sr);
-
+        ev = os_eventq_poll(&evqp, 1, 0);
         if (ev == NULL) {
             break;
         }
 
-        ev = os_eventq_get(&ble_hs_evq);
         switch (ev->ev_type) {
         case OS_EVENT_T_TIMER:
             cf = (struct os_callout_func *)ev;


[43/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-358: nrf51 cannot connect to directed advertisers w/privacy

Posted by we...@apache.org.
MYNEWT-358: nrf51 cannot connect to directed advertisers w/privacy

In order to fix this issue I have added the local RPA to the resolving list and
modified the code so that the RPA timer is a true LL timer that will be used
to regenerate local RPAs at the specified timeout. This allows the local RPA
to be generated early and not during an IFS interval


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 6a5c8ae64fa31bffbf1424ffcd6930a57892586b
Parents: b0e058b
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Aug 11 14:19:56 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 .../include/controller/ble_ll_resolv.h          |   7 +-
 net/nimble/controller/src/ble_ll_adv.c          |  17 +++
 net/nimble/controller/src/ble_ll_conn.c         |   1 +
 net/nimble/controller/src/ble_ll_resolv.c       | 150 +++++++++++++++----
 net/nimble/controller/src/ble_ll_scan.c         |   2 +-
 5 files changed, 142 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a5c8ae6/net/nimble/controller/include/controller/ble_ll_resolv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_resolv.h b/net/nimble/controller/include/controller/ble_ll_resolv.h
index 251543b..6c63c79 100644
--- a/net/nimble/controller/include/controller/ble_ll_resolv.h
+++ b/net/nimble/controller/include/controller/ble_ll_resolv.h
@@ -23,15 +23,18 @@
 /*
  * An entry in the resolving list.
  *      The identity address is stored in little endian format.
+ *      The local rpa is stored in little endian format.
  *      The IRKs are stored in big endian format.
  */
 struct ble_ll_resolv_entry
 {
-    uint8_t rl_reserved;
     uint8_t rl_addr_type;
-    uint8_t rl_identity_addr[BLE_DEV_ADDR_LEN];
+    uint8_t rl_local_rpa_set;
+    uint16_t rl_reserved;
     uint8_t rl_local_irk[16];
     uint8_t rl_peer_irk[16];
+    uint8_t rl_identity_addr[BLE_DEV_ADDR_LEN];
+    uint8_t rl_local_rpa[BLE_DEV_ADDR_LEN];
 };
 
 extern struct ble_ll_resolv_entry g_ble_ll_resolv_list[];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a5c8ae6/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
index 1f43a5d..0c6df3b 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -121,6 +121,23 @@ struct ble_ll_adv_sm g_ble_ll_adv_sm;
 
 
 #if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+/**
+ * Called to change advertisers ADVA and INITA (for directed advertisements)
+ * as an advertiser needs to adhere to the resolvable private address generation
+ * timer.
+ *
+ * NOTE: the resolvable private address code uses its own timer to regenerate
+ * local resolvable private addresses. The advertising code uses its own
+ * timer to reset the INITA (for directed advertisements). This code also sets
+ * the appropriate txadd and rxadd bits that will go into the advertisement.
+ *
+ * Another thing to note: it is possible that an IRK is all zeroes in the
+ * resolving list. That is why we need to check if the generated address is
+ * in fact a RPA as a resolving list entry with all zeroes will use the
+ * identity address (which may be a private address or public).
+ *
+ * @param advsm
+ */
 void
 ble_ll_adv_chk_rpa_timeout(struct ble_ll_adv_sm *advsm)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a5c8ae6/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn.c b/net/nimble/controller/src/ble_ll_conn.c
index bfbdf99..fb95367 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -1904,6 +1904,7 @@ ble_ll_conn_event_end(void *arg)
      * The way this works is that whenever the timer expires it just gets reset
      * and we send the autheticated payload timeout event. Note that this timer
      * should run even when encryption is paused.
+     * XXX: what should be here? Was there code here that got deleted?
      */
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a5c8ae6/net/nimble/controller/src/ble_ll_resolv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_resolv.c b/net/nimble/controller/src/ble_ll_resolv.c
index b4d58b0..903c95f 100644
--- a/net/nimble/controller/src/ble_ll_resolv.c
+++ b/net/nimble/controller/src/ble_ll_resolv.c
@@ -31,12 +31,15 @@
 #include "ble_ll_conn_priv.h"
 
 #if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
-
-/* Flag denoting whether or not address translation is enabled. */
-uint8_t g_ble_ll_addr_res_enabled;
-uint8_t g_ble_ll_resolv_list_size;
-uint8_t g_ble_ll_resolv_list_cnt;
-uint32_t g_ble_ll_resolv_rpa_tmo;
+struct ble_ll_resolv_data
+{
+    uint8_t addr_res_enabled;
+    uint8_t rl_size;
+    uint8_t rl_cnt;
+    uint32_t rpa_tmo;
+    struct os_callout_func rpa_timer;
+};
+struct ble_ll_resolv_data g_ble_ll_resolv_data;
 
 struct ble_ll_resolv_entry g_ble_ll_resolv_list[NIMBLE_OPT_LL_RESOLV_LIST_SIZE];
 
@@ -53,9 +56,9 @@ ble_ll_resolv_list_chg_allowed(void)
 {
     int rc;
 
-    if (g_ble_ll_addr_res_enabled && (ble_ll_adv_enabled()  ||
-                                      ble_ll_scan_enabled() ||
-                                      g_ble_ll_conn_create_sm)) {
+    if (g_ble_ll_resolv_data.addr_res_enabled &&
+            (ble_ll_adv_enabled() || ble_ll_scan_enabled() ||
+             g_ble_ll_conn_create_sm)) {
         rc = 0;
     } else {
         rc = 1;
@@ -64,6 +67,32 @@ ble_ll_resolv_list_chg_allowed(void)
 }
 
 /**
+ * Called when the Resolvable private address timer expires. This timer
+ * is used to regenerate local RPA's in the resolving list.
+ *
+ * @param arg
+ */
+void
+ble_ll_resolv_rpa_timer_cb(void *arg)
+{
+    int i;
+    os_sr_t sr;
+    struct ble_ll_resolv_entry *rl;
+
+    rl = &g_ble_ll_resolv_list[0];
+    for (i = 0; i < g_ble_ll_resolv_data.rl_cnt; ++i) {
+        OS_ENTER_CRITICAL(sr);
+        rl->rl_local_rpa_set = 0;
+        ble_ll_resolv_gen_priv_addr(rl, 1, rl->rl_local_rpa);
+        rl->rl_local_rpa_set = 1;
+        OS_EXIT_CRITICAL(sr);
+        ++rl;
+    }
+    os_callout_reset(&g_ble_ll_resolv_data.rpa_timer.cf_c,
+                     (int32_t)g_ble_ll_resolv_data.rpa_tmo);
+}
+
+/**
  * Called to determine if the IRK is all zero.
  *
  * @param irk
@@ -102,7 +131,7 @@ ble_ll_resolv_list_clr(void)
     }
 
     /* Sets total on list to 0. Clears HW resolve list */
-    g_ble_ll_resolv_list_cnt = 0;
+    g_ble_ll_resolv_data.rl_cnt = 0;
     ble_hw_resolv_list_clear();
 
     return BLE_ERR_SUCCESS;
@@ -119,7 +148,7 @@ ble_ll_resolv_list_clr(void)
 int
 ble_ll_resolv_list_read_size(uint8_t *rspbuf, uint8_t *rsplen)
 {
-    rspbuf[0] = g_ble_ll_resolv_list_size;
+    rspbuf[0] = g_ble_ll_resolv_data.rl_size;
     *rsplen = 1;
     return BLE_ERR_SUCCESS;
 }
@@ -141,7 +170,7 @@ ble_ll_is_on_resolv_list(uint8_t *addr, uint8_t addr_type)
     struct ble_ll_resolv_entry *rl;
 
     rl = &g_ble_ll_resolv_list[0];
-    for (i = 0; i < g_ble_ll_resolv_list_cnt; ++i) {
+    for (i = 0; i < g_ble_ll_resolv_data.rl_cnt; ++i) {
         if ((rl->rl_addr_type == addr_type) &&
             (!memcmp(&rl->rl_identity_addr[0], addr, BLE_DEV_ADDR_LEN))) {
             return i + 1;
@@ -167,7 +196,7 @@ ble_ll_resolv_list_find(uint8_t *addr, uint8_t addr_type)
     struct ble_ll_resolv_entry *rl;
 
     rl = &g_ble_ll_resolv_list[0];
-    for (i = 0; i < g_ble_ll_resolv_list_cnt; ++i) {
+    for (i = 0; i < g_ble_ll_resolv_data.rl_cnt; ++i) {
         if ((rl->rl_addr_type == addr_type) &&
             (!memcmp(&rl->rl_identity_addr[0], addr, BLE_DEV_ADDR_LEN))) {
             return rl;
@@ -197,7 +226,7 @@ ble_ll_resolv_list_add(uint8_t *cmdbuf)
     }
 
     /* Check if we have any open entries */
-    if (g_ble_ll_resolv_list_cnt >= g_ble_ll_resolv_list_size) {
+    if (g_ble_ll_resolv_data.rl_cnt >= g_ble_ll_resolv_data.rl_size) {
         return BLE_ERR_MEM_CAPACITY;
     }
 
@@ -206,14 +235,23 @@ ble_ll_resolv_list_add(uint8_t *cmdbuf)
 
     rc = BLE_ERR_SUCCESS;
     if (!ble_ll_is_on_resolv_list(ident_addr, addr_type)) {
-        rl = &g_ble_ll_resolv_list[g_ble_ll_resolv_list_cnt];
+        rl = &g_ble_ll_resolv_list[g_ble_ll_resolv_data.rl_cnt];
         rl->rl_addr_type = addr_type;
+        rl->rl_local_rpa_set = 0;
         memcpy(&rl->rl_identity_addr[0], ident_addr, BLE_DEV_ADDR_LEN);
         swap_buf(rl->rl_peer_irk, cmdbuf + 7, 16);
         swap_buf(rl->rl_local_irk, cmdbuf + 23, 16);
-        ++g_ble_ll_resolv_list_cnt;
 
+        /*
+         * Add peer IRK to HW resolving list. If we can add it, also
+         * generate a local RPA now to save time later.
+         */
         rc = ble_hw_resolv_list_add(rl->rl_peer_irk);
+        if (!rc) {
+            ble_ll_resolv_gen_priv_addr(rl, 1, rl->rl_local_rpa);
+            rl->rl_local_rpa_set = 1;
+        }
+        ++g_ble_ll_resolv_data.rl_cnt;
     }
 
     return rc;
@@ -243,11 +281,11 @@ ble_ll_resolv_list_rmv(uint8_t *cmdbuf)
 
     /* Remove from IRK records */
     position = ble_ll_is_on_resolv_list(ident_addr, addr_type);
-    if (position && (position < g_ble_ll_resolv_list_cnt)) {
+    if (position && (position < g_ble_ll_resolv_data.rl_cnt)) {
         memmove(&g_ble_ll_resolv_list[position - 1],
                 &g_ble_ll_resolv_list[position],
-                g_ble_ll_resolv_list_cnt - position);
-        --g_ble_ll_resolv_list_cnt;
+                g_ble_ll_resolv_data.rl_cnt - position);
+        --g_ble_ll_resolv_data.rl_cnt;
 
         /* Remove from HW list */
         ble_hw_resolv_list_rmv(position - 1);
@@ -267,13 +305,29 @@ int
 ble_ll_resolv_enable_cmd(uint8_t *cmdbuf)
 {
     int rc;
+    int32_t tmo;
+    uint8_t enabled;
 
     if (ble_ll_adv_enabled() || ble_ll_scan_enabled() ||
         g_ble_ll_conn_create_sm) {
         rc = BLE_ERR_CMD_DISALLOWED;
     } else {
-        g_ble_ll_addr_res_enabled = cmdbuf[0];
-        rc = BLE_ERR_SUCCESS;
+        enabled = cmdbuf[0];
+        if (enabled <= 1) {
+            /* If we change state, we need to disable/enable the RPA timer */
+            if ((enabled ^ g_ble_ll_resolv_data.addr_res_enabled) != 0) {
+                if (enabled) {
+                    tmo = (int32_t)g_ble_ll_resolv_data.rpa_tmo;
+                    os_callout_reset(&g_ble_ll_resolv_data.rpa_timer.cf_c, tmo);
+                } else {
+                    os_callout_stop(&g_ble_ll_resolv_data.rpa_timer.cf_c);
+                }
+                g_ble_ll_resolv_data.addr_res_enabled = enabled;
+            }
+            rc = BLE_ERR_SUCCESS;
+        } else {
+            rc = BLE_ERR_INV_HCI_CMD_PARMS;
+        }
     }
 
     return rc;
@@ -306,7 +360,11 @@ ble_ll_resolv_set_rpa_tmo(uint8_t *cmdbuf)
 
     tmo_secs = le16toh(cmdbuf);
     if ((tmo_secs > 0) && (tmo_secs <= 0xA1B8)) {
-        g_ble_ll_resolv_rpa_tmo = tmo_secs * OS_TICKS_PER_SEC;
+        g_ble_ll_resolv_data.rpa_tmo = tmo_secs * OS_TICKS_PER_SEC;
+        if (g_ble_ll_resolv_data.addr_res_enabled) {
+            os_callout_reset(&g_ble_ll_resolv_data.rpa_timer.cf_c,
+                             (int32_t)g_ble_ll_resolv_data.rpa_tmo);
+        }
     } else {
         rc = BLE_ERR_INV_HCI_CMD_PARMS;
     }
@@ -323,12 +381,11 @@ ble_ll_resolv_set_rpa_tmo(uint8_t *cmdbuf)
 uint32_t
 ble_ll_resolv_get_rpa_tmo(void)
 {
-    return g_ble_ll_resolv_rpa_tmo;
+    return g_ble_ll_resolv_data.rpa_tmo;
 }
 
 /**
- * Called the generate a resolvable private address
- *
+ * Called to generate a resolvable private address
  *
  * @param rl
  * @param local
@@ -348,6 +405,12 @@ ble_ll_resolv_gen_priv_addr(struct ble_ll_resolv_entry *rl, int local,
     assert(rl != NULL);
     assert(addr != NULL);
 
+    /* If the local rpa has already been generated, just copy it */
+    if (local && rl->rl_local_rpa_set) {
+        memcpy(addr, rl->rl_local_rpa, BLE_DEV_ADDR_LEN);
+        return;
+    }
+
     /* Get prand */
     prand = addr + 3;
     ble_ll_rand_prand_get(prand);
@@ -427,11 +490,29 @@ int
 ble_ll_resolv_rpa(uint8_t *rpa, uint8_t *irk)
 {
     int rc;
+    uint32_t *irk32;
+    uint32_t *key32;
+    uint32_t *pt32;
     struct ble_encryption_block ecb;
 
-    memcpy(ecb.key, irk, BLE_ENC_BLOCK_SIZE);
-    memset(ecb.plain_text, 0, BLE_ENC_BLOCK_SIZE);
-    swap_buf(&ecb.plain_text[13], rpa + 3, 3);
+    irk32 = (uint32_t *)irk;
+    key32 = (uint32_t *)&ecb.key[0];
+
+    key32[0] = irk32[0];
+    key32[1] = irk32[1];
+    key32[2] = irk32[2];
+    key32[3] = irk32[3];
+
+    pt32 = (uint32_t *)&ecb.plain_text[0];
+    pt32[0] = 0;
+    pt32[1] = 0;
+    pt32[2] = 0;
+    pt32[3] = 0;
+
+    ecb.plain_text[15] = rpa[3];
+    ecb.plain_text[14] = rpa[4];
+    ecb.plain_text[13] = rpa[5];
+
     ble_hw_encrypt_block(&ecb);
     if ((ecb.cipher_text[15] == rpa[0]) && (ecb.cipher_text[14] == rpa[1]) &&
         (ecb.cipher_text[13] == rpa[2])) {
@@ -451,7 +532,7 @@ ble_ll_resolv_rpa(uint8_t *rpa, uint8_t *irk)
 uint8_t
 ble_ll_resolv_enabled(void)
 {
-    return g_ble_ll_addr_res_enabled;
+    return g_ble_ll_resolv_data.addr_res_enabled;
 }
 
 /**
@@ -460,7 +541,8 @@ ble_ll_resolv_enabled(void)
 void
 ble_ll_resolv_list_reset(void)
 {
-    g_ble_ll_addr_res_enabled = 0;
+    g_ble_ll_resolv_data.addr_res_enabled = 0;
+    os_callout_stop(&g_ble_ll_resolv_data.rpa_timer.cf_c);
     ble_ll_resolv_list_clr();
     ble_ll_resolv_init();
 }
@@ -471,14 +553,18 @@ ble_ll_resolv_init(void)
     uint8_t hw_size;
 
     /* Default is 15 minutes */
-    g_ble_ll_resolv_rpa_tmo = 15 * 60 * OS_TICKS_PER_SEC;
+    g_ble_ll_resolv_data.rpa_tmo = 15 * 60 * OS_TICKS_PER_SEC;
 
     hw_size = ble_hw_resolv_list_size();
     if (hw_size > NIMBLE_OPT_LL_RESOLV_LIST_SIZE) {
         hw_size = NIMBLE_OPT_LL_RESOLV_LIST_SIZE;
     }
-    g_ble_ll_resolv_list_size = hw_size;
+    g_ble_ll_resolv_data.rl_size = hw_size;
 
+    os_callout_func_init(&g_ble_ll_resolv_data.rpa_timer,
+                         &g_ble_ll_data.ll_evq,
+                         ble_ll_resolv_rpa_timer_cb,
+                         NULL);
 }
 
 #endif  /* if BLE_LL_CFG_FEAT_LL_PRIVACY == 1 */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a5c8ae6/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_scan.c b/net/nimble/controller/src/ble_ll_scan.c
index 0fc4e79..29193dc 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -448,7 +448,7 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t txadd, uint8_t *rxbuf,
                  * are 2 greater than the unresolved ones in the spec, so
                  * we just add 2 here.
                  */
-                addr_type += 2;
+                addr_type = g_ble_ll_resolv_list[index].rl_addr_type + 2;
             } else{
                 adv_addr = rxbuf;
             }


[12/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Free mbufs on exec-write-failure

Posted by we...@apache.org.
BLE Host - Free mbufs on exec-write-failure

This was an mbuf leak.  If the device receives an exec-write request,
and the attribute callback fails (or the attribute doesn't allow writes
at all), the mbufs holding the data intended to be written to that
attribute never got freed.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: a2d04082bb1bdb84f14bdb33546162ce5856f3d5
Parents: ab25d84
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Aug 6 16:04:17 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c           |  6 ++++--
 net/nimble/host/src/ble_gattc.c             |  8 ++++++++
 net/nimble/host/src/test/ble_att_svr_test.c | 14 +++++++++++++-
 3 files changed, 25 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2d04082/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 2e1f158..b3dd2f3 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -2347,9 +2347,11 @@ ble_att_svr_prep_write(uint16_t conn_handle,
             *err_handle = attr_handle;
         } else {
             rc = ble_att_svr_write(conn_handle, attr, 0, &om, &att_err);
+            os_mbuf_free_chain(om);
+            if (rc != 0) {
+                return att_err;
+            }
         }
-
-        os_mbuf_free_chain(om);
     }
 
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2d04082/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 84ecff3..67c13d6 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -177,6 +177,7 @@ struct ble_gattc_proc {
         struct {
             struct ble_gatt_attr attr;
             uint16_t length;
+            uint8_t exec_sent:1;
             ble_gatt_attr_fn *cb;
             void *cb_arg;
         } write_long;
@@ -186,6 +187,7 @@ struct ble_gattc_proc {
             uint8_t num_attrs;
             uint8_t cur_attr;
             uint16_t length;
+            uint8_t exec_sent:1;
             ble_gatt_reliable_attr_fn *cb;
             void *cb_arg;
         } write_reliable;
@@ -3528,6 +3530,12 @@ static int
 ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status)
 {
     ble_gattc_dbg_assert_proc_not_inserted(proc);
+
+    /* Ignore the response if we haven't sent the corresponding request yet. */
+    if (!proc->write_long.exec_sent) {
+        return 0;
+    }
+
     ble_gattc_write_long_cb(proc, status, 0);
     return BLE_HS_EDONE;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a2d04082/net/nimble/host/src/test/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_svr_test.c b/net/nimble/host/src/test/ble_att_svr_test.c
index e5968d5..09e7bf9 100644
--- a/net/nimble/host/src/test/ble_att_svr_test.c
+++ b/net/nimble/host/src/test/ble_att_svr_test.c
@@ -2083,12 +2083,16 @@ TEST_CASE(ble_att_svr_test_prep_write)
         data[i] = i;
     }
 
-    /* Register two attributes. */
+    /* Register two writable attributes. */
     ble_att_svr_test_misc_register_uuid16(0x1234, HA_FLAG_PERM_RW, 1,
                                           ble_att_svr_test_misc_attr_fn_w_1);
     ble_att_svr_test_misc_register_uuid16(0x8989, HA_FLAG_PERM_RW, 2,
                                           ble_att_svr_test_misc_attr_fn_w_2);
 
+    /* Register a third attribute that is not writable. */
+    ble_att_svr_test_misc_register_uuid16(0xabab, BLE_ATT_F_READ, 3,
+                                          ble_att_svr_test_misc_attr_fn_r_1);
+
     /*** Empty write succeeds. */
     ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
                                      0, 0);
@@ -2132,6 +2136,14 @@ TEST_CASE(ble_att_svr_test_prep_write)
                                      BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 1);
     ble_att_svr_test_misc_verify_w_1(NULL, 0);
 
+    /*** Failure due to attribute callback. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 3, 0, data, 35, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 3, 35, data + 35, 43, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 3, 78, data + 78, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_WRITE_NOT_PERMITTED, 0);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
     /*** Successful two part write. */
     ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 20, 0);
     ble_att_svr_test_misc_prep_write(conn_handle, 1, 20, data + 20, 20, 0);


[21/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Clean up ACL tx fragmentation.

Posted by we...@apache.org.
BLE Host - Clean up ACL tx fragmentation.

This commit does not result in a change in behavior.  The fragmentation
code was overly complicated.  This change just simplifies the
implementation.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: c7fbdb8a51d160a247171ef1f704ee4be4920783
Parents: 5c65aaf
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Aug 5 12:18:13 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs_hci.c | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c7fbdb8a/net/nimble/host/src/ble_hs_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci.c b/net/nimble/host/src/ble_hs_hci.c
index cd5cc6c..433c2e0 100644
--- a/net/nimble/host/src/ble_hs_hci.c
+++ b/net/nimble/host/src/ble_hs_hci.c
@@ -359,17 +359,16 @@ ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg)
  * buffer size requirements, no splitting is performed.  The fragment data is
  * removed from the data packet mbuf.
  *
- * @param om                    The ACL data packet.
+ * @param om                    The ACL data packet.  If this constitutes a
+ *                                  single fragment, it gets set to NULL on
+ *                                  success.
  * @param out_frag              On success, this points to the fragment to
  *                                  send.  If the entire packet can fit within
  *                                  a single fragment, this will point to the
  *                                  ACL data packet itself ('om').
  *
- * @return                      BLE_HS_EDONE: success; this is the final
- *                                  fragment.
- *                              BLE_HS_EAGAIN: success; more data remains in
- *                                  the original mbuf.
- *                              Other BLE host core return code on error.
+ * @return                      0 on success;
+ *                              BLE host core return code on error.
  */
 static int
 ble_hs_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
@@ -377,11 +376,14 @@ ble_hs_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
     struct os_mbuf *frag;
     int rc;
 
+    /* Assume failure. */
+    *out_frag = NULL;
+
     if (OS_MBUF_PKTLEN(*om) <= ble_hs_hci_buf_sz) {
         /* Final fragment. */
         *out_frag = *om;
         *om = NULL;
-        return BLE_HS_EDONE;
+        return 0;
     }
 
     frag = ble_hs_mbuf_acm_pkt();
@@ -400,7 +402,7 @@ ble_hs_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
 
     /* More fragments to follow. */
     *out_frag = frag;
-    return BLE_HS_EAGAIN;
+    return 0;
 
 err:
     os_mbuf_free_chain(frag);
@@ -449,7 +451,6 @@ ble_hs_hci_acl_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
 {
     struct os_mbuf *frag;
     uint8_t pb;
-    int done;
     int rc;
 
     /* The first fragment uses the first-non-flush packet boundary value.
@@ -459,20 +460,9 @@ ble_hs_hci_acl_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
     pb = BLE_HCI_PB_FIRST_NON_FLUSH;
 
     /* Send fragments until the entire packet has been sent. */
-    done = 0;
-    while (!done) {
+    while (txom != NULL) {
         rc = ble_hs_hci_split_frag(&txom, &frag);
-        switch (rc) {
-        case BLE_HS_EDONE:
-            /* This is the final fragment. */
-            done = 1;
-            break;
-
-        case BLE_HS_EAGAIN:
-            /* More fragments to follow. */
-            break;
-
-        default:
+        if (rc != 0) {
             goto err;
         }
 


[19/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Don't assume leading space in l2cap tx.

Posted by we...@apache.org.
BLE Host - Don't assume leading space in l2cap tx.

Call os_mbuf_prepend_pullup() rather than calling os_mbuf_prepend() and
assuming the resulting buffer is the same as the input.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 4b24ccae17f92f8deba58f4d2de45b69577d890c
Parents: c7fbdb8
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Aug 5 12:19:15 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4b24ccae/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 66bad51..43ccd6f 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -119,16 +119,14 @@ struct os_mbuf *
 ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid, uint16_t len)
 {
     struct ble_l2cap_hdr hdr;
-    struct os_mbuf *om2;
 
     htole16(&hdr.blh_len, len);
     htole16(&hdr.blh_cid, cid);
 
-    om2 = os_mbuf_prepend(om, sizeof hdr);
-    if (om2 == NULL) {
+    om = os_mbuf_prepend_pullup(om, sizeof hdr);
+    if (om == NULL) {
         return NULL;
     }
-    BLE_HS_DBG_ASSERT(om2 == om);
 
     memcpy(om->om_data, &hdr, sizeof hdr);
 


[24/50] [abbrv] incubator-mynewt-core git commit: bletiny - Be consistent with addr type strings.

Posted by we...@apache.org.
bletiny - Be consistent with addr type strings.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 582201db07f0c9bcf68449f070e921cc2d19f3c8
Parents: dff04c8
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 18:31:32 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/cmd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/582201db/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 1d133da..e50d6b0 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -363,8 +363,8 @@ cmd_adv(int argc, char **argv)
 static struct kv_pair cmd_conn_peer_addr_types[] = {
     { "public",         BLE_HCI_CONN_PEER_ADDR_PUBLIC },
     { "random",         BLE_HCI_CONN_PEER_ADDR_RANDOM },
-    { "public_ident",   BLE_HCI_CONN_PEER_ADDR_PUBLIC_IDENT },
-    { "random_ident",   BLE_HCI_CONN_PEER_ADDR_RANDOM_IDENT },
+    { "rpa_pub",        BLE_HCI_CONN_PEER_ADDR_PUBLIC_IDENT },
+    { "rpa_rnd",        BLE_HCI_CONN_PEER_ADDR_RANDOM_IDENT },
     { "wl",             BLE_GAP_ADDR_TYPE_WL },
     { NULL }
 };


[25/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Free mbuf on os_mqueue_put() failure.

Posted by we...@apache.org.
BLE Host - Free mbuf on os_mqueue_put() failure.

This fixes a theoretical mbuf leak.  In practice, the call to
os_mqueue_put() will never fail.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 5c65aafea8e366336e5bae303b665efef3c3d560
Parents: 5f3def8
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Aug 5 12:16:40 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c65aafe/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 225b26c..02db879 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -496,6 +496,15 @@ ble_hs_rx_data(struct os_mbuf *om, void *arg)
     return rc;
 }
 
+/**
+ * Enqueues an ACL data packet for transmission.  This function consumes the
+ * supplied mbuf, regardless of the outcome.
+ *
+ * @param om                    The outgoing data packet, beginning with the
+ *                                  HCI ACL data header.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
 int
 ble_hs_tx_data(struct os_mbuf *om)
 {
@@ -503,6 +512,7 @@ ble_hs_tx_data(struct os_mbuf *om)
 
     rc = os_mqueue_put(&ble_hs_tx_q, &ble_hs_evq, om);
     if (rc != 0) {
+        os_mbuf_free_chain(om);
         return BLE_HS_EOS;
     }
     os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);


[05/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by we...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/host_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci_cmd.c b/net/nimble/host/src/host_hci_cmd.c
deleted file mode 100644
index cce2e26..0000000
--- a/net/nimble/host/src/host_hci_cmd.c
+++ /dev/null
@@ -1,1364 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <stdint.h>
-#include <assert.h>
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#include "os/os.h"
-#include "console/console.h"
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/host_hci.h"
-#include "host_dbg_priv.h"
-#include "ble_hs_priv.h"
-
-static int
-host_hci_cmd_transport(uint8_t *cmdbuf)
-{
-    int rc;
-
-    rc = ble_hci_trans_hs_cmd_tx(cmdbuf);
-    switch (rc) {
-    case 0:
-        return 0;
-
-    case BLE_ERR_MEM_CAPACITY:
-        return BLE_HS_ENOMEM_EVT;
-
-    default:
-        return BLE_HS_EUNKNOWN;
-    }
-}
-
-void
-host_hci_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
-{
-    uint16_t opcode;
-    uint8_t *u8ptr;
-
-    u8ptr = buf;
-
-    opcode = (ogf << 10) | ocf;
-    htole16(u8ptr, opcode);
-    u8ptr[2] = len;
-}
-
-int
-host_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
-{
-    uint8_t *buf;
-    int rc;
-
-    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
-    BLE_HS_DBG_ASSERT(buf != NULL);
-
-    htole16(buf, ogf << 10 | ocf);
-    buf[2] = len;
-    if (len != 0) {
-        memcpy(buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
-    }
-
-    BLE_HS_LOG(DEBUG, "host_hci_cmd_send: ogf=0x%02x ocf=0x%02x len=%d\n",
-               ogf, ocf, len);
-    ble_hs_log_flat_buf(buf, len + BLE_HCI_CMD_HDR_LEN);
-    BLE_HS_LOG(DEBUG, "\n");
-    rc = host_hci_cmd_transport(buf);
-
-    if (rc == 0) {
-        STATS_INC(ble_hs_stats, hci_cmd);
-    } else {
-        BLE_HS_LOG(DEBUG, "host_hci_cmd_send failure; rc=%d\n", rc);
-    }
-
-    return rc;
-}
-
-int
-host_hci_cmd_send_buf(void *buf)
-{
-    uint16_t opcode;
-    uint8_t *u8ptr;
-    uint8_t len;
-    int rc;
-
-    switch (ble_hs_sync_state) {
-    case BLE_HS_SYNC_STATE_BAD:
-        return BLE_HS_ENOTSYNCED;
-
-    case BLE_HS_SYNC_STATE_BRINGUP:
-        if (!ble_hs_is_parent_task()) {
-            return BLE_HS_ENOTSYNCED;
-        }
-        break;
-
-    case BLE_HS_SYNC_STATE_GOOD:
-        break;
-
-    default:
-        BLE_HS_DBG_ASSERT(0);
-        return BLE_HS_EUNKNOWN;
-    }
-
-    u8ptr = buf;
-
-    opcode = le16toh(u8ptr + 0);
-    len = u8ptr[2];
-
-    rc = host_hci_cmd_send(BLE_HCI_OGF(opcode), BLE_HCI_OCF(opcode), len,
-                           u8ptr + BLE_HCI_CMD_HDR_LEN);
-    return rc;
-}
-
-
-/**
- * Send a LE command from the host to the controller.
- *
- * @param ocf
- * @param len
- * @param cmddata
- *
- * @return int
- */
-static int
-host_hci_le_cmd_send(uint16_t ocf, uint8_t len, void *cmddata)
-{
-    int rc;
-    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, ocf, len, cmddata);
-    return rc;
-}
-
-/**
- * Read BD_ADDR
- *
- * OGF = 0x04 (Informational parameters)
- * OCF = 0x0009
- */
-void
-host_hci_cmd_build_read_bd_addr(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR,
-                       0, dst);
-}
-
-static int
-host_hci_cmd_body_le_whitelist_chg(const uint8_t *addr, uint8_t addr_type,
-                                   uint8_t *dst)
-{
-    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = addr_type;
-    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
-
-    return 0;
-}
-
-static int
-host_hci_cmd_body_le_set_adv_params(const struct hci_adv_params *adv,
-                                    uint8_t *dst)
-{
-    uint16_t itvl;
-
-    BLE_HS_DBG_ASSERT(adv != NULL);
-
-    /* Make sure parameters are valid */
-    if ((adv->adv_itvl_min > adv->adv_itvl_max) ||
-        (adv->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) ||
-        (adv->peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX) ||
-        (adv->adv_filter_policy > BLE_HCI_ADV_FILT_MAX) ||
-        (adv->adv_type > BLE_HCI_ADV_TYPE_MAX) ||
-        (adv->adv_channel_map == 0) ||
-        ((adv->adv_channel_map & 0xF8) != 0)) {
-        /* These parameters are not valid */
-        return -1;
-    }
-
-    /* Make sure interval is valid for advertising type. */
-    if ((adv->adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND) ||
-        (adv->adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND)) {
-        itvl = BLE_HCI_ADV_ITVL_NONCONN_MIN;
-    } else {
-        itvl = BLE_HCI_ADV_ITVL_MIN;
-    }
-
-    /* Do not check if high duty-cycle directed */
-    if (adv->adv_type != BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
-        if ((adv->adv_itvl_min < itvl) ||
-            (adv->adv_itvl_min > BLE_HCI_ADV_ITVL_MAX)) {
-            return -1;
-        }
-    }
-
-    htole16(dst, adv->adv_itvl_min);
-    htole16(dst + 2, adv->adv_itvl_max);
-    dst[4] = adv->adv_type;
-    dst[5] = adv->own_addr_type;
-    dst[6] = adv->peer_addr_type;
-    memcpy(dst + 7, adv->peer_addr, BLE_DEV_ADDR_LEN);
-    dst[13] = adv->adv_channel_map;
-    dst[14] = adv->adv_filter_policy;
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_le_set_adv_params(const struct hci_adv_params *adv,
-                                     uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_PARAM_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_PARAMS,
-                       BLE_HCI_SET_ADV_PARAM_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_set_adv_params(adv, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-/**
- * Set advertising data
- *
- * OGF = 0x08 (LE)
- * OCF = 0x0008
- *
- * @param data
- * @param len
- * @param dst
- *
- * @return int
- */
-static int
-host_hci_cmd_body_le_set_adv_data(const uint8_t *data, uint8_t len,
-                                  uint8_t *dst)
-{
-    /* Check for valid parameters */
-    if (((data == NULL) && (len != 0)) || (len > BLE_HCI_MAX_ADV_DATA_LEN)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    memset(dst, 0, BLE_HCI_SET_ADV_DATA_LEN);
-    dst[0] = len;
-    memcpy(dst + 1, data, len);
-
-    return 0;
-}
-
-/**
- * Set advertising data
- *
- * OGF = 0x08 (LE)
- * OCF = 0x0008
- *
- * @param data
- * @param len
- * @param dst
- *
- * @return int
- */
-int
-host_hci_cmd_build_le_set_adv_data(const uint8_t *data, uint8_t len,
-                                   uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_DATA_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_DATA,
-                       BLE_HCI_SET_ADV_DATA_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_set_adv_data(data, len, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static int
-host_hci_cmd_body_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
-                                       uint8_t *dst)
-{
-    /* Check for valid parameters */
-    if (((data == NULL) && (len != 0)) ||
-         (len > BLE_HCI_MAX_SCAN_RSP_DATA_LEN)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    memset(dst, 0, BLE_HCI_SET_SCAN_RSP_DATA_LEN);
-    dst[0] = len;
-    memcpy(dst + 1, data, len);
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
-                                        uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_RSP_DATA_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA,
-                       BLE_HCI_SET_SCAN_RSP_DATA_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_set_scan_rsp_data(data, len, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static void
-host_hci_cmd_body_set_event_mask(uint64_t event_mask, uint8_t *dst)
-{
-    htole64(dst, event_mask);
-}
-
-void
-host_hci_cmd_build_set_event_mask(uint64_t event_mask,
-                                  uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_EVENT_MASK_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND,
-                       BLE_HCI_OCF_CB_SET_EVENT_MASK,
-                       BLE_HCI_SET_EVENT_MASK_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_set_event_mask(event_mask, dst);
-}
-
-void
-host_hci_cmd_build_set_event_mask2(uint64_t event_mask,
-                                   uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_EVENT_MASK_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND,
-                       BLE_HCI_OCF_CB_SET_EVENT_MASK2,
-                       BLE_HCI_SET_EVENT_MASK_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_set_event_mask(event_mask, dst);
-}
-
-static void
-host_hci_cmd_body_disconnect(uint16_t handle, uint8_t reason, uint8_t *dst)
-{
-    htole16(dst + 0, handle);
-    dst[2] = reason;
-}
-
-void
-host_hci_cmd_build_disconnect(uint16_t handle, uint8_t reason,
-                              uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_DISCONNECT_CMD_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LINK_CTRL, BLE_HCI_OCF_DISCONNECT_CMD,
-                       BLE_HCI_DISCONNECT_CMD_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_disconnect(handle, reason, dst);
-}
-
-int
-host_hci_cmd_disconnect(uint16_t handle, uint8_t reason)
-{
-    uint8_t cmd[BLE_HCI_DISCONNECT_CMD_LEN];
-    int rc;
-
-    host_hci_cmd_body_disconnect(handle, reason, cmd);
-    rc = host_hci_cmd_send(BLE_HCI_OGF_LINK_CTRL,
-                           BLE_HCI_OCF_DISCONNECT_CMD,
-                           BLE_HCI_DISCONNECT_CMD_LEN,
-                           cmd);
-    return rc;
-}
-
-static void
-host_hci_cmd_body_le_set_event_mask(uint64_t event_mask, uint8_t *dst)
-{
-    htole64(dst, event_mask);
-}
-
-void
-host_hci_cmd_build_le_set_event_mask(uint64_t event_mask,
-                                     uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_LE_EVENT_MASK_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE,
-                       BLE_HCI_OCF_LE_SET_EVENT_MASK,
-                       BLE_HCI_SET_LE_EVENT_MASK_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_set_event_mask(event_mask, dst);
-}
-
-/**
- * LE Read buffer size
- *
- * OGF = 0x08 (LE)
- * OCF = 0x0002
- *
- * @return int
- */
-void
-host_hci_cmd_build_le_read_buffer_size(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_BUF_SIZE, 0, dst);
-}
-
-/**
- * LE Read buffer size
- *
- * OGF = 0x08 (LE)
- * OCF = 0x0002
- *
- * @return int
- */
-int
-host_hci_cmd_le_read_buffer_size(void)
-{
-    int rc;
-
-    rc = host_hci_le_cmd_send(BLE_HCI_OCF_LE_RD_BUF_SIZE, 0, NULL);
-    return rc;
-}
-
-/**
- * OGF=LE, OCF=0x0003
- */
-void
-host_hci_cmd_build_le_read_loc_supp_feat(uint8_t *dst, uint8_t dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT,
-                       0, dst);
-}
-
-static void
-host_hci_cmd_body_le_set_adv_enable(uint8_t enable, uint8_t *dst)
-{
-    dst[0] = enable;
-}
-
-void
-host_hci_cmd_build_le_set_adv_enable(uint8_t enable, uint8_t *dst,
-                                     int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_ENABLE_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE,
-                       BLE_HCI_SET_ADV_ENABLE_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_set_adv_enable(enable, dst);
-}
-
-static int
-host_hci_cmd_body_le_set_scan_params(
-    uint8_t scan_type, uint16_t scan_itvl, uint16_t scan_window,
-    uint8_t own_addr_type, uint8_t filter_policy, uint8_t *dst) {
-
-    /* Make sure parameters are valid */
-    if ((scan_type != BLE_HCI_SCAN_TYPE_PASSIVE) &&
-        (scan_type != BLE_HCI_SCAN_TYPE_ACTIVE)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check interval and window */
-    if ((scan_itvl < BLE_HCI_SCAN_ITVL_MIN) ||
-        (scan_itvl > BLE_HCI_SCAN_ITVL_MAX) ||
-        (scan_window < BLE_HCI_SCAN_WINDOW_MIN) ||
-        (scan_window > BLE_HCI_SCAN_WINDOW_MAX) ||
-        (scan_itvl < scan_window)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check own addr type */
-    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check scanner filter policy */
-    if (filter_policy > BLE_HCI_SCAN_FILT_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = scan_type;
-    htole16(dst + 1, scan_itvl);
-    htole16(dst + 3, scan_window);
-    dst[5] = own_addr_type;
-    dst[6] = filter_policy;
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_le_set_scan_params(uint8_t scan_type, uint16_t scan_itvl,
-                                      uint16_t scan_window,
-                                      uint8_t own_addr_type,
-                                      uint8_t filter_policy,
-                                      uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_PARAM_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
-                       BLE_HCI_SET_SCAN_PARAM_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_set_scan_params(scan_type, scan_itvl,
-                                              scan_window, own_addr_type,
-                                              filter_policy, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static void
-host_hci_cmd_body_le_set_scan_enable(uint8_t enable, uint8_t filter_dups,
-                                     uint8_t *dst)
-{
-    dst[0] = enable;
-    dst[1] = filter_dups;
-}
-
-void
-host_hci_cmd_build_le_set_scan_enable(uint8_t enable, uint8_t filter_dups,
-                                      uint8_t *dst, uint8_t dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_ENABLE_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
-                       BLE_HCI_SET_SCAN_ENABLE_LEN, dst);
-
-    host_hci_cmd_body_le_set_scan_enable(enable, filter_dups,
-                                         dst + BLE_HCI_CMD_HDR_LEN);
-}
-
-static int
-host_hci_cmd_body_le_create_connection(const struct hci_create_conn *hcc,
-                                       uint8_t *cmd)
-{
-    /* Check scan interval and scan window */
-    if ((hcc->scan_itvl < BLE_HCI_SCAN_ITVL_MIN) ||
-        (hcc->scan_itvl > BLE_HCI_SCAN_ITVL_MAX) ||
-        (hcc->scan_window < BLE_HCI_SCAN_WINDOW_MIN) ||
-        (hcc->scan_window > BLE_HCI_SCAN_WINDOW_MAX) ||
-        (hcc->scan_itvl < hcc->scan_window)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check initiator filter policy */
-    if (hcc->filter_policy > BLE_HCI_CONN_FILT_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check peer addr type */
-    if (hcc->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check own addr type */
-    if (hcc->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check connection interval min */
-    if ((hcc->conn_itvl_min < BLE_HCI_CONN_ITVL_MIN) ||
-        (hcc->conn_itvl_min > BLE_HCI_CONN_ITVL_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check connection interval max */
-    if ((hcc->conn_itvl_max < BLE_HCI_CONN_ITVL_MIN) ||
-        (hcc->conn_itvl_max > BLE_HCI_CONN_ITVL_MAX) ||
-        (hcc->conn_itvl_max < hcc->conn_itvl_min)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check connection latency */
-    if ((hcc->conn_latency < BLE_HCI_CONN_LATENCY_MIN) ||
-        (hcc->conn_latency > BLE_HCI_CONN_LATENCY_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check supervision timeout */
-    if ((hcc->supervision_timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN) ||
-        (hcc->supervision_timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check connection event length */
-    if (hcc->min_ce_len > hcc->max_ce_len) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    htole16(cmd + 0, hcc->scan_itvl);
-    htole16(cmd + 2, hcc->scan_window);
-    cmd[4] = hcc->filter_policy;
-    cmd[5] = hcc->peer_addr_type;
-    memcpy(cmd + 6, hcc->peer_addr, BLE_DEV_ADDR_LEN);
-    cmd[12] = hcc->own_addr_type;
-    htole16(cmd + 13, hcc->conn_itvl_min);
-    htole16(cmd + 15, hcc->conn_itvl_max);
-    htole16(cmd + 17, hcc->conn_latency);
-    htole16(cmd + 19, hcc->supervision_timeout);
-    htole16(cmd + 21, hcc->min_ce_len);
-    htole16(cmd + 23, hcc->max_ce_len);
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_le_create_connection(const struct hci_create_conn *hcc,
-                                        uint8_t *cmd, int cmd_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        cmd_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CREATE_CONN_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN,
-                       BLE_HCI_CREATE_CONN_LEN, cmd);
-
-    rc = host_hci_cmd_body_le_create_connection(hcc,
-                                                cmd + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-void
-host_hci_cmd_build_le_clear_whitelist(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLEAR_WHITE_LIST,
-                       0, dst);
-}
-
-int
-host_hci_cmd_build_le_add_to_whitelist(const uint8_t *addr, uint8_t addr_type,
-                                       uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CHG_WHITE_LIST_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_WHITE_LIST,
-                       BLE_HCI_CHG_WHITE_LIST_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_whitelist_chg(addr, addr_type, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-void
-host_hci_cmd_build_reset(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET,
-                       0, dst);
-}
-
-/**
- * Reset the controller and link manager.
- *
- * @return int
- */
-int
-host_hci_cmd_reset(void)
-{
-    int rc;
-
-    rc = host_hci_cmd_send(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET, 0,
-                           NULL);
-    return rc;
-}
-
-void
-host_hci_cmd_build_read_adv_pwr(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR,
-                       0, dst);
-}
-
-/**
- * Read the transmit power level used for LE advertising channel packets.
- *
- * @return int
- */
-int
-host_hci_cmd_read_adv_pwr(void)
-{
-    int rc;
-
-    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR, 0,
-                           NULL);
-    return rc;
-}
-
-void
-host_hci_cmd_build_le_create_conn_cancel(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
-                       0, dst);
-}
-
-int
-host_hci_cmd_le_create_conn_cancel(void)
-{
-    int rc;
-
-    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
-                           0, NULL);
-    return rc;
-}
-
-static int
-host_hci_cmd_body_le_conn_update(const struct hci_conn_update *hcu,
-                                 uint8_t *dst)
-{
-    /* XXX: add parameter checking later */
-    htole16(dst + 0, hcu->handle);
-    htole16(dst + 2, hcu->conn_itvl_min);
-    htole16(dst + 4, hcu->conn_itvl_max);
-    htole16(dst + 6, hcu->conn_latency);
-    htole16(dst + 8, hcu->supervision_timeout);
-    htole16(dst + 10, hcu->min_ce_len);
-    htole16(dst + 12, hcu->max_ce_len);
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_le_conn_update(const struct hci_conn_update *hcu,
-                                  uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_UPDATE_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CONN_UPDATE,
-                       BLE_HCI_CONN_UPDATE_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_le_conn_update(hcu, dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-int
-host_hci_cmd_le_conn_update(const struct hci_conn_update *hcu)
-{
-    uint8_t cmd[BLE_HCI_CONN_UPDATE_LEN];
-    int rc;
-
-    rc = host_hci_cmd_body_le_conn_update(hcu, cmd);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = host_hci_le_cmd_send(BLE_HCI_OCF_LE_CONN_UPDATE,
-                              BLE_HCI_CONN_UPDATE_LEN, cmd);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static void
-host_hci_cmd_body_le_lt_key_req_reply(const struct hci_lt_key_req_reply *hkr,
-                                      uint8_t *dst)
-{
-    htole16(dst + 0, hkr->conn_handle);
-    memcpy(dst + 2, hkr->long_term_key, sizeof hkr->long_term_key);
-}
-
-/**
- * Sends the long-term key (LTK) to the controller.
- *
- * Note: This function expects the 128-bit key to be in little-endian byte
- * order.
- *
- * OGF = 0x08 (LE)
- * OCF = 0x001a
- *
- * @param key
- * @param pt
- *
- * @return int
- */
-void
-host_hci_cmd_build_le_lt_key_req_reply(const struct hci_lt_key_req_reply *hkr,
-                                       uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LT_KEY_REQ_REPLY_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY,
-                       BLE_HCI_LT_KEY_REQ_REPLY_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_lt_key_req_reply(hkr, dst);
-}
-
-void
-host_hci_cmd_build_le_lt_key_req_neg_reply(uint16_t conn_handle,
-                                           uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
-                       BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    htole16(dst + 0, conn_handle);
-}
-
-static void
-host_hci_cmd_body_le_conn_param_reply(const struct hci_conn_param_reply *hcr,
-                                      uint8_t *dst)
-{
-    htole16(dst + 0, hcr->handle);
-    htole16(dst + 2, hcr->conn_itvl_min);
-    htole16(dst + 4, hcr->conn_itvl_max);
-    htole16(dst + 6, hcr->conn_latency);
-    htole16(dst + 8, hcr->supervision_timeout);
-    htole16(dst + 10, hcr->min_ce_len);
-    htole16(dst + 12, hcr->max_ce_len);
-}
-
-void
-host_hci_cmd_build_le_conn_param_reply(const struct hci_conn_param_reply *hcr,
-                                       uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_PARAM_REPLY_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
-                       BLE_HCI_CONN_PARAM_REPLY_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_conn_param_reply(hcr, dst);
-}
-
-int
-host_hci_cmd_le_conn_param_reply(const struct hci_conn_param_reply *hcr)
-{
-    uint8_t cmd[BLE_HCI_CONN_PARAM_REPLY_LEN];
-    int rc;
-
-    htole16(cmd + 0, hcr->handle);
-    htole16(cmd + 2, hcr->conn_itvl_min);
-    htole16(cmd + 4, hcr->conn_itvl_max);
-    htole16(cmd + 6, hcr->conn_latency);
-    htole16(cmd + 8, hcr->supervision_timeout);
-    htole16(cmd + 10, hcr->min_ce_len);
-    htole16(cmd + 12, hcr->max_ce_len);
-
-    rc = host_hci_le_cmd_send(BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
-                              BLE_HCI_CONN_PARAM_REPLY_LEN, cmd);
-    return rc;
-}
-
-static void
-host_hci_cmd_body_le_conn_param_neg_reply(
-    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst)
-{
-    htole16(dst + 0, hcn->handle);
-    dst[2] = hcn->reason;
-}
-
-
-void
-host_hci_cmd_build_le_conn_param_neg_reply(
-    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_PARAM_NEG_REPLY_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
-                       BLE_HCI_CONN_PARAM_NEG_REPLY_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_conn_param_neg_reply(hcn, dst);
-}
-
-int
-host_hci_cmd_le_conn_param_neg_reply(
-    const struct hci_conn_param_neg_reply *hcn)
-{
-    uint8_t cmd[BLE_HCI_CONN_PARAM_NEG_REPLY_LEN];
-    int rc;
-
-    host_hci_cmd_body_le_conn_param_neg_reply(hcn, cmd);
-
-    rc = host_hci_le_cmd_send(BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
-                              BLE_HCI_CONN_PARAM_NEG_REPLY_LEN, cmd);
-    return rc;
-}
-
-/**
- * Get random data
- *
- * OGF = 0x08 (LE)
- * OCF = 0x0018
- *
- * @return int
- */
-void
-host_hci_cmd_build_le_rand(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RAND, 0, dst);
-}
-
-static void
-host_hci_cmd_body_le_start_encrypt(const struct hci_start_encrypt *cmd,
-                                   uint8_t *dst)
-{
-    htole16(dst + 0, cmd->connection_handle);
-    htole64(dst + 2, cmd->random_number);
-    htole16(dst + 10, cmd->encrypted_diversifier);
-    memcpy(dst + 12, cmd->long_term_key, sizeof cmd->long_term_key);
-}
-
-/*
- * OGF=0x08 OCF=0x0019
- */
-void
-host_hci_cmd_build_le_start_encrypt(const struct hci_start_encrypt *cmd,
-                                    uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_START_ENCRYPT_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_START_ENCRYPT,
-                       BLE_HCI_LE_START_ENCRYPT_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_le_start_encrypt(cmd, dst);
-}
-
-/**
- * Read the RSSI for a given connection handle
- *
- * NOTE: OGF=0x05 OCF=0x0005
- *
- * @param handle
- *
- * @return int
- */
-static void
-host_hci_cmd_body_read_rssi(uint16_t handle, uint8_t *dst)
-{
-    htole16(dst, handle);
-}
-
-void
-host_hci_cmd_build_read_rssi(uint16_t handle, uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_READ_RSSI_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_STATUS_PARAMS, BLE_HCI_OCF_RD_RSSI,
-                       BLE_HCI_READ_RSSI_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    host_hci_cmd_body_read_rssi(handle, dst);
-}
-
-static int
-host_hci_cmd_body_set_data_len(uint16_t connection_handle, uint16_t tx_octets,
-                               uint16_t tx_time, uint8_t *dst)
-{
-
-    if (tx_octets < BLE_HCI_SET_DATALEN_TX_OCTETS_MIN ||
-        tx_octets > BLE_HCI_SET_DATALEN_TX_OCTETS_MAX) {
-
-        return BLE_HS_EINVAL;
-    }
-
-    if (tx_time < BLE_HCI_SET_DATALEN_TX_TIME_MIN ||
-        tx_time > BLE_HCI_SET_DATALEN_TX_TIME_MAX) {
-
-        return BLE_HS_EINVAL;
-    }
-
-    htole16(dst + 0, connection_handle);
-    htole16(dst + 2, tx_octets);
-    htole16(dst + 4, tx_time);
-
-    return 0;
-}
-
-/*
- * OGF=0x08 OCF=0x0022
- */
-int
-host_hci_cmd_build_set_data_len(uint16_t connection_handle,
-                                uint16_t tx_octets, uint16_t tx_time,
-                                uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DATA_LEN,
-                       BLE_HCI_SET_DATALEN_LEN, dst);
-    dst += BLE_HCI_CMD_HDR_LEN;
-
-    rc = host_hci_cmd_body_set_data_len(connection_handle, tx_octets, tx_time,
-                                        dst);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-/**
- * IRKs are in little endian.
- */
-static int
-host_hci_cmd_body_add_to_resolv_list(uint8_t addr_type, const uint8_t *addr,
-                                     const uint8_t *peer_irk,
-                                     const uint8_t *local_irk,
-                                     uint8_t *dst)
-{
-    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = addr_type;
-    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
-    memcpy(dst + 1 + 6, peer_irk , 16);
-    memcpy(dst + 1 + 6 + 16, local_irk , 16);
-    /* 16 + 16 + 6 + 1 == 39 */
-    return 0;
-}
-
-/**
- * OGF=0x08 OCF=0x0027
- *
- * IRKs are in little endian.
- */
-int
-host_hci_cmd_build_add_to_resolv_list(
-    const struct hci_add_dev_to_resolving_list *padd,
-    uint8_t *dst,
-    int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_ADD_TO_RESOLV_LIST_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
-                       BLE_HCI_ADD_TO_RESOLV_LIST_LEN, dst);
-
-    rc = host_hci_cmd_body_add_to_resolv_list(
-        padd->addr_type, padd->addr, padd->peer_irk, padd->local_irk,
-        dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static int
-host_hci_cmd_body_remove_from_resolv_list(uint8_t addr_type,
-                                          const uint8_t *addr,
-                                          uint8_t *dst)
-{
-    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = addr_type;
-    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
-    return 0;
-}
-
-
-int
-host_hci_cmd_build_remove_from_resolv_list(uint8_t addr_type,
-                                           const uint8_t *addr,
-                                           uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RMV_FROM_RESOLV_LIST_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RMV_RESOLV_LIST,
-                       BLE_HCI_RMV_FROM_RESOLV_LIST_LEN, dst);
-
-    rc = host_hci_cmd_body_remove_from_resolv_list(addr_type, addr,
-                                                dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-    return 0;
-}
-
-int
-host_hci_cmd_build_clear_resolv_list(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLR_RESOLV_LIST,
-                       0, dst);
-
-    return 0;
-}
-
-int
-host_hci_cmd_build_read_resolv_list_size(uint8_t *dst, int dst_len)
-{
-    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_RESOLV_LIST_SIZE,
-                       0, dst);
-
-    return 0;
-}
-
-static int
-host_hci_cmd_body_read_peer_resolv_addr(uint8_t peer_identity_addr_type,
-                                        const uint8_t *peer_identity_addr,
-                                        uint8_t *dst)
-{
-    if (peer_identity_addr_type > BLE_ADDR_TYPE_RANDOM) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = peer_identity_addr_type;
-    memcpy(dst + 1, peer_identity_addr, BLE_DEV_ADDR_LEN);
-    return 0;
-}
-
-int
-host_hci_cmd_build_read_peer_resolv_addr(uint8_t peer_identity_addr_type,
-                                         const uint8_t *peer_identity_addr,
-                                         uint8_t *dst,
-                                         int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RD_PEER_RESOLV_ADDR_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_PEER_RESOLV_ADDR,
-                       BLE_HCI_RD_PEER_RESOLV_ADDR_LEN, dst);
-
-    rc = host_hci_cmd_body_read_peer_resolv_addr(peer_identity_addr_type,
-                                                peer_identity_addr,
-                                                dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-    return 0;
-}
-
-static int
-host_hci_cmd_body_read_lcl_resolv_addr(
-    uint8_t local_identity_addr_type,
-    const uint8_t *local_identity_addr,
-    uint8_t *dst)
-{
-    if (local_identity_addr_type > BLE_ADDR_TYPE_RANDOM) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = local_identity_addr_type;
-    memcpy(dst + 1, local_identity_addr, BLE_DEV_ADDR_LEN);
-    return 0;
-}
-
-/*
- * OGF=0x08 OCF=0x002c
- */
-int
-host_hci_cmd_build_read_lcl_resolv_addr(uint8_t local_identity_addr_type,
-                                        const uint8_t *local_identity_addr,
-                                        uint8_t *dst,
-                                        int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RD_LOC_RESOLV_ADDR_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOCAL_RESOLV_ADDR,
-                       BLE_HCI_RD_LOC_RESOLV_ADDR_LEN, dst);
-
-    rc = host_hci_cmd_body_read_lcl_resolv_addr(local_identity_addr_type,
-                                                local_identity_addr,
-                                                dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-    return 0;
-}
-
-static int
-host_hci_cmd_body_set_addr_res_en(uint8_t enable, uint8_t *dst)
-{
-    if (enable > 1) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    dst[0] = enable;
-    return 0;
-}
-
-/*
- * OGF=0x08 OCF=0x002d
- */
-int
-host_hci_cmd_build_set_addr_res_en(uint8_t enable, uint8_t *dst, int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADDR_RESOL_ENA_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN,
-                       BLE_HCI_SET_ADDR_RESOL_ENA_LEN, dst);
-
-    rc = host_hci_cmd_body_set_addr_res_en(enable, dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-    return 0;
-}
-
-static int
-host_hci_cmd_body_set_resolv_priv_addr_timeout(uint16_t timeout, uint8_t *dst)
-{
-    if (timeout == 0 || timeout > 0xA1B8) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    htole16(dst, timeout);
-    return 0;
-}
-
-/*
- * OGF=0x08 OCF=0x002e
- */
-int
-host_hci_cmd_build_set_resolv_priv_addr_timeout(uint16_t timeout, uint8_t *dst,
-                                                int dst_len)
-{
-    int rc;
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RPA_TMO,
-                       BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN, dst);
-
-    rc = host_hci_cmd_body_set_resolv_priv_addr_timeout(
-        timeout, dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-    return 0;
-}
-
-static int
-host_hci_cmd_body_set_random_addr(const struct hci_rand_addr *paddr,
-                                  uint8_t *dst)
-{
-    memcpy(dst, paddr->addr, BLE_DEV_ADDR_LEN);
-    return 0;
-}
-
-int
-host_hci_cmd_build_set_random_addr(const uint8_t *addr,
-                                   uint8_t *dst, int dst_len)
-{
-    struct hci_rand_addr r_addr;
-    int rc;
-
-    memcpy(r_addr.addr, addr, sizeof(r_addr.addr));
-
-    BLE_HS_DBG_ASSERT(
-        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN);
-
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RAND_ADDR,
-                       BLE_HCI_SET_RAND_ADDR_LEN, dst);
-
-    rc = host_hci_cmd_body_set_random_addr(&r_addr, dst + BLE_HCI_CMD_HDR_LEN);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_att_clt_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_clt_test.c b/net/nimble/host/src/test/ble_att_clt_test.c
index 9461337..ab89e9d 100644
--- a/net/nimble/host/src/test/ble_att_clt_test.c
+++ b/net/nimble/host/src/test/ble_att_clt_test.c
@@ -412,7 +412,8 @@ TEST_CASE(ble_att_clt_test_rx_read_mult)
     htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 0, 12);
 
     rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf, BLE_ATT_READ_MULT_RSP_BASE_SZ + 2);
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 2);
     TEST_ASSERT(rc == 0);
 
     /*** Larger response. */
@@ -420,12 +421,14 @@ TEST_CASE(ble_att_clt_test_rx_read_mult)
     htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 2, 43);
     htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 4, 91);
     rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf, BLE_ATT_READ_MULT_RSP_BASE_SZ + 6);
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 6);
     TEST_ASSERT(rc == 0);
 
     /*** Zero-length response. */
     rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf, BLE_ATT_READ_MULT_RSP_BASE_SZ + 0);
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 0);
     TEST_ASSERT(rc == 0);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index 2ae3354..170b8d9 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -372,11 +372,11 @@ ble_gap_test_util_rx_param_req(struct ble_gap_upd_params *params, int pos,
     evt.timeout = params->supervision_timeout;
 
     if (pos) {
-        opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                      BLE_HCI_OCF_LE_REM_CONN_PARAM_RR);
+        opcode = ble_hs_hci_util_opcode_join(
+            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_RR);
     } else {
-        opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                      BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR);
+        opcode = ble_hs_hci_util_opcode_join(
+            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR);
     }
     if (*cmd_idx == cmd_fail_idx) {
         hci_status = fail_status;
@@ -825,7 +825,8 @@ TEST_CASE(ble_gap_test_case_conn_dir_good)
 
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
 
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
 }
@@ -1059,7 +1060,8 @@ TEST_CASE(ble_gap_test_case_conn_terminate_good)
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
     TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
                 BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_arg == NULL);
 
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
@@ -1099,7 +1101,8 @@ TEST_CASE(ble_gap_test_case_conn_terminate_ctlr_fail)
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
     TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
                 BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_arg == NULL);
 
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
@@ -1494,7 +1497,8 @@ TEST_CASE(ble_gap_test_case_adv_good)
 
             if (c != BLE_GAP_CONN_MODE_NON) {
                 TEST_ASSERT(!ble_gap_adv_active());
-                TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
+                TEST_ASSERT(ble_gap_test_conn_event_type ==
+                                BLE_GAP_EVENT_CONNECT);
                 TEST_ASSERT(ble_gap_test_conn_status == 0);
                 TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
                 TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
@@ -1689,7 +1693,8 @@ ble_gap_test_util_update(struct ble_gap_upd_params *params,
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == params->itvl_max);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == params->latency);
     TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
@@ -1703,7 +1708,8 @@ fail:
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == status);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
                 BLE_GAP_INITIAL_CONN_ITVL_MAX);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
@@ -1789,7 +1795,8 @@ ble_gap_test_util_update_req_pos(struct ble_gap_upd_params *peer_params,
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
     TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
@@ -1801,7 +1808,8 @@ hci_fail:
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
                 BLE_GAP_INITIAL_CONN_ITVL_MAX);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
@@ -1850,7 +1858,8 @@ hci_fail:
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
                 BLE_GAP_INITIAL_CONN_ITVL_MAX);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
@@ -1925,7 +1934,8 @@ ble_gap_test_util_update_req_concurrent(
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
     TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
@@ -1937,7 +1947,8 @@ hci_fail:
     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
     TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(fail_status));
     TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr, peer_addr, 6) == 0);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
                 BLE_GAP_INITIAL_CONN_ITVL_MAX);
     TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
@@ -2269,8 +2280,8 @@ ble_gap_test_util_conn_timeout(int32_t duration_ms)
     os_time_advance(duration_ms);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE,
-                             BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
         0);
 
     TEST_ASSERT(ble_gap_test_conn_event_type == -1);
@@ -2350,8 +2361,8 @@ ble_gap_test_util_disc_timeout(int32_t duration_ms)
     os_time_advance(duration_ms);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE,
-                             BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
         0);
     ticks_from_now = ble_gap_heartbeat();
     TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/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 0c9f1cb..822de5c 100644
--- a/net/nimble/host/src/test/ble_gatt_read_test.c
+++ b/net/nimble/host/src/test/ble_gatt_read_test.c
@@ -495,21 +495,24 @@ ble_gatt_read_test_misc_mult_verify_bad(
 TEST_CASE(ble_gatt_read_test_by_handle)
 {
     /* Read a seven-byte attribute. */
-    ble_gatt_read_test_misc_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 43,
         .value = { 1,2,3,4,5,6,7 },
         .value_len = 7
     } });
 
     /* Read a one-byte attribute. */
-    ble_gatt_read_test_misc_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 0x5432,
         .value = { 0xff },
         .value_len = 1
     } });
 
     /* Read a 200-byte attribute. */
-    ble_gatt_read_test_misc_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 815,
         .value = { 0 },
         .value_len = 200,
@@ -669,7 +672,8 @@ TEST_CASE(ble_gatt_read_test_mult)
     }
 
     /* Read one attribute. */
-    ble_gatt_read_test_misc_mult_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 43,
         .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
         .value_len = 7
@@ -678,7 +682,8 @@ TEST_CASE(ble_gatt_read_test_mult)
     } });
 
     /* Read two attributes. */
-    ble_gatt_read_test_misc_mult_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 43,
         .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
         .value_len = 7,
@@ -691,7 +696,8 @@ TEST_CASE(ble_gatt_read_test_mult)
     } });
 
     /* Read two attributes (swap order). */
-    ble_gatt_read_test_misc_mult_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 44,
         .value = { 8, 9, 10, 11 },
         .value_len = 4,
@@ -704,7 +710,8 @@ TEST_CASE(ble_gatt_read_test_mult)
     } });
 
     /* Read five attributes. */
-    ble_gatt_read_test_misc_mult_verify_good((struct ble_hs_test_util_flat_attr[]) { {
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
         .handle = 43,
         .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
         .value_len = 7,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_host_hci_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_host_hci_test.c b/net/nimble/host/src/test/ble_host_hci_test.c
deleted file mode 100644
index 32c5cf4..0000000
--- a/net/nimble/host/src/test/ble_host_hci_test.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- * 
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/host_hci.h"
-#include "host/ble_hs_test.h"
-#include "testutil/testutil.h"
-#include "ble_hs_test_util.h"
-
-TEST_CASE(ble_host_hci_test_event_bad)
-{
-    uint8_t *buf;
-    int rc;
-
-    /*** Invalid event code. */
-    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-    TEST_ASSERT_FATAL(buf != NULL);
-
-    buf[0] = 0xff;
-    buf[1] = 0;
-    rc = host_hci_evt_process(buf);
-    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
-}
-
-TEST_CASE(ble_host_hci_test_rssi)
-{
-    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
-    uint16_t opcode;
-    int8_t rssi;
-    int rc;
-
-    opcode = host_hci_opcode_join(BLE_HCI_OGF_STATUS_PARAMS,
-                                  BLE_HCI_OCF_RD_RSSI);
-
-    /*** Success. */
-    /* Connection handle. */
-    htole16(params + 0, 1);
-
-    /* RSSI. */
-    params[2] = -8;
-
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
-
-    rc = ble_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(rssi == -8);
-
-    /*** Failure: incorrect connection handle. */
-    htole16(params + 0, 99);
-
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
-
-    rc = ble_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-
-    /*** Failure: params too short. */
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params - 1);
-    rc = ble_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-
-    /*** Failure: params too long. */
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params + 1);
-    rc = ble_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-}
-
-TEST_SUITE(ble_host_hci_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_host_hci_test_event_bad();
-    ble_host_hci_test_rssi();
-}
-
-int
-ble_host_hci_test_all(void)
-{
-    ble_host_hci_suite();
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_hs_adv_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_adv_test.c b/net/nimble/host/src/test/ble_hs_adv_test.c
index 89c61a2..e99fd5b 100644
--- a/net/nimble/host/src/test/ble_hs_adv_test.c
+++ b/net/nimble/host/src/test/ble_hs_adv_test.c
@@ -24,7 +24,6 @@
 #include "nimble/hci_common.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_hs_test.h"
-#include "host/host_hci.h"
 #include "ble_hs_test_util.h"
 
 #define BLE_ADV_TEST_DATA_OFF   4

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_conn_test.c b/net/nimble/host/src/test/ble_hs_conn_test.c
index 0e896f4..c957446 100644
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ b/net/nimble/host/src/test/ble_hs_conn_test.c
@@ -24,7 +24,6 @@
 #include "nimble/hci_common.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_hs_test.h"
-#include "host/host_hci.h"
 #include "ble_hs_test_util.h"
 
 static int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_hs_hci_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_hci_test.c b/net/nimble/host/src/test/ble_hs_hci_test.c
new file mode 100644
index 0000000..21184b8
--- /dev/null
+++ b/net/nimble/host/src/test/ble_hs_hci_test.c
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "host/ble_hs_test.h"
+#include "testutil/testutil.h"
+#include "ble_hs_test_util.h"
+
+TEST_CASE(ble_hs_hci_test_event_bad)
+{
+    uint8_t *buf;
+    int rc;
+
+    /*** Invalid event code. */
+    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
+    TEST_ASSERT_FATAL(buf != NULL);
+
+    buf[0] = 0xff;
+    buf[1] = 0;
+    rc = ble_hs_hci_evt_process(buf);
+    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
+}
+
+TEST_CASE(ble_hs_hci_test_rssi)
+{
+    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
+    uint16_t opcode;
+    int8_t rssi;
+    int rc;
+
+    opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_STATUS_PARAMS,
+                                  BLE_HCI_OCF_RD_RSSI);
+
+    /*** Success. */
+    /* Connection handle. */
+    htole16(params + 0, 1);
+
+    /* RSSI. */
+    params[2] = -8;
+
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
+
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(rssi == -8);
+
+    /*** Failure: incorrect connection handle. */
+    htole16(params + 0, 99);
+
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
+
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+
+    /*** Failure: params too short. */
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params - 1);
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+
+    /*** Failure: params too long. */
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params + 1);
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+}
+
+TEST_SUITE(ble_hs_hci_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_hs_hci_test_event_bad();
+    ble_hs_hci_test_rssi();
+}
+
+int
+ble_hs_hci_test_all(void)
+{
+    ble_hs_hci_suite();
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test.c b/net/nimble/host/src/test/ble_hs_test.c
index 0b89bc9..3bc468e 100644
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ b/net/nimble/host/src/test/ble_hs_test.c
@@ -46,7 +46,7 @@ main(int argc, char **argv)
     ble_gatts_notify_test_all();
     ble_gatts_read_test_suite();
     ble_gatts_reg_test_all();
-    ble_host_hci_test_all();
+    ble_hs_hci_test_all();
     ble_hs_adv_test_all();
     ble_hs_conn_test_all();
     ble_l2cap_test_all();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 831a077..cfb86fa 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -26,7 +26,6 @@
 #include "nimble/ble_hci_trans.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_hs_id.h"
-#include "host/host_hci.h"
 #include "transport/ram/ble_hci_ram.h"
 #include "ble_hs_test_util.h"
 
@@ -47,7 +46,7 @@ static const uint8_t ble_hs_test_util_pub_addr[BLE_DEV_ADDR_LEN] =
     OS_MEMPOOL_SIZE(BLE_HS_TEST_UTIL_NUM_MBUFS, BLE_HS_TEST_UTIL_MEMBLOCK_SIZE)
 
 #define BLE_HS_TEST_UTIL_LE_OPCODE(ocf) \
-    host_hci_opcode_join(BLE_HCI_OGF_LE, (ocf))
+    ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE, (ocf))
 
 struct os_eventq ble_hs_test_util_evq;
 
@@ -106,7 +105,7 @@ ble_hs_test_util_prev_tx_dequeue_once(struct hci_data_hdr *out_hci_hdr)
 
     om = OS_MBUF_PKTHDR_TO_MBUF(omp);
 
-    rc = ble_hci_util_data_hdr_strip(om, out_hci_hdr);
+    rc = ble_hs_hci_util_data_hdr_strip(om, out_hci_hdr);
     TEST_ASSERT_FATAL(rc == 0);
     TEST_ASSERT_FATAL(out_hci_hdr->hdh_len == OS_MBUF_PKTLEN(om));
 
@@ -262,7 +261,7 @@ ble_hs_test_util_rx_hci_evt(uint8_t *evt)
         memcpy(evbuf, evt, totlen);
         rc = ble_hci_trans_ll_evt_tx(evbuf);
     } else {
-        rc = host_hci_evt_process(evt);
+        rc = ble_hs_hci_evt_process(evt);
     }
 
     TEST_ASSERT_FATAL(rc == 0);
@@ -352,7 +351,7 @@ ble_hs_test_util_set_ack_params(uint16_t opcode, uint8_t status, void *params,
     }
     ble_hs_test_util_num_phony_acks = 1;
 
-    ble_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
+    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
 }
 
 void
@@ -371,7 +370,7 @@ ble_hs_test_util_set_ack_seq(struct ble_hs_test_util_phony_ack *acks)
     }
     ble_hs_test_util_num_phony_acks = i;
 
-    ble_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
+    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
 }
 
 void
@@ -500,7 +499,8 @@ ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
     ble_hs_test_util_prev_hci_tx_clear();
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN),
         ack_status);
 
     rc = ble_gap_connect(own_addr_type, peer_addr_type, peer_addr, duration_ms,
@@ -526,8 +526,8 @@ ble_hs_test_util_conn_cancel(uint8_t ack_status)
     int rc;
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE,
-                             BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
         ack_status);
 
     rc = ble_gap_conn_cancel();
@@ -557,8 +557,8 @@ ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
     int rc;
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LINK_CTRL,
-                             BLE_HCI_OCF_DISCONNECT_CMD),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LINK_CTRL,
+                                    BLE_HCI_OCF_DISCONNECT_CMD),
         hci_status);
 
     rc = ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
@@ -623,8 +623,8 @@ ble_hs_test_util_disc_cancel(uint8_t ack_status)
     int rc;
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE,
-                             BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
         ack_status);
 
     rc = ble_gap_disc_cancel();
@@ -654,8 +654,9 @@ ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
 
     if (auto_pwr) {
         ble_hs_test_util_set_ack_params(
-            host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                 BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR), hci_status,
+            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                        BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR),
+            hci_status,
             ((uint8_t[1]){0}), 1);
     }
 
@@ -884,8 +885,8 @@ ble_hs_test_util_l2cap_rx_payload_flat(uint16_t conn_handle, uint16_t cid,
     TEST_ASSERT_FATAL(rc == 0);
 
     hci_hdr.hdh_handle_pb_bc =
-        host_hci_handle_pb_bc_join(conn_handle,
-                                   BLE_HCI_PB_FIRST_FLUSH, 0);
+        ble_hs_hci_util_handle_pb_bc_join(conn_handle,
+                                          BLE_HCI_PB_FIRST_FLUSH, 0);
     hci_hdr.hdh_len = OS_MBUF_PKTHDR(om)->omp_len;
 
     rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, cid, &hci_hdr, om);
@@ -919,55 +920,55 @@ ble_hs_test_util_set_startup_acks(void)
      */
     ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
-                                           BLE_HCI_OCF_CB_RESET),
+            .opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
+                                                  BLE_HCI_OCF_CB_RESET),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
-                                           BLE_HCI_OCF_CB_SET_EVENT_MASK),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
-                                           BLE_HCI_OCF_CB_SET_EVENT_MASK2),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK2),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_EVENT_MASK),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_EVENT_MASK),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_RD_BUF_SIZE),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_BUF_SIZE),
             /* Use a very low buffer size (16) to test fragmentation. */
             .evt_params = { 0x10, 0x00, 0x20 },
             .evt_params_len = 3,
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT),
             .evt_params = { 0 },
             .evt_params_len = 8,
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_INFO_PARAMS,
-                                           BLE_HCI_OCF_IP_RD_BD_ADDR),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR),
             .evt_params = BLE_HS_TEST_UTIL_PUB_ADDR_VAL,
             .evt_params_len = 6,
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
         },
         {
-            .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
         },
         { 0 }
     }));
@@ -1375,10 +1376,10 @@ ble_hs_test_util_init(void)
     rc = os_msys_register(&ble_hs_test_util_mbuf_pool);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hci_set_phony_ack_cb(NULL);
+    ble_hs_hci_set_phony_ack_cb(NULL);
 
     ble_hci_trans_cfg_ll(ble_hs_test_util_hci_txed, NULL,
-                                ble_hs_test_util_pkt_txed, NULL);
+                         ble_hs_test_util_pkt_txed, NULL);
 
     hci_cfg = ble_hci_ram_cfg_dflt;
     hci_cfg.num_evt_bufs = cfg.max_hci_bufs;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_l2cap_test.c b/net/nimble/host/src/test/ble_l2cap_test.c
index 118d04f..69db2f8 100644
--- a/net/nimble/host/src/test/ble_l2cap_test.c
+++ b/net/nimble/host/src/test/ble_l2cap_test.c
@@ -21,7 +21,6 @@
 #include <errno.h>
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
-#include "host/host_hci.h"
 #include "host/ble_hs_test.h"
 #include "ble_hs_test_util.h"
 
@@ -74,7 +73,8 @@ ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
     ble_l2cap_sig_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CONN_UPDATE), 0);
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CONN_UPDATE), 0);
     rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
                                               &hci_hdr, om);
     TEST_ASSERT_FATAL(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_os_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_os_test.c b/net/nimble/host/src/test/ble_os_test.c
index e12e489..a9c28ea 100644
--- a/net/nimble/host/src/test/ble_os_test.c
+++ b/net/nimble/host/src/test/ble_os_test.c
@@ -225,7 +225,8 @@ ble_os_disc_test_task_handler(void *arg)
     TEST_ASSERT(!cb_called);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
         0);
 
     /* Wait 250 more ms; verify scan completed. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_sm_lgcy_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_lgcy_test.c b/net/nimble/host/src/test/ble_sm_lgcy_test.c
index 9a61087..6e451ca 100644
--- a/net/nimble/host/src/test/ble_sm_lgcy_test.c
+++ b/net/nimble/host/src/test/ble_sm_lgcy_test.c
@@ -23,7 +23,6 @@
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
 #include "nimble/nimble_opt.h"
-#include "host/host_hci.h"
 #include "host/ble_sm.h"
 #include "host/ble_hs_test.h"
 #include "ble_hs_test_util.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_sm_sc_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_sc_test.c b/net/nimble/host/src/test/ble_sm_sc_test.c
index f69c60e..518720c 100644
--- a/net/nimble/host/src/test/ble_sm_sc_test.c
+++ b/net/nimble/host/src/test/ble_sm_sc_test.c
@@ -23,7 +23,6 @@
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
 #include "nimble/nimble_opt.h"
-#include "host/host_hci.h"
 #include "host/ble_sm.h"
 #include "host/ble_hs_test.h"
 #include "ble_hs_test_util.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test.c b/net/nimble/host/src/test/ble_sm_test.c
index 26efea9..f139ddb 100644
--- a/net/nimble/host/src/test/ble_sm_test.c
+++ b/net/nimble/host/src/test/ble_sm_test.c
@@ -23,7 +23,6 @@
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
 #include "nimble/nimble_opt.h"
-#include "host/host_hci.h"
 #include "host/ble_sm.h"
 #include "host/ble_hs_test.h"
 #include "ble_hs_test_util.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/test/ble_sm_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test_util.c b/net/nimble/host/src/test/ble_sm_test_util.c
index bdecde6..5cb1d40 100644
--- a/net/nimble/host/src/test/ble_sm_test_util.c
+++ b/net/nimble/host/src/test/ble_sm_test_util.c
@@ -23,7 +23,6 @@
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
 #include "nimble/nimble_opt.h"
-#include "host/host_hci.h"
 #include "host/ble_sm.h"
 #include "host/ble_hs_test.h"
 #include "host/ble_hs_id.h"
@@ -916,8 +915,8 @@ ble_sm_test_util_set_lt_key_req_neg_reply_ack(uint8_t status,
 
     htole16(params, conn_handle);
     ble_hs_test_util_set_ack_params(
-        host_hci_opcode_join(BLE_HCI_OGF_LE,
-                             BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY),
         status, params, sizeof params);
 }
 
@@ -928,7 +927,8 @@ ble_sm_test_util_set_lt_key_req_reply_ack(uint8_t status, uint16_t conn_handle)
 
     htole16(params, conn_handle);
     ble_hs_test_util_set_ack_params(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY),
         status, params, sizeof params);
 }
 
@@ -1371,7 +1371,8 @@ ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t our_addr_type,
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_START_ENCRYPT),
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT),
         0);
 
     if (send_enc_req) {
@@ -1656,8 +1657,8 @@ ble_sm_test_util_rx_keys(struct ble_sm_test_params *params,
     }
     if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
         ble_hs_test_util_set_ack(
-            host_hci_opcode_join(BLE_HCI_OGF_LE,
-                                 BLE_HCI_OCF_LE_ADD_RESOLV_LIST), 0);
+            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                        BLE_HCI_OCF_LE_ADD_RESOLV_LIST), 0);
         ble_sm_test_util_rx_id_info(2, peer_id_info, 0);
         ble_sm_test_util_rx_id_addr_info(2, peer_id_addr_info, 0);
     }
@@ -1724,7 +1725,8 @@ ble_sm_test_util_us_lgcy_good_once(struct ble_sm_test_params *params)
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_START_ENCRYPT), 0);
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
     if (params->sec_req.authreq != 0) {
         ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
     } else {
@@ -1984,7 +1986,8 @@ ble_sm_test_util_us_sc_good_once(struct ble_sm_test_params *params)
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     ble_hs_test_util_set_ack(
-        host_hci_opcode_join(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_START_ENCRYPT), 0);
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
     if (params->sec_req.authreq != 0) {
         ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
     } else {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/store/ram/src/ble_store_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/store/ram/src/ble_store_ram.c b/net/nimble/host/store/ram/src/ble_store_ram.c
index d1e41b4..7528f03 100644
--- a/net/nimble/host/store/ram/src/ble_store_ram.c
+++ b/net/nimble/host/store/ram/src/ble_store_ram.c
@@ -136,7 +136,8 @@ ble_store_ram_read_our_sec(struct ble_store_key_sec *key_sec,
 {
     int idx;
 
-    idx = ble_store_ram_find_sec(key_sec, ble_store_ram_our_secs, ble_store_ram_num_our_secs);
+    idx = ble_store_ram_find_sec(key_sec, ble_store_ram_our_secs,
+                                 ble_store_ram_num_our_secs);
     if (idx == -1) {
         return BLE_HS_ENOENT;
     }


[29/50] [abbrv] incubator-mynewt-core git commit: BLE Host - reuse exec-write-req mbuf for rsp.

Posted by we...@apache.org.
BLE Host - reuse exec-write-req mbuf for rsp.

The response has the same contents as the request; only the op code is
different.  By reusing the mbuf, it makes failure due to mbuf exhaustion
less likely.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: ebbd1bd66b16edc2152b68218a7b08029905075d
Parents: a248989
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 12:41:53 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c | 41 +++++++++++-----------------------
 1 file changed, 13 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ebbd1bd6/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 f282093..860794d 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -588,6 +588,8 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
             rc = BLE_HS_ENOTCONN;
         } else {
             if (rc == 0) {
+                BLE_HS_DBG_ASSERT(om != NULL);
+
                 ble_att_inc_tx_stat(om->om_data[0]);
                 ble_att_truncate_to_mtu(chan, om);
                 rc = ble_l2cap_tx(conn, chan, om);
@@ -2372,7 +2374,6 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
     struct ble_hs_conn *conn;
     struct os_mbuf *txom;
     uint16_t err_handle;
-    uint8_t *buf;
     uint8_t att_err;
     int rc;
 
@@ -2394,16 +2395,13 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
                     ble_att_prep_write_cmd_log, &req);
     err_handle = req.bapc_handle;
 
-    /* Strip the request base from the front of the mbuf. */
-    os_mbuf_adj(*rxom, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-
     attr_entry = ble_att_svr_find_by_handle(req.bapc_handle);
 
     /* A prepare write request gets rejected for the following reasons:
      * 1. Insufficient authorization.
      * 2. Insufficient authentication.
      * 3. Insufficient encryption key size (XXX: Not checked).
-     * 4. Insufficient encryption.
+     * 4. Insufficient encryption (XXX: Not checked).
      * 5. Invalid handle.
      * 6. Write not permitted.
      */
@@ -2447,8 +2445,10 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
         }
 
         /* Append attribute value from request onto prep mbuf. */
-        rc = os_mbuf_appendfrom(prep_entry->bape_value, *rxom, 0,
-                                OS_MBUF_PKTLEN(*rxom));
+        rc = os_mbuf_appendfrom(prep_entry->bape_value, *rxom,
+                                BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+                                OS_MBUF_PKTLEN(*rxom) -
+                                    BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
         if (rc != 0) {
             att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
         }
@@ -2460,27 +2460,12 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
         goto done;
     }
 
-    txom = ble_hs_mbuf_l2cap_pkt();
-    if (txom == NULL) {
-        att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
-        rc = BLE_HS_ENOMEM;
-        goto done;
-    }
-
-    buf = os_mbuf_extend(txom, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-    if (buf == NULL) {
-        att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
-        rc = BLE_HS_ENOMEM;
-        goto done;
-    }
-    ble_att_prep_write_rsp_write(buf, BLE_ATT_PREP_WRITE_CMD_BASE_SZ, &req);
-
-    rc = os_mbuf_appendfrom(txom, *rxom, 0, OS_MBUF_PKTLEN(*rxom));
-    if (rc != 0) {
-        att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
-        rc = BLE_HS_ENOMEM;
-        goto done;
-    }
+    /* Reuse rxom for response.  Response is identical to request except for op
+     * code.
+     */
+    txom = *rxom;
+    *rxom = NULL;
+    txom->om_data[0] = BLE_ATT_OP_PREP_WRITE_RSP;
 
     BLE_ATT_LOG_CMD(1, "prep write rsp", conn_handle,
                     ble_att_prep_write_cmd_log, &req);


[11/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Free mbufs on exec-write-failure

Posted by we...@apache.org.
BLE Host - Free mbufs on exec-write-failure

The previous fix didn't fix an mbuf leak.  It added one!  The real bug
was that an exec-write-response (success) gets sent when a write fails.
This commit fixes the mbuf leak that was introduced.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: f0cff8c068950c36a651af6f17ed53f8f9737e81
Parents: c46ed0c
Author: Christopher Collins <cc...@apache.org>
Authored: Sun Aug 7 02:34:00 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f0cff8c0/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 b3dd2f3..1b420b5 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -2342,16 +2342,19 @@ ble_att_svr_prep_write(uint16_t conn_handle,
         ble_att_svr_prep_extract(prep_list, &attr_handle, &om);
 
         attr = ble_att_svr_find_by_handle(attr_handle);
+        *err_handle = attr_handle;
         if (attr == NULL) {
             rc = BLE_ATT_ERR_INVALID_HANDLE;
-            *err_handle = attr_handle;
         } else {
             rc = ble_att_svr_write(conn_handle, attr, 0, &om, &att_err);
-            os_mbuf_free_chain(om);
             if (rc != 0) {
-                return att_err;
+                rc = att_err;
             }
         }
+        os_mbuf_free_chain(om);
+        if (rc != 0) {
+            return rc;
+        }
     }
 
     return 0;


[04/50] [abbrv] incubator-mynewt-core git commit: eventq - os_eventq_poll() - Allow a timeout of 0.

Posted by we...@apache.org.
eventq - os_eventq_poll() - Allow a timeout of 0.

If the timeout is 0, don't involve the scheduler at all.  Grab an event
if one is available, else return immediately.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 7defc1fe30188cfbdef3e84b42a498b76cf82418
Parents: 0f11f27
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 12:41:46 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 libs/os/src/os_eventq.c        |  33 +++++++-
 libs/os/src/test/eventq_test.c | 149 +++++++++++++++++++++++-------------
 2 files changed, 125 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7defc1fe/libs/os/src/os_eventq.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_eventq.c b/libs/os/src/os_eventq.c
index b3ff26f..f9cc283 100644
--- a/libs/os/src/os_eventq.c
+++ b/libs/os/src/os_eventq.c
@@ -117,6 +117,29 @@ pull_one:
     return (ev);
 }
 
+static struct os_event *
+os_eventq_poll_0timo(struct os_eventq **evq, int nevqs)
+{
+    struct os_event *ev;
+    os_sr_t sr;
+    int i;
+
+    ev = NULL;
+
+    OS_ENTER_CRITICAL(sr);
+    for (i = 0; i < nevqs; i++) {
+        ev = STAILQ_FIRST(&evq[i]->evq_list);
+        if (ev) {
+            STAILQ_REMOVE(&evq[i]->evq_list, ev, os_event, ev_next);
+            ev->ev_queued = 0;
+            break;
+        }
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return ev;
+}
+
 /**
  * Poll the list of event queues specified by the evq parameter 
  * (size nevqs), and return the "first" event available on any of 
@@ -137,6 +160,13 @@ os_eventq_poll(struct os_eventq **evq, int nevqs, os_time_t timo)
     int i, j;
     os_sr_t sr;
 
+    /* If the timeout is 0, don't involve the scheduler at all.  Grab an event
+     * if one is available, else return immediately.
+     */
+    if (timo == 0) {
+        return os_eventq_poll_0timo(evq, nevqs);
+    }
+
     ev = NULL;
 
     OS_ENTER_CRITICAL(sr);
@@ -147,8 +177,7 @@ os_eventq_poll(struct os_eventq **evq, int nevqs, os_time_t timo)
         if (ev) {
             STAILQ_REMOVE(&evq[i]->evq_list, ev, os_event, ev_next);
             ev->ev_queued = 0;
-            /* Reset the items that already have an evq task set
-             */
+            /* Reset the items that already have an evq task set. */
             for (j = 0; j < i; j++) {
                 evq[j]->evq_task = NULL;
             }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7defc1fe/libs/os/src/test/eventq_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/eventq_test.c b/libs/os/src/test/eventq_test.c
index f17e5e4..cb1ed94 100644
--- a/libs/os/src/test/eventq_test.c
+++ b/libs/os/src/test/eventq_test.c
@@ -17,7 +17,7 @@
  * under the License.
  */
  
-
+#include <string.h>
 #include "testutil/testutil.h"
 #include "os/os.h"
 #include "os_test_priv.h"
@@ -59,10 +59,6 @@ os_stack_t eventq_task_stack_poll_s[POLL_STACK_SIZE];
 struct os_task eventq_task_poll_r;
 os_stack_t eventq_task_stack_poll_r[POLL_STACK_SIZE ];
 
-struct os_eventq *poll_eventq[POLL_STACK_SIZE];
-struct os_eventq p_event[POLL_STACK_SIZE];
-struct os_event poll_event[POLL_STACK_SIZE];
-
 /* Setting the data for the poll timeout */
 /* Define the task stack for the eventq_task_poll_timeout_send */
 #define SEND_TASK_POLL_TIMEOUT_PRIO        (5)
@@ -74,10 +70,6 @@ os_stack_t eventq_task_stack_poll_timeout_s[POLL_STACK_SIZE];
 struct os_task eventq_task_poll_timeout_r;
 os_stack_t eventq_task_stack_poll_timeout_r[POLL_STACK_SIZE];
 
-struct os_eventq *poll_eventq_timeout[POLL_STACK_SIZE];
-struct os_eventq p_event_timeout[POLL_STACK_SIZE];
-struct os_event poll_event_timeout[POLL_STACK_SIZE];
-
 /* Setting the data for the poll single */
 /* Define the task stack for the eventq_task_poll_single_send */
 #define SEND_TASK_POLL_SINGLE_PRIO        (7)
@@ -89,10 +81,6 @@ os_stack_t eventq_task_stack_poll_single_s[POLL_STACK_SIZE];
 struct os_task eventq_task_poll_single_r;
 os_stack_t eventq_task_stack_poll_single_r[POLL_STACK_SIZE];
 
-struct os_eventq *poll_eventq_single[POLL_STACK_SIZE];
-struct os_eventq p_event_single[POLL_STACK_SIZE];
-struct os_event poll_event_single[POLL_STACK_SIZE];
-
 /* This is the task function  to send data */
 void
 eventq_task_send(void *arg)
@@ -143,24 +131,19 @@ eventq_task_receive(void *arg)
 void
 eventq_task_poll_send(void *arg)
 {
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
     int i;
 
-    /* Assigning the *poll_eventq[] */
     for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        poll_eventq[i] = &p_event[i];
+        eventqs[i] = &multi_eventq[i];
     }
 
-    /* Initializing the *poll_eventq */
     for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(poll_eventq[i]);
-    }
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        poll_event[i].ev_type = i + 10;
-        poll_event[i].ev_arg = NULL;
+        m_event[i].ev_type = i + 10;
+        m_event[i].ev_arg = NULL;
 
         /* Put and send */
-        os_eventq_put(poll_eventq[i], &poll_event[i]);
+        os_eventq_put(eventqs[i], &m_event[i]);
         os_time_delay(OS_TICKS_PER_SEC / 2);
     }
 
@@ -171,12 +154,17 @@ eventq_task_poll_send(void *arg)
 void
 eventq_task_poll_receive(void *arg)
 {
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
     struct os_event *event;
     int i;
 
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
     /* Recieving using the os_eventq_poll*/
     for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        event = os_eventq_poll(poll_eventq, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
+        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
         TEST_ASSERT(event->ev_type == i +10);
     }
 
@@ -189,26 +177,18 @@ eventq_task_poll_receive(void *arg)
 void
 eventq_task_poll_timeout_send(void *arg)
 {
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
     int i;
 
-    /* Assigning the *poll_eventq_timeout[] */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        poll_eventq_timeout[i] = &p_event_timeout[i];
-    }
-
-    /* Initializing the *poll_eventq_timeout */
     for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(poll_eventq_timeout[i]);
+        eventqs[i] = &multi_eventq[i];
     }
 
     for (i = 0; i < SIZE_MULTI_EVENT; i++){
          os_time_delay(1000);
 
-        poll_event_timeout[i].ev_type = i + 10;
-        poll_event_timeout[i].ev_arg = NULL;
-
         /* Put and send */
-        os_eventq_put(poll_eventq_timeout[i], &poll_event_timeout[i]);
+        os_eventq_put(eventqs[i], &m_event[i]);
         os_time_delay(OS_TICKS_PER_SEC / 2);
     }
 
@@ -221,12 +201,17 @@ eventq_task_poll_timeout_send(void *arg)
 void
 eventq_task_poll_timeout_receive(void *arg)
 {
-     struct os_event *event;
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *event;
     int i;
 
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
     /* Recieving using the os_eventq_poll_timeout*/
     for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        event = os_eventq_poll(poll_eventq_timeout, SIZE_MULTI_EVENT, 200);
+        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 200);
         TEST_ASSERT(event == NULL);
     }
 
@@ -239,45 +224,40 @@ eventq_task_poll_timeout_receive(void *arg)
 void
 eventq_task_poll_single_send(void *arg)
 {
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
     int i;
     int position = 2;
 
-    /* Assigning the *poll_eventq_single */
     for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        poll_eventq_single[i] = &p_event_single[i];
+        eventqs[i] = &multi_eventq[i];
     }
 
-    /* Initializing the *poll_eventq_single */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(poll_eventq_single[i]);
-    }
-
-    poll_event_single[position].ev_type = 20;
-    poll_event_single[position].ev_arg = NULL;
-
-        /* Put and send */
-        os_eventq_put(poll_eventq_single[position],
-            &poll_event_single[position]);
-        os_time_delay(OS_TICKS_PER_SEC / 2);
+    /* Put and send */
+    os_eventq_put(eventqs[position], &m_event[position]);
+    os_time_delay(OS_TICKS_PER_SEC / 2);
 
     /* This task sleeps until the receive task completes the test. */
     os_time_delay(1000000);
-
 }
 
 /* Recieving the single event */
 void
 eventq_task_poll_single_receive(void *arg)
 {
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
     struct os_event *event;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
 
     /* Recieving using the os_eventq_poll*/
-    event = os_eventq_poll(poll_eventq_single, SIZE_MULTI_EVENT,  OS_WAIT_FOREVER);
+    event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
     TEST_ASSERT(event->ev_type == 20);
 
     /* Finishes the test when OS has been started */
     os_test_restart();
-
 }
 
 TEST_CASE(event_test_sr)
@@ -309,6 +289,8 @@ TEST_CASE(event_test_sr)
 /* To test for the basic function of os_eventq_poll() */
 TEST_CASE(event_test_poll_sr)
 {
+    int i;
+
     /* Initializing the OS */
     os_init();
     /* Initialize the task */
@@ -321,6 +303,11 @@ TEST_CASE(event_test_poll_sr)
         NULL, RECEIVE_TASK_POLL_PRIO, OS_WAIT_FOREVER, eventq_task_stack_poll_r,
         POLL_STACK_SIZE);
 
+    /* Initializing the eventqs. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+    }
+
     /* Does not return until OS_restart is called */
     os_start();
 
@@ -329,6 +316,8 @@ TEST_CASE(event_test_poll_sr)
 /* Test case for poll timeout */
 TEST_CASE(event_test_poll_timeout_sr)
 {
+    int i;
+
     /* Initializing the OS */
     os_init();
     /* Initialize the task */
@@ -341,6 +330,14 @@ TEST_CASE(event_test_poll_timeout_sr)
         eventq_task_poll_timeout_receive, NULL, RECEIVE_TASK_POLL_TIMEOUT_PRIO,
         OS_WAIT_FOREVER, eventq_task_stack_poll_timeout_r, POLL_STACK_SIZE);
 
+    /* Initializing the eventqs. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+
+        m_event[i].ev_type = i + 10;
+        m_event[i].ev_arg = NULL;
+    }
+
     /* Does not return until OS_restart is called */
     os_start();
 
@@ -350,6 +347,8 @@ TEST_CASE(event_test_poll_timeout_sr)
 /* Test case for poll timeout */
 TEST_CASE(event_test_poll_single_sr)
 {
+    int i;
+
     /* Initializing the OS */
     os_init();
     /* Initialize the task */
@@ -362,16 +361,56 @@ TEST_CASE(event_test_poll_single_sr)
         eventq_task_poll_single_receive, NULL, RECEIVE_TASK_POLL_SINGLE_PRIO,
         OS_WAIT_FOREVER, eventq_task_stack_poll_single_r, POLL_STACK_SIZE);
 
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+
+        m_event[i].ev_type = 10 * i;
+        m_event[i].ev_arg = NULL;
+    }
+
     /* Does not return until OS_restart is called */
     os_start();
 
 }
 
+/**
+ * Tests eventq_poll() with a timeout of 0.  This should not involve the
+ * scheduler at all, so it should work without starting the OS.
+ */
+TEST_CASE(event_test_poll_0timo)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *evp;
+    struct os_event ev;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
+    TEST_ASSERT(evp == NULL);
+
+    /* Ensure no eventq thinks a task is waiting on it. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
+        TEST_ASSERT(eventqs[i]->evq_task == NULL);
+    }
+
+    /* Put an event on one of the queues. */
+    memset(&ev, 0, sizeof ev);
+    ev.ev_type = 1;
+    os_eventq_put(eventqs[3], &ev);
+
+    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
+    TEST_ASSERT(evp == &ev);
+}
+
 TEST_SUITE(os_eventq_test_suite)
 {
     event_test_sr();
     event_test_poll_sr();
     event_test_poll_timeout_sr();
     event_test_poll_single_sr();
-
+    event_test_poll_0timo();
 }


[16/50] [abbrv] incubator-mynewt-core git commit: BLE Host - gap test needs to hb when checking tmo.

Posted by we...@apache.org.
BLE Host - gap test needs to hb when checking tmo.

When ensuring that the passage of time causes no effects, the test node
needs to call ble_hs_heartbeat() to let the host know that time has
passed.  This is only necessary because the OS is not running during
this test.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 1280abccbf57ca0cc7f9b732ca864302c1e78ff5
Parents: ac93fc4
Author: Christopher Collins <cc...@apache.org>
Authored: Sun Aug 7 17:58:16 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_gap_test.c | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1280abcc/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index 2d4c551..1943d01 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -2324,6 +2324,7 @@ ble_gap_test_util_conn_forever(void)
 
     /* Advance 100 seconds; ensure no timeout reported. */
     os_time_advance(100 * OS_TICKS_PER_SEC);
+    ble_gap_heartbeat();
     TEST_ASSERT(ble_gap_test_conn_event_type == -1);
     TEST_ASSERT(ble_gap_conn_active());
 }


[28/50] [abbrv] incubator-mynewt-core git commit: os_mbuf_appendfrom - Detect source mbuf overrun.

Posted by we...@apache.org.
os_mbuf_appendfrom - Detect source mbuf overrun.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: a248989e2a86b05a8460719ae9047baf239db2aa
Parents: e5413e6
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 12:29:28 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 libs/os/src/os_mbuf.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a248989e/libs/os/src/os_mbuf.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mbuf.c b/libs/os/src/os_mbuf.c
index eda58ef..a2302df 100644
--- a/libs/os/src/os_mbuf.c
+++ b/libs/os/src/os_mbuf.c
@@ -520,6 +520,10 @@ err:
  * @param src_off               The absolute offset within the source mbuf
  *                                  chain to read from.
  * @param len                   The number of bytes to append.
+ *
+ * @return                      0 on success;
+ *                              OS_EINVAL if the specified range extends beyond
+ *                                  the end of the source mbuf chain.
  */
 int
 os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src,
@@ -531,11 +535,11 @@ os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src,
     int rc;
 
     src_cur_om = os_mbuf_off(src, src_off, &src_cur_off);
-    if (src_cur_om == NULL) {
-        return OS_EINVAL;
-    }
-
     while (len > 0) {
+        if (src_cur_om == NULL) {
+            return OS_EINVAL;
+        }
+
         chunk_sz = min(len, src_cur_om->om_len - src_cur_off);
         rc = os_mbuf_append(dst, src_cur_om->om_data + src_cur_off, chunk_sz);
         if (rc != 0) {


[22/50] [abbrv] incubator-mynewt-core git commit: BLE Host - distinguish already/busy in conn/disc.

Posted by we...@apache.org.
BLE Host - distinguish already/busy in conn/disc.

Prior to this change, both ble_gap_connect() and ble_gap_disc() return
BLE_HS_EALREADY if any master procedure was already in progress (either
connect or discover).

Now, Each function returns:

    * BLE_HS_EALREDY if the same procedure is already in progress
    * BLE_HS_EBUSY if the other procedure is already in progress


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

Branch: refs/heads/phyrx_no_mbuf
Commit: b3610c44b9e62e71cc63600980cb9ef94da602ce
Parents: 582201d
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Aug 6 14:08:59 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c           |  13 +++-
 net/nimble/host/src/test/ble_gap_test.c | 111 +++++++++++++++++++++++----
 2 files changed, 105 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b3610c44/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 6086c50..49d678e 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1994,7 +1994,11 @@ ble_gap_disc_validate(uint8_t own_addr_type,
         return BLE_HS_EINVAL;
     }
 
-    if (ble_gap_master.op != BLE_GAP_OP_NULL) {
+    if (ble_gap_conn_active()) {
+        return BLE_HS_EBUSY;
+    }
+
+    if (ble_gap_disc_active()) {
         return BLE_HS_EALREADY;
     }
 
@@ -2230,11 +2234,16 @@ ble_gap_connect(uint8_t own_addr_type,
 
     ble_hs_lock();
 
-    if (ble_gap_master.op != BLE_GAP_OP_NULL) {
+    if (ble_gap_conn_active()) {
         rc = BLE_HS_EALREADY;
         goto done;
     }
 
+    if (ble_gap_disc_active()) {
+        rc = BLE_HS_EBUSY;
+        goto done;
+    }
+
     if (!ble_hs_conn_can_alloc()) {
         rc = BLE_HS_ENOMEM;
         goto done;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b3610c44/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index 170b8d9..2d4c551 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -572,15 +572,6 @@ TEST_CASE(ble_gap_test_case_disc_bad_args)
     rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
                       ble_gap_test_util_disc_cb, NULL);
     TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Master operation already in progress. */
-    params.filter_policy = BLE_HCI_SCAN_FILT_NO_WL;
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_GAP_ADDR_TYPE_WL, NULL, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
-                      ble_gap_test_util_disc_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
 }
 
 TEST_CASE(ble_gap_test_case_disc_good)
@@ -763,6 +754,45 @@ TEST_CASE(ble_gap_test_case_disc_dflts)
     ble_gap_test_util_disc_dflts_once(1);
 }
 
+TEST_CASE(ble_gap_test_case_disc_already)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a discovery procedure. */
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
+                               &disc_params, ble_gap_test_util_disc_cb,
+                               NULL, -1, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EALREADY if we try to discover. */
+    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
+                               ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_disc_busy)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a connect procedure. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EBUSY if we try to discover. */
+    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
+                               ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EBUSY);
+}
+
 TEST_SUITE(ble_gap_test_suite_disc)
 {
     tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
@@ -772,13 +802,15 @@ TEST_SUITE(ble_gap_test_suite_disc)
     ble_gap_test_case_disc_ltd_mismatch();
     ble_gap_test_case_disc_hci_fail();
     ble_gap_test_case_disc_dflts();
+    ble_gap_test_case_disc_already();
+    ble_gap_test_case_disc_busy();
 }
 
 /*****************************************************************************
  * $direct connect                                                           *
  *****************************************************************************/
 
-TEST_CASE(ble_gap_test_case_conn_dir_good)
+TEST_CASE(ble_gap_test_case_conn_gen_good)
 {
     struct hci_le_conn_complete evt;
     struct ble_gap_conn_params params;
@@ -831,7 +863,7 @@ TEST_CASE(ble_gap_test_case_conn_dir_good)
     TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
 }
 
-TEST_CASE(ble_gap_test_case_conn_dir_bad_args)
+TEST_CASE(ble_gap_test_case_conn_gen_bad_args)
 {
     int rc;
 
@@ -861,7 +893,7 @@ TEST_CASE(ble_gap_test_case_conn_dir_bad_args)
     TEST_ASSERT(rc == BLE_HS_EALREADY);
 }
 
-TEST_CASE(ble_gap_test_case_conn_dir_dflt_params)
+TEST_CASE(ble_gap_test_case_conn_gen_dflt_params)
 {
     static const uint8_t peer_addr[6] = { 2, 3, 8, 6, 6, 1 };
     int rc;
@@ -874,13 +906,58 @@ TEST_CASE(ble_gap_test_case_conn_dir_dflt_params)
     TEST_ASSERT(rc == 0);
 }
 
-TEST_SUITE(ble_gap_test_suite_conn_dir)
+TEST_CASE(ble_gap_test_case_conn_gen_already)
+{
+    static const struct ble_gap_conn_params conn_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a connect procedure. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EALREADY if we try to connect. */
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                         peer_addr, BLE_HS_FOREVER, &conn_params,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_conn_gen_busy)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    static const struct ble_gap_conn_params conn_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a discovery procedure. */
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
+                               &disc_params, ble_gap_test_util_disc_cb,
+                               NULL, -1, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EBUSY if we try to connect. */
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                         peer_addr, BLE_HS_FOREVER, &conn_params,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EBUSY);
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_gen)
 {
     tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
 
-    ble_gap_test_case_conn_dir_good();
-    ble_gap_test_case_conn_dir_bad_args();
-    ble_gap_test_case_conn_dir_dflt_params();
+    ble_gap_test_case_conn_gen_good();
+    ble_gap_test_case_conn_gen_bad_args();
+    ble_gap_test_case_conn_gen_dflt_params();
+    ble_gap_test_case_conn_gen_already();
+    ble_gap_test_case_conn_gen_busy();
 }
 
 /*****************************************************************************
@@ -2487,7 +2564,7 @@ ble_gap_test_all(void)
 {
     ble_gap_test_suite_wl();
     ble_gap_test_suite_disc();
-    ble_gap_test_suite_conn_dir();
+    ble_gap_test_suite_conn_gen();
     ble_gap_test_suite_conn_cancel();
     ble_gap_test_suite_conn_terminate();
     ble_gap_test_suite_conn_find();


[27/50] [abbrv] incubator-mynewt-core git commit: fixup adv

Posted by we...@apache.org.
fixup adv


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 11a90c6a1c7ecd3ffdb4a374c1cad026ee0f463b
Parents: bec12a6
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:01:02 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_hs_adv.h | 3 +++
 net/nimble/host/src/ble_hs_adv.c          | 3 ---
 2 files changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/11a90c6a/net/nimble/host/include/host/ble_hs_adv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs_adv.h b/net/nimble/host/include/host/ble_hs_adv.h
index cbbb468..8ae2b00 100644
--- a/net/nimble/host/include/host/ble_hs_adv.h
+++ b/net/nimble/host/include/host/ble_hs_adv.h
@@ -22,6 +22,9 @@
 
 #include <inttypes.h>
 
+/** Max field payload size (account for 2-byte header). */
+#define BLE_HS_ADV_MAX_FIELD_SZ     (BLE_HCI_MAX_ADV_DATA_LEN - 2)
+
 struct ble_hs_adv_fields {
     /*** 0x01 - Flags. */
     uint8_t flags;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/11a90c6a/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
index 305c672..fd7bfaf 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -23,9 +23,6 @@
 #include "host/ble_hs_adv.h"
 #include "ble_hs_priv.h"
 
-/** Max field paylaod size (account for 2-byte header). */
-#define BLE_HS_ADV_MAX_FIELD_SZ     (BLE_HCI_MAX_ADV_DATA_LEN - 2)
-
 static uint16_t ble_hs_adv_uuids16[BLE_HS_ADV_MAX_FIELD_SZ / 2];
 static uint32_t ble_hs_adv_uuids32[BLE_HS_ADV_MAX_FIELD_SZ / 4];
 


[32/50] [abbrv] incubator-mynewt-core git commit: bletiny - Correctly limit var-len adv field sizes.

Posted by we...@apache.org.
bletiny - Correctly limit var-len adv field sizes.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 768e63563530271244e9c8d346a9fbb9f9bde572
Parents: 11a90c6
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:03:48 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/cmd.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/768e6356/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index e50d6b0..5118aeb 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1212,11 +1212,11 @@ cmd_sec(int argc, char **argv)
 #define CMD_ADV_DATA_MAX_UUIDS32                8
 #define CMD_ADV_DATA_MAX_UUIDS128               2
 #define CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS       8
-#define CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN    32
-#define CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN    32
-#define CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN   32
-#define CMD_ADV_DATA_URI_MAX_LEN                32
-#define CMD_ADV_DATA_MFG_DATA_MAX_LEN           32
+#define CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN    BLE_HS_ADV_MAX_FIELD_SZ
+#define CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN    BLE_HS_ADV_MAX_FIELD_SZ
+#define CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN   BLE_HS_ADV_MAX_FIELD_SZ
+#define CMD_ADV_DATA_URI_MAX_LEN                BLE_HS_ADV_MAX_FIELD_SZ
+#define CMD_ADV_DATA_MFG_DATA_MAX_LEN           BLE_HS_ADV_MAX_FIELD_SZ
 
 static int
 cmd_set_adv_data(void)


[34/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Fix crash when #-evt-bufs > #-os-events

Posted by we...@apache.org.
BLE Host - Fix crash when #-evt-bufs > #-os-events

When the host could not allocate an OS event for the incoming HCI event,
it tried to free the wrong thing, resulting in a null pointer
dereference.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 202e5c3e9b18997c32fe9229217324e8440a43a6
Parents: 2c3618d
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:51:37 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/202e5c3e/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 231dce3..8d4b047 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -406,7 +406,7 @@ ble_hs_enqueue_hci_event(uint8_t *hci_evt)
 
     ev = os_memblock_get(&ble_hs_hci_ev_pool);
     if (ev == NULL) {
-        ble_hci_trans_buf_free(ev->ev_arg);
+        ble_hci_trans_buf_free(hci_evt);
     } else {
         ev->ev_queued = 0;
         ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;


[35/50] [abbrv] incubator-mynewt-core git commit: BLE RAM trans: evt buf sz 260-->45; ct 3-->14

Posted by we...@apache.org.
BLE RAM trans: evt buf sz 260-->45; ct 3-->14

For the combined host-controller (RAM HCI transport), the default HCI
event buffer size was much larger than necessary.  The largest event the
NimBLE controller will send is 45 bytes, but the default buffer size was
260.

This change:

* reduces the default event buffer size from 260 to 45
* increases the default lo-prio event count from 2 to 12
* increases the default hi-prio event count from 1 to 2


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 74c3c9bee638a425d7f2be65ce1d8637eee75c9c
Parents: 202e5c3
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 17:53:46 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll_hci.h  |  3 ++
 net/nimble/controller/src/ble_ll_conn_hci.c     | 32 ++++++++++----------
 net/nimble/controller/src/ble_ll_hci.c          |  2 ++
 net/nimble/host/include/host/ble_hs.h           | 18 ++---------
 net/nimble/host/src/ble_hs_cfg.c                |  2 +-
 net/nimble/transport/ram/src/ble_hci_ram.c      |  8 +++--
 6 files changed, 29 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/controller/include/controller/ble_ll_hci.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_hci.h b/net/nimble/controller/include/controller/ble_ll_hci.h
index 47daa76..43b98fc 100644
--- a/net/nimble/controller/include/controller/ble_ll_hci.h
+++ b/net/nimble/controller/include/controller/ble_ll_hci.h
@@ -24,6 +24,9 @@
 #define BLE_LL_SUPP_CMD_LEN (36)
 extern const uint8_t g_ble_ll_supp_cmds[BLE_LL_SUPP_CMD_LEN];
 
+/* The largest event the controller will send. */
+#define BLE_LL_MAX_EVT_LEN  (45)
+
 /*
  * This determines the number of outstanding commands allowed from the
  * host to the controller.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/controller/src/ble_ll_conn_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_hci.c b/net/nimble/controller/src/ble_ll_conn_hci.c
index f653ca6..d7001f8 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -203,22 +203,25 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status)
     }
 }
 
+
 /**
  * Called to create and send the number of completed packets event to the
  * host.
  *
- * Because of the ridiculous spec, all the connection handles are contiguous and
- * then all the completed packets are contiguous. In order to avoid multiple
- * passes through the connection list or allocating a large stack variable or
- * malloc, I just use the event buffer and place the completed packets after
- * the last possible handle. I then copy the completed packets to make it
- * contiguous with the handles.
- *
- * @param connsm
+ * Because of the ridiculous spec, all the connection handles are contiguous
+ * and then all the completed packets are contiguous. In order to avoid
+ * multiple passes through the connection list or allocating a large stack
+ * variable or malloc, I just use the event buffer and place the completed
+ * packets after the last possible handle. I then copy the completed packets
+ * to make it contiguous with the handles.
  */
 void
 ble_ll_conn_num_comp_pkts_event_send(void)
 {
+    /** The maximum number of handles that will fit in an event buffer. */
+    static const int max_handles =
+        (BLE_LL_MAX_EVT_LEN - BLE_HCI_EVENT_HDR_LEN - 1) / 4;
+
     int event_sent;
     uint8_t *evbuf;
     uint8_t *handle_ptr;
@@ -253,7 +256,7 @@ ble_ll_conn_num_comp_pkts_event_send(void)
                 }
                 handles = 0;
                 handle_ptr = evbuf + 3;
-                comp_pkt_ptr = handle_ptr + (sizeof(uint16_t) * 60);
+                comp_pkt_ptr = handle_ptr + (sizeof(uint16_t) * max_handles);
             }
 
             /* Add handle and complete packets */
@@ -264,11 +267,8 @@ ble_ll_conn_num_comp_pkts_event_send(void)
             comp_pkt_ptr += sizeof(uint16_t);
             ++handles;
 
-            /*
-             * The event buffer should fit at least 255 bytes so this means we
-             * can fit up to 60 handles per event (a little more but who cares).
-             */
-            if (handles == 60) {
+            /* Send now if the buffer is full. */
+            if (handles == max_handles) {
                 evbuf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
                 evbuf[1] = (handles * 2 * sizeof(uint16_t)) + 1;
                 evbuf[2] = handles;
@@ -285,9 +285,9 @@ ble_ll_conn_num_comp_pkts_event_send(void)
         evbuf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
         evbuf[1] = (handles * 2 * sizeof(uint16_t)) + 1;
         evbuf[2] = handles;
-        if (handles < 60) {
+        if (handles < max_handles) {
             /* Make the pkt counts contiguous with handles */
-            memmove(handle_ptr, evbuf + 3 + (60 * 2), handles * 2);
+            memmove(handle_ptr, evbuf + 3 + (max_handles * 2), handles * 2);
         }
         ble_ll_hci_event_send(evbuf);
         event_sent = 1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c b/net/nimble/controller/src/ble_ll_hci.c
index 352dda2..758568b 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -64,6 +64,8 @@ ble_ll_hci_event_send(uint8_t *evbuf)
 {
     int rc;
 
+    assert(BLE_HCI_EVENT_HDR_LEN + evbuf[1] <= BLE_LL_MAX_EVT_LEN);
+
     /* Count number of events sent */
     STATS_INC(ble_ll_stats, hci_events_sent);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 954d278..8d85009 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -95,26 +95,12 @@ typedef void ble_hs_sync_fn(void);
 
 struct ble_hs_cfg {
     /**
-     * An HCI buffer is a "flat" 260-byte buffer.  HCI buffers are used by the
-     * controller to send unsolicited events to the host.
-     *
-     * HCI buffers can get tied up when the controller sends lots of
-     * asynchronous / unsolicited events (i.e., non-acks).  When the controller
-     * needs to send one of these events, it allocates an HCI buffer, fills it
-     * with the event payload, and puts it on a host queue.  If the controller
-     * sends a quick burst of these events, the buffer pool may be exhausted,
-     * preventing the host from sending an HCI command to the controller.
-     *
      * Every time the controller sends a non-ack HCI event to the host, it also
      * allocates an OS event (it is unfortunate that these are both called
      * "events").  The OS event is put on the host-parent-task's event queue;
      * it is what wakes up the host-parent-task and indicates that an HCI event
-     * needs to be processsed.  The pool of OS events is allocated with the
-     * same number of elements as the HCI buffer pool.
-     */
-    /* XXX: This should either be renamed to indicate it is only used for OS
-     * events, or it should go away entirely (copy the number from the
-     * transport's config).
+     * needs to be processsed.  This setting should be equal to the total
+     * number of HCI event buffers that the transport is configured to use.
      */
     uint8_t max_hci_bufs;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/host/src/ble_hs_cfg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_cfg.c b/net/nimble/host/src/ble_hs_cfg.c
index fb5fb45..eca8407 100644
--- a/net/nimble/host/src/ble_hs_cfg.c
+++ b/net/nimble/host/src/ble_hs_cfg.c
@@ -27,7 +27,7 @@
 
 const struct ble_hs_cfg ble_hs_cfg_dflt = {
     /** HCI settings. */
-    .max_hci_bufs = 3,
+    .max_hci_bufs = 14,
 
     /** Connection settings. */
     .max_connections = BLE_HS_CFG_MAX_CONNECTIONS,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/74c3c9be/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index 2ce5fd6..ef46309 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -9,9 +9,11 @@
 
 /** Default configuration. */
 const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
-    .num_evt_hi_bufs = 1,
-    .num_evt_lo_bufs = 2,
-    .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
+    .num_evt_hi_bufs = 2,
+    .num_evt_lo_bufs = 12,
+
+    /* The largest event the nimble controller will send is 45 bytes. */
+    .evt_buf_sz = 45,
 };
 
 static ble_hci_trans_rx_cmd_fn *ble_hci_ram_rx_cmd_hs_cb;


[48/50] [abbrv] incubator-mynewt-core git commit: Fix incorrect handling of multiple connections and starting encrypted links with all of them

Posted by we...@apache.org.
Fix incorrect handling of multiple connections and starting encrypted links with all of them


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

Branch: refs/heads/phyrx_no_mbuf
Commit: c619a9fe6a2ccf6e42215c0c6cd7e281dcf61f75
Parents: 1a3ad80
Author: William San Filippo <wi...@runtime.io>
Authored: Tue Aug 9 15:49:07 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 apps/bletest/src/main.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c619a9fe/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index c2635dd..7cc6425 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -656,6 +656,8 @@ bletest_execute_advertiser(void)
     int i;
 #if (BLETEST_CONCURRENT_CONN_TEST == 1)
     int j;
+    uint16_t mask;
+    uint16_t reply_handle;
 #endif
     int rc;
     uint16_t handle;
@@ -727,10 +729,16 @@ bletest_execute_advertiser(void)
     if ((int32_t)(os_time_get() - g_next_os_time) >= 0) {
 #if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
         /* Do we need to send a LTK reply? */
-        if (g_bletest_ltk_reply_handle) {
-            //bletest_send_ltk_req_neg_reply(g_bletest_ltk_reply_handle);
-            bletest_send_ltk_req_reply(g_bletest_ltk_reply_handle);
-            g_bletest_ltk_reply_handle = 0;
+        mask = 1;
+        reply_handle = 1;
+        while (g_bletest_ltk_reply_handle && mask) {
+            if (g_bletest_ltk_reply_handle & mask) {
+                bletest_send_ltk_req_reply(reply_handle);
+                //bletest_send_ltk_req_neg_reply(reply_handle);
+                g_bletest_ltk_reply_handle &= ~mask;
+            }
+            ++reply_handle;
+            mask <<= 1;
         }
 #endif
         if (g_bletest_current_conns) {


[17/50] [abbrv] incubator-mynewt-core git commit: BLE Host - validate write upon rx of prep write

Posted by we...@apache.org.
BLE Host - validate write upon rx of prep write

Before, upon receiving a prepare write request, the code was only
verifying that the specified attribute existed.  There are a number of
checks that need to be performed in addition to that one:

1. Insufficient authorization.
2. Insufficient authentication.
3. Insufficient encryption key size.
4. Insufficient encryption (check if persisted bond exists for
   unencrypted peer).
5. Invalid handle.
6. Write not permitted.

The host does not properly handle 3 and 4 yet (for any writes).

Now the host rejects a prepare write request if any of the above issues
are detected (except for 3 and 4).


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

Branch: refs/heads/phyrx_no_mbuf
Commit: ac93fc4dab0d57ad72ad82bbdffdc28e4a8e153c
Parents: f0cff8c
Author: Christopher Collins <cc...@apache.org>
Authored: Sun Aug 7 17:48:44 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c           | 69 ++++++++++++++----------
 net/nimble/host/src/test/ble_att_svr_test.c | 62 +++++++++++++++++----
 2 files changed, 95 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ac93fc4d/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 1b420b5..f282093 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -237,9 +237,9 @@ ble_att_svr_get_sec_state(uint16_t conn_handle,
 }
 
 static int
-ble_att_svr_check_security(uint16_t conn_handle, int is_read,
-                           struct ble_att_svr_entry *entry,
-                           uint8_t *out_att_err)
+ble_att_svr_check_perms(uint16_t conn_handle, int is_read,
+                        struct ble_att_svr_entry *entry,
+                        uint8_t *out_att_err)
 {
     struct ble_gap_sec_state sec_state;
     int author;
@@ -248,10 +248,20 @@ ble_att_svr_check_security(uint16_t conn_handle, int is_read,
     int rc;
 
     if (is_read) {
+        if (!(entry->ha_flags & BLE_ATT_F_READ)) {
+            *out_att_err = BLE_ATT_ERR_READ_NOT_PERMITTED;
+            return BLE_HS_ENOTSUP;
+        }
+
         enc = entry->ha_flags & BLE_ATT_F_READ_ENC;
         authen = entry->ha_flags & BLE_ATT_F_READ_AUTHEN;
         author = entry->ha_flags & BLE_ATT_F_READ_AUTHOR;
     } else {
+        if (!(entry->ha_flags & BLE_ATT_F_WRITE)) {
+            *out_att_err = BLE_ATT_ERR_WRITE_NOT_PERMITTED;
+            return BLE_HS_ENOTSUP;
+        }
+
         enc = entry->ha_flags & BLE_ATT_F_WRITE_ENC;
         authen = entry->ha_flags & BLE_ATT_F_WRITE_AUTHEN;
         author = entry->ha_flags & BLE_ATT_F_WRITE_AUTHOR;
@@ -264,6 +274,8 @@ ble_att_svr_check_security(uint16_t conn_handle, int is_read,
 
     rc = ble_att_svr_get_sec_state(conn_handle, &sec_state);
     if (rc != 0) {
+        /* Peer no longer connected. */
+        *out_att_err = 0;
         return rc;
     }
 
@@ -300,13 +312,7 @@ ble_att_svr_read(uint16_t conn_handle,
     att_err = 0;    /* Silence gcc warning. */
 
     if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
-        if (!(entry->ha_flags & BLE_ATT_F_READ)) {
-            att_err = BLE_ATT_ERR_READ_NOT_PERMITTED;
-            rc = BLE_HS_ENOTSUP;
-            goto err;
-        }
-
-        rc = ble_att_svr_check_security(conn_handle, 1, entry, &att_err);
+        rc = ble_att_svr_check_perms(conn_handle, 1, entry, &att_err);
         if (rc != 0) {
             goto err;
         }
@@ -448,13 +454,7 @@ ble_att_svr_write(uint16_t conn_handle, struct ble_att_svr_entry *entry,
     BLE_HS_DBG_ASSERT(!ble_hs_locked_by_cur_task());
 
     if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
-        if (!(entry->ha_flags & BLE_ATT_F_WRITE)) {
-            att_err = BLE_ATT_ERR_WRITE_NOT_PERMITTED;
-            rc = BLE_HS_ENOTSUP;
-            goto done;
-        }
-
-        rc = ble_att_svr_check_security(conn_handle, 0, entry, &att_err);
+        rc = ble_att_svr_check_perms(conn_handle, 0, entry, &att_err);
         if (rc != 0) {
             goto done;
         }
@@ -2341,19 +2341,17 @@ ble_att_svr_prep_write(uint16_t conn_handle,
     while (!SLIST_EMPTY(prep_list)) {
         ble_att_svr_prep_extract(prep_list, &attr_handle, &om);
 
+        /* Attribute existence was verified during prepare-write request
+         * processing.
+         */
         attr = ble_att_svr_find_by_handle(attr_handle);
-        *err_handle = attr_handle;
-        if (attr == NULL) {
-            rc = BLE_ATT_ERR_INVALID_HANDLE;
-        } else {
-            rc = ble_att_svr_write(conn_handle, attr, 0, &om, &att_err);
-            if (rc != 0) {
-                rc = att_err;
-            }
-        }
+        BLE_HS_DBG_ASSERT(attr != NULL);
+
+        rc = ble_att_svr_write(conn_handle, attr, 0, &om, &att_err);
         os_mbuf_free_chain(om);
         if (rc != 0) {
-            return rc;
+            *err_handle = attr_handle;
+            return att_err;
         }
     }
 
@@ -2400,12 +2398,29 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
     os_mbuf_adj(*rxom, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
 
     attr_entry = ble_att_svr_find_by_handle(req.bapc_handle);
+
+    /* A prepare write request gets rejected for the following reasons:
+     * 1. Insufficient authorization.
+     * 2. Insufficient authentication.
+     * 3. Insufficient encryption key size (XXX: Not checked).
+     * 4. Insufficient encryption.
+     * 5. Invalid handle.
+     * 6. Write not permitted.
+     */
+
+    /* <5> */
     if (attr_entry == NULL) {
         rc = BLE_HS_ENOENT;
         att_err = BLE_ATT_ERR_INVALID_HANDLE;
         goto done;
     }
 
+    /* <1>, <2>, <4>, <6> */
+    rc = ble_att_svr_check_perms(conn_handle, 0, attr_entry, &att_err);
+    if (rc != 0) {
+        goto done;
+    }
+
     prep_entry = ble_att_svr_prep_alloc();
     if (prep_entry == NULL) {
         att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ac93fc4d/net/nimble/host/src/test/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_svr_test.c b/net/nimble/host/src/test/ble_att_svr_test.c
index 09e7bf9..86565e3 100644
--- a/net/nimble/host/src/test/ble_att_svr_test.c
+++ b/net/nimble/host/src/test/ble_att_svr_test.c
@@ -319,6 +319,15 @@ ble_att_svr_test_misc_attr_fn_w_2(uint16_t conn_handle, uint16_t attr_handle,
     }
 }
 
+static int
+ble_att_svr_test_misc_attr_fn_w_fail(uint16_t conn_handle,
+                                     uint16_t attr_handle,
+                                     uint8_t op, uint16_t offset,
+                                     struct os_mbuf **om, void *arg)
+{
+    return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+}
+
 static void
 ble_att_svr_test_misc_verify_w_1(void *data, int data_len)
 {
@@ -2071,6 +2080,7 @@ TEST_CASE(ble_att_svr_test_read_group_type)
 
 TEST_CASE(ble_att_svr_test_prep_write)
 {
+    struct ble_hs_conn *conn;
     uint16_t conn_handle;
     int i;
 
@@ -2089,9 +2099,22 @@ TEST_CASE(ble_att_svr_test_prep_write)
     ble_att_svr_test_misc_register_uuid16(0x8989, HA_FLAG_PERM_RW, 2,
                                           ble_att_svr_test_misc_attr_fn_w_2);
 
-    /* Register a third attribute that is not writable. */
+    /* 3: not writable. */
     ble_att_svr_test_misc_register_uuid16(0xabab, BLE_ATT_F_READ, 3,
                                           ble_att_svr_test_misc_attr_fn_r_1);
+    /* 4: Encryption required. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabac, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, 4,
+        ble_att_svr_test_misc_attr_fn_w_1);
+
+    /* 5: Encryption+authentication required. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabad, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC | BLE_ATT_F_WRITE_AUTHEN,
+        5, ble_att_svr_test_misc_attr_fn_w_1);
+
+    /* 6: Write callback always fails. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabae, BLE_ATT_F_WRITE, 6, ble_att_svr_test_misc_attr_fn_w_fail);
 
     /*** Empty write succeeds. */
     ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
@@ -2104,6 +2127,28 @@ TEST_CASE(ble_att_svr_test_prep_write)
     ble_att_svr_test_misc_prep_write(conn_handle, 53525, 0, data, 10,
                                      BLE_ATT_ERR_INVALID_HANDLE);
 
+    /*** Failure due to write-not-permitted. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 3, 0, data, 35,
+                                     BLE_ATT_ERR_WRITE_NOT_PERMITTED);
+
+    /*** Failure due to insufficient authentication (encryption required). */
+    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1,
+                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
+    /*** Encrypt connection; ensure previous prep write now succeeds. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    conn->bhc_sec_state.encrypted = 1;
+    ble_hs_unlock();
+
+    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
+
+    /*** Failure due to insufficient authentication (not authenticated). */
+    ble_att_svr_test_misc_prep_write(conn_handle, 5, 0, data, 35,
+                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
     /*** Failure for write starting at nonzero offset. */
     ble_att_svr_test_misc_prep_write(conn_handle, 1, 1, data, 10, 0);
     ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
@@ -2136,14 +2181,6 @@ TEST_CASE(ble_att_svr_test_prep_write)
                                      BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 1);
     ble_att_svr_test_misc_verify_w_1(NULL, 0);
 
-    /*** Failure due to attribute callback. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 3, 0, data, 35, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 3, 35, data + 35, 43, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 3, 78, data + 78, 1, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_WRITE_NOT_PERMITTED, 0);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
     /*** Successful two part write. */
     ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 20, 0);
     ble_att_svr_test_misc_prep_write(conn_handle, 1, 20, data + 20, 20, 0);
@@ -2188,6 +2225,13 @@ TEST_CASE(ble_att_svr_test_prep_write)
                                      0, 0);
     ble_att_svr_test_misc_verify_w_1(data, 12);
     ble_att_svr_test_misc_verify_w_2(data, 61);
+
+    /*** Fail due to attribute callback error. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 0, data, 35, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 35, data + 35, 43, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 78, data + 78, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 6);
 }
 
 TEST_CASE(ble_att_svr_test_notify)


[45/50] [abbrv] incubator-mynewt-core git commit: BLE Ctlr - max evt sz too low for rd-lcl-sup-ftrs

Posted by we...@apache.org.
BLE Ctlr - max evt sz too low for rd-lcl-sup-ftrs

In a previous commit (8b8d887358be1d64e6002fb4efb6b4e0673a1760), the max
event size was reduced from 260 to 45.  The belief was that the largest
event the NimBLE controller sends is 45 bytes (advertising report with
one entry, entry has 0x1f data bytes).  However, the controller's
response to the read-local-supported-features HCI command is larger:
0x44 + 2 byte header = 0x46 (70).


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 2aa6631131b58bf45f0f9166914d22fe44db24e4
Parents: a2e20c8
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 11 13:39:53 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 net/nimble/controller/include/controller/ble_ll_hci.h | 2 +-
 net/nimble/transport/ram/src/ble_hci_ram.c            | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2aa66311/net/nimble/controller/include/controller/ble_ll_hci.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_hci.h b/net/nimble/controller/include/controller/ble_ll_hci.h
index 43b98fc..e3a7e3e 100644
--- a/net/nimble/controller/include/controller/ble_ll_hci.h
+++ b/net/nimble/controller/include/controller/ble_ll_hci.h
@@ -25,7 +25,7 @@
 extern const uint8_t g_ble_ll_supp_cmds[BLE_LL_SUPP_CMD_LEN];
 
 /* The largest event the controller will send. */
-#define BLE_LL_MAX_EVT_LEN  (45)
+#define BLE_LL_MAX_EVT_LEN  (70)
 
 /*
  * This determines the number of outstanding commands allowed from the

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2aa66311/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index ef46309..f5191b8 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -10,10 +10,10 @@
 /** Default configuration. */
 const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
     .num_evt_hi_bufs = 2,
-    .num_evt_lo_bufs = 12,
+    .num_evt_lo_bufs = 8,
 
     /* The largest event the nimble controller will send is 45 bytes. */
-    .evt_buf_sz = 45,
+    .evt_buf_sz = 70,
 };
 
 static ble_hci_trans_rx_cmd_fn *ble_hci_ram_rx_cmd_hs_cb;


[23/50] [abbrv] incubator-mynewt-core git commit: BLE UART HCI - Remove debug log.

Posted by we...@apache.org.
BLE UART HCI - Remove debug log.

This was accidentally included in the initial commit!


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

Branch: refs/heads/phyrx_no_mbuf
Commit: dff04c856e50fe054dc9c8d47994ea8480260871
Parents: 90de67d
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Aug 5 10:37:27 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/transport/uart/src/ble_hci_uart.c | 18 ------------------
 1 file changed, 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dff04c85/net/nimble/transport/uart/src/ble_hci_uart.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/src/ble_hci_uart.c b/net/nimble/transport/uart/src/ble_hci_uart.c
index 98a49b6..1285f56 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -73,12 +73,6 @@ static void *ble_hci_uart_evt_buf;
 static struct os_mempool ble_hci_uart_pkt_pool;
 static void *ble_hci_uart_pkt_buf;
 
-#define BLE_HCI_UART_LOG_SZ 1024
-static uint8_t ble_hci_uart_tx_log[BLE_HCI_UART_LOG_SZ];
-static int ble_hci_uart_tx_log_sz;
-static uint8_t ble_hci_uart_rx_log[BLE_HCI_UART_LOG_SZ];
-static int ble_hci_uart_rx_log_sz;
-
 /**
  * An incoming or outgoing command or event.
  */
@@ -263,13 +257,6 @@ ble_hci_uart_tx_char(void *arg)
         break;
     }
 
-    if (rc != -1) {
-        ble_hci_uart_tx_log[ble_hci_uart_tx_log_sz++] = rc;
-        if (ble_hci_uart_tx_log_sz == sizeof ble_hci_uart_tx_log) {
-            ble_hci_uart_tx_log_sz = 0;
-        }
-    }
-
     return rc;
 }
 
@@ -410,11 +397,6 @@ ble_hci_uart_rx_acl(uint8_t data)
 static int
 ble_hci_uart_rx_char(void *arg, uint8_t data)
 {
-    ble_hci_uart_rx_log[ble_hci_uart_rx_log_sz++] = data;
-    if (ble_hci_uart_rx_log_sz == sizeof ble_hci_uart_rx_log) {
-        ble_hci_uart_rx_log_sz = 0;
-    }
-
     switch (ble_hci_uart_state.rx_type) {
     case BLE_HCI_UART_H4_NONE:
         return ble_hci_uart_rx_pkt_type(data);


[49/50] [abbrv] incubator-mynewt-core git commit: hw/bsp/nrf51-blenano - fix compile error.

Posted by we...@apache.org.
hw/bsp/nrf51-blenano - fix compile error.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 0008660e82c6e99b8a2b2268e197bb83b4bae8cf
Parents: a13dd64
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 11 13:30:36 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 hw/bsp/nrf51-blenano/src/os_bsp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0008660e/hw/bsp/nrf51-blenano/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51-blenano/src/os_bsp.c b/hw/bsp/nrf51-blenano/src/os_bsp.c
index 2b88f27..5f7e7a9 100644
--- a/hw/bsp/nrf51-blenano/src/os_bsp.c
+++ b/hw/bsp/nrf51-blenano/src/os_bsp.c
@@ -64,7 +64,7 @@ bsp_imgr_current_slot(void)
 }
 
 void
-os_bsp_init(void)
+bsp_init(void)
 {
     /*
      * XXX this reference is here to keep this function in.


[40/50] [abbrv] incubator-mynewt-core git commit: util - Fix up mem function comments.

Posted by we...@apache.org.
util - Fix up mem function comments.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: e5413e6c7cdb7decac233d62c08250d861a5fe01
Parents: 2697a4f
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 9 11:16:17 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 libs/util/src/mem.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e5413e6c/libs/util/src/mem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/mem.c b/libs/util/src/mem.c
index eeca71f..faf8234 100644
--- a/libs/util/src/mem.c
+++ b/libs/util/src/mem.c
@@ -67,7 +67,9 @@ mem_malloc_mempool(struct os_mempool *mempool, int num_blocks, int block_size,
 }
 
 /**
- * Mallocs a block of memory and initializes an mbuf pool to use it.
+ * Mallocs a block of memory and initializes an mbuf pool to use it.  The
+ * specified block_size indicates the size of an mbuf acquired from the pool if
+ * it does not contain a pkthdr.
  *
  * @param mempool               The mempool to initialize.
  * @param mbuf_pool             The mbuf pool to initialize.
@@ -111,6 +113,24 @@ mem_malloc_mbuf_pool(struct os_mempool *mempool,
     return 0;
 }
 
+/**
+ * Mallocs a block of memory and initializes an mbuf pool to use it.  The
+ * specified block_size indicates the size of an mbuf acquired from the pool if
+ * it contains a pkthdr.
+ *
+ * @param mempool               The mempool to initialize.
+ * @param mbuf_pool             The mbuf pool to initialize.
+ * @param num_blocks            The total number of mbufs in the pool.
+ * @param block_size            The size of each mbuf.
+ * @param name                  The name to give the mempool.
+ * @param out_buf               On success, this points to the malloced memory.
+ *                                  Pass NULL if you don't need this
+ *                                  information.
+ *
+ * @return                      0 on success;
+ *                              OS_ENOMEM on malloc failure;
+ *                              Other OS code on unexpected error.
+ */
 int
 mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
                         struct os_mbuf_pool *mbuf_pool, int num_blocks,


[26/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Use os_mbuf_append_from, not handrolled

Posted by we...@apache.org.
BLE Host - Use os_mbuf_append_from, not handrolled

This commit does not change any behavior.  Some code was re-inventing
os_mbuf_append_from().  Now it just calls the function.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 5f3def8132dc3b7dff8c09dd42fd328637cc68fe
Parents: b3610c4
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Aug 5 12:15:11 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att_svr.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5f3def81/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 7ccae06..2e1f158 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -617,7 +617,7 @@ ble_att_svr_tx_rsp(uint16_t conn_handle, int rc, struct os_mbuf *om,
         ble_hs_unlock();
     }
 
-    /* Free mbuf if it was not consumed (send failed). */
+    /* Free mbuf if it was not consumed (i.e., if the send failed). */
     os_mbuf_free_chain(om);
 
     return rc;
@@ -2367,7 +2367,6 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
     struct ble_att_prep_entry *prep_prev;
     struct ble_att_svr_entry *attr_entry;
     struct ble_hs_conn *conn;
-    struct os_mbuf *srcom;
     struct os_mbuf *txom;
     uint16_t err_handle;
     uint8_t *buf;
@@ -2428,16 +2427,10 @@ ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
         }
 
         /* Append attribute value from request onto prep mbuf. */
-        for (srcom = *rxom;
-             srcom != NULL;
-             srcom = SLIST_NEXT(srcom, om_next)) {
-
-            rc = os_mbuf_append(prep_entry->bape_value, srcom->om_data,
-                                srcom->om_len);
-            if (rc != 0) {
-                att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
-                break;
-            }
+        rc = os_mbuf_appendfrom(prep_entry->bape_value, *rxom, 0,
+                                OS_MBUF_PKTLEN(*rxom));
+        if (rc != 0) {
+            att_err = BLE_ATT_ERR_PREPARE_QUEUE_FULL;
         }
     }
 


[37/50] [abbrv] incubator-mynewt-core git commit: util - Functions to alloc mempools and mbuf pools.

Posted by we...@apache.org.
util - Functions to alloc mempools and mbuf pools.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: ef34d0170b896f17904babce6d09e838ba2a5b41
Parents: 02fa2d0
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Aug 8 15:57:20 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:26 2016 -0700

----------------------------------------------------------------------
 libs/util/include/util/mem.h |  38 ++++++++++++
 libs/util/src/mem.c          | 126 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ef34d017/libs/util/include/util/mem.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/mem.h b/libs/util/include/util/mem.h
new file mode 100644
index 0000000..52ddce4
--- /dev/null
+++ b/libs/util/include/util/mem.h
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_UTIL_MEM_
+#define H_UTIL_MEM_
+
+struct os_mempool;
+struct os_mbuf_pool;
+
+int mem_malloc_mempool(struct os_mempool *mempool, int num_blocks,
+                       int block_size, char *name, void **out_buf);
+
+int mem_malloc_mbuf_pool(struct os_mempool *mempool,
+                         struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                         int block_size, char *name,
+                         void **out_buf);
+int mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
+                            struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                            int block_size, char *name,
+                            void **out_buf);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ef34d017/libs/util/src/mem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/mem.c b/libs/util/src/mem.c
new file mode 100644
index 0000000..eeca71f
--- /dev/null
+++ b/libs/util/src/mem.c
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "os/os.h"
+
+/**
+ * Mallocs a block of memory and initializes a mempool to use it.
+ *
+ * @param mempool               The mempool to initialize.
+ * @param num_blocks            The total number of memory blocks in the
+ *                                  mempool.
+ * @param block_size            The size of each mempool entry.
+ * @param name                  The name to give the mempool.
+ * @param out_buf               On success, this points to the malloced memory.
+ *                                  Pass NULL if you don't need this
+ *                                  information.
+ *
+ * @return                      0 on success;
+ *                              OS_ENOMEM on malloc failure;
+ *                              Other OS code on unexpected error.
+ */
+int
+mem_malloc_mempool(struct os_mempool *mempool, int num_blocks, int block_size,
+                   char *name, void **out_buf)
+{
+    void *buf;
+    int rc;
+
+    block_size = OS_ALIGN(block_size, OS_ALIGNMENT);
+
+    if (num_blocks > 0) {
+        buf = malloc(OS_MEMPOOL_BYTES(num_blocks, block_size));
+        if (buf == NULL) {
+            return OS_ENOMEM;
+        }
+    } else {
+        buf = NULL;
+    }
+
+    rc = os_mempool_init(mempool, num_blocks, block_size, buf, name);
+    if (rc != 0) {
+        free(buf);
+        return rc;
+    }
+
+    if (out_buf != NULL) {
+        *out_buf = buf;
+    }
+
+    return 0;
+}
+
+/**
+ * Mallocs a block of memory and initializes an mbuf pool to use it.
+ *
+ * @param mempool               The mempool to initialize.
+ * @param mbuf_pool             The mbuf pool to initialize.
+ * @param num_blocks            The total number of mbufs in the pool.
+ * @param block_size            The size of each mbuf.
+ * @param name                  The name to give the mempool.
+ * @param out_buf               On success, this points to the malloced memory.
+ *                                  Pass NULL if you don't need this
+ *                                  information.
+ *
+ * @return                      0 on success;
+ *                              OS_ENOMEM on malloc failure;
+ *                              Other OS code on unexpected error.
+ */
+int
+mem_malloc_mbuf_pool(struct os_mempool *mempool,
+                     struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                     int block_size, char *name,
+                     void **out_buf)
+{
+    void *buf;
+    int rc;
+
+    block_size = OS_ALIGN(block_size + sizeof (struct os_mbuf), OS_ALIGNMENT);
+
+    rc = mem_malloc_mempool(mempool, num_blocks, block_size, name, &buf);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = os_mbuf_pool_init(mbuf_pool, mempool, block_size, num_blocks);
+    if (rc != 0) {
+        free(buf);
+        return rc;
+    }
+
+    if (out_buf != NULL) {
+        *out_buf = buf;
+    }
+
+    return 0;
+}
+
+int
+mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
+                        struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                        int block_size, char *name,
+                        void **out_buf)
+{
+    int rc;
+
+    rc = mem_malloc_mbuf_pool(mempool, mbuf_pool, num_blocks,
+                              block_size + sizeof (struct os_mbuf_pkthdr),
+                              name, out_buf);
+    return rc;
+}


[09/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Reset on hardware error.

Posted by we...@apache.org.
BLE Host - Reset on hardware error.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 97b45e7fcad49dd8bf49e52cb3cf6f06c5442d28
Parents: c79b2fe
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 13:30:54 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_hs.h  |  4 ++++
 net/nimble/host/src/ble_hs.c           |  6 ++++++
 net/nimble/host/src/ble_hs_priv.h      |  1 +
 net/nimble/host/src/host_hci.c         | 17 +++++++++++++++++
 net/nimble/include/nimble/hci_common.h |  3 +++
 5 files changed, 31 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/97b45e7f/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index faeaf14..4a707af 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -80,6 +80,10 @@ struct os_event;
 #define BLE_HS_ERR_SM_PEER_BASE     0x500   /* 1280 */
 #define BLE_HS_SM_PEER_ERR(x)       ((x) ? BLE_HS_ERR_SM_PEER_BASE + (x) : 0)
 
+/* Note: A hardware error of 0 is not success. */
+#define BLE_HS_ERR_HW_BASE          0x600   /* 1536 */
+#define BLE_HS_HW_ERR(x)            (BLE_HS_ERR_HW_BASE + (x))
+
 /* Defines the IO capabilities for the local device. */
 #define BLE_HS_IO_DISPLAY_ONLY              0x00
 #define BLE_HS_IO_DISPLAY_YESNO             0x01

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/97b45e7f/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 6e8a59f..95befc0 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -443,6 +443,12 @@ ble_hs_sched_reset(int reason)
     ble_hs_event_enqueue(&ble_hs_event_reset);
 }
 
+void
+ble_hs_hw_error(uint8_t hw_code)
+{
+    ble_hs_sched_reset(BLE_HS_HW_ERR(hw_code));
+}
+
 /**
  * Synchronizes the host with the controller by sending a sequence of HCI
  * commands.  This function must be called before any other host functionality

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/97b45e7f/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_priv.h b/net/nimble/host/src/ble_hs_priv.h
index 9e788dd..d8b5e82 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -102,6 +102,7 @@ int ble_hs_is_parent_task(void);
 void ble_hs_lock(void);
 void ble_hs_unlock(void);
 void ble_hs_sched_reset(int reason);
+void ble_hs_hw_error(uint8_t hw_code);
 void ble_hs_heartbeat_sched(int32_t ticks);
 void ble_hs_notifications_sched(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/97b45e7f/net/nimble/host/src/host_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c
index ed5dd6a..b9bdaf9 100644
--- a/net/nimble/host/src/host_hci.c
+++ b/net/nimble/host/src/host_hci.c
@@ -35,6 +35,7 @@ _Static_assert(sizeof (struct hci_data_hdr) == BLE_HCI_DATA_HDR_SZ,
 typedef int host_hci_event_fn(uint8_t event_code, uint8_t *data, int len);
 static host_hci_event_fn host_hci_rx_disconn_complete;
 static host_hci_event_fn host_hci_rx_encrypt_change;
+static host_hci_event_fn host_hci_rx_hw_error;
 static host_hci_event_fn host_hci_rx_num_completed_pkts;
 static host_hci_event_fn host_hci_rx_enc_key_refresh;
 static host_hci_event_fn host_hci_rx_le_meta;
@@ -70,6 +71,7 @@ struct host_hci_event_dispatch_entry {
 static const struct host_hci_event_dispatch_entry host_hci_event_dispatch[] = {
     { BLE_HCI_EVCODE_DISCONN_CMP, host_hci_rx_disconn_complete },
     { BLE_HCI_EVCODE_ENCRYPT_CHG, host_hci_rx_encrypt_change },
+    { BLE_HCI_EVCODE_HW_ERROR, host_hci_rx_hw_error },
     { BLE_HCI_EVCODE_NUM_COMP_PKTS, host_hci_rx_num_completed_pkts },
     { BLE_HCI_EVCODE_ENC_KEY_REFRESH, host_hci_rx_enc_key_refresh },
     { BLE_HCI_EVCODE_LE_META, host_hci_rx_le_meta },
@@ -185,6 +187,21 @@ host_hci_rx_encrypt_change(uint8_t event_code, uint8_t *data, int len)
 }
 
 static int
+host_hci_rx_hw_error(uint8_t event_code, uint8_t *data, int len)
+{
+    uint8_t hw_code;
+
+    if (len < BLE_HCI_EVENT_HW_ERROR_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    hw_code = data[0];
+    ble_hs_hw_error(hw_code);
+
+    return 0;
+}
+
+static int
 host_hci_rx_enc_key_refresh(uint8_t event_code, uint8_t *data, int len)
 {
     struct hci_encrypt_key_refresh evt;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/97b45e7f/net/nimble/include/nimble/hci_common.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_common.h b/net/nimble/include/nimble/hci_common.h
index 3b44b0e..f094cc7 100644
--- a/net/nimble/include/nimble/hci_common.h
+++ b/net/nimble/include/nimble/hci_common.h
@@ -480,6 +480,9 @@
 /* Event encryption change (code=0x08) */
 #define BLE_HCI_EVENT_ENCRYPT_CHG_LEN       (4)
 
+/* Event hardware error (code=0x10) */
+#define BLE_HCI_EVENT_HW_ERROR_LEN          (1)
+
 /* Event key refresh complete (code=0x30) */
 #define BLE_HCI_EVENT_ENC_KEY_REFRESH_LEN   (3)
 


[20/50] [abbrv] incubator-mynewt-core git commit: NimBLE - high / low prio event buffers.

Posted by we...@apache.org.
NimBLE - high / low prio event buffers.

Events have one of two priorities:
o Low-priority   (BLE_HCI_TRANS_BUF_EVT_LO)
o High-priority  (BLE_HCI_TRANS_BUF_EVT_HI)

Low-priority event buffers are only used for advertising reports.  If
there are no free low-priority event buffers, then an incoming
advertising report will get dropped.

High-priority event buffers are for everything except advertising
reports.  If there are no free high-priority event buffers, a request to
allocate one will try to allocate a low-priority buffer instead.

If you want all events to be given equal treatment, then you should
allocate low-priority events only.

Event priorities solve the problem of critical events getting dropped
due to a flood of advertising reports.  This solution is likely
temporary: when HCI flow control is added, event priorities may become
obsolete.

Not all transports distinguish between low and high priority events.  If
the transport does not have separate settings for low and high buffer
counts, then it treats all events with equal priority.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 13019264c26633b973b0a6a0ff600ab8b34c3928
Parents: 5862c8f
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 16:31:05 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_hs_test_util.c     |  1 -
 net/nimble/include/nimble/ble_hci_trans.h       | 27 ++++--
 .../ram/include/transport/ram/ble_hci_ram.h     | 17 +++-
 net/nimble/transport/ram/src/ble_hci_ram.c      | 95 +++++++++++++++-----
 4 files changed, 110 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index cfb86fa..4f1c706 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1382,7 +1382,6 @@ ble_hs_test_util_init(void)
                          ble_hs_test_util_pkt_txed, NULL);
 
     hci_cfg = ble_hci_ram_cfg_dflt;
-    hci_cfg.num_evt_bufs = cfg.max_hci_bufs;
     rc = ble_hci_ram_init(&hci_cfg);
     TEST_ASSERT_FATAL(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/include/nimble/ble_hci_trans.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble_hci_trans.h b/net/nimble/include/nimble/ble_hci_trans.h
index 822da96..3e2ec7a 100644
--- a/net/nimble/include/nimble/ble_hci_trans.h
+++ b/net/nimble/include/nimble/ble_hci_trans.h
@@ -27,13 +27,30 @@ struct os_mbuf;
 
 /*** Type of buffers for holding commands and events. */
 /**
- * Low-priority event (advertising reports).  A request to allocate a
- * high-priority event buffer may allocate one of these instead if no
- * high-priority buffers are available.
+ * Controller-to-host event buffers.  Events have one of two priorities:
+ * o Low-priority   (BLE_HCI_TRANS_BUF_EVT_LO)
+ * o High-priority  (BLE_HCI_TRANS_BUF_EVT_HI)
+ *
+ * Low-priority event buffers are only used for advertising reports.  If there
+ * are no free low-priority event buffers, then an incoming advertising report
+ * will get dropped.
+ *
+ * High-priority event buffers are for everything except advertising reports.
+ * If there are no free high-priority event buffers, a request to allocate one
+ * will try to allocate a low-priority buffer instead.
+ *
+ * If you want all events to be given equal treatment, then you should allocate
+ * low-priority events only.
+ *
+ * Event priorities solve the problem of critical events getting dropped due to
+ * a flood of advertising reports.  This solution is likely temporary: when
+ * HCI flow control is added, event priorities may become obsolete.
+ *
+ * Not all transports distinguish between low and high priority events.  If the
+ * transport does not have separate settings for low and high buffer counts,
+ * then it treats all events with equal priority.
  */
 #define BLE_HCI_TRANS_BUF_EVT_LO    1
-
-/* High-priority event (all events except advertising reports). */
 #define BLE_HCI_TRANS_BUF_EVT_HI    2
 
 /* Host-to-controller command. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
index 1c5b58e..9d2d672 100644
--- a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
+++ b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
@@ -4,8 +4,23 @@
 #include "nimble/ble_hci_trans.h"
 
 struct ble_hci_ram_cfg {
-    uint16_t num_evt_bufs;
+    /** Number of high-priority event buffers. */
+    uint16_t num_evt_hi_bufs;
+
+    /** Number of low-priority event buffers. */
+    uint16_t num_evt_lo_bufs;
+
+    /** Size of each event buffer, in bytes. */
     uint16_t evt_buf_sz;
+
+    /* Note: For information about high-priority vs. low-priority event
+     * buffers, see net/nimble/include/nimble/ble_hci_trans.h.
+     */
+
+    /* Note: host-to-controller command buffers are not configurable.  The RAM
+     * transport only allows one outstanding command, so it uses a single
+     * statically-allocated buffer.
+     */
 };
 
 extern const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index aac5eb1..23a375d 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -7,7 +7,8 @@
 
 /** Default configuration. */
 const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
-    .num_evt_bufs = 3,
+    .num_evt_hi_bufs = 1,
+    .num_evt_lo_bufs = 2,
     .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
 };
 
@@ -23,17 +24,19 @@ static void *ble_hci_ram_rx_acl_hs_arg;
 static ble_hci_trans_rx_acl_fn *ble_hci_ram_rx_acl_ll_cb;
 static void *ble_hci_ram_rx_acl_ll_arg;
 
-static struct os_mempool ble_hci_ram_evt_pool;
-static void *ble_hci_ram_evt_buf;
+static struct os_mempool ble_hci_ram_evt_hi_pool;
+static void *ble_hci_ram_evt_hi_buf;
+static struct os_mempool ble_hci_ram_evt_lo_pool;
+static void *ble_hci_ram_evt_lo_buf;
 
 static uint8_t *ble_hci_ram_hs_cmd_buf;
 static uint8_t ble_hci_ram_hs_cmd_buf_alloced;
 
 void
 ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
-                                void *cmd_arg,
-                                ble_hci_trans_rx_acl_fn *acl_cb,
-                                void *acl_arg)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_ram_rx_cmd_hs_cb = cmd_cb;
     ble_hci_ram_rx_cmd_hs_arg = cmd_arg;
@@ -43,9 +46,9 @@ ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
 
 void
 ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
-                                void *cmd_arg,
-                                ble_hci_trans_rx_acl_fn *acl_cb,
-                                void *acl_arg)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_ram_rx_cmd_ll_cb = cmd_cb;
     ble_hci_ram_rx_cmd_ll_arg = cmd_arg;
@@ -78,7 +81,7 @@ ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
 int
 ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
 {
-    int rc;
+   int rc;
 
     assert(ble_hci_ram_rx_acl_ll_cb != NULL);
 
@@ -103,9 +106,18 @@ ble_hci_trans_buf_alloc(int type)
     uint8_t *buf;
 
     switch (type) {
-    case BLE_HCI_TRANS_BUF_EVT_LO:
     case BLE_HCI_TRANS_BUF_EVT_HI:
-        buf = os_memblock_get(&ble_hci_ram_evt_pool);
+        buf = os_memblock_get(&ble_hci_ram_evt_hi_pool);
+        if (buf == NULL) {
+            /* If no high-priority event buffers remain, try to grab a
+             * low-priority one.
+             */
+            buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
+        }
+        break;
+
+    case BLE_HCI_TRANS_BUF_EVT_LO:
+        buf = os_memblock_get(&ble_hci_ram_evt_lo_pool);
         break;
 
     case BLE_HCI_TRANS_BUF_CMD:
@@ -130,8 +142,12 @@ ble_hci_trans_buf_free(uint8_t *buf)
     if (buf == ble_hci_ram_hs_cmd_buf) {
         assert(ble_hci_ram_hs_cmd_buf_alloced);
         ble_hci_ram_hs_cmd_buf_alloced = 0;
+    } else if (os_memblock_from(&ble_hci_ram_evt_hi_pool, buf)) {
+        rc = os_memblock_put(&ble_hci_ram_evt_hi_pool, buf);
+        assert(rc == 0);
     } else {
-        rc = os_memblock_put(&ble_hci_ram_evt_pool, buf);
+        assert(os_memblock_from(&ble_hci_ram_evt_lo_pool, buf));
+        rc = os_memblock_put(&ble_hci_ram_evt_lo_pool, buf);
         assert(rc == 0);
     }
 }
@@ -139,8 +155,11 @@ ble_hci_trans_buf_free(uint8_t *buf)
 static void
 ble_hci_ram_free_mem(void)
 {
-    free(ble_hci_ram_evt_buf);
-    ble_hci_ram_evt_buf = NULL;
+    free(ble_hci_ram_evt_hi_buf);
+    ble_hci_ram_evt_hi_buf = NULL;
+
+    free(ble_hci_ram_evt_lo_buf);
+    ble_hci_ram_evt_lo_buf = NULL;
 
     free(ble_hci_ram_hs_cmd_buf);
     ble_hci_ram_hs_cmd_buf = NULL;
@@ -150,9 +169,21 @@ ble_hci_ram_free_mem(void)
 int
 ble_hci_trans_reset(void)
 {
+    /* No work to do.  All allocated buffers are owned by the host or
+     * controller, and they will get freed by their owners.
+     */
     return 0;
 }
 
+/**
+ * Initializes the RAM HCI transport module.
+ *
+ * @param cfg                   The settings to initialize the HCI RAM
+ *                                  transport with.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
 int
 ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
 {
@@ -160,17 +191,35 @@ ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
 
     ble_hci_ram_free_mem();
 
-    ble_hci_ram_evt_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_bufs,
-                                                  cfg->evt_buf_sz));
-    if (ble_hci_ram_evt_buf == NULL) {
-        rc = ENOMEM;
+    if (cfg->num_evt_hi_bufs > 0) {
+        ble_hci_ram_evt_hi_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_hi_bufs,
+                                                         cfg->evt_buf_sz));
+        if (ble_hci_ram_evt_hi_buf == NULL) {
+            rc = ENOMEM;
+            goto err;
+        }
+    }
+
+    rc = os_mempool_init(&ble_hci_ram_evt_hi_pool, cfg->num_evt_hi_bufs,
+                         cfg->evt_buf_sz, ble_hci_ram_evt_hi_buf,
+                         "ble_hci_ram_evt_hi_pool");
+    if (rc != 0) {
+        rc = EINVAL;
         goto err;
     }
 
-    /* Create memory pool of command buffers */
-    rc = os_mempool_init(&ble_hci_ram_evt_pool, cfg->num_evt_bufs,
-                         cfg->evt_buf_sz, ble_hci_ram_evt_buf,
-                         "ble_hci_ram_evt_pool");
+    if (cfg->num_evt_lo_bufs > 0) {
+        ble_hci_ram_evt_lo_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_lo_bufs,
+                                                         cfg->evt_buf_sz));
+        if (ble_hci_ram_evt_lo_buf == NULL) {
+            rc = ENOMEM;
+            goto err;
+        }
+    }
+
+    rc = os_mempool_init(&ble_hci_ram_evt_lo_pool, cfg->num_evt_lo_bufs,
+                         cfg->evt_buf_sz, ble_hci_ram_evt_lo_buf,
+                         "ble_hci_ram_evt_lo_pool");
     if (rc != 0) {
         rc = EINVAL;
         goto err;


[14/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Send exec-write when prep-writes cmplt.

Posted by we...@apache.org.
BLE Host - Send exec-write when prep-writes cmplt.

This fixes a bug where the GATT client would never send the execute
write command when performing a long-write procedure.  Instead, it would
repeatedly send 0-length prepare write requests.

Also, the GATT client is now more strict about when it accepts prepare
write responses and execute write responses.  Some unit tests were
passing, despite the bug described in the above paragraph, because an
incoming execute write response was accepted when it should have been.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: c46ed0ccb49b4b7d21c8266d38a69d530717add0
Parents: a2d0408
Author: Christopher Collins <cc...@apache.org>
Authored: Sat Aug 6 16:06:06 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gattc.c                | 44 ++++++-----
 net/nimble/host/src/test/ble_gatt_write_test.c | 82 +++++++++++++++++----
 net/nimble/host/src/test/ble_hs_test_util.c    | 23 ++++++
 net/nimble/host/src/test/ble_hs_test_util.h    |  3 +
 4 files changed, 120 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c46ed0cc/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 67c13d6..43d2134 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -177,7 +177,6 @@ struct ble_gattc_proc {
         struct {
             struct ble_gatt_attr attr;
             uint16_t length;
-            uint8_t exec_sent:1;
             ble_gatt_attr_fn *cb;
             void *cb_arg;
         } write_long;
@@ -187,7 +186,6 @@ struct ble_gattc_proc {
             uint8_t num_attrs;
             uint8_t cur_attr;
             uint16_t length;
-            uint8_t exec_sent:1;
             ble_gatt_reliable_attr_fn *cb;
             void *cb_arg;
         } write_reliable;
@@ -3370,6 +3368,7 @@ ble_gattc_write_long_go(struct ble_gattc_proc *proc, int cb_on_err)
     struct ble_att_prep_write_cmd prep_req;
     struct ble_att_exec_write_req exec_req;
     struct os_mbuf *om;
+    int write_len;
     int max_sz;
     int rc;
 
@@ -3377,14 +3376,6 @@ ble_gattc_write_long_go(struct ble_gattc_proc *proc, int cb_on_err)
 
     om = NULL;
 
-    if (proc->write_long.attr.offset +
-        OS_MBUF_PKTLEN(proc->write_long.attr.om) <= 0) {
-
-        exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
-        rc = ble_att_clt_tx_exec_write(proc->conn_handle, &exec_req);
-        goto done;
-    }
-
     max_sz = ble_att_mtu(proc->conn_handle) -
              BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
     if (max_sz <= 0) {
@@ -3393,11 +3384,17 @@ ble_gattc_write_long_go(struct ble_gattc_proc *proc, int cb_on_err)
         goto done;
     }
 
-    proc->write_long.length =
-        min(max_sz,
-            OS_MBUF_PKTLEN(proc->write_long.attr.om) -
-                proc->write_long.attr.offset);
+    write_len = min(max_sz,
+                    OS_MBUF_PKTLEN(proc->write_long.attr.om) -
+                        proc->write_long.attr.offset);
 
+    if (write_len <= 0) {
+        exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
+        rc = ble_att_clt_tx_exec_write(proc->conn_handle, &exec_req);
+        goto done;
+    }
+
+    proc->write_long.length = write_len;
     om = ble_hs_mbuf_att_pkt();
     if (om == NULL) {
         rc = BLE_HS_ENOMEM;
@@ -3481,6 +3478,15 @@ ble_gattc_write_long_rx_prep(struct ble_gattc_proc *proc,
     }
 
     /* Verify the response. */
+    if (proc->write_long.attr.offset >=
+        OS_MBUF_PKTLEN(proc->write_long.attr.om)) {
+
+        /* Expecting a prepare write response, not an execute write
+         * response.
+         */
+        rc = BLE_HS_EBADDATA;
+        goto err;
+    }
     if (rsp->bapc_handle != proc->write_long.attr.handle) {
         rc = BLE_HS_EBADDATA;
         goto err;
@@ -3531,9 +3537,13 @@ ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status)
 {
     ble_gattc_dbg_assert_proc_not_inserted(proc);
 
-    /* Ignore the response if we haven't sent the corresponding request yet. */
-    if (!proc->write_long.exec_sent) {
-        return 0;
+    if (proc->write_long.attr.offset <
+        OS_MBUF_PKTLEN(proc->write_long.attr.om)) {
+
+        /* Expecting an execute write response, not a prepare write
+         * response.
+         */
+        return BLE_HS_EBADDATA;
     }
 
     ble_gattc_write_long_cb(proc, status, 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c46ed0cc/net/nimble/host/src/test/ble_gatt_write_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_write_test.c b/net/nimble/host/src/test/ble_gatt_write_test.c
index f145ea8..f548198 100644
--- a/net/nimble/host/src/test/ble_gatt_write_test.c
+++ b/net/nimble/host/src/test/ble_gatt_write_test.c
@@ -123,6 +123,7 @@ ble_gatt_write_test_rx_exec_rsp(uint16_t conn_handle)
 static void
 ble_gatt_write_test_misc_long_good(int attr_len)
 {
+    uint16_t mtu;
     int off;
     int len;
     int rc;
@@ -132,6 +133,8 @@ ble_gatt_write_test_misc_long_good(int attr_len)
     ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
                                  NULL, NULL);
 
+    mtu = ble_att_mtu(2);
+
     rc = ble_hs_test_util_gatt_write_long_flat(
         2, 100, ble_gatt_write_test_attr_value, attr_len,
         ble_gatt_write_test_cb_good, &attr_len);
@@ -139,17 +142,18 @@ ble_gatt_write_test_misc_long_good(int attr_len)
 
     off = 0;
     while (off < attr_len) {
-        /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_tx_all();
-
-        /* Receive Prep Write response. */
-        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
         if (off + len > attr_len) {
             len = attr_len - off;
         }
-        ble_gatt_write_test_rx_prep_rsp(2, 100, off,
-                                        ble_gatt_write_test_attr_value + off,
-                                        len);
+
+        /* Send the pending ATT Prep Write Command. */
+        ble_hs_test_util_verify_tx_prep_write(
+            100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Receive Prep Write response. */
+        ble_gatt_write_test_rx_prep_rsp(
+            2, 100, off, ble_gatt_write_test_attr_value + off, len);
 
         /* Verify callback hasn't gotten called. */
         TEST_ASSERT(!ble_gatt_write_test_cb_called);
@@ -157,6 +161,9 @@ ble_gatt_write_test_misc_long_good(int attr_len)
         off += len;
     }
 
+    /* Verify execute write request sent. */
+    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
+
     /* Receive Exec Write response. */
     ble_hs_test_util_tx_all();
     ble_gatt_write_test_rx_exec_rsp(2);
@@ -172,6 +179,7 @@ static void
 ble_gatt_write_test_misc_long_bad(int attr_len,
                                   ble_gatt_write_test_long_fail_fn *cb)
 {
+    uint16_t mtu;
     int fail_now;
     int off;
     int len;
@@ -181,6 +189,7 @@ ble_gatt_write_test_misc_long_bad(int attr_len,
 
     ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
                                  NULL, NULL);
+    mtu = ble_att_mtu(2);
 
     rc = ble_hs_test_util_gatt_write_long_flat(
         2, 100, ble_gatt_write_test_attr_value, attr_len,
@@ -190,9 +199,14 @@ ble_gatt_write_test_misc_long_bad(int attr_len,
     fail_now = 0;
     off = 0;
     while (off < attr_len) {
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len > attr_len) {
+            len = attr_len - off;
+        }
+
         /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_tx_all();
-        TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
+        ble_hs_test_util_verify_tx_prep_write(
+            100, off, ble_gatt_write_test_attr_value + off, len);
 
         /* Receive Prep Write response. */
         len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
@@ -283,9 +297,13 @@ static void
 ble_gatt_write_test_misc_reliable_good(
     struct ble_hs_test_util_flat_attr *flat_attrs)
 {
+    const struct ble_hs_test_util_flat_attr *attr;
     struct ble_gatt_attr attrs[16];
+    uint16_t mtu;
     int num_attrs;
     int attr_idx;
+    int len;
+    int off;
     int rc;
     int i;
 
@@ -299,24 +317,43 @@ ble_gatt_write_test_misc_reliable_good(
 
     ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
                                  NULL, NULL);
+    mtu = ble_att_mtu(2);
 
     rc = ble_gattc_write_reliable(2, attrs, num_attrs,
                                   ble_gatt_write_test_reliable_cb_good, NULL);
     TEST_ASSERT(rc == 0);
 
-    for (attr_idx = 0; attr_idx < num_attrs; attr_idx++) {
+    attr_idx = 0;
+    off = 0;
+    while (attr_idx < num_attrs) {
+        attr = flat_attrs + attr_idx;
+
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len > attr->value_len) {
+            len = attr->value_len - off;
+        }
+
         /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_tx_all();
+        ble_hs_test_util_verify_tx_prep_write(attr->handle, off,
+                                              attr->value + off, len);
 
         /* Receive Prep Write response. */
-        ble_gatt_write_test_rx_prep_rsp(2, flat_attrs[attr_idx].handle, 0,
-                                        flat_attrs[attr_idx].value,
-                                        flat_attrs[attr_idx].value_len);
+        ble_gatt_write_test_rx_prep_rsp(2, attr->handle, off,
+                                        attr->value + off, len);
 
         /* Verify callback hasn't gotten called. */
         TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+        if (off >= attr->value_len) {
+            attr_idx++;
+            off = 0;
+        }
     }
 
+    /* Verify execute write request sent. */
+    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
+
     /* Receive Exec Write response. */
     ble_hs_test_util_tx_all();
     ble_gatt_write_test_rx_exec_rsp(2);
@@ -509,6 +546,21 @@ TEST_CASE(ble_gatt_write_test_reliable_good)
         }, {
             0
         } }));
+
+    /*** Long attributes. */
+    ble_gatt_write_test_misc_reliable_good(
+        ((struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 100,
+            .value_len = 20,
+            .value = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 },
+        }, {
+            .handle = 144,
+            .value_len = 20,
+            .value = { 11,12,13,14,15,16,17,18,19,110,
+                       111,112,113,114,115,116,117,118,119,120 },
+        }, {
+            0
+        } }));
 }
 
 TEST_CASE(ble_gatt_write_test_long_queue_full)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c46ed0cc/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 4f1c706..54a3527 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1051,6 +1051,29 @@ ble_hs_test_util_tx_all(void)
 }
 
 void
+ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle, uint16_t offset,
+                                      const void *data, int data_len)
+{
+    struct ble_att_prep_write_cmd req;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) ==
+                BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
+
+    om = os_mbuf_pullup(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_prep_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.bapc_handle == attr_handle);
+    TEST_ASSERT(req.bapc_offset == offset);
+    TEST_ASSERT(os_mbuf_cmpf(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+                             data, data_len) == 0);
+}
+
+void
 ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags)
 {
     struct ble_att_exec_write_req req;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c46ed0cc/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index 00b090f..0ae6cf5 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -129,6 +129,9 @@ void ble_hs_test_util_rx_disconn_complete_event(
 uint8_t *ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
                                         uint8_t *out_param_len);
 void ble_hs_test_util_tx_all(void);
+void ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle,
+                                           uint16_t offset,
+                                           const void *data, int data_len);
 void ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags);
 void ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len);
 void ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data,


[50/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Allow MTU to be checked for unconn peer

Posted by we...@apache.org.
BLE Host - Allow MTU to be checked for unconn peer

A previous commit (8ec1b8d9aa8b33a1a26e0d47841331870e97cd88) went a
little overboard in assuming a peer is connected.  Some code checks a
peer's MTU without knowing if the peer is connected.  A return value of
0 indicates the peer is not connected.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: a13dd6410794e47800f7c5c03beb7c5f950ff701
Parents: 8e95fbe
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 10 17:30:21 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a13dd641/net/nimble/host/src/ble_att.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att.c b/net/nimble/host/src/ble_att.c
index d5160d0..29e359a 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -147,8 +147,8 @@ void
 ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
                        struct ble_l2cap_chan **out_chan)
 {
-    ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_ATT,
-                                    out_conn, out_chan);
+    ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
+                               out_conn, out_chan);
 }
 
 void


[07/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by we...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci.c b/net/nimble/host/src/ble_hs_hci.c
new file mode 100644
index 0000000..cd5cc6c
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_hci.c
@@ -0,0 +1,528 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "os/os.h"
+#include "nimble/ble_hci_trans.h"
+#include "ble_hs_priv.h"
+#include "ble_hs_dbg_priv.h"
+
+#define BLE_HCI_CMD_TIMEOUT     (OS_TICKS_PER_SEC)
+
+static struct os_mutex ble_hs_hci_mutex;
+static struct os_sem ble_hs_hci_sem;
+
+static uint8_t *ble_hs_hci_ack;
+static uint16_t ble_hs_hci_buf_sz;
+static uint8_t ble_hs_hci_max_pkts;
+
+#if PHONY_HCI_ACKS
+static ble_hs_hci_phony_ack_fn *ble_hs_hci_phony_ack_cb;
+#endif
+
+#if PHONY_HCI_ACKS
+void
+ble_hs_hci_set_phony_ack_cb(ble_hs_hci_phony_ack_fn *cb)
+{
+    ble_hs_hci_phony_ack_cb = cb;
+}
+#endif
+
+static void
+ble_hs_hci_lock(void)
+{
+    int rc;
+
+    rc = os_mutex_pend(&ble_hs_hci_mutex, 0xffffffff);
+    BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+static void
+ble_hs_hci_unlock(void)
+{
+    int rc;
+
+    rc = os_mutex_release(&ble_hs_hci_mutex);
+    BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+int
+ble_hs_hci_set_buf_sz(uint16_t pktlen, uint8_t max_pkts)
+{
+    if (pktlen == 0 || max_pkts == 0) {
+        return BLE_HS_EINVAL;
+    }
+
+    ble_hs_hci_buf_sz = pktlen;
+    ble_hs_hci_max_pkts = max_pkts;
+
+    return 0;
+}
+
+static int
+ble_hs_hci_rx_cmd_complete(uint8_t event_code, uint8_t *data, int len,
+                           struct ble_hs_hci_ack *out_ack)
+{
+    uint16_t opcode;
+    uint8_t *params;
+    uint8_t params_len;
+    uint8_t num_pkts;
+
+    if (len < BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    num_pkts = data[2];
+    opcode = le16toh(data + 3);
+    params = data + 5;
+
+    /* XXX: Process num_pkts field. */
+    (void)num_pkts;
+
+    out_ack->bha_opcode = opcode;
+
+    params_len = len - BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN;
+    if (params_len > 0) {
+        out_ack->bha_status = BLE_HS_HCI_ERR(params[0]);
+    } else if (opcode == BLE_HCI_OPCODE_NOP) {
+        out_ack->bha_status = 0;
+    } else {
+        out_ack->bha_status = BLE_HS_ECONTROLLER;
+    }
+
+    /* Don't include the status byte in the parameters blob. */
+    if (params_len > 1) {
+        out_ack->bha_params = params + 1;
+        out_ack->bha_params_len = params_len - 1;
+    } else {
+        out_ack->bha_params = NULL;
+        out_ack->bha_params_len = 0;
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_rx_cmd_status(uint8_t event_code, uint8_t *data, int len,
+                         struct ble_hs_hci_ack *out_ack)
+{
+    uint16_t opcode;
+    uint8_t num_pkts;
+    uint8_t status;
+
+    if (len < BLE_HCI_EVENT_CMD_STATUS_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    status = data[2];
+    num_pkts = data[3];
+    opcode = le16toh(data + 4);
+
+    /* XXX: Process num_pkts field. */
+    (void)num_pkts;
+
+    out_ack->bha_opcode = opcode;
+    out_ack->bha_params = NULL;
+    out_ack->bha_params_len = 0;
+    out_ack->bha_status = BLE_HS_HCI_ERR(status);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_process_ack(uint16_t expected_opcode,
+                       uint8_t *params_buf, uint8_t params_buf_len,
+                       struct ble_hs_hci_ack *out_ack)
+{
+    uint8_t event_code;
+    uint8_t param_len;
+    uint8_t event_len;
+    int rc;
+
+    BLE_HS_DBG_ASSERT(ble_hs_hci_ack != NULL);
+
+    /* Count events received */
+    STATS_INC(ble_hs_stats, hci_event);
+
+    /* Display to console */
+    ble_hs_dbg_event_disp(ble_hs_hci_ack);
+
+    event_code = ble_hs_hci_ack[0];
+    param_len = ble_hs_hci_ack[1];
+    event_len = param_len + 2;
+
+    /* Clear ack fields up front to silence spurious gcc warnings. */
+    memset(out_ack, 0, sizeof *out_ack);
+
+    switch (event_code) {
+    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
+        rc = ble_hs_hci_rx_cmd_complete(event_code, ble_hs_hci_ack,
+                                         event_len, out_ack);
+        break;
+
+    case BLE_HCI_EVCODE_COMMAND_STATUS:
+        rc = ble_hs_hci_rx_cmd_status(event_code, ble_hs_hci_ack,
+                                       event_len, out_ack);
+        break;
+
+    default:
+        BLE_HS_DBG_ASSERT(0);
+        rc = BLE_HS_EUNKNOWN;
+        break;
+    }
+
+    if (rc == 0) {
+        if (params_buf == NULL) {
+            out_ack->bha_params_len = 0;
+        } else {
+            if (out_ack->bha_params_len > params_buf_len) {
+                out_ack->bha_params_len = params_buf_len;
+                rc = BLE_HS_ECONTROLLER;
+            }
+            memcpy(params_buf, out_ack->bha_params, out_ack->bha_params_len);
+        }
+        out_ack->bha_params = params_buf;
+
+        if (out_ack->bha_opcode != expected_opcode) {
+            rc = BLE_HS_ECONTROLLER;
+        }
+    }
+
+    if (rc != 0) {
+        STATS_INC(ble_hs_stats, hci_invalid_ack);
+    }
+
+    return rc;
+}
+
+static int
+ble_hs_hci_wait_for_ack(void)
+{
+    int rc;
+
+#if PHONY_HCI_ACKS
+    if (ble_hs_hci_phony_ack_cb == NULL) {
+        rc = BLE_HS_ETIMEOUT_HCI;
+    } else {
+        ble_hs_hci_ack =
+            ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+        BLE_HS_DBG_ASSERT(ble_hs_hci_ack != NULL);
+        rc = ble_hs_hci_phony_ack_cb(ble_hs_hci_ack, 260);
+    }
+#else
+    rc = os_sem_pend(&ble_hs_hci_sem, BLE_HCI_CMD_TIMEOUT);
+    switch (rc) {
+    case 0:
+        BLE_HS_DBG_ASSERT(ble_hs_hci_ack != NULL);
+        break;
+    case OS_TIMEOUT:
+        rc = BLE_HS_ETIMEOUT_HCI;
+        STATS_INC(ble_hs_stats, hci_timeout);
+        break;
+    default:
+        rc = BLE_HS_EOS;
+        break;
+    }
+#endif
+
+    return rc;
+}
+
+int
+ble_hs_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_len,
+                  uint8_t *out_evt_buf_len)
+{
+    struct ble_hs_hci_ack ack;
+    uint16_t opcode;
+    int rc;
+
+    opcode = le16toh(cmd);
+
+    BLE_HS_DBG_ASSERT(ble_hs_hci_ack == NULL);
+    ble_hs_hci_lock();
+
+    rc = ble_hs_hci_cmd_send_buf(cmd);
+    if (rc != 0) {
+        goto done;
+    }
+
+    rc = ble_hs_hci_wait_for_ack();
+    if (rc != 0) {
+        ble_hs_sched_reset(rc);
+        goto done;
+    }
+
+    rc = ble_hs_hci_process_ack(opcode, evt_buf, evt_buf_len, &ack);
+    if (rc != 0) {
+        ble_hs_sched_reset(rc);
+        goto done;
+    }
+
+    if (out_evt_buf_len != NULL) {
+        *out_evt_buf_len = ack.bha_params_len;
+    }
+
+    rc = ack.bha_status;
+
+done:
+    if (ble_hs_hci_ack != NULL) {
+        ble_hci_trans_buf_free(ble_hs_hci_ack);
+        ble_hs_hci_ack = NULL;
+    }
+
+    ble_hs_hci_unlock();
+    return rc;
+}
+
+int
+ble_hs_hci_cmd_tx_empty_ack(void *cmd)
+{
+    int rc;
+
+    rc = ble_hs_hci_cmd_tx(cmd, NULL, 0, NULL);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+void
+ble_hs_hci_rx_ack(uint8_t *ack_ev)
+{
+    if (ble_hs_hci_sem.sem_tokens != 0) {
+        /* This ack is unexpected; ignore it. */
+        ble_hci_trans_buf_free(ack_ev);
+        return;
+    }
+    BLE_HS_DBG_ASSERT(ble_hs_hci_ack == NULL);
+
+    /* Unblock the application now that the HCI command buffer is populated
+     * with the acknowledgement.
+     */
+    ble_hs_hci_ack = ack_ev;
+    os_sem_release(&ble_hs_hci_sem);
+}
+
+int
+ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg)
+{
+    int enqueue;
+
+    BLE_HS_DBG_ASSERT(hci_ev != NULL);
+
+    switch (hci_ev[0]) {
+    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
+    case BLE_HCI_EVCODE_COMMAND_STATUS:
+        if (hci_ev[3] == 0 && hci_ev[4] == 0) {
+            enqueue = 1;
+        } else {
+            ble_hs_hci_rx_ack(hci_ev);
+            enqueue = 0;
+        }
+        break;
+
+    default:
+        enqueue = 1;
+        break;
+    }
+
+    if (enqueue) {
+        ble_hs_enqueue_hci_event(hci_ev);
+    }
+
+    return 0;
+}
+
+
+/**
+ * Splits an appropriately-sized fragment from the front of an outgoing ACL
+ * data packet, if necessary.  If the packet size is within the controller's
+ * buffer size requirements, no splitting is performed.  The fragment data is
+ * removed from the data packet mbuf.
+ *
+ * @param om                    The ACL data packet.
+ * @param out_frag              On success, this points to the fragment to
+ *                                  send.  If the entire packet can fit within
+ *                                  a single fragment, this will point to the
+ *                                  ACL data packet itself ('om').
+ *
+ * @return                      BLE_HS_EDONE: success; this is the final
+ *                                  fragment.
+ *                              BLE_HS_EAGAIN: success; more data remains in
+ *                                  the original mbuf.
+ *                              Other BLE host core return code on error.
+ */
+static int
+ble_hs_hci_split_frag(struct os_mbuf **om, struct os_mbuf **out_frag)
+{
+    struct os_mbuf *frag;
+    int rc;
+
+    if (OS_MBUF_PKTLEN(*om) <= ble_hs_hci_buf_sz) {
+        /* Final fragment. */
+        *out_frag = *om;
+        *om = NULL;
+        return BLE_HS_EDONE;
+    }
+
+    frag = ble_hs_mbuf_acm_pkt();
+    if (frag == NULL) {
+        rc = BLE_HS_ENOMEM;
+        goto err;
+    }
+
+    /* Move data from the front of the packet into the fragment mbuf. */
+    rc = os_mbuf_appendfrom(frag, *om, 0, ble_hs_hci_buf_sz);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto err;
+    }
+    os_mbuf_adj(*om, ble_hs_hci_buf_sz);
+
+    /* More fragments to follow. */
+    *out_frag = frag;
+    return BLE_HS_EAGAIN;
+
+err:
+    os_mbuf_free_chain(frag);
+    return rc;
+}
+
+static struct os_mbuf *
+ble_hs_hci_acl_hdr_prepend(struct os_mbuf *om, uint16_t handle,
+                           uint8_t pb_flag)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om2;
+
+    hci_hdr.hdh_handle_pb_bc =
+        ble_hs_hci_util_handle_pb_bc_join(handle, pb_flag, 0);
+    htole16(&hci_hdr.hdh_len, OS_MBUF_PKTHDR(om)->omp_len);
+
+    om2 = os_mbuf_prepend(om, sizeof hci_hdr);
+    if (om2 == NULL) {
+        return NULL;
+    }
+
+    om = om2;
+    om = os_mbuf_pullup(om, sizeof hci_hdr);
+    if (om == NULL) {
+        return NULL;
+    }
+
+    memcpy(om->om_data, &hci_hdr, sizeof hci_hdr);
+
+    BLE_HS_LOG(DEBUG, "host tx hci data; handle=%d length=%d\n", handle,
+               le16toh(&hci_hdr.hdh_len));
+
+    return om;
+}
+
+/**
+ * Transmits an HCI ACL data packet.  This function consumes the supplied mbuf,
+ * regardless of the outcome.
+ *
+ * XXX: Ensure the controller has sufficient buffer capacity for the outgoing
+ * fragments.
+ */
+int
+ble_hs_hci_acl_tx(struct ble_hs_conn *connection, struct os_mbuf *txom)
+{
+    struct os_mbuf *frag;
+    uint8_t pb;
+    int done;
+    int rc;
+
+    /* The first fragment uses the first-non-flush packet boundary value.
+     * After sending the first fragment, pb gets set appropriately for all
+     * subsequent fragments in this packet.
+     */
+    pb = BLE_HCI_PB_FIRST_NON_FLUSH;
+
+    /* Send fragments until the entire packet has been sent. */
+    done = 0;
+    while (!done) {
+        rc = ble_hs_hci_split_frag(&txom, &frag);
+        switch (rc) {
+        case BLE_HS_EDONE:
+            /* This is the final fragment. */
+            done = 1;
+            break;
+
+        case BLE_HS_EAGAIN:
+            /* More fragments to follow. */
+            break;
+
+        default:
+            goto err;
+        }
+
+        frag = ble_hs_hci_acl_hdr_prepend(frag, connection->bhc_handle, pb);
+        if (frag == NULL) {
+            rc = BLE_HS_ENOMEM;
+            goto err;
+        }
+        pb = BLE_HCI_PB_MIDDLE;
+
+        BLE_HS_LOG(DEBUG, "ble_hs_hci_acl_tx(): ");
+        ble_hs_log_mbuf(frag);
+        BLE_HS_LOG(DEBUG, "\n");
+
+        /* XXX: Try to pullup the entire fragment.  The controller currently
+         * requires the entire fragment to fit in a single buffer.  When this
+         * restriction is removed from the controller, this operation can be
+         * removed.
+         */
+        frag = os_mbuf_pullup(frag, OS_MBUF_PKTLEN(frag));
+        if (frag == NULL) {
+            rc = BLE_HS_ENOMEM;
+            goto err;
+        }
+
+        rc = ble_hs_tx_data(frag);
+        if (rc != 0) {
+            goto err;
+        }
+
+        connection->bhc_outstanding_pkts++;
+    }
+
+    return 0;
+
+err:
+    BLE_HS_DBG_ASSERT(rc != 0);
+
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_hs_hci_init(void)
+{
+    int rc;
+
+    rc = os_sem_init(&ble_hs_hci_sem, 0);
+    BLE_HS_DBG_ASSERT_EVAL(rc == 0);
+
+    rc = os_mutex_init(&ble_hs_hci_mutex);
+    BLE_HS_DBG_ASSERT_EVAL(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_cmd.c b/net/nimble/host/src/ble_hs_hci_cmd.c
new file mode 100644
index 0000000..82b442d
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_hci_cmd.c
@@ -0,0 +1,1375 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdint.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "os/os.h"
+#include "console/console.h"
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "ble_hs_dbg_priv.h"
+#include "ble_hs_priv.h"
+
+static int
+ble_hs_hci_cmd_transport(uint8_t *cmdbuf)
+{
+    int rc;
+
+    rc = ble_hci_trans_hs_cmd_tx(cmdbuf);
+    switch (rc) {
+    case 0:
+        return 0;
+
+    case BLE_ERR_MEM_CAPACITY:
+        return BLE_HS_ENOMEM_EVT;
+
+    default:
+        return BLE_HS_EUNKNOWN;
+    }
+}
+
+void
+ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
+{
+    uint16_t opcode;
+    uint8_t *u8ptr;
+
+    u8ptr = buf;
+
+    opcode = (ogf << 10) | ocf;
+    htole16(u8ptr, opcode);
+    u8ptr[2] = len;
+}
+
+int
+ble_hs_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
+{
+    uint8_t *buf;
+    int rc;
+
+    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+    BLE_HS_DBG_ASSERT(buf != NULL);
+
+    htole16(buf, ogf << 10 | ocf);
+    buf[2] = len;
+    if (len != 0) {
+        memcpy(buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
+    }
+
+    BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send: ogf=0x%02x ocf=0x%02x len=%d\n",
+               ogf, ocf, len);
+    ble_hs_log_flat_buf(buf, len + BLE_HCI_CMD_HDR_LEN);
+    BLE_HS_LOG(DEBUG, "\n");
+    rc = ble_hs_hci_cmd_transport(buf);
+
+    if (rc == 0) {
+        STATS_INC(ble_hs_stats, hci_cmd);
+    } else {
+        BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send failure; rc=%d\n", rc);
+    }
+
+    return rc;
+}
+
+int
+ble_hs_hci_cmd_send_buf(void *buf)
+{
+    uint16_t opcode;
+    uint8_t *u8ptr;
+    uint8_t len;
+    int rc;
+
+    switch (ble_hs_sync_state) {
+    case BLE_HS_SYNC_STATE_BAD:
+        return BLE_HS_ENOTSYNCED;
+
+    case BLE_HS_SYNC_STATE_BRINGUP:
+        if (!ble_hs_is_parent_task()) {
+            return BLE_HS_ENOTSYNCED;
+        }
+        break;
+
+    case BLE_HS_SYNC_STATE_GOOD:
+        break;
+
+    default:
+        BLE_HS_DBG_ASSERT(0);
+        return BLE_HS_EUNKNOWN;
+    }
+
+    u8ptr = buf;
+
+    opcode = le16toh(u8ptr + 0);
+    len = u8ptr[2];
+
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF(opcode), BLE_HCI_OCF(opcode), len,
+                             u8ptr + BLE_HCI_CMD_HDR_LEN);
+    return rc;
+}
+
+
+/**
+ * Send a LE command from the host to the controller.
+ *
+ * @param ocf
+ * @param len
+ * @param cmddata
+ *
+ * @return int
+ */
+static int
+ble_hs_hci_cmd_le_send(uint16_t ocf, uint8_t len, void *cmddata)
+{
+    int rc;
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF_LE, ocf, len, cmddata);
+    return rc;
+}
+
+/**
+ * Read BD_ADDR
+ *
+ * OGF = 0x04 (Informational parameters)
+ * OCF = 0x0009
+ */
+void
+ble_hs_hci_cmd_build_read_bd_addr(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_INFO_PARAMS,
+                             BLE_HCI_OCF_IP_RD_BD_ADDR,
+                             0, dst);
+}
+
+static int
+ble_hs_hci_cmd_body_le_whitelist_chg(const uint8_t *addr, uint8_t addr_type,
+                                     uint8_t *dst)
+{
+    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = addr_type;
+    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_le_set_adv_params(const struct hci_adv_params *adv,
+                                      uint8_t *dst)
+{
+    uint16_t itvl;
+
+    BLE_HS_DBG_ASSERT(adv != NULL);
+
+    /* Make sure parameters are valid */
+    if ((adv->adv_itvl_min > adv->adv_itvl_max) ||
+        (adv->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) ||
+        (adv->peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX) ||
+        (adv->adv_filter_policy > BLE_HCI_ADV_FILT_MAX) ||
+        (adv->adv_type > BLE_HCI_ADV_TYPE_MAX) ||
+        (adv->adv_channel_map == 0) ||
+        ((adv->adv_channel_map & 0xF8) != 0)) {
+        /* These parameters are not valid */
+        return -1;
+    }
+
+    /* Make sure interval is valid for advertising type. */
+    if ((adv->adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND) ||
+        (adv->adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND)) {
+        itvl = BLE_HCI_ADV_ITVL_NONCONN_MIN;
+    } else {
+        itvl = BLE_HCI_ADV_ITVL_MIN;
+    }
+
+    /* Do not check if high duty-cycle directed */
+    if (adv->adv_type != BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD) {
+        if ((adv->adv_itvl_min < itvl) ||
+            (adv->adv_itvl_min > BLE_HCI_ADV_ITVL_MAX)) {
+            return -1;
+        }
+    }
+
+    htole16(dst, adv->adv_itvl_min);
+    htole16(dst + 2, adv->adv_itvl_max);
+    dst[4] = adv->adv_type;
+    dst[5] = adv->own_addr_type;
+    dst[6] = adv->peer_addr_type;
+    memcpy(dst + 7, adv->peer_addr, BLE_DEV_ADDR_LEN);
+    dst[13] = adv->adv_channel_map;
+    dst[14] = adv->adv_filter_policy;
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_le_set_adv_params(const struct hci_adv_params *adv,
+                                       uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_PARAM_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_PARAMS,
+                       BLE_HCI_SET_ADV_PARAM_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_set_adv_params(adv, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+/**
+ * Set advertising data
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x0008
+ *
+ * @param data
+ * @param len
+ * @param dst
+ *
+ * @return int
+ */
+static int
+ble_hs_hci_cmd_body_le_set_adv_data(const uint8_t *data, uint8_t len,
+                                    uint8_t *dst)
+{
+    /* Check for valid parameters */
+    if (((data == NULL) && (len != 0)) || (len > BLE_HCI_MAX_ADV_DATA_LEN)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    memset(dst, 0, BLE_HCI_SET_ADV_DATA_LEN);
+    dst[0] = len;
+    memcpy(dst + 1, data, len);
+
+    return 0;
+}
+
+/**
+ * Set advertising data
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x0008
+ *
+ * @param data
+ * @param len
+ * @param dst
+ *
+ * @return int
+ */
+int
+ble_hs_hci_cmd_build_le_set_adv_data(const uint8_t *data, uint8_t len,
+                                     uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_DATA_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_DATA,
+                       BLE_HCI_SET_ADV_DATA_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_set_adv_data(data, len, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
+                                         uint8_t *dst)
+{
+    /* Check for valid parameters */
+    if (((data == NULL) && (len != 0)) ||
+         (len > BLE_HCI_MAX_SCAN_RSP_DATA_LEN)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    memset(dst, 0, BLE_HCI_SET_SCAN_RSP_DATA_LEN);
+    dst[0] = len;
+    memcpy(dst + 1, data, len);
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
+                                          uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_RSP_DATA_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA,
+                       BLE_HCI_SET_SCAN_RSP_DATA_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_set_scan_rsp_data(data, len, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static void
+ble_hs_hci_cmd_body_set_event_mask(uint64_t event_mask, uint8_t *dst)
+{
+    htole64(dst, event_mask);
+}
+
+void
+ble_hs_hci_cmd_build_set_event_mask(uint64_t event_mask,
+                                    uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_EVENT_MASK_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND,
+                       BLE_HCI_OCF_CB_SET_EVENT_MASK,
+                       BLE_HCI_SET_EVENT_MASK_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_set_event_mask(event_mask, dst);
+}
+
+void
+ble_hs_hci_cmd_build_set_event_mask2(uint64_t event_mask,
+                                     uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_EVENT_MASK_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND,
+                       BLE_HCI_OCF_CB_SET_EVENT_MASK2,
+                       BLE_HCI_SET_EVENT_MASK_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_set_event_mask(event_mask, dst);
+}
+
+static void
+ble_hs_hci_cmd_body_disconnect(uint16_t handle, uint8_t reason, uint8_t *dst)
+{
+    htole16(dst + 0, handle);
+    dst[2] = reason;
+}
+
+void
+ble_hs_hci_cmd_build_disconnect(uint16_t handle, uint8_t reason,
+                                uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_DISCONNECT_CMD_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LINK_CTRL, BLE_HCI_OCF_DISCONNECT_CMD,
+                       BLE_HCI_DISCONNECT_CMD_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_disconnect(handle, reason, dst);
+}
+
+int
+ble_hs_hci_cmd_disconnect(uint16_t handle, uint8_t reason)
+{
+    uint8_t cmd[BLE_HCI_DISCONNECT_CMD_LEN];
+    int rc;
+
+    ble_hs_hci_cmd_body_disconnect(handle, reason, cmd);
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF_LINK_CTRL,
+                           BLE_HCI_OCF_DISCONNECT_CMD,
+                           BLE_HCI_DISCONNECT_CMD_LEN,
+                           cmd);
+    return rc;
+}
+
+static void
+ble_hs_hci_cmd_body_le_set_event_mask(uint64_t event_mask, uint8_t *dst)
+{
+    htole64(dst, event_mask);
+}
+
+void
+ble_hs_hci_cmd_build_le_set_event_mask(uint64_t event_mask,
+                                       uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_LE_EVENT_MASK_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE,
+                       BLE_HCI_OCF_LE_SET_EVENT_MASK,
+                       BLE_HCI_SET_LE_EVENT_MASK_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_set_event_mask(event_mask, dst);
+}
+
+/**
+ * LE Read buffer size
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x0002
+ *
+ * @return int
+ */
+void
+ble_hs_hci_cmd_build_le_read_buffer_size(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_BUF_SIZE,
+                             0, dst);
+}
+
+/**
+ * LE Read buffer size
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x0002
+ *
+ * @return int
+ */
+int
+ble_hs_hci_cmd_le_read_buffer_size(void)
+{
+    int rc;
+
+    rc = ble_hs_hci_cmd_le_send(BLE_HCI_OCF_LE_RD_BUF_SIZE, 0, NULL);
+    return rc;
+}
+
+/**
+ * OGF=LE, OCF=0x0003
+ */
+void
+ble_hs_hci_cmd_build_le_read_loc_supp_feat(uint8_t *dst, uint8_t dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT,
+                       0, dst);
+}
+
+static void
+ble_hs_hci_cmd_body_le_set_adv_enable(uint8_t enable, uint8_t *dst)
+{
+    dst[0] = enable;
+}
+
+void
+ble_hs_hci_cmd_build_le_set_adv_enable(uint8_t enable, uint8_t *dst,
+                                       int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_ENABLE_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                       BLE_HCI_SET_ADV_ENABLE_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_set_adv_enable(enable, dst);
+}
+
+static int
+ble_hs_hci_cmd_body_le_set_scan_params(
+    uint8_t scan_type, uint16_t scan_itvl, uint16_t scan_window,
+    uint8_t own_addr_type, uint8_t filter_policy, uint8_t *dst) {
+
+    /* Make sure parameters are valid */
+    if ((scan_type != BLE_HCI_SCAN_TYPE_PASSIVE) &&
+        (scan_type != BLE_HCI_SCAN_TYPE_ACTIVE)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check interval and window */
+    if ((scan_itvl < BLE_HCI_SCAN_ITVL_MIN) ||
+        (scan_itvl > BLE_HCI_SCAN_ITVL_MAX) ||
+        (scan_window < BLE_HCI_SCAN_WINDOW_MIN) ||
+        (scan_window > BLE_HCI_SCAN_WINDOW_MAX) ||
+        (scan_itvl < scan_window)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check own addr type */
+    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check scanner filter policy */
+    if (filter_policy > BLE_HCI_SCAN_FILT_MAX) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = scan_type;
+    htole16(dst + 1, scan_itvl);
+    htole16(dst + 3, scan_window);
+    dst[5] = own_addr_type;
+    dst[6] = filter_policy;
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_le_set_scan_params(uint8_t scan_type,
+                                        uint16_t scan_itvl,
+                                        uint16_t scan_window,
+                                        uint8_t own_addr_type,
+                                        uint8_t filter_policy,
+                                        uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_PARAM_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
+                       BLE_HCI_SET_SCAN_PARAM_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_set_scan_params(scan_type, scan_itvl,
+                                              scan_window, own_addr_type,
+                                              filter_policy, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static void
+ble_hs_hci_cmd_body_le_set_scan_enable(uint8_t enable, uint8_t filter_dups,
+                                       uint8_t *dst)
+{
+    dst[0] = enable;
+    dst[1] = filter_dups;
+}
+
+void
+ble_hs_hci_cmd_build_le_set_scan_enable(uint8_t enable, uint8_t filter_dups,
+                                        uint8_t *dst, uint8_t dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_ENABLE_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
+                       BLE_HCI_SET_SCAN_ENABLE_LEN, dst);
+
+    ble_hs_hci_cmd_body_le_set_scan_enable(enable, filter_dups,
+                                         dst + BLE_HCI_CMD_HDR_LEN);
+}
+
+static int
+ble_hs_hci_cmd_body_le_create_connection(const struct hci_create_conn *hcc,
+                                         uint8_t *cmd)
+{
+    /* Check scan interval and scan window */
+    if ((hcc->scan_itvl < BLE_HCI_SCAN_ITVL_MIN) ||
+        (hcc->scan_itvl > BLE_HCI_SCAN_ITVL_MAX) ||
+        (hcc->scan_window < BLE_HCI_SCAN_WINDOW_MIN) ||
+        (hcc->scan_window > BLE_HCI_SCAN_WINDOW_MAX) ||
+        (hcc->scan_itvl < hcc->scan_window)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check initiator filter policy */
+    if (hcc->filter_policy > BLE_HCI_CONN_FILT_MAX) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check peer addr type */
+    if (hcc->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_MAX) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check own addr type */
+    if (hcc->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check connection interval min */
+    if ((hcc->conn_itvl_min < BLE_HCI_CONN_ITVL_MIN) ||
+        (hcc->conn_itvl_min > BLE_HCI_CONN_ITVL_MAX)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check connection interval max */
+    if ((hcc->conn_itvl_max < BLE_HCI_CONN_ITVL_MIN) ||
+        (hcc->conn_itvl_max > BLE_HCI_CONN_ITVL_MAX) ||
+        (hcc->conn_itvl_max < hcc->conn_itvl_min)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check connection latency */
+    if ((hcc->conn_latency < BLE_HCI_CONN_LATENCY_MIN) ||
+        (hcc->conn_latency > BLE_HCI_CONN_LATENCY_MAX)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check supervision timeout */
+    if ((hcc->supervision_timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN) ||
+        (hcc->supervision_timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX)) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    /* Check connection event length */
+    if (hcc->min_ce_len > hcc->max_ce_len) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    htole16(cmd + 0, hcc->scan_itvl);
+    htole16(cmd + 2, hcc->scan_window);
+    cmd[4] = hcc->filter_policy;
+    cmd[5] = hcc->peer_addr_type;
+    memcpy(cmd + 6, hcc->peer_addr, BLE_DEV_ADDR_LEN);
+    cmd[12] = hcc->own_addr_type;
+    htole16(cmd + 13, hcc->conn_itvl_min);
+    htole16(cmd + 15, hcc->conn_itvl_max);
+    htole16(cmd + 17, hcc->conn_latency);
+    htole16(cmd + 19, hcc->supervision_timeout);
+    htole16(cmd + 21, hcc->min_ce_len);
+    htole16(cmd + 23, hcc->max_ce_len);
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_le_create_connection(const struct hci_create_conn *hcc,
+                                          uint8_t *cmd, int cmd_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        cmd_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CREATE_CONN_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN,
+                       BLE_HCI_CREATE_CONN_LEN, cmd);
+
+    rc = ble_hs_hci_cmd_body_le_create_connection(hcc,
+                                                cmd + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+void
+ble_hs_hci_cmd_build_le_clear_whitelist(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLEAR_WHITE_LIST,
+                       0, dst);
+}
+
+int
+ble_hs_hci_cmd_build_le_add_to_whitelist(const uint8_t *addr,
+                                         uint8_t addr_type,
+                                         uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CHG_WHITE_LIST_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_WHITE_LIST,
+                       BLE_HCI_CHG_WHITE_LIST_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_whitelist_chg(addr, addr_type, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+void
+ble_hs_hci_cmd_build_reset(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET,
+                       0, dst);
+}
+
+/**
+ * Reset the controller and link manager.
+ *
+ * @return int
+ */
+int
+ble_hs_hci_cmd_reset(void)
+{
+    int rc;
+
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET,
+                             0, NULL);
+    return rc;
+}
+
+void
+ble_hs_hci_cmd_build_read_adv_pwr(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR,
+                             0, dst);
+}
+
+/**
+ * Read the transmit power level used for LE advertising channel packets.
+ *
+ * @return int
+ */
+int
+ble_hs_hci_cmd_read_adv_pwr(void)
+{
+    int rc;
+
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR,
+                             0, NULL);
+    return rc;
+}
+
+void
+ble_hs_hci_cmd_build_le_create_conn_cancel(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
+                       0, dst);
+}
+
+int
+ble_hs_hci_cmd_le_create_conn_cancel(void)
+{
+    int rc;
+
+    rc = ble_hs_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
+                           0, NULL);
+    return rc;
+}
+
+static int
+ble_hs_hci_cmd_body_le_conn_update(const struct hci_conn_update *hcu,
+                                   uint8_t *dst)
+{
+    /* XXX: add parameter checking later */
+    htole16(dst + 0, hcu->handle);
+    htole16(dst + 2, hcu->conn_itvl_min);
+    htole16(dst + 4, hcu->conn_itvl_max);
+    htole16(dst + 6, hcu->conn_latency);
+    htole16(dst + 8, hcu->supervision_timeout);
+    htole16(dst + 10, hcu->min_ce_len);
+    htole16(dst + 12, hcu->max_ce_len);
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_le_conn_update(const struct hci_conn_update *hcu,
+                                    uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_UPDATE_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CONN_UPDATE,
+                       BLE_HCI_CONN_UPDATE_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_conn_update(hcu, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_le_conn_update(const struct hci_conn_update *hcu)
+{
+    uint8_t cmd[BLE_HCI_CONN_UPDATE_LEN];
+    int rc;
+
+    rc = ble_hs_hci_cmd_body_le_conn_update(hcu, cmd);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_le_send(BLE_HCI_OCF_LE_CONN_UPDATE,
+                              BLE_HCI_CONN_UPDATE_LEN, cmd);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static void
+ble_hs_hci_cmd_body_le_lt_key_req_reply(const struct hci_lt_key_req_reply *hkr,
+                                        uint8_t *dst)
+{
+    htole16(dst + 0, hkr->conn_handle);
+    memcpy(dst + 2, hkr->long_term_key, sizeof hkr->long_term_key);
+}
+
+/**
+ * Sends the long-term key (LTK) to the controller.
+ *
+ * Note: This function expects the 128-bit key to be in little-endian byte
+ * order.
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x001a
+ *
+ * @param key
+ * @param pt
+ *
+ * @return int
+ */
+void
+ble_hs_hci_cmd_build_le_lt_key_req_reply(
+    const struct hci_lt_key_req_reply *hkr, uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LT_KEY_REQ_REPLY_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY,
+                       BLE_HCI_LT_KEY_REQ_REPLY_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_lt_key_req_reply(hkr, dst);
+}
+
+void
+ble_hs_hci_cmd_build_le_lt_key_req_neg_reply(uint16_t conn_handle,
+                                             uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
+                             BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    htole16(dst + 0, conn_handle);
+}
+
+static void
+ble_hs_hci_cmd_body_le_conn_param_reply(const struct hci_conn_param_reply *hcr,
+                                        uint8_t *dst)
+{
+    htole16(dst + 0, hcr->handle);
+    htole16(dst + 2, hcr->conn_itvl_min);
+    htole16(dst + 4, hcr->conn_itvl_max);
+    htole16(dst + 6, hcr->conn_latency);
+    htole16(dst + 8, hcr->supervision_timeout);
+    htole16(dst + 10, hcr->min_ce_len);
+    htole16(dst + 12, hcr->max_ce_len);
+}
+
+void
+ble_hs_hci_cmd_build_le_conn_param_reply(
+    const struct hci_conn_param_reply *hcr, uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_PARAM_REPLY_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
+                       BLE_HCI_CONN_PARAM_REPLY_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_conn_param_reply(hcr, dst);
+}
+
+int
+ble_hs_hci_cmd_le_conn_param_reply(const struct hci_conn_param_reply *hcr)
+{
+    uint8_t cmd[BLE_HCI_CONN_PARAM_REPLY_LEN];
+    int rc;
+
+    htole16(cmd + 0, hcr->handle);
+    htole16(cmd + 2, hcr->conn_itvl_min);
+    htole16(cmd + 4, hcr->conn_itvl_max);
+    htole16(cmd + 6, hcr->conn_latency);
+    htole16(cmd + 8, hcr->supervision_timeout);
+    htole16(cmd + 10, hcr->min_ce_len);
+    htole16(cmd + 12, hcr->max_ce_len);
+
+    rc = ble_hs_hci_cmd_le_send(BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
+                              BLE_HCI_CONN_PARAM_REPLY_LEN, cmd);
+    return rc;
+}
+
+static void
+ble_hs_hci_cmd_body_le_conn_param_neg_reply(
+    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst)
+{
+    htole16(dst + 0, hcn->handle);
+    dst[2] = hcn->reason;
+}
+
+
+void
+ble_hs_hci_cmd_build_le_conn_param_neg_reply(
+    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_PARAM_NEG_REPLY_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
+                       BLE_HCI_CONN_PARAM_NEG_REPLY_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_conn_param_neg_reply(hcn, dst);
+}
+
+int
+ble_hs_hci_cmd_le_conn_param_neg_reply(
+    const struct hci_conn_param_neg_reply *hcn)
+{
+    uint8_t cmd[BLE_HCI_CONN_PARAM_NEG_REPLY_LEN];
+    int rc;
+
+    ble_hs_hci_cmd_body_le_conn_param_neg_reply(hcn, cmd);
+
+    rc = ble_hs_hci_cmd_le_send(BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
+                              BLE_HCI_CONN_PARAM_NEG_REPLY_LEN, cmd);
+    return rc;
+}
+
+/**
+ * Get random data
+ *
+ * OGF = 0x08 (LE)
+ * OCF = 0x0018
+ *
+ * @return int
+ */
+void
+ble_hs_hci_cmd_build_le_rand(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RAND, 0, dst);
+}
+
+static void
+ble_hs_hci_cmd_body_le_start_encrypt(const struct hci_start_encrypt *cmd,
+                                     uint8_t *dst)
+{
+    htole16(dst + 0, cmd->connection_handle);
+    htole64(dst + 2, cmd->random_number);
+    htole16(dst + 10, cmd->encrypted_diversifier);
+    memcpy(dst + 12, cmd->long_term_key, sizeof cmd->long_term_key);
+}
+
+/*
+ * OGF=0x08 OCF=0x0019
+ */
+void
+ble_hs_hci_cmd_build_le_start_encrypt(const struct hci_start_encrypt *cmd,
+                                      uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_START_ENCRYPT_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_START_ENCRYPT,
+                       BLE_HCI_LE_START_ENCRYPT_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_le_start_encrypt(cmd, dst);
+}
+
+/**
+ * Read the RSSI for a given connection handle
+ *
+ * NOTE: OGF=0x05 OCF=0x0005
+ *
+ * @param handle
+ *
+ * @return int
+ */
+static void
+ble_hs_hci_cmd_body_read_rssi(uint16_t handle, uint8_t *dst)
+{
+    htole16(dst, handle);
+}
+
+void
+ble_hs_hci_cmd_build_read_rssi(uint16_t handle, uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_READ_RSSI_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_STATUS_PARAMS, BLE_HCI_OCF_RD_RSSI,
+                       BLE_HCI_READ_RSSI_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    ble_hs_hci_cmd_body_read_rssi(handle, dst);
+}
+
+static int
+ble_hs_hci_cmd_body_set_data_len(uint16_t connection_handle,
+                                 uint16_t tx_octets,
+                                 uint16_t tx_time, uint8_t *dst)
+{
+
+    if (tx_octets < BLE_HCI_SET_DATALEN_TX_OCTETS_MIN ||
+        tx_octets > BLE_HCI_SET_DATALEN_TX_OCTETS_MAX) {
+
+        return BLE_HS_EINVAL;
+    }
+
+    if (tx_time < BLE_HCI_SET_DATALEN_TX_TIME_MIN ||
+        tx_time > BLE_HCI_SET_DATALEN_TX_TIME_MAX) {
+
+        return BLE_HS_EINVAL;
+    }
+
+    htole16(dst + 0, connection_handle);
+    htole16(dst + 2, tx_octets);
+    htole16(dst + 4, tx_time);
+
+    return 0;
+}
+
+/*
+ * OGF=0x08 OCF=0x0022
+ */
+int
+ble_hs_hci_cmd_build_set_data_len(uint16_t connection_handle,
+                                  uint16_t tx_octets, uint16_t tx_time,
+                                  uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DATA_LEN,
+                       BLE_HCI_SET_DATALEN_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_set_data_len(connection_handle, tx_octets,
+                                          tx_time, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+/**
+ * IRKs are in little endian.
+ */
+static int
+ble_hs_hci_cmd_body_add_to_resolv_list(uint8_t addr_type, const uint8_t *addr,
+                                       const uint8_t *peer_irk,
+                                       const uint8_t *local_irk,
+                                       uint8_t *dst)
+{
+    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = addr_type;
+    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
+    memcpy(dst + 1 + 6, peer_irk , 16);
+    memcpy(dst + 1 + 6 + 16, local_irk , 16);
+    /* 16 + 16 + 6 + 1 == 39 */
+    return 0;
+}
+
+/**
+ * OGF=0x08 OCF=0x0027
+ *
+ * IRKs are in little endian.
+ */
+int
+ble_hs_hci_cmd_build_add_to_resolv_list(
+    const struct hci_add_dev_to_resolving_list *padd,
+    uint8_t *dst,
+    int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_ADD_TO_RESOLV_LIST_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
+                       BLE_HCI_ADD_TO_RESOLV_LIST_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_add_to_resolv_list(
+        padd->addr_type, padd->addr, padd->peer_irk, padd->local_irk,
+        dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_remove_from_resolv_list(uint8_t addr_type,
+                                            const uint8_t *addr,
+                                            uint8_t *dst)
+{
+    if (addr_type > BLE_ADDR_TYPE_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = addr_type;
+    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
+    return 0;
+}
+
+
+int
+ble_hs_hci_cmd_build_remove_from_resolv_list(uint8_t addr_type,
+                                             const uint8_t *addr,
+                                             uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RMV_FROM_RESOLV_LIST_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RMV_RESOLV_LIST,
+                       BLE_HCI_RMV_FROM_RESOLV_LIST_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_remove_from_resolv_list(addr_type, addr,
+                                                dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_clear_resolv_list(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLR_RESOLV_LIST,
+                       0, dst);
+
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_read_resolv_list_size(uint8_t *dst, int dst_len)
+{
+    BLE_HS_DBG_ASSERT(dst_len >= BLE_HCI_CMD_HDR_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_RD_RESOLV_LIST_SIZE,
+                             0, dst);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_read_peer_resolv_addr(uint8_t peer_identity_addr_type,
+                                          const uint8_t *peer_identity_addr,
+                                          uint8_t *dst)
+{
+    if (peer_identity_addr_type > BLE_ADDR_TYPE_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = peer_identity_addr_type;
+    memcpy(dst + 1, peer_identity_addr, BLE_DEV_ADDR_LEN);
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_read_peer_resolv_addr(uint8_t peer_identity_addr_type,
+                                           const uint8_t *peer_identity_addr,
+                                           uint8_t *dst,
+                                           int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RD_PEER_RESOLV_ADDR_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_RD_PEER_RESOLV_ADDR,
+                             BLE_HCI_RD_PEER_RESOLV_ADDR_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_read_peer_resolv_addr(peer_identity_addr_type,
+                                                   peer_identity_addr,
+                                                   dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_read_lcl_resolv_addr(
+    uint8_t local_identity_addr_type,
+    const uint8_t *local_identity_addr,
+    uint8_t *dst)
+{
+    if (local_identity_addr_type > BLE_ADDR_TYPE_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = local_identity_addr_type;
+    memcpy(dst + 1, local_identity_addr, BLE_DEV_ADDR_LEN);
+    return 0;
+}
+
+/*
+ * OGF=0x08 OCF=0x002c
+ */
+int
+ble_hs_hci_cmd_build_read_lcl_resolv_addr(uint8_t local_identity_addr_type,
+                                          const uint8_t *local_identity_addr,
+                                          uint8_t *dst,
+                                          int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_RD_LOC_RESOLV_ADDR_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE,
+                             BLE_HCI_OCF_LE_RD_LOCAL_RESOLV_ADDR,
+                             BLE_HCI_RD_LOC_RESOLV_ADDR_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_read_lcl_resolv_addr(local_identity_addr_type,
+                                                  local_identity_addr,
+                                                  dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_set_addr_res_en(uint8_t enable, uint8_t *dst)
+{
+    if (enable > 1) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = enable;
+    return 0;
+}
+
+/*
+ * OGF=0x08 OCF=0x002d
+ */
+int
+ble_hs_hci_cmd_build_set_addr_res_en(uint8_t enable, uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADDR_RESOL_ENA_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN,
+                       BLE_HCI_SET_ADDR_RESOL_ENA_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_set_addr_res_en(enable,
+                                             dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_set_resolv_priv_addr_timeout(uint16_t timeout,
+                                                 uint8_t *dst)
+{
+    if (timeout == 0 || timeout > 0xA1B8) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    htole16(dst, timeout);
+    return 0;
+}
+
+/*
+ * OGF=0x08 OCF=0x002e
+ */
+int
+ble_hs_hci_cmd_build_set_resolv_priv_addr_timeout(
+    uint16_t timeout, uint8_t *dst, int dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RPA_TMO,
+                       BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_set_resolv_priv_addr_timeout(
+        timeout, dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+    return 0;
+}
+
+static int
+ble_hs_hci_cmd_body_set_random_addr(const struct hci_rand_addr *paddr,
+                                    uint8_t *dst)
+{
+    memcpy(dst, paddr->addr, BLE_DEV_ADDR_LEN);
+    return 0;
+}
+
+int
+ble_hs_hci_cmd_build_set_random_addr(const uint8_t *addr,
+                                     uint8_t *dst, int dst_len)
+{
+    struct hci_rand_addr r_addr;
+    int rc;
+
+    memcpy(r_addr.addr, addr, sizeof(r_addr.addr));
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RAND_ADDR,
+                       BLE_HCI_SET_RAND_ADDR_LEN, dst);
+
+    rc = ble_hs_hci_cmd_body_set_random_addr(&r_addr,
+                                             dst + BLE_HCI_CMD_HDR_LEN);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_hci_evt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_evt.c b/net/nimble/host/src/ble_hs_hci_evt.c
new file mode 100644
index 0000000..3d382dc
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_hci_evt.c
@@ -0,0 +1,677 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "os/os.h"
+#include "console/console.h"
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "host/ble_gap.h"
+#include "ble_hs_priv.h"
+#include "ble_hs_dbg_priv.h"
+
+_Static_assert(sizeof (struct hci_data_hdr) == BLE_HCI_DATA_HDR_SZ,
+               "struct hci_data_hdr must be 4 bytes");
+
+typedef int ble_hs_hci_evt_fn(uint8_t event_code, uint8_t *data, int len);
+static ble_hs_hci_evt_fn ble_hs_hci_evt_disconn_complete;
+static ble_hs_hci_evt_fn ble_hs_hci_evt_encrypt_change;
+static ble_hs_hci_evt_fn ble_hs_hci_evt_hw_error;
+static ble_hs_hci_evt_fn ble_hs_hci_evt_num_completed_pkts;
+static ble_hs_hci_evt_fn ble_hs_hci_evt_enc_key_refresh;
+static ble_hs_hci_evt_fn ble_hs_hci_evt_le_meta;
+
+typedef int ble_hs_hci_evt_le_fn(uint8_t subevent, uint8_t *data, int len);
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_conn_complete;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_adv_rpt;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_conn_upd_complete;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_lt_key_req;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_conn_parm_req;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_dir_adv_rpt;
+
+/* Statistics */
+struct host_hci_stats
+{
+    uint32_t events_rxd;
+    uint32_t good_acks_rxd;
+    uint32_t bad_acks_rxd;
+    uint32_t unknown_events_rxd;
+};
+
+#define BLE_HS_HCI_EVT_TIMEOUT        50      /* Milliseconds. */
+
+/** Dispatch table for incoming HCI events.  Sorted by event code field. */
+struct ble_hs_hci_evt_dispatch_entry {
+    uint8_t event_code;
+    ble_hs_hci_evt_fn *cb;
+};
+
+static const struct ble_hs_hci_evt_dispatch_entry ble_hs_hci_evt_dispatch[] = {
+    { BLE_HCI_EVCODE_DISCONN_CMP, ble_hs_hci_evt_disconn_complete },
+    { BLE_HCI_EVCODE_ENCRYPT_CHG, ble_hs_hci_evt_encrypt_change },
+    { BLE_HCI_EVCODE_HW_ERROR, ble_hs_hci_evt_hw_error },
+    { BLE_HCI_EVCODE_NUM_COMP_PKTS, ble_hs_hci_evt_num_completed_pkts },
+    { BLE_HCI_EVCODE_ENC_KEY_REFRESH, ble_hs_hci_evt_enc_key_refresh },
+    { BLE_HCI_EVCODE_LE_META, ble_hs_hci_evt_le_meta },
+};
+
+#define BLE_HS_HCI_EVT_DISPATCH_SZ \
+    (sizeof ble_hs_hci_evt_dispatch / sizeof ble_hs_hci_evt_dispatch[0])
+
+/** Dispatch table for incoming LE meta events.  Sorted by subevent field. */
+struct ble_hs_hci_evt_le_dispatch_entry {
+    uint8_t subevent;
+    ble_hs_hci_evt_le_fn *cb;
+};
+
+static const struct ble_hs_hci_evt_le_dispatch_entry
+        ble_hs_hci_evt_le_dispatch[] = {
+    { BLE_HCI_LE_SUBEV_CONN_COMPLETE, ble_hs_hci_evt_le_conn_complete },
+    { BLE_HCI_LE_SUBEV_ADV_RPT, ble_hs_hci_evt_le_adv_rpt },
+    { BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE,
+          ble_hs_hci_evt_le_conn_upd_complete },
+    { BLE_HCI_LE_SUBEV_LT_KEY_REQ, ble_hs_hci_evt_le_lt_key_req },
+    { BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ, ble_hs_hci_evt_le_conn_parm_req },
+    { BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE, ble_hs_hci_evt_le_conn_complete },
+    { BLE_HCI_LE_SUBEV_DIRECT_ADV_RPT, ble_hs_hci_evt_le_dir_adv_rpt },
+};
+
+#define BLE_HS_HCI_EVT_LE_DISPATCH_SZ \
+    (sizeof ble_hs_hci_evt_le_dispatch / sizeof ble_hs_hci_evt_le_dispatch[0])
+
+static const struct ble_hs_hci_evt_dispatch_entry *
+ble_hs_hci_evt_dispatch_find(uint8_t event_code)
+{
+    const struct ble_hs_hci_evt_dispatch_entry *entry;
+    int i;
+
+    for (i = 0; i < BLE_HS_HCI_EVT_DISPATCH_SZ; i++) {
+        entry = ble_hs_hci_evt_dispatch + i;
+        if (entry->event_code == event_code) {
+            return entry;
+        }
+    }
+
+    return NULL;
+}
+
+static const struct ble_hs_hci_evt_le_dispatch_entry *
+ble_hs_hci_evt_le_dispatch_find(uint8_t event_code)
+{
+    const struct ble_hs_hci_evt_le_dispatch_entry *entry;
+    int i;
+
+    for (i = 0; i < BLE_HS_HCI_EVT_LE_DISPATCH_SZ; i++) {
+        entry = ble_hs_hci_evt_le_dispatch + i;
+        if (entry->subevent == event_code) {
+            return entry;
+        }
+    }
+
+    return NULL;
+}
+
+static int
+ble_hs_hci_evt_disconn_complete(uint8_t event_code, uint8_t *data, int len)
+{
+    struct hci_disconn_complete evt;
+
+    if (len < BLE_HCI_EVENT_DISCONN_COMPLETE_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.status = data[2];
+    evt.connection_handle = le16toh(data + 3);
+    evt.reason = data[5];
+
+    ble_gap_rx_disconn_complete(&evt);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_encrypt_change(uint8_t event_code, uint8_t *data, int len)
+{
+    struct hci_encrypt_change evt;
+
+    if (len < BLE_HCI_EVENT_ENCRYPT_CHG_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.status = data[2];
+    evt.connection_handle = le16toh(data + 3);
+    evt.encryption_enabled = data[5];
+
+    ble_sm_enc_change_rx(&evt);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_hw_error(uint8_t event_code, uint8_t *data, int len)
+{
+    uint8_t hw_code;
+
+    if (len < BLE_HCI_EVENT_HW_ERROR_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    hw_code = data[0];
+    ble_hs_hw_error(hw_code);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_enc_key_refresh(uint8_t event_code, uint8_t *data, int len)
+{
+    struct hci_encrypt_key_refresh evt;
+
+    if (len < BLE_HCI_EVENT_ENC_KEY_REFRESH_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.status = data[2];
+    evt.connection_handle = le16toh(data + 3);
+
+    ble_sm_enc_key_refresh_rx(&evt);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_num_completed_pkts(uint8_t event_code, uint8_t *data, int len)
+{
+    uint16_t num_pkts;
+    uint16_t handle;
+    uint8_t num_handles;
+    int off;
+    int i;
+
+    if (len < BLE_HCI_EVENT_HDR_LEN + BLE_HCI_EVENT_NUM_COMP_PKTS_HDR_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    off = BLE_HCI_EVENT_HDR_LEN;
+    num_handles = data[off];
+    if (len < BLE_HCI_EVENT_NUM_COMP_PKTS_HDR_LEN +
+              num_handles * BLE_HCI_EVENT_NUM_COMP_PKTS_ENT_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+    off++;
+
+    for (i = 0; i < num_handles; i++) {
+        handle = le16toh(data + off + 2 * i);
+        num_pkts = le16toh(data + off + 2 * num_handles + 2 * i);
+
+        /* XXX: Do something with these values. */
+        (void)handle;
+        (void)num_pkts;
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_meta(uint8_t event_code, uint8_t *data, int len)
+{
+    const struct ble_hs_hci_evt_le_dispatch_entry *entry;
+    uint8_t subevent;
+    int rc;
+
+    if (len < BLE_HCI_EVENT_HDR_LEN + BLE_HCI_LE_MIN_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    subevent = data[2];
+    entry = ble_hs_hci_evt_le_dispatch_find(subevent);
+    if (entry != NULL) {
+        rc = entry->cb(subevent, data + BLE_HCI_EVENT_HDR_LEN,
+                           len - BLE_HCI_EVENT_HDR_LEN);
+        if (rc != 0) {
+            return rc;
+        }
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_conn_complete(uint8_t subevent, uint8_t *data, int len)
+{
+    struct hci_le_conn_complete evt;
+    int extended_offset = 0;
+    int rc;
+
+    if (len < BLE_HCI_LE_CONN_COMPLETE_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    /* this code processes two different events that are really similar */
+    if ((subevent == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) &&
+        ( len < BLE_HCI_LE_ENH_CONN_COMPLETE_LEN)) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.subevent_code = data[0];
+    evt.status = data[1];
+    evt.connection_handle = le16toh(data + 2);
+    evt.role = data[4];
+    evt.peer_addr_type = data[5];
+    memcpy(evt.peer_addr, data + 6, BLE_DEV_ADDR_LEN);
+
+    /* enhanced connection event has the same information with these
+     * extra fields stuffed into the middle */
+    if (subevent == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) {
+        memcpy(evt.local_rpa, data + 12, BLE_DEV_ADDR_LEN);
+        memcpy(evt.peer_rpa, data + 18, BLE_DEV_ADDR_LEN);
+        extended_offset = 12;
+    } else {
+        memset(evt.local_rpa, 0, BLE_DEV_ADDR_LEN);
+        memset(evt.peer_rpa, 0, BLE_DEV_ADDR_LEN);
+    }
+
+    evt.conn_itvl = le16toh(data + 12 + extended_offset);
+    evt.conn_latency = le16toh(data + 14 + extended_offset);
+    evt.supervision_timeout = le16toh(data + 16 + extended_offset);
+    evt.master_clk_acc = data[18 + extended_offset];
+
+    if (evt.status == 0) {
+        if (evt.role != BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER &&
+            evt.role != BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE) {
+
+            return BLE_HS_EBADDATA;
+        }
+    }
+
+    rc = ble_gap_rx_conn_complete(&evt);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_adv_rpt_first_pass(uint8_t *data, int len,
+                                     uint8_t *out_num_reports,
+                                     int *out_rssi_off)
+{
+    uint8_t num_reports;
+    int data_len;
+    int off;
+    int i;
+
+    if (len < BLE_HCI_LE_ADV_RPT_MIN_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    num_reports = data[1];
+    if (num_reports < BLE_HCI_LE_ADV_RPT_NUM_RPTS_MIN ||
+        num_reports > BLE_HCI_LE_ADV_RPT_NUM_RPTS_MAX) {
+
+        return BLE_HS_EBADDATA;
+    }
+
+    off = 2 +       /* Subevent code and num reports. */
+          (1 +      /* Event type. */
+           1 +      /* Address type. */
+           6        /* Address. */
+          ) * num_reports;
+    if (off + num_reports >= len) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    data_len = 0;
+    for (i = 0; i < num_reports; i++) {
+        data_len += data[off];
+        off++;
+    }
+
+    off += data_len;
+
+    /* Check if RSSI fields fit in the packet. */
+    if (off + num_reports > len) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    *out_num_reports = num_reports;
+    *out_rssi_off = off;
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_adv_rpt(uint8_t subevent, uint8_t *data, int len)
+{
+    struct ble_gap_disc_desc desc;
+    uint8_t num_reports;
+    int rssi_off;
+    int data_off;
+    int suboff;
+    int off;
+    int rc;
+    int i;
+
+    rc = ble_hs_hci_evt_le_adv_rpt_first_pass(data, len, &num_reports,
+                                             &rssi_off);
+    if (rc != 0) {
+        return rc;
+    }
+
+    /* Direct address fields not present in a standard advertising report. */
+    desc.direct_addr_type = BLE_GAP_ADDR_TYPE_NONE;
+    memset(desc.direct_addr, 0, sizeof desc.direct_addr);
+
+    data_off = 0;
+    for (i = 0; i < num_reports; i++) {
+        suboff = 0;
+
+        off = 2 + suboff * num_reports + i;
+        desc.event_type = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + i;
+        desc.addr_type = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + i * 6;
+        memcpy(desc.addr, data + off, 6);
+        suboff += 6;
+
+        off = 2 + suboff * num_reports + i;
+        desc.length_data = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + data_off;
+        desc.data = data + off;
+        data_off += desc.length_data;
+
+        off = rssi_off + 1 * i;
+        desc.rssi = data[off];
+
+        ble_gap_rx_adv_report(&desc);
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_dir_adv_rpt(uint8_t subevent, uint8_t *data, int len)
+{
+    struct ble_gap_disc_desc desc;
+    uint8_t num_reports;
+    int suboff;
+    int off;
+    int i;
+
+    if (len < BLE_HCI_LE_ADV_DIRECT_RPT_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    num_reports = data[1];
+    if (len != 2 + num_reports * BLE_HCI_LE_ADV_DIRECT_RPT_SUB_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    /* Data fields not present in a direct advertising report. */
+    desc.data = NULL;
+    desc.fields = NULL;
+
+    for (i = 0; i < num_reports; i++) {
+        suboff = 0;
+
+        off = 2 + suboff * num_reports + i;
+        desc.event_type = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + i;
+        desc.addr_type = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + i * 6;
+        memcpy(desc.addr, data + off, 6);
+        suboff += 6;
+
+        off = 2 + suboff * num_reports + i;
+        desc.direct_addr_type = data[off];
+        suboff++;
+
+        off = 2 + suboff * num_reports + i * 6;
+        memcpy(desc.direct_addr, data + off, 6);
+        suboff += 6;
+
+        off = 2 + suboff * num_reports + i;
+        desc.rssi = data[off];
+        suboff++;
+
+        ble_gap_rx_adv_report(&desc);
+    }
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_conn_upd_complete(uint8_t subevent, uint8_t *data, int len)
+{
+    struct hci_le_conn_upd_complete evt;
+
+    if (len < BLE_HCI_LE_CONN_UPD_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.subevent_code = data[0];
+    evt.status = data[1];
+    evt.connection_handle = le16toh(data + 2);
+    evt.conn_itvl = le16toh(data + 4);
+    evt.conn_latency = le16toh(data + 6);
+    evt.supervision_timeout = le16toh(data + 8);
+
+    if (evt.status == 0) {
+        if (evt.conn_itvl < BLE_HCI_CONN_ITVL_MIN ||
+            evt.conn_itvl > BLE_HCI_CONN_ITVL_MAX) {
+
+            return BLE_HS_EBADDATA;
+        }
+        if (evt.conn_latency < BLE_HCI_CONN_LATENCY_MIN ||
+            evt.conn_latency > BLE_HCI_CONN_LATENCY_MAX) {
+
+            return BLE_HS_EBADDATA;
+        }
+        if (evt.supervision_timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN ||
+            evt.supervision_timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX) {
+
+            return BLE_HS_EBADDATA;
+        }
+    }
+
+    ble_gap_rx_update_complete(&evt);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_lt_key_req(uint8_t subevent, uint8_t *data, int len)
+{
+    struct hci_le_lt_key_req evt;
+
+    if (len < BLE_HCI_LE_LT_KEY_REQ_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.subevent_code = data[0];
+    evt.connection_handle = le16toh(data + 1);
+    evt.random_number = le64toh(data + 3);
+    evt.encrypted_diversifier = le16toh(data + 11);
+
+    ble_sm_ltk_req_rx(&evt);
+
+    return 0;
+}
+
+static int
+ble_hs_hci_evt_le_conn_parm_req(uint8_t subevent, uint8_t *data, int len)
+{
+    struct hci_le_conn_param_req evt;
+
+    if (len < BLE_HCI_LE_REM_CONN_PARM_REQ_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.subevent_code = data[0];
+    evt.connection_handle = le16toh(data + 1);
+    evt.itvl_min = le16toh(data + 3);
+    evt.itvl_max = le16toh(data + 5);
+    evt.latency = le16toh(data + 7);
+    evt.timeout = le16toh(data + 9);
+
+    if (evt.itvl_min < BLE_HCI_CONN_ITVL_MIN ||
+        evt.itvl_max > BLE_HCI_CONN_ITVL_MAX ||
+        evt.itvl_min > evt.itvl_max) {
+
+        return BLE_HS_EBADDATA;
+    }
+    if (evt.latency < BLE_HCI_CONN_LATENCY_MIN ||
+        evt.latency > BLE_HCI_CONN_LATENCY_MAX) {
+
+        return BLE_HS_EBADDATA;
+    }
+    if (evt.timeout < BLE_HCI_CONN_SPVN_TIMEOUT_MIN ||
+        evt.timeout > BLE_HCI_CONN_SPVN_TIMEOUT_MAX) {
+
+        return BLE_HS_EBADDATA;
+    }
+
+    ble_gap_rx_param_req(&evt);
+
+    return 0;
+}
+
+int
+ble_hs_hci_evt_process(uint8_t *data)
+{
+    const struct ble_hs_hci_evt_dispatch_entry *entry;
+    uint8_t event_code;
+    uint8_t param_len;
+    int event_len;
+    int rc;
+
+    /* Count events received */
+    STATS_INC(ble_hs_stats, hci_event);
+
+    /* Display to console */
+    ble_hs_dbg_event_disp(data);
+
+    /* Process the event */
+    event_code = data[0];
+    param_len = data[1];
+
+    event_len = param_len + 2;
+
+    entry = ble_hs_hci_evt_dispatch_find(event_code);
+    if (entry == NULL) {
+        STATS_INC(ble_hs_stats, hci_unknown_event);
+        rc = BLE_HS_ENOTSUP;
+    } else {
+        rc = entry->cb(event_code, data, event_len);
+    }
+
+    ble_hci_trans_buf_free(data);
+
+    return rc;
+}
+
+/**
+ * Called when a data packet is received from the controller.  This function
+ * consumes the supplied mbuf, regardless of the outcome.
+ *
+ * @param om                    The incoming data packet, beginning with the
+ *                                  HCI ACL data header.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
+int
+ble_hs_hci_evt_acl_process(struct os_mbuf *om)
+{
+    struct hci_data_hdr hci_hdr;
+    struct ble_hs_conn *conn;
+    ble_l2cap_rx_fn *rx_cb;
+    struct os_mbuf *rx_buf;
+    uint16_t handle;
+    int rc;
+
+    rc = ble_hs_hci_util_data_hdr_strip(om, &hci_hdr);
+    if (rc != 0) {
+        goto err;
+    }
+
+#if (BLETEST_THROUGHPUT_TEST == 0)
+    BLE_HS_LOG(DEBUG, "ble_hs_hci_evt_acl_process(): handle=%u pb=%x len=%u "
+                      "data=",
+               BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc), 
+               BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc), 
+               hci_hdr.hdh_len);
+    ble_hs_log_mbuf(om);
+    BLE_HS_LOG(DEBUG, "\n");
+#endif
+
+    if (hci_hdr.hdh_len != OS_MBUF_PKTHDR(om)->omp_len) {
+        rc = BLE_HS_EBADDATA;
+        goto err;
+    }
+
+    handle = BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc);
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(handle);
+    if (conn == NULL) {
+        rc = BLE_HS_ENOTCONN;
+    } else {
+        rc = ble_l2cap_rx(conn, &hci_hdr, om, &rx_cb, &rx_buf);
+        om = NULL;
+    }
+
+    ble_hs_unlock();
+
+    switch (rc) {
+    case 0:
+        /* Final fragment received. */
+        BLE_HS_DBG_ASSERT(rx_cb != NULL);
+        BLE_HS_DBG_ASSERT(rx_buf != NULL);
+        rc = rx_cb(handle, &rx_buf);
+        os_mbuf_free_chain(rx_buf);
+        break;
+
+    case BLE_HS_EAGAIN:
+        /* More fragments on the way. */
+        break;
+
+    default:
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(om);
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_priv.h b/net/nimble/host/src/ble_hs_hci_priv.h
new file mode 100644
index 0000000..7d2fb12
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_hci_priv.h
@@ -0,0 +1,158 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_HS_HCI_PRIV_
+#define H_BLE_HS_HCI_PRIV_
+
+#include "nimble/hci_common.h"
+struct ble_hs_conn;
+struct os_mbuf;
+
+struct ble_hs_hci_ack {
+    int bha_status;         /* A BLE_HS_E<...> error; NOT a naked HCI code. */
+    uint8_t *bha_params;
+    int bha_params_len;
+    uint16_t bha_opcode;
+    uint8_t bha_hci_handle;
+};
+
+int ble_hs_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_len,
+                      uint8_t *out_evt_buf_len);
+int ble_hs_hci_cmd_tx_empty_ack(void *cmd);
+void ble_hs_hci_rx_ack(uint8_t *ack_ev);
+void ble_hs_hci_init(void);
+
+#if PHONY_HCI_ACKS
+typedef int ble_hs_hci_phony_ack_fn(uint8_t *ack, int ack_buf_len);
+void ble_hs_hci_set_phony_ack_cb(ble_hs_hci_phony_ack_fn *cb);
+#endif
+
+int ble_hs_hci_util_read_adv_tx_pwr(int8_t *out_pwr);
+int ble_hs_hci_util_rand(void *dst, int len);
+int ble_hs_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi);
+int ble_hs_hci_util_set_random_addr(const uint8_t *addr);
+int ble_hs_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
+                                 uint16_t tx_time);
+int ble_hs_hci_util_data_hdr_strip(struct os_mbuf *om,
+                                   struct hci_data_hdr *out_hdr);
+
+int ble_hs_hci_evt_process(uint8_t *data);
+uint16_t ble_hs_hci_util_opcode_join(uint8_t ogf, uint16_t ocf);
+void ble_hs_hci_cmd_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len,
+                              void *buf);
+int ble_hs_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len,
+                        const void *cmddata);
+int ble_hs_hci_cmd_send_buf(void *cmddata);
+void ble_hs_hci_cmd_build_read_bd_addr(uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_set_event_mask(uint64_t event_mask,
+                                         uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_set_event_mask2(uint64_t event_mask, uint8_t *dst,
+                                          int dst_len);
+void ble_hs_hci_cmd_build_disconnect(uint16_t handle, uint8_t reason,
+                                     uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_disconnect(uint16_t handle, uint8_t reason);
+void ble_hs_hci_cmd_build_read_rssi(uint16_t handle, uint8_t *dst,
+                                    int dst_len);
+int ble_hs_hci_cmd_read_rssi(uint16_t handle);
+int ble_hs_hci_cmd_build_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
+                                              uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_le_set_adv_data(const uint8_t *data, uint8_t len,
+                                         uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_le_set_adv_params(const struct hci_adv_params *adv,
+                                           uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_set_event_mask(uint64_t event_mask,
+                                            uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_read_buffer_size(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_le_read_buffer_size(void);
+void ble_hs_hci_cmd_build_le_read_loc_supp_feat(uint8_t *dst, uint8_t dst_len);
+void ble_hs_hci_cmd_build_le_set_adv_enable(uint8_t enable, uint8_t *dst,
+                                            int dst_len);
+int ble_hs_hci_cmd_le_set_adv_enable(uint8_t enable);
+int ble_hs_hci_cmd_build_le_set_scan_params(uint8_t scan_type,
+                                            uint16_t scan_itvl,
+                                            uint16_t scan_window,
+                                            uint8_t own_addr_type,
+                                            uint8_t filter_policy,
+                                            uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_set_scan_enable(uint8_t enable,
+                                             uint8_t filter_dups,
+                                             uint8_t *dst, uint8_t dst_len);
+int ble_hs_hci_cmd_le_set_scan_enable(uint8_t enable, uint8_t filter_dups);
+int ble_hs_hci_cmd_build_le_create_connection(
+    const struct hci_create_conn *hcc, uint8_t *cmd, int cmd_len);
+int ble_hs_hci_cmd_le_create_connection(const struct hci_create_conn *hcc);
+void ble_hs_hci_cmd_build_le_clear_whitelist(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_le_add_to_whitelist(const uint8_t *addr,
+                                             uint8_t addr_type,
+                                             uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_reset(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_reset(void);
+void ble_hs_hci_cmd_build_read_adv_pwr(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_read_adv_pwr(void);
+void ble_hs_hci_cmd_build_le_create_conn_cancel(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_le_create_conn_cancel(void);
+int ble_hs_hci_cmd_build_le_conn_update(const struct hci_conn_update *hcu,
+                                        uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_le_conn_update(const struct hci_conn_update *hcu);
+void ble_hs_hci_cmd_build_le_lt_key_req_reply(
+    const struct hci_lt_key_req_reply *hkr, uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_lt_key_req_neg_reply(uint16_t conn_handle,
+                                                  uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_conn_param_reply(
+    const struct hci_conn_param_reply *hcr, uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_le_conn_param_reply(const struct hci_conn_param_reply *hcr);
+void ble_hs_hci_cmd_build_le_conn_param_neg_reply(
+    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_le_conn_param_neg_reply(
+    const struct hci_conn_param_neg_reply *hcn);
+void ble_hs_hci_cmd_build_le_rand(uint8_t *dst, int dst_len);
+void ble_hs_hci_cmd_build_le_start_encrypt(const struct hci_start_encrypt *cmd,
+                                           uint8_t *dst, int dst_len);
+int ble_hs_hci_set_buf_sz(uint16_t pktlen, uint8_t max_pkts);
+
+uint16_t ble_hs_hci_util_handle_pb_bc_join(uint16_t handle, uint8_t pb,
+                                           uint8_t bc);
+
+int ble_hs_hci_acl_tx(struct ble_hs_conn *connection, struct os_mbuf *txom);
+
+int ble_hs_hci_cmd_build_set_data_len(uint16_t connection_handle,
+                                      uint16_t tx_octets, uint16_t tx_time,
+                                      uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_add_to_resolv_list(
+    const struct hci_add_dev_to_resolving_list *padd,
+    uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_remove_from_resolv_list(
+    uint8_t addr_type, const uint8_t *addr, uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_read_resolv_list_size(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_clear_resolv_list(uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_read_peer_resolv_addr(
+    uint8_t peer_identity_addr_type, const uint8_t *peer_identity_addr,
+    uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_read_lcl_resolv_addr(
+    uint8_t local_identity_addr_type, const uint8_t *local_identity_addr,
+    uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_set_addr_res_en(
+    uint8_t enable, uint8_t *dst, int dst_len);
+int ble_hs_hci_cmd_build_set_resolv_priv_addr_timeout(
+    uint16_t timeout, uint8_t *dst, int dst_len);
+
+int ble_hs_hci_cmd_build_set_random_addr(const uint8_t *addr,
+                                         uint8_t *dst, int dst_len);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_hci_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_util.c b/net/nimble/host/src/ble_hs_hci_util.c
new file mode 100644
index 0000000..0d9f625
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_hci_util.c
@@ -0,0 +1,196 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include "nimble/hci_common.h"
+#include "ble_hs_priv.h"
+
+uint16_t
+ble_hs_hci_util_opcode_join(uint8_t ogf, uint16_t ocf)
+{
+    return (ogf << 10) | ocf;
+}
+
+uint16_t
+ble_hs_hci_util_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc)
+{
+    BLE_HS_DBG_ASSERT(handle <= 0x0fff);
+    BLE_HS_DBG_ASSERT(pb <= 0x03);
+    BLE_HS_DBG_ASSERT(bc <= 0x03);
+
+    return (handle  << 0)   |
+           (pb      << 12)  |
+           (bc      << 14);
+}
+
+int
+ble_hs_hci_util_read_adv_tx_pwr(int8_t *out_tx_pwr)
+{
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN];
+    uint8_t params_len;
+    int rc;
+
+    ble_hs_hci_cmd_build_read_adv_pwr(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, out_tx_pwr, 1, &params_len);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (params_len != 1                     ||
+        *out_tx_pwr < BLE_HCI_ADV_CHAN_TXPWR_MIN ||
+        *out_tx_pwr > BLE_HCI_ADV_CHAN_TXPWR_MAX) {
+
+        return BLE_HS_ECONTROLLER;
+    }
+
+    return 0;
+}
+
+int
+ble_hs_hci_util_rand(void *dst, int len)
+{
+    uint8_t rsp_buf[BLE_HCI_LE_RAND_LEN];
+    uint8_t req_buf[BLE_HCI_CMD_HDR_LEN];
+    uint8_t params_len;
+    uint8_t *u8ptr;
+    int chunk_sz;
+    int rc;
+
+    ble_hs_hci_cmd_build_le_rand(req_buf, sizeof req_buf);
+
+    u8ptr = dst;
+    while (len > 0) {
+        rc = ble_hs_hci_cmd_tx(req_buf, rsp_buf, sizeof rsp_buf, &params_len);
+        if (rc != 0) {
+            return rc;
+        }
+        if (params_len != sizeof rsp_buf) {
+            return BLE_HS_ECONTROLLER;
+        }
+
+        chunk_sz = min(len, sizeof rsp_buf);
+        memcpy(u8ptr, rsp_buf, chunk_sz);
+
+        len -= chunk_sz;
+        u8ptr += chunk_sz;
+    }
+
+    return 0;
+}
+
+int
+ble_hs_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi)
+{
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_READ_RSSI_LEN];
+    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
+    uint16_t params_conn_handle;
+    uint8_t params_len;
+    int rc;
+
+    ble_hs_hci_cmd_build_read_rssi(conn_handle, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, params, sizeof params, &params_len);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (params_len != BLE_HCI_READ_RSSI_ACK_PARAM_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    params_conn_handle = le16toh(params + 0);
+    if (params_conn_handle != conn_handle) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    *out_rssi = params[2];
+
+    return 0;
+}
+
+int
+ble_hs_hci_util_set_random_addr(const uint8_t *addr)
+{
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN];
+    int rc;
+
+    /* set the address in the controller */
+
+    rc = ble_hs_hci_cmd_build_set_random_addr(addr, buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
+    return rc;
+}
+
+int
+ble_hs_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
+                             uint16_t tx_time)
+{
+
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN];
+    uint8_t params[BLE_HCI_SET_DATALEN_ACK_PARAM_LEN];
+    uint16_t params_conn_handle;
+    uint8_t params_len;
+    int rc;
+
+    rc = ble_hs_hci_cmd_build_set_data_len(conn_handle, tx_octets, tx_time,
+                                           buf, sizeof buf);
+    if (rc != 0) {
+        return BLE_HS_HCI_ERR(rc);
+    }
+
+    rc = ble_hs_hci_cmd_tx(buf, params, BLE_HCI_SET_DATALEN_ACK_PARAM_LEN,
+                           &params_len);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (params_len != BLE_HCI_SET_DATALEN_ACK_PARAM_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    params_conn_handle = le16toh(params + 0);
+    if (params_conn_handle != conn_handle) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    return 0;
+}
+
+int
+ble_hs_hci_util_data_hdr_strip(struct os_mbuf *om,
+                               struct hci_data_hdr *out_hdr)
+{
+    int rc;
+
+    rc = os_mbuf_copydata(om, 0, BLE_HCI_DATA_HDR_SZ, out_hdr);
+    if (rc != 0) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    /* Strip HCI ACL data header from the front of the packet. */
+    os_mbuf_adj(om, BLE_HCI_DATA_HDR_SZ);
+
+    out_hdr->hdh_handle_pb_bc = le16toh(&out_hdr->hdh_handle_pb_bc);
+    out_hdr->hdh_len = le16toh(&out_hdr->hdh_len);
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_id.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_id.c b/net/nimble/host/src/ble_hs_id.c
index 6d7df2b..9ef384b 100644
--- a/net/nimble/host/src/ble_hs_id.c
+++ b/net/nimble/host/src/ble_hs_id.c
@@ -50,7 +50,7 @@ ble_hs_id_gen_rnd(int nrpa, uint8_t *out_addr)
 {
     int rc;
 
-    rc = ble_hci_util_rand(out_addr, 6);
+    rc = ble_hs_hci_util_rand(out_addr, 6);
     if (rc != 0) {
         return rc;
     }
@@ -91,7 +91,7 @@ ble_hs_id_set_rnd(const uint8_t *rnd_addr)
         goto done;
     }
 
-    rc = ble_hci_util_set_random_addr(rnd_addr);
+    rc = ble_hs_hci_util_set_random_addr(rnd_addr);
     if (rc != 0) {
         goto done;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_priv.h b/net/nimble/host/src/ble_hs_priv.h
index d8b5e82..d61f0ba 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -26,7 +26,8 @@
 #include "ble_att_priv.h"
 #include "ble_gap_priv.h"
 #include "ble_gatt_priv.h"
-#include "ble_hci_priv.h"
+#include "ble_hs_dbg_priv.h"
+#include "ble_hs_hci_priv.h"
 #include "ble_hs_atomic_priv.h"
 #include "ble_hs_conn_priv.h"
 #include "ble_hs_atomic_priv.h"
@@ -82,8 +83,8 @@ int ble_hs_tx_data(struct os_mbuf *om);
 void ble_hs_enqueue_hci_event(uint8_t *hci_evt);
 void ble_hs_event_enqueue(struct os_event *ev);
 
-int host_hci_evt_rx(uint8_t *hci_ev, void *arg);
-int host_hci_acl_process(struct os_mbuf *om);
+int ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg);
+int ble_hs_hci_evt_acl_process(struct os_mbuf *om);
 
 int ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
                                int num_entries, int entry_size, char *name);


[18/50] [abbrv] incubator-mynewt-core git commit: BLE Host - prep/exec tests: better mbuf leak check

Posted by we...@apache.org.
BLE Host - prep/exec tests: better mbuf leak check

When ensuring no mbuf leaks at the end of the att_svr_test suite, also
ensure that each connection's prep list is empty.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 16ead26ad499137c1c04bceb8c0360a39b0c7d43
Parents: 1280abc
Author: Christopher Collins <cc...@apache.org>
Authored: Sun Aug 7 17:59:02 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_att_svr_test.c |  9 ++++-
 net/nimble/host/src/test/ble_hs_test_util.c | 46 ++++++++++++++++--------
 net/nimble/host/src/test/ble_hs_test_util.h | 12 +++++--
 3 files changed, 50 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/16ead26a/net/nimble/host/src/test/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_svr_test.c b/net/nimble/host/src/test/ble_att_svr_test.c
index 86565e3..1509b41 100644
--- a/net/nimble/host/src/test/ble_att_svr_test.c
+++ b/net/nimble/host/src/test/ble_att_svr_test.c
@@ -2282,7 +2282,14 @@ TEST_CASE(ble_att_svr_test_indicate)
 
 TEST_SUITE(ble_att_svr_suite)
 {
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+    /* When checking for mbuf leaks, ensure no stale prep entries. */
+    static struct ble_hs_test_util_mbuf_params mbuf_params = {
+        .prev_tx = 1,
+        .rx_queue = 1,
+        .prep_list = 0,
+    };
+
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, &mbuf_params);
 
     ble_att_svr_test_mtu();
     ble_att_svr_test_read();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/16ead26a/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 54a3527..460642e 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1286,7 +1286,7 @@ ble_hs_test_util_mbuf_chain_len(const struct os_mbuf *om)
 }
 
 int
-ble_hs_test_util_mbuf_count(void)
+ble_hs_test_util_mbuf_count(const struct ble_hs_test_util_mbuf_params *params)
 {
     const struct ble_att_prep_entry *prep;
     const struct os_mbuf_pkthdr *omp;
@@ -1296,14 +1296,17 @@ ble_hs_test_util_mbuf_count(void)
     int count;
     int i;
 
-    count = ble_hs_test_util_mbuf_mpool.mp_num_free;
-    count += ble_hs_test_util_mbuf_chain_len(ble_hs_test_util_prev_tx_cur);
-
     ble_hs_process_tx_data_queue();
     ble_hs_process_rx_data_queue();
-    STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
-        om = OS_MBUF_PKTHDR_TO_MBUF(omp);
-        count += ble_hs_test_util_mbuf_chain_len(om);
+
+    count = ble_hs_test_util_mbuf_mpool.mp_num_free;
+
+    if (params->prev_tx) {
+        count += ble_hs_test_util_mbuf_chain_len(ble_hs_test_util_prev_tx_cur);
+        STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
+            om = OS_MBUF_PKTHDR_TO_MBUF(omp);
+            count += ble_hs_test_util_mbuf_chain_len(om);
+        }
     }
 
     ble_hs_lock();
@@ -1313,12 +1316,16 @@ ble_hs_test_util_mbuf_count(void)
             break;
         }
 
-        SLIST_FOREACH(chan, &conn->bhc_channels, blc_next) {
-            count += ble_hs_test_util_mbuf_chain_len(chan->blc_rx_buf);
+        if (params->rx_queue) {
+            SLIST_FOREACH(chan, &conn->bhc_channels, blc_next) {
+                count += ble_hs_test_util_mbuf_chain_len(chan->blc_rx_buf);
+            }
         }
 
-        SLIST_FOREACH(prep, &conn->bhc_att_svr.basc_prep_list, bape_next) {
-            count += ble_hs_test_util_mbuf_chain_len(prep->bape_value);
+        if (params->prep_list) {
+            SLIST_FOREACH(prep, &conn->bhc_att_svr.basc_prep_list, bape_next) {
+                count += ble_hs_test_util_mbuf_chain_len(prep->bape_value);
+            }
         }
     }
     ble_hs_unlock();
@@ -1327,18 +1334,29 @@ ble_hs_test_util_mbuf_count(void)
 }
 
 void
-ble_hs_test_util_assert_mbufs_freed(void)
+ble_hs_test_util_assert_mbufs_freed(
+    const struct ble_hs_test_util_mbuf_params *params)
 {
+    static const struct ble_hs_test_util_mbuf_params dflt = {
+        .prev_tx = 1,
+        .rx_queue = 1,
+        .prep_list = 1,
+    };
+
     int count;
 
-    count = ble_hs_test_util_mbuf_count();
+    if (params == NULL) {
+        params = &dflt;
+    }
+
+    count = ble_hs_test_util_mbuf_count(params);
     TEST_ASSERT(count == ble_hs_test_util_mbuf_mpool.mp_num_blocks);
 }
 
 void
 ble_hs_test_util_post_test(void *arg)
 {
-    ble_hs_test_util_assert_mbufs_freed();
+    ble_hs_test_util_assert_mbufs_freed(arg);
 }
 
 static int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/16ead26a/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index 0ae6cf5..3320e9b 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -44,6 +44,12 @@ struct ble_hs_test_util_flat_attr {
     uint16_t value_len;
 };
 
+struct ble_hs_test_util_mbuf_params {
+    unsigned prev_tx:1;
+    unsigned rx_queue:1;
+    unsigned prep_list:1;
+};
+
 void ble_hs_test_util_prev_tx_enqueue(struct os_mbuf *om);
 struct os_mbuf *ble_hs_test_util_prev_tx_dequeue(void);
 struct os_mbuf *ble_hs_test_util_prev_tx_dequeue_pullup(void);
@@ -160,8 +166,10 @@ int ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
                                           uint16_t attr_handle,
                                           const void *data, uint16_t data_len,
                                           ble_gatt_attr_fn *cb, void *cb_arg);
-int ble_hs_test_util_mbuf_count(void);
-void ble_hs_test_util_assert_mbufs_freed(void);
+int ble_hs_test_util_mbuf_count(
+    const struct ble_hs_test_util_mbuf_params *params);
+void ble_hs_test_util_assert_mbufs_freed(
+    const struct ble_hs_test_util_mbuf_params *params);
 void ble_hs_test_util_post_test(void *arg);
 void ble_hs_test_util_init(void);
 


[46/50] [abbrv] incubator-mynewt-core git commit: BLE - Fix out-of-date comment.

Posted by we...@apache.org.
BLE - Fix out-of-date comment.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 1a3ad80d72854faa429357402e4af42990415da8
Parents: 2aa6631
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 11 13:49:16 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:27 2016 -0700

----------------------------------------------------------------------
 net/nimble/transport/ram/src/ble_hci_ram.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1a3ad80d/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index f5191b8..e3d236e 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -12,7 +12,7 @@ const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
     .num_evt_hi_bufs = 2,
     .num_evt_lo_bufs = 8,
 
-    /* The largest event the nimble controller will send is 45 bytes. */
+    /* The largest event the nimble controller will send is 70 bytes. */
     .evt_buf_sz = 70,
 };
 


[10/50] [abbrv] incubator-mynewt-core git commit: Update sample BLE apps to use transports.

Posted by we...@apache.org.
Update sample BLE apps to use transports.


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 0f11f27c04868668a2600e3805442a42291dd880
Parents: 093cae0
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 19:43:50 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 apps/blecent/pkg.yml           |   1 +
 apps/blecent/src/main.c        |  25 ++-
 apps/blehci/pkg.yml            |   5 +-
 apps/blehci/src/main.c         | 312 +-----------------------------------
 apps/bleprph/pkg.yml           |   1 +
 apps/bleprph/src/main.c        |  32 +++-
 apps/bletest/pkg.yml           |   1 +
 apps/bletest/src/bletest_hci.c |   2 +-
 apps/bletest/src/main.c        |  12 +-
 apps/bletiny/pkg.yml           |   1 +
 apps/bletiny/src/main.c        |  24 ++-
 apps/bleuart/pkg.yml           |   1 +
 apps/bleuart/src/main.c        |  11 +-
 13 files changed, 94 insertions(+), 334 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/blecent/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/blecent/pkg.yml b/apps/blecent/pkg.yml
index 2fb8be9..b413fad 100644
--- a/apps/blecent/pkg.yml
+++ b/apps/blecent/pkg.yml
@@ -30,6 +30,7 @@ pkg.deps:
     - net/nimble/host/services/gap
     - net/nimble/host/services/gatt
     - net/nimble/host/store/ram
+    - net/nimble/transport/ram
     - libs/console/full
     - libs/baselibc
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index cea4c56..6cc01e2 100755
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@ -29,6 +29,9 @@
 #include "controller/ble_ll.h"
 #include "host/ble_hs.h"
 
+/* RAM HCI transport. */
+#include "transport/ram/ble_hci_ram.h"
+
 /* RAM persistence layer. */
 #include "store/ram/ble_store_ram.h"
 
@@ -448,6 +451,19 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
     }
 }
 
+static void
+blecent_on_reset(int reason)
+{
+    BLECENT_LOG(ERROR, "Resetting state; reason=%d\n", reason);
+}
+
+static void
+blecent_on_sync(void)
+{
+    /* Begin scanning for a peripheral to connect to. */
+    blecent_scan();
+}
+
 /**
  * Event loop for the main blecent task.
  */
@@ -464,9 +480,6 @@ blecent_task_handler(void *unused)
     rc = ble_hs_start();
     assert(rc == 0);
 
-    /* Begin scanning for a peripheral to connect to. */
-    blecent_scan();
-
     while (1) {
         ev = os_eventq_get(&blecent_evq);
         switch (ev->ev_type) {
@@ -553,6 +566,10 @@ main(void)
     rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
     assert(rc == 0);
 
+    /* Initialize the RAM HCI transport. */
+    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    assert(rc == 0);
+
     /* Configure the host. */
     cfg = ble_hs_cfg_dflt;
     cfg.max_hci_bufs = 3;
@@ -560,6 +577,8 @@ main(void)
     cfg.sm_bonding = 1;
     cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
     cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
+    cfg.reset_cb = blecent_on_reset;
+    cfg.sync_cb = blecent_on_sync;
     cfg.store_read_cb = ble_store_ram_read;
     cfg.store_write_cb = ble_store_ram_write;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/blehci/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/blehci/pkg.yml b/apps/blehci/pkg.yml
index f36e383..198e395 100644
--- a/apps/blehci/pkg.yml
+++ b/apps/blehci/pkg.yml
@@ -23,7 +23,8 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
 pkg.deps:
-    - libs/os
-    - net/nimble/controller
     - libs/baselibc
     - libs/console/stub
+    - libs/os
+    - net/nimble/controller
+    - net/nimble/transport/uart

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/blehci/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blehci/src/main.c b/apps/blehci/src/main.c
index 09fb456..20fc4bc 100755
--- a/apps/blehci/src/main.c
+++ b/apps/blehci/src/main.c
@@ -17,24 +17,14 @@
  * under the License.
  */
 #include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
-#include "bsp/bsp.h"
 #include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
 #include "hal/hal_cputime.h"
 #include "hal/hal_uart.h"
 
 /* BLE */
 #include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "nimble/hci_transport.h"
 #include "controller/ble_ll.h"
-
-#define HCI_UART_SPEED 1000000
-#define HCI_UART CONSOLE_UART
+#include "transport/uart/ble_hci_uart.h"
 
 /* Nimble task priorities */
 #define BLE_LL_TASK_PRI         (OS_TASK_PRI_HIGHEST)
@@ -53,291 +43,11 @@ uint8_t g_random_addr[BLE_DEV_ADDR_LEN] = { 0 };
 
 #define HCI_MAX_BUFS        (5)
 
-#define HCI_EVT_BUF_SIZE    (260)
-struct os_mempool g_hci_evt_pool;
-static void *hci_cmd_buf;
-
-#define HCI_OS_EVENT_BUF_SIZE   (sizeof(struct os_event))
-
-#define BLE_HOST_HCI_EVENT_CTLR_EVENT   (OS_EVENT_T_PERUSER + 0)
-#define BLE_HOST_HCI_EVENT_CTLR_DATA    (OS_EVENT_T_PERUSER + 1)
-
-struct os_mempool g_hci_os_event_pool;
-static void *hci_os_event_buf;
-
 os_membuf_t default_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
 
 struct os_mbuf_pool default_mbuf_pool;
 struct os_mempool default_mbuf_mpool;
 
-#define H4_NONE 0x00
-#define H4_CMD  0x01
-#define H4_ACL  0x02
-#define H4_SCO  0x03
-#define H4_EVT  0x04
-
-#define HCI_CMD_HDR_LEN 3
-#define HCI_ACL_HDR_LEN 4
-#define HCI_EVT_HDR_LEN 2
-
-struct memblock {
-    uint8_t *data;      /* Pointer to memblock data */
-    uint16_t cur;       /* Number of bytes read/written */
-    uint16_t len;       /* Total number of bytes to read/write */
-};
-
-struct tx_acl {
-    struct os_mbuf *buf; /* Buffer containing the data */
-    uint16_t len;        /* Target size when buf is considered complete */
-};
-
-static struct {
-    /* State of data from host to controller */
-    uint8_t tx_type;    /* Pending packet type. 0 means nothing pending */
-    union {
-        struct memblock tx_cmd;
-        struct tx_acl tx_acl;
-    };
-
-    /* State of data from controller to host */
-    uint8_t rx_type;    /* Pending packet type. 0 means nothing pending */
-    union {
-        struct memblock rx_evt;
-        struct os_mbuf *rx_acl;
-    };
-    STAILQ_HEAD(, os_event) rx_pkts; /* Packet queue to send to UART */
-} hci;
-
-int
-ble_hs_rx_data(struct os_mbuf *om)
-{
-    struct os_event *ev;
-    os_sr_t sr;
-
-    ev = os_memblock_get(&g_hci_os_event_pool);
-    if (!ev) {
-        os_mbuf_free_chain(om);
-        return -1;
-    }
-
-    ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_DATA;
-    ev->ev_arg = om;
-    ev->ev_queued = 1;
-
-    OS_ENTER_CRITICAL(sr);
-    STAILQ_INSERT_TAIL(&hci.rx_pkts, ev, ev_next);
-    OS_EXIT_CRITICAL(sr);
-
-    hal_uart_start_tx(HCI_UART);
-
-    return 0;
-}
-
-int
-ble_hci_transport_ctlr_event_send(uint8_t *hci_ev)
-{
-    struct os_event *ev;
-    os_sr_t sr;
-
-    ev = os_memblock_get(&g_hci_os_event_pool);
-    if (!ev) {
-        os_error_t err;
-
-        err = os_memblock_put(&g_hci_evt_pool, hci_ev);
-        assert(err == OS_OK);
-
-        return -1;
-    }
-
-    ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
-    ev->ev_arg = hci_ev;
-    ev->ev_queued = 1;
-
-    OS_ENTER_CRITICAL(sr);
-    STAILQ_INSERT_TAIL(&hci.rx_pkts, ev, ev_next);
-    OS_EXIT_CRITICAL(sr);
-
-    hal_uart_start_tx(HCI_UART);
-
-    return 0;
-}
-
-static int
-uart_tx_pkt_type(void)
-{
-    struct os_event *ev;
-    os_sr_t sr;
-    int rc;
-
-    OS_ENTER_CRITICAL(sr);
-
-    ev = STAILQ_FIRST(&hci.rx_pkts);
-    if (!ev) {
-        OS_EXIT_CRITICAL(sr);
-        return -1;
-    }
-
-    STAILQ_REMOVE(&hci.rx_pkts, ev, os_event, ev_next);
-    ev->ev_queued = 0;
-
-    OS_EXIT_CRITICAL(sr);
-
-    switch (ev->ev_type) {
-    case BLE_HOST_HCI_EVENT_CTLR_EVENT:
-        hci.rx_type = H4_EVT;
-        hci.rx_evt.data = ev->ev_arg;
-        hci.rx_evt.cur = 0;
-        hci.rx_evt.len = hci.rx_evt.data[1] + HCI_EVT_HDR_LEN;
-        rc = H4_EVT;
-        break;
-    case BLE_HOST_HCI_EVENT_CTLR_DATA:
-        hci.rx_type = H4_ACL;
-        hci.rx_acl = ev->ev_arg;
-        rc = H4_ACL;
-        break;
-    default:
-        rc = -1;
-        break;
-    }
-
-    os_memblock_put(&g_hci_os_event_pool, ev);
-
-    return rc;
-}
-
-static int
-uart_tx_char(void *arg)
-{
-    int rc = -1;
-
-    switch (hci.rx_type) {
-    case H4_NONE: /* No pending packet, pick one from the queue */
-        rc = uart_tx_pkt_type();
-        break;
-    case H4_EVT:
-        rc = hci.rx_evt.data[hci.rx_evt.cur++];
-
-        if (hci.rx_evt.cur == hci.rx_evt.len) {
-            os_memblock_put(&g_hci_evt_pool, hci.rx_evt.data);
-            hci.rx_type = H4_NONE;
-        }
-
-        break;
-    case H4_ACL:
-        rc = *OS_MBUF_DATA(hci.rx_acl, uint8_t *);
-        os_mbuf_adj(hci.rx_acl, 1);
-        if (!OS_MBUF_PKTLEN(hci.rx_acl)) {
-            os_mbuf_free_chain(hci.rx_acl);
-            hci.rx_type = H4_NONE;
-        }
-
-        break;
-    }
-
-    return rc;
-}
-
-static int
-uart_rx_pkt_type(uint8_t data)
-{
-    hci.tx_type = data;
-
-    switch (hci.tx_type) {
-    case H4_CMD:
-        hci.tx_cmd.data = os_memblock_get(&g_hci_evt_pool);
-        hci.tx_cmd.len = 0;
-        hci.tx_cmd.cur = 0;
-        break;
-    case H4_ACL:
-        hci.tx_acl.buf = os_msys_get_pkthdr(HCI_ACL_HDR_LEN, 0);
-        hci.tx_acl.len = 0;
-        break;
-    default:
-        hci.tx_type = H4_NONE;
-        return -1;
-    }
-
-    return 0;
-}
-
-static int
-uart_rx_cmd(uint8_t data)
-{
-    int rc;
-
-    hci.tx_cmd.data[hci.tx_cmd.cur++] = data;
-
-    if (hci.tx_cmd.cur < HCI_CMD_HDR_LEN) {
-        return 0;
-    } else if (hci.tx_cmd.cur == HCI_CMD_HDR_LEN) {
-        hci.tx_cmd.len = hci.tx_cmd.data[2] + HCI_CMD_HDR_LEN;
-    }
-
-    if (hci.tx_cmd.cur == hci.tx_cmd.len) {
-        rc = ble_hci_transport_host_cmd_send(hci.tx_cmd.data);
-        if (rc != 0) {
-            os_memblock_put(&g_hci_evt_pool, hci.tx_cmd.data);
-        }
-        hci.tx_type = H4_NONE;
-    }
-
-    return 0;
-}
-
-static int
-uart_rx_acl(uint8_t data)
-{
-    os_mbuf_append(hci.tx_acl.buf, &data, 1);
-
-    if (OS_MBUF_PKTLEN(hci.tx_acl.buf) < HCI_ACL_HDR_LEN) {
-        return 0;
-    } else if (OS_MBUF_PKTLEN(hci.tx_acl.buf) == HCI_ACL_HDR_LEN) {
-        os_mbuf_copydata(hci.tx_acl.buf, 2, sizeof(hci.tx_acl.len),
-                         &hci.tx_acl.len);
-        hci.tx_acl.len = le16toh(&hci.tx_acl.len) + HCI_ACL_HDR_LEN;
-    }
-
-    if (OS_MBUF_PKTLEN(hci.tx_acl.buf) == hci.tx_acl.len) {
-        ble_hci_transport_host_acl_data_send(hci.tx_acl.buf);
-        hci.tx_type = H4_NONE;
-    }
-
-    return 0;
-}
-
-static int
-uart_rx_char(void *arg, uint8_t data)
-{
-    switch (hci.tx_type) {
-    case H4_NONE:
-        return uart_rx_pkt_type(data);
-    case H4_CMD:
-        return uart_rx_cmd(data);
-    case H4_ACL:
-        return uart_rx_acl(data);
-    default:
-        return -1;
-    }
-}
-
-static int
-uart_init(void)
-{
-    int rc;
-
-    memset(&hci, 0, sizeof(hci));
-
-    STAILQ_INIT(&hci.rx_pkts);
-
-    rc = hal_uart_init_cbs(HCI_UART, uart_tx_char, NULL, uart_rx_char, NULL);
-    if (rc) {
-        return rc;
-    }
-
-    return hal_uart_config(HCI_UART, HCI_UART_SPEED, 8, 1, HAL_UART_PARITY_NONE,
-                           HAL_UART_FLOW_CTL_RTS_CTS);
-}
-
 int
 main(void)
 {
@@ -366,25 +76,7 @@ main(void)
     rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
     assert(rc == 0);
 
-    hci_cmd_buf = malloc(OS_MEMPOOL_BYTES(HCI_MAX_BUFS, HCI_EVT_BUF_SIZE));
-    assert(hci_cmd_buf != NULL);
-
-    /* Create memory pool of command buffers */
-    rc = os_mempool_init(&g_hci_evt_pool, HCI_MAX_BUFS, HCI_EVT_BUF_SIZE,
-                         hci_cmd_buf, "HCICmdPool");
-    assert(rc == 0);
-
-    hci_os_event_buf = malloc(OS_MEMPOOL_BYTES(HCI_MAX_BUFS,
-                              HCI_OS_EVENT_BUF_SIZE));
-    assert(hci_os_event_buf != NULL);
-
-    /* Create memory pool of OS events */
-    rc = os_mempool_init(&g_hci_os_event_pool, HCI_MAX_BUFS,
-                         HCI_OS_EVENT_BUF_SIZE, hci_os_event_buf,
-                         "HCIOsEventPool");
-    assert(rc == 0);
-
-    rc = uart_init();
+    rc = ble_hci_uart_init(&ble_hci_uart_cfg_dflt);
     assert(rc == 0);
 
     /* Start the OS */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index f697d83..cfc8e3b 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -30,6 +30,7 @@ pkg.deps:
     - net/nimble/host/services/gap
     - net/nimble/host/services/gatt
     - net/nimble/host/store/ram
+    - net/nimble/transport/ram
     - libs/console/full
     - libs/baselibc
     - libs/newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index b1d38e6..8261de3 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -40,9 +40,9 @@
 #include "host/ble_l2cap.h"
 #include "host/ble_sm.h"
 #include "controller/ble_ll.h"
-/* Newtmgr include */
-#include "newtmgr/newtmgr.h"
-#include "nmgrble/newtmgr_ble.h"
+
+/* RAM HCI transport. */
+#include "transport/ram/ble_hci_ram.h"
 
 /* RAM persistence layer. */
 #include "store/ram/ble_store_ram.h"
@@ -51,6 +51,10 @@
 #include "services/gap/ble_svc_gap.h"
 #include "services/gatt/ble_svc_gatt.h"
 
+/* Newtmgr include */
+#include "newtmgr/newtmgr.h"
+#include "nmgrble/newtmgr_ble.h"
+
 /* Application-specified header. */
 #include "bleprph.h"
 
@@ -266,6 +270,19 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
     return 0;
 }
 
+static void
+bleprph_on_reset(int reason)
+{
+    BLEPRPH_LOG(ERROR, "Resetting state; reason=%d\n", reason);
+}
+
+static void
+bleprph_on_sync(void)
+{
+    /* Begin advertising. */
+    bleprph_advertise();
+}
+
 /**
  * Event loop for the main bleprph task.
  */
@@ -280,10 +297,6 @@ bleprph_task_handler(void *unused)
      * controller.
      */
     rc = ble_hs_start();
-    assert(rc == 0);
-
-    /* Begin advertising. */
-    bleprph_advertise();
 
     while (1) {
         ev = os_eventq_get(&bleprph_evq);
@@ -383,6 +396,8 @@ main(void)
     cfg.sm_bonding = 1;
     cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
     cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
+    cfg.reset_cb = bleprph_on_reset;
+    cfg.sync_cb = bleprph_on_sync;
     cfg.store_read_cb = ble_store_ram_read;
     cfg.store_write_cb = ble_store_ram_write;
     cfg.gatts_register_cb = gatt_svr_register_cb;
@@ -404,6 +419,9 @@ main(void)
     rc = ble_hs_init(&bleprph_evq, &cfg);
     assert(rc == 0);
 
+    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    assert(rc == 0);
+
     nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
     imgmgr_module_init();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bletest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletest/pkg.yml b/apps/bletest/pkg.yml
index 84b6e71..3e94ede 100644
--- a/apps/bletest/pkg.yml
+++ b/apps/bletest/pkg.yml
@@ -28,6 +28,7 @@ pkg.deps:
     - fs/nffs
     - net/nimble/controller
     - net/nimble/host
+    - net/nimble/transport/ram
     - libs/os 
     - libs/console/full
     - libs/shell

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bletest/src/bletest_hci.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_hci.c b/apps/bletest/src/bletest_hci.c
index 5e6c31e..64d0270 100755
--- a/apps/bletest/src/bletest_hci.c
+++ b/apps/bletest/src/bletest_hci.c
@@ -24,7 +24,7 @@
 
 /* BLE */
 #include "nimble/ble.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "nimble/hci_common.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index f6bb187..be0cf6d 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -36,7 +36,7 @@
 
 /* BLE */
 #include "nimble/ble.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "nimble/hci_common.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"
@@ -44,6 +44,7 @@
 #include "controller/ble_ll_conn.h"
 #include "controller/ble_ll_scan.h"
 #include "controller/ble_ll_adv.h"
+#include "transport/ram/ble_hci_ram.h"
 
 /* XXX: An app should not include private headers from a library.  The bletest
  * app uses some of nimble's internal details for logging.
@@ -767,7 +768,7 @@ bletest_execute_advertiser(void)
 
                         /* Add length */
                         OS_MBUF_PKTHDR(om)->omp_len = om->om_len;
-                        ble_hci_transport_host_acl_data_send(om);
+                        ble_hci_trans_hs_acl_data_send(om);
 
                         /* Increment last handle used */
                         ++g_last_handle_used;
@@ -823,7 +824,7 @@ bletest_execute_advertiser(void)
 
                 /* Add length */
                 OS_MBUF_PKTHDR(om)->omp_len = om->om_len;
-                ble_hci_transport_host_acl_data_send(om);
+                ble_hci_trans_hs_acl_data_send(om);
 
                 ++g_bletest_outstanding_pkts;
             }
@@ -895,6 +896,8 @@ bletest_task_handler(void *arg)
     os_callout_func_init(&g_bletest_timer, &g_bletest_evq, bletest_timer_cb,
                          NULL);
 
+    ble_hs_dbg_set_sync_state(BLE_HS_SYNC_STATE_GOOD);
+
     /* Send the reset command first */
     rc = bletest_hci_reset_ctlr();
     assert(rc == 0);
@@ -1151,6 +1154,9 @@ main(void)
     rc = ble_hs_init(&g_bletest_evq, NULL);
     assert(rc == 0);
 
+    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
+    assert(rc == 0);
+
     rc = os_task_init(&bletest_task, "bletest", bletest_task_handler, NULL,
                       BLETEST_TASK_PRIO, OS_WAIT_FOREVER, bletest_stack,
                       BLETEST_STACK_SIZE);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 49eba38..9a8b08f 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -30,6 +30,7 @@ pkg.deps:
     - net/nimble/host/services/gap
     - net/nimble/host/services/gatt
     - net/nimble/host/store/ram
+    - net/nimble/transport/ram
     - libs/console/full
     - libs/shell
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index bb88706..33de2f0 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -35,8 +35,8 @@
 /* BLE */
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
-#include "nimble/hci_transport.h"
-#include "host/host_hci.h"
+#include "nimble/ble_hci_trans.h"
+#include "controller/ble_ll.h"
 #include "host/ble_hs.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_uuid.h"
@@ -45,7 +45,7 @@
 #include "host/ble_gatt.h"
 #include "host/ble_store.h"
 #include "host/ble_sm.h"
-#include "controller/ble_ll.h"
+#include "transport/ram/ble_hci_ram.h"
 
 /* RAM persistence layer. */
 #include "store/ram/ble_store_ram.h"
@@ -1084,7 +1084,7 @@ bletiny_tx_timer_cb(void *arg)
 
         /* Set packet header length */
         OS_MBUF_PKTHDR(om)->omp_len = om->om_len;
-        ble_hci_transport_host_acl_data_send(om);
+        ble_hci_trans_hs_acl_tx(om);
 
         --bletiny_tx_data.tx_num;
     }
@@ -1552,6 +1552,12 @@ bletiny_rssi(uint16_t conn_handle, int8_t *out_rssi)
     return 0;
 }
 
+static void
+bletiny_on_reset(int reason)
+{
+    console_printf("Error: Resetting state; reason=%d\n", reason);
+}
+
 /**
  * BLE test task
  *
@@ -1594,6 +1600,7 @@ bletiny_task_handler(void *arg)
 int
 main(void)
 {
+    struct ble_hci_ram_cfg hci_cfg;
     struct ble_hs_cfg cfg;
     uint32_t seed;
     int rc;
@@ -1688,10 +1695,17 @@ main(void)
     rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
     assert(rc == 0);
 
+    /* Initialize the RAM HCI transport. */
+    hci_cfg = ble_hci_ram_cfg_dflt;
+    hci_cfg.num_evt_bufs = 3;
+    rc = ble_hci_ram_init(&hci_cfg);
+    assert(rc == 0);
+
     /* Initialize the NimBLE host configuration. */
     cfg = ble_hs_cfg_dflt;
-    cfg.max_hci_bufs = 3;
+    cfg.max_hci_bufs = hci_cfg.num_evt_bufs;
     cfg.max_gattc_procs = 2;
+    cfg.reset_cb = bletiny_on_reset;
     cfg.store_read_cb = ble_store_ram_read;
     cfg.store_write_cb = ble_store_ram_write;
     cfg.gatts_register_cb = gatt_svr_register_cb;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bleuart/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleuart/pkg.yml b/apps/bleuart/pkg.yml
index dbe08cc..7a7d20e 100644
--- a/apps/bleuart/pkg.yml
+++ b/apps/bleuart/pkg.yml
@@ -30,6 +30,7 @@ pkg.deps:
     - net/nimble/host/services/gap
     - net/nimble/host/services/gatt
     - net/nimble/host/store/ram
+    - net/nimble/transport/ram
     - libs/console/full
     - libs/baselibc
     - libs/newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0f11f27c/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index 75d9e16..e68d097 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -195,6 +195,13 @@ bleuart_gap_event(struct ble_gap_event *event, void *arg)
     return 0;
 }
 
+static void
+bleuart_on_sync(void)
+{
+    /* Begin advertising. */
+    bleuart_advertise();
+}
+
 /**
  * Event loop for the main bleuart task.
  */
@@ -208,9 +215,6 @@ bleuart_task_handler(void *unused)
     rc = ble_hs_start();
     assert(rc == 0);
 
-    /* Begin advertising. */
-    bleuart_advertise();
-
     while (1) {
         ev = os_eventq_get(&bleuart_evq);
 
@@ -298,6 +302,7 @@ main(void)
     cfg.sm_bonding = 1;
     cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
     cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
+    cfg.sync_cb = bleuart_on_sync;
     cfg.store_read_cb = ble_store_ram_read;
     cfg.store_write_cb = ble_store_ram_write;
 


[08/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by we...@apache.org.
BLE Host - Rename HCI identifiers and files.

The HCI-related host functionality was using some really inconsistent
names.  Identifiers and files have been renamed as reorganized as
follows:

* host_dbg      --> ble_hs_dbg
* host_hci_cmd  --> ble_hs_hci_cmd
* host_hci      --> ble_hs_hci_evt / ble_hs_hci
* ble_hci_util  --> ble_hs_hci_util


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

Branch: refs/heads/phyrx_no_mbuf
Commit: 093cae09940fed0635978fac6df774f70c3babe3
Parents: 97b45e7
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 18:41:01 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:24 2016 -0700

----------------------------------------------------------------------
 apps/blecent/src/main.c                       |    1 -
 apps/bleprph/src/main.c                       |    1 -
 apps/bletest/src/bletest_hci.c                |  128 +-
 apps/bletest/src/main.c                       |    9 +-
 apps/bletiny/src/main.c                       |    4 +-
 apps/bleuart/src/main.c                       |    1 -
 net/nimble/host/include/host/ble_hs.h         |    1 -
 net/nimble/host/include/host/ble_hs_test.h    |    2 +-
 net/nimble/host/include/host/host_hci.h       |  130 --
 net/nimble/host/src/ble_eddystone.c           |    2 +-
 net/nimble/host/src/ble_gap.c                 |   85 +-
 net/nimble/host/src/ble_gap_priv.h            |    2 +-
 net/nimble/host/src/ble_gattc.c               |    4 +-
 net/nimble/host/src/ble_hci_cmd.c             |  320 -----
 net/nimble/host/src/ble_hci_priv.h            |   51 -
 net/nimble/host/src/ble_hci_util.c            |  178 ---
 net/nimble/host/src/ble_hs.c                  |    9 +-
 net/nimble/host/src/ble_hs_adv.c              |    2 +-
 net/nimble/host/src/ble_hs_conn.c             |    1 -
 net/nimble/host/src/ble_hs_dbg.c              |  509 ++++++++
 net/nimble/host/src/ble_hs_dbg_priv.h         |   26 +
 net/nimble/host/src/ble_hs_hci.c              |  528 ++++++++
 net/nimble/host/src/ble_hs_hci_cmd.c          | 1375 ++++++++++++++++++++
 net/nimble/host/src/ble_hs_hci_evt.c          |  677 ++++++++++
 net/nimble/host/src/ble_hs_hci_priv.h         |  158 +++
 net/nimble/host/src/ble_hs_hci_util.c         |  196 +++
 net/nimble/host/src/ble_hs_id.c               |    4 +-
 net/nimble/host/src/ble_hs_priv.h             |    7 +-
 net/nimble/host/src/ble_hs_pvcy.c             |   18 +-
 net/nimble/host/src/ble_hs_startup.c          |   35 +-
 net/nimble/host/src/ble_ibeacon.c             |    2 +-
 net/nimble/host/src/ble_l2cap.c               |    3 +-
 net/nimble/host/src/ble_sm.c                  |   26 +-
 net/nimble/host/src/ble_sm_alg.c              |    5 +-
 net/nimble/host/src/ble_sm_sc.c               |    2 +-
 net/nimble/host/src/host_dbg.c                |  503 -------
 net/nimble/host/src/host_dbg_priv.h           |   25 -
 net/nimble/host/src/host_hci.c                |  899 -------------
 net/nimble/host/src/host_hci_cmd.c            | 1364 -------------------
 net/nimble/host/src/test/ble_att_clt_test.c   |    9 +-
 net/nimble/host/src/test/ble_gap_test.c       |   49 +-
 net/nimble/host/src/test/ble_gatt_read_test.c |   21 +-
 net/nimble/host/src/test/ble_host_hci_test.c  |  100 --
 net/nimble/host/src/test/ble_hs_adv_test.c    |    1 -
 net/nimble/host/src/test/ble_hs_conn_test.c   |    1 -
 net/nimble/host/src/test/ble_hs_hci_test.c    |   99 ++
 net/nimble/host/src/test/ble_hs_test.c        |    2 +-
 net/nimble/host/src/test/ble_hs_test_util.c   |   83 +-
 net/nimble/host/src/test/ble_l2cap_test.c     |    4 +-
 net/nimble/host/src/test/ble_os_test.c        |    3 +-
 net/nimble/host/src/test/ble_sm_lgcy_test.c   |    1 -
 net/nimble/host/src/test/ble_sm_sc_test.c     |    1 -
 net/nimble/host/src/test/ble_sm_test.c        |    1 -
 net/nimble/host/src/test/ble_sm_test_util.c   |   21 +-
 net/nimble/host/store/ram/src/ble_store_ram.c |    3 +-
 55 files changed, 3855 insertions(+), 3837 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index 7b43214..cea4c56 100755
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@ -27,7 +27,6 @@
 /* BLE */
 #include "nimble/ble.h"
 #include "controller/ble_ll.h"
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 
 /* RAM persistence layer. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 6a9ae84..b1d38e6 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -31,7 +31,6 @@
 
 /* BLE */
 #include "nimble/ble.h"
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_uuid.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/bletest/src/bletest_hci.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_hci.c b/apps/bletest/src/bletest_hci.c
index 9b0eacd..5e6c31e 100755
--- a/apps/bletest/src/bletest_hci.c
+++ b/apps/bletest/src/bletest_hci.c
@@ -26,7 +26,6 @@
 #include "nimble/ble.h"
 #include "nimble/hci_transport.h"
 #include "nimble/hci_common.h"
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
@@ -56,7 +55,7 @@ bletest_send_conn_update(uint16_t handle)
     hcu.min_ce_len = 4;
     hcu.max_ce_len = 4;
 
-    rc = host_hci_cmd_le_conn_update(&hcu);
+    rc = ble_hs_hci_cmd_le_conn_update(&hcu);
     assert(rc == 0);
 }
 
@@ -77,12 +76,12 @@ bletest_send_ltk_req_neg_reply(uint16_t handle)
     uint8_t rsplen;
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
                        sizeof(uint16_t), dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, handle);
-    rc = ble_hci_cmd_tx(buf, &ack_conn_handle, 2, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, &ack_conn_handle, 2, &rsplen);
     if (rc == 0) {
         if (rsplen != 2) {
             rc = -1;
@@ -104,8 +103,8 @@ bletest_send_ltk_req_reply(uint16_t handle)
     hkr.conn_handle = handle;
     swap_buf(hkr.long_term_key, (uint8_t *)g_bletest_LTK, 16);
 
-    host_hci_cmd_build_le_lt_key_req_reply(&hkr, buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
+    ble_hs_hci_cmd_build_le_lt_key_req_reply(&hkr, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx(buf, &ack_conn_handle, sizeof ack_conn_handle,
                         &ack_params_len);
     if (rc != 0) {
         return rc;
@@ -127,8 +126,9 @@ bletest_hci_reset_ctlr(void)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN];
 
-    host_hci_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET, 0, buf);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET,
+                             0, buf);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -139,9 +139,9 @@ bletest_hci_rd_bd_addr(void)
     uint8_t rspbuf[BLE_DEV_ADDR_LEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR, 0,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR, 0,
                        buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_DEV_ADDR_LEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_DEV_ADDR_LEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -164,13 +164,13 @@ bletest_hci_le_encrypt(uint8_t *key, uint8_t *pt)
     uint8_t rsplen;
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ENCRYPT,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ENCRYPT,
                        BLE_HCI_LE_ENCRYPT_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     swap_buf(dst, key, BLE_ENC_BLOCK_SIZE);
     swap_buf(dst + BLE_ENC_BLOCK_SIZE, pt, BLE_ENC_BLOCK_SIZE);
-    rc = ble_hci_cmd_tx(buf, rspbuf, 16, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, 16, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -192,14 +192,14 @@ bletest_hci_le_set_datalen(uint16_t handle, uint16_t txoctets, uint16_t txtime)
     uint8_t rsplen;
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DATA_LEN,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DATA_LEN,
                        BLE_HCI_SET_DATALEN_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, handle);
     htole16(dst + 2, txoctets);
     htole16(dst + 4, txtime);
-    rc = ble_hci_cmd_tx(buf, rspbuf, 2, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, 2, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -218,13 +218,13 @@ bletest_hci_le_write_sugg_datalen(uint16_t txoctets, uint16_t txtime)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_WR_SUGG_DATALEN_LEN];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_WR_SUGG_DEF_DATA_LEN,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_WR_SUGG_DEF_DATA_LEN,
                        BLE_HCI_WR_SUGG_DATALEN_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, txoctets);
     htole16(dst + 2, txtime);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -235,10 +235,10 @@ bletest_hci_le_rd_sugg_datalen(void)
     uint8_t rspbuf[BLE_HCI_RD_SUGG_DATALEN_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_SUGG_DEF_DATA_LEN, 0,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_SUGG_DEF_DATA_LEN, 0,
                        buf);
 
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_SUGG_DATALEN_RSPLEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_SUGG_DATALEN_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -258,9 +258,9 @@ bletest_hci_rd_local_version(void)
     uint8_t rspbuf[BLE_HCI_RD_LOC_VER_INFO_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOCAL_VER, 0,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOCAL_VER, 0,
                        buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_VER_INFO_RSPLEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_VER_INFO_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -279,9 +279,9 @@ bletest_hci_rd_local_feat(void)
     uint8_t rspbuf[BLE_HCI_RD_LOC_SUPP_FEAT_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOC_SUPP_FEAT,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOC_SUPP_FEAT,
                        0, buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_SUPP_FEAT_RSPLEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_SUPP_FEAT_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -300,9 +300,9 @@ bletest_hci_rd_local_supp_cmd(void)
     uint8_t rspbuf[BLE_HCI_RD_LOC_SUPP_CMD_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOC_SUPP_CMD,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_LOC_SUPP_CMD,
                        0, buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_SUPP_CMD_RSPLEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_LOC_SUPP_CMD_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -329,8 +329,8 @@ bletest_hci_le_read_supp_states(void)
     uint8_t rspbuf[BLE_HCI_RD_SUPP_STATES_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_SUPP_STATES, 0, buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_SUPP_STATES_RSPLEN, &rsplen);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_SUPP_STATES, 0, buf);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_SUPP_STATES_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -349,8 +349,8 @@ bletest_hci_le_rd_max_datalen(void)
     uint8_t rspbuf[BLE_HCI_RD_MAX_DATALEN_RSPLEN];
     uint8_t rsplen;
 
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_MAX_DATA_LEN, 0, buf);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_MAX_DATALEN_RSPLEN, &rsplen);
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_MAX_DATA_LEN, 0, buf);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_MAX_DATALEN_RSPLEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -367,9 +367,9 @@ bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len)
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_DATA_LEN];
 
-    rc = host_hci_cmd_build_le_set_adv_data(data, len, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_adv_data(data, len, buf, sizeof buf);
     assert(rc == 0);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
 #if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
@@ -378,8 +378,8 @@ bletest_hci_le_start_encrypt(struct hci_start_encrypt *cmd)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_START_ENCRYPT_LEN];
 
-    host_hci_cmd_build_le_start_encrypt(cmd, buf, sizeof buf);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_start_encrypt(cmd, buf, sizeof buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 #endif
 
@@ -390,12 +390,12 @@ bletest_hci_le_read_rem_used_feat(uint16_t handle)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_CONN_RD_REM_FEAT_LEN];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_REM_FEAT,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_REM_FEAT,
                        BLE_HCI_CONN_RD_REM_FEAT_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, handle);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -404,9 +404,9 @@ bletest_hci_le_set_adv_params(struct hci_adv_params *adv)
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_PARAM_LEN];
 
-    rc = host_hci_cmd_build_le_set_adv_params(adv, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_adv_params(adv, buf, sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }
@@ -418,12 +418,12 @@ bletest_hci_le_set_rand_addr(uint8_t *addr)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RAND_ADDR,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_RAND_ADDR,
                        BLE_DEV_ADDR_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     memcpy(dst, addr, BLE_DEV_ADDR_LEN);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -433,12 +433,12 @@ bletest_hci_rd_rem_version(uint16_t handle)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + sizeof(uint16_t)];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LINK_CTRL, BLE_HCI_OCF_RD_REM_VER_INFO,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LINK_CTRL, BLE_HCI_OCF_RD_REM_VER_INFO,
                        sizeof(uint16_t), dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, handle);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -448,12 +448,12 @@ bletest_hci_le_set_host_chan_class(uint8_t *chanmap)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_HOST_CHAN_CLASS_LEN];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_HOST_CHAN_CLASS,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_HOST_CHAN_CLASS,
                        BLE_HCI_SET_HOST_CHAN_CLASS_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     memcpy(dst, chanmap, BLE_HCI_SET_HOST_CHAN_CLASS_LEN);
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -466,12 +466,12 @@ bletest_hci_le_rd_chanmap(uint16_t handle)
     uint8_t rsplen;
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_CHAN_MAP,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_CHAN_MAP,
                        BLE_HCI_RD_CHANMAP_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     htole16(dst, handle);
-    rc = ble_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_CHANMAP_RSP_LEN, &rsplen);
+    rc = ble_hs_hci_cmd_tx(buf, rspbuf, BLE_HCI_RD_CHANMAP_RSP_LEN, &rsplen);
     if (rc != 0) {
         return rc;
     }
@@ -490,12 +490,12 @@ bletest_hci_le_set_adv_enable(uint8_t enable)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_ENABLE_LEN];
 
     dst = buf;
-    host_hci_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE,
                        BLE_HCI_SET_ADV_ENABLE_LEN, dst);
     dst += BLE_HCI_CMD_HDR_LEN;
 
     dst[0] = enable;
-    return ble_hci_cmd_tx(buf, NULL, 0, NULL);
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
 }
 
 int
@@ -503,8 +503,8 @@ bletest_hci_le_set_event_mask(uint64_t event_mask)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_LE_EVENT_MASK_LEN];
 
-    host_hci_cmd_build_le_set_event_mask(event_mask, buf, sizeof buf);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_set_event_mask(event_mask, buf, sizeof buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
 int
@@ -512,8 +512,8 @@ bletest_hci_set_event_mask(uint64_t event_mask)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_EVENT_MASK_LEN];
 
-    host_hci_cmd_build_set_event_mask(event_mask, buf, sizeof buf);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_set_event_mask(event_mask, buf, sizeof buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
 int
@@ -522,9 +522,9 @@ bletest_hci_le_set_scan_rsp_data(uint8_t *data, uint8_t len)
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_RSP_DATA_LEN];
 
-    rc = host_hci_cmd_build_le_set_scan_rsp_data(data, len, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_scan_rsp_data(data, len, buf, sizeof buf);
     assert(rc == 0);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
 int
@@ -534,11 +534,11 @@ bletest_hci_cmd_le_set_scan_params(uint8_t scan_type, uint16_t scan_itvl,
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_PARAM_LEN];
 
-    rc = host_hci_cmd_build_le_set_scan_params(scan_type, scan_itvl,
+    rc = ble_hs_hci_cmd_build_le_set_scan_params(scan_type, scan_itvl,
                                                scan_window, own_addr_type,
                                                filter_policy, buf, sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }
@@ -549,10 +549,10 @@ bletest_hci_le_add_to_whitelist(uint8_t *addr, uint8_t addr_type)
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_PARAM_LEN];
 
-    rc = host_hci_cmd_build_le_add_to_whitelist(addr, addr_type, buf,
+    rc = ble_hs_hci_cmd_build_le_add_to_whitelist(addr, addr_type, buf,
                                                 sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }
@@ -562,8 +562,8 @@ bletest_hci_le_set_scan_enable(uint8_t enable, uint8_t filter_dups)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_ENABLE_LEN];
 
-    host_hci_cmd_build_le_set_scan_enable(enable, filter_dups, buf, sizeof buf);
-    return ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_set_scan_enable(enable, filter_dups, buf, sizeof buf);
+    return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
 int
@@ -572,9 +572,9 @@ bletest_hci_le_create_connection(struct hci_create_conn *hcc)
     int rc;
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_CREATE_CONN_LEN];
 
-    rc = host_hci_cmd_build_le_create_connection(hcc, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_create_connection(hcc, buf, sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }
@@ -591,9 +591,9 @@ bletest_hci_le_add_resolv_list(uint8_t *local_irk, uint8_t *peer_irk,
     memcpy(padd.addr, peer_ident_addr, BLE_DEV_ADDR_LEN);
     swap_buf(padd.local_irk, local_irk, 16);
     swap_buf(padd.peer_irk, peer_irk, 16);
-    rc = host_hci_cmd_build_add_to_resolv_list(&padd, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_add_to_resolv_list(&padd, buf, sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }
@@ -605,9 +605,9 @@ bletest_hci_le_enable_resolv_list(uint8_t enable)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADDR_RESOL_ENA_LEN];
 
 
-    rc = host_hci_cmd_build_set_addr_res_en(enable, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_set_addr_res_en(enable, buf, sizeof buf);
     if (!rc) {
-        rc = ble_hci_cmd_tx_empty_ack(buf);
+        rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     }
     return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index dbfa053..f6bb187 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -38,7 +38,6 @@
 #include "nimble/ble.h"
 #include "nimble/hci_transport.h"
 #include "nimble/hci_common.h"
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
@@ -420,14 +419,14 @@ bletest_init_scanner(void)
     uint8_t add_whitelist;
 
     own_addr_type = BLETEST_CFG_SCAN_OWN_ADDR_TYPE;
-    rc = host_hci_cmd_build_le_set_scan_params(BLETEST_CFG_SCAN_TYPE,
+    rc = ble_hs_hci_cmd_build_le_set_scan_params(BLETEST_CFG_SCAN_TYPE,
                                                BLETEST_CFG_SCAN_ITVL,
                                                BLETEST_CFG_SCAN_WINDOW,
                                                BLETEST_CFG_SCAN_OWN_ADDR_TYPE,
                                                BLETEST_CFG_SCAN_FILT_POLICY,
                                                buf, sizeof buf);
     assert(rc == 0);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc == 0) {
         add_whitelist = BLETEST_CFG_SCAN_FILT_POLICY;
 #if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
@@ -615,7 +614,7 @@ bletest_execute_initiator(void)
             } else {
                 for (i = 0; i < g_bletest_current_conns; ++i) {
                     if (ble_ll_conn_find_active_conn(i + 1)) {
-                        ble_hci_util_read_rssi(i+1, &rssi);
+                        ble_hs_hci_util_read_rssi(i+1, &rssi);
                     }
                 }
             }
@@ -989,7 +988,7 @@ bletest_task_handler(void *arg)
 #endif
 
     /* Get a random number */
-    rc = ble_hci_util_rand(&rand64, 8);
+    rc = ble_hs_hci_util_rand(&rand64, 8);
     assert(rc == 0);
 
     /* Wait some time before starting */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 5b1bc8c..bb88706 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -59,7 +59,7 @@
  */
 #include "../src/ble_hs_conn_priv.h"
 #include "../src/ble_hs_atomic_priv.h"
-#include "../src/ble_hci_priv.h"
+#include "../src/ble_hs_hci_priv.h"
 
 /* Nimble task priorities */
 #define BLE_LL_TASK_PRI         (OS_TASK_PRI_HIGHEST)
@@ -1408,7 +1408,7 @@ bletiny_datalen(uint16_t conn_handle, uint16_t tx_octets, uint16_t tx_time)
 {
     int rc;
 
-    rc = ble_hci_util_set_data_len(conn_handle, tx_octets, tx_time);
+    rc = ble_hs_hci_util_set_data_len(conn_handle, tx_octets, tx_time);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index 1da0ae8..75d9e16 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -30,7 +30,6 @@
 
 /* BLE */
 #include "nimble/ble.h"
-#include "host/host_hci.h"
 #include "host/ble_hs.h"
 #include "host/ble_hs_adv.h"
 #include "host/ble_uuid.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 4a707af..954d278 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -34,7 +34,6 @@
 #include "host/ble_sm.h"
 #include "host/ble_store.h"
 #include "host/ble_uuid.h"
-#include "host/host_hci.h"
 struct os_eventq;
 struct os_event;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/include/host/ble_hs_test.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs_test.h b/net/nimble/host/include/host/ble_hs_test.h
index 5d28291..3247b59 100644
--- a/net/nimble/host/include/host/ble_hs_test.h
+++ b/net/nimble/host/include/host/ble_hs_test.h
@@ -36,7 +36,7 @@ int ble_gatt_write_test_all(void);
 int ble_gatts_notify_test_all(void);
 int ble_gatts_read_test_suite(void);
 int ble_gatts_reg_test_all(void);
-int ble_host_hci_test_all(void);
+int ble_hs_hci_test_all(void);
 int ble_hs_adv_test_all(void);
 int ble_hs_conn_test_all(void);
 int ble_l2cap_test_all(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/include/host/host_hci.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/host_hci.h b/net/nimble/host/include/host/host_hci.h
deleted file mode 100644
index 79a0034..0000000
--- a/net/nimble/host/include/host/host_hci.h
+++ /dev/null
@@ -1,130 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_HOST_HCI_
-#define H_HOST_HCI_
-
-#include "nimble/hci_common.h"
-struct ble_hs_conn;
-struct os_mbuf;
-
-int host_hci_evt_process(uint8_t *data);
-uint16_t host_hci_opcode_join(uint8_t ogf, uint16_t ocf);
-void host_hci_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf);
-int host_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len,
-                      const void *cmddata);
-int host_hci_cmd_send_buf(void *cmddata);
-void host_hci_cmd_build_read_bd_addr(uint8_t *dst, int dst_len);
-void host_hci_cmd_build_set_event_mask(uint64_t event_mask,
-                                       uint8_t *dst, int dst_len);
-void host_hci_cmd_build_set_event_mask2(uint64_t event_mask, uint8_t *dst,
-                                        int dst_len);
-void host_hci_cmd_build_disconnect(uint16_t handle, uint8_t reason,
-                                   uint8_t *dst, int dst_len);
-int host_hci_cmd_disconnect(uint16_t handle, uint8_t reason);
-void host_hci_cmd_build_read_rssi(uint16_t handle, uint8_t *dst, int dst_len);
-int host_hci_cmd_read_rssi(uint16_t handle);
-int host_hci_cmd_build_le_set_scan_rsp_data(const uint8_t *data, uint8_t len,
-                                            uint8_t *dst, int dst_len);
-int host_hci_cmd_build_le_set_adv_data(const uint8_t *data, uint8_t len,
-                                       uint8_t *dst, int dst_len);
-int host_hci_cmd_build_le_set_adv_params(const struct hci_adv_params *adv,
-                                         uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_set_event_mask(uint64_t event_mask,
-                                          uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_read_buffer_size(uint8_t *dst, int dst_len);
-int host_hci_cmd_le_read_buffer_size(void);
-void host_hci_cmd_build_le_read_loc_supp_feat(uint8_t *dst, uint8_t dst_len);
-void host_hci_cmd_build_le_set_adv_enable(uint8_t enable, uint8_t *dst,
-                                          int dst_len);
-int host_hci_cmd_le_set_adv_enable(uint8_t enable);
-int host_hci_cmd_build_le_set_scan_params(uint8_t scan_type,
-                                          uint16_t scan_itvl,
-                                          uint16_t scan_window,
-                                          uint8_t own_addr_type,
-                                          uint8_t filter_policy,
-                                          uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_set_scan_enable(uint8_t enable,
-                                           uint8_t filter_dups,
-                                           uint8_t *dst, uint8_t dst_len);
-int host_hci_cmd_le_set_scan_enable(uint8_t enable, uint8_t filter_dups);
-int host_hci_cmd_build_le_create_connection(const struct hci_create_conn *hcc,
-                                            uint8_t *cmd, int cmd_len);
-int host_hci_cmd_le_create_connection(const struct hci_create_conn *hcc);
-void host_hci_cmd_build_le_clear_whitelist(uint8_t *dst, int dst_len);
-int host_hci_cmd_build_le_add_to_whitelist(const uint8_t *addr,
-                                           uint8_t addr_type,
-                                           uint8_t *dst, int dst_len);
-void host_hci_cmd_build_reset(uint8_t *dst, int dst_len);
-int host_hci_cmd_reset(void);
-void host_hci_cmd_build_read_adv_pwr(uint8_t *dst, int dst_len);
-int host_hci_cmd_read_adv_pwr(void);
-void host_hci_cmd_build_le_create_conn_cancel(uint8_t *dst, int dst_len);
-int host_hci_cmd_le_create_conn_cancel(void);
-int host_hci_cmd_build_le_conn_update(const struct hci_conn_update *hcu,
-                                      uint8_t *dst, int dst_len);
-int host_hci_cmd_le_conn_update(const struct hci_conn_update *hcu);
-void host_hci_cmd_build_le_lt_key_req_reply(
-    const struct hci_lt_key_req_reply *hkr, uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_lt_key_req_neg_reply(uint16_t conn_handle,
-                                                uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_conn_param_reply(
-    const struct hci_conn_param_reply *hcr, uint8_t *dst, int dst_len);
-int host_hci_cmd_le_conn_param_reply(const struct hci_conn_param_reply *hcr);
-void host_hci_cmd_build_le_conn_param_neg_reply(
-    const struct hci_conn_param_neg_reply *hcn, uint8_t *dst, int dst_len);
-int host_hci_cmd_le_conn_param_neg_reply(
-    const struct hci_conn_param_neg_reply *hcn);
-void host_hci_cmd_build_le_rand(uint8_t *dst, int dst_len);
-void host_hci_cmd_build_le_start_encrypt(const struct hci_start_encrypt *cmd,
-                                         uint8_t *dst, int dst_len);
-int host_hci_set_buf_size(uint16_t pktlen, uint8_t max_pkts);
-
-uint16_t host_hci_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc);
-
-int host_hci_data_rx(struct os_mbuf *om);
-int host_hci_data_tx(struct ble_hs_conn *connection, struct os_mbuf *txom);
-
-int host_hci_cmd_build_set_data_len(uint16_t connection_handle,
-                                    uint16_t tx_octets, uint16_t tx_time,
-                                    uint8_t *dst, int dst_len);
-int host_hci_cmd_build_add_to_resolv_list(
-    const struct hci_add_dev_to_resolving_list *padd,
-    uint8_t *dst, int dst_len);
-int host_hci_cmd_build_remove_from_resolv_list(
-    uint8_t addr_type, const uint8_t *addr, uint8_t *dst, int dst_len);
-int host_hci_cmd_build_read_resolv_list_size(uint8_t *dst, int dst_len);
-int host_hci_cmd_build_clear_resolv_list(uint8_t *dst, int dst_len);
-int host_hci_cmd_build_read_peer_resolv_addr(
-    uint8_t peer_identity_addr_type, const uint8_t *peer_identity_addr,
-    uint8_t *dst, int dst_len);
-int host_hci_cmd_build_read_lcl_resolv_addr(
-    uint8_t local_identity_addr_type, const uint8_t *local_identity_addr,
-    uint8_t *dst, int dst_len);
-int host_hci_cmd_build_set_addr_res_en(
-    uint8_t enable, uint8_t *dst, int dst_len);
-int host_hci_cmd_build_set_resolv_priv_addr_timeout(
-    uint16_t timeout, uint8_t *dst, int dst_len);
-
-void host_hci_timer_set(void);
-
-int host_hci_cmd_build_set_random_addr(const uint8_t *addr,
-                                       uint8_t *dst, int dst_len);
-
-#endif /* H_HOST_HCI_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_eddystone.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_eddystone.c b/net/nimble/host/src/ble_eddystone.c
index 5e7a3f0..2ca496f 100644
--- a/net/nimble/host/src/ble_eddystone.c
+++ b/net/nimble/host/src/ble_eddystone.c
@@ -186,7 +186,7 @@ ble_eddystone_set_adv_data_url(struct ble_hs_adv_fields *adv_fields,
 
     svc_data = ble_eddystone_set_svc_data_base(BLE_EDDYSTONE_FRAME_TYPE_URL);
     
-    rc = ble_hci_util_read_adv_tx_pwr(&tx_pwr);
+    rc = ble_hs_hci_util_read_adv_tx_pwr(&tx_pwr);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 1a41956..6086c50 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -23,7 +23,6 @@
 #include "bsp/bsp.h"
 #include "os/os.h"
 #include "nimble/nimble_opt.h"
-#include "host/host_hci.h"
 #include "host/ble_hs_adv.h"
 #include "ble_hs_priv.h"
 
@@ -1201,13 +1200,13 @@ ble_gap_wl_tx_add(const struct ble_gap_white_entry *entry)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_CHG_WHITE_LIST_LEN];
     int rc;
 
-    rc = host_hci_cmd_build_le_add_to_whitelist(entry->addr, entry->addr_type,
-                                                buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_add_to_whitelist(
+        entry->addr, entry->addr_type, buf, sizeof buf);
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1221,8 +1220,8 @@ ble_gap_wl_tx_clear(void)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN];
     int rc;
 
-    host_hci_cmd_build_le_clear_whitelist(buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_clear_whitelist(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1309,8 +1308,8 @@ ble_gap_adv_enable_tx(int enable)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_ADV_ENABLE_LEN];
     int rc;
 
-    host_hci_cmd_build_le_set_adv_enable(!!enable, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_set_adv_enable(!!enable, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1377,14 +1376,14 @@ ble_gap_adv_rsp_data_tx(void)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_RSP_DATA_LEN];
     int rc;
 
-    rc = host_hci_cmd_build_le_set_scan_rsp_data(ble_gap_slave.rsp_data,
-                                                 ble_gap_slave.rsp_data_len,
-                                                 buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_scan_rsp_data(ble_gap_slave.rsp_data,
+                                                   ble_gap_slave.rsp_data_len,
+                                                   buf, sizeof buf);
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1443,14 +1442,14 @@ ble_gap_adv_data_tx(void)
         ble_gap_slave.adv_auto_flags = 0;
     }
 
-    rc = host_hci_cmd_build_le_set_adv_data(ble_gap_slave.adv_data,
-                                            ble_gap_slave.adv_data_len,
-                                            buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_adv_data(ble_gap_slave.adv_data,
+                                              ble_gap_slave.adv_data_len,
+                                              buf, sizeof buf);
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1549,13 +1548,13 @@ ble_gap_adv_params_tx(uint8_t own_addr_type,
     hci_adv_params.adv_filter_policy = adv_params->filter_policy;
 
     hci_adv_params.adv_type = ble_gap_adv_type(adv_params);
-    rc = host_hci_cmd_build_le_set_adv_params(&hci_adv_params,
-                                              buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_adv_params(&hci_adv_params,
+                                                buf, sizeof buf);
     if (rc != 0) {
         return BLE_HS_EINVAL;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1881,9 +1880,9 @@ ble_gap_disc_enable_tx(int enable, int filter_duplicates)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_SCAN_ENABLE_LEN];
     int rc;
 
-    host_hci_cmd_build_le_set_scan_enable(!!enable, !!filter_duplicates,
-                                          buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_set_scan_enable(!!enable, !!filter_duplicates,
+                                            buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -1905,17 +1904,17 @@ ble_gap_disc_tx_params(uint8_t own_addr_type,
         scan_type = BLE_HCI_SCAN_TYPE_ACTIVE;
     }
 
-    rc = host_hci_cmd_build_le_set_scan_params(scan_type,
-                                               disc_params->itvl,
-                                               disc_params->window,
-                                               own_addr_type,
-                                               disc_params->filter_policy,
-                                               buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_set_scan_params(scan_type,
+                                                 disc_params->itvl,
+                                                 disc_params->window,
+                                                 own_addr_type,
+                                                 disc_params->filter_policy,
+                                                 buf, sizeof buf);
     if (rc != 0) {
         return BLE_HS_EINVAL;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2164,12 +2163,12 @@ ble_gap_conn_create_tx(uint8_t own_addr_type,
     hcc.min_ce_len = params->min_ce_len;
     hcc.max_ce_len = params->max_ce_len;
 
-    rc = host_hci_cmd_build_le_create_connection(&hcc, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_create_connection(&hcc, buf, sizeof buf);
     if (rc != 0) {
         return BLE_HS_EUNKNOWN;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2356,9 +2355,9 @@ ble_gap_terminate(uint16_t conn_handle, uint8_t hci_reason)
                      "conn_handle=%d hci_reason=%d\n",
                conn_handle, hci_reason);
 
-    host_hci_cmd_build_disconnect(conn_handle, hci_reason,
-                                  buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_disconnect(conn_handle, hci_reason,
+                                    buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         goto done;
     }
@@ -2384,8 +2383,8 @@ ble_gap_conn_cancel_tx(void)
     uint8_t buf[BLE_HCI_CMD_HDR_LEN];
     int rc;
 
-    host_hci_cmd_build_le_create_conn_cancel(buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_create_conn_cancel(buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2454,8 +2453,8 @@ ble_gap_tx_param_pos_reply(uint16_t conn_handle,
     pos_reply.min_ce_len = params->min_ce_len;
     pos_reply.max_ce_len = params->max_ce_len;
 
-    host_hci_cmd_build_le_conn_param_reply(&pos_reply, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_conn_param_reply(&pos_reply, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2473,8 +2472,8 @@ ble_gap_tx_param_neg_reply(uint16_t conn_handle, uint8_t reject_reason)
     neg_reply.handle = conn_handle;
     neg_reply.reason = reject_reason;
 
-    host_hci_cmd_build_le_conn_param_neg_reply(&neg_reply, buf, sizeof buf);
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    ble_hs_hci_cmd_build_le_conn_param_neg_reply(&neg_reply, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2551,12 +2550,12 @@ ble_gap_update_tx(uint16_t conn_handle,
     cmd.min_ce_len = params->min_ce_len;
     cmd.max_ce_len = params->max_ce_len;
 
-    rc = host_hci_cmd_build_le_conn_update(&cmd, buf, sizeof buf);
+    rc = ble_hs_hci_cmd_build_le_conn_update(&cmd, buf, sizeof buf);
     if (rc != 0) {
         return rc;
     }
 
-    rc = ble_hci_cmd_tx_empty_ack(buf);
+    rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc != 0) {
         return rc;
     }
@@ -2811,7 +2810,7 @@ ble_gap_conn_rssi(uint16_t conn_handle, int8_t *out_rssi)
 {
     int rc;
 
-    rc = ble_hci_util_read_rssi(conn_handle, out_rssi);
+    rc = ble_hs_hci_util_read_rssi(conn_handle, out_rssi);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_gap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_priv.h b/net/nimble/host/src/ble_gap_priv.h
index 3168d48..c2abbf3 100644
--- a/net/nimble/host/src/ble_gap_priv.h
+++ b/net/nimble/host/src/ble_gap_priv.h
@@ -28,7 +28,7 @@ struct hci_le_conn_param_req;
 struct hci_le_conn_complete;
 struct hci_disconn_complete;
 struct hci_encrypt_change;
-struct ble_hci_ack;
+struct ble_hs_hci_ack;
 struct ble_hs_adv;
 
 STATS_SECT_START(ble_gap_stats)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/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 49c432f..84ecff3 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3754,7 +3754,9 @@ ble_gattc_write_reliable_rx_prep(struct ble_gattc_proc *proc,
     }
 
     if (proc->write_reliable.cur_attr >= proc->write_reliable.num_attrs) {
-        /* Expecting an execute write response, not a prepare write response. */
+        /* Expecting an execute write response, not a prepare write
+         * response.
+         */
         rc = BLE_HS_EBADDATA;
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hci_cmd.c b/net/nimble/host/src/ble_hci_cmd.c
deleted file mode 100644
index 53fe726..0000000
--- a/net/nimble/host/src/ble_hci_cmd.c
+++ /dev/null
@@ -1,320 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#include <stdint.h>
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#include "os/os.h"
-#include "nimble/ble_hci_trans.h"
-#include "ble_hs_priv.h"
-#include "host_dbg_priv.h"
-
-#define BLE_HCI_CMD_TIMEOUT     (OS_TICKS_PER_SEC)
-
-static struct os_mutex ble_hci_cmd_mutex;
-static struct os_sem ble_hci_cmd_sem;
-
-static uint8_t *ble_hci_cmd_ack;
-
-#if PHONY_HCI_ACKS
-static ble_hci_cmd_phony_ack_fn *ble_hci_cmd_phony_ack_cb;
-#endif
-
-#if PHONY_HCI_ACKS
-void
-ble_hci_set_phony_ack_cb(ble_hci_cmd_phony_ack_fn *cb)
-{
-    ble_hci_cmd_phony_ack_cb = cb;
-}
-#endif
-
-static void
-ble_hci_cmd_lock(void)
-{
-    int rc;
-
-    rc = os_mutex_pend(&ble_hci_cmd_mutex, 0xffffffff);
-    BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-static void
-ble_hci_cmd_unlock(void)
-{
-    int rc;
-
-    rc = os_mutex_release(&ble_hci_cmd_mutex);
-    BLE_HS_DBG_ASSERT_EVAL(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-static int
-ble_hci_cmd_rx_cmd_complete(uint8_t event_code, uint8_t *data, int len,
-                            struct ble_hci_ack *out_ack)
-{
-    uint16_t opcode;
-    uint8_t *params;
-    uint8_t params_len;
-    uint8_t num_pkts;
-
-    if (len < BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    num_pkts = data[2];
-    opcode = le16toh(data + 3);
-    params = data + 5;
-
-    /* XXX: Process num_pkts field. */
-    (void)num_pkts;
-
-    out_ack->bha_opcode = opcode;
-
-    params_len = len - BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN;
-    if (params_len > 0) {
-        out_ack->bha_status = BLE_HS_HCI_ERR(params[0]);
-    } else if (opcode == BLE_HCI_OPCODE_NOP) {
-        out_ack->bha_status = 0;
-    } else {
-        out_ack->bha_status = BLE_HS_ECONTROLLER;
-    }
-
-    /* Don't include the status byte in the parameters blob. */
-    if (params_len > 1) {
-        out_ack->bha_params = params + 1;
-        out_ack->bha_params_len = params_len - 1;
-    } else {
-        out_ack->bha_params = NULL;
-        out_ack->bha_params_len = 0;
-    }
-
-    return 0;
-}
-
-static int
-ble_hci_cmd_rx_cmd_status(uint8_t event_code, uint8_t *data, int len,
-                          struct ble_hci_ack *out_ack)
-{
-    uint16_t opcode;
-    uint8_t num_pkts;
-    uint8_t status;
-
-    if (len < BLE_HCI_EVENT_CMD_STATUS_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    status = data[2];
-    num_pkts = data[3];
-    opcode = le16toh(data + 4);
-
-    /* XXX: Process num_pkts field. */
-    (void)num_pkts;
-
-    out_ack->bha_opcode = opcode;
-    out_ack->bha_params = NULL;
-    out_ack->bha_params_len = 0;
-    out_ack->bha_status = BLE_HS_HCI_ERR(status);
-
-    return 0;
-}
-
-static int
-ble_hci_cmd_process_ack(uint16_t expected_opcode,
-                        uint8_t *params_buf, uint8_t params_buf_len,
-                        struct ble_hci_ack *out_ack)
-{
-    uint8_t event_code;
-    uint8_t param_len;
-    uint8_t event_len;
-    int rc;
-
-    BLE_HS_DBG_ASSERT(ble_hci_cmd_ack != NULL);
-
-    /* Count events received */
-    STATS_INC(ble_hs_stats, hci_event);
-
-    /* Display to console */
-    host_hci_dbg_event_disp(ble_hci_cmd_ack);
-
-    event_code = ble_hci_cmd_ack[0];
-    param_len = ble_hci_cmd_ack[1];
-    event_len = param_len + 2;
-
-    /* Clear ack fields up front to silence spurious gcc warnings. */
-    memset(out_ack, 0, sizeof *out_ack);
-
-    switch (event_code) {
-    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
-        rc = ble_hci_cmd_rx_cmd_complete(event_code, ble_hci_cmd_ack,
-                                         event_len, out_ack);
-        break;
-
-    case BLE_HCI_EVCODE_COMMAND_STATUS:
-        rc = ble_hci_cmd_rx_cmd_status(event_code, ble_hci_cmd_ack,
-                                       event_len, out_ack);
-        break;
-
-    default:
-        BLE_HS_DBG_ASSERT(0);
-        rc = BLE_HS_EUNKNOWN;
-        break;
-    }
-
-    if (rc == 0) {
-        if (params_buf == NULL) {
-            out_ack->bha_params_len = 0;
-        } else {
-            if (out_ack->bha_params_len > params_buf_len) {
-                out_ack->bha_params_len = params_buf_len;
-                rc = BLE_HS_ECONTROLLER;
-            }
-            memcpy(params_buf, out_ack->bha_params, out_ack->bha_params_len);
-        }
-        out_ack->bha_params = params_buf;
-
-        if (out_ack->bha_opcode != expected_opcode) {
-            rc = BLE_HS_ECONTROLLER;
-        }
-    }
-
-    if (rc != 0) {
-        STATS_INC(ble_hs_stats, hci_invalid_ack);
-    }
-
-    return rc;
-}
-
-static int
-ble_hci_cmd_wait_for_ack(void)
-{
-    int rc;
-
-#if PHONY_HCI_ACKS
-    if (ble_hci_cmd_phony_ack_cb == NULL) {
-        rc = BLE_HS_ETIMEOUT_HCI;
-    } else {
-        ble_hci_cmd_ack =
-            ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
-        BLE_HS_DBG_ASSERT(ble_hci_cmd_ack != NULL);
-        rc = ble_hci_cmd_phony_ack_cb(ble_hci_cmd_ack, 260);
-    }
-#else
-    rc = os_sem_pend(&ble_hci_cmd_sem, BLE_HCI_CMD_TIMEOUT);
-    switch (rc) {
-    case 0:
-        BLE_HS_DBG_ASSERT(ble_hci_cmd_ack != NULL);
-        break;
-    case OS_TIMEOUT:
-        rc = BLE_HS_ETIMEOUT_HCI;
-        STATS_INC(ble_hs_stats, hci_timeout);
-        break;
-    default:
-        rc = BLE_HS_EOS;
-        break;
-    }
-#endif
-
-    return rc;
-}
-
-int
-ble_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_len,
-               uint8_t *out_evt_buf_len)
-{
-    struct ble_hci_ack ack;
-    uint16_t opcode;
-    int rc;
-
-    opcode = le16toh((uint8_t *)cmd);
-
-    BLE_HS_DBG_ASSERT(ble_hci_cmd_ack == NULL);
-    ble_hci_cmd_lock();
-
-    rc = host_hci_cmd_send_buf(cmd);
-    if (rc != 0) {
-        goto done;
-    }
-
-    rc = ble_hci_cmd_wait_for_ack();
-    if (rc != 0) {
-        ble_hs_sched_reset(rc);
-        goto done;
-    }
-
-    rc = ble_hci_cmd_process_ack(opcode, evt_buf, evt_buf_len, &ack);
-    if (rc != 0) {
-        ble_hs_sched_reset(rc);
-        goto done;
-    }
-
-    if (out_evt_buf_len != NULL) {
-        *out_evt_buf_len = ack.bha_params_len;
-    }
-
-    rc = ack.bha_status;
-
-done:
-    if (ble_hci_cmd_ack != NULL) {
-        ble_hci_trans_buf_free(ble_hci_cmd_ack);
-        ble_hci_cmd_ack = NULL;
-    }
-
-    ble_hci_cmd_unlock();
-    return rc;
-}
-
-int
-ble_hci_cmd_tx_empty_ack(void *cmd)
-{
-    int rc;
-
-    rc = ble_hci_cmd_tx(cmd, NULL, 0, NULL);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-void
-ble_hci_cmd_rx_ack(uint8_t *ack_ev)
-{
-    if (ble_hci_cmd_sem.sem_tokens != 0) {
-        /* This ack is unexpected; ignore it. */
-        ble_hci_trans_buf_free(ack_ev);
-        return;
-    }
-    BLE_HS_DBG_ASSERT(ble_hci_cmd_ack == NULL);
-
-    /* Unblock the application now that the HCI command buffer is populated
-     * with the acknowledgement.
-     */
-    ble_hci_cmd_ack = ack_ev;
-    os_sem_release(&ble_hci_cmd_sem);
-}
-
-void
-ble_hci_cmd_init(void)
-{
-    int rc;
-
-    rc = os_sem_init(&ble_hci_cmd_sem, 0);
-    BLE_HS_DBG_ASSERT_EVAL(rc == 0);
-
-    rc = os_mutex_init(&ble_hci_cmd_mutex);
-    BLE_HS_DBG_ASSERT_EVAL(rc == 0);
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hci_priv.h b/net/nimble/host/src/ble_hci_priv.h
deleted file mode 100644
index 7bc019f..0000000
--- a/net/nimble/host/src/ble_hci_priv.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_BLE_HCI_PRIV_
-#define H_BLE_HCI_PRIV_
-
-struct ble_hci_ack {
-    int bha_status;         /* A BLE_HS_E<...> error; NOT a naked HCI code. */
-    uint8_t *bha_params;
-    int bha_params_len;
-    uint16_t bha_opcode;
-    uint8_t bha_hci_handle;
-};
-
-int ble_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_len,
-                   uint8_t *out_evt_buf_len);
-int ble_hci_cmd_tx_empty_ack(void *cmd);
-void ble_hci_cmd_rx_ack(uint8_t *ack_ev);
-void ble_hci_cmd_init(void);
-
-#if PHONY_HCI_ACKS
-typedef int ble_hci_cmd_phony_ack_fn(uint8_t *ack, int ack_buf_len);
-void ble_hci_set_phony_ack_cb(ble_hci_cmd_phony_ack_fn *cb);
-#endif
-
-int ble_hci_util_read_adv_tx_pwr(int8_t *out_pwr);
-int ble_hci_util_rand(void *dst, int len);
-int ble_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi);
-int ble_hci_util_set_random_addr(const uint8_t *addr);
-int ble_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
-                              uint16_t tx_time);
-int ble_hci_util_data_hdr_strip(struct os_mbuf *om,
-                                struct hci_data_hdr *out_hdr);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hci_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hci_util.c b/net/nimble/host/src/ble_hci_util.c
deleted file mode 100644
index caf36e2..0000000
--- a/net/nimble/host/src/ble_hci_util.c
+++ /dev/null
@@ -1,178 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <string.h>
-#include "nimble/hci_common.h"
-#include "host/host_hci.h"
-#include "ble_hs_priv.h"
-
-int
-ble_hci_util_read_adv_tx_pwr(int8_t *out_tx_pwr)
-{
-    uint8_t buf[BLE_HCI_CMD_HDR_LEN];
-    uint8_t params_len;
-    int rc;
-
-    host_hci_cmd_build_read_adv_pwr(buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, out_tx_pwr, 1, &params_len);
-    if (rc != 0) {
-        return rc;
-    }
-
-    if (params_len != 1                     ||
-        *out_tx_pwr < BLE_HCI_ADV_CHAN_TXPWR_MIN ||
-        *out_tx_pwr > BLE_HCI_ADV_CHAN_TXPWR_MAX) {
-
-        return BLE_HS_ECONTROLLER;
-    }
-
-    return 0;
-}
-
-int
-ble_hci_util_rand(void *dst, int len)
-{
-    uint8_t rsp_buf[BLE_HCI_LE_RAND_LEN];
-    uint8_t req_buf[BLE_HCI_CMD_HDR_LEN];
-    uint8_t params_len;
-    uint8_t *u8ptr;
-    int chunk_sz;
-    int rc;
-
-    host_hci_cmd_build_le_rand(req_buf, sizeof req_buf);
-
-    u8ptr = dst;
-    while (len > 0) {
-        rc = ble_hci_cmd_tx(req_buf, rsp_buf, sizeof rsp_buf, &params_len);
-        if (rc != 0) {
-            return rc;
-        }
-        if (params_len != sizeof rsp_buf) {
-            return BLE_HS_ECONTROLLER;
-        }
-
-        chunk_sz = min(len, sizeof rsp_buf);
-        memcpy(u8ptr, rsp_buf, chunk_sz);
-
-        len -= chunk_sz;
-        u8ptr += chunk_sz;
-    }
-
-    return 0;
-}
-
-int
-ble_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi)
-{
-    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_READ_RSSI_LEN];
-    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
-    uint16_t params_conn_handle;
-    uint8_t params_len;
-    int rc;
-
-    host_hci_cmd_build_read_rssi(conn_handle, buf, sizeof buf);
-    rc = ble_hci_cmd_tx(buf, params, sizeof params, &params_len);
-    if (rc != 0) {
-        return rc;
-    }
-
-    if (params_len != BLE_HCI_READ_RSSI_ACK_PARAM_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    params_conn_handle = le16toh(params + 0);
-    if (params_conn_handle != conn_handle) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    *out_rssi = params[2];
-
-    return 0;
-}
-
-int
-ble_hci_util_set_random_addr(const uint8_t *addr)
-{
-    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN];
-    int rc;
-
-    /* set the address in the controller */
-
-    rc = host_hci_cmd_build_set_random_addr(addr, buf, sizeof(buf));
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = ble_hci_cmd_tx_empty_ack(buf);
-    return rc;
-}
-
-int
-ble_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
-                          uint16_t tx_time)
-{
-
-    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_DATALEN_LEN];
-    uint8_t params[BLE_HCI_SET_DATALEN_ACK_PARAM_LEN];
-    uint16_t params_conn_handle;
-    uint8_t params_len;
-    int rc;
-
-    rc = host_hci_cmd_build_set_data_len(conn_handle, tx_octets, tx_time, buf,
-                                         sizeof buf);
-    if (rc != 0) {
-        return BLE_HS_HCI_ERR(rc);
-    }
-
-    rc = ble_hci_cmd_tx(buf, params, BLE_HCI_SET_DATALEN_ACK_PARAM_LEN,
-                        &params_len);
-    if (rc != 0) {
-        return rc;
-    }
-
-    if (params_len != BLE_HCI_SET_DATALEN_ACK_PARAM_LEN) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    params_conn_handle = le16toh(params + 0);
-    if (params_conn_handle != conn_handle) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    return 0;
-}
-
-int
-ble_hci_util_data_hdr_strip(struct os_mbuf *om, struct hci_data_hdr *out_hdr)
-{
-    int rc;
-
-    rc = os_mbuf_copydata(om, 0, BLE_HCI_DATA_HDR_SZ, out_hdr);
-    if (rc != 0) {
-        return BLE_HS_ECONTROLLER;
-    }
-
-    /* Strip HCI ACL data header from the front of the packet. */
-    os_mbuf_adj(om, BLE_HCI_DATA_HDR_SZ);
-
-    out_hdr->hdh_handle_pb_bc = le16toh(&out_hdr->hdh_handle_pb_bc);
-    out_hdr->hdh_len = le16toh(&out_hdr->hdh_len);
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 95befc0..8035b49 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -24,7 +24,6 @@
 #include "util/tpq.h"
 #include "os/os.h"
 #include "nimble/ble_hci_trans.h"
-#include "host/host_hci.h"
 #include "ble_hs_priv.h"
 
 /**
@@ -173,7 +172,7 @@ ble_hs_process_rx_data_queue(void)
     struct os_mbuf *om;
 
     while ((om = os_mqueue_get(&ble_hs_rx_q)) != NULL) {
-        host_hci_acl_process(om);
+        ble_hs_hci_evt_acl_process(om);
     }
 }
 
@@ -370,7 +369,7 @@ ble_hs_event_handle(void *unused)
             rc = os_memblock_put(&ble_hs_hci_ev_pool, ev);
             BLE_HS_DBG_ASSERT_EVAL(rc == 0);
 
-            host_hci_evt_process(hci_evt);
+            ble_hs_hci_evt_process(hci_evt);
             break;
 
         case BLE_HS_EVENT_TX_NOTIFICATIONS:
@@ -579,7 +578,7 @@ ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
         goto err;
     }
 
-    ble_hci_cmd_init();
+    ble_hs_hci_init();
 
     rc = ble_hs_conn_init();
     if (rc != 0) {
@@ -642,7 +641,7 @@ ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
 #endif
 
     /* Configure the HCI transport to communicate with a host. */
-    ble_hci_trans_cfg_hs(host_hci_evt_rx, NULL, ble_hs_rx_data, NULL);
+    ble_hci_trans_cfg_hs(ble_hs_hci_rx_evt, NULL, ble_hs_rx_data, NULL);
 
     return 0;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
index 4fdc529..da0d5c1 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -214,7 +214,7 @@ ble_hs_adv_set_fields(const struct ble_hs_adv_fields *adv_fields,
          * the explicitly specified value.
          */
         if (adv_fields->tx_pwr_lvl == BLE_HS_ADV_TX_PWR_LVL_AUTO) {
-            rc = ble_hci_util_read_adv_tx_pwr(&tx_pwr_lvl);
+            rc = ble_hs_hci_util_read_adv_tx_pwr(&tx_pwr_lvl);
             if (rc != 0) {
                 return rc;
             }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 164cc6d..76196ef 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -20,7 +20,6 @@
 #include <string.h>
 #include <errno.h>
 #include "os/os.h"
-#include "host/host_hci.h"
 #include "host/ble_hs_id.h"
 #include "ble_hs_priv.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_dbg.c b/net/nimble/host/src/ble_hs_dbg.c
new file mode 100644
index 0000000..bb6ebdf
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_dbg.c
@@ -0,0 +1,509 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdint.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include "os/os.h"
+#include "console/console.h"
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "ble_hs_priv.h"
+
+static void
+ble_hs_dbg_le_event_disp(uint8_t subev, uint8_t len, uint8_t *evdata)
+{
+    int8_t rssi;
+    uint8_t advlen;
+    uint8_t status;
+    int i;
+    int imax;
+    uint8_t *dptr;
+    char *adv_ptr;
+    char adv_data_buf[32];
+
+    switch (subev) {
+    case BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE:
+    case BLE_HCI_LE_SUBEV_CONN_COMPLETE:
+        status = evdata[0];
+        if (status == BLE_ERR_SUCCESS) {
+            BLE_HS_LOG(DEBUG, "LE connection complete. handle=%u role=%u "
+                              "paddrtype=%u addr=%x.%x.%x.%x.%x.%x ",
+                       le16toh(evdata + 1), evdata[3], evdata[4],
+                       evdata[10], evdata[9], evdata[8], evdata[7],
+                       evdata[6], evdata[5]);
+
+            evdata += 11;
+            if (subev == BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE) {
+                BLE_HS_LOG(DEBUG, "local_rpa=%x.%x.%x.%x.%x.%x "
+                                   "peer_rpa=%x.%x.%x.%x.%x.%x ",
+                           evdata[5], evdata[4], evdata[3], evdata[2],
+                           evdata[1], evdata[0],
+                           evdata[11], evdata[10], evdata[9], evdata[8],
+                           evdata[7], evdata[6]);
+
+                evdata += 12;
+            }
+            BLE_HS_LOG(DEBUG, "itvl=%u latency=%u spvn_tmo=%u mca=%u\n",
+                       le16toh(evdata), le16toh(evdata + 2),
+                       le16toh(evdata + 4), evdata[6]);
+        } else {
+            BLE_HS_LOG(DEBUG, "LE connection complete. FAIL (status=%u)\n",
+                       status);
+        }
+        break;
+    case BLE_HCI_LE_SUBEV_ADV_RPT:
+        advlen = evdata[9];
+        rssi = evdata[10 + advlen];
+        BLE_HS_LOG(DEBUG, "LE advertising report. len=%u num=%u evtype=%u "
+                          "addrtype=%u addr=%x.%x.%x.%x.%x.%x advlen=%u "
+                          "rssi=%d\n", len, evdata[0], evdata[1], evdata[2],
+                   evdata[8], evdata[7], evdata[6], evdata[5],
+                   evdata[4], evdata[3], advlen, rssi);
+        if (advlen) {
+            dptr = &evdata[10];
+            while (advlen > 0) {
+                memset(adv_data_buf, 0, 32);
+                imax = advlen;
+                if (imax > 8) {
+                    imax = 8;
+                }
+                adv_ptr = &adv_data_buf[0];
+                for (i = 0; i < imax; ++i) {
+                    snprintf(adv_ptr, 4, "%02x ", *dptr);
+                    adv_ptr += 3;
+                    ++dptr;
+                }
+                advlen -= imax;
+                BLE_HS_LOG(DEBUG, "%s\n", adv_data_buf);
+            }
+        }
+        break;
+    case BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE:
+        status = evdata[0];
+        if (status == BLE_ERR_SUCCESS) {
+            BLE_HS_LOG(DEBUG, "LE Connection Update Complete. handle=%u "
+                              "itvl=%u latency=%u timeout=%u\n",
+                       le16toh(evdata + 1), le16toh(evdata + 3),
+                       le16toh(evdata + 5), le16toh(evdata + 7));
+        } else {
+            BLE_HS_LOG(DEBUG, "LE Connection Update Complete. FAIL "
+                              "(status=%u)\n", status);
+        }
+        break;
+
+    case BLE_HCI_LE_SUBEV_DATA_LEN_CHG:
+        BLE_HS_LOG(DEBUG, "LE Data Length Change. handle=%u max_tx_bytes=%u "
+                          "max_tx_time=%u max_rx_bytes=%u max_rx_time=%u\n",
+                   le16toh(evdata), le16toh(evdata + 2),
+                   le16toh(evdata + 4), le16toh(evdata + 6),
+                   le16toh(evdata + 8));
+        break;
+    case BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ:
+        BLE_HS_LOG(DEBUG, "LE Remote Connection Parameter Request. handle=%u "
+                          "min_itvl=%u max_itvl=%u latency=%u timeout=%u\n",
+                   le16toh(evdata), le16toh(evdata + 2),
+                   le16toh(evdata + 4), le16toh(evdata + 6),
+                   le16toh(evdata + 8));
+        break;
+
+    case BLE_HCI_LE_SUBEV_RD_REM_USED_FEAT:
+        status = evdata[0];
+        if (status == BLE_ERR_SUCCESS) {
+            BLE_HS_LOG(DEBUG, "LE Remote Used Features. handle=%u feat=",
+                       le16toh(evdata + 1));
+            for (i = 0; i < BLE_HCI_RD_LOC_SUPP_FEAT_RSPLEN; ++i) {
+                BLE_HS_LOG(DEBUG, "%02x ", evdata[3 + i]);
+            }
+            BLE_HS_LOG(DEBUG, "\n");
+        } else {
+            BLE_HS_LOG(DEBUG, "LE Remote Used Features. FAIL (status=%u)\n",
+                       status);
+        }
+        break;
+
+    case BLE_HCI_LE_SUBEV_LT_KEY_REQ:
+            BLE_HS_LOG(DEBUG, "LE LTK Req. handle=%u rand=%lx%lx encdiv=%u\n",
+                       le16toh(evdata), le32toh(evdata + 6),
+                       le32toh(evdata + 2), le16toh(evdata + 10));
+        break;
+
+    default:
+        BLE_HS_LOG(DEBUG, "\tUnknown LE event\n");
+        break;
+    }
+}
+
+/**
+ * Display a disconnection complete command.
+ *
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_disconn_comp_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t status;
+    uint8_t reason;
+    uint16_t handle;
+
+    status = evdata[0];
+    handle = le16toh(evdata + 1);
+    /* Ignore reason if status is not success */
+    if (status != BLE_ERR_SUCCESS) {
+        reason = 0;
+    } else {
+        reason = evdata[3];
+    }
+    BLE_HS_LOG(DEBUG, "Disconnection Complete: status=%u handle=%u "
+                      "reason=%u\n", status, handle, reason);
+}
+
+/**
+ * Display an encryption change event or encryption key refresh event
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_encrypt_chg_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t status;
+    uint8_t enabled;
+    uint16_t handle;
+
+    status = evdata[0];
+    handle = le16toh(evdata + 1);
+
+    /* Ignore reason if status is not success */
+    if (status != BLE_ERR_SUCCESS) {
+        enabled = 0;
+    } else {
+        enabled = evdata[3];
+    }
+    BLE_HS_LOG(DEBUG, "Encrypt change: status=%u handle=%u state=%u\n",
+               status, handle, enabled);
+}
+
+/**
+ * Display an encryption encryption key refresh event
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_encrypt_refresh_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t status;
+    uint16_t handle;
+
+    status = evdata[0];
+    handle = le16toh(evdata + 1);
+
+    BLE_HS_LOG(DEBUG, "Encrypt key refresh: status=%u handle=%u\n",
+               status, handle);
+}
+
+/**
+ * Display a version information event
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_rd_rem_ver_disp(uint8_t *evdata, uint8_t len)
+{
+    BLE_HS_LOG(DEBUG, "Remote Version Info: status=%u handle=%u vers_nr=%u "
+                      "compid=%u subver=%u\n",
+               evdata[0], le16toh(evdata + 1), evdata[3],
+               le16toh(evdata + 4), le16toh(evdata + 6));
+}
+
+/**
+ * Display the number of completed packets event
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_num_comp_pkts_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t handles;
+    uint8_t *handle_ptr;
+    uint8_t *pkt_ptr;
+    uint16_t handle;
+    uint16_t pkts;
+
+    handles = evdata[0];
+    if (len != ((handles * 4) + 1)) {
+        BLE_HS_LOG(DEBUG, "ERR: Number of Completed Packets bad length: "
+                          "num_handles=%u len=%u\n", handles, len);
+        return;
+
+    }
+
+    BLE_HS_LOG(DEBUG, "Number of Completed Packets: num_handles=%u\n",
+               handles);
+    if (handles) {
+        handle_ptr = evdata + 1;
+        pkt_ptr = handle_ptr + (2 * handles);
+        while (handles) {
+            handle = le16toh(handle_ptr);
+            handle_ptr += 2;
+            pkts = le16toh(pkt_ptr);
+            pkt_ptr += 2;
+            BLE_HS_LOG(DEBUG, "handle:%u pkts:%u\n", handle, pkts);
+            --handles;
+        }
+    }
+}
+
+/**
+ * Display the authenticated payload timeout event
+ *
+ * @param evdata
+ * @param len
+ */
+static void
+ble_hs_dbg_auth_pyld_tmo_disp(uint8_t *evdata, uint8_t len)
+{
+    uint16_t handle;
+
+    if (len != sizeof(uint16_t)) {
+        BLE_HS_LOG(DEBUG, "ERR: AuthPyldTmoEvent bad length %u\n", len);
+        return;
+
+    }
+
+    handle = le16toh(evdata);
+    BLE_HS_LOG(DEBUG, "AuthPyldTmo: handle=%u\n", handle);
+}
+
+
+static void
+ble_hs_dbg_cmd_comp_info_params(uint8_t status, uint8_t ocf, uint8_t *evdata)
+{
+    int i;
+    uint8_t *dptr;
+
+    if (status != BLE_ERR_SUCCESS) {
+        return;
+    }
+
+    switch (ocf) {
+    case BLE_HCI_OCF_IP_RD_LOCAL_VER:
+        BLE_HS_LOG(DEBUG, "hci_ver=%u hci_rev=%u lmp_ver=%u mfrg=%u "
+                          "lmp_subver=%u",
+                   evdata[0], le16toh(evdata + 1), evdata[3],
+                   le16toh(evdata + 4), le16toh(evdata + 6));
+        break;
+    case BLE_HCI_OCF_IP_RD_LOC_SUPP_CMD:
+        BLE_HS_LOG(DEBUG, "supp_cmds=");
+        dptr = evdata;
+        for (i = 0; i < 8; ++i) {
+            BLE_HS_LOG(DEBUG, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:",
+                       dptr[0], dptr[1], dptr[2], dptr[3],
+                       dptr[4], dptr[5], dptr[6], dptr[7]);
+            dptr += 8;
+        }
+        break;
+    case BLE_HCI_OCF_IP_RD_LOC_SUPP_FEAT:
+        BLE_HS_LOG(DEBUG, "supp_feat=0x%lx%08lx",
+                   le32toh(evdata + 4), le32toh(evdata));
+        break;
+    case BLE_HCI_OCF_IP_RD_BD_ADDR:
+        BLE_HS_LOG(DEBUG, "bd_addr=%x:%x:%x:%x:%x:%x",
+                   evdata[5], evdata[4], evdata[3],
+                   evdata[2], evdata[1], evdata[0]);
+        break;
+    default:
+        break;
+    }
+}
+
+static void
+ble_hs_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t cmd_pkts;
+    uint8_t ogf;
+    uint8_t ocf;
+    uint8_t status;
+    uint16_t opcode;
+
+    if (len < 3) {
+        BLE_HS_LOG(DEBUG, "Invalid command complete: len=%d "
+                          "(expected >= 3)", len);
+        goto done;
+    }
+
+    cmd_pkts = evdata[0];
+    opcode = le16toh(evdata + 1);
+    ogf = BLE_HCI_OGF(opcode);
+    ocf = BLE_HCI_OCF(opcode);
+
+    BLE_HS_LOG(DEBUG, "Command complete: cmd_pkts=%u ogf=0x%x ocf=0x%x",
+               cmd_pkts, ogf, ocf);
+
+    if (len == 3) {
+        goto done;
+    }
+
+    status = evdata[3];
+    BLE_HS_LOG(DEBUG, " status=%u ", status);
+
+    /* Move past header and status */
+    evdata += 4;
+
+    /* Display parameters based on command. */
+    switch (ogf) {
+    case BLE_HCI_OGF_INFO_PARAMS:
+        ble_hs_dbg_cmd_comp_info_params(status, ocf, evdata);
+        break;
+    case BLE_HCI_OGF_STATUS_PARAMS:
+        switch (ocf) {
+        case BLE_HCI_OCF_RD_RSSI:
+            BLE_HS_LOG(DEBUG, "handle=%u rssi=%d", le16toh(evdata),
+                       (int8_t)evdata[2]);
+            break;
+        default:
+            break;
+        }
+        break;
+    case BLE_HCI_OGF_LE:
+        switch (ocf) {
+        case BLE_HCI_OCF_LE_RD_CHAN_MAP:
+            BLE_HS_LOG(DEBUG, "handle=%u chanmap=%x.%x.%x.%x.%x",
+                       le16toh(evdata), evdata[2], evdata[3], evdata[4],
+                       evdata[5], evdata[6]);
+            break;
+        case BLE_HCI_OCF_LE_RD_MAX_DATA_LEN:
+            BLE_HS_LOG(DEBUG, "txoct=%u txtime=%u rxoct=%u rxtime=%u",
+                       le16toh(evdata), le16toh(evdata + 2),
+                       le16toh(evdata + 4), le16toh(evdata + 6));
+            break;
+        case BLE_HCI_OCF_LE_RD_SUPP_STATES:
+            BLE_HS_LOG(DEBUG, "states=0x%lx%08lx", le32toh(evdata + 4),
+                       le32toh(evdata));
+            break;
+        case BLE_HCI_OCF_LE_ENCRYPT:
+            BLE_HS_LOG(DEBUG, "encdata=0x%02x%02x%02x%02x%02x%02x%02x%02x",
+                       evdata[15], evdata[14], evdata[13], evdata[12],
+                       evdata[11], evdata[10], evdata[9], evdata[8]);
+            BLE_HS_LOG(DEBUG, "%02x%02x%02x%02x%02x%02x%02x%02x",
+                       evdata[7], evdata[6], evdata[5], evdata[4],
+                       evdata[3], evdata[2], evdata[1], evdata[0]);
+
+            break;
+        case BLE_HCI_OCF_LE_RAND:
+            BLE_HS_LOG(DEBUG, "rand=0x%02x%02x%02x%02x%02x%02x%02x%02x",
+                       evdata[0], evdata[1], evdata[2], evdata[3],
+                       evdata[4], evdata[5], evdata[6], evdata[7]);
+            break;
+        case BLE_HCI_OCF_LE_RD_SUGG_DEF_DATA_LEN:
+            BLE_HS_LOG(DEBUG, "txoct=%u txtime=%u", le16toh(evdata),
+                       le16toh(evdata + 2));
+            break;
+        case BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY:
+        case BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY:
+        case BLE_HCI_OCF_LE_SET_DATA_LEN:
+            BLE_HS_LOG(DEBUG, "handle=%u", le16toh(evdata));
+            break;
+        default:
+            break;
+        }
+        break;
+    default:
+        break;
+    }
+
+done:
+    BLE_HS_LOG(DEBUG, "\n");
+}
+
+static void
+ble_hs_dbg_cmd_status_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t ogf;
+    uint8_t ocf;
+    uint16_t opcode;
+
+    opcode = le16toh(evdata + 2);
+    ogf = BLE_HCI_OGF(opcode);
+    ocf = BLE_HCI_OCF(opcode);
+
+    BLE_HS_LOG(DEBUG, "Command Status: status=%u cmd_pkts=%u ocf=0x%x "
+                      "ogf=0x%x\n", evdata[0], evdata[1], ocf, ogf);
+}
+
+void
+ble_hs_dbg_event_disp(uint8_t *evbuf)
+{
+#if LOG_LEVEL > LOG_LEVEL_DEBUG
+    return;
+#endif
+
+    uint8_t *evdata;
+    uint8_t evcode;
+    uint8_t len;
+
+    /* Extract event code and length; move pointer to event parameter data */
+    evcode = evbuf[0];
+    len = evbuf[1];
+    evdata = evbuf + BLE_HCI_EVENT_HDR_LEN;
+
+    switch (evcode) {
+    case BLE_HCI_EVCODE_DISCONN_CMP:
+        ble_hs_dbg_disconn_comp_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_ENC_KEY_REFRESH:
+        ble_hs_dbg_encrypt_refresh_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_ENCRYPT_CHG:
+        ble_hs_dbg_encrypt_chg_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_RD_REM_VER_INFO_CMP:
+        ble_hs_dbg_rd_rem_ver_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_COMMAND_COMPLETE:
+        ble_hs_dbg_cmd_complete_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_COMMAND_STATUS:
+        ble_hs_dbg_cmd_status_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_NUM_COMP_PKTS:
+        ble_hs_dbg_num_comp_pkts_disp(evdata, len);
+        break;
+    case BLE_HCI_EVCODE_LE_META:
+        ble_hs_dbg_le_event_disp(evdata[0], len, evdata + 1);
+        break;
+    case BLE_HCI_EVCODE_AUTH_PYLD_TMO:
+        ble_hs_dbg_auth_pyld_tmo_disp(evdata, len);
+        break;
+    default:
+        BLE_HS_LOG(DEBUG, "Unknown event 0x%x len=%u\n", evcode, len);
+        break;
+    }
+}
+
+void
+ble_hs_dbg_set_sync_state(uint8_t sync_state)
+{
+    ble_hs_sync_state = sync_state;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/093cae09/net/nimble/host/src/ble_hs_dbg_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_dbg_priv.h b/net/nimble/host/src/ble_hs_dbg_priv.h
new file mode 100644
index 0000000..cf8d203
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_dbg_priv.h
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_HS_DBG_PRIV_
+#define H_BLE_HS_DBG_PRIV_
+
+void ble_hs_dbg_event_disp(uint8_t *evbuf);
+void ble_hs_dbg_set_sync_state(uint8_t sync_state);
+
+#endif /* H_HOST_DBG_ */