You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/08/04 02:59:25 UTC

[01/14] incubator-mynewt-core git commit: nrf52dk-uart - Clear state on open.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 21638d963 -> 29b69ddf2


nrf52dk-uart - Clear state on open.


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

Branch: refs/heads/develop
Commit: 3d3f81db963be5e09184e8b6ad4a8d96ebae14c7
Parents: 93c01c5
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 2 09:58:49 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:15 2016 -0700

----------------------------------------------------------------------
 hw/mcu/nordic/nrf52xxx/src/hal_uart.c | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3d3f81db/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_uart.c b/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
index ad1700c..eb9cc32 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_uart.c
@@ -316,6 +316,8 @@ hal_uart_config(int port, int32_t baudrate, uint8_t databits, uint8_t stopbits,
     NRF_UARTE0->RXD.MAXCNT = sizeof(u->u_rx_buf);
     NRF_UARTE0->TASKS_STARTRX = 1;
 
+    u->u_rx_stall = 0;
+    u->u_tx_started = 0;
     u->u_open = 1;
 
     return 0;


[11/14] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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);


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

Posted by cc...@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/29b69ddf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/29b69ddf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/29b69ddf

Branch: refs/heads/develop
Commit: 29b69ddf240d49955990a405211b7cf8f48fc585
Parents: d842a43
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 19:43:50 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 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/29b69ddf/apps/blecent/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/blecent/pkg.yml b/apps/blecent/pkg.yml
index b1710aa..3798fee 100644
--- a/apps/blecent/pkg.yml
+++ b/apps/blecent/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - net/nimble/host
     - net/nimble/host/services/mandatory
     - 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/29b69ddf/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index 8f10814..66a4e3f 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/29b69ddf/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/29b69ddf/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/29b69ddf/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index ec75359..7c45ee2 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - net/nimble/host
     - net/nimble/host/services/mandatory
     - 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/29b69ddf/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 0e6ec14..cff4763 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/mandatory/ble_svc_gap.h"
 #include "services/mandatory/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/29b69ddf/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/29b69ddf/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/29b69ddf/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/29b69ddf/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index a3c6ee7..625adc6 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - net/nimble/host
     - net/nimble/host/services/mandatory
     - 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/29b69ddf/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 2f3c474..38ee467 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/29b69ddf/apps/bleuart/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleuart/pkg.yml b/apps/bleuart/pkg.yml
index a79077f..c93011b 100644
--- a/apps/bleuart/pkg.yml
+++ b/apps/bleuart/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - net/nimble/host
     - net/nimble/host/services/mandatory
     - 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/29b69ddf/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index 0a5dd82..f7c7714 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/14] incubator-mynewt-core git commit: BLE Host - Use HCI transport API

Posted by cc...@apache.org.
BLE Host - Use HCI transport API

Prior to this change, the host assumed it was running with a combined
host-controller setup.  Now it uses the available HCI transport instead.


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

Branch: refs/heads/develop
Commit: 2444d8f66060385b3b3dce29b1b7ff09cacdf9eb
Parents: c970865
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 19:44:39 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 2016 -0700

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll.h      |   1 +
 net/nimble/controller/pkg.yml                   |   6 +-
 net/nimble/controller/src/ble_ll.c              |  17 +
 net/nimble/controller/src/ble_ll_conn.c         |   6 +-
 net/nimble/controller/src/ble_ll_conn_hci.c     |   9 +-
 net/nimble/controller/src/ble_ll_conn_priv.h    |   7 +
 net/nimble/controller/src/ble_ll_hci.c          |  14 +-
 net/nimble/controller/src/ble_ll_hci_ev.c       |  15 +-
 net/nimble/controller/src/ble_ll_scan.c         |   3 +-
 net/nimble/host/include/host/ble_hs.h           |  28 +-
 net/nimble/host/include/host/ble_hs_test.h      |   3 -
 net/nimble/host/include/host/host_hci.h         |   8 +-
 net/nimble/host/pkg.yml                         |  15 +-
 net/nimble/host/src/ble_gap.c                   |   2 +-
 net/nimble/host/src/ble_gap_priv.h              |   2 +-
 net/nimble/host/src/ble_hci_cmd.c               |  58 +-
 net/nimble/host/src/ble_hs.c                    | 224 ++++--
 net/nimble/host/src/ble_hs_atomic.c             |  20 +
 net/nimble/host/src/ble_hs_atomic_priv.h        |   1 +
 net/nimble/host/src/ble_hs_priv.h               |  16 +
 net/nimble/host/src/ble_hs_pvcy.c               |  27 +-
 net/nimble/host/src/ble_hs_pvcy_priv.h          |   2 +-
 net/nimble/host/src/ble_hs_startup.c            |   4 -
 net/nimble/host/src/host_dbg.c                  |   2 +-
 net/nimble/host/src/host_hci.c                  |  49 +-
 net/nimble/host/src/host_hci_cmd.c              |  52 +-
 net/nimble/host/src/test/ble_gap_test.c         |  11 +-
 net/nimble/host/src/test/ble_host_hci_test.c    |   8 +-
 net/nimble/host/src/test/ble_hs_test.c          |  16 -
 net/nimble/host/src/test/ble_hs_test_util.c     |  96 ++-
 net/nimble/host/src/test/ble_hs_test_util.h     |   2 +
 net/nimble/host/src/test/ble_os_test.c          |   2 +-
 net/nimble/host/src/test/ble_sm_test_util.c     |  20 +-
 net/nimble/include/nimble/ble.h                 |   4 -
 net/nimble/include/nimble/ble_hci_trans.h       | 152 ++++
 net/nimble/include/nimble/hci_transport.h       |  32 -
 .../ram/include/transport/ram/ble_hci_ram.h     |  15 +
 net/nimble/transport/ram/pkg.yml                |  33 +
 net/nimble/transport/ram/src/ble_hci_ram.c      | 190 +++++
 .../uart/include/transport/uart/ble_hci_uart.h  |  19 +
 net/nimble/transport/uart/pkg.yml               |  34 +
 net/nimble/transport/uart/src/ble_hci_uart.c    | 775 +++++++++++++++++++
 42 files changed, 1731 insertions(+), 269 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index 81cba53..2be8425 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -22,6 +22,7 @@
 
 #include "stats/stats.h"
 #include "hal/hal_cputime.h"
+#include "os/os_eventq.h"
 #include "nimble/nimble_opt.h"
 
 /* Controller revision. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/controller/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml
index 07a9d56..8e52aba 100644
--- a/net/nimble/controller/pkg.yml
+++ b/net/nimble/controller/pkg.yml
@@ -25,10 +25,14 @@ pkg.keywords:
     - ble
     - bluetooth
 
-pkg.req_apis: ble_driver
+pkg.req_apis:
+    - ble_driver
+    - ble_transport
+
 pkg.deps:
     - libs/os
     - sys/stats
     - net/nimble
+
 pkg.features:
     - BLE_DEVICE

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
index 3a09e3e..1a4f558 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -26,6 +26,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
 #include "controller/ble_hw.h"
 #include "controller/ble_phy.h"
 #include "controller/ble_ll.h"
@@ -182,6 +183,9 @@ STATS_NAME_END(ble_ll_stats)
 struct os_task g_ble_ll_task;
 os_stack_t g_ble_ll_stack[BLE_LL_STACK_SIZE];
 
+struct os_mempool g_ble_ll_hci_ev_pool;
+static void *ble_ll_hci_os_event_buf;
+
 /* XXX: temporary logging until we transition to real logging */
 #ifdef BLE_LL_LOG
 struct ble_ll_log
@@ -1129,6 +1133,16 @@ ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
     cputime_timer_init(&g_ble_ll_data.ll_wfr_timer, ble_ll_wfr_timer_exp,
                        NULL);
 
+    ble_ll_hci_os_event_buf = malloc(
+        OS_MEMPOOL_BYTES(16, sizeof (struct os_event)));
+    assert(ble_ll_hci_os_event_buf != NULL);
+
+    /* Create memory pool of OS events */
+    rc = os_mempool_init(&g_ble_ll_hci_ev_pool, 16,
+                         sizeof (struct os_event), ble_ll_hci_os_event_buf,
+                         "g_ble_ll_hci_ev_pool");
+    assert(rc == 0);
+
     /* Initialize LL HCI */
     ble_ll_hci_init();
 
@@ -1182,6 +1196,9 @@ ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
                             STATS_SIZE_INIT_PARMS(ble_ll_stats, STATS_SIZE_32),
                             STATS_NAME_INIT_PARMS(ble_ll_stats),
                             "ble_ll");
+
+    ble_hci_trans_cfg_ll(ble_ll_hci_cmd_rx, NULL,
+                                    ble_ll_hci_acl_rx, NULL);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 f711dfb..cb0f89b 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -25,6 +25,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
 #include "ble/xcvr.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
@@ -103,9 +104,6 @@ extern void bletest_completed_pkt(uint16_t handle);
  *  are hosed. Well, anchor point can get really messed up!
  */
 
-/* XXX: this does not belong here! Move to transport? */
-extern int ble_hs_rx_data(struct os_mbuf *om);
-
 /*
  * The amount of time that we will wait to hear the start of a receive
  * packet after we have transmitted a packet. This time is at least
@@ -2569,7 +2567,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                     acl_hdr = (acl_hdr << 12) | connsm->conn_handle;
                     htole16(rxbuf, acl_hdr);
                     htole16(rxbuf + 2, acl_len);
-                    ble_hs_rx_data(rxpdu);
+                    ble_hci_trans_ll_acl_tx(rxpdu);
                 }
 
                 /* NOTE: we dont free the mbuf since we handed it off! */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 1d6102e..f653ca6 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -25,6 +25,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_conn.h"
@@ -140,7 +141,7 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status)
     enh_enabled = ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE);
 
     if (enabled || enh_enabled) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             /* Put common elements in event */
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
@@ -246,7 +247,7 @@ ble_ll_conn_num_comp_pkts_event_send(void)
             (connsm->completed_pkts || !STAILQ_EMPTY(&connsm->conn_txq))) {
             /* If no buffer, get one, If cant get one, leave. */
             if (!evbuf) {
-                evbuf = os_memblock_get(&g_hci_evt_pool);
+                evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
                 if (!evbuf) {
                     break;
                 }
@@ -313,7 +314,7 @@ ble_ll_auth_pyld_tmo_event_send(struct ble_ll_conn_sm *connsm)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_event_enabled(BLE_HCI_EVCODE_AUTH_PYLD_TMO)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_AUTH_PYLD_TMO;
             evbuf[1] = sizeof(uint16_t);
@@ -338,7 +339,7 @@ ble_ll_disconn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t reason)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_event_enabled(BLE_HCI_EVCODE_DISCONN_CMP)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_DISCONN_CMP;
             evbuf[1] = BLE_HCI_EVENT_DISCONN_COMPLETE_LEN;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/controller/src/ble_ll_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_priv.h b/net/nimble/controller/src/ble_ll_conn_priv.h
index 218ecbc..b855b22 100644
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ b/net/nimble/controller/src/ble_ll_conn_priv.h
@@ -81,6 +81,9 @@ extern struct ble_ll_conn_free_list g_ble_ll_conn_free_list;
 /* Pointer to connection state machine we are trying to create */
 extern struct ble_ll_conn_sm *g_ble_ll_conn_create_sm;
 
+extern struct os_mempool g_ble_ll_hci_ev_pool;
+
+
 /* Generic interface */
 struct ble_ll_len_req;
 struct hci_create_conn;
@@ -150,4 +153,8 @@ void ble_ll_conn_auth_pyld_timer_start(struct ble_ll_conn_sm *connsm);
 #else
 #define ble_ll_conn_auth_pyld_timer_start(x)
 #endif
+
+int ble_ll_hci_cmd_rx(uint8_t *cmd, void *arg);
+int ble_ll_hci_acl_rx(struct os_mbuf *om, void *arg);
+
 #endif /* H_BLE_LL_CONN_PRIV_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 2889367..352dda2 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -23,7 +23,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "controller/ble_hw.h"
 #include "controller/ble_ll_adv.h"
 #include "controller/ble_ll_scan.h"
@@ -68,7 +68,7 @@ ble_ll_hci_event_send(uint8_t *evbuf)
     STATS_INC(ble_ll_stats, hci_events_sent);
 
     /* Send the event to the host */
-    rc = ble_hci_transport_ctlr_event_send(evbuf);
+    rc = ble_hci_trans_ll_evt_tx(evbuf);
 
     return rc;
 }
@@ -86,7 +86,7 @@ ble_ll_hci_send_noop(void)
     uint8_t *evbuf;
     uint16_t opcode;
 
-    evbuf = os_memblock_get(&g_hci_evt_pool);
+    evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
     if (evbuf) {
         /* Create a command complete event with a NO-OP opcode */
         opcode = 0;
@@ -919,7 +919,7 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
     assert(cmdbuf != NULL);
 
     /* Free the event */
-    err = os_memblock_put(&g_hci_os_event_pool, ev);
+    err = os_memblock_put(&g_ble_ll_hci_ev_pool, ev);
     assert(err == OS_OK);
 
     /* Get the opcode from the command buffer */
@@ -994,12 +994,12 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
  *                              BLE_ERR_MEM_CAPACITY on HCI buffer exhaustion.
  */
 int
-ble_hci_transport_host_cmd_send(uint8_t *cmd)
+ble_ll_hci_cmd_rx(uint8_t *cmd, void *arg)
 {
     struct os_event *ev;
 
     /* Get an event structure off the queue */
-    ev = (struct os_event *)os_memblock_get(&g_hci_os_event_pool);
+    ev = (struct os_event *)os_memblock_get(&g_ble_ll_hci_ev_pool);
     if (!ev) {
         return BLE_ERR_MEM_CAPACITY;
     }
@@ -1015,7 +1015,7 @@ ble_hci_transport_host_cmd_send(uint8_t *cmd)
 
 /* Send ACL data from host to contoller */
 int
-ble_hci_transport_host_acl_data_send(struct os_mbuf *om)
+ble_ll_hci_acl_rx(struct os_mbuf *om, void *arg)
 {
     ble_ll_acl_data_in(om);
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/controller/src/ble_ll_hci_ev.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci_ev.c b/net/nimble/controller/src/ble_ll_hci_ev.c
index 5624c65..2548bdc 100644
--- a/net/nimble/controller/src/ble_ll_hci_ev.c
+++ b/net/nimble/controller/src/ble_ll_hci_ev.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
 #include "controller/ble_ll.h"
 #include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_ctrl.h"
@@ -41,7 +42,7 @@ ble_ll_hci_ev_datalen_chg(struct ble_ll_conn_sm *connsm)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_DATA_LEN_CHG)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = BLE_HCI_LE_DATA_LEN_CHG_LEN;
@@ -68,7 +69,7 @@ ble_ll_hci_ev_rem_conn_parm_req(struct ble_ll_conn_sm *connsm,
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = BLE_HCI_LE_REM_CONN_PARM_REQ_LEN;
@@ -95,7 +96,7 @@ ble_ll_hci_ev_conn_update(struct ble_ll_conn_sm *connsm, uint8_t status)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = BLE_HCI_LE_CONN_UPD_LEN;
@@ -127,7 +128,7 @@ ble_ll_hci_ev_encrypt_chg(struct ble_ll_conn_sm *connsm, uint8_t status)
     }
 
     if (ble_ll_hci_is_event_enabled(evcode)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = evcode;
             evbuf[1] = evlen;
@@ -158,7 +159,7 @@ ble_ll_hci_ev_ltk_req(struct ble_ll_conn_sm *connsm)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_LT_KEY_REQ)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = BLE_HCI_LE_LT_KEY_REQ_LEN;
@@ -188,7 +189,7 @@ ble_ll_hci_ev_rd_rem_used_feat(struct ble_ll_conn_sm *connsm, uint8_t status)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_RD_REM_USED_FEAT)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = BLE_HCI_LE_RD_REM_USED_FEAT_LEN;
@@ -208,7 +209,7 @@ ble_ll_hci_ev_rd_rem_ver(struct ble_ll_conn_sm *connsm, uint8_t status)
     uint8_t *evbuf;
 
     if (ble_ll_hci_is_event_enabled(BLE_HCI_EVCODE_RD_REM_VER_INFO_CMP)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_RD_REM_VER_INFO_CMP;
             evbuf[1] = BLE_HCI_EVENT_RD_RM_VER_LEN;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 c926846..64c3990 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -25,6 +25,7 @@
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
 #include "controller/ble_phy.h"
 #include "controller/ble_hw.h"
 #include "controller/ble_ll.h"
@@ -424,7 +425,7 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t txadd, uint8_t *rxbuf,
     }
 
     if (ble_ll_hci_is_le_event_enabled(subev)) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
         if (evbuf) {
             evbuf[0] = BLE_HCI_EVCODE_LE_META;
             evbuf[1] = event_len;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 5cdf863..faeaf14 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -21,6 +21,7 @@
 #define H_BLE_HS_
 
 #include <inttypes.h>
+#include "nimble/hci_common.h"
 #include "host/ble_att.h"
 #include "host/ble_gap.h"
 #include "host/ble_gatt.h"
@@ -62,6 +63,7 @@ struct os_event;
 #define BLE_HS_ETIMEOUT_HCI         19
 #define BLE_HS_ENOMEM_EVT           20
 #define BLE_HS_ENOADDR              21
+#define BLE_HS_ENOTSYNCED           22
 
 #define BLE_HS_ERR_ATT_BASE         0x100   /* 256 */
 #define BLE_HS_ATT_ERR(x)           ((x) ? BLE_HS_ERR_ATT_BASE + (x) : 0)
@@ -85,8 +87,10 @@ struct os_event;
 #define BLE_HS_IO_NO_INPUT_OUTPUT           0x03
 #define BLE_HS_IO_KEYBOARD_DISPLAY          0x04
 
+typedef void ble_hs_reset_fn(int reason);
+typedef void ble_hs_sync_fn(void);
+
 struct ble_hs_cfg {
-    /*** HCI settings. */
     /**
      * An HCI buffer is a "flat" 260-byte buffer.  HCI buffers are used by the
      * controller to send unsolicited events to the host.
@@ -105,6 +109,10 @@ struct ble_hs_cfg {
      * 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).
+     */
     uint8_t max_hci_bufs;
 
     /*** Connection settings. */
@@ -211,6 +219,19 @@ struct ble_hs_cfg {
     uint8_t sm_our_key_dist;
     uint8_t sm_their_key_dist;
 
+    /*** HCI settings */
+    /**
+     * This callback is executed when the host resets itself and the controller
+     * due to fatal error.
+     */
+    ble_hs_reset_fn *reset_cb;
+
+    /**
+     * This callback is executed when the host and controller become synced.
+     * This happens at startup and after a reset.
+     */
+    ble_hs_sync_fn *sync_cb;
+
     /*** Store settings. */
     /**
      * These function callbacks handle persistence of sercurity material
@@ -220,7 +241,7 @@ struct ble_hs_cfg {
     ble_store_write_fn *store_write_cb;
     ble_store_delete_fn *store_delete_cb;
 
-    /*** privacy settings */
+    /*** Privacy settings. */
     /**
      * The frequency at which new resovlable private addresses are generated.
      * Units are seconds.
@@ -230,9 +251,8 @@ struct ble_hs_cfg {
 
 extern const struct ble_hs_cfg ble_hs_cfg_dflt;
 
-int ble_hs_rx_data(struct os_mbuf *om);
+int ble_hs_synced(void);
 int ble_hs_start(void);
-void ble_hs_event_enqueue(struct os_event *ev);
 int ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 a883018..5d28291 100644
--- a/net/nimble/host/include/host/ble_hs_test.h
+++ b/net/nimble/host/include/host/ble_hs_test.h
@@ -23,9 +23,6 @@
 #include <inttypes.h>
 struct os_mbuf;
 
-void ble_hs_test_pkt_txed(struct os_mbuf *om);
-void ble_hs_test_hci_txed(uint8_t *cmdbuf);
-
 int ble_att_clt_test_all(void);
 int ble_att_svr_test_all(void);
 int ble_gap_test_all(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
index dbe626a..79a0034 100644
--- a/net/nimble/host/include/host/host_hci.h
+++ b/net/nimble/host/include/host/host_hci.h
@@ -24,13 +24,7 @@
 struct ble_hs_conn;
 struct os_mbuf;
 
-#define HCI_CMD_BUF_SIZE        260
-#define HCI_EVT_BUF_SIZE        260
-
-extern uint8_t host_hci_cmd_buf[HCI_CMD_BUF_SIZE];
-
-int host_hci_os_event_proc(struct os_event *ev);
-int host_hci_event_rx(uint8_t *data);
+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,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/host/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/pkg.yml b/net/nimble/host/pkg.yml
index 0edea58..a45ba1e 100644
--- a/net/nimble/host/pkg.yml
+++ b/net/nimble/host/pkg.yml
@@ -36,20 +36,23 @@ pkg.deps:
     # Tinycrypt is only required when secure connections (NIMBPLE_OPT_SM_SC)
     # is enabled.  It always gets built as a dependency, but not is not
     # included by the linker unless SC is enabled.  XXX: We should not build
-    # this library if it is not requiresd.
+    # this library if it is not required.
     - libs/tinycrypt
 
 pkg.req_apis:
+    - ble_transport
     - console
 
 pkg.features:
     - BLE_HOST
 
 # Satisfy capability dependencies for the self-contained test executable.
-pkg.deps.SELFTEST: libs/console/stub
+pkg.deps.SELFTEST:
+    - libs/console/stub
+    - net/nimble/transport/ram
+
 pkg.cflags.SELFTEST:
-    - -DPHONY_TRANSPORT=1
-    - -DPHONY_HCI_ACKS=1
-    - -DNIMBLE_OPT_SM=1
-    - -DNIMBLE_OPT_SM_SC=1
+    - "-DPHONY_HCI_ACKS=1"
+    - "-DNIMBLE_OPT_SM=1"
+    - "-DNIMBLE_OPT_SM_SC=1"
 pkg.cflags.TEST: -DBLE_HS_DEBUG

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 a001ae3..1a41956 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -708,7 +708,7 @@ ble_gap_update_failed(uint16_t conn_handle, int status)
     ble_gap_update_notify(conn_handle, status);
 }
 
-static void
+void
 ble_gap_conn_broken(uint16_t conn_handle, int reason)
 {
     struct ble_gap_snapshot snap;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 449a4b0..3168d48 100644
--- a/net/nimble/host/src/ble_gap_priv.h
+++ b/net/nimble/host/src/ble_gap_priv.h
@@ -70,7 +70,6 @@ extern STATS_SECT_DECL(ble_gap_stats) ble_gap_stats;
 #define BLE_GAP_CONN_MODE_MAX               3
 #define BLE_GAP_DISC_MODE_MAX               3
 
-int ble_gap_locked_by_cur_task(void);
 void ble_gap_rx_adv_report(struct ble_gap_disc_desc *desc);
 int ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt);
 void ble_gap_rx_disconn_complete(struct hci_disconn_complete *evt);
@@ -92,6 +91,7 @@ void ble_gap_subscribe_event(uint16_t conn_handle, uint16_t attr_handle,
                              uint8_t prev_indicate, uint8_t cur_indicate);
 int ble_gap_master_in_progress(void);
 
+void ble_gap_conn_broken(uint16_t conn_handle, int reason);
 int32_t ble_gap_heartbeat(void);
 
 int ble_gap_init(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
index 23eaba3..53fe726 100644
--- a/net/nimble/host/src/ble_hci_cmd.c
+++ b/net/nimble/host/src/ble_hci_cmd.c
@@ -21,6 +21,7 @@
 #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"
 
@@ -29,6 +30,8 @@
 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
@@ -69,7 +72,6 @@ ble_hci_cmd_rx_cmd_complete(uint8_t event_code, uint8_t *data, int len,
     uint8_t num_pkts;
 
     if (len < BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN) {
-        /* XXX: Increment stat. */
         return BLE_HS_ECONTROLLER;
     }
 
@@ -112,7 +114,6 @@ ble_hci_cmd_rx_cmd_status(uint8_t event_code, uint8_t *data, int len,
     uint8_t status;
 
     if (len < BLE_HCI_EVENT_CMD_STATUS_LEN) {
-        /* XXX: Increment stat. */
         return BLE_HS_ECONTROLLER;
     }
 
@@ -132,7 +133,8 @@ ble_hci_cmd_rx_cmd_status(uint8_t event_code, uint8_t *data, int len,
 }
 
 static int
-ble_hci_cmd_process_ack(uint8_t *params_buf, uint8_t params_buf_len,
+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;
@@ -140,20 +142,16 @@ ble_hci_cmd_process_ack(uint8_t *params_buf, uint8_t params_buf_len,
     uint8_t event_len;
     int rc;
 
-    /***
-     * The controller always reuses the command buffer for its acknowledgement
-     * events.  This function processes the acknowledgement event contained in
-     * the command buffer.
-     */
+    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(host_hci_cmd_buf);
+    host_hci_dbg_event_disp(ble_hci_cmd_ack);
 
-    event_code = host_hci_cmd_buf[0];
-    param_len = host_hci_cmd_buf[1];
+    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. */
@@ -161,12 +159,12 @@ ble_hci_cmd_process_ack(uint8_t *params_buf, uint8_t params_buf_len,
 
     switch (event_code) {
     case BLE_HCI_EVCODE_COMMAND_COMPLETE:
-        rc = ble_hci_cmd_rx_cmd_complete(event_code, host_hci_cmd_buf,
+        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, host_hci_cmd_buf,
+        rc = ble_hci_cmd_rx_cmd_status(event_code, ble_hci_cmd_ack,
                                        event_len, out_ack);
         break;
 
@@ -187,6 +185,14 @@ ble_hci_cmd_process_ack(uint8_t *params_buf, uint8_t params_buf_len,
             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;
@@ -201,15 +207,20 @@ ble_hci_cmd_wait_for_ack(void)
     if (ble_hci_cmd_phony_ack_cb == NULL) {
         rc = BLE_HS_ETIMEOUT_HCI;
     } else {
-        rc = ble_hci_cmd_phony_ack_cb(host_hci_cmd_buf, 260);
+        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;
@@ -225,8 +236,12 @@ 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);
@@ -236,11 +251,13 @@ ble_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_len,
 
     rc = ble_hci_cmd_wait_for_ack();
     if (rc != 0) {
+        ble_hs_sched_reset(rc);
         goto done;
     }
 
-    rc = ble_hci_cmd_process_ack(evt_buf, evt_buf_len, &ack);
+    rc = ble_hci_cmd_process_ack(opcode, evt_buf, evt_buf_len, &ack);
     if (rc != 0) {
+        ble_hs_sched_reset(rc);
         goto done;
     }
 
@@ -251,6 +268,11 @@ ble_hci_cmd_tx(void *cmd, void *evt_buf, uint8_t evt_buf_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;
 }
@@ -271,17 +293,17 @@ ble_hci_cmd_tx_empty_ack(void *cmd)
 void
 ble_hci_cmd_rx_ack(uint8_t *ack_ev)
 {
-    /* The controller should always reuse the command buffer for its acks. */
-    BLE_HS_DBG_ASSERT(ack_ev == host_hci_cmd_buf);
-
     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);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 aeba768..6e8a59f 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -23,12 +23,9 @@
 #include "stats/stats.h"
 #include "util/tpq.h"
 #include "os/os.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "host/host_hci.h"
 #include "ble_hs_priv.h"
-#ifdef PHONY_TRANSPORT
-#include "host/ble_hs_test.h"
-#endif
 
 /**
  * The maximum number of events the host will process in a row before returning
@@ -38,27 +35,33 @@
 
 static struct log_handler ble_hs_log_console_handler;
 
-struct os_mempool g_hci_evt_pool;
-static void *ble_hs_hci_evt_buf;
-
-/* XXX: this might be transport layer */
-#define HCI_OS_EVENT_BUF_SIZE   (sizeof(struct os_event))
-
-struct os_mempool g_hci_os_event_pool;
+struct os_mempool ble_hs_hci_ev_pool;
 static void *ble_hs_hci_os_event_buf;
 
+/** OS event - triggers tx of pending notifications and indications. */
 static struct os_event ble_hs_event_tx_notifications = {
     .ev_type = BLE_HS_EVENT_TX_NOTIFICATIONS,
     .ev_arg = NULL,
 };
 
+/** OS event - triggers a full reset. */
+static struct os_event ble_hs_event_reset = {
+    .ev_type = BLE_HS_EVENT_RESET,
+    .ev_arg = NULL,
+};
+
+uint8_t ble_hs_sync_state;
+static int ble_hs_reset_reason;
+
 #if MYNEWT_SELFTEST
 /** Use a higher frequency timer to allow tests to run faster. */
-#define BLE_HS_HEARTBEAT_OS_TICKS         (OS_TICKS_PER_SEC / 10)
+#define BLE_HS_HEARTBEAT_OS_TICKS       (OS_TICKS_PER_SEC / 10)
 #else
-#define BLE_HS_HEARTBEAT_OS_TICKS         OS_TICKS_PER_SEC
+#define BLE_HS_HEARTBEAT_OS_TICKS       OS_TICKS_PER_SEC
 #endif
 
+#define BLE_HS_SYNC_RETRY_RATE          (OS_TICKS_PER_SEC / 10)    
+
 /**
  * Handles unresponsive timeouts and periodic retries in case of resource
  * shortage.
@@ -90,6 +93,9 @@ STATS_NAME_START(ble_hs_stats)
     STATS_NAME(ble_hs_stats, hci_event)
     STATS_NAME(ble_hs_stats, hci_invalid_ack)
     STATS_NAME(ble_hs_stats, hci_unknown_event)
+    STATS_NAME(ble_hs_stats, hci_timeout)
+    STATS_NAME(ble_hs_stats, reset)
+    STATS_NAME(ble_hs_stats, sync)
 STATS_NAME_END(ble_hs_stats)
 
 int
@@ -157,11 +163,7 @@ ble_hs_process_tx_data_queue(void)
     struct os_mbuf *om;
 
     while ((om = os_mqueue_get(&ble_hs_tx_q)) != NULL) {
-#ifdef PHONY_TRANSPORT
-        ble_hs_test_pkt_txed(om);
-#else
-        ble_hci_transport_host_acl_data_send(om);
-#endif
+        ble_hci_trans_hs_acl_tx(om);
     }
 }
 
@@ -171,11 +173,22 @@ ble_hs_process_rx_data_queue(void)
     struct os_mbuf *om;
 
     while ((om = os_mqueue_get(&ble_hs_rx_q)) != NULL) {
-        host_hci_data_rx(om);
+        host_hci_acl_process(om);
     }
 }
 
 static void
+ble_hs_clear_data_queue(struct os_mqueue *mqueue)
+{
+    struct os_mbuf *om;
+
+    while ((om = os_mqueue_get(mqueue)) != NULL) {
+        os_mbuf_free_chain(om);
+    }
+}
+
+
+static void
 ble_hs_heartbeat_timer_reset(uint32_t ticks)
 {
     int rc;
@@ -202,6 +215,85 @@ ble_hs_heartbeat_sched(int32_t ticks_from_now)
 }
 
 /**
+ * Indicates whether the host has synchronized with the controller.
+ * Synchronization must occur before any host procedures can be performed.
+ *
+ * @return                      1 if the host and controller are in sync;
+ *                              0 if the host and controller our out of sync.
+ */
+int
+ble_hs_synced(void)
+{
+    return ble_hs_sync_state == BLE_HS_SYNC_STATE_GOOD;
+}
+
+static int
+ble_hs_sync(void)
+{
+    int rc;
+
+    /* Set the sync state to "bringup."  This allows the parent task to send
+     * the startup sequence to the controller.  No other tasks are allowed to
+     * send any commands.
+     */
+    ble_hs_sync_state = BLE_HS_SYNC_STATE_BRINGUP;
+
+    rc = ble_hs_startup_go();
+    if (rc == 0) {
+        ble_hs_sync_state = BLE_HS_SYNC_STATE_GOOD;
+        if (ble_hs_cfg.sync_cb != NULL) {
+            ble_hs_cfg.sync_cb();
+        }
+    } else {
+        ble_hs_sync_state = BLE_HS_SYNC_STATE_BAD;
+    }
+
+    ble_hs_heartbeat_sched(BLE_HS_SYNC_RETRY_RATE);
+
+    if (rc == 0) {
+        STATS_INC(ble_hs_stats, sync);
+    }
+
+    return rc;
+}
+
+static int
+ble_hs_reset(void)
+{
+    uint16_t conn_handle;
+    int rc;
+
+    STATS_INC(ble_hs_stats, reset);
+
+    ble_hs_sync_state = 0;
+
+    rc = ble_hci_trans_reset();
+    if (rc != 0) {
+        return rc;
+    }
+
+    ble_hs_clear_data_queue(&ble_hs_tx_q);
+    ble_hs_clear_data_queue(&ble_hs_rx_q);
+
+    while (1) {
+        conn_handle = ble_hs_atomic_first_conn_handle();
+        if (conn_handle == BLE_HS_CONN_HANDLE_NONE) {
+            break;
+        }
+
+        ble_gap_conn_broken(conn_handle, ble_hs_reset_reason);
+    }
+
+    if (ble_hs_cfg.reset_cb != NULL && ble_hs_reset_reason != 0) {
+        ble_hs_cfg.reset_cb(ble_hs_reset_reason);
+    }
+    ble_hs_reset_reason = 0;
+
+    rc = ble_hs_sync();
+    return rc;
+}
+
+/**
  * Called once a second by the ble_hs heartbeat timer.  Handles unresponsive
  * timeouts and periodic retries in case of resource shortage.
  */
@@ -210,6 +302,11 @@ ble_hs_heartbeat(void *unused)
 {
     int32_t ticks_until_next;
 
+    if (!ble_hs_sync_state) {
+        ble_hs_reset();
+        return;
+    }
+
     /* Ensure the timer expires at least once in the next second.
      * XXX: This is not very power efficient.  We will need separate timers for
      * each module.
@@ -235,7 +332,9 @@ ble_hs_event_handle(void *unused)
 {
     struct os_callout_func *cf;
     struct os_event *ev;
+    uint8_t *hci_evt;
     os_sr_t sr;
+    int rc;
     int i;
 
     i = 0;
@@ -267,10 +366,15 @@ ble_hs_event_handle(void *unused)
             break;
 
         case BLE_HOST_HCI_EVENT_CTLR_EVENT:
-            host_hci_os_event_proc(ev);
+            hci_evt = ev->ev_arg;
+            rc = os_memblock_put(&ble_hs_hci_ev_pool, ev);
+            BLE_HS_DBG_ASSERT_EVAL(rc == 0);
+
+            host_hci_evt_process(hci_evt);
             break;
 
         case BLE_HS_EVENT_TX_NOTIFICATIONS:
+            BLE_HS_DBG_ASSERT(ev == &ble_hs_event_tx_notifications);
             ble_gatts_tx_notifications();
 
         case OS_EVENT_T_MQUEUE_DATA:
@@ -278,6 +382,11 @@ ble_hs_event_handle(void *unused)
             ble_hs_process_rx_data_queue();
             break;
 
+        case BLE_HS_EVENT_RESET:
+            BLE_HS_DBG_ASSERT(ev == &ble_hs_event_reset);
+            ble_hs_reset();
+            break;
+
         default:
             BLE_HS_DBG_ASSERT(0);
             break;
@@ -292,6 +401,22 @@ ble_hs_event_enqueue(struct os_event *ev)
     os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
 }
 
+void
+ble_hs_enqueue_hci_event(uint8_t *hci_evt)
+{
+    struct os_event *ev;
+
+    ev = os_memblock_get(&ble_hs_hci_ev_pool);
+    if (ev == NULL) {
+        ble_hci_trans_buf_free(ev->ev_arg);
+    } else {
+        ev->ev_queued = 0;
+        ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
+        ev->ev_arg = hci_evt;
+        ble_hs_event_enqueue(ev);
+    }
+}
+
 /**
  * Schedules for all pending notifications and indications to be sent in the
  * host parent task.
@@ -309,6 +434,15 @@ ble_hs_notifications_sched(void)
     ble_hs_event_enqueue(&ble_hs_event_tx_notifications);
 }
 
+void
+ble_hs_sched_reset(int reason)
+{
+    BLE_HS_DBG_ASSERT(ble_hs_reset_reason == 0);
+
+    ble_hs_reset_reason = reason;
+    ble_hs_event_enqueue(&ble_hs_event_reset);
+}
+
 /**
  * Synchronizes the host with the controller by sending a sequence of HCI
  * commands.  This function must be called before any other host functionality
@@ -316,6 +450,10 @@ ble_hs_notifications_sched(void)
  * initialized.  Typically, the host-parent-task calls this function at the top
  * of its task routine.
  *
+ * If the host fails to synchronize with the controller (if the controller is
+ * not fully booted, for example), the host will attempt to resynchronize every
+ * 100 ms.  For this reason, an error return code is not necessarily fatal.
+ *
  * @return                      0 on success; nonzero on error.
  */
 int
@@ -325,11 +463,9 @@ ble_hs_start(void)
 
     ble_hs_parent_task = os_sched_get_current_task();
 
-    ble_hs_heartbeat_timer_reset(BLE_HS_HEARTBEAT_OS_TICKS);
-
     ble_gatts_start();
 
-    rc = ble_hs_startup_go();
+    rc = ble_hs_sync();
     return rc;
 }
 
@@ -343,19 +479,18 @@ ble_hs_start(void)
  * @return                      0 on success; nonzero on failure.
  */
 int
-ble_hs_rx_data(struct os_mbuf *om)
+ble_hs_rx_data(struct os_mbuf *om, void *arg)
 {
     int rc;
 
     rc = os_mqueue_put(&ble_hs_rx_q, &ble_hs_evq, om);
-    if (rc != 0) {
+    if (rc == 0) {
+        os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
+    } else {
         os_mbuf_free_chain(om);
-        return BLE_HS_EOS;
+        rc = BLE_HS_EOS;
     }
-
-    os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
-
-    return 0;
+    return rc;
 }
 
 int
@@ -375,9 +510,6 @@ ble_hs_tx_data(struct os_mbuf *om)
 static void
 ble_hs_free_mem(void)
 {
-    free(ble_hs_hci_evt_buf);
-    ble_hs_hci_evt_buf = NULL;
-
     free(ble_hs_hci_os_event_buf);
     ble_hs_hci_os_event_buf = NULL;
 }
@@ -418,30 +550,17 @@ ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
     log_console_handler_init(&ble_hs_log_console_handler);
     log_register("ble_hs", &ble_hs_log, &ble_hs_log_console_handler);
 
-    ble_hs_hci_evt_buf = malloc(OS_MEMPOOL_BYTES(ble_hs_cfg.max_hci_bufs,
-                                                 HCI_EVT_BUF_SIZE));
-    if (ble_hs_hci_evt_buf == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    /* Create memory pool of command buffers */
-    rc = os_mempool_init(&g_hci_evt_pool, ble_hs_cfg.max_hci_bufs,
-                         HCI_EVT_BUF_SIZE, ble_hs_hci_evt_buf,
-                         "HCICmdPool");
-    assert(rc == 0);
-
-    ble_hs_hci_os_event_buf = malloc(OS_MEMPOOL_BYTES(ble_hs_cfg.max_hci_bufs,
-                                                      HCI_OS_EVENT_BUF_SIZE));
+    ble_hs_hci_os_event_buf = malloc(
+        OS_MEMPOOL_BYTES(ble_hs_cfg.max_hci_bufs, sizeof (struct os_event)));
     if (ble_hs_hci_os_event_buf == NULL) {
         rc = BLE_HS_ENOMEM;
         goto err;
     }
 
     /* Create memory pool of OS events */
-    rc = os_mempool_init(&g_hci_os_event_pool, ble_hs_cfg.max_hci_bufs,
-                         HCI_OS_EVENT_BUF_SIZE, ble_hs_hci_os_event_buf,
-                         "HCIOsEventPool");
+    rc = os_mempool_init(&ble_hs_hci_ev_pool, ble_hs_cfg.max_hci_bufs,
+                         sizeof (struct os_event), ble_hs_hci_os_event_buf,
+                         "ble_hs_hci_ev_pool");
     assert(rc == 0);
 
     /* Initialize eventq */
@@ -516,6 +635,9 @@ ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
     ble_hs_dbg_mutex_locked = 0;
 #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);
+
     return 0;
 
 err:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/host/src/ble_hs_atomic.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_atomic.c b/net/nimble/host/src/ble_hs_atomic.c
index 36a603d..9c933fc 100644
--- a/net/nimble/host/src/ble_hs_atomic.c
+++ b/net/nimble/host/src/ble_hs_atomic.c
@@ -93,3 +93,23 @@ ble_hs_atomic_conn_set_flags(uint16_t conn_handle, ble_hs_conn_flags_t flags,
 
     return rc;
 }
+
+uint16_t
+ble_hs_atomic_first_conn_handle(void)
+{
+    const struct ble_hs_conn *conn;
+    uint16_t conn_handle;
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_first();
+    if (conn != NULL) {
+        conn_handle = conn->bhc_handle;
+    } else {
+        conn_handle = BLE_HS_CONN_HANDLE_NONE;
+    }
+
+    ble_hs_unlock();
+
+    return conn_handle;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/host/src/ble_hs_atomic_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_atomic_priv.h b/net/nimble/host/src/ble_hs_atomic_priv.h
index a08b7ca..d82eeab 100644
--- a/net/nimble/host/src/ble_hs_atomic_priv.h
+++ b/net/nimble/host/src/ble_hs_atomic_priv.h
@@ -28,5 +28,6 @@ int ble_hs_atomic_conn_flags(uint16_t conn_handle,
                              ble_hs_conn_flags_t *out_flags);
 int ble_hs_atomic_conn_set_flags(uint16_t conn_handle,
                                  ble_hs_conn_flags_t flags, int on);
+uint16_t ble_hs_atomic_first_conn_handle(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 79ef6b9..9e788dd 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -47,9 +47,15 @@ struct ble_hs_conn;
 struct ble_l2cap_chan;
 struct os_mbuf;
 struct os_mempool;
+struct os_event;
 
 #define BLE_HOST_HCI_EVENT_CTLR_EVENT   (OS_EVENT_T_PERUSER + 0)
 #define BLE_HS_EVENT_TX_NOTIFICATIONS   (OS_EVENT_T_PERUSER + 1)
+#define BLE_HS_EVENT_RESET              (OS_EVENT_T_PERUSER + 2)
+
+#define BLE_HS_SYNC_STATE_BAD           0
+#define BLE_HS_SYNC_STATE_BRINGUP       1
+#define BLE_HS_SYNC_STATE_GOOD          2
 
 STATS_SECT_START(ble_hs_stats)
     STATS_SECT_ENTRY(conn_create)
@@ -58,17 +64,26 @@ STATS_SECT_START(ble_hs_stats)
     STATS_SECT_ENTRY(hci_event)
     STATS_SECT_ENTRY(hci_invalid_ack)
     STATS_SECT_ENTRY(hci_unknown_event)
+    STATS_SECT_ENTRY(hci_timeout)
+    STATS_SECT_ENTRY(reset)
+    STATS_SECT_ENTRY(sync)
 STATS_SECT_END
 extern STATS_SECT_DECL(ble_hs_stats) ble_hs_stats;
 
 extern struct ble_hs_cfg ble_hs_cfg;
 extern struct os_mbuf_pool ble_hs_mbuf_pool;
+extern uint8_t ble_hs_sync_state;
 
 extern const uint8_t ble_hs_misc_null_addr[6];
 
 void ble_hs_process_tx_data_queue(void);
 void ble_hs_process_rx_data_queue(void);
 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_misc_malloc_mempool(void **mem, struct os_mempool *pool,
                                int num_entries, int entry_size, char *name);
@@ -86,6 +101,7 @@ int ble_hs_locked_by_cur_task(void);
 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_heartbeat_sched(int32_t ticks);
 void ble_hs_notifications_sched(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 05f9122..94a5586 100644
--- a/net/nimble/host/src/ble_hs_pvcy.c
+++ b/net/nimble/host/src/ble_hs_pvcy.c
@@ -145,11 +145,12 @@ ble_hs_pvcy_ensure_started(void)
     return 0;
 }
 
-void
+int
 ble_hs_pvcy_set_our_irk(const uint8_t *irk)
 {
     uint8_t tmp_addr[6];
     uint8_t new_irk[16];
+    int rc;
 
     memset(new_irk, 0, sizeof(new_irk));
 
@@ -163,17 +164,33 @@ ble_hs_pvcy_set_our_irk(const uint8_t *irk)
     if (memcmp(ble_hs_pvcy_irk, new_irk, 16) != 0) {
         memcpy(ble_hs_pvcy_irk, new_irk, 16);
 
-        ble_hs_pvcy_set_resolve_enabled(0);
-        ble_hs_pvcy_clear_entries();
-        ble_hs_pvcy_set_resolve_enabled(1);
+        rc = ble_hs_pvcy_set_resolve_enabled(0);
+        if (rc != 0) {
+            return rc;
+        }
+
+        rc = ble_hs_pvcy_clear_entries();
+        if (rc != 0) {
+            return rc;
+        }
+
+        rc = ble_hs_pvcy_set_resolve_enabled(1);
+        if (rc != 0) {
+            return rc;
+        }
 
         /* Push a null address identity to the controller.  The controller uses
          * this entry to generate an RPA when we do advertising with
          * own-addr-type = rpa.
          */
         memset(tmp_addr, 0, 6);
-        ble_hs_pvcy_add_entry(tmp_addr, 0, ble_hs_pvcy_irk);
+        rc = ble_hs_pvcy_add_entry(tmp_addr, 0, ble_hs_pvcy_irk);
+        if (rc != 0) {
+            return rc;
+        }
     }
+
+    return 0;
 }
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/host/src/ble_hs_pvcy_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_pvcy_priv.h b/net/nimble/host/src/ble_hs_pvcy_priv.h
index 2635778..16ded35 100644
--- a/net/nimble/host/src/ble_hs_pvcy_priv.h
+++ b/net/nimble/host/src/ble_hs_pvcy_priv.h
@@ -22,7 +22,7 @@
 
 #include <inttypes.h>
 
-void ble_hs_pvcy_set_our_irk(const uint8_t *irk);
+int ble_hs_pvcy_set_our_irk(const uint8_t *irk);
 int ble_hs_pvcy_our_irk(const uint8_t **out_irk);
 int ble_hs_pvcy_remove_entry(uint8_t addr_type, uint8_t *addr);
 int ble_hs_pvcy_add_entry(uint8_t *addr, uint8_t addrtype, uint8_t *irk);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 45fb5ce..1ce49a7 100644
--- a/net/nimble/host/src/ble_hs_startup.c
+++ b/net/nimble/host/src/ble_hs_startup.c
@@ -222,19 +222,16 @@ ble_hs_startup_go(void)
 
     rc = ble_hs_startup_set_evmask_tx();
     if (rc != 0) {
-        assert(0);
         return rc;
     }
 
     rc = ble_hs_startup_le_set_evmask_tx();
     if (rc != 0) {
-        assert(0);
         return rc;
     }
 
     rc = ble_hs_startup_le_read_buf_sz_tx();
     if (rc != 0) {
-        assert(0);
         return rc;
     }
 
@@ -242,7 +239,6 @@ ble_hs_startup_go(void)
 
     rc = ble_hs_startup_le_read_sup_f_tx();
     if (rc != 0) {
-        assert(0);
         return rc;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
index 6b58624..f5d46a3 100644
--- a/net/nimble/host/src/host_dbg.c
+++ b/net/nimble/host/src/host_dbg.c
@@ -23,7 +23,7 @@
 #include "os/os.h"
 #include "console/console.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "ble_hs_priv.h"
 
 static void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 791aec2..ed5dd6a 100644
--- a/net/nimble/host/src/host_hci.c
+++ b/net/nimble/host/src/host_hci.c
@@ -23,7 +23,7 @@
 #include "os/os.h"
 #include "console/console.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "host/host_hci.h"
 #include "host/ble_gap.h"
 #include "ble_hs_priv.h"
@@ -582,7 +582,7 @@ host_hci_set_buf_size(uint16_t pktlen, uint8_t max_pkts)
 }
 
 int
-host_hci_event_rx(uint8_t *data)
+host_hci_evt_process(uint8_t *data)
 {
     const struct host_hci_event_dispatch_entry *entry;
     uint8_t event_code;
@@ -604,40 +604,20 @@ host_hci_event_rx(uint8_t *data)
 
     entry = host_hci_dispatch_entry_find(event_code);
     if (entry == NULL) {
-        STATS_INC(ble_hs_stats, hci_invalid_ack);
+        STATS_INC(ble_hs_stats, hci_unknown_event);
         rc = BLE_HS_ENOTSUP;
     } else {
         rc = entry->hed_fn(event_code, data, event_len);
     }
 
-    return rc;
-}
-
-int
-host_hci_os_event_proc(struct os_event *ev)
-{
-    os_error_t err;
-    int rc;
-
-    rc = host_hci_event_rx(ev->ev_arg);
-
-    /* Free the command buffer */
-    err = os_memblock_put(&g_hci_evt_pool, ev->ev_arg);
-    BLE_HS_DBG_ASSERT_EVAL(err == OS_OK);
-
-    /* Free the event */
-    err = os_memblock_put(&g_hci_os_event_pool, ev);
-    BLE_HS_DBG_ASSERT_EVAL(err == OS_OK);
+    ble_hci_trans_buf_free(data);
 
     return rc;
 }
 
-/* XXX: For now, put this here */
 int
-ble_hci_transport_ctlr_event_send(uint8_t *hci_ev)
+host_hci_evt_rx(uint8_t *hci_ev, void *arg)
 {
-    struct os_event *ev;
-    os_error_t err;
     int enqueue;
 
     BLE_HS_DBG_ASSERT(hci_ev != NULL);
@@ -659,25 +639,12 @@ ble_hci_transport_ctlr_event_send(uint8_t *hci_ev)
     }
 
     if (enqueue) {
-        /* Get an event structure off the queue */
-        ev = (struct os_event *)os_memblock_get(&g_hci_os_event_pool);
-        if (!ev) {
-            err = os_memblock_put(&g_hci_evt_pool, hci_ev);
-            BLE_HS_DBG_ASSERT_EVAL(err == OS_OK);
-            return -1;
-        }
-
-        /* Fill out the event and post to host task. */
-        ev->ev_queued = 0;
-        ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
-        ev->ev_arg = hci_ev;
-        ble_hs_event_enqueue(ev);
+        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.
@@ -688,7 +655,7 @@ ble_hci_transport_ctlr_event_send(uint8_t *hci_ev)
  * @return                      0 on success; nonzero on failure.
  */
 int
-host_hci_data_rx(struct os_mbuf *om)
+host_hci_acl_process(struct os_mbuf *om)
 {
     struct hci_data_hdr hci_hdr;
     struct ble_hs_conn *conn;
@@ -703,7 +670,7 @@ host_hci_data_rx(struct os_mbuf *om)
     }
 
 #if (BLETEST_THROUGHPUT_TEST == 0)
-    BLE_HS_LOG(DEBUG, "host_hci_data_rx(): handle=%u pb=%x len=%u data=",
+    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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
index a3e021d..cce2e26 100644
--- a/net/nimble/host/src/host_hci_cmd.c
+++ b/net/nimble/host/src/host_hci_cmd.c
@@ -24,32 +24,17 @@
 #include "os/os.h"
 #include "console/console.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "host/host_hci.h"
 #include "host_dbg_priv.h"
 #include "ble_hs_priv.h"
-#ifdef PHONY_TRANSPORT
-#include "host/ble_hs_test.h"
-#endif
-
-/**
- * This buffer holds one of the following:
- * 1. The current outgoing HCI command.
- * 2. The current incoming HCI acknowledgement (command complete or command
- *    status event).
- */
-uint8_t host_hci_cmd_buf[HCI_EVT_BUF_SIZE];
 
 static int
 host_hci_cmd_transport(uint8_t *cmdbuf)
 {
-#ifdef PHONY_TRANSPORT
-    ble_hs_test_hci_txed(cmdbuf);
-    return 0;
-#else
     int rc;
 
-    rc = ble_hci_transport_host_cmd_send(cmdbuf);
+    rc = ble_hci_trans_hs_cmd_tx(cmdbuf);
     switch (rc) {
     case 0:
         return 0;
@@ -60,7 +45,6 @@ host_hci_cmd_transport(uint8_t *cmdbuf)
     default:
         return BLE_HS_EUNKNOWN;
     }
-#endif
 }
 
 void
@@ -79,19 +63,23 @@ 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)
 {
+    uint8_t *buf;
     int rc;
 
-    htole16(host_hci_cmd_buf, ogf << 10 | ocf);
-    host_hci_cmd_buf[2] = len;
+    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(host_hci_cmd_buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
+        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(host_hci_cmd_buf, len + BLE_HCI_CMD_HDR_LEN);
+    ble_hs_log_flat_buf(buf, len + BLE_HCI_CMD_HDR_LEN);
     BLE_HS_LOG(DEBUG, "\n");
-    rc = host_hci_cmd_transport(host_hci_cmd_buf);
+    rc = host_hci_cmd_transport(buf);
 
     if (rc == 0) {
         STATS_INC(ble_hs_stats, hci_cmd);
@@ -110,6 +98,24 @@ host_hci_cmd_send_buf(void *buf)
     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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 faf62c3..2ae3354 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -1136,6 +1136,7 @@ TEST_CASE(ble_gap_test_case_conn_find)
 
     struct ble_gap_conn_desc desc;
     struct ble_hs_conn *conn;
+    uint8_t pub_addr[6];
     int rc;
 
     /*** We are master; public addresses. */
@@ -1150,6 +1151,10 @@ TEST_CASE(ble_gap_test_case_conn_find)
                                      ble_gap_test_util_connect_cb,
                                      NULL);
 
+
+    rc = ble_hs_id_copy_addr(BLE_ADDR_TYPE_PUBLIC, pub_addr, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
     rc = ble_gap_conn_find(8, &desc);
     TEST_ASSERT_FATAL(rc == 0);
     TEST_ASSERT(desc.conn_handle == 8);
@@ -1157,8 +1162,8 @@ TEST_CASE(ble_gap_test_case_conn_find)
     TEST_ASSERT(desc.our_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
     TEST_ASSERT(desc.peer_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
     TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
-    TEST_ASSERT(memcmp(desc.our_ota_addr, g_dev_addr, 6) == 0);
-    TEST_ASSERT(memcmp(desc.our_id_addr, g_dev_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.our_ota_addr, pub_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
     TEST_ASSERT(memcmp(desc.peer_ota_addr,
                        ((uint8_t[6]){2,3,4,5,6,7}), 6) == 0);
     TEST_ASSERT(memcmp(desc.peer_id_addr,
@@ -1203,7 +1208,7 @@ TEST_CASE(ble_gap_test_case_conn_find)
     TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
     TEST_ASSERT(memcmp(desc.our_ota_addr,
                        ((uint8_t[6]){0x40,1,2,3,4,5}), 6) == 0);
-    TEST_ASSERT(memcmp(desc.our_id_addr, g_dev_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
     TEST_ASSERT(memcmp(desc.peer_ota_addr,
                        ((uint8_t[6]){0x50,1,2,3,4,5}), 6) == 0);
     TEST_ASSERT(memcmp(desc.peer_id_addr,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
index a8e8d82..32c5cf4 100644
--- a/net/nimble/host/src/test/ble_host_hci_test.c
+++ b/net/nimble/host/src/test/ble_host_hci_test.c
@@ -21,6 +21,7 @@
 #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"
@@ -28,13 +29,16 @@
 
 TEST_CASE(ble_host_hci_test_event_bad)
 {
-    uint8_t buf[2];
+    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_event_rx(buf);
+    rc = host_hci_evt_process(buf);
     TEST_ASSERT(rc == BLE_HS_ENOTSUP);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 16ae9b9..0b89bc9 100644
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ b/net/nimble/host/src/test/ble_hs_test.c
@@ -23,22 +23,6 @@
 #include "testutil/testutil.h"
 #include "ble_hs_test_util.h"
 
-/* Our global device address. */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN] = { 0x0a, 0x54, 0xab, 0x49, 0x7f, 0x06 };
-
-void
-ble_hs_test_pkt_txed(struct os_mbuf *om)
-{
-    ble_hs_test_util_prev_tx_enqueue(om);
-}
-
-void
-ble_hs_test_hci_txed(uint8_t *cmdbuf)
-{
-    ble_hs_test_util_enqueue_hci_tx(cmdbuf);
-    os_memblock_put(&g_hci_evt_pool, cmdbuf);
-}
-
 #ifdef MYNEWT_SELFTEST
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 1a6dd5e..831a077 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -23,12 +23,21 @@
 #include "testutil/testutil.h"
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#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"
 
+/* Our global device address. */
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+
+#define BLE_HS_TEST_UTIL_PUB_ADDR_VAL { 0x0a, 0x54, 0xab, 0x49, 0x7f, 0x06 }
+
+static const uint8_t ble_hs_test_util_pub_addr[BLE_DEV_ADDR_LEN] =
+    BLE_HS_TEST_UTIL_PUB_ADDR_VAL;
+
 /** Use lots of small mbufs to ensure correct mbuf usage. */
 #define BLE_HS_TEST_UTIL_NUM_MBUFS      (100)
 #define BLE_HS_TEST_UTIL_BUF_SIZE       OS_ALIGN(100, 4)
@@ -246,13 +255,14 @@ ble_hs_test_util_rx_hci_evt(uint8_t *evt)
     TEST_ASSERT_FATAL(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
 
     if (os_started()) {
-        evbuf = os_memblock_get(&g_hci_evt_pool);
+        evbuf = ble_hci_trans_buf_alloc(
+            BLE_HCI_TRANS_BUF_EVT_LO);
         TEST_ASSERT_FATAL(evbuf != NULL);
 
         memcpy(evbuf, evt, totlen);
-        rc = ble_hci_transport_ctlr_event_send(evbuf);
+        rc = ble_hci_trans_ll_evt_tx(evbuf);
     } else {
-        rc = host_hci_event_rx(evt);
+        rc = host_hci_evt_process(evt);
     }
 
     TEST_ASSERT_FATAL(rc == 0);
@@ -679,14 +689,6 @@ ble_hs_test_util_adv_start(uint8_t own_addr_type,
 
     if (adv_params->conn_mode != BLE_GAP_CONN_MODE_DIR) {
         acks[i] = (struct ble_hs_test_util_phony_ack) {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR),
-            ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
-            { 0 },
-            1,
-        };
-        i++;
-
-        acks[i] = (struct ble_hs_test_util_phony_ack) {
             BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_DATA),
             ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
         };
@@ -777,6 +779,35 @@ ble_hs_test_util_conn_update(uint16_t conn_handle,
 }
 
 int
+ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
+                             uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            ble_hs_test_util_exp_hci_status(0, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
+            ble_hs_test_util_exp_hci_status(1, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            ble_hs_test_util_exp_hci_status(2, fail_idx, hci_status),
+        },
+        {
+            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),
+        },
+    }));
+
+    rc = ble_hs_pvcy_set_our_irk(irk);
+    return rc;
+}
+
+int
 ble_hs_test_util_security_initiate(uint16_t conn_handle, uint8_t hci_status)
 {
     int rc;
@@ -906,7 +937,8 @@ ble_hs_test_util_set_startup_acks(void)
         {
             .opcode = host_hci_opcode_join(BLE_HCI_OGF_LE,
                                            BLE_HCI_OCF_LE_RD_BUF_SIZE),
-            .evt_params = { 0xff, 0xff, 1 },
+            /* Use a very low buffer size (16) to test fragmentation. */
+            .evt_params = { 0x10, 0x00, 0x20 },
             .evt_params_len = 3,
         },
         {
@@ -916,6 +948,12 @@ ble_hs_test_util_set_startup_acks(void)
             .evt_params_len = 8,
         },
         {
+            .opcode = host_hci_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),
         },
@@ -1279,9 +1317,25 @@ ble_hs_test_util_post_test(void *arg)
     ble_hs_test_util_assert_mbufs_freed();
 }
 
+static int
+ble_hs_test_util_pkt_txed(struct os_mbuf *om, void *arg)
+{
+    ble_hs_test_util_prev_tx_enqueue(om);
+    return 0;
+}
+
+static int
+ble_hs_test_util_hci_txed(uint8_t *cmdbuf, void *arg)
+{
+    ble_hs_test_util_enqueue_hci_tx(cmdbuf);
+    ble_hci_trans_buf_free(cmdbuf);
+    return 0;
+}
+
 void
 ble_hs_test_util_init(void)
 {
+    struct ble_hci_ram_cfg hci_cfg;
     struct ble_hs_cfg cfg;
     int rc;
 
@@ -1323,10 +1377,18 @@ ble_hs_test_util_init(void)
 
     ble_hci_set_phony_ack_cb(NULL);
 
-    ble_hs_test_util_prev_hci_tx_clear();
+    ble_hci_trans_cfg_ll(ble_hs_test_util_hci_txed, NULL,
+                                ble_hs_test_util_pkt_txed, NULL);
 
-    ble_hs_id_set_pub(g_dev_addr);
+    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);
+
+    ble_hs_test_util_set_startup_acks();
 
-    /* Use a very low buffer size (16) to test fragmentation. */
-    host_hci_set_buf_size(16, 64);
+    rc = ble_hs_start();
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_prev_hci_tx_clear();
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 9c4b982..00b090f 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -106,6 +106,8 @@ int ble_hs_test_util_wl_set(struct ble_gap_white_entry *white_list,
 int ble_hs_test_util_conn_update(uint16_t conn_handle,
                                  struct ble_gap_upd_params *params,
                                  uint8_t hci_status);
+int ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
+                                 uint8_t hci_status);
 int ble_hs_test_util_security_initiate(uint16_t conn_handle,
                                        uint8_t hci_status);
 int ble_hs_test_util_l2cap_rx_first_frag(uint16_t conn_handle, uint16_t cid,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 1ab8e0a..e12e489 100644
--- a/net/nimble/host/src/test/ble_os_test.c
+++ b/net/nimble/host/src/test/ble_os_test.c
@@ -21,7 +21,7 @@
 #include "os/os.h"
 #include "testutil/testutil.h"
 #include "nimble/hci_common.h"
-#include "nimble/hci_transport.h"
+#include "nimble/ble_hci_trans.h"
 #include "host/ble_hs_test.h"
 #include "host/ble_gap.h"
 #include "ble_hs_test_util.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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 1c052fb..bdecde6 100644
--- a/net/nimble/host/src/test/ble_sm_test_util.c
+++ b/net/nimble/host/src/test/ble_sm_test_util.c
@@ -229,7 +229,7 @@ ble_sm_test_util_init_good(struct ble_sm_test_params *params,
     ble_sm_dbg_set_next_ediv(out_us->ediv);
     ble_sm_dbg_set_next_master_id_rand(out_us->rand_num);
     ble_sm_dbg_set_next_ltk(out_us->ltk);
-    ble_hs_pvcy_set_our_irk(out_us->id_info->irk);
+    ble_hs_test_util_set_our_irk(out_us->id_info->irk, 0, 0);
     ble_sm_dbg_set_next_csrk(out_us->sign_info->sig_key);
 
     if (out_us->public_key != NULL) {
@@ -909,8 +909,20 @@ ble_sm_test_util_verify_tx_lt_key_req_neg_reply(uint16_t conn_handle)
 }
 
 static void
-ble_sm_test_util_set_lt_key_req_reply_ack(uint8_t status,
-                                                uint16_t conn_handle)
+ble_sm_test_util_set_lt_key_req_neg_reply_ack(uint8_t status,
+                                              uint16_t conn_handle)
+{
+    static uint8_t params[BLE_HCI_LT_KEY_REQ_NEG_REPLY_ACK_PARAM_LEN];
+
+    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),
+        status, params, sizeof params);
+}
+
+static void
+ble_sm_test_util_set_lt_key_req_reply_ack(uint8_t status, uint16_t conn_handle)
 {
     static uint8_t params[BLE_HCI_LT_KEY_REQ_REPLY_ACK_PARAM_LEN];
 
@@ -1303,7 +1315,7 @@ ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num)
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Receive a long term key request from the controller. */
-    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
+    ble_sm_test_util_set_lt_key_req_neg_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index f2041c8..4deb9f3 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -34,10 +34,6 @@ struct ble_encryption_block
     uint8_t     cipher_text[BLE_ENC_BLOCK_SIZE];
 };
 
-/* Shared command pool for transort between host and controller */
-extern struct os_mempool g_hci_evt_pool;
-extern struct os_mempool g_hci_os_event_pool;
-
 /*
  * BLE MBUF structure:
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
new file mode 100644
index 0000000..822da96
--- /dev/null
+++ b/net/nimble/include/nimble/ble_hci_trans.h
@@ -0,0 +1,152 @@
+/**
+ * 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_HCI_TRANSPORT_
+#define H_HCI_TRANSPORT_
+
+#include <inttypes.h>
+struct os_mbuf;
+
+#define BLE_HCI_TRANS_CMD_SZ        260
+
+/*** 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.
+ */
+#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. */
+#define BLE_HCI_TRANS_BUF_CMD       3
+
+/** Callback function types; executed when HCI packets are received. */
+typedef int ble_hci_trans_rx_cmd_fn(uint8_t *cmd, void *arg);
+typedef int ble_hci_trans_rx_acl_fn(struct os_mbuf *om, void *arg);
+
+/**
+ * Sends an HCI event from the controller to the host.
+ *
+ * @param cmd                   The HCI event to send.  This buffer must be
+ *                                  allocated via ble_hci_trans_buf_alloc().
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int ble_hci_trans_ll_evt_tx(uint8_t *hci_ev);
+
+/**
+ * Sends ACL data from controller to host.
+ *
+ * @param om                    The ACL data packet to send.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int ble_hci_trans_ll_acl_tx(struct os_mbuf *om);
+
+/**
+ * Sends an HCI command from the host to the controller.
+ *
+ * @param cmd                   The HCI command to send.  This buffer must be
+ *                                  allocated via ble_hci_trans_buf_alloc().
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int ble_hci_trans_hs_cmd_tx(uint8_t *cmd);
+
+/**
+ * Sends ACL data from host to controller.
+ *
+ * @param om                    The ACL data packet to send.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int ble_hci_trans_hs_acl_tx(struct os_mbuf *om);
+
+/**
+ * Allocates a flat buffer of the specified type.
+ *
+ * @param type                  The type of buffer to allocate; one of the
+ *                                  BLE_HCI_TRANS_BUF_[...] constants.
+ *
+ * @return                      The allocated buffer on success;
+ *                              NULL on buffer exhaustion.
+ */
+uint8_t *ble_hci_trans_buf_alloc(int type);
+
+/**
+ * Frees the specified flat buffer.  The buffer must have been allocated via
+ * ble_hci_trans_buf_alloc().
+ *
+ * @param buf                   The buffer to free.
+ */
+void ble_hci_trans_buf_free(uint8_t *buf);
+
+/**
+ * Configures the HCI transport to operate with a controller.  The transport
+ * will execute specified callbacks upon receiving HCI packets from the host.
+ *
+ * @param cmd_cb                The callback to execute upon receiving an HCI
+ *                                  command.
+ * @param cmd_arg               Optional argument to pass to the command
+ *                                  callback.
+ * @param acl_cb                The callback to execute upon receiving ACL
+ *                                  data.
+ * @param acl_arg               Optional argument to pass to the ACL
+ *                                  callback.
+ */
+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);
+
+/**
+ * Configures the HCI transport to operate with a host.  The transport will
+ * execute specified callbacks upon receiving HCI packets from the controller.
+ *
+ * @param cmd_cb                The callback to execute upon receiving an HCI
+ *                                  event.
+ * @param cmd_arg               Optional argument to pass to the command
+ *                                  callback.
+ * @param acl_cb                The callback to execute upon receiving ACL
+ *                                  data.
+ * @param acl_arg               Optional argument to pass to the ACL
+ *                                  callback.
+ */
+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);
+
+/**
+ * Resets the HCI module to a clean state.  Frees all buffers and reinitializes
+ * the underlying transport.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int ble_hci_trans_reset(void);
+
+#endif /* H_HCI_TRANSPORT_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/include/nimble/hci_transport.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_transport.h b/net/nimble/include/nimble/hci_transport.h
deleted file mode 100644
index 7de737c..0000000
--- a/net/nimble/include/nimble/hci_transport.h
+++ /dev/null
@@ -1,32 +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_HCI_TRANSPORT_
-#define H_HCI_TRANSPORT_
-
-/* Send a HCI command from the host to the controller */
-int ble_hci_transport_host_cmd_send(uint8_t *cmd);
-
-/* Send a HCI event from the controller to the host */
-int ble_hci_transport_ctlr_event_send(uint8_t *hci_ev);
-
-/* Send ACL data from host to contoller */
-int ble_hci_transport_host_acl_data_send(struct os_mbuf *om);
-
-#endif /* H_HCI_TRANSPORT_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
new file mode 100644
index 0000000..1c5b58e
--- /dev/null
+++ b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
@@ -0,0 +1,15 @@
+#ifndef H_BLE_HCI_RAM_
+#define H_BLE_HCI_RAM_
+
+#include "nimble/ble_hci_trans.h"
+
+struct ble_hci_ram_cfg {
+    uint16_t num_evt_bufs;
+    uint16_t evt_buf_sz;
+};
+
+extern const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt;
+
+int ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/transport/ram/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/pkg.yml b/net/nimble/transport/ram/pkg.yml
new file mode 100644
index 0000000..a3524a1
--- /dev/null
+++ b/net/nimble/transport/ram/pkg.yml
@@ -0,0 +1,33 @@
+#
+# 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.
+#
+
+pkg.name: net/nimble/transport/ram
+pkg.description: XXX
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - ble
+    - bluetooth
+
+pkg.deps:
+    - net/nimble
+    - libs/os
+
+pkg.apis:
+    - ble_transport

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
new file mode 100644
index 0000000..aac5eb1
--- /dev/null
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -0,0 +1,190 @@
+#include <assert.h>
+#include <errno.h>
+#include <stddef.h>
+#include "os/os.h"
+#include "nimble/ble_hci_trans.h"
+#include "transport/ram/ble_hci_ram.h"
+
+/** Default configuration. */
+const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
+    .num_evt_bufs = 3,
+    .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
+};
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_ram_rx_cmd_hs_cb;
+static void *ble_hci_ram_rx_cmd_hs_arg;
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_ram_rx_cmd_ll_cb;
+static void *ble_hci_ram_rx_cmd_ll_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_ram_rx_acl_hs_cb;
+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 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)
+{
+    ble_hci_ram_rx_cmd_hs_cb = cmd_cb;
+    ble_hci_ram_rx_cmd_hs_arg = cmd_arg;
+    ble_hci_ram_rx_acl_hs_cb = acl_cb;
+    ble_hci_ram_rx_acl_hs_arg = acl_arg;
+}
+
+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)
+{
+    ble_hci_ram_rx_cmd_ll_cb = cmd_cb;
+    ble_hci_ram_rx_cmd_ll_arg = cmd_arg;
+    ble_hci_ram_rx_acl_ll_cb = acl_cb;
+    ble_hci_ram_rx_acl_ll_arg = acl_arg;
+}
+
+int
+ble_hci_trans_hs_cmd_tx(uint8_t *cmd)
+{
+    int rc;
+
+    assert(ble_hci_ram_rx_cmd_ll_cb != NULL);
+
+    rc = ble_hci_ram_rx_cmd_ll_cb(cmd, ble_hci_ram_rx_cmd_ll_arg);
+    return rc;
+}
+
+int
+ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
+{
+    int rc;
+
+    assert(ble_hci_ram_rx_cmd_hs_cb != NULL);
+
+    rc = ble_hci_ram_rx_cmd_hs_cb(hci_ev, ble_hci_ram_rx_cmd_hs_arg);
+    return rc;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_ram_rx_acl_ll_cb != NULL);
+
+    rc = ble_hci_ram_rx_acl_ll_cb(om, ble_hci_ram_rx_acl_ll_arg);
+    return rc;
+}
+
+int
+ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_ram_rx_acl_hs_cb != NULL);
+
+    rc = ble_hci_ram_rx_acl_hs_cb(om, ble_hci_ram_rx_acl_hs_arg);
+    return rc;
+}
+
+uint8_t *
+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);
+        break;
+
+    case BLE_HCI_TRANS_BUF_CMD:
+        assert(!ble_hci_ram_hs_cmd_buf_alloced);
+        ble_hci_ram_hs_cmd_buf_alloced = 1;
+        buf = ble_hci_ram_hs_cmd_buf;
+        break;
+
+    default:
+        assert(0);
+        buf = NULL;
+    }
+
+    return buf;
+}
+
+void
+ble_hci_trans_buf_free(uint8_t *buf)
+{
+    int rc;
+
+    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 {
+        rc = os_memblock_put(&ble_hci_ram_evt_pool, buf);
+        assert(rc == 0);
+    }
+}
+
+static void
+ble_hci_ram_free_mem(void)
+{
+    free(ble_hci_ram_evt_buf);
+    ble_hci_ram_evt_buf = NULL;
+
+    free(ble_hci_ram_hs_cmd_buf);
+    ble_hci_ram_hs_cmd_buf = NULL;
+    ble_hci_ram_hs_cmd_buf_alloced = 0;
+}
+
+int
+ble_hci_trans_reset(void)
+{
+    return 0;
+}
+
+int
+ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
+{
+    int rc;
+
+    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;
+        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 (rc != 0) {
+        rc = EINVAL;
+        goto err;
+    }
+
+    ble_hci_ram_hs_cmd_buf = malloc(BLE_HCI_TRANS_CMD_SZ);
+    if (ble_hci_ram_hs_cmd_buf == NULL) {
+        rc = ENOMEM;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    ble_hci_ram_free_mem();
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
new file mode 100644
index 0000000..1fbaa74
--- /dev/null
+++ b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
@@ -0,0 +1,19 @@
+#ifndef H_BLE_HCI_UART_
+#define H_BLE_HCI_UART_
+
+struct ble_hci_uart_cfg {
+    uint32_t baud;
+    uint16_t num_evt_bufs;
+    uint16_t evt_buf_sz;
+    uint8_t uart_port;
+    uint8_t flow_ctrl;
+    uint8_t data_bits;
+    uint8_t stop_bits;
+    uint8_t parity;
+};
+
+extern const struct ble_hci_uart_cfg ble_hci_uart_cfg_dflt;
+
+int ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/net/nimble/transport/uart/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/pkg.yml b/net/nimble/transport/uart/pkg.yml
new file mode 100644
index 0000000..cce429c
--- /dev/null
+++ b/net/nimble/transport/uart/pkg.yml
@@ -0,0 +1,34 @@
+#
+# 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.
+#
+
+pkg.name: net/nimble/transport/uart
+pkg.description: XXX
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - ble
+    - bluetooth
+
+pkg.deps:
+    - hw/hal
+    - libs/os
+    - net/nimble
+
+pkg.apis:
+    - ble_transport



[02/14] incubator-mynewt-core git commit: BLE Host - Read BD_ADDR on startup.

Posted by cc...@apache.org.
BLE Host - Read BD_ADDR on startup.


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

Branch: refs/heads/develop
Commit: 93c01c53da4e79430ed2bb64d89a2ab37a3fe1cc
Parents: 1f8bbd8
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Aug 1 13:32:25 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:15 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/host_hci.h |  1 +
 net/nimble/host/src/ble_hs_startup.c    | 30 +++++++++++++++++++++++++---
 net/nimble/host/src/host_hci_cmd.c      | 14 +++++++++++++
 net/nimble/include/nimble/hci_common.h  |  3 +++
 4 files changed, 45 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93c01c53/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
index f92c1a9..dbe626a 100644
--- a/net/nimble/host/include/host/host_hci.h
+++ b/net/nimble/host/include/host/host_hci.h
@@ -36,6 +36,7 @@ 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,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93c01c53/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 568f1ed..45fb5ce 100644
--- a/net/nimble/host/src/ble_hs_startup.c
+++ b/net/nimble/host/src/ble_hs_startup.c
@@ -78,6 +78,28 @@ ble_hs_startup_le_read_buf_sz_tx(void)
 }
 
 static int
+ble_hs_startup_read_bd_addr(void)
+{
+    uint8_t ack_params[BLE_HCI_IP_RD_BD_ADDR_ACK_PARAM_LEN];
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN];
+    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);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (ack_params_len != sizeof ack_params) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    ble_hs_id_set_pub(ack_params);
+    return 0;
+}
+
+static int
 ble_hs_startup_le_set_evmask_tx(void)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_LE_EVENT_MASK_LEN];
@@ -224,10 +246,12 @@ ble_hs_startup_go(void)
         return rc;
     }
 
-    /* XXX: Read BD_ADDR. */
+    rc = ble_hs_startup_read_bd_addr();
+    if (rc != 0) {
+        return rc;
+    }
 
-    ble_hs_id_set_pub(g_dev_addr);
     ble_hs_pvcy_set_our_irk(NULL);
 
-    return rc;
+    return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93c01c53/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
index 9dbb036..a3e021d 100644
--- a/net/nimble/host/src/host_hci_cmd.c
+++ b/net/nimble/host/src/host_hci_cmd.c
@@ -138,6 +138,20 @@ host_hci_le_cmd_send(uint16_t ocf, uint8_t len, void *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)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93c01c53/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 d5c8743..3b44b0e 100644
--- a/net/nimble/include/nimble/hci_common.h
+++ b/net/nimble/include/nimble/hci_common.h
@@ -131,6 +131,9 @@
 /* --- Set event mask (OGF 0x03, OCF 0x0001 --- */
 #define BLE_HCI_SET_EVENT_MASK_LEN          (8)
 
+/* --- Read BD_ADDR (OGF 0x04, OCF 0x0009 --- */
+#define BLE_HCI_IP_RD_BD_ADDR_ACK_PARAM_LEN (6)
+
 /* --- Read/Write authenticated payload timeout (ocf 0x007B/0x007C) */
 #define BLE_HCI_RD_AUTH_PYLD_TMO_LEN        (4)
 #define BLE_HCI_WR_AUTH_PYLD_TMO_LEN        (2)


[09/14] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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;
     }


[10/14] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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;
-}


[05/14] incubator-mynewt-core git commit: olimex-e407 - UART flow control.

Posted by cc...@apache.org.
olimex-e407 - UART flow control.


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

Branch: refs/heads/develop
Commit: fca8bc15e456e259100553fc8cfff46e15db51af
Parents: 3d3f81d
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 2 12:40:19 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 2016 -0700

----------------------------------------------------------------------
 hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fca8bc15/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c b/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
index b68e370..763002f 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
+++ b/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
@@ -32,8 +32,8 @@ static const struct stm32f4_uart_cfg uart_cfg[UART_CNT] = {
         .suc_rcc_dev = RCC_APB2ENR_USART6EN,
         .suc_pin_tx = 38,
         .suc_pin_rx = 39,
-        .suc_pin_rts = -1,
-        .suc_pin_cts = -1,
+        .suc_pin_rts = 34,
+        .suc_pin_cts = 35,
         .suc_pin_af = GPIO_AF8_USART6,
         .suc_irqn = USART6_IRQn
     }


[04/14] incubator-mynewt-core git commit: BLE Host - Reset on hardware error.

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

Branch: refs/heads/develop
Commit: ef6ef4c50980df5a0d28b9bdff51118d6a41c8c9
Parents: 2444d8f
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 13:30:54 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 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/ef6ef4c5/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/ef6ef4c5/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/ef6ef4c5/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/ef6ef4c5/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/ef6ef4c5/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)
 


[13/14] incubator-mynewt-core git commit: olimex debug script - remove "set -x" debug flag.

Posted by cc...@apache.org.
olimex debug script - remove "set -x" debug flag.


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

Branch: refs/heads/develop
Commit: c9708658fddf140f2b5ac25b58e165b6191e01ae
Parents: 3a7f4e2
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 2 19:18:49 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 2016 -0700

----------------------------------------------------------------------
 .../olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh  | 1 -
 1 file changed, 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c9708658/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh b/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh
index d402753..2f30773 100755
--- a/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh
+++ b/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard_debug.sh
@@ -23,7 +23,6 @@
 #  - identities is the project identities string.
 # 
 #
-set -x
 if [ $# -lt 2 ]; then
     echo "Need binary to debug"
     exit 1


[12/14] incubator-mynewt-core git commit: BLE Host - Rename HCI identifiers and files.

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

Branch: refs/heads/develop
Commit: d842a43c98ee8acc812b49ac6a4da3681af86ed9
Parents: ef6ef4c
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Aug 3 18:41:01 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 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/d842a43c/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index 3cc0f24..8f10814 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/d842a43c/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 3a3d64a..0e6ec14 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/d842a43c/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/d842a43c/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/d842a43c/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 733f8d1..2f3c474 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/d842a43c/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index 24f1a3b..0a5dd82 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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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/d842a43c/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_ */


[06/14] incubator-mynewt-core git commit: olimex-e407 - hal_uart_close().

Posted by cc...@apache.org.
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/3a7f4e27
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/3a7f4e27
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/3a7f4e27

Branch: refs/heads/develop
Commit: 3a7f4e273004c88dc8c65bbfba7418caf5486a6c
Parents: fca8bc1
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Aug 2 12:40:46 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:16 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/3a7f4e27/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;
+}


[07/14] incubator-mynewt-core git commit: BLE Host - Use HCI transport API

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2444d8f6/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
new file mode 100755
index 0000000..98a49b6
--- /dev/null
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -0,0 +1,775 @@
+/**
+ * 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 <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_common.h"
+#include "nimble/ble_hci_trans.h"
+
+#include "transport/uart/ble_hci_uart.h"
+
+/***
+ * NOTE:
+ * The UART HCI transport doesn't use event buffer priorities.  All incoming
+ * and outgoing events and commands use buffers from the same pool.
+ */
+
+#define BLE_HCI_UART_H4_NONE        0x00
+#define BLE_HCI_UART_H4_CMD         0x01
+#define BLE_HCI_UART_H4_ACL         0x02
+#define BLE_HCI_UART_H4_SCO         0x03
+#define BLE_HCI_UART_H4_EVT         0x04
+
+/** Default configuration. */
+const struct ble_hci_uart_cfg ble_hci_uart_cfg_dflt = {
+    .uart_port = 0,
+    .baud = 1000000,
+    .flow_ctrl = HAL_UART_FLOW_CTL_RTS_CTS,
+    .data_bits = 8,
+    .stop_bits = 1,
+    .parity = HAL_UART_PARITY_NONE,
+
+    .num_evt_bufs = 8,
+    .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
+};
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_uart_rx_cmd_cb;
+static void *ble_hci_uart_rx_cmd_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_uart_rx_acl_cb;
+static void *ble_hci_uart_rx_acl_arg;
+
+static struct os_mempool ble_hci_uart_evt_pool;
+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.
+ */
+struct ble_hci_uart_cmd {
+    uint8_t *data;      /* Pointer to ble_hci_uart_cmd data */
+    uint16_t cur;       /* Number of bytes read/written */
+    uint16_t len;       /* Total number of bytes to read/write */
+};
+
+/**
+ * An incoming ACL data packet.
+ */
+struct ble_hci_uart_acl {
+    struct os_mbuf *buf; /* Buffer containing the data */
+    uint16_t len;        /* Target size when buf is considered complete */
+};
+
+/**
+ * A packet to be sent over the UART.  This can be a command, an event, or ACL
+ * data.
+ */
+struct ble_hci_uart_pkt {
+    STAILQ_ENTRY(ble_hci_uart_pkt) next;
+    void *data;
+    uint8_t type;
+};
+
+static struct {
+    /*** State of data received over UART. */
+    uint8_t rx_type;    /* Pending packet type. 0 means nothing pending */
+    union {
+        struct ble_hci_uart_cmd rx_cmd;
+        struct ble_hci_uart_acl rx_acl;
+    };
+
+    /*** State of data transmitted over UART. */
+    uint8_t tx_type;    /* Pending packet type. 0 means nothing pending */
+    union {
+        struct ble_hci_uart_cmd tx_cmd;
+        struct os_mbuf *tx_acl;
+    };
+    STAILQ_HEAD(, ble_hci_uart_pkt) tx_pkts; /* Packet queue to send to UART */
+} ble_hci_uart_state;
+
+static struct ble_hci_uart_cfg ble_hci_uart_cfg;
+
+static int
+ble_hci_uart_acl_tx(struct os_mbuf *om)
+{
+    struct ble_hci_uart_pkt *pkt;
+    os_sr_t sr;
+
+    pkt = os_memblock_get(&ble_hci_uart_pkt_pool);
+    if (pkt == NULL) {
+        os_mbuf_free_chain(om);
+        return BLE_ERR_MEM_CAPACITY;
+    }
+
+    pkt->type = BLE_HCI_UART_H4_ACL;
+    pkt->data = om;
+
+    OS_ENTER_CRITICAL(sr);
+    STAILQ_INSERT_TAIL(&ble_hci_uart_state.tx_pkts, pkt, next);
+    OS_EXIT_CRITICAL(sr);
+
+    hal_uart_start_tx(ble_hci_uart_cfg.uart_port);
+
+    return 0;
+}
+
+static int
+ble_hci_uart_cmdevt_tx(uint8_t *hci_ev, uint8_t h4_type)
+{
+    struct ble_hci_uart_pkt *pkt;
+    os_sr_t sr;
+
+    pkt = os_memblock_get(&ble_hci_uart_pkt_pool);
+    if (pkt == NULL) {
+        ble_hci_trans_buf_free(hci_ev);
+        return BLE_ERR_MEM_CAPACITY;
+    }
+
+    pkt->type = h4_type;
+    pkt->data = hci_ev;
+
+    OS_ENTER_CRITICAL(sr);
+    STAILQ_INSERT_TAIL(&ble_hci_uart_state.tx_pkts, pkt, next);
+    OS_EXIT_CRITICAL(sr);
+
+    hal_uart_start_tx(ble_hci_uart_cfg.uart_port);
+
+    return 0;
+}
+
+/**
+ * @return                      The packet type to transmit on success;
+ *                              -1 if there is nothing to send.
+ */
+static int
+ble_hci_uart_tx_pkt_type(void)
+{
+    struct ble_hci_uart_pkt *pkt;
+    os_sr_t sr;
+    int rc;
+
+    OS_ENTER_CRITICAL(sr);
+
+    pkt = STAILQ_FIRST(&ble_hci_uart_state.tx_pkts);
+    if (!pkt) {
+        OS_EXIT_CRITICAL(sr);
+        return -1;
+    }
+
+    STAILQ_REMOVE(&ble_hci_uart_state.tx_pkts, pkt, ble_hci_uart_pkt, next);
+
+    OS_EXIT_CRITICAL(sr);
+
+    rc = pkt->type;
+    switch (pkt->type) {
+    case BLE_HCI_UART_H4_CMD:
+        ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_CMD;
+        ble_hci_uart_state.tx_cmd.data = pkt->data;
+        ble_hci_uart_state.tx_cmd.cur = 0;
+        ble_hci_uart_state.tx_cmd.len = ble_hci_uart_state.tx_cmd.data[2] +
+                                        BLE_HCI_CMD_HDR_LEN;
+        break;
+
+    case BLE_HCI_UART_H4_EVT:
+        ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_EVT;
+        ble_hci_uart_state.tx_cmd.data = pkt->data;
+        ble_hci_uart_state.tx_cmd.cur = 0;
+        ble_hci_uart_state.tx_cmd.len = ble_hci_uart_state.tx_cmd.data[1] +
+                                        BLE_HCI_EVENT_HDR_LEN;
+        break;
+
+    case BLE_HCI_UART_H4_ACL:
+        ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_ACL;
+        ble_hci_uart_state.tx_acl = pkt->data;
+        break;
+
+    default:
+        rc = -1;
+        break;
+    }
+
+    os_memblock_put(&ble_hci_uart_pkt_pool, pkt);
+
+    return rc;
+}
+
+/**
+ * @return                      The byte to transmit on success;
+ *                              -1 if there is nothing to send.
+ */
+static int
+ble_hci_uart_tx_char(void *arg)
+{
+    int rc = -1;
+
+    switch (ble_hci_uart_state.tx_type) {
+    case BLE_HCI_UART_H4_NONE: /* No pending packet, pick one from the queue */
+        rc = ble_hci_uart_tx_pkt_type();
+        break;
+
+    case BLE_HCI_UART_H4_CMD:
+    case BLE_HCI_UART_H4_EVT:
+        rc = ble_hci_uart_state.tx_cmd.data[ble_hci_uart_state.tx_cmd.cur++];
+
+        if (ble_hci_uart_state.tx_cmd.cur == ble_hci_uart_state.tx_cmd.len) {
+            ble_hci_trans_buf_free(ble_hci_uart_state.tx_cmd.data);
+            ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_NONE;
+        }
+        break;
+
+    case BLE_HCI_UART_H4_ACL:
+        rc = *OS_MBUF_DATA(ble_hci_uart_state.tx_acl, uint8_t *);
+        os_mbuf_adj(ble_hci_uart_state.tx_acl, 1);
+        if (!OS_MBUF_PKTLEN(ble_hci_uart_state.tx_acl)) {
+            os_mbuf_free_chain(ble_hci_uart_state.tx_acl);
+            ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_NONE;
+        }
+        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;
+}
+
+/**
+ * @return                      The type of packet to follow success;
+ *                              -1 if there is no valid packet to receive.
+ */
+static int
+ble_hci_uart_rx_pkt_type(uint8_t data)
+{
+    ble_hci_uart_state.rx_type = data;
+
+    /* XXX: For now we assert that buffer allocation succeeds.  The correct
+     * thing to do is return -1 on allocation failure so that flow control is
+     * engaged.  Then, we will need to tell the UART to start receiving again
+     * as follows:
+     *     o flat buf: when we free a buffer.
+     *     o mbuf: periodically? (which task executes the callout?)
+     */
+    switch (ble_hci_uart_state.rx_type) {
+    case BLE_HCI_UART_H4_CMD:
+        ble_hci_uart_state.rx_cmd.data =
+            ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+        assert(ble_hci_uart_state.rx_cmd.data != NULL);
+
+        ble_hci_uart_state.rx_cmd.len = 0;
+        ble_hci_uart_state.rx_cmd.cur = 0;
+        break;
+
+    case BLE_HCI_UART_H4_EVT:
+        ble_hci_uart_state.rx_cmd.data =
+            ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
+        assert(ble_hci_uart_state.rx_cmd.data != NULL);
+
+        ble_hci_uart_state.rx_cmd.len = 0;
+        ble_hci_uart_state.rx_cmd.cur = 0;
+        break;
+
+    case BLE_HCI_UART_H4_ACL:
+        ble_hci_uart_state.rx_acl.buf =
+            os_msys_get_pkthdr(BLE_HCI_DATA_HDR_SZ, 0);
+        assert(ble_hci_uart_state.rx_acl.buf != NULL);
+
+        ble_hci_uart_state.rx_acl.len = 0;
+        break;
+
+    default:
+        ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
+        return -1;
+    }
+
+    return 0;
+}
+
+static void
+ble_hci_uart_rx_cmd(uint8_t data)
+{
+    int rc;
+
+    ble_hci_uart_state.rx_cmd.data[ble_hci_uart_state.rx_cmd.cur++] = data;
+
+    if (ble_hci_uart_state.rx_cmd.cur < BLE_HCI_CMD_HDR_LEN) {
+        return;
+    }
+
+    if (ble_hci_uart_state.rx_cmd.cur == BLE_HCI_CMD_HDR_LEN) {
+        ble_hci_uart_state.rx_cmd.len = ble_hci_uart_state.rx_cmd.data[2] +
+                                         BLE_HCI_CMD_HDR_LEN;
+    }
+
+    if (ble_hci_uart_state.rx_cmd.cur == ble_hci_uart_state.rx_cmd.len) {
+        assert(ble_hci_uart_rx_cmd_cb != NULL);
+        rc = ble_hci_uart_rx_cmd_cb(ble_hci_uart_state.rx_cmd.data,
+                                    ble_hci_uart_rx_cmd_arg);
+        if (rc != 0) {
+            ble_hci_trans_buf_free(ble_hci_uart_state.rx_cmd.data);
+        }
+        ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
+    }
+}
+
+static void
+ble_hci_uart_rx_evt(uint8_t data)
+{
+    int rc;
+
+    ble_hci_uart_state.rx_cmd.data[ble_hci_uart_state.rx_cmd.cur++] = data;
+
+    if (ble_hci_uart_state.rx_cmd.cur < BLE_HCI_EVENT_HDR_LEN) {
+        return;
+    }
+
+    if (ble_hci_uart_state.rx_cmd.cur == BLE_HCI_EVENT_HDR_LEN) {
+        ble_hci_uart_state.rx_cmd.len = ble_hci_uart_state.rx_cmd.data[1] +
+                                        BLE_HCI_EVENT_HDR_LEN;
+    }
+
+    if (ble_hci_uart_state.rx_cmd.cur == ble_hci_uart_state.rx_cmd.len) {
+        assert(ble_hci_uart_rx_cmd_cb != NULL);
+        rc = ble_hci_uart_rx_cmd_cb(ble_hci_uart_state.rx_cmd.data,
+                                    ble_hci_uart_rx_cmd_arg);
+        if (rc != 0) {
+            ble_hci_trans_buf_free(ble_hci_uart_state.rx_cmd.data);
+        }
+        ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
+    }
+}
+
+static void
+ble_hci_uart_rx_acl(uint8_t data)
+{
+    uint16_t pktlen;
+
+    os_mbuf_append(ble_hci_uart_state.rx_acl.buf, &data, 1);
+
+    pktlen = OS_MBUF_PKTLEN(ble_hci_uart_state.rx_acl.buf);
+
+    if (pktlen < BLE_HCI_DATA_HDR_SZ) {
+        return;
+    }
+
+    if (pktlen == BLE_HCI_DATA_HDR_SZ) {
+        os_mbuf_copydata(ble_hci_uart_state.rx_acl.buf, 2,
+                         sizeof(ble_hci_uart_state.rx_acl.len),
+                         &ble_hci_uart_state.rx_acl.len);
+        ble_hci_uart_state.rx_acl.len =
+            le16toh(&ble_hci_uart_state.rx_acl.len) + BLE_HCI_DATA_HDR_SZ;
+    }
+
+    if (pktlen == ble_hci_uart_state.rx_acl.len) {
+        assert(ble_hci_uart_rx_cmd_cb != NULL);
+        ble_hci_uart_rx_acl_cb(ble_hci_uart_state.rx_acl.buf,
+                               ble_hci_uart_rx_acl_arg);
+        ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
+    }
+}
+
+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);
+    case BLE_HCI_UART_H4_CMD:
+        ble_hci_uart_rx_cmd(data);
+        return 0;
+    case BLE_HCI_UART_H4_EVT:
+        ble_hci_uart_rx_evt(data);
+        return 0;
+    case BLE_HCI_UART_H4_ACL:
+        ble_hci_uart_rx_acl(data);
+        return 0;
+    default:
+        return -1;
+    }
+}
+
+static void
+ble_hci_uart_set_rx_cbs(ble_hci_trans_rx_cmd_fn *cmd_cb,
+                        void *cmd_arg,
+                        ble_hci_trans_rx_acl_fn *acl_cb,
+                        void *acl_arg)
+{
+    ble_hci_uart_rx_cmd_cb = cmd_cb;
+    ble_hci_uart_rx_cmd_arg = cmd_arg;
+    ble_hci_uart_rx_acl_cb = acl_cb;
+    ble_hci_uart_rx_acl_arg = acl_arg;
+}
+
+static void
+ble_hci_uart_free_pkt(uint8_t type, uint8_t *cmdevt, struct os_mbuf *acl)
+{
+    switch (type) {
+    case BLE_HCI_UART_H4_NONE:
+        break;
+
+    case BLE_HCI_UART_H4_CMD:
+    case BLE_HCI_UART_H4_EVT:
+        ble_hci_trans_buf_free(cmdevt);
+        break;
+
+    case BLE_HCI_UART_H4_ACL:
+        os_mbuf_free_chain(acl);
+        break;
+
+    default:
+        assert(0);
+        break;
+    }
+}
+
+static void
+ble_hci_uart_free_mem(void)
+{
+    free(ble_hci_uart_evt_buf);
+    ble_hci_uart_evt_buf = NULL;
+
+    free(ble_hci_uart_pkt_buf);
+    ble_hci_uart_pkt_buf = NULL;
+}
+
+static int
+ble_hci_uart_config(void)
+{
+    int rc;
+
+    rc = hal_uart_init_cbs(ble_hci_uart_cfg.uart_port,
+                           ble_hci_uart_tx_char, NULL,
+                           ble_hci_uart_rx_char, NULL);
+    if (rc != 0) {
+        return BLE_ERR_UNSPECIFIED;
+    }
+
+    rc = hal_uart_config(ble_hci_uart_cfg.uart_port,
+                         ble_hci_uart_cfg.baud,
+                         ble_hci_uart_cfg.data_bits,
+                         ble_hci_uart_cfg.stop_bits,
+                         ble_hci_uart_cfg.parity,
+                         ble_hci_uart_cfg.flow_ctrl);
+    if (rc != 0) {
+        return BLE_ERR_HW_FAIL;
+    }
+
+    return 0;
+}
+
+/**
+ * Sends an HCI event from the controller to the host.
+ *
+ * @param cmd                   The HCI event to send.  This buffer must be
+ *                                  allocated via ble_hci_trans_buf_alloc().
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_trans_ll_evt_tx(uint8_t *cmd)
+{
+    int rc;
+
+    rc = ble_hci_uart_cmdevt_tx(cmd, BLE_HCI_UART_H4_EVT);
+    return rc;
+}
+
+/**
+ * Sends ACL data from controller to host.
+ *
+ * @param om                    The ACL data packet to send.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    rc = ble_hci_uart_acl_tx(om);
+    return rc;
+}
+
+/**
+ * Sends an HCI command from the host to the controller.
+ *
+ * @param cmd                   The HCI command to send.  This buffer must be
+ *                                  allocated via ble_hci_trans_buf_alloc().
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_trans_hs_cmd_tx(uint8_t *cmd)
+{
+    int rc;
+
+    rc = ble_hci_uart_cmdevt_tx(cmd, BLE_HCI_UART_H4_CMD);
+    return rc;
+}
+
+/**
+ * Sends ACL data from host to controller.
+ *
+ * @param om                    The ACL data packet to send.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    rc = ble_hci_uart_acl_tx(om);
+    return rc;
+}
+
+/**
+ * Configures the HCI transport to call the specified callback upon receiving
+ * HCI packets from the controller.  This function should only be called by by
+ * host.
+ *
+ * @param cmd_cb                The callback to execute upon receiving an HCI
+ *                                  event.
+ * @param cmd_arg               Optional argument to pass to the command
+ *                                  callback.
+ * @param acl_cb                The callback to execute upon receiving ACL
+ *                                  data.
+ * @param acl_arg               Optional argument to pass to the ACL
+ *                                  callback.
+ */
+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)
+{
+    ble_hci_uart_set_rx_cbs(cmd_cb, cmd_arg, acl_cb, acl_arg);
+}
+
+/**
+ * Configures the HCI transport to operate with a host.  The transport will
+ * execute specified callbacks upon receiving HCI packets from the controller.
+ *
+ * @param cmd_cb                The callback to execute upon receiving an HCI
+ *                                  event.
+ * @param cmd_arg               Optional argument to pass to the command
+ *                                  callback.
+ * @param acl_cb                The callback to execute upon receiving ACL
+ *                                  data.
+ * @param acl_arg               Optional argument to pass to the ACL
+ *                                  callback.
+ */
+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)
+{
+    ble_hci_uart_set_rx_cbs(cmd_cb, cmd_arg, acl_cb, acl_arg);
+}
+
+/**
+ * Allocates a flat buffer of the specified type.
+ *
+ * @param type                  The type of buffer to allocate; one of the
+ *                                  BLE_HCI_TRANS_BUF_[...] constants.
+ *
+ * @return                      The allocated buffer on success;
+ *                              NULL on buffer exhaustion.
+ */
+uint8_t *
+ble_hci_trans_buf_alloc(int type)
+{
+    uint8_t *buf;
+
+    switch (type) {
+    case BLE_HCI_TRANS_BUF_CMD:
+    case BLE_HCI_TRANS_BUF_EVT_LO:
+    case BLE_HCI_TRANS_BUF_EVT_HI:
+        buf = os_memblock_get(&ble_hci_uart_evt_pool);
+        break;
+
+    default:
+        assert(0);
+        buf = NULL;
+    }
+
+    return buf;
+}
+
+/**
+ * Frees the specified flat buffer.  The buffer must have been allocated via
+ * ble_hci_trans_buf_alloc().
+ *
+ * @param buf                   The buffer to free.
+ */
+void
+ble_hci_trans_buf_free(uint8_t *buf)
+{
+    int rc;
+
+    rc = os_memblock_put(&ble_hci_uart_evt_pool, buf);
+    assert(rc == 0);
+}
+
+/**
+ * Resets the HCI UART transport to a clean state.  Frees all buffers and
+ * reconfigures the UART.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_trans_reset(void)
+{
+    struct ble_hci_uart_pkt *pkt;
+    int rc;
+
+    /* Close the UART to prevent race conditions as the buffers are freed. */
+    rc = hal_uart_close(ble_hci_uart_cfg.uart_port);
+    if (rc != 0) {
+        return BLE_ERR_HW_FAIL;
+    }
+
+    ble_hci_uart_free_pkt(ble_hci_uart_state.rx_type,
+                          ble_hci_uart_state.rx_cmd.data,
+                          ble_hci_uart_state.rx_acl.buf);
+    ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
+
+    ble_hci_uart_free_pkt(ble_hci_uart_state.tx_type,
+                          ble_hci_uart_state.tx_cmd.data,
+                          ble_hci_uart_state.tx_acl);
+    ble_hci_uart_state.tx_type = BLE_HCI_UART_H4_NONE;
+
+    while ((pkt = STAILQ_FIRST(&ble_hci_uart_state.tx_pkts)) != NULL) {
+        STAILQ_REMOVE(&ble_hci_uart_state.tx_pkts, pkt, ble_hci_uart_pkt,
+                      next);
+        ble_hci_uart_free_pkt(pkt->type, pkt->data, pkt->data);
+        os_memblock_put(&ble_hci_uart_pkt_pool, pkt);
+    }
+
+    /* Reopen the UART. */
+    rc = ble_hci_uart_config();
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+/**
+ * Initializes the UART HCI transport module.
+ *
+ * @param cfg                   The settings to initialize the HCI UART
+ *                                  transport with.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
+int
+ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg)
+{
+    int rc;
+
+    ble_hci_uart_free_mem();
+
+    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");
+    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;
+        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");
+    if (rc != 0) {
+        rc = BLE_ERR_UNSPECIFIED;
+        goto err;
+    }
+
+    rc = ble_hci_uart_config();
+    if (rc != 0) {
+        goto err;
+    }
+
+    memset(&ble_hci_uart_state, 0, sizeof ble_hci_uart_state);
+    STAILQ_INIT(&ble_hci_uart_state.tx_pkts);
+
+    return 0;
+
+err:
+    ble_hci_uart_free_mem();
+    return rc;
+}


[03/14] incubator-mynewt-core git commit: console - Fix whitespace issues.

Posted by cc...@apache.org.
console - Fix whitespace issues.


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

Branch: refs/heads/develop
Commit: 1f8bbd8c08e127c63e931d7d8f5b1a025c39f00d
Parents: 21638d9
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Aug 1 14:41:00 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Aug 3 19:54:15 2016 -0700

----------------------------------------------------------------------
 libs/console/full/src/cons_tty.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1f8bbd8c/libs/console/full/src/cons_tty.c
----------------------------------------------------------------------
diff --git a/libs/console/full/src/cons_tty.c b/libs/console/full/src/cons_tty.c
index fb2015c..9c576e2 100644
--- a/libs/console/full/src/cons_tty.c
+++ b/libs/console/full/src/cons_tty.c
@@ -203,13 +203,13 @@ console_read(char *str, int cnt, int *newline)
             break;
         }
 
-	if ((i & (CONSOLE_RX_CHUNK - 1)) == (CONSOLE_RX_CHUNK - 1)) {
-		/*
-		 * Make a break from blocking interrupts during the copy.
-		 */
-		OS_EXIT_CRITICAL(sr);
-		OS_ENTER_CRITICAL(sr);
-	}
+        if ((i & (CONSOLE_RX_CHUNK - 1)) == (CONSOLE_RX_CHUNK - 1)) {
+            /*
+             * Make a break from blocking interrupts during the copy.
+             */
+            OS_EXIT_CRITICAL(sr);
+            OS_ENTER_CRITICAL(sr);
+        }
 
         ch = console_pull_char(cr);
         if (ch == '\n') {