You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/03/16 08:50:38 UTC

[GitHub] andrzej-kaczmarek closed pull request #907: nimble: Remove NimBLE code from core repository

andrzej-kaczmarek closed pull request #907: nimble: Remove NimBLE code from core repository
URL: https://github.com/apache/mynewt-core/pull/907
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/drivers/nimble/native/include/ble/xcvr.h b/hw/drivers/nimble/native/include/ble/xcvr.h
deleted file mode 100644
index 04741dd30..000000000
--- a/hw/drivers/nimble/native/include/ble/xcvr.h
+++ /dev/null
@@ -1,46 +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_XCVR_
-#define H_BLE_XCVR_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Transceiver specific defintions */
-#define XCVR_RX_START_DELAY_USECS     (140)
-#define XCVR_TX_START_DELAY_USECS     (140)
-#define XCVR_PROC_DELAY_USECS         (100)
-#define XCVR_TX_SCHED_DELAY_USECS     \
-    (XCVR_TX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-#define XCVR_RX_SCHED_DELAY_USECS     \
-    (XCVR_RX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-
-/*
- * Define HW whitelist size. This is the total possible whitelist size;
- * not necessarily the size that will be used (may be smaller)
- */
-#define BLE_HW_WHITE_LIST_SIZE        (0)
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_XCVR_ */
diff --git a/hw/drivers/nimble/native/pkg.yml b/hw/drivers/nimble/native/pkg.yml
index f57af6272..6f76f991a 100644
--- a/hw/drivers/nimble/native/pkg.yml
+++ b/hw/drivers/nimble/native/pkg.yml
@@ -18,13 +18,7 @@
 #
 
 pkg.name: hw/drivers/nimble/native
-pkg.description: BLE driver for simulations.
 pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-    - ble
-    - bluetooth
-
-pkg.apis: ble_driver
 pkg.deps:
-    - net/nimble/controller
+    - "@apache-mynewt-nimble/nimble/drivers/native"
diff --git a/hw/drivers/nimble/native/src/ble_hw.c b/hw/drivers/nimble/native/src/ble_hw.c
deleted file mode 100644
index aa5046676..000000000
--- a/hw/drivers/nimble/native/src/ble_hw.c
+++ /dev/null
@@ -1,239 +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 "syscfg/syscfg.h"
-#include "os/os.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "controller/ble_hw.h"
-
-/* Total number of white list elements supported by nrf52 */
-#define BLE_HW_WHITE_LIST_SIZE      (0)
-
-/* We use this to keep track of which entries are set to valid addresses */
-static uint8_t g_ble_hw_whitelist_mask;
-
-/* Returns public device address or -1 if not present */
-int
-ble_hw_get_public_addr(ble_addr_t *addr)
-{
-    return -1;
-}
-
-/* Returns random static address or -1 if not present */
-int
-ble_hw_get_static_addr(ble_addr_t *addr)
-{
-    return -1;
-}
-
-/**
- * Clear the whitelist
- *
- * @return int
- */
-void
-ble_hw_whitelist_clear(void)
-{
-    g_ble_hw_whitelist_mask = 0;
-}
-
-/**
- * Add a device to the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_whitelist_add(uint8_t *addr, uint8_t addr_type)
-{
-    return BLE_ERR_MEM_CAPACITY;
-}
-
-/**
- * Remove a device from the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- */
-void
-ble_hw_whitelist_rmv(uint8_t *addr, uint8_t addr_type)
-{
-    return;
-}
-
-/**
- * Returns the size of the whitelist in HW
- *
- * @return int Number of devices allowed in whitelist
- */
-uint8_t
-ble_hw_whitelist_size(void)
-{
-    return BLE_HW_WHITE_LIST_SIZE;
-}
-
-/**
- * Enable the whitelisted devices
- */
-void
-ble_hw_whitelist_enable(void)
-{
-    return;
-}
-
-/**
- * Disables the whitelisted devices
- */
-void
-ble_hw_whitelist_disable(void)
-{
-    return;
-}
-
-/**
- * Boolean function which returns true ('1') if there is a match on the
- * whitelist.
- *
- * @return int
- */
-int
-ble_hw_whitelist_match(void)
-{
-    return 0;
-}
-
-/* Encrypt data */
-int
-ble_hw_encrypt_block(struct ble_encryption_block *ecb)
-{
-    return -1;
-}
-
-/**
- * Initialize the random number generator
- *
- * @param cb
- * @param bias
- *
- * @return int
- */
-int
-ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias)
-{
-    return -1;
-}
-
-/**
- * Start the random number generator
- *
- * @return int
- */
-int
-ble_hw_rng_start(void)
-{
-    return -1;
-}
-
-/**
- * Stop the random generator
- *
- * @return int
- */
-int
-ble_hw_rng_stop(void)
-{
-    return -1;
-}
-
-/**
- * Read the random number generator.
- *
- * @return uint8_t
- */
-uint8_t
-ble_hw_rng_read(void)
-{
-    return 0;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-/**
- * Clear the resolving list
- *
- * @return int
- */
-void
-ble_hw_resolv_list_clear(void)
-{
-}
-
-/**
- * Add a device to the hw resolving list
- *
- * @param irk   Pointer to IRK to add
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_resolv_list_add(uint8_t *irk)
-{
-    return BLE_ERR_MEM_CAPACITY;
-}
-
-/**
- * Remove a device from the hw resolving list
- *
- * @param index Index of IRK to remove
- */
-void
-ble_hw_resolv_list_rmv(int index)
-{
-}
-
-/**
- * Returns the size of the resolving list. NOTE: this returns the maximum
- * allowable entries in the HW. Configuration options may limit this.
- *
- * @return int Number of devices allowed in resolving list
- */
-uint8_t
-ble_hw_resolv_list_size(void)
-{
-    return 0;
-}
-
-/**
- * Called to determine if the address received was resolved.
- *
- * @return int  Negative values indicate unresolved address; positive values
- *              indicate index in resolving list of resolved address.
- */
-int
-ble_hw_resolv_list_match(void)
-{
-    return -1;
-}
-#endif
diff --git a/hw/drivers/nimble/native/src/ble_phy.c b/hw/drivers/nimble/native/src/ble_phy.c
deleted file mode 100644
index 1450585ba..000000000
--- a/hw/drivers/nimble/native/src/ble_phy.c
+++ /dev/null
@@ -1,632 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "ble/xcvr.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "controller/ble_phy.h"
-#include "controller/ble_ll.h"
-
-/* BLE PHY data structure */
-struct ble_phy_obj
-{
-    uint8_t phy_stats_initialized;
-    int8_t  phy_txpwr_dbm;
-    uint8_t phy_chan;
-    uint8_t phy_state;
-    uint8_t phy_transition;
-    uint8_t phy_rx_started;
-    uint8_t phy_encrypted;
-    uint8_t phy_privacy;
-    uint8_t phy_tx_pyld_len;
-    uint32_t phy_aar_scratch;
-    uint32_t phy_access_address;
-    struct ble_mbuf_hdr rxhdr;
-    void *txend_arg;
-    uint8_t *rxdptr;
-    ble_phy_tx_end_func txend_cb;
-};
-struct ble_phy_obj g_ble_phy_data;
-
-/* Statistics */
-struct ble_phy_statistics
-{
-    uint32_t tx_good;
-    uint32_t tx_fail;
-    uint32_t tx_late;
-    uint32_t tx_bytes;
-    uint32_t rx_starts;
-    uint32_t rx_aborts;
-    uint32_t rx_valid;
-    uint32_t rx_crc_err;
-    uint32_t phy_isrs;
-    uint32_t radio_state_errs;
-    uint32_t no_bufs;
-};
-
-struct ble_phy_statistics g_ble_phy_stats;
-
-static uint8_t g_ble_phy_tx_buf[BLE_PHY_MAX_PDU_LEN];
-
-/* XCVR object to emulate transceiver */
-struct xcvr_data
-{
-    uint32_t irq_status;
-};
-static struct xcvr_data g_xcvr_data;
-
-#define BLE_XCVR_IRQ_F_RX_START     (0x00000001)
-#define BLE_XCVR_IRQ_F_RX_END       (0x00000002)
-#define BLE_XCVR_IRQ_F_TX_START     (0x00000004)
-#define BLE_XCVR_IRQ_F_TX_END       (0x00000008)
-#define BLE_XCVR_IRQ_F_BYTE_CNTR    (0x00000010)
-
-/* "Rail" power level if outside supported range */
-#define BLE_XCVR_TX_PWR_MAX_DBM     (30)
-#define BLE_XCVR_TX_PWR_MIN_DBM     (-20)
-
-/* Statistics */
-STATS_SECT_START(ble_phy_stats)
-    STATS_SECT_ENTRY(phy_isrs)
-    STATS_SECT_ENTRY(tx_good)
-    STATS_SECT_ENTRY(tx_fail)
-    STATS_SECT_ENTRY(tx_late)
-    STATS_SECT_ENTRY(tx_bytes)
-    STATS_SECT_ENTRY(rx_starts)
-    STATS_SECT_ENTRY(rx_aborts)
-    STATS_SECT_ENTRY(rx_valid)
-    STATS_SECT_ENTRY(rx_crc_err)
-    STATS_SECT_ENTRY(rx_late)
-    STATS_SECT_ENTRY(no_bufs)
-    STATS_SECT_ENTRY(radio_state_errs)
-    STATS_SECT_ENTRY(rx_hw_err)
-    STATS_SECT_ENTRY(tx_hw_err)
-STATS_SECT_END
-STATS_SECT_DECL(ble_phy_stats) ble_phy_stats;
-
-STATS_NAME_START(ble_phy_stats)
-    STATS_NAME(ble_phy_stats, phy_isrs)
-    STATS_NAME(ble_phy_stats, tx_good)
-    STATS_NAME(ble_phy_stats, tx_fail)
-    STATS_NAME(ble_phy_stats, tx_late)
-    STATS_NAME(ble_phy_stats, tx_bytes)
-    STATS_NAME(ble_phy_stats, rx_starts)
-    STATS_NAME(ble_phy_stats, rx_aborts)
-    STATS_NAME(ble_phy_stats, rx_valid)
-    STATS_NAME(ble_phy_stats, rx_crc_err)
-    STATS_NAME(ble_phy_stats, rx_late)
-    STATS_NAME(ble_phy_stats, no_bufs)
-    STATS_NAME(ble_phy_stats, radio_state_errs)
-    STATS_NAME(ble_phy_stats, rx_hw_err)
-    STATS_NAME(ble_phy_stats, tx_hw_err)
-STATS_NAME_END(ble_phy_stats)
-
-/* XXX: TODO:
-
- * 1) Test the following to make sure it works: suppose an event is already
- * set to 1 and the interrupt is not enabled. What happens if you enable the
- * interrupt with the event bit already set to 1
- * 2) how to deal with interrupts?
- */
-static uint32_t
-ble_xcvr_get_irq_status(void)
-{
-    return g_xcvr_data.irq_status;
-}
-
-static void
-ble_xcvr_clear_irq(uint32_t mask)
-{
-    g_xcvr_data.irq_status &= ~mask;
-}
-
-/**
- * Copies the data from the phy receive buffer into a mbuf chain.
- *
- * @param dptr Pointer to receive buffer
- * @param rxpdu Pointer to already allocated mbuf chain
- *
- * NOTE: the packet header already has the total mbuf length in it. The
- * lengths of the individual mbufs are not set prior to calling.
- *
- */
-void
-ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu)
-{
-    uint16_t rem_bytes;
-    uint16_t mb_bytes;
-    uint16_t copylen;
-    uint32_t *dst;
-    uint32_t *src;
-    struct os_mbuf *m;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct os_mbuf_pkthdr *pkthdr;
-
-    /* Better be aligned */
-    assert(((uint32_t)dptr & 3) == 0);
-
-    pkthdr = OS_MBUF_PKTHDR(rxpdu);
-    rem_bytes = pkthdr->omp_len;
-
-    /* Fill in the mbuf pkthdr first. */
-    dst = (uint32_t *)(rxpdu->om_data);
-    src = (uint32_t *)dptr;
-
-    mb_bytes = (rxpdu->om_omp->omp_databuf_len - rxpdu->om_pkthdr_len - 4);
-    copylen = min(mb_bytes, rem_bytes);
-    copylen &= 0xFFFC;
-    rem_bytes -= copylen;
-    mb_bytes -= copylen;
-    rxpdu->om_len = copylen;
-    while (copylen > 0) {
-        *dst = *src;
-        ++dst;
-        ++src;
-        copylen -= 4;
-    }
-
-    /* Copy remaining bytes */
-    m = rxpdu;
-    while (rem_bytes > 0) {
-        /* If there are enough bytes in the mbuf, copy them and leave */
-        if (rem_bytes <= mb_bytes) {
-            memcpy(m->om_data + m->om_len, src, rem_bytes);
-            m->om_len += rem_bytes;
-            break;
-        }
-
-        m = SLIST_NEXT(m, om_next);
-        assert(m != NULL);
-
-        mb_bytes = m->om_omp->omp_databuf_len;
-        copylen = min(mb_bytes, rem_bytes);
-        copylen &= 0xFFFC;
-        rem_bytes -= copylen;
-        mb_bytes -= copylen;
-        m->om_len = copylen;
-        dst = (uint32_t *)m->om_data;
-        while (copylen > 0) {
-            *dst = *src;
-            ++dst;
-            ++src;
-            copylen -= 4;
-        }
-    }
-
-    /* Copy ble header */
-    ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
-    memcpy(ble_hdr, &g_ble_phy_data.rxhdr, sizeof(struct ble_mbuf_hdr));
-}
-
-void
-ble_phy_isr(void)
-{
-    int rc;
-    uint8_t transition;
-    uint32_t irq_en;
-    struct ble_mbuf_hdr *ble_hdr;
-
-    /* Check for disabled event. This only happens for transmits now */
-    irq_en = ble_xcvr_get_irq_status();
-    if (irq_en & BLE_XCVR_IRQ_F_TX_END) {
-
-        /* Better be in TX state! */
-        assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
-        ble_xcvr_clear_irq(BLE_XCVR_IRQ_F_TX_END);
-
-        transition = g_ble_phy_data.phy_transition;
-        if (transition == BLE_PHY_TRANSITION_TX_RX) {
-            /* Disable the phy */
-            /* XXX: count no bufs? */
-            ble_phy_disable();
-        } else {
-            /* Better not be going from rx to tx! */
-            assert(transition == BLE_PHY_TRANSITION_NONE);
-        }
-    }
-
-    /* We get this if we have started to receive a frame */
-    if (irq_en & BLE_XCVR_IRQ_F_RX_START) {
-
-        ble_xcvr_clear_irq(BLE_XCVR_IRQ_F_RX_START);
-
-        /* Call Link Layer receive start function */
-        rc = ble_ll_rx_start(g_ble_phy_data.rxdptr, g_ble_phy_data.phy_chan,
-                             &g_ble_phy_data.rxhdr);
-        if (rc >= 0) {
-            /* XXX: set rx end enable isr */
-        } else {
-            /* Disable PHY */
-            ble_phy_disable();
-            irq_en = 0;
-            ++g_ble_phy_stats.rx_aborts;
-        }
-
-        /* Count rx starts */
-        ++g_ble_phy_stats.rx_starts;
-    }
-
-    /* Receive packet end (we dont enable this for transmit) */
-    if (irq_en & BLE_XCVR_IRQ_F_RX_END) {
-
-        ble_xcvr_clear_irq(BLE_XCVR_IRQ_F_RX_END);
-
-        /* Construct BLE header before handing up */
-        ble_hdr = &g_ble_phy_data.rxhdr;
-        ble_hdr->rxinfo.flags = 0;
-        ble_hdr->rxinfo.rssi = -77;    /* XXX: dummy rssi */
-        ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
-        ble_hdr->rxinfo.phy = BLE_PHY_1M;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        ble_hdr->rxinfo.aux_data = NULL;
-#endif
-
-        /* Count PHY valid packets */
-        ++g_ble_phy_stats.rx_valid;
-        ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
-
-        /* Call Link Layer receive payload function */
-        rc = ble_ll_rx_end(g_ble_phy_data.rxdptr, ble_hdr);
-        if (rc < 0) {
-            /* Disable the PHY. */
-            ble_phy_disable();
-        }
-    }
-
-    /* Count # of interrupts */
-    ++g_ble_phy_stats.phy_isrs;
-}
-
-/**
- * ble phy init
- *
- * Initialize the PHY. This is expected to be called once.
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_init(void)
-{
-    /* Set phy channel to an invalid channel so first set channel works */
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
-    g_ble_phy_data.phy_chan = BLE_PHY_NUM_CHANS;
-
-    /* XXX: emulate ISR? */
-
-    return 0;
-}
-
-int
-ble_phy_rx(void)
-{
-    /* Check radio state */
-    if (ble_phy_state_get() != BLE_PHY_STATE_IDLE) {
-        ble_phy_disable();
-        ++g_ble_phy_stats.radio_state_errs;
-        return BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
-
-    return 0;
-}
-
-void
-ble_phy_restart_rx(void)
-{
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
-void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
-{
-}
-
-void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
-{
-}
-
-void
-ble_phy_encrypt_disable(void)
-{
-}
-#endif
-
-void
-ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
-{
-    /* Set transmit end callback and arg */
-    g_ble_phy_data.txend_cb = txend_cb;
-    g_ble_phy_data.txend_arg = arg;
-}
-
-/**
- * Called to set the start time of a transmission.
- *
- * This function is called to set the start time when we are not going from
- * rx to tx automatically.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime
- * @param rem_usecs
- *
- * @return int
- */
-int
-ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    return 0;
-}
-
-/**
- * Called to set the start time of a reception
- *
- * This function acts a bit differently than transmit. If we are late getting
- * here we will still attempt to receive.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime
- * @param rem_usecs
- *
- * @return int
- */
-int
-ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    return 0;
-}
-
-
-int
-ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
-{
-    uint8_t hdr_byte;
-    int rc;
-
-    if (ble_phy_state_get() != BLE_PHY_STATE_IDLE) {
-        ble_phy_disable();
-        ++g_ble_phy_stats.radio_state_errs;
-        return BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    /* Select tx address */
-    if (g_ble_phy_data.phy_chan < BLE_PHY_NUM_DATA_CHANS) {
-        /* XXX: fix this */
-        assert(0);
-    } else {
-    }
-
-    /* Set the PHY transition */
-    g_ble_phy_data.phy_transition = end_trans;
-
-    /* Set phy state to transmitting and count packet statistics */
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
-    ++g_ble_phy_stats.tx_good;
-    g_ble_phy_stats.tx_bytes += pducb(g_ble_phy_tx_buf, pducb_arg, &hdr_byte) +
-                                BLE_LL_PDU_HDR_LEN;
-    rc = BLE_ERR_SUCCESS;
-
-    return rc;
-}
-
-/**
- * ble phy txpwr set
- *
- * Set the transmit output power (in dBm).
- *
- * NOTE: If the output power specified is within the BLE limits but outside
- * the chip limits, we "rail" the power level so we dont exceed the min/max
- * chip values.
- *
- * @param dbm Power output in dBm.
- *
- * @return int 0: success; anything else is an error
- */
-int
-ble_phy_txpwr_set(int dbm)
-{
-    /* Check valid range */
-    assert(dbm <= BLE_PHY_MAX_PWR_DBM);
-
-    /* "Rail" power level if outside supported range */
-    if (dbm > BLE_XCVR_TX_PWR_MAX_DBM) {
-        dbm = BLE_XCVR_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < BLE_XCVR_TX_PWR_MIN_DBM) {
-            dbm = BLE_XCVR_TX_PWR_MIN_DBM;
-        }
-    }
-
-    g_ble_phy_data.phy_txpwr_dbm = dbm;
-
-    return 0;
-}
-
-/**
- * ble phy txpwr round
- *
- * Get the rounded transmit output power (in dBm).
- *
- * @param dbm Power output in dBm.
- *
- * @return int Rounded power in dBm
- */
-int ble_phy_txpower_round(int dbm)
-{
-    /* "Rail" power level if outside supported range */
-    if (dbm > BLE_XCVR_TX_PWR_MAX_DBM) {
-        dbm = BLE_XCVR_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < BLE_XCVR_TX_PWR_MIN_DBM) {
-            dbm = BLE_XCVR_TX_PWR_MIN_DBM;
-        }
-    }
-
-    return dbm;
-}
-
-/**
- * ble phy txpwr get
- *
- * Get the transmit power.
- *
- * @return int  The current PHY transmit power, in dBm
- */
-int
-ble_phy_txpwr_get(void)
-{
-    return g_ble_phy_data.phy_txpwr_dbm;
-}
-
-/**
- * ble phy setchan
- *
- * Sets the logical frequency of the transceiver. The input parameter is the
- * BLE channel index (0 to 39, inclusive). The NRF52 frequency register
- * works like this: logical frequency = 2400 + FREQ (MHz).
- *
- * Thus, to get a logical frequency of 2402 MHz, you would program the
- * FREQUENCY register to 2.
- *
- * @param chan This is the Data Channel Index or Advertising Channel index
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
-{
-    assert(chan < BLE_PHY_NUM_CHANS);
-
-    /* Check for valid channel range */
-    if (chan >= BLE_PHY_NUM_CHANS) {
-        return BLE_PHY_ERR_INV_PARAM;
-    }
-
-    g_ble_phy_data.phy_access_address = access_addr;
-
-    g_ble_phy_data.phy_chan = chan;
-
-    return 0;
-}
-
-/**
- * Disable the PHY. This will do the following:
- *  -> Turn off all phy interrupts.
- *  -> Disable internal shortcuts.
- *  -> Disable the radio.
- *  -> Sets phy state to idle.
- */
-void
-ble_phy_disable(void)
-{
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
-}
-
-/* Gets the current access address */
-uint32_t ble_phy_access_addr_get(void)
-{
-    return g_ble_phy_data.phy_access_address;
-}
-
-/**
- * Return the phy state
- *
- * @return int The current PHY state.
- */
-int
-ble_phy_state_get(void)
-{
-    return g_ble_phy_data.phy_state;
-}
-
-/**
- * Called to see if a reception has started
- *
- * @return int
- */
-int
-ble_phy_rx_started(void)
-{
-    return g_ble_phy_data.phy_rx_started;
-}
-
-/**
- * Called to return the maximum data pdu payload length supported by the
- * phy. For this chip, if encryption is enabled, the maximum payload is 27
- * bytes.
- *
- * @return uint8_t Maximum data channel PDU payload size supported
- */
-uint8_t
-ble_phy_max_data_pdu_pyld(void)
-{
-    return BLE_LL_DATA_PDU_MAX_PYLD;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-void
-ble_phy_resolv_list_enable(void)
-{
-    g_ble_phy_data.phy_privacy = 1;
-}
-
-void
-ble_phy_resolv_list_disable(void)
-{
-    g_ble_phy_data.phy_privacy = 0;
-}
-
-/**
- * Return the transceiver state
- *
- * @return int transceiver state.
- */
-uint8_t
-ble_phy_xcvr_state_get(void)
-{
-   return g_ble_phy_data.phy_state;
-}
-
-#endif
-
-void
-ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs)
-{
-}
diff --git a/hw/drivers/nimble/nrf51/include/ble/xcvr.h b/hw/drivers/nimble/nrf51/include/ble/xcvr.h
deleted file mode 100644
index 848c8cc84..000000000
--- a/hw/drivers/nimble/nrf51/include/ble/xcvr.h
+++ /dev/null
@@ -1,48 +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_XCVR_
-#define H_BLE_XCVR_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Transceiver specific defintions */
-/* NOTE: we have to account for the RTC output compare issue */
-#define XCVR_PROC_DELAY_USECS         (230)
-
-#define XCVR_RX_START_DELAY_USECS     (140)
-#define XCVR_TX_START_DELAY_USECS     (140)
-#define XCVR_TX_SCHED_DELAY_USECS     \
-    (XCVR_TX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-#define XCVR_RX_SCHED_DELAY_USECS     \
-    (XCVR_RX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-
-/*
- * Define HW whitelist size. This is the total possible whitelist size;
- * not necessarily the size that will be used (may be smaller)
- */
-#define BLE_HW_WHITE_LIST_SIZE        (8)
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_XCVR_ */
diff --git a/hw/drivers/nimble/nrf51/pkg.yml b/hw/drivers/nimble/nrf51/pkg.yml
index 6c427b54f..14a0cd122 100644
--- a/hw/drivers/nimble/nrf51/pkg.yml
+++ b/hw/drivers/nimble/nrf51/pkg.yml
@@ -18,14 +18,7 @@
 #
 
 pkg.name: hw/drivers/nimble/nrf51
-pkg.description: BLE driver for nRF51 systems.
 pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-    - ble
-    - bluetooth
-
-pkg.apis: ble_driver
 pkg.deps:
-    - net/nimble
-    - net/nimble/controller
+    - "@apache-mynewt-nimble/nimble/drivers/nrf51"
diff --git a/hw/drivers/nimble/nrf51/src/ble_hw.c b/hw/drivers/nimble/nrf51/src/ble_hw.c
deleted file mode 100644
index 42d034cca..000000000
--- a/hw/drivers/nimble/nrf51/src/ble_hw.c
+++ /dev/null
@@ -1,482 +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 "syscfg/syscfg.h"
-#include "os/os.h"
-#include "ble/xcvr.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "nrfx.h"
-#include "controller/ble_hw.h"
-#include "mcu/cmsis_nvic.h"
-
-/* Total number of resolving list elements */
-#define BLE_HW_RESOLV_LIST_SIZE     (16)
-
-/* We use this to keep track of which entries are set to valid addresses */
-static uint8_t g_ble_hw_whitelist_mask;
-
-/* Random number generator isr callback */
-ble_rng_isr_cb_t g_ble_rng_isr_cb;
-
-/* If LL privacy is enabled, allocate memory for AAR */
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-
-/* The NRF51 supports up to 16 IRK entries */
-#if (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE) < 16)
-#define NRF_IRK_LIST_ENTRIES    (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE))
-#else
-#define NRF_IRK_LIST_ENTRIES    (16)
-#endif
-
-/* NOTE: each entry is 16 bytes long. */
-uint32_t g_nrf_irk_list[NRF_IRK_LIST_ENTRIES * 4];
-
-/* Current number of IRK entries */
-uint8_t g_nrf_num_irks;
-
-#endif
-
-/* Returns public device address or -1 if not present */
-int
-ble_hw_get_public_addr(ble_addr_t *addr)
-{
-    int rc;
-    uint32_t addr_high;
-    uint32_t addr_low;
-
-    /* Does FICR have a public address */
-    rc = -1;
-    if ((NRF_FICR->DEVICEADDRTYPE & 1) == 0) {
-        addr_low = NRF_FICR->DEVICEADDR[0];
-        addr_high = NRF_FICR->DEVICEADDR[1];
-        rc = 0;
-    } else {
-        /* See if programmed in UICR. Upper 16 bits must all be zero */
-        addr_high = NRF_UICR->CUSTOMER[1];
-        if (addr_high < 65536) {
-            addr_low = NRF_UICR->CUSTOMER[0];
-            rc = 0;
-        }
-    }
-
-    if (!rc) {
-        /* Copy into device address. We can do this because we know platform */
-        memcpy(addr->val, &addr_low, 4);
-        memcpy(&addr->val[4], &addr_high, 2);
-        addr->type = BLE_ADDR_PUBLIC;
-    }
-
-    return rc;
-}
-
-/* Returns random static address or -1 if not present */
-int
-ble_hw_get_static_addr(ble_addr_t *addr)
-{
-    int rc;
-
-    if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) {
-        memcpy(addr->val, (void *)&NRF_FICR->DEVICEADDR[0], 4);
-        memcpy(&addr->val[4], (void *)&NRF_FICR->DEVICEADDR[1], 2);
-        addr->val[5] |= 0xc0;
-        addr->type = BLE_ADDR_RANDOM;
-        rc = 0;
-    } else {
-        rc = -1;
-    }
-
-    return rc;
-}
-
-/**
- * Clear the whitelist
- *
- * @return int
- */
-void
-ble_hw_whitelist_clear(void)
-{
-    NRF_RADIO->DACNF = 0;
-    g_ble_hw_whitelist_mask = 0;
-}
-
-/**
- * Add a device to the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_whitelist_add(uint8_t *addr, uint8_t addr_type)
-{
-    int i;
-    uint32_t mask;
-
-    /* Find first ununsed device address match element */
-    mask = 0x01;
-    for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) {
-        if ((mask & g_ble_hw_whitelist_mask) == 0) {
-            NRF_RADIO->DAB[i] = get_le32(addr);
-            NRF_RADIO->DAP[i] = get_le16(addr + 4);
-            if (addr_type == BLE_ADDR_RANDOM) {
-                NRF_RADIO->DACNF |= (mask << 8);
-            }
-            g_ble_hw_whitelist_mask |= mask;
-            return BLE_ERR_SUCCESS;
-        }
-        mask <<= 1;
-    }
-
-    return BLE_ERR_MEM_CAPACITY;
-}
-
-/**
- * Remove a device from the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- */
-void
-ble_hw_whitelist_rmv(uint8_t *addr, uint8_t addr_type)
-{
-    int i;
-    uint8_t cfg_addr;
-    uint16_t dap;
-    uint16_t txadd;
-    uint32_t dab;
-    uint32_t mask;
-
-    /* Find first ununsed device address match element */
-    dab = get_le32(addr);
-    dap = get_le16(addr + 4);
-    txadd = NRF_RADIO->DACNF >> 8;
-    mask = 0x01;
-    for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) {
-        if (mask & g_ble_hw_whitelist_mask) {
-            if ((dab == NRF_RADIO->DAB[i]) && (dap == NRF_RADIO->DAP[i])) {
-                cfg_addr = txadd & mask;
-                if (addr_type == BLE_ADDR_RANDOM) {
-                    if (cfg_addr != 0) {
-                        break;
-                    }
-                } else {
-                    if (cfg_addr == 0) {
-                        break;
-                    }
-                }
-            }
-        }
-        mask <<= 1;
-    }
-
-    if (i < BLE_HW_WHITE_LIST_SIZE) {
-        g_ble_hw_whitelist_mask &= ~mask;
-        NRF_RADIO->DACNF &= ~mask;
-    }
-}
-
-/**
- * Returns the size of the whitelist in HW
- *
- * @return int Number of devices allowed in whitelist
- */
-uint8_t
-ble_hw_whitelist_size(void)
-{
-    return BLE_HW_WHITE_LIST_SIZE;
-}
-
-/**
- * Enable the whitelisted devices
- */
-void
-ble_hw_whitelist_enable(void)
-{
-    /* Enable the configured device addresses */
-    NRF_RADIO->DACNF |= g_ble_hw_whitelist_mask;
-}
-
-/**
- * Disables the whitelisted devices
- */
-void
-ble_hw_whitelist_disable(void)
-{
-    /* Disable all whitelist devices */
-    NRF_RADIO->DACNF &= 0x0000ff00;
-}
-
-/**
- * Boolean function which returns true ('1') if there is a match on the
- * whitelist.
- *
- * @return int
- */
-int
-ble_hw_whitelist_match(void)
-{
-    return (int)NRF_RADIO->EVENTS_DEVMATCH;
-}
-
-/* Encrypt data */
-int
-ble_hw_encrypt_block(struct ble_encryption_block *ecb)
-{
-    int rc;
-    uint32_t end;
-    uint32_t err;
-
-    /* Stop ECB */
-    NRF_ECB->TASKS_STOPECB = 1;
-    /* XXX: does task stop clear these counters? Anyway to do this quicker? */
-    NRF_ECB->EVENTS_ENDECB = 0;
-    NRF_ECB->EVENTS_ERRORECB = 0;
-    NRF_ECB->ECBDATAPTR = (uint32_t)ecb;
-
-    /* Start ECB */
-    NRF_ECB->TASKS_STARTECB = 1;
-
-    /* Wait till error or done */
-    rc = 0;
-    while (1) {
-        end = NRF_ECB->EVENTS_ENDECB;
-        err = NRF_ECB->EVENTS_ERRORECB;
-        if (end || err) {
-            if (err) {
-                rc = -1;
-            }
-            break;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Random number generator ISR.
- */
-static void
-ble_rng_isr(void)
-{
-    uint8_t rnum;
-
-    /* No callback? Clear and disable interrupts */
-    if (g_ble_rng_isr_cb == NULL) {
-        NRF_RNG->INTENCLR = 1;
-        NRF_RNG->EVENTS_VALRDY = 0;
-        (void)NRF_RNG->SHORTS;
-        return;
-    }
-
-    /* If there is a value ready grab it */
-    if (NRF_RNG->EVENTS_VALRDY) {
-        NRF_RNG->EVENTS_VALRDY = 0;
-        rnum = (uint8_t)NRF_RNG->VALUE;
-        (*g_ble_rng_isr_cb)(rnum);
-    }
-}
-
-/**
- * Initialize the random number generator
- *
- * @param cb
- * @param bias
- *
- * @return int
- */
-int
-ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias)
-{
-    /* Set bias */
-    if (bias) {
-        NRF_RNG->CONFIG = 1;
-    } else {
-        NRF_RNG->CONFIG = 0;
-    }
-
-    /* If we were passed a function pointer we need to enable the interrupt */
-    if (cb != NULL) {
-        NVIC_SetPriority(RNG_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
-        NVIC_SetVector(RNG_IRQn, (uint32_t)ble_rng_isr);
-        NVIC_EnableIRQ(RNG_IRQn);
-        g_ble_rng_isr_cb = cb;
-    }
-
-    return 0;
-}
-
-/**
- * Start the random number generator
- *
- * @return int
- */
-int
-ble_hw_rng_start(void)
-{
-    os_sr_t sr;
-
-    /* No need for interrupt if there is no callback */
-    OS_ENTER_CRITICAL(sr);
-    NRF_RNG->EVENTS_VALRDY = 0;
-    if (g_ble_rng_isr_cb) {
-        NRF_RNG->INTENSET = 1;
-    }
-    NRF_RNG->TASKS_START = 1;
-    OS_EXIT_CRITICAL(sr);
-
-    return 0;
-}
-
-/**
- * Stop the random generator
- *
- * @return int
- */
-int
-ble_hw_rng_stop(void)
-{
-    os_sr_t sr;
-
-    /* No need for interrupt if there is no callback */
-    OS_ENTER_CRITICAL(sr);
-    NRF_RNG->INTENCLR = 1;
-    NRF_RNG->TASKS_STOP = 1;
-    NRF_RNG->EVENTS_VALRDY = 0;
-    OS_EXIT_CRITICAL(sr);
-
-    return 0;
-}
-
-/**
- * Read the random number generator.
- *
- * @return uint8_t
- */
-uint8_t
-ble_hw_rng_read(void)
-{
-    uint8_t rnum;
-
-    /* Wait for a sample */
-    while (NRF_RNG->EVENTS_VALRDY == 0) {
-    }
-
-    NRF_RNG->EVENTS_VALRDY = 0;
-    rnum = (uint8_t)NRF_RNG->VALUE;
-
-    return rnum;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY))
-/**
- * Clear the resolving list
- *
- * @return int
- */
-void
-ble_hw_resolv_list_clear(void)
-{
-    g_nrf_num_irks = 0;
-}
-
-/**
- * Add a device to the hw resolving list
- *
- * @param irk   Pointer to IRK to add
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_resolv_list_add(uint8_t *irk)
-{
-    uint32_t *nrf_entry;
-
-    /* Find first ununsed device address match element */
-    if (g_nrf_num_irks == NRF_IRK_LIST_ENTRIES) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-    /* Copy into irk list */
-    nrf_entry = &g_nrf_irk_list[4 * g_nrf_num_irks];
-    memcpy(nrf_entry, irk, 16);
-
-    /* Add to total */
-    ++g_nrf_num_irks;
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Remove a device from the hw resolving list
- *
- * @param index Index of IRK to remove
- */
-void
-ble_hw_resolv_list_rmv(int index)
-{
-    uint32_t *irk_entry;
-
-    if (index < g_nrf_num_irks) {
-        --g_nrf_num_irks;
-        irk_entry = &g_nrf_irk_list[index];
-        if (g_nrf_num_irks > index) {
-            memmove(irk_entry, irk_entry + 4, g_nrf_num_irks - index);
-        }
-    }
-}
-
-/**
- * Returns the size of the resolving list. NOTE: this returns the maximum
- * allowable entries in the HW. Configuration options may limit this.
- *
- * @return int Number of devices allowed in resolving list
- */
-uint8_t
-ble_hw_resolv_list_size(void)
-{
-    return BLE_HW_RESOLV_LIST_SIZE;
-}
-
-/**
- * Called to determine if the address received was resolved.
- *
- * @return int  Negative values indicate unresolved address; positive values
- *              indicate index in resolving list of resolved address.
- */
-int
-ble_hw_resolv_list_match(void)
-{
-    uint32_t index;
-
-    if (NRF_AAR->EVENTS_END) {
-        if (NRF_AAR->EVENTS_RESOLVED) {
-            index = NRF_AAR->STATUS;
-            return (int)index;
-        }
-    }
-
-    return -1;
-}
-#endif
diff --git a/hw/drivers/nimble/nrf51/src/ble_phy.c b/hw/drivers/nimble/nrf51/src/ble_phy.c
deleted file mode 100644
index d928f591b..000000000
--- a/hw/drivers/nimble/nrf51/src/ble_phy.c
+++ /dev/null
@@ -1,1487 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "ble/xcvr.h"
-#include "mcu/cmsis_nvic.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "controller/ble_phy.h"
-#include "controller/ble_ll.h"
-#include "nrfx.h"
-
-/* XXX: 4) Make sure RF is higher priority interrupt than schedule */
-
-/*
- * XXX: Maximum possible transmit time is 1 msec for a 60ppm crystal
- * and 16ms for a 30ppm crystal! We need to limit PDU size based on
- * crystal accuracy. Look at this in the spec.
- */
-
-/* XXX: private header file? */
-extern uint8_t g_nrf_num_irks;
-extern uint32_t g_nrf_irk_list[];
-
-/* To disable all radio interrupts */
-#define NRF_RADIO_IRQ_MASK_ALL  (0x34FF)
-
-/*
- * We configure the nrf with a 1 byte S0 field, 8 bit length field, and
- * zero bit S1 field. The preamble is 8 bits long.
- */
-#define NRF_LFLEN_BITS          (8)
-#define NRF_S0_LEN              (1)
-
-/* Maximum length of frames */
-#define NRF_MAXLEN              (255)
-#define NRF_BALEN               (3)     /* For base address of 3 bytes */
-
-/* Maximum tx power */
-#define NRF_TX_PWR_MAX_DBM      (4)
-#define NRF_TX_PWR_MIN_DBM      (-40)
-
-/* Max. encrypted payload length */
-#define NRF_MAX_ENCRYPTED_PYLD_LEN  (27)
-#define NRF_ENC_HDR_SIZE            (3)
-#define NRF_ENC_BUF_SIZE            \
-    (NRF_MAX_ENCRYPTED_PYLD_LEN + NRF_ENC_HDR_SIZE + BLE_LL_DATA_MIC_LEN)
-
-/* BLE PHY data structure */
-struct ble_phy_obj
-{
-    uint8_t phy_stats_initialized;
-    int8_t  phy_txpwr_dbm;
-    uint8_t phy_chan;
-    uint8_t phy_state;
-    uint8_t phy_transition;
-    uint8_t phy_rx_started;
-    uint8_t phy_encrypted;
-    uint8_t phy_privacy;
-    uint8_t phy_tx_pyld_len;
-    uint8_t *rxdptr;
-    uint32_t phy_aar_scratch;
-    uint32_t phy_access_address;
-    struct ble_mbuf_hdr rxhdr;
-    void *txend_arg;
-    ble_phy_tx_end_func txend_cb;
-    uint32_t phy_start_cputime;
-};
-struct ble_phy_obj g_ble_phy_data;
-
-/* XXX: if 27 byte packets desired we can make this smaller */
-/* Global transmit/receive buffer */
-static uint32_t g_ble_phy_tx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
-static uint32_t g_ble_phy_rx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/* Make sure word-aligned for faster copies */
-static uint32_t g_ble_phy_enc_buf[(NRF_ENC_BUF_SIZE + 3) / 4];
-#endif
-
-/* RF center frequency for each channel index (offset from 2400 MHz) */
-static const uint8_t g_ble_phy_chan_freq[BLE_PHY_NUM_CHANS] = {
-     4,  6,  8, 10, 12, 14, 16, 18, 20, 22, /* 0-9 */
-    24, 28, 30, 32, 34, 36, 38, 40, 42, 44, /* 10-19 */
-    46, 48, 50, 52, 54, 56, 58, 60, 62, 64, /* 20-29 */
-    66, 68, 70, 72, 74, 76, 78,  2, 26, 80, /* 30-39 */
-};
-
-/* Statistics */
-STATS_SECT_START(ble_phy_stats)
-    STATS_SECT_ENTRY(phy_isrs)
-    STATS_SECT_ENTRY(tx_good)
-    STATS_SECT_ENTRY(tx_fail)
-    STATS_SECT_ENTRY(tx_late)
-    STATS_SECT_ENTRY(tx_bytes)
-    STATS_SECT_ENTRY(rx_starts)
-    STATS_SECT_ENTRY(rx_aborts)
-    STATS_SECT_ENTRY(rx_valid)
-    STATS_SECT_ENTRY(rx_crc_err)
-    STATS_SECT_ENTRY(rx_late)
-    STATS_SECT_ENTRY(radio_state_errs)
-    STATS_SECT_ENTRY(rx_hw_err)
-    STATS_SECT_ENTRY(tx_hw_err)
-STATS_SECT_END
-STATS_SECT_DECL(ble_phy_stats) ble_phy_stats;
-
-STATS_NAME_START(ble_phy_stats)
-    STATS_NAME(ble_phy_stats, phy_isrs)
-    STATS_NAME(ble_phy_stats, tx_good)
-    STATS_NAME(ble_phy_stats, tx_fail)
-    STATS_NAME(ble_phy_stats, tx_late)
-    STATS_NAME(ble_phy_stats, tx_bytes)
-    STATS_NAME(ble_phy_stats, rx_starts)
-    STATS_NAME(ble_phy_stats, rx_aborts)
-    STATS_NAME(ble_phy_stats, rx_valid)
-    STATS_NAME(ble_phy_stats, rx_crc_err)
-    STATS_NAME(ble_phy_stats, rx_late)
-    STATS_NAME(ble_phy_stats, radio_state_errs)
-    STATS_NAME(ble_phy_stats, rx_hw_err)
-    STATS_NAME(ble_phy_stats, tx_hw_err)
-STATS_NAME_END(ble_phy_stats)
-
-/*
- * NOTE:
- * Tested the following to see what would happen:
- *  -> NVIC has radio irq enabled (interrupt # 1, mask 0x2).
- *  -> Set up nrf to receive. Clear ADDRESS event register.
- *  -> Enable ADDRESS interrupt on nrf5 by writing to INTENSET.
- *  -> Enable RX.
- *  -> Disable interrupts globally using OS_ENTER_CRITICAL().
- *  -> Wait until a packet is received and the ADDRESS event occurs.
- *  -> Call ble_phy_disable().
- *
- *  At this point I wanted to see the state of the cortex NVIC. The IRQ
- *  pending bit was TRUE for the radio interrupt (as expected) as we never
- *  serviced the radio interrupt (interrupts were disabled).
- *
- *  What was unexpected was this: without clearing the pending IRQ in the NVIC,
- *  when radio interrupts were re-enabled (address event bit in INTENSET set to
- *  1) and the radio ADDRESS event register read 1 (it was never cleared after
- *  the first address event), the radio did not enter the ISR! I would have
- *  expected that if the following were true, an interrupt would occur:
- *      -> NVIC ISER bit set to TRUE
- *      -> NVIC ISPR bit reads TRUE, meaning interrupt is pending.
- *      -> Radio peripheral interrupts are enabled for some event (or events).
- *      -> Corresponding event register(s) in radio peripheral read 1.
- *
- *  Not sure what the end result of all this is. We will clear the pending
- *  bit in the NVIC just to be sure when we disable the PHY.
- */
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/* Per nordic, the number of bytes needed for scratch is 16 + MAX_PKT_SIZE. */
-#define NRF_ENC_SCRATCH_WORDS   (((NRF_MAX_ENCRYPTED_PYLD_LEN + 16) + 3) / 4)
-
-uint32_t g_nrf_encrypt_scratchpad[NRF_ENC_SCRATCH_WORDS];
-
-struct nrf_ccm_data
-{
-    uint8_t key[16];
-    uint64_t pkt_counter;
-    uint8_t dir_bit;
-    uint8_t iv[8];
-} __attribute__((packed));
-
-struct nrf_ccm_data g_nrf_ccm_data;
-#endif
-
-/**
- * Copies the data from the phy receive buffer into a mbuf chain.
- *
- * @param dptr Pointer to receive buffer
- * @param rxpdu Pointer to already allocated mbuf chain
- *
- * NOTE: the packet header already has the total mbuf length in it. The
- * lengths of the individual mbufs are not set prior to calling.
- *
- */
-void
-ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu)
-{
-    uint16_t rem_bytes;
-    uint16_t mb_bytes;
-    uint16_t copylen;
-    uint32_t *dst;
-    uint32_t *src;
-    struct os_mbuf *m;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct os_mbuf_pkthdr *pkthdr;
-
-    /* Better be aligned */
-    assert(((uint32_t)dptr & 3) == 0);
-
-    pkthdr = OS_MBUF_PKTHDR(rxpdu);
-    rem_bytes = pkthdr->omp_len;
-
-    /* Fill in the mbuf pkthdr first. */
-    dst = (uint32_t *)(rxpdu->om_data);
-    src = (uint32_t *)dptr;
-
-    mb_bytes = (rxpdu->om_omp->omp_databuf_len - rxpdu->om_pkthdr_len - 4);
-    copylen = min(mb_bytes, rem_bytes);
-    copylen &= 0xFFFC;
-    rem_bytes -= copylen;
-    mb_bytes -= copylen;
-    rxpdu->om_len = copylen;
-    while (copylen > 0) {
-        *dst = *src;
-        ++dst;
-        ++src;
-        copylen -= 4;
-    }
-
-    /* Copy remaining bytes */
-    m = rxpdu;
-    while (rem_bytes > 0) {
-        /* If there are enough bytes in the mbuf, copy them and leave */
-        if (rem_bytes <= mb_bytes) {
-            memcpy(m->om_data + m->om_len, src, rem_bytes);
-            m->om_len += rem_bytes;
-            break;
-        }
-
-        m = SLIST_NEXT(m, om_next);
-        assert(m != NULL);
-
-        mb_bytes = m->om_omp->omp_databuf_len;
-        copylen = min(mb_bytes, rem_bytes);
-        copylen &= 0xFFFC;
-        rem_bytes -= copylen;
-        mb_bytes -= copylen;
-        m->om_len = copylen;
-        dst = (uint32_t *)m->om_data;
-        while (copylen > 0) {
-            *dst = *src;
-            ++dst;
-            ++src;
-            copylen -= 4;
-        }
-    }
-
-    /* Copy ble header */
-    ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
-    memcpy(ble_hdr, &g_ble_phy_data.rxhdr, sizeof(struct ble_mbuf_hdr));
-}
-
-/**
- * Called when we want to wait if the radio is in either the rx or tx
- * disable states. We want to wait until that state is over before doing
- * anything to the radio
- */
-static void
-nrf_wait_disabled(void)
-{
-    uint32_t state;
-
-    /*
-     * RX and TX states have the same values except for 3rd bit (0=RX, 1=TX) so
-     * we use RX symbols only.
-     */
-    state = NRF_RADIO->STATE & 0x07;
-
-    if (state != RADIO_STATE_STATE_Disabled) {
-        /* If PHY is in idle state for whatever reason, disable it now */
-        if (state == RADIO_STATE_STATE_RxIdle) {
-            NRF_RADIO->TASKS_DISABLE = 1;
-            STATS_INC(ble_phy_stats, radio_state_errs);
-        }
-
-        if (state == RADIO_STATE_STATE_RxDisable) {
-            /* This will end within a short time (6 usecs). Just poll */
-            while (NRF_RADIO->STATE == state) {
-                /* If this fails, something is really wrong. Should last
-                 * no more than 6 usecs */
-            }
-        }
-    }
-}
-
-/**
- *
- *
- */
-int
-ble_phy_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    uint32_t next_cc;
-    uint32_t cur_cc;
-    uint32_t cntr;
-    uint32_t delta;
-
-    /*
-     * XXX: The TXEN time is 140 usecs but there may be additional delays
-     * Need to look at this.
-     */
-
-    /*
-     * With the 32.768 kHz crystal, we may need to adjust the RTC compare
-     * value by 1 tick due to the time it takes for TXEN. The code uses a 5 RTC
-     * tick offset, which is 152.5 usecs. The TXEN time is 140 usecs. This
-     * means that with a remainder of 0, TIMER0 should be set to 12 or 13 (as
-     * TIMER0 counts at 1MHz). A remainder of 19 or more we will need to add
-     * 1 tick. We dont need to add 1 tick per se, but it does give us slightly
-     * more time and thus less of a chance to miss a tick. Another note: we
-     * cant set TIMER0 CC to 0 as the compare wont occur; it must be 1 or more.
-     * This is why we subtract 18 (as opposed to 19) as rem_uses will be >= 1.
-     */
-    if (rem_usecs <= 18) {
-        cputime -= 5;
-        rem_usecs += 12;
-    } else {
-        cputime -= 4;
-        rem_usecs -= 18;
-    }
-
-    /*
-     * Can we set the RTC compare to start TIMER0? We can do it if:
-     *      a) Current compare value is not N+1 or N+2 ticks from current
-     *      counter.
-     *      b) The value we want to set is not at least N+2 from current
-     *      counter.
-     *
-     * NOTE: since the counter can tick 1 while we do these calculations we
-     * need to account for it.
-     */
-    next_cc = cputime & 0xffffff;
-    cur_cc = NRF_RTC0->CC[0];
-    cntr = NRF_RTC0->COUNTER;
-
-    delta = (cur_cc - cntr) & 0xffffff;
-    if ((delta <= 3) && (delta != 0)) {
-        return -1;
-    }
-    delta = (next_cc - cntr) & 0xffffff;
-    if ((delta & 0x800000) || (delta < 3)) {
-        return -1;
-    }
-
-    /* Clear and set TIMER0 to fire off at proper time */
-    NRF_TIMER0->TASKS_CLEAR = 1;
-    NRF_TIMER0->CC[0] = rem_usecs;
-    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
-
-    /* Set RTC compare to start TIMER0 */
-    NRF_RTC0->EVENTS_COMPARE[0] = 0;
-    NRF_RTC0->CC[0] = next_cc;
-    NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
-
-    /* Enable PPI */
-    NRF_PPI->CHENSET = PPI_CHEN_CH31_Msk;
-
-    /* Store the cputime at which we set the RTC */
-    g_ble_phy_data.phy_start_cputime = cputime;
-
-    return 0;
-}
-
-/**
- * Function is used to set PPI so that we can time out waiting for a reception
- * to occur. This happens for two reasons: we have sent a packet and we are
- * waiting for a respons (txrx should be set to ENABLE_TXRX) or we are
- * starting a connection event and we are a slave and we are waiting for the
- * master to send us a packet (txrx should be set to ENABLE_RX).
- *
- * NOTE: when waiting for a txrx turn-around, wfr_usecs is not used as there
- * is no additional time to wait; we know when we should receive the address of
- * the received frame.
- *
- * @param txrx Flag denoting if this wfr is a txrx turn-around or not.
- * @param tx_phy_mode phy mode for last TX (not used on nRF51)
- * @param wfr_usecs Amount of usecs to wait.
- */
-void
-ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs)
-{
-    uint32_t end_time;
-
-    if (txrx == BLE_PHY_WFR_ENABLE_TXRX) {
-        /*
-         * Timeout occurs an IFS time plus time it takes to receive address
-         * from the transmit end. We add additional time to make sure the
-         * address event comes before the compare. Note that transmit end
-         * is captured in CC[2]. I just made up the 16 usecs I add here.
-         */
-        end_time = NRF_TIMER0->CC[2] + BLE_LL_IFS +
-            ble_phy_mode_pdu_start_off(BLE_PHY_MODE_1M) + 16;
-    } else {
-        /* CC[0] is set to when RXEN occurs. */
-        end_time = NRF_TIMER0->CC[0] + XCVR_RX_START_DELAY_USECS + wfr_usecs +
-            ble_phy_mode_pdu_start_off(BLE_PHY_MODE_1M) + BLE_LL_JITTER_USECS;
-    }
-
-    /* wfr_secs is the time from rxen until timeout */
-    NRF_TIMER0->CC[3] = end_time;
-    NRF_TIMER0->EVENTS_COMPARE[3] = 0;
-
-    /* Enable wait for response PPI */
-    NRF_PPI->CHENSET = (PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk);
-
-    /* Enable the disabled interrupt so we time out on events compare */
-    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
-}
-
-/**
- * Setup transceiver for receive.
- */
-static void
-ble_phy_rx_xcvr_setup(void)
-{
-    uint8_t *dptr;
-
-    dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    if (g_ble_phy_data.phy_encrypted) {
-        dptr += 3;
-        NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-        NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-        NRF_CCM->OUTPTR = (uint32_t)dptr;
-        NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
-        NRF_CCM->MODE = CCM_MODE_MODE_Decryption;
-        NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-        NRF_CCM->SHORTS = 0;
-        NRF_CCM->EVENTS_ERROR = 0;
-        NRF_CCM->EVENTS_ENDCRYPT = 0;
-        NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk;
-    } else {
-        NRF_RADIO->PACKETPTR = (uint32_t)dptr;
-    }
-#else
-    NRF_RADIO->PACKETPTR = (uint32_t)dptr;
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    if (g_ble_phy_data.phy_privacy) {
-        dptr += 3;
-        NRF_RADIO->PACKETPTR = (uint32_t)dptr;
-        NRF_RADIO->PCNF0 = (6 << RADIO_PCNF0_LFLEN_Pos) |
-                           (2 << RADIO_PCNF0_S1LEN_Pos) |
-                           (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-        NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Enabled;
-        NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-        NRF_AAR->SCRATCHPTR = (uint32_t)&g_ble_phy_data.phy_aar_scratch;
-        NRF_AAR->EVENTS_END = 0;
-        NRF_AAR->EVENTS_RESOLVED = 0;
-        NRF_AAR->EVENTS_NOTRESOLVED = 0;
-    } else {
-        if (g_ble_phy_data.phy_encrypted == 0) {
-            NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
-                               (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-            /* XXX: do I only need to do this once? Figure out what I can do
-               once. */
-            NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Disabled;
-        }
-    }
-#endif
-
-    /* Turn off trigger TXEN on output compare match and AAR on bcmatch */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk | PPI_CHEN_CH23_Msk;
-
-    /* Reset the rx started flag. Used for the wait for response */
-    g_ble_phy_data.phy_rx_started = 0;
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
-    g_ble_phy_data.rxdptr = dptr;
-
-    /* I want to know when 1st byte received (after address) */
-    NRF_RADIO->BCC = 8; /* in bits */
-    NRF_RADIO->EVENTS_ADDRESS = 0;
-    NRF_RADIO->EVENTS_DEVMATCH = 0;
-    NRF_RADIO->EVENTS_BCMATCH = 0;
-    NRF_RADIO->EVENTS_RSSIEND = 0;
-    NRF_RADIO->SHORTS = RADIO_SHORTS_END_DISABLE_Msk |
-                        RADIO_SHORTS_READY_START_Msk |
-                        RADIO_SHORTS_DISABLED_TXEN_Msk |
-                        RADIO_SHORTS_ADDRESS_BCSTART_Msk |
-                        RADIO_SHORTS_ADDRESS_RSSISTART_Msk |
-                        RADIO_SHORTS_DISABLED_RSSISTOP_Msk;
-
-    NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk;
-}
-
-/**
- * Called from interrupt context when the transmit ends
- *
- */
-static void
-ble_phy_tx_end_isr(void)
-{
-    uint8_t was_encrypted;
-    uint8_t transition;
-    uint8_t txlen;
-    uint32_t wfr_time;
-
-    /* If this transmission was encrypted we need to remember it */
-    was_encrypted = g_ble_phy_data.phy_encrypted;
-
-    /* Better be in TX state! */
-    assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
-
-    /* Log the event */
-    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
-               was_encrypted, NRF_TIMER0->CC[2]);
-
-    /* Clear events and clear interrupt on disabled event */
-    NRF_RADIO->EVENTS_DISABLED = 0;
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
-    NRF_RADIO->EVENTS_END = 0;
-    wfr_time = NRF_RADIO->SHORTS;
-    (void)wfr_time;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    /*
-     * XXX: not sure what to do. We had a HW error during transmission.
-     * For now I just count a stat but continue on like all is good.
-     */
-    if (was_encrypted) {
-        if (NRF_CCM->EVENTS_ERROR) {
-            STATS_INC(ble_phy_stats, tx_hw_err);
-            NRF_CCM->EVENTS_ERROR = 0;
-        }
-    }
-#endif
-
-    /* Call transmit end callback */
-    if (g_ble_phy_data.txend_cb) {
-        g_ble_phy_data.txend_cb(g_ble_phy_data.txend_arg);
-    }
-
-    transition = g_ble_phy_data.phy_transition;
-    if (transition == BLE_PHY_TRANSITION_TX_RX) {
-        /* Packet pointer needs to be reset. */
-        ble_phy_rx_xcvr_setup();
-
-        /*
-         * Enable the wait for response timer. Note that cc #1 on
-         * timer 0 contains the transmit start time
-         */
-        txlen = g_ble_phy_data.phy_tx_pyld_len;
-        if (txlen && was_encrypted) {
-            txlen += BLE_LL_DATA_MIC_LEN;
-        }
-        ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_TXRX, 0, 0);
-    } else {
-        /*
-         * XXX: not sure we need to stop the timer here all the time. Or that
-         * it should be stopped here.
-         */
-        NRF_TIMER0->TASKS_STOP = 1;
-        NRF_TIMER0->TASKS_SHUTDOWN = 1;
-        NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk |
-                           PPI_CHEN_CH20_Msk | PPI_CHEN_CH31_Msk;
-        assert(transition == BLE_PHY_TRANSITION_NONE);
-    }
-}
-
-static void
-ble_phy_rx_end_isr(void)
-{
-    int rc;
-    uint8_t *dptr;
-    uint8_t crcok;
-    struct ble_mbuf_hdr *ble_hdr;
-
-    /* Clear events and clear interrupt */
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_END_Msk;
-
-    /* Disable automatic RXEN */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-
-    /* Set RSSI and CRC status flag in header */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    assert(NRF_RADIO->EVENTS_RSSIEND != 0);
-    ble_hdr->rxinfo.rssi = -1 * NRF_RADIO->RSSISAMPLE;
-
-    dptr = g_ble_phy_data.rxdptr;
-
-    /* Count PHY crc errors and valid packets */
-    crcok = (uint8_t)NRF_RADIO->CRCSTATUS;
-    if (!crcok) {
-        STATS_INC(ble_phy_stats, rx_crc_err);
-    } else {
-        STATS_INC(ble_phy_stats, rx_valid);
-        ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-        if (g_ble_phy_data.phy_encrypted) {
-            /* Only set MIC failure flag if frame is not zero length */
-            if ((dptr[1] != 0) && (NRF_CCM->MICSTATUS == 0)) {
-                ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_MIC_FAILURE;
-            }
-
-            /*
-             * XXX: not sure how to deal with this. This should not
-             * be a MIC failure but we should not hand it up. I guess
-             * this is just some form of rx error and that is how we
-             * handle it? For now, just set CRC error flags
-             */
-            if (NRF_CCM->EVENTS_ERROR) {
-                STATS_INC(ble_phy_stats, rx_hw_err);
-                ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK;
-            }
-
-            /*
-             * XXX: This is a total hack work-around for now but I dont
-             * know what else to do. If ENDCRYPT is not set and we are
-             * encrypted we need to not trust this frame and drop it.
-             */
-            if (NRF_CCM->EVENTS_ENDCRYPT == 0) {
-                STATS_INC(ble_phy_stats, rx_hw_err);
-                ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK;
-            }
-        }
-#endif
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    if (g_ble_phy_data.phy_encrypted || g_ble_phy_data.phy_privacy) {
-        /*
-         * XXX: This is a horrible ugly hack to deal with the RAM S1 byte.
-         * This should get fixed as we should not be handing up the header
-         * and length as part of the pdu.
-         */
-        dptr[2] = dptr[1];
-        dptr[1] = dptr[0];
-        ++dptr;
-    }
-#endif
-    rc = ble_ll_rx_end(dptr, ble_hdr);
-    if (rc < 0) {
-        ble_phy_disable();
-    }
-}
-
-static void
-ble_phy_rx_start_isr(void)
-{
-    int rc;
-    uint32_t state;
-    uint32_t usecs;
-    uint32_t ticks;
-    struct ble_mbuf_hdr *ble_hdr;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    uint8_t *dptr;
-
-    dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
-#endif
-
-    /* Clear events and clear interrupt */
-    NRF_RADIO->EVENTS_ADDRESS = 0;
-
-    /* Clear wfr timer channels and DISABLED interrupt */
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk | RADIO_INTENCLR_ADDRESS_Msk;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk;
-
-    /* Initialize flags, channel and state in ble header at rx start */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    ble_hdr->rxinfo.flags = ble_ll_state_get();
-    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
-    ble_hdr->rxinfo.handle = 0;
-    ble_hdr->rxinfo.phy = BLE_PHY_1M;
-    ble_hdr->rxinfo.phy_mode = BLE_PHY_MODE_1M;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    ble_hdr->rxinfo.user_data = NULL;
-#endif
-
-    /*
-     * Calculate receive start time.
-     *
-     * XXX: possibly use other routine with remainder!
-     */
-    usecs = NRF_TIMER0->CC[1] - ble_phy_mode_pdu_start_off(BLE_PHY_MODE_1M);
-    ticks = os_cputime_usecs_to_ticks(usecs);
-    ble_hdr->rem_usecs = usecs - os_cputime_ticks_to_usecs(ticks);
-    if (ble_hdr->rem_usecs == 31) {
-        ble_hdr->rem_usecs = 0;
-        ++ticks;
-    }
-    ble_hdr->beg_cputime = g_ble_phy_data.phy_start_cputime + ticks;
-
-    /* Wait to get 1st byte of frame */
-    while (1) {
-        state = NRF_RADIO->STATE;
-        if (NRF_RADIO->EVENTS_BCMATCH != 0) {
-            break;
-        }
-
-        /*
-         * If state is disabled, we should have the BCMATCH. If not,
-         * something is wrong!
-         */
-        if (state == RADIO_STATE_STATE_Disabled) {
-            NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-            NRF_RADIO->SHORTS = 0;
-            return;
-        }
-    }
-
-    /* Call Link Layer receive start function */
-    rc = ble_ll_rx_start(g_ble_phy_data.rxdptr, g_ble_phy_data.phy_chan,
-                         &g_ble_phy_data.rxhdr);
-    if (rc >= 0) {
-        /* Set rx started flag and enable rx end ISR */
-        g_ble_phy_data.phy_rx_started = 1;
-        NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        /* Must start aar if we need to  */
-        if (g_ble_phy_data.phy_privacy) {
-            NRF_RADIO->EVENTS_BCMATCH = 0;
-            NRF_PPI->CHENSET = PPI_CHEN_CH23_Msk;
-            /*
-             * Setup AAR to resolve AdvA and trigger it after complete address
-             * is received, i.e. after PDU header and AdvA is received.
-             *
-             * AdvA starts at 4th octet in receive buffer, after S0, len and S1
-             * fields.
-             *
-             * In case of extended advertising AdvA is located after extended
-             * header (+2 octets).
-             */
-            if (BLE_MBUF_HDR_EXT_ADV(&g_ble_phy_data.rxhdr)) {
-                NRF_AAR->ADDRPTR = (uint32_t)(dptr + 5);
-                NRF_RADIO->BCC = (BLE_DEV_ADDR_LEN + BLE_LL_PDU_HDR_LEN + 2) * 8;
-
-            } else {
-                NRF_AAR->ADDRPTR = (uint32_t)(dptr + 3);
-                NRF_RADIO->BCC = (BLE_DEV_ADDR_LEN + BLE_LL_PDU_HDR_LEN) * 8;
-            }
-        }
-#endif
-    } else {
-        /* Disable PHY */
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, rx_aborts);
-    }
-
-    /* Count rx starts */
-    STATS_INC(ble_phy_stats, rx_starts);
-}
-
-static void
-ble_phy_isr(void)
-{
-    uint32_t irq_en;
-
-    /* Read irq register to determine which interrupts are enabled */
-    irq_en = NRF_RADIO->INTENCLR;
-
-    /*
-     * NOTE: order of checking is important! Possible, if things get delayed,
-     * we have both an ADDRESS and DISABLED interrupt in rx state. If we get
-     * an address, we disable the DISABLED interrupt.
-     */
-
-    /* We get this if we have started to receive a frame */
-    if ((irq_en & RADIO_INTENCLR_ADDRESS_Msk) && NRF_RADIO->EVENTS_ADDRESS) {
-        irq_en &= ~RADIO_INTENCLR_DISABLED_Msk;
-        ble_phy_rx_start_isr();
-    }
-
-    /* Check for disabled event. This only happens for transmits now */
-    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-        if (g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) {
-            NRF_RADIO->EVENTS_DISABLED = 0;
-            ble_ll_wfr_timer_exp(NULL);
-        } else {
-            ble_phy_tx_end_isr();
-        }
-    }
-
-    /* Receive packet end (we dont enable this for transmit) */
-    if ((irq_en & RADIO_INTENCLR_END_Msk) && NRF_RADIO->EVENTS_END) {
-        ble_phy_rx_end_isr();
-    }
-
-    /* Ensures IRQ is cleared */
-    irq_en = NRF_RADIO->SHORTS;
-
-    /* Count # of interrupts */
-    STATS_INC(ble_phy_stats, phy_isrs);
-}
-
-/**
- * ble phy init
- *
- * Initialize the PHY.
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_init(void)
-{
-    int rc;
-
-#if !defined(BLE_XCVR_RFCLK)
-    uint32_t os_tmo;
-
-    /* Make sure HFXO is started */
-    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
-    NRF_CLOCK->TASKS_HFCLKSTART = 1;
-    os_tmo = os_time_get() + (5 * (1000 / OS_TICKS_PER_SEC));
-    while (1) {
-        if (NRF_CLOCK->EVENTS_HFCLKSTARTED) {
-            break;
-        }
-        if ((int32_t)(os_time_get() - os_tmo) > 0) {
-            return BLE_PHY_ERR_INIT;
-        }
-    }
-#endif
-
-    /* Set phy channel to an invalid channel so first set channel works */
-    g_ble_phy_data.phy_chan = BLE_PHY_NUM_CHANS;
-
-    /* Toggle peripheral power to reset (just in case) */
-    NRF_RADIO->POWER = 0;
-    NRF_RADIO->POWER = 1;
-
-    /* Disable all interrupts */
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-
-    /* Set configuration registers */
-    NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_1Mbit;
-    NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
-                       (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-    /* XXX: should maxlen be 251 for encryption? */
-    NRF_RADIO->PCNF1 = NRF_MAXLEN |
-                       (RADIO_PCNF1_ENDIAN_Little <<  RADIO_PCNF1_ENDIAN_Pos) |
-                       (NRF_BALEN << RADIO_PCNF1_BALEN_Pos) |
-                       RADIO_PCNF1_WHITEEN_Msk;
-
-    /* Set base0 with the advertising access address */
-    NRF_RADIO->BASE0 = (BLE_ACCESS_ADDR_ADV << 8) & 0xFFFFFF00;
-    NRF_RADIO->PREFIX0 = (BLE_ACCESS_ADDR_ADV >> 24) & 0xFF;
-
-    /* Configure the CRC registers */
-    NRF_RADIO->CRCCNF = RADIO_CRCCNF_SKIPADDR_Msk | RADIO_CRCCNF_LEN_Three;
-
-    /* Configure BLE poly */
-    NRF_RADIO->CRCPOLY = 0x0100065B;
-
-    /* Configure IFS */
-    NRF_RADIO->TIFS = BLE_LL_IFS;
-
-    /* Captures tx/rx start in timer0 cc 1 and tx/rx end in timer0 cc 2 */
-    NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk | PPI_CHEN_CH27_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    NRF_CCM->INTENCLR = 0xffffffff;
-    NRF_CCM->SHORTS = CCM_SHORTS_ENDKSGEN_CRYPT_Msk;
-    NRF_CCM->EVENTS_ERROR = 0;
-    memset(g_nrf_encrypt_scratchpad, 0, sizeof(g_nrf_encrypt_scratchpad));
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    g_ble_phy_data.phy_aar_scratch = 0;
-    NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-    NRF_AAR->INTENCLR = 0xffffffff;
-    NRF_AAR->EVENTS_END = 0;
-    NRF_AAR->EVENTS_RESOLVED = 0;
-    NRF_AAR->EVENTS_NOTRESOLVED = 0;
-    NRF_AAR->NIRK = 0;
-#endif
-
-    /* TIMER0 setup for PHY when using RTC */
-    NRF_TIMER0->TASKS_STOP = 1;
-    NRF_TIMER0->TASKS_SHUTDOWN = 1;
-    NRF_TIMER0->BITMODE = 3;    /* 32-bit timer */
-    NRF_TIMER0->MODE = 0;       /* Timer mode */
-    NRF_TIMER0->PRESCALER = 4;  /* gives us 1 MHz */
-
-    /*
-     * PPI setup.
-     * Channel 4: Captures TIMER0 in CC[3] when EVENTS_ADDRESS occurs. Used
-     *            to cancel the wait for response timer.
-     * Channel 5: TIMER0 CC[3] to TASKS_DISABLE on radio. This is the wait
-     *            for response timer.
-     */
-    NRF_PPI->CH[4].EEP = (uint32_t)&(NRF_RADIO->EVENTS_ADDRESS);
-    NRF_PPI->CH[4].TEP = (uint32_t)&(NRF_TIMER0->TASKS_CAPTURE[3]);
-    NRF_PPI->CH[5].EEP = (uint32_t)&(NRF_TIMER0->EVENTS_COMPARE[3]);
-    NRF_PPI->CH[5].TEP = (uint32_t)&(NRF_RADIO->TASKS_DISABLE);
-
-    /* Set isr in vector table and enable interrupt */
-    NVIC_SetPriority(RADIO_IRQn, 0);
-    NVIC_SetVector(RADIO_IRQn, (uint32_t)ble_phy_isr);
-    NVIC_EnableIRQ(RADIO_IRQn);
-
-    /* Register phy statistics */
-    if (!g_ble_phy_data.phy_stats_initialized) {
-        rc = stats_init_and_reg(STATS_HDR(ble_phy_stats),
-                                STATS_SIZE_INIT_PARMS(ble_phy_stats,
-                                                      STATS_SIZE_32),
-                                STATS_NAME_INIT_PARMS(ble_phy_stats),
-                                "ble_phy");
-        assert(rc == 0);
-
-        g_ble_phy_data.phy_stats_initialized  = 1;
-    }
-
-    return 0;
-}
-
-/**
- * Puts the phy into receive mode.
- *
- * @return int 0: success; BLE Phy error code otherwise
- */
-int
-ble_phy_rx(void)
-{
-    /* Check radio state */
-    nrf_wait_disabled();
-    if (NRF_RADIO->STATE != RADIO_STATE_STATE_Disabled) {
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, radio_state_errs);
-        return BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    /* Make sure all interrupts are disabled */
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-
-    /* Clear events prior to enabling receive */
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->EVENTS_DISABLED = 0;
-
-    /* Setup for rx */
-    ble_phy_rx_xcvr_setup();
-
-    /* Start the receive task in the radio if not automatically going to rx */
-    if ((NRF_PPI->CHEN & PPI_CHEN_CH21_Msk) == 0) {
-        NRF_RADIO->TASKS_RXEN = 1;
-    }
-
-    ble_ll_log(BLE_LL_LOG_ID_PHY_RX, g_ble_phy_data.phy_encrypted, 0, 0);
-
-    return 0;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
-void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
-{
-    memcpy(g_nrf_ccm_data.key, key, 16);
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    memcpy(g_nrf_ccm_data.iv, iv, 8);
-    g_nrf_ccm_data.dir_bit = is_master;
-    g_ble_phy_data.phy_encrypted = 1;
-
-    /* Encryption uses LFLEN=5, S1LEN = 3. */
-    NRF_RADIO->PCNF0 = (5 << RADIO_PCNF0_LFLEN_Pos) |
-                       (3 << RADIO_PCNF0_S1LEN_Pos) |
-                       (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-
-    /* Enable the module (AAR cannot be on while CCM on) */
-    NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Disabled;
-    NRF_CCM->ENABLE = CCM_ENABLE_ENABLE_Enabled;
-}
-
-void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
-{
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    g_nrf_ccm_data.dir_bit = dir;
-}
-
-void
-ble_phy_encrypt_disable(void)
-{
-    NRF_PPI->CHENCLR = (PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk);
-    NRF_CCM->TASKS_STOP = 1;
-    NRF_CCM->EVENTS_ERROR = 0;
-    NRF_CCM->ENABLE = CCM_ENABLE_ENABLE_Disabled;
-
-    /* Switch back to normal length */
-    NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
-                       (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-
-    g_ble_phy_data.phy_encrypted = 0;
-}
-#endif
-
-void
-ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
-{
-    /* Set transmit end callback and arg */
-    g_ble_phy_data.txend_cb = txend_cb;
-    g_ble_phy_data.txend_arg = arg;
-}
-
-/**
- * Called to set the start time of a transmission.
- *
- * This function is called to set the start time when we are not going from
- * rx to tx automatically.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime   This is the tick at which the 1st bit of the preamble
- *                  should be transmitted
- * @param rem_usecs This is used only when the underlying timing uses a 32.768
- *                  kHz crystal. It is the # of usecs from the cputime tick
- *                  at which the first bit of the preamble should be
- *                  transmitted.
- * @return int
- */
-int
-ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    int rc;
-
-    /* XXX: This should not be necessary, but paranoia is good! */
-    /* Clear timer0 compare to RXEN since we are transmitting */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-
-    /*
-     * XXX: The TXEN time is 140 usecs but there may be additional delays
-     * Need to look at this.
-     */
-    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
-        STATS_INC(ble_phy_stats, tx_late);
-        ble_phy_disable();
-        rc = BLE_PHY_ERR_TX_LATE;
-    } else {
-        /* Enable PPI to automatically start TXEN */
-        NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
-        rc = 0;
-    }
-    return rc;
-}
-/**
- * Called to set the start time of a reception
- *
- * This function acts a bit differently than transmit. If we are late getting
- * here we will still attempt to receive.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime
- *
- * @return int
- */
-int
-ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    int rc;
-
-    /* XXX: This should not be necessary, but paranoia is good! */
-    /* Clear timer0 compare to TXEN since we are transmitting */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
-
-    /*
-     * XXX: The RXEN time is 138 usecs but there may be additional delays
-     * Need to look at this.
-     */
-    if (ble_phy_set_start_time(cputime, rem_usecs) != 0) {
-        STATS_INC(ble_phy_stats, rx_late);
-        NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-        NRF_RADIO->TASKS_RXEN = 1;
-        rc = BLE_PHY_ERR_RX_LATE;
-    } else {
-        /* Enable PPI to automatically start RXEN */
-        NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
-
-        /* Start rx */
-        rc = ble_phy_rx();
-    }
-    return rc;
-}
-
-int
-ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
-{
-    int rc;
-    uint8_t *dptr;
-    uint8_t payload_len;
-    uint8_t payload_off;
-    uint8_t hdr_byte;
-    uint32_t state;
-    uint32_t shortcuts;
-
-    /*
-     * This check is to make sure that the radio is not in a state where
-     * it is moving to disabled state. If so, let it get there.
-     */
-    nrf_wait_disabled();
-
-    /*
-     * XXX: Although we may not have to do this here, I clear all the PPI
-     * that should not be used when transmitting. Some of them are only enabled
-     * if encryption and/or privacy is on, but I dont care. Better to be
-     * paranoid, and if you are going to clear one, might as well clear them
-     * all.
-     */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH23_Msk |
-                       PPI_CHEN_CH25_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    if (g_ble_phy_data.phy_encrypted) {
-        /* RAM representation has S0, LENGTH and S1 fields. (3 bytes) */
-        dptr = (uint8_t *)&g_ble_phy_enc_buf[0];
-        payload_off = 3;
-
-        NRF_CCM->SHORTS = 1;
-        NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-        NRF_CCM->OUTPTR = (uint32_t)&g_ble_phy_tx_buf[0];
-        NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
-        NRF_CCM->EVENTS_ERROR = 0;
-        NRF_CCM->MODE = CCM_MODE_MODE_Encryption;
-        NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-        NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk;
-    } else {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        /* Reconfigure PCNF0 */
-        NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
-                           (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-        NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-#endif
-        /* RAM representation has S0 and LENGTH fields (2 bytes) */
-        dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
-        payload_off = 2;
-    }
-#else
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    /* Reconfigure PCNF0 */
-    NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
-                       (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
-#endif
-
-    /* RAM representation has S0 and LENGTH fields (2 bytes) */
-    dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
-    payload_off = 2;
-#endif
-
-    NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_tx_buf[0];
-
-    /* Clear the ready, end and disabled events */
-    NRF_RADIO->EVENTS_READY = 0;
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->EVENTS_DISABLED = 0;
-
-    /* Enable shortcuts for transmit start/end. */
-    shortcuts = RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_READY_START_Msk;
-    if (end_trans == BLE_PHY_TRANSITION_TX_RX) {
-        shortcuts |= RADIO_SHORTS_DISABLED_RXEN_Msk;
-    }
-    NRF_RADIO->SHORTS = shortcuts;
-    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
-
-    /* Set PDU payload */
-    payload_len = pducb(&dptr[payload_off], pducb_arg, &hdr_byte);
-
-    /* Set PDU header */
-    dptr[0] = hdr_byte;
-    dptr[1] = payload_len;
-    if (payload_off > 2) {
-        dptr[2] = 0;
-    }
-
-    /* Set the PHY transition */
-    g_ble_phy_data.phy_transition = end_trans;
-
-    /* Set transmitted payload length */
-    g_ble_phy_data.phy_tx_pyld_len = payload_len;
-
-    /* If we already started transmitting, abort it! */
-    state = NRF_RADIO->STATE;
-    if (state != RADIO_STATE_STATE_Tx) {
-
-        /* Set phy state to transmitting and count packet statistics */
-        g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
-        STATS_INC(ble_phy_stats, tx_good);
-        STATS_INCN(ble_phy_stats, tx_bytes, payload_len + BLE_LL_PDU_HDR_LEN);
-        rc = BLE_ERR_SUCCESS;
-    } else {
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, tx_late);
-        rc = BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    return rc;
-}
-
-/**
- * ble phy txpwr set
- *
- * Set the transmit output power (in dBm).
- *
- * NOTE: If the output power specified is within the BLE limits but outside
- * the chip limits, we "rail" the power level so we dont exceed the min/max
- * chip values.
- *
- * @param dbm Power output in dBm.
- *
- * @return int 0: success; anything else is an error
- */
-int
-ble_phy_txpwr_set(int dbm)
-{
-    /* Check valid range */
-    assert(dbm <= BLE_PHY_MAX_PWR_DBM);
-
-    /* "Rail" power level if outside supported range */
-    if (dbm > NRF_TX_PWR_MAX_DBM) {
-        dbm = NRF_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < NRF_TX_PWR_MIN_DBM) {
-            dbm = NRF_TX_PWR_MIN_DBM;
-        }
-    }
-
-    NRF_RADIO->TXPOWER = dbm;
-    g_ble_phy_data.phy_txpwr_dbm = dbm;
-
-    return 0;
-}
-
-/**
- * ble phy txpwr round
- *
- * Get the rounded transmit output power (in dBm).
- *
- * @param dbm Power output in dBm.
- *
- * @return int Rounded power in dBm
- */
-int ble_phy_txpower_round(int dbm)
-{
-    /* "Rail" power level if outside supported range */
-    if (dbm > NRF_TX_PWR_MAX_DBM) {
-        dbm = NRF_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < NRF_TX_PWR_MIN_DBM) {
-            dbm = NRF_TX_PWR_MIN_DBM;
-        }
-    }
-
-    return dbm;
-}
-
-/**
- * ble phy txpwr get
- *
- * Get the transmit power.
- *
- * @return int  The current PHY transmit power, in dBm
- */
-int
-ble_phy_txpwr_get(void)
-{
-    return g_ble_phy_data.phy_txpwr_dbm;
-}
-
-/**
- * ble phy setchan
- *
- * Sets the logical frequency of the transceiver. The input parameter is the
- * BLE channel index (0 to 39, inclusive). The NRF frequency register works like
- * this: logical frequency = 2400 + FREQ (MHz).
- *
- * Thus, to get a logical frequency of 2402 MHz, you would program the
- * FREQUENCY register to 2.
- *
- * @param chan This is the Data Channel Index or Advertising Channel index
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
-{
-    uint32_t prefix;
-
-    assert(chan < BLE_PHY_NUM_CHANS);
-
-    /* Check for valid channel range */
-    if (chan >= BLE_PHY_NUM_CHANS) {
-        return BLE_PHY_ERR_INV_PARAM;
-    }
-
-    /* Get correct frequency */
-    if (chan < BLE_PHY_NUM_DATA_CHANS) {
-        /* Set current access address */
-        g_ble_phy_data.phy_access_address = access_addr;
-
-        /* Configure logical address 1 and crcinit */
-        prefix = NRF_RADIO->PREFIX0;
-        prefix &= 0xffff00ff;
-        prefix |= ((access_addr >> 24) & 0xFF) << 8;
-        NRF_RADIO->BASE1 = (access_addr << 8) & 0xFFFFFF00;
-        NRF_RADIO->PREFIX0 = prefix;
-        NRF_RADIO->TXADDRESS = 1;
-        NRF_RADIO->RXADDRESSES = (1 << 1);
-        NRF_RADIO->CRCINIT = crcinit;
-    } else {
-        /* Logical adddress 0 preconfigured */
-        NRF_RADIO->TXADDRESS = 0;
-        NRF_RADIO->RXADDRESSES = (1 << 0);
-        NRF_RADIO->CRCINIT = BLE_LL_CRCINIT_ADV;
-
-        /* Set current access address */
-        g_ble_phy_data.phy_access_address = BLE_ACCESS_ADDR_ADV;
-    }
-
-    /* Set the frequency and the data whitening initial value */
-    g_ble_phy_data.phy_chan = chan;
-    NRF_RADIO->FREQUENCY = g_ble_phy_chan_freq[chan];
-    NRF_RADIO->DATAWHITEIV = chan;
-
-    ble_ll_log(BLE_LL_LOG_ID_PHY_SETCHAN, chan, freq, access_addr);
-
-    return 0;
-}
-
-/**
- * Stop the timer used to count microseconds when using RTC for cputime
- */
-void
-ble_phy_stop_usec_timer(void)
-{
-    NRF_TIMER0->TASKS_STOP = 1;
-    NRF_TIMER0->TASKS_SHUTDOWN = 1;
-    NRF_RTC0->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
-}
-
-/**
- * ble phy disable irq and ppi
- *
- * This routine is to be called when reception was stopped due to either a
- * wait for response timeout or a packet being received and the phy is to be
- * restarted in receive mode. Generally, the disable routine is called to stop
- * the phy.
- */
-void
-ble_phy_disable_irq_and_ppi(void)
-{
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-    NRF_RADIO->SHORTS = 0;
-    NRF_RADIO->TASKS_DISABLE = 1;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH20_Msk |
-          PPI_CHEN_CH21_Msk | PPI_CHEN_CH23_Msk | PPI_CHEN_CH24_Msk |
-          PPI_CHEN_CH25_Msk | PPI_CHEN_CH31_Msk;
-    NVIC_ClearPendingIRQ(RADIO_IRQn);
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
-}
-
-void
-ble_phy_restart_rx(void)
-{
-    ble_phy_disable_irq_and_ppi();
-    ble_phy_rx();
-}
-
-/**
- * ble phy disable
- *
- * Disables the PHY. This should be called when an event is over. It stops
- * the usec timer (if used), disables interrupts, disables the RADIO, disables
- * PPI and sets state to idle.
- */
-void
-ble_phy_disable(void)
-{
-    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
-    ble_phy_stop_usec_timer();
-    ble_phy_disable_irq_and_ppi();
-}
-
-/* Gets the current access address */
-uint32_t ble_phy_access_addr_get(void)
-{
-    return g_ble_phy_data.phy_access_address;
-}
-
-/**
- * Return the phy state
- *
- * @return int The current PHY state.
- */
-int
-ble_phy_state_get(void)
-{
-    return g_ble_phy_data.phy_state;
-}
-
-/**
- * Called to see if a reception has started
- *
- * @return int
- */
-int
-ble_phy_rx_started(void)
-{
-    return g_ble_phy_data.phy_rx_started;
-}
-
-/**
- * Return the transceiver state
- *
- * @return int transceiver state.
- */
-uint8_t
-ble_phy_xcvr_state_get(void)
-{
-    uint32_t state;
-    state = NRF_RADIO->STATE;
-    return (uint8_t)state;
-}
-
-/**
- * Called to return the maximum data pdu payload length supported by the
- * phy. For this chip, if encryption is enabled, the maximum payload is 27
- * bytes.
- *
- * @return uint8_t Maximum data channel PDU payload size supported
- */
-uint8_t
-ble_phy_max_data_pdu_pyld(void)
-{
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    return NRF_MAX_ENCRYPTED_PYLD_LEN;
-#else
-    return BLE_LL_DATA_PDU_MAX_PYLD;
-#endif
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-void
-ble_phy_resolv_list_enable(void)
-{
-    NRF_AAR->NIRK = (uint32_t)g_nrf_num_irks;
-    g_ble_phy_data.phy_privacy = 1;
-}
-
-void
-ble_phy_resolv_list_disable(void)
-{
-    g_ble_phy_data.phy_privacy = 0;
-}
-#endif
-
-#ifdef BLE_XCVR_RFCLK
-void
-ble_phy_rfclk_enable(void)
-{
-    NRF_CLOCK->TASKS_HFCLKSTART = 1;
-}
-
-void
-ble_phy_rfclk_disable(void)
-{
-    NRF_CLOCK->TASKS_HFCLKSTOP = 1;
-}
-#endif
diff --git a/hw/drivers/nimble/nrf52/include/ble/xcvr.h b/hw/drivers/nimble/nrf52/include/ble/xcvr.h
deleted file mode 100644
index 757bb80fa..000000000
--- a/hw/drivers/nimble/nrf52/include/ble/xcvr.h
+++ /dev/null
@@ -1,52 +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_XCVR_
-#define H_BLE_XCVR_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#define XCVR_RX_RADIO_RAMPUP_USECS  (40)
-#define XCVR_TX_RADIO_RAMPUP_USECS  (40)
-
-/*
- * NOTE: we have to account for the RTC output compare issue. We want it to be
- * 5 ticks.
- */
-#define XCVR_PROC_DELAY_USECS         (153)
-#define XCVR_RX_START_DELAY_USECS     (XCVR_RX_RADIO_RAMPUP_USECS)
-#define XCVR_TX_START_DELAY_USECS     (XCVR_TX_RADIO_RAMPUP_USECS)
-#define XCVR_TX_SCHED_DELAY_USECS     \
-    (XCVR_TX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-#define XCVR_RX_SCHED_DELAY_USECS     \
-    (XCVR_RX_START_DELAY_USECS + XCVR_PROC_DELAY_USECS)
-
-/*
- * Define HW whitelist size. This is the total possible whitelist size;
- * not necessarily the size that will be used (may be smaller)
- */
-#define BLE_HW_WHITE_LIST_SIZE        (8)
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_XCVR_ */
diff --git a/hw/drivers/nimble/nrf52/pkg.yml b/hw/drivers/nimble/nrf52/pkg.yml
index f6826672d..56cf452e9 100644
--- a/hw/drivers/nimble/nrf52/pkg.yml
+++ b/hw/drivers/nimble/nrf52/pkg.yml
@@ -18,14 +18,7 @@
 #
 
 pkg.name: hw/drivers/nimble/nrf52
-pkg.description: BLE driver for nRF52 systems.
 pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-    - ble
-    - bluetooth
-
-pkg.apis: ble_driver
 pkg.deps:
-    - net/nimble
-    - net/nimble/controller
+    - "@apache-mynewt-nimble/nimble/drivers/nrf52"
diff --git a/hw/drivers/nimble/nrf52/src/ble_hw.c b/hw/drivers/nimble/nrf52/src/ble_hw.c
deleted file mode 100644
index af9c1804f..000000000
--- a/hw/drivers/nimble/nrf52/src/ble_hw.c
+++ /dev/null
@@ -1,486 +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 "syscfg/syscfg.h"
-#include "os/os.h"
-#include "ble/xcvr.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "nrfx.h"
-#include "controller/ble_hw.h"
-#include "mcu/cmsis_nvic.h"
-#include "os/os_trace_api.h"
-
-/* Total number of resolving list elements */
-#define BLE_HW_RESOLV_LIST_SIZE     (16)
-
-/* We use this to keep track of which entries are set to valid addresses */
-static uint8_t g_ble_hw_whitelist_mask;
-
-/* Random number generator isr callback */
-ble_rng_isr_cb_t g_ble_rng_isr_cb;
-
-/* If LL privacy is enabled, allocate memory for AAR */
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-
-/* The NRF51 supports up to 16 IRK entries */
-#if (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE) < 16)
-#define NRF_IRK_LIST_ENTRIES    (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE))
-#else
-#define NRF_IRK_LIST_ENTRIES    (16)
-#endif
-
-/* NOTE: each entry is 16 bytes long. */
-uint32_t g_nrf_irk_list[NRF_IRK_LIST_ENTRIES * 4];
-
-/* Current number of IRK entries */
-uint8_t g_nrf_num_irks;
-
-#endif
-
-/* Returns public device address or -1 if not present */
-int
-ble_hw_get_public_addr(ble_addr_t *addr)
-{
-    int rc;
-    uint32_t addr_high;
-    uint32_t addr_low;
-
-    /* Does FICR have a public address */
-    rc = -1;
-    if ((NRF_FICR->DEVICEADDRTYPE & 1) == 0) {
-        addr_low = NRF_FICR->DEVICEADDR[0];
-        addr_high = NRF_FICR->DEVICEADDR[1];
-        rc = 0;
-    } else {
-        /* See if programmed in UICR. Upper 16 bits must all be zero */
-        addr_high = NRF_UICR->CUSTOMER[1];
-        if (addr_high < 65536) {
-            addr_low = NRF_UICR->CUSTOMER[0];
-            rc = 0;
-        }
-    }
-
-    if (!rc) {
-        /* Copy into device address. We can do this because we know platform */
-        memcpy(addr->val, &addr_low, 4);
-        memcpy(&addr->val[4], &addr_high, 2);
-        addr->type = BLE_ADDR_PUBLIC;
-    }
-
-    return rc;
-}
-
-/* Returns random static address or -1 if not present */
-int
-ble_hw_get_static_addr(ble_addr_t *addr)
-{
-    int rc;
-
-    if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) {
-        memcpy(addr->val, (void *)&NRF_FICR->DEVICEADDR[0], 4);
-        memcpy(&addr->val[4], (void *)&NRF_FICR->DEVICEADDR[1], 2);
-        addr->val[5] |= 0xc0;
-        addr->type = BLE_ADDR_RANDOM;
-        rc = 0;
-    } else {
-        rc = -1;
-    }
-
-    return rc;
-}
-
-/**
- * Clear the whitelist
- *
- * @return int
- */
-void
-ble_hw_whitelist_clear(void)
-{
-    NRF_RADIO->DACNF = 0;
-    g_ble_hw_whitelist_mask = 0;
-}
-
-/**
- * Add a device to the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_whitelist_add(uint8_t *addr, uint8_t addr_type)
-{
-    int i;
-    uint32_t mask;
-
-    /* Find first ununsed device address match element */
-    mask = 0x01;
-    for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) {
-        if ((mask & g_ble_hw_whitelist_mask) == 0) {
-            NRF_RADIO->DAB[i] = get_le32(addr);
-            NRF_RADIO->DAP[i] = get_le16(addr + 4);
-            if (addr_type == BLE_ADDR_RANDOM) {
-                NRF_RADIO->DACNF |= (mask << 8);
-            }
-            g_ble_hw_whitelist_mask |= mask;
-            return BLE_ERR_SUCCESS;
-        }
-        mask <<= 1;
-    }
-
-    return BLE_ERR_MEM_CAPACITY;
-}
-
-/**
- * Remove a device from the hw whitelist
- *
- * @param addr
- * @param addr_type
- *
- */
-void
-ble_hw_whitelist_rmv(uint8_t *addr, uint8_t addr_type)
-{
-    int i;
-    uint8_t cfg_addr;
-    uint16_t dap;
-    uint16_t txadd;
-    uint32_t dab;
-    uint32_t mask;
-
-    /* Find first ununsed device address match element */
-    dab = get_le32(addr);
-    dap = get_le16(addr + 4);
-    txadd = NRF_RADIO->DACNF >> 8;
-    mask = 0x01;
-    for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) {
-        if (mask & g_ble_hw_whitelist_mask) {
-            if ((dab == NRF_RADIO->DAB[i]) && (dap == NRF_RADIO->DAP[i])) {
-                cfg_addr = txadd & mask;
-                if (addr_type == BLE_ADDR_RANDOM) {
-                    if (cfg_addr != 0) {
-                        break;
-                    }
-                } else {
-                    if (cfg_addr == 0) {
-                        break;
-                    }
-                }
-            }
-        }
-        mask <<= 1;
-    }
-
-    if (i < BLE_HW_WHITE_LIST_SIZE) {
-        g_ble_hw_whitelist_mask &= ~mask;
-        NRF_RADIO->DACNF &= ~mask;
-    }
-}
-
-/**
- * Returns the size of the whitelist in HW
- *
- * @return int Number of devices allowed in whitelist
- */
-uint8_t
-ble_hw_whitelist_size(void)
-{
-    return BLE_HW_WHITE_LIST_SIZE;
-}
-
-/**
- * Enable the whitelisted devices
- */
-void
-ble_hw_whitelist_enable(void)
-{
-    /* Enable the configured device addresses */
-    NRF_RADIO->DACNF |= g_ble_hw_whitelist_mask;
-}
-
-/**
- * Disables the whitelisted devices
- */
-void
-ble_hw_whitelist_disable(void)
-{
-    /* Disable all whitelist devices */
-    NRF_RADIO->DACNF &= 0x0000ff00;
-}
-
-/**
- * Boolean function which returns true ('1') if there is a match on the
- * whitelist.
- *
- * @return int
- */
-int
-ble_hw_whitelist_match(void)
-{
-    return (int)NRF_RADIO->EVENTS_DEVMATCH;
-}
-
-/* Encrypt data */
-int
-ble_hw_encrypt_block(struct ble_encryption_block *ecb)
-{
-    int rc;
-    uint32_t end;
-    uint32_t err;
-
-    /* Stop ECB */
-    NRF_ECB->TASKS_STOPECB = 1;
-    /* XXX: does task stop clear these counters? Anyway to do this quicker? */
-    NRF_ECB->EVENTS_ENDECB = 0;
-    NRF_ECB->EVENTS_ERRORECB = 0;
-    NRF_ECB->ECBDATAPTR = (uint32_t)ecb;
-
-    /* Start ECB */
-    NRF_ECB->TASKS_STARTECB = 1;
-
-    /* Wait till error or done */
-    rc = 0;
-    while (1) {
-        end = NRF_ECB->EVENTS_ENDECB;
-        err = NRF_ECB->EVENTS_ERRORECB;
-        if (end || err) {
-            if (err) {
-                rc = -1;
-            }
-            break;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Random number generator ISR.
- */
-static void
-ble_rng_isr(void)
-{
-    uint8_t rnum;
-
-    os_trace_enter_isr();
-
-    /* No callback? Clear and disable interrupts */
-    if (g_ble_rng_isr_cb == NULL) {
-        NRF_RNG->INTENCLR = 1;
-        NRF_RNG->EVENTS_VALRDY = 0;
-        (void)NRF_RNG->SHORTS;
-        return;
-    }
-
-    /* If there is a value ready grab it */
-    if (NRF_RNG->EVENTS_VALRDY) {
-        NRF_RNG->EVENTS_VALRDY = 0;
-        rnum = (uint8_t)NRF_RNG->VALUE;
-        (*g_ble_rng_isr_cb)(rnum);
-    }
-    os_trace_exit_isr();
-}
-
-/**
- * Initialize the random number generator
- *
- * @param cb
- * @param bias
- *
- * @return int
- */
-int
-ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias)
-{
-    /* Set bias */
-    if (bias) {
-        NRF_RNG->CONFIG = 1;
-    } else {
-        NRF_RNG->CONFIG = 0;
-    }
-
-    /* If we were passed a function pointer we need to enable the interrupt */
-    if (cb != NULL) {
-        NVIC_SetPriority(RNG_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
-        NVIC_SetVector(RNG_IRQn, (uint32_t)ble_rng_isr);
-        NVIC_EnableIRQ(RNG_IRQn);
-        g_ble_rng_isr_cb = cb;
-    }
-
-    return 0;
-}
-
-/**
- * Start the random number generator
- *
- * @return int
- */
-int
-ble_hw_rng_start(void)
-{
-    os_sr_t sr;
-
-    /* No need for interrupt if there is no callback */
-    OS_ENTER_CRITICAL(sr);
-    NRF_RNG->EVENTS_VALRDY = 0;
-    if (g_ble_rng_isr_cb) {
-        NRF_RNG->INTENSET = 1;
-    }
-    NRF_RNG->TASKS_START = 1;
-    OS_EXIT_CRITICAL(sr);
-
-    return 0;
-}
-
-/**
- * Stop the random generator
- *
- * @return int
- */
-int
-ble_hw_rng_stop(void)
-{
-    os_sr_t sr;
-
-    /* No need for interrupt if there is no callback */
-    OS_ENTER_CRITICAL(sr);
-    NRF_RNG->INTENCLR = 1;
-    NRF_RNG->TASKS_STOP = 1;
-    NRF_RNG->EVENTS_VALRDY = 0;
-    OS_EXIT_CRITICAL(sr);
-
-    return 0;
-}
-
-/**
- * Read the random number generator.
- *
- * @return uint8_t
- */
-uint8_t
-ble_hw_rng_read(void)
-{
-    uint8_t rnum;
-
-    /* Wait for a sample */
-    while (NRF_RNG->EVENTS_VALRDY == 0) {
-    }
-
-    NRF_RNG->EVENTS_VALRDY = 0;
-    rnum = (uint8_t)NRF_RNG->VALUE;
-
-    return rnum;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY))
-/**
- * Clear the resolving list
- *
- * @return int
- */
-void
-ble_hw_resolv_list_clear(void)
-{
-    g_nrf_num_irks = 0;
-}
-
-/**
- * Add a device to the hw resolving list
- *
- * @param irk   Pointer to IRK to add
- *
- * @return int 0: success, BLE error code otherwise
- */
-int
-ble_hw_resolv_list_add(uint8_t *irk)
-{
-    uint32_t *nrf_entry;
-
-    /* Find first ununsed device address match element */
-    if (g_nrf_num_irks == NRF_IRK_LIST_ENTRIES) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-    /* Copy into irk list */
-    nrf_entry = &g_nrf_irk_list[4 * g_nrf_num_irks];
-    memcpy(nrf_entry, irk, 16);
-
-    /* Add to total */
-    ++g_nrf_num_irks;
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Remove a device from the hw resolving list
- *
- * @param index Index of IRK to remove
- */
-void
-ble_hw_resolv_list_rmv(int index)
-{
-    uint32_t *irk_entry;
-
-    if (index < g_nrf_num_irks) {
-        --g_nrf_num_irks;
-        irk_entry = &g_nrf_irk_list[index];
-        if (g_nrf_num_irks > index) {
-            memmove(irk_entry, irk_entry + 4, g_nrf_num_irks - index);
-        }
-    }
-}
-
-/**
- * Returns the size of the resolving list. NOTE: this returns the maximum
- * allowable entries in the HW. Configuration options may limit this.
- *
- * @return int Number of devices allowed in resolving list
- */
-uint8_t
-ble_hw_resolv_list_size(void)
-{
-    return BLE_HW_RESOLV_LIST_SIZE;
-}
-
-/**
- * Called to determine if the address received was resolved.
- *
- * @return int  Negative values indicate unresolved address; positive values
- *              indicate index in resolving list of resolved address.
- */
-int
-ble_hw_resolv_list_match(void)
-{
-    uint32_t index;
-
-    if (NRF_AAR->EVENTS_END) {
-        if (NRF_AAR->EVENTS_RESOLVED) {
-            index = NRF_AAR->STATUS;
-            return (int)index;
-        }
-    }
-
-    return -1;
-}
-#endif
diff --git a/hw/drivers/nimble/nrf52/src/ble_phy.c b/hw/drivers/nimble/nrf52/src/ble_phy.c
deleted file mode 100644
index 7f478b6b8..000000000
--- a/hw/drivers/nimble/nrf52/src/ble_phy.c
+++ /dev/null
@@ -1,1867 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "ble/xcvr.h"
-#include "mcu/cmsis_nvic.h"
-#include "hal/hal_gpio.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "controller/ble_phy.h"
-#include "controller/ble_ll.h"
-#include "nrf.h"
-
-/*
- * NOTE: This code uses a couple of PPI channels so care should be taken when
- *       using PPI somewhere else.
- *
- * Pre-programmed channels: CH20, CH21, CH23, CH25, CH31
- * Regular channels: CH4, CH5 and optionally CH17, CH18, CH19
- *  - CH4 = cancel wfr timer on address match
- *  - CH5 = disable radio on wfr timer expiry
- *  - CH17 = (optional) gpio debug for radio ramp-up
- *  - CH18 = (optional) gpio debug for wfr timer RX enabled
- *  - CH19 = (optional) gpio debug for wfr timer radio disabled
- *
- */
-
-/* XXX: 4) Make sure RF is higher priority interrupt than schedule */
-
-/*
- * XXX: Maximum possible transmit time is 1 msec for a 60ppm crystal
- * and 16ms for a 30ppm crystal! We need to limit PDU size based on
- * crystal accuracy. Look at this in the spec.
- */
-
-/* XXX: private header file? */
-extern uint8_t g_nrf_num_irks;
-extern uint32_t g_nrf_irk_list[];
-
-/* To disable all radio interrupts */
-#define NRF_RADIO_IRQ_MASK_ALL  (0x34FF)
-
-/*
- * We configure the nrf with a 1 byte S0 field, 8 bit length field, and
- * zero bit S1 field. The preamble is 8 bits long.
- */
-#define NRF_LFLEN_BITS          (8)
-#define NRF_S0LEN               (1)
-#define NRF_S1LEN_BITS          (0)
-#define NRF_CILEN_BITS          (2)
-#define NRF_TERMLEN_BITS        (3)
-
-/* Maximum length of frames */
-#define NRF_MAXLEN              (255)
-#define NRF_BALEN               (3)     /* For base address of 3 bytes */
-
-/* Maximum tx power */
-#define NRF_TX_PWR_MAX_DBM      (4)
-#define NRF_TX_PWR_MIN_DBM      (-40)
-
-/* NRF_RADIO->PCNF0 configuration values */
-#define NRF_PCNF0               (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) | \
-                                (RADIO_PCNF0_S1INCL_Msk) | \
-                                (NRF_S0LEN << RADIO_PCNF0_S0LEN_Pos) | \
-                                (NRF_S1LEN_BITS << RADIO_PCNF0_S1LEN_Pos)
-#define NRF_PCNF0_1M            (NRF_PCNF0) | \
-                                (RADIO_PCNF0_PLEN_8bit << RADIO_PCNF0_PLEN_Pos)
-#define NRF_PCNF0_2M            (NRF_PCNF0) | \
-                                (RADIO_PCNF0_PLEN_16bit << RADIO_PCNF0_PLEN_Pos)
-#define NRF_PCNF0_CODED         (NRF_PCNF0) | \
-                                (RADIO_PCNF0_PLEN_LongRange << RADIO_PCNF0_PLEN_Pos) | \
-                                (NRF_CILEN_BITS << RADIO_PCNF0_CILEN_Pos) | \
-                                (NRF_TERMLEN_BITS << RADIO_PCNF0_TERMLEN_Pos)
-
-/* BLE PHY data structure */
-struct ble_phy_obj
-{
-    uint8_t phy_stats_initialized;
-    int8_t  phy_txpwr_dbm;
-    uint8_t phy_chan;
-    uint8_t phy_state;
-    uint8_t phy_transition;
-    uint8_t phy_transition_late;
-    uint8_t phy_rx_started;
-    uint8_t phy_encrypted;
-    uint8_t phy_privacy;
-    uint8_t phy_tx_pyld_len;
-    uint8_t phy_txtorx_phy_mode;
-    uint8_t phy_cur_phy_mode;
-    uint8_t phy_bcc_offset;
-    uint32_t phy_aar_scratch;
-    uint32_t phy_access_address;
-    struct ble_mbuf_hdr rxhdr;
-    void *txend_arg;
-    ble_phy_tx_end_func txend_cb;
-    uint32_t phy_start_cputime;
-};
-struct ble_phy_obj g_ble_phy_data;
-
-/* XXX: if 27 byte packets desired we can make this smaller */
-/* Global transmit/receive buffer */
-static uint32_t g_ble_phy_tx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
-static uint32_t g_ble_phy_rx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/* Make sure word-aligned for faster copies */
-static uint32_t g_ble_phy_enc_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
-#endif
-
-/* RF center frequency for each channel index (offset from 2400 MHz) */
-static const uint8_t g_ble_phy_chan_freq[BLE_PHY_NUM_CHANS] = {
-     4,  6,  8, 10, 12, 14, 16, 18, 20, 22, /* 0-9 */
-    24, 28, 30, 32, 34, 36, 38, 40, 42, 44, /* 10-19 */
-    46, 48, 50, 52, 54, 56, 58, 60, 62, 64, /* 20-29 */
-    66, 68, 70, 72, 74, 76, 78,  2, 26, 80, /* 30-39 */
-};
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-/* packet start offsets (in usecs) */
-static const uint16_t g_ble_phy_mode_pkt_start_off[BLE_PHY_NUM_MODE] = { 376, 40, 24, 376 };
-#endif
-
-/* Various radio timings */
-/* Radio ramp-up times in usecs (fast mode) */
-#define BLE_PHY_T_TXENFAST      (XCVR_TX_RADIO_RAMPUP_USECS)
-#define BLE_PHY_T_RXENFAST      (XCVR_RX_RADIO_RAMPUP_USECS)
-/* delay between EVENTS_READY and start of tx */
-static const uint8_t g_ble_phy_t_txdelay[BLE_PHY_NUM_MODE] = { 5, 3, 3, 5 };
-/* delay between EVENTS_END and end of txd packet */
-static const uint8_t g_ble_phy_t_txenddelay[BLE_PHY_NUM_MODE] = { 9, 3, 3, 3 };
-/* delay between rxd access address (w/ TERM1 for coded) and EVENTS_ADDRESS */
-static const uint8_t g_ble_phy_t_rxaddrdelay[BLE_PHY_NUM_MODE] = { 17, 7, 3, 17 };
-/* delay between end of rxd packet and EVENTS_END */
-static const uint8_t g_ble_phy_t_rxenddelay[BLE_PHY_NUM_MODE] = { 27, 7, 3, 22 };
-
-/* Statistics */
-STATS_SECT_START(ble_phy_stats)
-    STATS_SECT_ENTRY(phy_isrs)
-    STATS_SECT_ENTRY(tx_good)
-    STATS_SECT_ENTRY(tx_fail)
-    STATS_SECT_ENTRY(tx_late)
-    STATS_SECT_ENTRY(tx_bytes)
-    STATS_SECT_ENTRY(rx_starts)
-    STATS_SECT_ENTRY(rx_aborts)
-    STATS_SECT_ENTRY(rx_valid)
-    STATS_SECT_ENTRY(rx_crc_err)
-    STATS_SECT_ENTRY(rx_late)
-    STATS_SECT_ENTRY(radio_state_errs)
-    STATS_SECT_ENTRY(rx_hw_err)
-    STATS_SECT_ENTRY(tx_hw_err)
-STATS_SECT_END
-STATS_SECT_DECL(ble_phy_stats) ble_phy_stats;
-
-STATS_NAME_START(ble_phy_stats)
-    STATS_NAME(ble_phy_stats, phy_isrs)
-    STATS_NAME(ble_phy_stats, tx_good)
-    STATS_NAME(ble_phy_stats, tx_fail)
-    STATS_NAME(ble_phy_stats, tx_late)
-    STATS_NAME(ble_phy_stats, tx_bytes)
-    STATS_NAME(ble_phy_stats, rx_starts)
-    STATS_NAME(ble_phy_stats, rx_aborts)
-    STATS_NAME(ble_phy_stats, rx_valid)
-    STATS_NAME(ble_phy_stats, rx_crc_err)
-    STATS_NAME(ble_phy_stats, rx_late)
-    STATS_NAME(ble_phy_stats, radio_state_errs)
-    STATS_NAME(ble_phy_stats, rx_hw_err)
-    STATS_NAME(ble_phy_stats, tx_hw_err)
-STATS_NAME_END(ble_phy_stats)
-
-/*
- * NOTE:
- * Tested the following to see what would happen:
- *  -> NVIC has radio irq enabled (interrupt # 1, mask 0x2).
- *  -> Set up nrf to receive. Clear ADDRESS event register.
- *  -> Enable ADDRESS interrupt on nrf5 by writing to INTENSET.
- *  -> Enable RX.
- *  -> Disable interrupts globally using OS_ENTER_CRITICAL().
- *  -> Wait until a packet is received and the ADDRESS event occurs.
- *  -> Call ble_phy_disable().
- *
- *  At this point I wanted to see the state of the cortex NVIC. The IRQ
- *  pending bit was TRUE for the radio interrupt (as expected) as we never
- *  serviced the radio interrupt (interrupts were disabled).
- *
- *  What was unexpected was this: without clearing the pending IRQ in the NVIC,
- *  when radio interrupts were re-enabled (address event bit in INTENSET set to
- *  1) and the radio ADDRESS event register read 1 (it was never cleared after
- *  the first address event), the radio did not enter the ISR! I would have
- *  expected that if the following were true, an interrupt would occur:
- *      -> NVIC ISER bit set to TRUE
- *      -> NVIC ISPR bit reads TRUE, meaning interrupt is pending.
- *      -> Radio peripheral interrupts are enabled for some event (or events).
- *      -> Corresponding event register(s) in radio peripheral read 1.
- *
- *  Not sure what the end result of all this is. We will clear the pending
- *  bit in the NVIC just to be sure when we disable the PHY.
- */
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-
-/*
- * Per nordic, the number of bytes needed for scratch is 16 + MAX_PKT_SIZE.
- * However, when I used a smaller size it still overwrote the scratchpad. Until
- * I figure this out I am just going to allocate 67 words so we have enough
- * space for 267 bytes of scratch. I used 268 bytes since not sure if this
- * needs to be aligned and burning a byte is no big deal.
- */
-//#define NRF_ENC_SCRATCH_WORDS (((MYNEWT_VAL(BLE_LL_MAX_PKT_SIZE) + 16) + 3) / 4)
-#define NRF_ENC_SCRATCH_WORDS   (67)
-
-uint32_t g_nrf_encrypt_scratchpad[NRF_ENC_SCRATCH_WORDS];
-
-struct nrf_ccm_data
-{
-    uint8_t key[16];
-    uint64_t pkt_counter;
-    uint8_t dir_bit;
-    uint8_t iv[8];
-} __attribute__((packed));
-
-struct nrf_ccm_data g_nrf_ccm_data;
-#endif
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-
-/* Packet start offset (in usecs). This is the preamble plus access address.
- * For LE Coded PHY this also includes CI and TERM1. */
-uint32_t
-ble_phy_mode_pdu_start_off(int phy_mode)
-{
-    return g_ble_phy_mode_pkt_start_off[phy_mode];
-}
-
-void
-ble_phy_mode_set(int cur_phy_mode, int txtorx_phy_mode)
-{
-#if MYNEWT_VAL(BSP_NRF52840)
-    /*
-     * nRF52840 Engineering A Errata v1.2
-     * [164] RADIO: Low sensitivity in long range mode
-     */
-    if ((cur_phy_mode == BLE_PHY_MODE_CODED_125KBPS) ||
-                                (cur_phy_mode == BLE_PHY_MODE_CODED_500KBPS)) {
-        *(volatile uint32_t *)0x4000173C |= 0x80000000;
-        *(volatile uint32_t *)0x4000173C = ((*(volatile uint32_t *)0x4000173C &
-                                            0xFFFFFF00) | 0x5C);
-    } else {
-        *(volatile uint32_t *)0x4000173C &= ~0x80000000;
-    }
-#endif
-
-    if (cur_phy_mode == BLE_PHY_MODE_1M) {
-        NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_1Mbit;
-        NRF_RADIO->PCNF0 = NRF_PCNF0_1M;
-    } else if (cur_phy_mode == BLE_PHY_MODE_2M) {
-        NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_2Mbit;
-        NRF_RADIO->PCNF0 = NRF_PCNF0_2M;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    } else if (cur_phy_mode == BLE_PHY_MODE_CODED_125KBPS) {
-        NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_LR125Kbit;
-        NRF_RADIO->PCNF0 = NRF_PCNF0_CODED;
-    } else if (cur_phy_mode == BLE_PHY_MODE_CODED_500KBPS) {
-        NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_LR500Kbit;
-        NRF_RADIO->PCNF0 = NRF_PCNF0_CODED;
-#endif
-    } else {
-        assert(0);
-    }
-
-    g_ble_phy_data.phy_cur_phy_mode = (uint8_t)cur_phy_mode;
-    g_ble_phy_data.phy_txtorx_phy_mode = (uint8_t)txtorx_phy_mode;
-}
-#endif
-
-int
-ble_phy_get_cur_phy(void)
-{
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    switch (g_ble_phy_data.phy_cur_phy_mode) {
-        case BLE_PHY_MODE_1M:
-            return BLE_PHY_1M;
-        case BLE_PHY_MODE_2M:
-            return BLE_PHY_2M;
-        case BLE_PHY_MODE_CODED_125KBPS:
-        case BLE_PHY_MODE_CODED_500KBPS:
-            return BLE_PHY_CODED;
-        default:
-            assert(0);
-            return -1;
-    }
-#else
-    return BLE_PHY_1M;
-#endif
-}
-
-/**
- * Copies the data from the phy receive buffer into a mbuf chain.
- *
- * @param dptr Pointer to receive buffer
- * @param rxpdu Pointer to already allocated mbuf chain
- *
- * NOTE: the packet header already has the total mbuf length in it. The
- * lengths of the individual mbufs are not set prior to calling.
- *
- */
-void
-ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu)
-{
-    uint16_t rem_bytes;
-    uint16_t mb_bytes;
-    uint16_t copylen;
-    uint32_t *dst;
-    uint32_t *src;
-    struct os_mbuf *m;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct os_mbuf_pkthdr *pkthdr;
-
-    /* Better be aligned */
-    assert(((uint32_t)dptr & 3) == 0);
-
-    pkthdr = OS_MBUF_PKTHDR(rxpdu);
-    rem_bytes = pkthdr->omp_len;
-
-    /* Fill in the mbuf pkthdr first. */
-    dst = (uint32_t *)(rxpdu->om_data);
-    src = (uint32_t *)dptr;
-
-    mb_bytes = (rxpdu->om_omp->omp_databuf_len - rxpdu->om_pkthdr_len - 4);
-    copylen = min(mb_bytes, rem_bytes);
-    copylen &= 0xFFFC;
-    rem_bytes -= copylen;
-    mb_bytes -= copylen;
-    rxpdu->om_len = copylen;
-    while (copylen > 0) {
-        *dst = *src;
-        ++dst;
-        ++src;
-        copylen -= 4;
-    }
-
-    /* Copy remaining bytes */
-    m = rxpdu;
-    while (rem_bytes > 0) {
-        /* If there are enough bytes in the mbuf, copy them and leave */
-        if (rem_bytes <= mb_bytes) {
-            memcpy(m->om_data + m->om_len, src, rem_bytes);
-            m->om_len += rem_bytes;
-            break;
-        }
-
-        m = SLIST_NEXT(m, om_next);
-        assert(m != NULL);
-
-        mb_bytes = m->om_omp->omp_databuf_len;
-        copylen = min(mb_bytes, rem_bytes);
-        copylen &= 0xFFFC;
-        rem_bytes -= copylen;
-        mb_bytes -= copylen;
-        m->om_len = copylen;
-        dst = (uint32_t *)m->om_data;
-        while (copylen > 0) {
-            *dst = *src;
-            ++dst;
-            ++src;
-            copylen -= 4;
-        }
-    }
-
-    /* Copy ble header */
-    ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
-    memcpy(ble_hdr, &g_ble_phy_data.rxhdr, sizeof(struct ble_mbuf_hdr));
-}
-
-/**
- * Called when we want to wait if the radio is in either the rx or tx
- * disable states. We want to wait until that state is over before doing
- * anything to the radio
- */
-static void
-nrf_wait_disabled(void)
-{
-    uint32_t state;
-
-    state = NRF_RADIO->STATE;
-    if (state != RADIO_STATE_STATE_Disabled) {
-        if ((state == RADIO_STATE_STATE_RxDisable) ||
-            (state == RADIO_STATE_STATE_TxDisable)) {
-            /* This will end within a short time (6 usecs). Just poll */
-            while (NRF_RADIO->STATE == state) {
-                /* If this fails, something is really wrong. Should last
-                 * no more than 6 usecs */
-            }
-        }
-    }
-}
-
-/**
- *
- *
- */
-static int
-ble_phy_set_start_time(uint32_t cputime, uint8_t rem_usecs, bool tx)
-{
-    uint32_t next_cc;
-    uint32_t cur_cc;
-    uint32_t cntr;
-    uint32_t delta;
-
-    /*
-     * We need to adjust start time to include radio ramp-up and TX pipeline
-     * delay (the latter only if applicable, so only for TX).
-     *
-     * Radio ramp-up time is 40 usecs and TX delay is 3 or 5 usecs depending on
-     * phy, thus we'll offset RTC by 2 full ticks (61 usecs) and then compensate
-     * using TIMER0 with 1 usec precision.
-     */
-
-    cputime -= 2;
-    rem_usecs += 61;
-    if (tx) {
-        rem_usecs -= BLE_PHY_T_TXENFAST;
-        rem_usecs -= g_ble_phy_t_txdelay[g_ble_phy_data.phy_cur_phy_mode];
-    } else {
-        rem_usecs -= BLE_PHY_T_RXENFAST;
-    }
-
-    /*
-     * rem_usecs will be no more than 2 ticks, but if it is more than single
-     * tick then we should better count one more low-power tick rather than
-     * 30 high-power usecs. Also make sure we don't set TIMER0 CC to 0 as the
-     * compare won't occur.
-     */
-
-    if (rem_usecs > 30) {
-        cputime++;
-        rem_usecs -= 30;
-    }
-
-    /*
-     * Can we set the RTC compare to start TIMER0? We can do it if:
-     *      a) Current compare value is not N+1 or N+2 ticks from current
-     *      counter.
-     *      b) The value we want to set is not at least N+2 from current
-     *      counter.
-     *
-     * NOTE: since the counter can tick 1 while we do these calculations we
-     * need to account for it.
-     */
-    next_cc = cputime & 0xffffff;
-    cur_cc = NRF_RTC0->CC[0];
-    cntr = NRF_RTC0->COUNTER;
-
-    delta = (cur_cc - cntr) & 0xffffff;
-    if ((delta <= 3) && (delta != 0)) {
-        return -1;
-    }
-    delta = (next_cc - cntr) & 0xffffff;
-    if ((delta & 0x800000) || (delta < 3)) {
-        return -1;
-    }
-
-    /* Clear and set TIMER0 to fire off at proper time */
-    NRF_TIMER0->TASKS_CLEAR = 1;
-    NRF_TIMER0->CC[0] = rem_usecs;
-    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
-
-    /* Set RTC compare to start TIMER0 */
-    NRF_RTC0->EVENTS_COMPARE[0] = 0;
-    NRF_RTC0->CC[0] = next_cc;
-    NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
-
-    /* Enable PPI */
-    NRF_PPI->CHENSET = PPI_CHEN_CH31_Msk;
-
-    /* Store the cputime at which we set the RTC */
-    g_ble_phy_data.phy_start_cputime = cputime;
-
-    return 0;
-}
-
-/**
- * Function is used to set PPI so that we can time out waiting for a reception
- * to occur. This happens for two reasons: we have sent a packet and we are
- * waiting for a respons (txrx should be set to ENABLE_TXRX) or we are
- * starting a connection event and we are a slave and we are waiting for the
- * master to send us a packet (txrx should be set to ENABLE_RX).
- *
- * NOTE: when waiting for a txrx turn-around, wfr_usecs is not used as there
- * is no additional time to wait; we know when we should receive the address of
- * the received frame.
- *
- * @param txrx Flag denoting if this wfr is a txrx turn-around or not.
- * @param tx_phy_mode phy mode for last TX (only valid for TX->RX)
- * @param wfr_usecs Amount of usecs to wait.
- */
-void
-ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs)
-{
-    uint32_t end_time;
-    uint8_t phy;
-
-    phy = g_ble_phy_data.phy_cur_phy_mode;
-
-    if (txrx == BLE_PHY_WFR_ENABLE_TXRX) {
-        /* RX shall start exactly T_IFS after TX end captured in CC[2] */
-        end_time = NRF_TIMER0->CC[2] + BLE_LL_IFS;
-        /* Adjust for delay between EVENT_END and actual TX end time */
-        end_time += g_ble_phy_t_txenddelay[tx_phy_mode];
-        /* Wait a bit longer due to allowed active clock accuracy */
-        end_time += 2;
-#if MYNEWT_VAL(BLE_PHY_CODED_RX_IFS_EXTRA_MARGIN) > 0
-        if ((phy == BLE_PHY_MODE_CODED_125KBPS) ||
-                                    (phy == BLE_PHY_MODE_CODED_500KBPS)) {
-            /*
-             * Some controllers exceed T_IFS when transmitting on coded phy
-             * so let's wait a bit longer to be able to talk to them if this
-             * workaround is enabled.
-             */
-            end_time += MYNEWT_VAL(BLE_PHY_CODED_RX_IFS_EXTRA_MARGIN);
-        }
-#endif
-    } else {
-        /*
-         * RX shall start no later than wfr_usecs after RX enabled.
-         * CC[0] is the time of RXEN so adjust for radio ram-up.
-         * Do not add jitter since this is already covered by LL.
-         */
-        end_time = NRF_TIMER0->CC[0] + BLE_PHY_T_RXENFAST + wfr_usecs;
-    }
-
-    /*
-     * Note: on LE Coded EVENT_ADDRESS is fired after TERM1 is received, so
-     *       we are actually calculating relative to start of packet payload
-     *       which is fine.
-     */
-
-    /* Adjust for receiving access address since this triggers EVENT_ADDRESS */
-    end_time += ble_phy_mode_pdu_start_off(phy);
-    /* Adjust for delay between actual access address RX and EVENT_ADDRESS */
-    end_time += g_ble_phy_t_rxaddrdelay[phy];
-
-    /* wfr_secs is the time from rxen until timeout */
-    NRF_TIMER0->CC[3] = end_time;
-    NRF_TIMER0->EVENTS_COMPARE[3] = 0;
-
-    /* Enable wait for response PPI */
-    NRF_PPI->CHENSET = (PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk);
-
-    /* Enable the disabled interrupt so we time out on events compare */
-    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-static uint32_t
-ble_phy_get_ccm_datarate(void)
-{
-#if BLE_LL_BT5_PHY_SUPPORTED
-    switch (g_ble_phy_data.phy_cur_phy_mode) {
-    case BLE_PHY_MODE_1M:
-        return CCM_MODE_DATARATE_1Mbit << CCM_MODE_DATARATE_Pos;
-    case BLE_PHY_MODE_2M:
-        return CCM_MODE_DATARATE_2Mbit << CCM_MODE_DATARATE_Pos;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    case BLE_PHY_MODE_CODED_125KBPS:
-        return CCM_MODE_DATARATE_125Kbps << CCM_MODE_DATARATE_Pos;
-    case BLE_PHY_MODE_CODED_500KBPS:
-        return CCM_MODE_DATARATE_500Kbps << CCM_MODE_DATARATE_Pos;
-#endif
-    }
-
-    assert(0);
-    return 0;
-#else
-    return CCM_MODE_DATARATE_1Mbit << CCM_MODE_DATARATE_Pos;
-#endif
-}
-#endif
-
-/**
- * Setup transceiver for receive.
- */
-static void
-ble_phy_rx_xcvr_setup(void)
-{
-    uint8_t *dptr;
-
-    dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
-    dptr += 3;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    if (g_ble_phy_data.phy_encrypted) {
-        NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-        NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-        NRF_CCM->OUTPTR = (uint32_t)dptr;
-        NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
-        NRF_CCM->MODE = CCM_MODE_LENGTH_Msk | CCM_MODE_MODE_Decryption |
-                                                    ble_phy_get_ccm_datarate();
-        NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-        NRF_CCM->SHORTS = 0;
-        NRF_CCM->EVENTS_ERROR = 0;
-        NRF_CCM->EVENTS_ENDCRYPT = 0;
-        NRF_CCM->TASKS_KSGEN = 1;
-        NRF_PPI->CHENSET = PPI_CHEN_CH25_Msk;
-    } else {
-        NRF_RADIO->PACKETPTR = (uint32_t)dptr;
-    }
-#else
-    NRF_RADIO->PACKETPTR = (uint32_t)dptr;
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    if (g_ble_phy_data.phy_privacy) {
-        NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Enabled;
-        NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-        NRF_AAR->SCRATCHPTR = (uint32_t)&g_ble_phy_data.phy_aar_scratch;
-        NRF_AAR->EVENTS_END = 0;
-        NRF_AAR->EVENTS_RESOLVED = 0;
-        NRF_AAR->EVENTS_NOTRESOLVED = 0;
-    } else {
-        if (g_ble_phy_data.phy_encrypted == 0) {
-            NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Disabled;
-        }
-    }
-#endif
-
-    /* Turn off trigger TXEN on output compare match and AAR on bcmatch */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk | PPI_CHEN_CH23_Msk;
-
-    /* Reset the rx started flag. Used for the wait for response */
-    g_ble_phy_data.phy_rx_started = 0;
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
-
-#if BLE_LL_BT5_PHY_SUPPORTED
-    /*
-     * On Coded PHY there are CI and TERM1 fields before PDU starts so we need
-     * to take this into account when setting up BCC.
-     */
-    if (g_ble_phy_data.phy_cur_phy_mode == BLE_PHY_MODE_CODED_125KBPS ||
-            g_ble_phy_data.phy_cur_phy_mode == BLE_PHY_MODE_CODED_500KBPS) {
-        g_ble_phy_data.phy_bcc_offset = 5;
-    } else {
-        g_ble_phy_data.phy_bcc_offset = 0;
-    }
-#else
-    g_ble_phy_data.phy_bcc_offset = 0;
-#endif
-
-    /* I want to know when 1st byte received (after address) */
-    NRF_RADIO->BCC = 8 + g_ble_phy_data.phy_bcc_offset; /* in bits */
-    NRF_RADIO->EVENTS_ADDRESS = 0;
-    NRF_RADIO->EVENTS_DEVMATCH = 0;
-    NRF_RADIO->EVENTS_BCMATCH = 0;
-    NRF_RADIO->EVENTS_RSSIEND = 0;
-    NRF_RADIO->EVENTS_CRCOK = 0;
-    NRF_RADIO->SHORTS = RADIO_SHORTS_END_DISABLE_Msk |
-                        RADIO_SHORTS_READY_START_Msk |
-                        RADIO_SHORTS_ADDRESS_BCSTART_Msk |
-                        RADIO_SHORTS_ADDRESS_RSSISTART_Msk |
-                        RADIO_SHORTS_DISABLED_RSSISTOP_Msk;
-
-    NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk;
-}
-
-/**
- * Called from interrupt context when the transmit ends
- *
- */
-static void
-ble_phy_tx_end_isr(void)
-{
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    int phy;
-#endif
-    uint8_t tx_phy_mode;
-    uint8_t was_encrypted;
-    uint8_t transition;
-    uint32_t rx_time;
-    uint32_t wfr_time;
-
-    /* Store PHY on which we've just transmitted smth */
-    tx_phy_mode = g_ble_phy_data.phy_cur_phy_mode;
-
-    /* If this transmission was encrypted we need to remember it */
-    was_encrypted = g_ble_phy_data.phy_encrypted;
-
-    /* Better be in TX state! */
-    assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
-
-    /* Log the event */
-    ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, g_ble_phy_data.phy_tx_pyld_len,
-               was_encrypted, NRF_TIMER0->CC[2]);
-    (void)was_encrypted;
-
-    /* Clear events and clear interrupt on disabled event */
-    NRF_RADIO->EVENTS_DISABLED = 0;
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
-    NRF_RADIO->EVENTS_END = 0;
-    wfr_time = NRF_RADIO->SHORTS;
-    (void)wfr_time;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    /*
-     * XXX: not sure what to do. We had a HW error during transmission.
-     * For now I just count a stat but continue on like all is good.
-     */
-    if (was_encrypted) {
-        if (NRF_CCM->EVENTS_ERROR) {
-            STATS_INC(ble_phy_stats, tx_hw_err);
-            NRF_CCM->EVENTS_ERROR = 0;
-        }
-    }
-#endif
-
-    /* Call transmit end callback */
-    if (g_ble_phy_data.txend_cb) {
-        g_ble_phy_data.txend_cb(g_ble_phy_data.txend_arg);
-    }
-
-    transition = g_ble_phy_data.phy_transition;
-    if (transition == BLE_PHY_TRANSITION_TX_RX) {
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-        /* See if a new phy has been specified for tx to rx transition */
-        phy = g_ble_phy_data.phy_txtorx_phy_mode;
-        if (phy != g_ble_phy_data.phy_cur_phy_mode) {
-            ble_phy_mode_set(phy, phy);
-        }
-#endif
-
-        /* Packet pointer needs to be reset. */
-        ble_phy_rx_xcvr_setup();
-
-        ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_TXRX, tx_phy_mode, 0);
-
-        /* Schedule RX exactly T_IFS after TX end captured in CC[2] */
-        rx_time = NRF_TIMER0->CC[2] + BLE_LL_IFS;
-        /* Adjust for delay between EVENT_END and actual TX end time */
-        rx_time += g_ble_phy_t_txenddelay[tx_phy_mode];
-        /* Adjust for radio ramp-up */
-        rx_time -= BLE_PHY_T_RXENFAST;
-        /* Start listening a bit earlier due to allowed active clock accuracy */
-        rx_time -= 2;
-
-        NRF_TIMER0->CC[0] = rx_time;
-        NRF_TIMER0->EVENTS_COMPARE[0] = 0;
-        NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
-    } else {
-        /*
-         * XXX: not sure we need to stop the timer here all the time. Or that
-         * it should be stopped here.
-         */
-        NRF_TIMER0->TASKS_STOP = 1;
-        NRF_TIMER0->TASKS_SHUTDOWN = 1;
-        NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk |
-                           PPI_CHEN_CH20_Msk | PPI_CHEN_CH31_Msk;
-        assert(transition == BLE_PHY_TRANSITION_NONE);
-    }
-}
-
-static inline uint8_t
-ble_phy_get_cur_rx_phy_mode(void)
-{
-    uint8_t phy;
-
-    phy = g_ble_phy_data.phy_cur_phy_mode;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    /*
-     * For Coded PHY mode can be set to either codings since actual coding is
-     * set in packet header. However, here we need actual coding of received
-     * packet as this determines pipeline delays so need to figure this out
-     * using CI field.
-     */
-    if ((phy == BLE_PHY_MODE_CODED_125KBPS) ||
-                                    (phy == BLE_PHY_MODE_CODED_500KBPS)) {
-        phy = NRF_RADIO->PDUSTAT & RADIO_PDUSTAT_CISTAT_Msk ?
-                                   BLE_PHY_MODE_CODED_500KBPS :
-                                   BLE_PHY_MODE_CODED_125KBPS;
-    }
-#endif
-
-    return phy;
-}
-
-static void
-ble_phy_rx_end_isr(void)
-{
-    int rc;
-    uint8_t *dptr;
-    uint8_t crcok;
-    uint32_t tx_time;
-    struct ble_mbuf_hdr *ble_hdr;
-
-    /* Clear events and clear interrupt */
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_END_Msk;
-
-    /* Disable automatic RXEN */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-
-    /* Set RSSI and CRC status flag in header */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    assert(NRF_RADIO->EVENTS_RSSIEND != 0);
-    ble_hdr->rxinfo.rssi = -1 * NRF_RADIO->RSSISAMPLE;
-
-    dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
-    dptr += 3;
-
-    /* Count PHY crc errors and valid packets */
-    crcok = NRF_RADIO->EVENTS_CRCOK;
-    if (!crcok) {
-        STATS_INC(ble_phy_stats, rx_crc_err);
-    } else {
-        STATS_INC(ble_phy_stats, rx_valid);
-        ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-        if (g_ble_phy_data.phy_encrypted) {
-            /* Only set MIC failure flag if frame is not zero length */
-            if ((dptr[1] != 0) && (NRF_CCM->MICSTATUS == 0)) {
-                ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_MIC_FAILURE;
-            }
-
-            /*
-             * XXX: not sure how to deal with this. This should not
-             * be a MIC failure but we should not hand it up. I guess
-             * this is just some form of rx error and that is how we
-             * handle it? For now, just set CRC error flags
-             */
-            if (NRF_CCM->EVENTS_ERROR) {
-                STATS_INC(ble_phy_stats, rx_hw_err);
-                ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK;
-            }
-
-            /*
-             * XXX: This is a total hack work-around for now but I dont
-             * know what else to do. If ENDCRYPT is not set and we are
-             * encrypted we need to not trust this frame and drop it.
-             */
-            if (NRF_CCM->EVENTS_ENDCRYPT == 0) {
-                STATS_INC(ble_phy_stats, rx_hw_err);
-                ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK;
-            }
-        }
-#endif
-    }
-
-    /*
-     * Let's schedule TX now and we will just cancel it after processing RXed
-     * packet if we don't need TX.
-     *
-     * We need this to initiate connection in case AUX_CONNECT_REQ was sent on
-     * LE Coded S8. In this case the time we process RXed packet is roughly the
-     * same as the limit when we need to have TX scheduled (i.e. TIMER0 and PPI
-     * armed) so we may simply miss the slot and set the timer in the past.
-     *
-     * When TX is scheduled in advance, we may event process packet a bit longer
-     * during radio ramp-up - this gives us extra 40 usecs which is more than
-     * enough.
-     */
-
-    /* Schedule TX exactly T_IFS after RX end captured in CC[2] */
-    tx_time = NRF_TIMER0->CC[2] + BLE_LL_IFS;
-    /* Adjust for delay between actual RX end time and EVENT_END */
-    tx_time -= g_ble_phy_t_rxenddelay[ble_hdr->rxinfo.phy_mode];
-    /* Adjust for radio ramp-up */
-    tx_time -= BLE_PHY_T_TXENFAST;
-    /* Adjust for delay between EVENT_READY and actual TX start time */
-    /* XXX: we may have asymmetric phy so next phy may be different... */
-    tx_time -= g_ble_phy_t_txdelay[g_ble_phy_data.phy_cur_phy_mode];
-
-    NRF_TIMER0->CC[0] = tx_time;
-    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
-    NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
-
-    /*
-     * XXX: Hack warning!
-     *
-     * It may happen (during flash erase) that CPU is stopped for a moment and
-     * TIMER0 already counted past CC[0]. In such case we will be stuck waiting
-     * for TX to start since EVENTS_COMPARE[0] will not happen any time soon.
-     * For now let's set a flag denoting that we are late in RX-TX transition so
-     * ble_phy_tx() will fail - this allows everything to cleanup nicely without
-     * the need for extra handling in many places.
-     *
-     * Note: CC[3] is used only for wfr which we do not need here.
-     */
-    NRF_TIMER0->TASKS_CAPTURE[3] = 1;
-    if (NRF_TIMER0->CC[3] > NRF_TIMER0->CC[0]) {
-        NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
-        g_ble_phy_data.phy_transition_late = 1;
-    }
-
-    /*
-     * XXX: This is a horrible ugly hack to deal with the RAM S1 byte
-     * that is not sent over the air but is present here. Simply move the
-     * data pointer to deal with it. Fix this later.
-     */
-    dptr[2] = dptr[1];
-    dptr[1] = dptr[0];
-    rc = ble_ll_rx_end(dptr + 1, ble_hdr);
-    if (rc < 0) {
-        ble_phy_disable();
-    }
-}
-
-static bool
-ble_phy_rx_start_isr(void)
-{
-    int rc;
-    uint32_t state;
-    uint32_t usecs;
-    uint32_t pdu_usecs;
-    uint32_t ticks;
-    struct ble_mbuf_hdr *ble_hdr;
-    uint8_t *dptr;
-
-    dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
-
-    /* Clear events and clear interrupt */
-    NRF_RADIO->EVENTS_ADDRESS = 0;
-
-    /* Clear wfr timer channels and DISABLED interrupt */
-    NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk | RADIO_INTENCLR_ADDRESS_Msk;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk;
-
-    /* Initialize the ble mbuf header */
-    ble_hdr = &g_ble_phy_data.rxhdr;
-    ble_hdr->rxinfo.flags = ble_ll_state_get();
-    ble_hdr->rxinfo.channel = g_ble_phy_data.phy_chan;
-    ble_hdr->rxinfo.handle = 0;
-    ble_hdr->rxinfo.phy = ble_phy_get_cur_phy();
-    ble_hdr->rxinfo.phy_mode = ble_phy_get_cur_rx_phy_mode();
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    ble_hdr->rxinfo.user_data = NULL;
-#endif
-
-    /*
-     * Calculate accurate packets start time (with remainder)
-     *
-     * We may start receiving packet somewhere during preamble in which case
-     * it is possible that actual transmission started before TIMER0 was
-     * running - need to take this into account.
-     */
-    usecs = NRF_TIMER0->CC[1];
-    pdu_usecs = ble_phy_mode_pdu_start_off(ble_hdr->rxinfo.phy_mode) +
-                g_ble_phy_t_rxaddrdelay[ble_hdr->rxinfo.phy_mode];
-    if (usecs < pdu_usecs) {
-        ticks--;
-        usecs += 30;
-    }
-    usecs -= pdu_usecs;
-
-    ticks = os_cputime_usecs_to_ticks(usecs);
-    usecs -= os_cputime_ticks_to_usecs(ticks);
-    if (usecs == 31) {
-        usecs = 0;
-        ++ticks;
-    }
-
-    ble_hdr->beg_cputime = g_ble_phy_data.phy_start_cputime + ticks;
-    ble_hdr->rem_usecs = usecs;
-
-    /* XXX: I wonder if we always have the 1st byte. If we need to wait for
-     * rx chain delay, it could be 18 usecs from address interrupt. The
-       nrf52 may be able to get here early. */
-    /* Wait to get 1st byte of frame */
-    while (1) {
-        state = NRF_RADIO->STATE;
-        if (NRF_RADIO->EVENTS_BCMATCH != 0) {
-            break;
-        }
-
-        /*
-         * If state is disabled, we should have the BCMATCH. If not,
-         * something is wrong!
-         */
-        if (state == RADIO_STATE_STATE_Disabled) {
-            NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-            NRF_RADIO->SHORTS = 0;
-            return false;
-        }
-    }
-
-    /* Call Link Layer receive start function */
-    rc = ble_ll_rx_start(dptr + 3,
-                         g_ble_phy_data.phy_chan,
-                         &g_ble_phy_data.rxhdr);
-    if (rc >= 0) {
-        /* Set rx started flag and enable rx end ISR */
-        g_ble_phy_data.phy_rx_started = 1;
-        NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        /* Must start aar if we need to  */
-        if (g_ble_phy_data.phy_privacy) {
-            NRF_RADIO->EVENTS_BCMATCH = 0;
-            NRF_PPI->CHENSET = PPI_CHEN_CH23_Msk;
-
-            /*
-             * Setup AAR to resolve AdvA and trigger it after complete address
-             * is received, i.e. after PDU header and AdvA is received.
-             *
-             * AdvA starts at 4th octet in receive buffer, after S0, len and S1
-             * fields.
-             *
-             * In case of extended advertising AdvA is located after extended
-             * header (+2 octets).
-             */
-            if (BLE_MBUF_HDR_EXT_ADV(&g_ble_phy_data.rxhdr)) {
-                NRF_AAR->ADDRPTR = (uint32_t)(dptr + 5);
-                NRF_RADIO->BCC = (BLE_DEV_ADDR_LEN + BLE_LL_PDU_HDR_LEN + 2) * 8 +
-                                 g_ble_phy_data.phy_bcc_offset;
-            } else {
-                NRF_AAR->ADDRPTR = (uint32_t)(dptr + 3);
-                NRF_RADIO->BCC = (BLE_DEV_ADDR_LEN + BLE_LL_PDU_HDR_LEN) * 8 +
-                                 g_ble_phy_data.phy_bcc_offset;
-            }
-        }
-#endif
-    } else {
-        /* Disable PHY */
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, rx_aborts);
-    }
-
-    /* Count rx starts */
-    STATS_INC(ble_phy_stats, rx_starts);
-
-    return true;
-}
-
-static void
-ble_phy_isr(void)
-{
-    uint32_t irq_en;
-
-    /* Read irq register to determine which interrupts are enabled */
-    irq_en = NRF_RADIO->INTENCLR;
-
-    /*
-     * NOTE: order of checking is important! Possible, if things get delayed,
-     * we have both an ADDRESS and DISABLED interrupt in rx state. If we get
-     * an address, we disable the DISABLED interrupt.
-     */
-
-    /* We get this if we have started to receive a frame */
-    if ((irq_en & RADIO_INTENCLR_ADDRESS_Msk) && NRF_RADIO->EVENTS_ADDRESS) {
-        /*
-         * wfr timer is calculated to expire at the exact time we should start
-         * receiving a packet (with 1 usec precision) so it is possible  it will
-         * fire at the same time as EVENT_ADDRESS. If this happens, radio will
-         * be disabled while we are waiting for EVENT_BCCMATCH after 1st byte
-         * of payload is received and ble_phy_rx_start_isr() will fail. In this
-         * case we should not clear DISABLED irq mask so it will be handled as
-         * regular radio disabled event below. In other case radio was disabled
-         * on purpose and there's nothing more to handle so we can clear mask.
-         */
-        if (ble_phy_rx_start_isr()) {
-            irq_en &= ~RADIO_INTENCLR_DISABLED_Msk;
-        }
-    }
-
-    /* Check for disabled event. This only happens for transmits now */
-    if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-        if (g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) {
-            NRF_RADIO->EVENTS_DISABLED = 0;
-            ble_ll_wfr_timer_exp(NULL);
-        } else if (g_ble_phy_data.phy_state == BLE_PHY_STATE_IDLE) {
-            assert(0);
-        } else {
-            ble_phy_tx_end_isr();
-        }
-    }
-
-    /* Receive packet end (we dont enable this for transmit) */
-    if ((irq_en & RADIO_INTENCLR_END_Msk) && NRF_RADIO->EVENTS_END) {
-        ble_phy_rx_end_isr();
-    }
-
-    g_ble_phy_data.phy_transition_late = 0;
-
-    /* Ensures IRQ is cleared */
-    irq_en = NRF_RADIO->SHORTS;
-
-    /* Count # of interrupts */
-    STATS_INC(ble_phy_stats, phy_isrs);
-}
-
-#if MYNEWT_VAL(BLE_PHY_DBG_TIME_TXRXEN_READY_PIN) >= 0 || \
-        MYNEWT_VAL(BLE_PHY_DBG_TIME_ADDRESS_END_PIN) >= 0 || \
-        MYNEWT_VAL(BLE_PHY_DBG_TIME_WFR_PIN) >= 0
-static inline void
-ble_phy_dbg_time_setup_gpiote(int index, int pin)
-{
-    hal_gpio_init_out(pin, 0);
-
-    NRF_GPIOTE->CONFIG[index] =
-                        (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
-                        ((pin & 0x1F) << GPIOTE_CONFIG_PSEL_Pos) |
-                        ((pin > 31) << GPIOTE_CONFIG_PORT_Pos);
-}
-#endif
-
-static void
-ble_phy_dbg_time_setup(void)
-{
-    int gpiote_idx __attribute__((unused)) = 8;
-
-    /*
-     * We setup GPIOTE starting from last configuration index to minimize risk
-     * of conflict with GPIO setup via hal. It's not great solution, but since
-     * this is just debugging code we can live with this.
-     */
-
-#if MYNEWT_VAL(BLE_PHY_DBG_TIME_TXRXEN_READY_PIN) >= 0
-    ble_phy_dbg_time_setup_gpiote(--gpiote_idx,
-                              MYNEWT_VAL(BLE_PHY_DBG_TIME_TXRXEN_READY_PIN));
-
-    NRF_PPI->CH[17].EEP = (uint32_t)&(NRF_RADIO->EVENTS_READY);
-    NRF_PPI->CH[17].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_CLR[gpiote_idx]);
-    NRF_PPI->CHENSET = PPI_CHEN_CH17_Msk;
-
-    /* CH[20] and PPI CH[21] are on to trigger TASKS_TXEN or TASKS_RXEN */
-    NRF_PPI->FORK[20].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_SET[gpiote_idx]);
-    NRF_PPI->FORK[21].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_SET[gpiote_idx]);
-#endif
-
-#if MYNEWT_VAL(BLE_PHY_DBG_TIME_ADDRESS_END_PIN) >= 0
-    ble_phy_dbg_time_setup_gpiote(--gpiote_idx,
-                              MYNEWT_VAL(BLE_PHY_DBG_TIME_ADDRESS_END_PIN));
-
-    /* CH[26] and CH[27] are always on for EVENT_ADDRESS and EVENT_END */
-    NRF_PPI->FORK[26].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_SET[gpiote_idx]);
-    NRF_PPI->FORK[27].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_CLR[gpiote_idx]);
-#endif
-
-#if MYNEWT_VAL(BLE_PHY_DBG_TIME_WFR_PIN) >= 0
-    ble_phy_dbg_time_setup_gpiote(--gpiote_idx,
-                              MYNEWT_VAL(BLE_PHY_DBG_TIME_WFR_PIN));
-
-    NRF_PPI->CH[18].EEP = (uint32_t)&(NRF_RADIO->EVENTS_RXREADY);
-    NRF_PPI->CH[18].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_SET[gpiote_idx]);
-    NRF_PPI->CH[19].EEP = (uint32_t)&(NRF_RADIO->EVENTS_DISABLED);
-    NRF_PPI->CH[19].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_CLR[gpiote_idx]);
-    NRF_PPI->CHENSET = PPI_CHEN_CH18_Msk | PPI_CHEN_CH19_Msk;
-
-    /* CH[4] and CH[5] are always on for wfr */
-    NRF_PPI->FORK[4].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_CLR[gpiote_idx]);
-    NRF_PPI->FORK[5].TEP = (uint32_t)&(NRF_GPIOTE->TASKS_CLR[gpiote_idx]);
-#endif
-}
-
-/**
- * ble phy init
- *
- * Initialize the PHY.
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_init(void)
-{
-    int rc;
-
-    /* Default phy to use is 1M */
-    g_ble_phy_data.phy_cur_phy_mode = BLE_PHY_MODE_1M;
-    g_ble_phy_data.phy_txtorx_phy_mode = BLE_PHY_MODE_1M;
-
-#if !defined(BLE_XCVR_RFCLK)
-    uint32_t os_tmo;
-
-    /* Make sure HFXO is started */
-    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
-    NRF_CLOCK->TASKS_HFCLKSTART = 1;
-    os_tmo = os_time_get() + (5 * (1000 / OS_TICKS_PER_SEC));
-    while (1) {
-        if (NRF_CLOCK->EVENTS_HFCLKSTARTED) {
-            break;
-        }
-        if ((int32_t)(os_time_get() - os_tmo) > 0) {
-            return BLE_PHY_ERR_INIT;
-        }
-    }
-#endif
-
-    /* Set phy channel to an invalid channel so first set channel works */
-    g_ble_phy_data.phy_chan = BLE_PHY_NUM_CHANS;
-
-    /* Toggle peripheral power to reset (just in case) */
-    NRF_RADIO->POWER = 0;
-    NRF_RADIO->POWER = 1;
-
-    /* Disable all interrupts */
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-
-    /* Set configuration registers */
-    NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_1Mbit;
-    NRF_RADIO->PCNF0 = NRF_PCNF0;
-
-    /* XXX: should maxlen be 251 for encryption? */
-    NRF_RADIO->PCNF1 = NRF_MAXLEN |
-                       (RADIO_PCNF1_ENDIAN_Little <<  RADIO_PCNF1_ENDIAN_Pos) |
-                       (NRF_BALEN << RADIO_PCNF1_BALEN_Pos) |
-                       RADIO_PCNF1_WHITEEN_Msk;
-
-    /* Enable radio fast ramp-up */
-    NRF_RADIO->MODECNF0 |= (RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos) &
-                            RADIO_MODECNF0_RU_Msk;
-
-    /* Set logical address 1 for TX and RX */
-    NRF_RADIO->TXADDRESS  = 0;
-    NRF_RADIO->RXADDRESSES  = (1 << 0);
-
-    /* Configure the CRC registers */
-    NRF_RADIO->CRCCNF = (RADIO_CRCCNF_SKIPADDR_Skip << RADIO_CRCCNF_SKIPADDR_Pos) | RADIO_CRCCNF_LEN_Three;
-
-    /* Configure BLE poly */
-    NRF_RADIO->CRCPOLY = 0x0000065B;
-
-    /* Configure IFS */
-    NRF_RADIO->TIFS = BLE_LL_IFS;
-
-    /* Captures tx/rx start in timer0 cc 1 and tx/rx end in timer0 cc 2 */
-    NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk | PPI_CHEN_CH27_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    NRF_CCM->INTENCLR = 0xffffffff;
-    NRF_CCM->SHORTS = CCM_SHORTS_ENDKSGEN_CRYPT_Msk;
-    NRF_CCM->EVENTS_ERROR = 0;
-    memset(g_nrf_encrypt_scratchpad, 0, sizeof(g_nrf_encrypt_scratchpad));
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    g_ble_phy_data.phy_aar_scratch = 0;
-    NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-    NRF_AAR->INTENCLR = 0xffffffff;
-    NRF_AAR->EVENTS_END = 0;
-    NRF_AAR->EVENTS_RESOLVED = 0;
-    NRF_AAR->EVENTS_NOTRESOLVED = 0;
-    NRF_AAR->NIRK = 0;
-#endif
-
-    /* TIMER0 setup for PHY when using RTC */
-    NRF_TIMER0->TASKS_STOP = 1;
-    NRF_TIMER0->TASKS_SHUTDOWN = 1;
-    NRF_TIMER0->BITMODE = 3;    /* 32-bit timer */
-    NRF_TIMER0->MODE = 0;       /* Timer mode */
-    NRF_TIMER0->PRESCALER = 4;  /* gives us 1 MHz */
-
-    /*
-     * PPI setup.
-     * Channel 4: Captures TIMER0 in CC[3] when EVENTS_ADDRESS occurs. Used
-     *            to cancel the wait for response timer.
-     * Channel 5: TIMER0 CC[3] to TASKS_DISABLE on radio. This is the wait
-     *            for response timer.
-     */
-    NRF_PPI->CH[4].EEP = (uint32_t)&(NRF_RADIO->EVENTS_ADDRESS);
-    NRF_PPI->CH[4].TEP = (uint32_t)&(NRF_TIMER0->TASKS_CAPTURE[3]);
-    NRF_PPI->CH[5].EEP = (uint32_t)&(NRF_TIMER0->EVENTS_COMPARE[3]);
-    NRF_PPI->CH[5].TEP = (uint32_t)&(NRF_RADIO->TASKS_DISABLE);
-
-    /* Set isr in vector table and enable interrupt */
-    NVIC_SetPriority(RADIO_IRQn, 0);
-    NVIC_SetVector(RADIO_IRQn, (uint32_t)ble_phy_isr);
-    NVIC_EnableIRQ(RADIO_IRQn);
-
-    /* Register phy statistics */
-    if (!g_ble_phy_data.phy_stats_initialized) {
-        rc = stats_init_and_reg(STATS_HDR(ble_phy_stats),
-                                STATS_SIZE_INIT_PARMS(ble_phy_stats,
-                                                      STATS_SIZE_32),
-                                STATS_NAME_INIT_PARMS(ble_phy_stats),
-                                "ble_phy");
-        assert(rc == 0);
-
-        g_ble_phy_data.phy_stats_initialized  = 1;
-    }
-
-    ble_phy_dbg_time_setup();
-
-    return 0;
-}
-
-/**
- * Puts the phy into receive mode.
- *
- * @return int 0: success; BLE Phy error code otherwise
- */
-int
-ble_phy_rx(void)
-{
-    /* Check radio state */
-    nrf_wait_disabled();
-    if (NRF_RADIO->STATE != RADIO_STATE_STATE_Disabled) {
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, radio_state_errs);
-        return BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    /* Make sure all interrupts are disabled */
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-
-    /* Clear events prior to enabling receive */
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->EVENTS_DISABLED = 0;
-
-    /* Setup for rx */
-    ble_phy_rx_xcvr_setup();
-
-    /* Start the receive task in the radio if not automatically going to rx */
-    if ((NRF_PPI->CHEN & PPI_CHEN_CH21_Msk) == 0) {
-        NRF_RADIO->TASKS_RXEN = 1;
-    }
-
-    ble_ll_log(BLE_LL_LOG_ID_PHY_RX, g_ble_phy_data.phy_encrypted, 0, 0);
-
-    return 0;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
-void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
-{
-    memcpy(g_nrf_ccm_data.key, key, 16);
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    memcpy(g_nrf_ccm_data.iv, iv, 8);
-    g_nrf_ccm_data.dir_bit = is_master;
-    g_ble_phy_data.phy_encrypted = 1;
-    /* Enable the module (AAR cannot be on while CCM on) */
-    NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Disabled;
-    NRF_CCM->ENABLE = CCM_ENABLE_ENABLE_Enabled;
-}
-
-void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
-{
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    g_nrf_ccm_data.dir_bit = dir;
-}
-
-void
-ble_phy_encrypt_disable(void)
-{
-    NRF_PPI->CHENCLR = PPI_CHEN_CH25_Msk;
-    NRF_CCM->TASKS_STOP = 1;
-    NRF_CCM->EVENTS_ERROR = 0;
-    NRF_CCM->ENABLE = CCM_ENABLE_ENABLE_Disabled;
-
-    g_ble_phy_data.phy_encrypted = 0;
-}
-#endif
-
-void
-ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
-{
-    /* Set transmit end callback and arg */
-    g_ble_phy_data.txend_cb = txend_cb;
-    g_ble_phy_data.txend_arg = arg;
-}
-
-/**
- * Called to set the start time of a transmission.
- *
- * This function is called to set the start time when we are not going from
- * rx to tx automatically.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime   This is the tick at which the 1st bit of the preamble
- *                  should be transmitted
- * @param rem_usecs This is used only when the underlying timing uses a 32.768
- *                  kHz crystal. It is the # of usecs from the cputime tick
- *                  at which the first bit of the preamble should be
- *                  transmitted.
- * @return int
- */
-int
-ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    int rc;
-
-    /* XXX: This should not be necessary, but paranoia is good! */
-    /* Clear timer0 compare to RXEN since we are transmitting */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-
-    if (ble_phy_set_start_time(cputime, rem_usecs, true) != 0) {
-        STATS_INC(ble_phy_stats, tx_late);
-        ble_phy_disable();
-        rc = BLE_PHY_ERR_TX_LATE;
-    } else {
-        /* Enable PPI to automatically start TXEN */
-        NRF_PPI->CHENSET = PPI_CHEN_CH20_Msk;
-        rc = 0;
-    }
-    return rc;
-}
-
-/**
- * Called to set the start time of a reception
- *
- * This function acts a bit differently than transmit. If we are late getting
- * here we will still attempt to receive.
- *
- * NOTE: care must be taken when calling this function. The channel should
- * already be set.
- *
- * @param cputime
- *
- * @return int
- */
-int
-ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
-{
-    bool late = false;
-    int rc = 0;
-
-    /* XXX: This should not be necessary, but paranoia is good! */
-    /* Clear timer0 compare to TXEN since we are transmitting */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH20_Msk;
-
-    if (ble_phy_set_start_time(cputime, rem_usecs, false) != 0) {
-        STATS_INC(ble_phy_stats, rx_late);
-
-        /*
-         * Disable PPI so ble_phy_rx() will start RX immediately after
-         * configuring receiver.
-         */
-        NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
-        late = true;
-    } else {
-        /* Enable PPI to automatically start RXEN */
-        NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk;
-    }
-
-    /* Start rx */
-    rc = ble_phy_rx();
-
-    /*
-     * If we enabled receiver but were late, let's return proper error code so
-     * caller can handle this.
-     */
-    if (!rc && late) {
-        rc = BLE_PHY_ERR_RX_LATE;
-    }
-
-    return rc;
-}
-
-int
-ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
-{
-    int rc;
-    uint8_t *dptr;
-    uint8_t *pktptr;
-    uint8_t payload_len;
-    uint8_t hdr_byte;
-    uint32_t state;
-    uint32_t shortcuts;
-
-    if (g_ble_phy_data.phy_transition_late) {
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, tx_late);
-        return BLE_PHY_ERR_TX_LATE;
-    }
-
-    /*
-     * This check is to make sure that the radio is not in a state where
-     * it is moving to disabled state. If so, let it get there.
-     */
-    nrf_wait_disabled();
-
-    /*
-     * XXX: Although we may not have to do this here, I clear all the PPI
-     * that should not be used when transmitting. Some of them are only enabled
-     * if encryption and/or privacy is on, but I dont care. Better to be
-     * paranoid, and if you are going to clear one, might as well clear them
-     * all.
-     */
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH23_Msk |
-                       PPI_CHEN_CH25_Msk;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    if (g_ble_phy_data.phy_encrypted) {
-        dptr = (uint8_t *)&g_ble_phy_enc_buf[0];
-        pktptr = (uint8_t *)&g_ble_phy_tx_buf[0];
-        NRF_CCM->SHORTS = CCM_SHORTS_ENDKSGEN_CRYPT_Msk;
-        NRF_CCM->INPTR = (uint32_t)dptr;
-        NRF_CCM->OUTPTR = (uint32_t)pktptr;
-        NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
-        NRF_CCM->EVENTS_ERROR = 0;
-        NRF_CCM->MODE = CCM_MODE_LENGTH_Msk | ble_phy_get_ccm_datarate();
-        NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
-    } else {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
-#endif
-        dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
-        pktptr = dptr;
-    }
-#else
-    dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
-    pktptr = dptr;
-#endif
-
-    /* Set PDU payload */
-    payload_len = pducb(&dptr[3], pducb_arg, &hdr_byte);
-
-    /* RAM representation has S0, LENGTH and S1 fields. (3 bytes) */
-    dptr[0] = hdr_byte;
-    dptr[1] = payload_len;
-    dptr[2] = 0;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    /* Start key-stream generation and encryption (via short) */
-    if (g_ble_phy_data.phy_encrypted) {
-        NRF_CCM->TASKS_KSGEN = 1;
-    }
-#endif
-
-    NRF_RADIO->PACKETPTR = (uint32_t)pktptr;
-
-    /* Clear the ready, end and disabled events */
-    NRF_RADIO->EVENTS_READY = 0;
-    NRF_RADIO->EVENTS_END = 0;
-    NRF_RADIO->EVENTS_DISABLED = 0;
-
-    /* Enable shortcuts for transmit start/end. */
-    shortcuts = RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_READY_START_Msk;
-    NRF_RADIO->SHORTS = shortcuts;
-    NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
-
-    /* Set the PHY transition */
-    g_ble_phy_data.phy_transition = end_trans;
-
-    /* Set transmitted payload length */
-    g_ble_phy_data.phy_tx_pyld_len = payload_len;
-
-    /* If we already started transmitting, abort it! */
-    state = NRF_RADIO->STATE;
-    if (state != RADIO_STATE_STATE_Tx) {
-        /* Set phy state to transmitting and count packet statistics */
-        g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
-        STATS_INC(ble_phy_stats, tx_good);
-        STATS_INCN(ble_phy_stats, tx_bytes, payload_len + BLE_LL_PDU_HDR_LEN);
-        rc = BLE_ERR_SUCCESS;
-    } else {
-        ble_phy_disable();
-        STATS_INC(ble_phy_stats, tx_late);
-        rc = BLE_PHY_ERR_RADIO_STATE;
-    }
-
-    return rc;
-}
-
-/**
- * ble phy txpwr set
- *
- * Set the transmit output power (in dBm).
- *
- * NOTE: If the output power specified is within the BLE limits but outside
- * the chip limits, we "rail" the power level so we dont exceed the min/max
- * chip values.
- *
- * @param dbm Power output in dBm.
- *
- * @return int 0: success; anything else is an error
- */
-int
-ble_phy_txpwr_set(int dbm)
-{
-    /* Check valid range */
-    assert(dbm <= BLE_PHY_MAX_PWR_DBM);
-
-    /* "Rail" power level if outside supported range */
-    if (dbm > NRF_TX_PWR_MAX_DBM) {
-        dbm = NRF_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < NRF_TX_PWR_MIN_DBM) {
-            dbm = NRF_TX_PWR_MIN_DBM;
-        }
-    }
-
-    NRF_RADIO->TXPOWER = dbm;
-    g_ble_phy_data.phy_txpwr_dbm = dbm;
-
-    return 0;
-}
-
-/**
- * ble phy txpwr round
- *
- * Get the rounded transmit output power (in dBm).
- *
- * @param dbm Power output in dBm.
- *
- * @return int Rounded power in dBm
- */
-int ble_phy_txpower_round(int dbm)
-{
-    /* "Rail" power level if outside supported range */
-    if (dbm > NRF_TX_PWR_MAX_DBM) {
-        dbm = NRF_TX_PWR_MAX_DBM;
-    } else {
-        if (dbm < NRF_TX_PWR_MIN_DBM) {
-            dbm = NRF_TX_PWR_MIN_DBM;
-        }
-    }
-
-    return dbm;
-}
-
-/**
- * ble phy set access addr
- *
- * Set access address.
- *
- * @param access_addr Access address
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_set_access_addr(uint32_t access_addr)
-{
-    NRF_RADIO->BASE0 = (access_addr << 8);
-    NRF_RADIO->PREFIX0 = (NRF_RADIO->PREFIX0 & 0xFFFFFF00) | (access_addr >> 24);
-
-    g_ble_phy_data.phy_access_address = access_addr;
-
-    return 0;
-}
-
-/**
- * ble phy txpwr get
- *
- * Get the transmit power.
- *
- * @return int  The current PHY transmit power, in dBm
- */
-int
-ble_phy_txpwr_get(void)
-{
-    return g_ble_phy_data.phy_txpwr_dbm;
-}
-
-/**
- * ble phy setchan
- *
- * Sets the logical frequency of the transceiver. The input parameter is the
- * BLE channel index (0 to 39, inclusive). The NRF frequency register works like
- * this: logical frequency = 2400 + FREQ (MHz).
- *
- * Thus, to get a logical frequency of 2402 MHz, you would program the
- * FREQUENCY register to 2.
- *
- * @param chan This is the Data Channel Index or Advertising Channel index
- *
- * @return int 0: success; PHY error code otherwise
- */
-int
-ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
-{
-    assert(chan < BLE_PHY_NUM_CHANS);
-
-    /* Check for valid channel range */
-    if (chan >= BLE_PHY_NUM_CHANS) {
-        return BLE_PHY_ERR_INV_PARAM;
-    }
-
-    /* Set current access address */
-    ble_phy_set_access_addr(access_addr);
-
-    /* Configure crcinit */
-    NRF_RADIO->CRCINIT = crcinit;
-
-    /* Set the frequency and the data whitening initial value */
-    g_ble_phy_data.phy_chan = chan;
-    NRF_RADIO->FREQUENCY = g_ble_phy_chan_freq[chan];
-    NRF_RADIO->DATAWHITEIV = chan;
-
-    ble_ll_log(BLE_LL_LOG_ID_PHY_SETCHAN, chan, freq, access_addr);
-
-    return 0;
-}
-
-/**
- * Stop the timer used to count microseconds when using RTC for cputime
- */
-void
-ble_phy_stop_usec_timer(void)
-{
-    NRF_TIMER0->TASKS_STOP = 1;
-    NRF_TIMER0->TASKS_SHUTDOWN = 1;
-    NRF_RTC0->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
-}
-
-/**
- * ble phy disable irq and ppi
- *
- * This routine is to be called when reception was stopped due to either a
- * wait for response timeout or a packet being received and the phy is to be
- * restarted in receive mode. Generally, the disable routine is called to stop
- * the phy.
- */
-void
-ble_phy_disable_irq_and_ppi(void)
-{
-    NRF_RADIO->INTENCLR = NRF_RADIO_IRQ_MASK_ALL;
-    NRF_RADIO->SHORTS = 0;
-    NRF_RADIO->TASKS_DISABLE = 1;
-    NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk | PPI_CHEN_CH20_Msk |
-          PPI_CHEN_CH21_Msk | PPI_CHEN_CH23_Msk |
-          PPI_CHEN_CH25_Msk | PPI_CHEN_CH31_Msk;
-    NVIC_ClearPendingIRQ(RADIO_IRQn);
-    g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
-}
-
-void
-ble_phy_restart_rx(void)
-{
-    ble_phy_disable_irq_and_ppi();
-    ble_phy_rx();
-}
-
-/**
- * ble phy disable
- *
- * Disables the PHY. This should be called when an event is over. It stops
- * the usec timer (if used), disables interrupts, disables the RADIO, disables
- * PPI and sets state to idle.
- */
-void
-ble_phy_disable(void)
-{
-    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
-    ble_phy_stop_usec_timer();
-    ble_phy_disable_irq_and_ppi();
-}
-
-/* Gets the current access address */
-uint32_t ble_phy_access_addr_get(void)
-{
-    return g_ble_phy_data.phy_access_address;
-}
-
-/**
- * Return the phy state
- *
- * @return int The current PHY state.
- */
-int
-ble_phy_state_get(void)
-{
-    return g_ble_phy_data.phy_state;
-}
-
-/**
- * Called to see if a reception has started
- *
- * @return int
- */
-int
-ble_phy_rx_started(void)
-{
-    return g_ble_phy_data.phy_rx_started;
-}
-
-/**
- * Return the transceiver state
- *
- * @return int transceiver state.
- */
-uint8_t
-ble_phy_xcvr_state_get(void)
-{
-    uint32_t state;
-    state = NRF_RADIO->STATE;
-    return (uint8_t)state;
-}
-
-/**
- * Called to return the maximum data pdu payload length supported by the
- * phy. For this chip, if encryption is enabled, the maximum payload is 27
- * bytes.
- *
- * @return uint8_t Maximum data channel PDU payload size supported
- */
-uint8_t
-ble_phy_max_data_pdu_pyld(void)
-{
-    return BLE_LL_DATA_PDU_MAX_PYLD;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-void
-ble_phy_resolv_list_enable(void)
-{
-    NRF_AAR->NIRK = (uint32_t)g_nrf_num_irks;
-    g_ble_phy_data.phy_privacy = 1;
-}
-
-void
-ble_phy_resolv_list_disable(void)
-{
-    g_ble_phy_data.phy_privacy = 0;
-}
-#endif
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-void ble_phy_enable_dtm(void)
-{
-    /* When DTM is enabled we need to disable whitening as per
-     * Bluetooth v5.0 Vol 6. Part F. 4.1.1
-     */
-    NRF_RADIO->PCNF1 &= ~RADIO_PCNF1_WHITEEN_Msk;
-}
-
-void ble_phy_disable_dtm(void)
-{
-    /* Enable whitening */
-    NRF_RADIO->PCNF1 |= RADIO_PCNF1_WHITEEN_Msk;
-}
-#endif
-#ifdef BLE_XCVR_RFCLK
-void
-ble_phy_rfclk_enable(void)
-{
-    NRF_CLOCK->TASKS_HFCLKSTART = 1;
-}
-
-void
-ble_phy_rfclk_disable(void)
-{
-    NRF_CLOCK->TASKS_HFCLKSTOP = 1;
-}
-#endif
diff --git a/hw/drivers/nimble/nrf52/syscfg.yml b/hw/drivers/nimble/nrf52/syscfg.yml
deleted file mode 100644
index b4088aa10..000000000
--- a/hw/drivers/nimble/nrf52/syscfg.yml
+++ /dev/null
@@ -1,54 +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.
-#
-
-# Package: hw/drivers/nimble/nrf52
-
-syscfg.defs:
-    BLE_PHY_CODED_RX_IFS_EXTRA_MARGIN:
-        description: >
-            This defines additional margin for T_IFS tolerance while in
-            RX on coded phy to allow maintaining connections with some
-            controllers that exceed proper T_IFS (150 usecs) by more
-            than allowed 2 usecs.
-            This value shall be only used for debugging purposes. It is
-            strongly recommended to keep this settings at default value
-            to ensure compliance with specification.
-        value: 0
-    BLE_PHY_DBG_TIME_TXRXEN_READY_PIN:
-        description: >
-            When set to proper GPIO pin number, this pin will be set
-            to high state when radio is enabled using PPI channels
-            20 or 21 and back to low state on radio EVENTS_READY.
-            This can be used to measure radio ram-up time.
-        value: -1
-
-    BLE_PHY_DBG_TIME_ADDRESS_END_PIN:
-        description: >
-            When set to proper GPIO pin number, this pin will be set
-            to high state on radio EVENTS_ADDRESS and back to low state
-            on radio EVENTS_END.
-            This can be used to measure radio pipeline delays.
-        value: -1
-
-    BLE_PHY_DBG_TIME_WFR_PIN:
-        description: >
-            When set to proper GPIO pin number, this pin will be set
-            to high state on radio EVENTS_RXREADY and back to low
-            state when wfr timer expires.
-            This can be used to check if wfr is calculated properly.
-        value: -1
diff --git a/net/nimble/controller/include/controller/ble_hw.h b/net/nimble/controller/include/controller/ble_hw.h
deleted file mode 100644
index 0544ca263..000000000
--- a/net/nimble/controller/include/controller/ble_hw.h
+++ /dev/null
@@ -1,116 +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_HW_
-#define H_BLE_HW_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "syscfg/syscfg.h"
-
-#if defined(ARCH_sim)
-#define BLE_USES_HW_WHITELIST   (0)
-#else
-#define BLE_USES_HW_WHITELIST   MYNEWT_VAL(BLE_HW_WHITELIST_ENABLE)
-#endif
-
-/* Returns the number of hw whitelist elements */
-uint8_t ble_hw_whitelist_size(void);
-
-/* Clear the whitelist */
-void ble_hw_whitelist_clear(void);
-
-/* Remove a device from the hw whitelist */
-void ble_hw_whitelist_rmv(uint8_t *addr, uint8_t addr_type);
-
-/* Add a device to the hw whitelist */
-int ble_hw_whitelist_add(uint8_t *addr, uint8_t addr_type);
-
-/* Enable hw whitelisting */
-void ble_hw_whitelist_enable(void);
-
-/* Enable hw whitelisting */
-void ble_hw_whitelist_disable(void);
-
-/* Boolean function returning true if address matches a whitelist entry */
-int ble_hw_whitelist_match(void);
-
-/* Encrypt data */
-struct ble_encryption_block;
-int ble_hw_encrypt_block(struct ble_encryption_block *ecb);
-
-/* Random number generation */
-typedef void (*ble_rng_isr_cb_t)(uint8_t rnum);
-int ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias);
-
-/**
- * Start the random number generator
- *
- * @return int
- */
-int ble_hw_rng_start(void);
-
-/**
- * Stop the random generator
- *
- * @return int
- */
-int ble_hw_rng_stop(void);
-
-/**
- * Read the random number generator.
- *
- * @return uint8_t
- */
-uint8_t ble_hw_rng_read(void);
-
-/*  Clear the resolving list*/
-void ble_hw_resolv_list_clear(void);
-
-/* Add a device to the hw resolving list */
-int ble_hw_resolv_list_add(uint8_t *irk);
-
-/* Remove a device from the hw resolving list */
-void ble_hw_resolv_list_rmv(int index);
-
-/* Returns the size of the whitelist in HW */
-uint8_t ble_hw_resolv_list_size(void);
-
-/* Enable the resolving list */
-void ble_hw_resolv_list_enable(void);
-
-/* Disables resolving list devices */
-void ble_hw_resolv_list_disable(void);
-
-/* Returns index of resolved address; -1 if not resolved */
-int ble_hw_resolv_list_match(void);
-
-/* Returns public device address or -1 if not present */
-int ble_hw_get_public_addr(ble_addr_t *addr);
-
-/* Returns random static address or -1 if not present */
-int ble_hw_get_static_addr(ble_addr_t *addr);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_HW_ */
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
deleted file mode 100644
index c1fa15262..000000000
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ /dev/null
@@ -1,542 +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_LL_
-#define H_BLE_LL_
-
-#include "stats/stats.h"
-#include "os/os_eventq.h"
-#include "os/os_callout.h"
-#include "os/os_cputime.h"
-#include "nimble/nimble_opt.h"
-#include "controller/ble_phy.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#if MYNEWT_VAL(OS_CPUTIME_FREQ) != 32768
-#error 32.768kHz clock required
-#endif
-
-/*
- * XXX:
- * I guess this should not depend on the 32768 crystal to be honest. This
- * should be done for TIMER0 as well since the rf clock chews up more current.
- * Deal with this later.
- *
- * Another note: BLE_XTAL_SETTLE_TIME should be bsp related (I guess). There
- * should be a note in there that the converted usecs to ticks value of this
- * should not be 0. Thus: if you are using a 32.768 os cputime freq, the min
- * value of settle time should be 31 usecs. I would suspect all settling times
- * would exceed 31 usecs.
- */
-
-/* Determines if we need to turn on/off rf clock */
-#undef BLE_XCVR_RFCLK
-
-/* We will turn on/off rf clock */
-#if MYNEWT_VAL(BLE_XTAL_SETTLE_TIME) != 0
-#define BLE_XCVR_RFCLK
-
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-#define BLE_LL_BT5_PHY_SUPPORTED    (1)
-#else
-#define BLE_LL_BT5_PHY_SUPPORTED    (0)
-#endif
-
-/* Controller revision. */
-#define BLE_LL_SUB_VERS_NR      (0x0000)
-
-/* Timing jitter as per spec is +/16 usecs */
-#define BLE_LL_JITTER_USECS         (16)
-
-/* Packet queue header definition */
-STAILQ_HEAD(ble_ll_pkt_q, os_mbuf_pkthdr);
-
-/*
- * Global Link Layer data object. There is only one Link Layer data object
- * per controller although there may be many instances of the link layer state
- * machine running.
- */
-struct ble_ll_obj
-{
-    /* Supported features */
-    uint32_t ll_supp_features;
-
-    /* Current Link Layer state */
-    uint8_t ll_state;
-
-    /* Number of ACL data packets supported */
-    uint8_t ll_num_acl_pkts;
-
-    /* ACL data packet size */
-    uint16_t ll_acl_pkt_size;
-
-    /* Preferred PHY's */
-    uint8_t ll_pref_tx_phys;
-    uint8_t ll_pref_rx_phys;
-
-#ifdef BLE_XCVR_RFCLK
-    uint8_t ll_rfclk_state;
-    uint16_t ll_xtal_ticks;
-    uint32_t ll_rfclk_start_time;
-    struct hal_timer ll_rfclk_timer;
-#endif
-
-    /* Task event queue */
-    struct os_eventq ll_evq;
-
-    /* Wait for response timer */
-    struct hal_timer ll_wfr_timer;
-
-    /* Packet receive queue (and event). Holds received packets from PHY */
-    struct os_event ll_rx_pkt_ev;
-    struct ble_ll_pkt_q ll_rx_pkt_q;
-
-    /* Packet transmit queue */
-    struct os_event ll_tx_pkt_ev;
-    struct ble_ll_pkt_q ll_tx_pkt_q;
-
-    /* Data buffer overflow event */
-    struct os_event ll_dbuf_overflow_ev;
-
-    /* Number of completed packets event */
-    struct os_event ll_comp_pkt_ev;
-
-    /* HW error callout */
-    struct os_callout ll_hw_err_timer;
-};
-extern struct ble_ll_obj g_ble_ll_data;
-
-/* Link layer statistics */
-STATS_SECT_START(ble_ll_stats)
-    STATS_SECT_ENTRY(hci_cmds)
-    STATS_SECT_ENTRY(hci_cmd_errs)
-    STATS_SECT_ENTRY(hci_events_sent)
-    STATS_SECT_ENTRY(bad_ll_state)
-    STATS_SECT_ENTRY(bad_acl_hdr)
-    STATS_SECT_ENTRY(no_bufs)
-    STATS_SECT_ENTRY(rx_adv_pdu_crc_ok)
-    STATS_SECT_ENTRY(rx_adv_pdu_crc_err)
-    STATS_SECT_ENTRY(rx_adv_bytes_crc_ok)
-    STATS_SECT_ENTRY(rx_adv_bytes_crc_err)
-    STATS_SECT_ENTRY(rx_data_pdu_crc_ok)
-    STATS_SECT_ENTRY(rx_data_pdu_crc_err)
-    STATS_SECT_ENTRY(rx_data_bytes_crc_ok)
-    STATS_SECT_ENTRY(rx_data_bytes_crc_err)
-    STATS_SECT_ENTRY(rx_adv_malformed_pkts)
-    STATS_SECT_ENTRY(rx_adv_ind)
-    STATS_SECT_ENTRY(rx_adv_direct_ind)
-    STATS_SECT_ENTRY(rx_adv_nonconn_ind)
-    STATS_SECT_ENTRY(rx_adv_ext_ind)
-    STATS_SECT_ENTRY(rx_scan_reqs)
-    STATS_SECT_ENTRY(rx_scan_rsps)
-    STATS_SECT_ENTRY(rx_connect_reqs)
-    STATS_SECT_ENTRY(rx_scan_ind)
-    STATS_SECT_ENTRY(rx_aux_connect_rsp)
-    STATS_SECT_ENTRY(adv_txg)
-    STATS_SECT_ENTRY(adv_late_starts)
-    STATS_SECT_ENTRY(adv_resched_pdu_fail)
-    STATS_SECT_ENTRY(adv_drop_event)
-    STATS_SECT_ENTRY(sched_state_conn_errs)
-    STATS_SECT_ENTRY(sched_state_adv_errs)
-    STATS_SECT_ENTRY(scan_starts)
-    STATS_SECT_ENTRY(scan_stops)
-    STATS_SECT_ENTRY(scan_req_txf)
-    STATS_SECT_ENTRY(scan_req_txg)
-    STATS_SECT_ENTRY(scan_rsp_txg)
-    STATS_SECT_ENTRY(aux_missed_adv)
-    STATS_SECT_ENTRY(aux_scheduled)
-    STATS_SECT_ENTRY(aux_received)
-    STATS_SECT_ENTRY(aux_fired_for_read)
-    STATS_SECT_ENTRY(aux_conn_req_tx)
-    STATS_SECT_ENTRY(aux_conn_rsp_tx)
-    STATS_SECT_ENTRY(aux_conn_rsp_err)
-    STATS_SECT_ENTRY(aux_scan_req_tx)
-    STATS_SECT_ENTRY(aux_scan_rsp_err)
-    STATS_SECT_ENTRY(aux_chain_cnt)
-    STATS_SECT_ENTRY(aux_chain_err)
-    STATS_SECT_ENTRY(adv_evt_dropped)
-    STATS_SECT_ENTRY(scan_timer_stopped)
-    STATS_SECT_ENTRY(scan_timer_restarted)
-STATS_SECT_END
-extern STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
-
-/* States */
-#define BLE_LL_STATE_STANDBY        (0)
-#define BLE_LL_STATE_ADV            (1)
-#define BLE_LL_STATE_SCANNING       (2)
-#define BLE_LL_STATE_INITIATING     (3)
-#define BLE_LL_STATE_CONNECTION     (4)
-#define BLE_LL_STATE_DTM            (5)
-
-/* LL Features */
-#define BLE_LL_FEAT_LE_ENCRYPTION    (0x00000001)
-#define BLE_LL_FEAT_CONN_PARM_REQ    (0x00000002)
-#define BLE_LL_FEAT_EXTENDED_REJ     (0x00000004)
-#define BLE_LL_FEAT_SLAVE_INIT       (0x00000008)
-#define BLE_LL_FEAT_LE_PING          (0x00000010)
-#define BLE_LL_FEAT_DATA_LEN_EXT     (0x00000020)
-#define BLE_LL_FEAT_LL_PRIVACY       (0x00000040)
-#define BLE_LL_FEAT_EXT_SCAN_FILT    (0x00000080)
-#define BLE_LL_FEAT_LE_2M_PHY        (0x00000100)
-#define BLE_LL_FEAT_STABLE_MOD_ID_TX (0x00000200)
-#define BLE_LL_FEAT_STABLE_MOD_ID_RX (0x00000400)
-#define BLE_LL_FEAT_LE_CODED_PHY     (0x00000800)
-#define BLE_LL_FEAT_EXT_ADV          (0x00001000)
-#define BLE_LL_FEAT_PERIODIC_ADV     (0x00002000)
-#define BLE_LL_FEAT_CSA2             (0x00004000)
-#define BLE_LL_FEAT_LE_POWER_CLASS_1 (0x00008000)
-#define BLE_LL_FEAT_MIN_USED_CHAN    (0x00010000)
-
-/* LL timing */
-#define BLE_LL_IFS                  (150)       /* usecs */
-#define BLE_LL_MAFS                 (300)       /* usecs */
-
-/*
- * BLE LL device address. Note that element 0 of the array is the LSB and
- * is sent over the air first. Byte 5 is the MSB and is the last one sent over
- * the air.
- */
-#define BLE_DEV_ADDR_LEN            (6)     /* bytes */
-
-struct ble_dev_addr
-{
-    uint8_t u8[BLE_DEV_ADDR_LEN];
-};
-
-#define BLE_IS_DEV_ADDR_STATIC(addr)        ((addr->u8[5] & 0xc0) == 0xc0)
-#define BLE_IS_DEV_ADDR_RESOLVABLE(addr)    ((addr->u8[5] & 0xc0) == 0x40)
-#define BLE_IS_DEV_ADDR_UNRESOLVABLE(addr)  ((addr->u8[5] & 0xc0) == 0x00)
-
-/*
- * LL packet format
- *
- *  -> Preamble         (1/2 bytes)
- *  -> Access Address   (4 bytes)
- *  -> PDU              (2 to 257 octets)
- *  -> CRC              (3 bytes)
- */
-#define BLE_LL_PREAMBLE_LEN     (1)
-#define BLE_LL_ACC_ADDR_LEN     (4)
-#define BLE_LL_CRC_LEN          (3)
-#define BLE_LL_PDU_HDR_LEN      (2)
-#define BLE_LL_MAX_PAYLOAD_LEN  (255)
-#define BLE_LL_MIN_PDU_LEN      (BLE_LL_PDU_HDR_LEN)
-#define BLE_LL_MAX_PDU_LEN      ((BLE_LL_PDU_HDR_LEN) + (BLE_LL_MAX_PAYLOAD_LEN))
-#define BLE_LL_CRCINIT_ADV      (0x555555)
-
-/* Access address for advertising channels */
-#define BLE_ACCESS_ADDR_ADV             (0x8E89BED6)
-
-/*
- * Advertising PDU format:
- * -> 2 byte header
- *      -> LSB contains pdu type, txadd and rxadd bits.
- *      -> MSB contains length (6 bits). Length is length of payload. Does
- *         not include the header length itself.
- * -> Payload (max 37 bytes)
- */
-#define BLE_ADV_PDU_HDR_TYPE_MASK           (0x0F)
-#define BLE_ADV_PDU_HDR_CHSEL_MASK          (0x20)
-#define BLE_ADV_PDU_HDR_TXADD_MASK          (0x40)
-#define BLE_ADV_PDU_HDR_RXADD_MASK          (0x80)
-
-/* Advertising channel PDU types */
-#define BLE_ADV_PDU_TYPE_ADV_IND            (0)
-#define BLE_ADV_PDU_TYPE_ADV_DIRECT_IND     (1)
-#define BLE_ADV_PDU_TYPE_ADV_NONCONN_IND    (2)
-#define BLE_ADV_PDU_TYPE_SCAN_REQ           (3)
-#define BLE_ADV_PDU_TYPE_SCAN_RSP           (4)
-#define BLE_ADV_PDU_TYPE_CONNECT_REQ        (5)
-#define BLE_ADV_PDU_TYPE_ADV_SCAN_IND       (6)
-#define BLE_ADV_PDU_TYPE_ADV_EXT_IND        (7)
-#define BLE_ADV_PDU_TYPE_AUX_ADV_IND        BLE_ADV_PDU_TYPE_ADV_EXT_IND
-#define BLE_ADV_PDU_TYPE_AUX_SCAN_RSP       BLE_ADV_PDU_TYPE_ADV_EXT_IND
-#define BLE_ADV_PDU_TYPE_AUX_SYNC_IND       BLE_ADV_PDU_TYPE_ADV_EXT_IND
-#define BLE_ADV_PDU_TYPE_AUX_CHAIN_IND      BLE_ADV_PDU_TYPE_ADV_EXT_IND
-#define BLE_ADV_PDU_TYPE_AUX_CONNECT_REQ    BLE_ADV_PDU_TYPE_CONNECT_REQ
-#define BLE_ADV_PDU_TYPE_AUX_SCAN_REQ       BLE_ADV_PDU_TYPE_SCAN_REQ
-#define BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP    (8)
-
-/* Extended Header Length (6b) + AdvMode (2b) */
-#define BLE_LL_EXT_ADV_HDR_LEN          (1)
-
-#define BLE_LL_EXT_ADV_ADVA_BIT         (0)
-#define BLE_LL_EXT_ADV_TARGETA_BIT      (1)
-#define BLE_LL_EXT_ADV_RFU_BIT          (2)
-#define BLE_LL_EXT_ADV_DATA_INFO_BIT    (3)
-#define BLE_LL_EXT_ADV_AUX_PTR_BIT      (4)
-#define BLE_LL_EXT_ADV_SYNC_INFO_BIT    (5)
-#define BLE_LL_EXT_ADV_TX_POWER_BIT     (6)
-
-#define BLE_LL_EXT_ADV_FLAGS_SIZE       (1)
-#define BLE_LL_EXT_ADV_ADVA_SIZE        (6)
-#define BLE_LL_EXT_ADV_TARGETA_SIZE     (6)
-#define BLE_LL_EXT_ADV_DATA_INFO_SIZE   (2)
-#define BLE_LL_EXT_ADV_AUX_PTR_SIZE     (3)
-#define BLE_LL_EXT_ADV_SYNC_INFO_SIZE   (18)
-#define BLE_LL_EXT_ADV_TX_POWER_SIZE    (1)
-
-#define BLE_LL_EXT_ADV_MODE_NON_CONN    (0x00)
-#define BLE_LL_EXT_ADV_MODE_CONN        (0x01)
-#define BLE_LL_EXT_ADV_MODE_SCAN        (0x02)
-
-/* If Channel Selection Algorithm #2 is supported */
-#define BLE_ADV_PDU_HDR_CHSEL               (0x20)
-
-/*
- * TxAdd and RxAdd bit definitions. A 0 is a public address; a 1 is a
- * random address.
- */
-#define BLE_ADV_PDU_HDR_TXADD_RAND          (0x40)
-#define BLE_ADV_PDU_HDR_RXADD_RAND          (0x80)
-
-/*
- * Data Channel format
- *
- *  -> Header (2 bytes)
- *      -> LSB contains llid, nesn, sn and md
- *      -> MSB contains length (8 bits)
- *  -> Payload (0 to 251)
- *  -> MIC (0 or 4 bytes)
- */
-#define BLE_LL_DATA_HDR_LLID_MASK       (0x03)
-#define BLE_LL_DATA_HDR_NESN_MASK       (0x04)
-#define BLE_LL_DATA_HDR_SN_MASK         (0x08)
-#define BLE_LL_DATA_HDR_MD_MASK         (0x10)
-#define BLE_LL_DATA_HDR_RSRVD_MASK      (0xE0)
-#define BLE_LL_DATA_PDU_MAX_PYLD        (251)
-#define BLE_LL_DATA_MIC_LEN             (4)
-
-/* LLID definitions */
-#define BLE_LL_LLID_RSRVD               (0)
-#define BLE_LL_LLID_DATA_FRAG           (1)
-#define BLE_LL_LLID_DATA_START          (2)
-#define BLE_LL_LLID_CTRL                (3)
-
-/*
- * CONNECT_REQ
- *      -> InitA        (6 bytes)
- *      -> AdvA         (6 bytes)
- *      -> LLData       (22 bytes)
- *          -> Access address (4 bytes)
- *          -> CRC init (3 bytes)
- *          -> WinSize (1 byte)
- *          -> WinOffset (2 bytes)
- *          -> Interval (2 bytes)
- *          -> Latency (2 bytes)
- *          -> Timeout (2 bytes)
- *          -> Channel Map (5 bytes)
- *          -> Hop Increment (5 bits)
- *          -> SCA (3 bits)
- *
- *  InitA is the initiators public (TxAdd=0) or random (TxAdd=1) address.
- *  AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address.
- *  LLData contains connection request data.
- *      aa: Link Layer's access address
- *      crc_init: The CRC initialization value used for CRC calculation.
- *      winsize: The transmit window size = winsize * 1.25 msecs
- *      winoffset: The transmit window offset =  winoffset * 1.25 msecs
- *      interval: The connection interval = interval * 1.25 msecs.
- *      latency: connection slave latency = latency
- *      timeout: Connection supervision timeout = timeout * 10 msecs.
- *      chanmap: contains channel mapping indicating used and unused data
- *               channels. Only bits that are 1 are usable. LSB is channel 0.
- *      hop_inc: Hop increment used for frequency hopping. Random value in
- *               range of 5 to 16.
- */
-#define BLE_CONNECT_REQ_LEN         (34)
-#define BLE_CONNECT_REQ_PDU_LEN     (BLE_CONNECT_REQ_LEN + BLE_LL_PDU_HDR_LEN)
-
-#define BLE_SCAN_REQ_LEN            (12)
-#define BLE_SCAN_RSP_MAX_LEN        (37)
-#define BLE_SCAN_RSP_MAX_EXT_LEN    (251)
-
-/*--- External API ---*/
-/* Initialize the Link Layer */
-void ble_ll_init(void);
-
-/* Reset the Link Layer */
-int ble_ll_reset(void);
-
-/* 'Boolean' function returning true if address is a valid random address */
-int ble_ll_is_valid_random_addr(uint8_t *addr);
-
-/* Calculate the amount of time in microseconds a PDU with payload length of
- * 'payload_len' will take to transmit on a PHY 'phy_mode'. */
-uint32_t ble_ll_pdu_tx_time_get(uint16_t payload_len, int phy_mode);
-
-/* Calculate maximum octets of PDU payload which can be transmitted during
- * 'usecs' on a PHY 'phy_mode'. */
-uint16_t ble_ll_pdu_max_tx_octets_get(uint32_t usecs, int phy_mode);
-
-/* Is this address a resolvable private address? */
-int ble_ll_is_rpa(uint8_t *addr, uint8_t addr_type);
-
-/* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */
-int ble_ll_is_our_devaddr(uint8_t *addr, int addr_type);
-
-/**
- * Called to put a packet on the Link Layer transmit packet queue.
- *
- * @param txpdu Pointer to transmit packet
- */
-void ble_ll_acl_data_in(struct os_mbuf *txpkt);
-
-/**
- * Allocate a pdu (chain) for reception.
- *
- * @param len Length of PDU. This includes the PDU header as well as payload.
- * Does not include MIC if encrypted.
- *
- * @return struct os_mbuf* Pointer to mbuf chain to hold received packet
- */
-struct os_mbuf *ble_ll_rxpdu_alloc(uint16_t len);
-
-/* Tell the Link Layer there has been a data buffer overflow */
-void ble_ll_data_buffer_overflow(void);
-
-/* Tell the link layer there has been a hardware error */
-void ble_ll_hw_error(void);
-
-/*--- PHY interfaces ---*/
-struct ble_mbuf_hdr;
-
-/* Called by the PHY when a packet has started */
-int ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan, struct ble_mbuf_hdr *hdr);
-
-/* Called by the PHY when a packet reception ends */
-int ble_ll_rx_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr);
-
-/* Helper callback to tx mbuf using ble_phy_tx() */
-uint8_t ble_ll_tx_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte);
-
-/*--- Controller API ---*/
-void ble_ll_mbuf_init(struct os_mbuf *m, uint8_t pdulen, uint8_t hdr);
-
-/* Set the link layer state */
-void ble_ll_state_set(uint8_t ll_state);
-
-/* Get the link layer state */
-uint8_t ble_ll_state_get(void);
-
-/* Send an event to LL task */
-void ble_ll_event_send(struct os_event *ev);
-
-/* Hand received pdu's to LL task  */
-void ble_ll_rx_pdu_in(struct os_mbuf *rxpdu);
-
-/* Set random address */
-int ble_ll_set_random_addr(uint8_t *addr);
-
-/* Enable wait for response timer */
-void ble_ll_wfr_enable(uint32_t cputime);
-
-/* Disable wait for response timer */
-void ble_ll_wfr_disable(void);
-
-/* Wait for response timer expiration callback */
-void ble_ll_wfr_timer_exp(void *arg);
-
-/* Read set of features supported by the Link Layer */
-uint32_t ble_ll_read_supp_features(void);
-
-/* Read set of states supported by the Link Layer */
-uint64_t ble_ll_read_supp_states(void);
-
-/* Check if octets and time are valid. Returns 0 if not valid */
-int ble_ll_chk_txrx_octets(uint16_t octets);
-int ble_ll_chk_txrx_time(uint16_t time);
-
-/* Random numbers */
-int ble_ll_rand_init(void);
-void ble_ll_rand_sample(uint8_t rnum);
-int ble_ll_rand_data_get(uint8_t *buf, uint8_t len);
-void ble_ll_rand_prand_get(uint8_t *prand);
-int ble_ll_rand_start(void);
-
-static inline int
-ble_ll_get_addr_type(uint8_t txrxflag)
-{
-    if (txrxflag) {
-        return BLE_HCI_ADV_OWN_ADDR_RANDOM;
-    }
-    return BLE_HCI_ADV_OWN_ADDR_PUBLIC;
-}
-
-/* Convert usecs to ticks and round up to nearest tick */
-static inline uint32_t
-ble_ll_usecs_to_ticks_round_up(uint32_t usecs)
-{
-    return os_cputime_usecs_to_ticks(usecs + 30);
-}
-
-#include "console/console.h"
-
-#define BLE_LL_LOG_ID_PHY_SETCHAN       (1)
-#define BLE_LL_LOG_ID_RX_START          (2)
-#define BLE_LL_LOG_ID_RX_END            (3)
-#define BLE_LL_LOG_ID_WFR_EXP           (4)
-#define BLE_LL_LOG_ID_PHY_TXEND         (5)
-#define BLE_LL_LOG_ID_PHY_TX            (6)
-#define BLE_LL_LOG_ID_PHY_RX            (7)
-#define BLE_LL_LOG_ID_PHY_DISABLE       (9)
-#define BLE_LL_LOG_ID_CONN_EV_START     (10)
-#define BLE_LL_LOG_ID_CONN_TX           (15)
-#define BLE_LL_LOG_ID_CONN_RX           (16)
-#define BLE_LL_LOG_ID_CONN_TX_RETRY     (17)
-#define BLE_LL_LOG_ID_CONN_RX_ACK       (18)
-#define BLE_LL_LOG_ID_LL_CTRL_RX        (19)
-#define BLE_LL_LOG_ID_CONN_EV_END       (20)
-#define BLE_LL_LOG_ID_CONN_END          (30)
-#define BLE_LL_LOG_ID_ADV_TXBEG         (50)
-#define BLE_LL_LOG_ID_ADV_TXDONE        (60)
-#define BLE_LL_LOG_ID_SCHED             (80)
-#define BLE_LL_LOG_ID_RFCLK_START       (90)
-#define BLE_LL_LOG_ID_RFCLK_ENABLE      (91)
-#define BLE_LL_LOG_ID_RFCLK_STOP        (95)
-#define BLE_LL_LOG_ID_RFCLK_SCHED_DIS   (96)
-#define BLE_LL_LOG_ID_RFCLK_SCAN_DIS    (97)
-
-#ifdef BLE_LL_LOG
-void ble_ll_log(uint8_t id, uint8_t arg8, uint16_t arg16, uint32_t arg32);
-#else
-#define ble_ll_log(m,n,o,p)
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/* LTK 0x4C68384139F574D836BCF34E9DFB01BF */
-extern const uint8_t g_bletest_LTK[];
-extern uint16_t g_bletest_EDIV;
-extern uint64_t g_bletest_RAND;
-extern uint64_t g_bletest_SKDm;
-extern uint64_t g_bletest_SKDs;
-extern uint32_t g_bletest_IVm;
-extern uint32_t g_bletest_IVs;
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_LL_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_adv.h b/net/nimble/controller/include/controller/ble_ll_adv.h
deleted file mode 100644
index c2ce24d38..000000000
--- a/net/nimble/controller/include/controller/ble_ll_adv.h
+++ /dev/null
@@ -1,192 +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_LL_ADV_
-#define H_BLE_LL_ADV_
-
-#include "syscfg/syscfg.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * ADV event timing
- *      T_advEvent = advInterval + advDelay
- *
- *      advInterval: increments of 625 usecs
- *      advDelay: RAND[0, 10] msecs
- *
- */
-#define BLE_LL_ADV_ITVL                 (625)           /* usecs */
-#define BLE_LL_ADV_ITVL_MIN             (32)            /* units */
-#define BLE_LL_ADV_ITVL_MAX             (16384)         /* units */
-#define BLE_LL_ADV_ITVL_MS_MIN          (20)            /* msecs */
-#define BLE_LL_ADV_ITVL_MS_MAX          (10240)         /* msecs */
-#define BLE_LL_ADV_ITVL_SCAN_MIN        (160)           /* units */
-#define BLE_LL_ADV_ITVL_SCAN_MS_MIN     (100)           /* msecs */
-#define BLE_LL_ADV_ITVL_NONCONN_MS_MIN  (100)           /* msecs */
-#define BLE_LL_ADV_DELAY_MS_MIN         (0)             /* msecs */
-#define BLE_LL_ADV_DELAY_MS_MAX         (10)            /* msecs */
-#define BLE_LL_ADV_PDU_ITVL_LD_MS_MAX   (10)            /* msecs */
-#define BLE_LL_ADV_PDU_ITVL_HD_MS_MAX   (3750)          /* usecs */
-#define BLE_LL_ADV_STATE_HD_MAX         (1280)          /* msecs */
-
-/* Maximum advertisement data length */
-#define BLE_ADV_LEGACY_DATA_MAX_LEN     (31)
-#define BLE_ADV_LEGACY_MAX_PKT_LEN      (37)
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-#define BLE_ADV_DATA_MAX_LEN            MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE)
-#else
-#define BLE_ADV_DATA_MAX_LEN            BLE_ADV_LEGACY_DATA_MAX_LEN
-#endif
-
-/*
- * ADV_IND
- *      -> AdvA     (6 bytes)
- *      -> AdvData  (0 - 31 bytes)
- *
- *  The advertising address (AdvA) is a public address (TxAdd=0) or random
- *  address (TxAdd = 1)
- */
-#define BLE_ADV_IND_MIN_LEN             (6)
-#define BLE_ADV_IND_MAX_LEN             (37)
-
-/*
- * ADV_DIRECT_IND
- *      -> AdvA     (6 bytes)
- *      -> InitA    (6 bytes)
- *
- *  AdvA is the advertisers public address (TxAdd=0) or random address
- *  (TxAdd = 1).
- *
- *  InitA is the initiators public or random address. This is the address
- *  to which this packet is addressed.
- *
- */
-#define BLE_ADV_DIRECT_IND_LEN          (12)
-
-/*
- * ADV_NONCONN_IND
- *      -> AdvA     (6 bytes)
- *      -> AdvData  (0 - 31 bytes)
- *
- *  The advertising address (AdvA) is a public address (TxAdd=0) or random
- *  address (TxAdd = 1)
- *
- */
-#define BLE_ADV_NONCONN_IND_MIN_LEN     (6)
-#define BLE_ADV_NONCONN_IND_MAX_LEN     (37)
-
-/*
- * ADV_SCAN_IND
- *      -> AdvA     (6 bytes)
- *      -> AdvData  (0 - 31 bytes)
- *
- *  The advertising address (AdvA) is a public address (TxAdd=0) or random
- *  address (TxAdd = 1)
- *
- */
-#define BLE_ADV_SCAN_IND_MIN_LEN        (6)
-#define BLE_ADV_SCAN_IND_MAX_LEN        (37)
-
-/*---- HCI ----*/
-struct ble_ll_adv_sm;
-struct ble_ll_conn_sm;
-
-/* Start an advertiser */
-int ble_ll_adv_start_req(uint8_t adv_chanmask, uint8_t adv_type,
-                         uint8_t *init_addr, uint16_t adv_itvl, void *handle);
-
-/* Start or stop advertising */
-int ble_ll_adv_set_enable(uint8_t instance, uint8_t enable, int duration,
-                          uint8_t event);
-
-/* Set advertising data */
-int ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t instance, uint8_t operation);
-
-/* Set scan response data */
-int ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t instance,
-                                 uint8_t operation);
-
-/* Set advertising parameters */
-int ble_ll_adv_set_adv_params(uint8_t *cmd);
-
-/* Read advertising channel power */
-int ble_ll_adv_read_txpwr(uint8_t *rspbuf, uint8_t *rsplen);
-
-/*---- API used by BLE LL ----*/
-/* Send the connection complete event */
-void ble_ll_adv_send_conn_comp_ev(struct ble_ll_conn_sm *connsm,
-                                  struct ble_mbuf_hdr *rxhdr);
-
-/* Returns local resolvable private address */
-uint8_t *ble_ll_adv_get_local_rpa(struct ble_ll_adv_sm *advsm);
-
-/* Returns peer resolvable private address */
-uint8_t *ble_ll_adv_get_peer_rpa(struct ble_ll_adv_sm *advsm);
-
-/* Called to initialize advertising functionality. */
-void ble_ll_adv_init(void);
-
-/* Called when LL wait for response timer expires in advertising state */
-void ble_ll_adv_wfr_timer_exp(void);
-
-/* Called to reset the advertiser. */
-void ble_ll_adv_reset(void);
-
-/* Called on rx pdu start when in advertising state */
-int ble_ll_adv_rx_isr_start(uint8_t pdu_type);
-
-/* Called on rx pdu end when in advertising state */
-int ble_ll_adv_rx_isr_end(uint8_t pdu_type, struct os_mbuf *rxpdu, int crcok);
-
-/* Processes received packets at the link layer task */
-void ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf,
-                          struct ble_mbuf_hdr *hdr);
-
-/* Boolean function denoting whether or not the whitelist can be changed */
-int ble_ll_adv_can_chg_whitelist(void);
-
-/*
- * Called when an advertising event has been removed from the scheduler
- * without being run.
- */
-void ble_ll_adv_event_rmvd_from_sched(struct ble_ll_adv_sm *advsm);
-
-/* Called to halt currently running advertising event */
-void ble_ll_adv_halt(void);
-
-/* Called to determine if advertising is enabled */
-uint8_t ble_ll_adv_enabled(void);
-
-int ble_ll_adv_set_random_addr(uint8_t *addr, uint8_t instance);
-int ble_ll_adv_remove(uint8_t instance);
-int ble_ll_adv_clear_all(void);
-int ble_ll_adv_ext_set_param(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen);
-int ble_ll_adv_ext_set_adv_data(uint8_t *cmdbuf, uint8_t cmdlen);
-int ble_ll_adv_ext_set_scan_rsp(uint8_t *cmdbuf, uint8_t cmdlen);
-int ble_ll_adv_ext_set_enable(uint8_t *cmdbuf, uint8_t len);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_ADV_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_conn.h b/net/nimble/controller/include/controller/ble_ll_conn.h
deleted file mode 100644
index 149861e7a..000000000
--- a/net/nimble/controller/include/controller/ble_ll_conn.h
+++ /dev/null
@@ -1,366 +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_LL_CONN_
-#define H_BLE_LL_CONN_
-
-#include "os/os.h"
-#include "nimble/ble.h"
-#include "nimble/hci_common.h"
-#include "controller/ble_ll_sched.h"
-#include "controller/ble_ll_ctrl.h"
-#include "controller/ble_phy.h"
-#include "hal/hal_timer.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Roles */
-#define BLE_LL_CONN_ROLE_NONE           (0)
-#define BLE_LL_CONN_ROLE_MASTER         (1)
-#define BLE_LL_CONN_ROLE_SLAVE          (2)
-
-/* Connection states */
-#define BLE_LL_CONN_STATE_IDLE          (0)
-#define BLE_LL_CONN_STATE_CREATED       (1)
-#define BLE_LL_CONN_STATE_ESTABLISHED   (2)
-
-/* Channel map size */
-#define BLE_LL_CONN_CHMAP_LEN           (5)
-
-/* Definitions for source clock accuracy */
-#define BLE_MASTER_SCA_251_500_PPM      (0)
-#define BLE_MASTER_SCA_151_250_PPM      (1)
-#define BLE_MASTER_SCA_101_150_PPM      (2)
-#define BLE_MASTER_SCA_76_100_PPM       (3)
-#define BLE_MASTER_SCA_51_75_PPM        (4)
-#define BLE_MASTER_SCA_31_50_PPM        (5)
-#define BLE_MASTER_SCA_21_30_PPM        (6)
-#define BLE_MASTER_SCA_0_20_PPM         (7)
-
-/* Definition for RSSI when the RSSI is unknown */
-#define BLE_LL_CONN_UNKNOWN_RSSI        (127)
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/*
- * Encryption states for a connection
- *
- * NOTE: the states are ordered so that we can check to see if the state
- * is greater than ENCRYPTED. If so, it means that the start or pause
- * encryption procedure is running and we should not send data pdu's.
- */
-enum conn_enc_state {
-    CONN_ENC_S_UNENCRYPTED = 1,
-    CONN_ENC_S_ENCRYPTED,
-    CONN_ENC_S_ENC_RSP_WAIT,
-    CONN_ENC_S_START_ENC_REQ_WAIT,
-    CONN_ENC_S_START_ENC_RSP_WAIT,
-    CONN_ENC_S_PAUSE_ENC_RSP_WAIT,
-    CONN_ENC_S_LTK_REQ_WAIT,
-    CONN_ENC_S_LTK_NEG_REPLY
-};
-
-/*
- * Note that the LTK is the key, the SDK is the plain text, and the
- * session key is the cipher text portion of the encryption block.
- */
-struct ble_ll_conn_enc_data
-{
-    uint8_t enc_state;
-    uint8_t tx_encrypted;
-    uint16_t enc_div;
-    uint16_t tx_pkt_cntr;
-    uint16_t rx_pkt_cntr;
-    uint64_t host_rand_num;
-    uint8_t iv[8];
-    struct ble_encryption_block enc_block;
-};
-#endif
-
-/* Connection state machine flags. */
-union ble_ll_conn_sm_flags {
-    struct {
-        uint32_t pkt_rxd:1;
-        uint32_t terminate_ind_txd:1;
-        uint32_t terminate_ind_rxd:1;
-        uint32_t allow_slave_latency:1;
-        uint32_t slave_set_last_anchor:1;
-        uint32_t awaiting_host_reply:1;
-        uint32_t terminate_started:1;
-        uint32_t conn_update_sched:1;
-        uint32_t host_expects_upd_event:1;
-        uint32_t version_ind_sent:1;
-        uint32_t rxd_version_ind:1;
-        uint32_t chanmap_update_scheduled:1;
-        uint32_t conn_empty_pdu_txd:1;
-        uint32_t last_txd_md:1;
-        uint32_t conn_req_txd:1;
-        uint32_t send_ltk_req:1;
-        uint32_t encrypted:1;
-        uint32_t encrypt_chg_sent:1;
-        uint32_t le_ping_supp:1;
-        uint32_t csa2_supp:1;
-        uint32_t host_phy_update: 1;
-        uint32_t phy_update_sched: 1;
-        uint32_t ctrlr_phy_update: 1;
-        uint32_t phy_update_event: 1;
-        uint32_t peer_phy_update: 1; /* XXX:combine with ctrlr udpate bit? */
-        uint32_t aux_conn_req: 1;
-        uint32_t rxd_features:1;
-        uint32_t pending_hci_rd_features:1;
-    } cfbit;
-    uint32_t conn_flags;
-} __attribute__((packed));
-
-/**
- * Structure used for PHY data inside a connection.
- *
- * NOTE: the new phy's are the phys we will change to when a phy update
- * procedure is ongoing and the event counter hits the instant.
- *
- * tx_phy_mode: chip specific phy mode for tx
- * rx_phy_mode: chip specific phy mode for rx
- * cur_tx_phy: value denoting current tx_phy (not a bitmask!)
- * cur_rx_phy: value denoting current rx phy (not a bitmask!)
- * new_tx_phy: value denoting new tx_phy (not a bitmask!)
- * new_rx_phy: value denoting new rx phy (not a bitmask!)
- * req_pref_tx_phy: tx phy sent in a phy request (may be different than host)
- * req_pref_rx_phy: rx phy sent in a phy request (may be different than host)
- * host_pref_tx_phys: bitmask of preferred transmit PHYs sent by host
- * host_pref_rx_phys: bitmask of preferred receive PHYs sent by host
- * phy_options: preferred phy options for coded phy
- */
-struct ble_ll_conn_phy_data
-{
-    uint32_t tx_phy_mode: 2;
-    uint32_t rx_phy_mode: 2;
-    uint32_t cur_tx_phy: 2;
-    uint32_t cur_rx_phy: 2;
-    uint32_t new_tx_phy: 2;
-    uint32_t new_rx_phy: 2;
-    uint32_t host_pref_tx_phys_mask: 3;
-    uint32_t host_pref_rx_phys_mask: 3;
-    uint32_t req_pref_tx_phys_mask: 3;
-    uint32_t req_pref_rx_phys_mask: 3;
-    uint32_t phy_options: 2;
-}  __attribute__((packed));
-
-#define CONN_CUR_TX_PHY_MASK(csm)   (1 << ((csm)->phy_data.cur_tx_phy - 1))
-#define CONN_CUR_RX_PHY_MASK(csm)   (1 << ((csm)->phy_data.cur_rx_phy - 1))
-
-/* Connection state machine */
-struct ble_ll_conn_sm
-{
-    /* Connection state machine flags */
-    union ble_ll_conn_sm_flags csmflags;
-
-    /* Current connection handle, state and role */
-    uint16_t conn_handle;
-    uint8_t conn_state;
-    uint8_t conn_role;          /* Can possibly be 1 bit */
-
-    /* RSSI */
-    int8_t conn_rssi;
-
-    /* For privacy */
-    int8_t rpa_index;
-
-    /* Connection data length management */
-    uint8_t max_tx_octets;
-    uint8_t max_rx_octets;
-    uint8_t rem_max_tx_octets;
-    uint8_t rem_max_rx_octets;
-    uint8_t eff_max_tx_octets;
-    uint8_t eff_max_rx_octets;
-    uint16_t max_tx_time;
-    uint16_t max_rx_time;
-    uint16_t rem_max_tx_time;
-    uint16_t rem_max_rx_time;
-    uint16_t eff_max_tx_time;
-    uint16_t eff_max_rx_time;
-    uint8_t max_tx_octets_phy_mode[BLE_PHY_NUM_MODE];
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    struct ble_ll_conn_phy_data phy_data;
-    uint16_t phy_instant;
-#endif
-
-    /* Used to calculate data channel index for connection */
-    uint8_t chanmap[BLE_LL_CONN_CHMAP_LEN];
-    uint8_t req_chanmap[BLE_LL_CONN_CHMAP_LEN];
-    uint16_t chanmap_instant;
-    uint16_t channel_id; /* TODO could be union with hop and last chan used */
-    uint8_t hop_inc;
-    uint8_t data_chan_index;
-    uint8_t last_unmapped_chan;
-    uint8_t num_used_chans;
-
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-    uint8_t period_occ_mask;    /* mask: period 0 = 0x01, period 3 = 0x08 */
-#endif
-
-    /* Ack/Flow Control */
-    uint8_t tx_seqnum;          /* note: can be 1 bit */
-    uint8_t next_exp_seqnum;    /* note: can be 1 bit */
-    uint8_t cons_rxd_bad_crc;   /* note: can be 1 bit */
-    uint8_t last_rxd_sn;        /* note: cant be 1 bit given current code */
-    uint8_t last_rxd_hdr_byte;  /* note: possibly can make 1 bit since we
-                                   only use the MD bit now */
-
-    /* connection event mgmt */
-    uint8_t reject_reason;
-    uint8_t host_reply_opcode;
-    uint8_t master_sca;
-    uint8_t tx_win_size;
-    uint8_t cur_ctrl_proc;
-    uint8_t disconnect_reason;
-    uint8_t rxd_disconnect_reason;
-    uint8_t vers_nr;
-    uint8_t conn_features;
-    uint8_t remote_features[7];
-    uint16_t pending_ctrl_procs;
-    uint16_t event_cntr;
-    uint16_t completed_pkts;
-    uint16_t comp_id;
-    uint16_t sub_vers_nr;
-    uint16_t auth_pyld_tmo;         /* could be ifdef'd. 10 msec units */
-
-    uint32_t access_addr;
-    uint32_t crcinit;               /* only low 24 bits used */
-    /* XXX: do we need ce_end_time? Cant this be sched end time? */
-    uint32_t ce_end_time;   /* cputime at which connection event should end */
-    uint32_t terminate_timeout;
-    uint32_t last_scheduled;
-
-    /* Connection timing */
-    uint16_t conn_itvl;
-    uint16_t slave_latency;
-    uint16_t supervision_tmo;
-    uint16_t min_ce_len;
-    uint16_t max_ce_len;
-    uint16_t tx_win_off;
-    uint32_t anchor_point;
-    uint8_t anchor_point_usecs;     /* XXX: can this be uint8_t ?*/
-    uint8_t conn_itvl_usecs;
-    uint32_t conn_itvl_ticks;
-    uint32_t last_anchor_point;     /* Slave only */
-    uint32_t slave_cur_tx_win_usecs;
-    uint32_t slave_cur_window_widening;
-    uint32_t last_rxd_pdu_cputime;  /* Used exclusively for supervision timer */
-
-    /* address information */
-    uint8_t own_addr_type;
-    uint8_t peer_addr_type;
-    uint8_t peer_addr[BLE_DEV_ADDR_LEN];
-
-    /*
-     * XXX: TODO. Could save memory. Have single event at LL and put these
-     * on a singly linked list. Only would need list pointer here.
-     */
-    /* Connection end event */
-    struct os_event conn_ev_end;
-
-    /* Packet transmit queue */
-    struct os_mbuf *cur_tx_pdu;
-    STAILQ_HEAD(conn_txq_head, os_mbuf_pkthdr) conn_txq;
-
-    /* List entry for active/free connection pools */
-    union {
-        SLIST_ENTRY(ble_ll_conn_sm) act_sle;
-        STAILQ_ENTRY(ble_ll_conn_sm) free_stqe;
-    };
-
-    /* LL control procedure response timer */
-    struct os_callout ctrl_proc_rsp_timer;
-
-    /* For scheduling connections */
-    struct ble_ll_sched_item conn_sch;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-    struct os_callout auth_pyld_timer;
-#endif
-
-    /*
-     * XXX: a note on all these structures for control procedures. First off,
-     * all of these need to be ifdef'd to save memory. Another thing to
-     * consider is this: since most control procedures can only run when no
-     * others are running, can I use just one structure (a union)? Should I
-     * allocate these from a pool? Not sure what to do. For now, I just use
-     * a large chunk of memory per connection.
-     */
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    struct ble_ll_conn_enc_data enc_data;
-#endif
-    /*
-     * For connection update procedure. XXX: can make this a pointer and
-     * malloc it if we want to save space.
-     */
-    struct hci_conn_update conn_param_req;
-
-    /* For connection update procedure */
-    struct ble_ll_conn_upd_req conn_update_req;
-
-    /* XXX: for now, just store them all */
-    struct ble_ll_conn_params conn_cp;
-
-    struct ble_ll_scan_sm *scansm;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    struct hci_ext_create_conn initial_params;
-#endif
-
-};
-
-/* Flags */
-#define CONN_F_UPDATE_SCHED(csm)    ((csm)->csmflags.cfbit.conn_update_sched)
-#define CONN_F_EMPTY_PDU_TXD(csm)   ((csm)->csmflags.cfbit.conn_empty_pdu_txd)
-#define CONN_F_LAST_TXD_MD(csm)     ((csm)->csmflags.cfbit.last_txd_md)
-#define CONN_F_CONN_REQ_TXD(csm)    ((csm)->csmflags.cfbit.conn_req_txd)
-#define CONN_F_ENCRYPTED(csm)       ((csm)->csmflags.cfbit.encrypted)
-#define CONN_F_ENC_CHANGE_SENT(csm) ((csm)->csmflags.cfbit.encrypt_chg_sent)
-#define CONN_F_LE_PING_SUPP(csm)    ((csm)->csmflags.cfbit.le_ping_supp)
-#define CONN_F_TERMINATE_STARTED(csm) ((csm)->csmflags.cfbit.terminate_started)
-#define CONN_F_CSA2_SUPP(csm)       ((csm)->csmflags.cfbit.csa2_supp)
-#define CONN_F_TERMINATE_STARTED(csm) ((csm)->csmflags.cfbit.terminate_started)
-#define CONN_F_HOST_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.host_phy_update)
-#define CONN_F_PHY_UPDATE_SCHED(csm) ((csm)->csmflags.cfbit.phy_update_sched)
-#define CONN_F_CTRLR_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.ctrlr_phy_update)
-#define CONN_F_PHY_UPDATE_EVENT(csm) ((csm)->csmflags.cfbit.phy_update_event)
-#define CONN_F_PEER_PHY_UPDATE(csm)  ((csm)->csmflags.cfbit.peer_phy_update)
-#define CONN_F_AUX_CONN_REQ(csm)  ((csm)->csmflags.cfbit.aux_conn_req)
-
-/* Role */
-#define CONN_IS_MASTER(csm)         (csm->conn_role == BLE_LL_CONN_ROLE_MASTER)
-#define CONN_IS_SLAVE(csm)          (csm->conn_role == BLE_LL_CONN_ROLE_SLAVE)
-
-/*
- * Given a handle, returns an active connection state machine (or NULL if the
- * handle does not exist
- *
- */
-struct ble_ll_conn_sm *ble_ll_conn_find_active_conn(uint16_t handle);
-
-/* required for unit testing */
-uint8_t ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_CONN_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_ctrl.h b/net/nimble/controller/include/controller/ble_ll_ctrl.h
deleted file mode 100644
index 3a9a8c7b7..000000000
--- a/net/nimble/controller/include/controller/ble_ll_ctrl.h
+++ /dev/null
@@ -1,285 +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_LL_CTRL_
-#define H_BLE_LL_CTRL_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * LL control procedures. This "enumeration" is not in the specification;
- * It is used to determine which LL control procedure is currently running
- * in a connection and which ones may be pending.
- */
-#define BLE_LL_CTRL_PROC_CONN_UPDATE    (0)
-#define BLE_LL_CTRL_PROC_CHAN_MAP_UPD   (1)
-#define BLE_LL_CTRL_PROC_ENCRYPT        (2)
-#define BLE_LL_CTRL_PROC_FEATURE_XCHG   (3)
-#define BLE_LL_CTRL_PROC_VERSION_XCHG   (4)
-#define BLE_LL_CTRL_PROC_TERMINATE      (5)
-#define BLE_LL_CTRL_PROC_CONN_PARAM_REQ (6)
-#define BLE_LL_CTRL_PROC_LE_PING        (7)
-#define BLE_LL_CTRL_PROC_DATA_LEN_UPD   (8)
-#define BLE_LL_CTRL_PROC_PHY_UPDATE     (9)
-#define BLE_LL_CTRL_PROC_NUM            (10)
-#define BLE_LL_CTRL_PROC_IDLE           (255)
-
-/* Checks if a particular control procedure is running */
-#define IS_PENDING_CTRL_PROC(sm, proc)  (sm->pending_ctrl_procs & (1 << proc))
-#define CLR_PENDING_CTRL_PROC(sm, proc) (sm->pending_ctrl_procs &= ~(1 << proc))
-
-/* LL control procedure timeout */
-#define BLE_LL_CTRL_PROC_TIMEOUT        (40)    /* in secs */
-
-/*
- * LL CTRL PDU format
- *  -> Opcode   (1 byte)
- *  -> Data     (0 - 26 bytes)
- */
-#define BLE_LL_CTRL_CONN_UPDATE_IND     (0)
-#define BLE_LL_CTRL_CHANNEL_MAP_REQ     (1)
-#define BLE_LL_CTRL_TERMINATE_IND       (2)
-#define BLE_LL_CTRL_ENC_REQ             (3)
-#define BLE_LL_CTRL_ENC_RSP             (4)
-#define BLE_LL_CTRL_START_ENC_REQ       (5)
-#define BLE_LL_CTRL_START_ENC_RSP       (6)
-#define BLE_LL_CTRL_UNKNOWN_RSP         (7)
-#define BLE_LL_CTRL_FEATURE_REQ         (8)
-#define BLE_LL_CTRL_FEATURE_RSP         (9)
-#define BLE_LL_CTRL_PAUSE_ENC_REQ       (10)
-#define BLE_LL_CTRL_PAUSE_ENC_RSP       (11)
-#define BLE_LL_CTRL_VERSION_IND         (12)
-#define BLE_LL_CTRL_REJECT_IND          (13)
-#define BLE_LL_CTRL_SLAVE_FEATURE_REQ   (14)
-#define BLE_LL_CTRL_CONN_PARM_REQ       (15)
-#define BLE_LL_CTRL_CONN_PARM_RSP       (16)
-#define BLE_LL_CTRL_REJECT_IND_EXT      (17)
-#define BLE_LL_CTRL_PING_REQ            (18)
-#define BLE_LL_CTRL_PING_RSP            (19)
-#define BLE_LL_CTRL_LENGTH_REQ          (20)
-#define BLE_LL_CTRL_LENGTH_RSP          (21)
-#define BLE_LL_CTRL_PHY_REQ             (22)
-#define BLE_LL_CTRL_PHY_RSP             (23)
-#define BLE_LL_CTRL_PHY_UPDATE_IND      (24)
-#define BLE_LL_CTRL_MIN_USED_CHAN_IND   (25)
-
-/* Maximum opcode value */
-#define BLE_LL_CTRL_OPCODES             (BLE_LL_CTRL_MIN_USED_CHAN_IND + 1)
-
-extern const uint8_t g_ble_ll_ctrl_pkt_lengths[BLE_LL_CTRL_OPCODES];
-
-/* Maximum # of payload bytes in a LL control PDU */
-#define BLE_LL_CTRL_MAX_PAYLOAD         (26)
-#define BLE_LL_CTRL_MAX_PDU_LEN         (27)
-
-/* LL control connection update request */
-struct ble_ll_conn_upd_req
-{
-    uint8_t winsize;
-    uint16_t winoffset;
-    uint16_t interval;
-    uint16_t latency;
-    uint16_t timeout;
-    uint16_t instant;
-};
-#define BLE_LL_CTRL_CONN_UPD_REQ_LEN        (11)
-
-/* LL control channel map request */
-struct ble_ll_chan_map_req
-{
-    uint8_t chmap[5];
-    uint16_t instant;
-};
-#define BLE_LL_CTRL_CHAN_MAP_LEN            (7)
-
-/*
- * LL control terminate ind
- *  -> error code (1 byte)
- */
-#define BLE_LL_CTRL_TERMINATE_IND_LEN      (1)
-
-/* LL control enc req */
-struct ble_ll_enc_req
-{
-    uint8_t rand[8];
-    uint16_t ediv;
-    uint8_t skdm[8];
-    uint32_t ivm;
-};
-
-#define BLE_LL_CTRL_ENC_REQ_LEN             (22)
-
-/* LL control enc rsp */
-struct ble_ll_enc_rsp
-{
-    uint8_t skds[8];
-    uint32_t ivs;
-};
-
-#define BLE_LL_CTRL_ENC_RSP_LEN             (12)
-
-/* LL control start/pause enc request and response */
-#define BLE_LL_CTRL_START_ENC_REQ_LEN       (0)
-#define BLE_LL_CTRL_START_ENC_RSP_LEN       (0)
-#define BLE_LL_CTRL_PAUSE_ENC_REQ_LEN       (0)
-#define BLE_LL_CTRL_PAUSE_ENC_RSP_LEN       (0)
-
-/*
- * LL control unknown response
- *  -> 1 byte which contains the unknown or un-supported opcode.
- */
-#define BLE_LL_CTRL_UNK_RSP_LEN             (1)
-
-/*
- * LL control feature req and LL control feature rsp
- *  -> 8 bytes of data containing features supported by device.
- */
-#define BLE_LL_CTRL_FEATURE_LEN             (8)
-
-/*
- * LL control version ind
- *  -> version (1 byte):
- *      Contains the version number of the bluetooth controller specification.
- *  -> comp_id (2 bytes)
- *      Contains the company identifier of the manufacturer of the controller.
- *  -> sub_ver_num: Contains a unique value for implementation or revision of
- *      the bluetooth controller.
- */
-struct ble_ll_version_ind
-{
-    uint8_t ble_ctrlr_ver;
-    uint16_t company_id;
-    uint16_t sub_ver_num;
-};
-
-#define BLE_LL_CTRL_VERSION_IND_LEN         (5)
-
-/*
- * LL control reject ind
- *  -> error code (1 byte): contains reason why request was rejected.
- */
-#define BLE_LL_CTRL_REJ_IND_LEN             (1)
-
-/*
- * LL control slave feature req
- *  -> 8 bytes of data containing features supported by device.
- */
-#define BLE_LL_CTRL_SLAVE_FEATURE_REQ_LEN   (8)
-
-/* LL control connection param req and connection param rsp */
-struct ble_ll_conn_params
-{
-    uint16_t interval_min;
-    uint16_t interval_max;
-    uint16_t latency;
-    uint16_t timeout;
-    uint8_t pref_periodicity;
-    uint16_t ref_conn_event_cnt;
-    uint16_t offset0;
-    uint16_t offset1;
-    uint16_t offset2;
-    uint16_t offset3;
-    uint16_t offset4;
-    uint16_t offset5;
-};
-
-#define BLE_LL_CTRL_CONN_PARAMS_LEN     (23)
-
-/* LL control reject ind ext */
-struct ble_ll_reject_ind_ext
-{
-    uint8_t reject_opcode;
-    uint8_t err_code;
-};
-
-#define BLE_LL_CTRL_REJECT_IND_EXT_LEN  (2)
-
-/* LL control ping req and ping rsp (contain no data) */
-#define BLE_LL_CTRL_PING_LEN            (0)
-
-/*
- * LL control length req and length rsp
- *  -> max_rx_bytes (2 bytes): defines connMaxRxOctets. Range 27 to 251
- *  -> max_rx_time (2 bytes): defines connMaxRxTime. Range 328 to 2120 usecs.
- *  -> max_tx_bytes (2 bytes): defines connMaxTxOctets. Range 27 to 251
- *  -> max_tx_time (2 bytes): defines connMaxTxTime. Range 328 to 2120 usecs.
- */
-struct ble_ll_len_req
-{
-    uint16_t max_rx_bytes;
-    uint16_t max_rx_time;
-    uint16_t max_tx_bytes;
-    uint16_t max_tx_time;
-};
-
-#define BLE_LL_CTRL_LENGTH_REQ_LEN      (8)
-
-/* PHY request/response */
-#define BLE_LL_CTRL_PHY_REQ_LEN         (2)
-#define BLE_LL_CTRL_PHY_RSP_LEN         (2)
-#define BLE_LL_CTRL_PHY_UPD_IND_LEN     (4)
-
-/* Min used channels */
-#define BLE_LL_CTRL_MIN_USED_CHAN_LEN   (2)
-
-/* API */
-struct ble_ll_conn_sm;
-void ble_ll_ctrl_proc_start(struct ble_ll_conn_sm *connsm, int ctrl_proc);
-void ble_ll_ctrl_proc_stop(struct ble_ll_conn_sm *connsm, int ctrl_proc);
-int ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om);
-void ble_ll_ctrl_chk_proc_start(struct ble_ll_conn_sm *connsm);
-void ble_ll_ctrl_terminate_start(struct ble_ll_conn_sm *connsm);
-int ble_ll_ctrl_is_terminate_ind(uint8_t hdr, uint8_t opcode);
-uint8_t ble_ll_ctrl_conn_param_reply(struct ble_ll_conn_sm *connsm,
-                                     uint8_t *rsp,
-                                     struct ble_ll_conn_params *req);
-int ble_ll_ctrl_reject_ind_send(struct ble_ll_conn_sm *connsm,
-                                uint8_t rej_opcode, uint8_t err);
-int ble_ll_ctrl_start_enc_send(struct ble_ll_conn_sm *connsm, uint8_t opcode);
-int ble_ll_ctrl_enc_allowed_pdu(struct os_mbuf_pkthdr *pkthdr);
-int ble_ll_ctrl_tx_done(struct os_mbuf *txpdu, struct ble_ll_conn_sm *connsm);
-int ble_ll_ctrl_is_start_enc_rsp(struct os_mbuf *txpdu);
-
-void ble_ll_hci_ev_datalen_chg(struct ble_ll_conn_sm *connsm);
-void ble_ll_hci_ev_rem_conn_parm_req(struct ble_ll_conn_sm *connsm,
-                                     struct ble_ll_conn_params *cp);
-void ble_ll_hci_ev_conn_update(struct ble_ll_conn_sm *connsm, uint8_t status);
-void ble_ll_hci_ev_rd_rem_used_feat(struct ble_ll_conn_sm *connsm,
-                                      uint8_t status);
-void ble_ll_hci_ev_rd_rem_ver(struct ble_ll_conn_sm *connsm, uint8_t status);
-void ble_ll_hci_ev_encrypt_chg(struct ble_ll_conn_sm *connsm, uint8_t status);
-int ble_ll_hci_ev_ltk_req(struct ble_ll_conn_sm *connsm);
-int ble_ll_hci_ev_hw_err(uint8_t hw_err);
-void ble_ll_hci_ev_databuf_overflow(void);
-void ble_ll_hci_ev_le_csa(struct ble_ll_conn_sm *connsm);
-void ble_ll_hci_ev_send_scan_req_recv(uint8_t adv_handle, const uint8_t *peer,
-                                      uint8_t peer_addr_type);
-void ble_ll_hci_ev_send_adv_set_terminated(uint8_t status, uint8_t adv_handle,
-                                           uint16_t conn_handle, uint8_t events);
-int ble_ll_hci_ev_phy_update(struct ble_ll_conn_sm *connsm, uint8_t status);
-void ble_ll_calc_session_key(struct ble_ll_conn_sm *connsm);
-void ble_ll_ctrl_phy_update_proc_complete(struct ble_ll_conn_sm *connsm);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_CTRL_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_hci.h b/net/nimble/controller/include/controller/ble_ll_hci.h
deleted file mode 100644
index 3eb7f60d8..000000000
--- a/net/nimble/controller/include/controller/ble_ll_hci.h
+++ /dev/null
@@ -1,64 +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_LL_HCI_
-#define H_BLE_LL_HCI_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* For supported commands */
-#define BLE_LL_SUPP_CMD_LEN (40)
-extern const uint8_t g_ble_ll_supp_cmds[BLE_LL_SUPP_CMD_LEN];
-
-/* The largest event the controller will send. */
-#define BLE_LL_MAX_EVT_LEN  MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)
-
-/*
- * This determines the number of outstanding commands allowed from the
- * host to the controller. NOTE: you cannot change this without modifying
- * other portions of the code as we currently use a global os event for
- * the command; you would need to allocate a pool of these.
- */
-#define BLE_LL_CFG_NUM_HCI_CMD_PKTS     (1)
-
-/* Initialize LL HCI */
-void ble_ll_hci_init(void);
-
-/* Used to determine if the LE event is enabled/disabled */
-uint8_t ble_ll_hci_is_le_event_enabled(int subev);
-
-/* Used to determine if event is enabled/disabled */
-uint8_t ble_ll_hci_is_event_enabled(int evcode);
-
-/* Send event from controller to host */
-int ble_ll_hci_event_send(uint8_t *evbuf);
-
-/* Sends a command complete with a no-op opcode to host */
-int ble_ll_hci_send_noop(void);
-
-/* Checks the preferref phy masks from set default phy and set phy commands */
-int ble_ll_hci_chk_phy_masks(uint8_t *cmdbuf, uint8_t *txphy, uint8_t *rxphy);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_HCI_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_resolv.h b/net/nimble/controller/include/controller/ble_ll_resolv.h
deleted file mode 100644
index 6c09bee7a..000000000
--- a/net/nimble/controller/include/controller/ble_ll_resolv.h
+++ /dev/null
@@ -1,105 +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_LL_RESOLV_
-#define H_BLE_LL_RESOLV_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * An entry in the resolving list.
- *      The identity address is stored in little endian format.
- *      The local rpa is stored in little endian format.
- *      The IRKs are stored in big endian format.
- */
-struct ble_ll_resolv_entry
-{
-    uint8_t rl_addr_type;
-    uint8_t rl_local_rpa_set;
-    uint8_t rl_reserved;
-    uint8_t rl_priv_mode;
-    uint8_t rl_local_irk[16];
-    uint8_t rl_peer_irk[16];
-    uint8_t rl_identity_addr[BLE_DEV_ADDR_LEN];
-    uint8_t rl_local_rpa[BLE_DEV_ADDR_LEN];
-};
-
-extern struct ble_ll_resolv_entry g_ble_ll_resolv_list[];
-
-/* Clear the resolving list */
-int ble_ll_resolv_list_clr(void);
-
-/* Read the size of the resolving list */
-int ble_ll_resolv_list_read_size(uint8_t *rspbuf, uint8_t *rsplen);
-
-/* Add a device to the resolving list */
-int ble_ll_resolv_list_add(uint8_t *cmdbuf);
-
-/* Remove a device from the resolving list */
-int ble_ll_resolv_list_rmv(uint8_t *cmdbuf);
-
-/* Address resolution enable command */
-int ble_ll_resolv_enable_cmd(uint8_t *cmdbuf);
-
-/* XXX: implement */
-int ble_ll_resolv_peer_addr_rd(uint8_t *cmdbuf);
-void ble_ll_resolv_local_addr_rd(uint8_t *cmdbuf);
-
-/* Finds 'addr' in resolving list. Doesnt check if address resolution enabled */
-struct ble_ll_resolv_entry *
-ble_ll_resolv_list_find(uint8_t *addr, uint8_t addr_type);
-
-/* Called to determine if the IRK is all zero. */
-int ble_ll_resolv_irk_nonzero(uint8_t *irk);
-
-/* Returns true if address resolution is enabled */
-uint8_t ble_ll_resolv_enabled(void);
-
-/* Reset private address resolution */
-void ble_ll_resolv_list_reset(void);
-
-void ble_ll_resolv_gen_priv_addr(struct ble_ll_resolv_entry *rl, int local,
-                                 uint8_t *addr);
-
-/* Generate a resolvable private address. */
-int ble_ll_resolv_gen_rpa(uint8_t *addr, uint8_t addr_type, uint8_t *rpa,
-                          int local);
-
-/* Set the resolvable private address timeout */
-int ble_ll_resolv_set_rpa_tmo(uint8_t *cmdbuf);
-
-/* Set the privacy mode */
-int ble_ll_resolve_set_priv_mode(uint8_t *cmdbuf);
-
-/* Get the RPA timeout, in seconds */
-uint32_t ble_ll_resolv_get_rpa_tmo(void);
-
-/* Resolve a resolvable private address */
-int ble_ll_resolv_rpa(uint8_t *rpa, uint8_t *irk);
-
-/* Initialize resolv*/
-void ble_ll_resolv_init(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/net/nimble/controller/include/controller/ble_ll_scan.h b/net/nimble/controller/include/controller/ble_ll_scan.h
deleted file mode 100644
index 35cbeb44a..000000000
--- a/net/nimble/controller/include/controller/ble_ll_scan.h
+++ /dev/null
@@ -1,246 +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_LL_SCAN_
-#define H_BLE_LL_SCAN_
-
-#include "controller/ble_ll_sched.h"
-#include "hal/hal_timer.h"
-#include "syscfg/syscfg.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * SCAN_REQ
- *      -> ScanA    (6 bytes)
- *      -> AdvA     (6 bytes)
- *
- *  ScanA is the scanners public (TxAdd=0) or random (TxAdd = 1) address
- *  AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address.
- *
- * Sent by the LL in the Scanning state; received by the LL in the advertising
- * state. The advertising address is the intended recipient of this frame.
- */
-#define BLE_SCAN_REQ_LEN                (12)
-
-/*
- * SCAN_RSP
- *      -> AdvA         (6 bytes)
- *      -> ScanRspData  (0 - 31 bytes)
- *
- *  AdvaA is the advertisers public (TxAdd=0) or random (TxAdd=1) address.
- *  ScanRspData may contain any data from the advertisers host.
- *
- * Sent by the LL in the advertising state; received by the LL in the
- * scanning state.
- */
-#define BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN       (31)
-#define BLE_SCAN_LEGACY_MAX_PKT_LEN            (37)
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-#define BLE_SCAN_RSP_DATA_MAX_LEN       MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE)
-
-/* For Bluetooth 5.0 we need state machine for two PHYs*/
-#define BLE_LL_SCAN_PHY_NUMBER          (2)
-#else
-#define BLE_LL_SCAN_PHY_NUMBER          (1)
-#define BLE_SCAN_RSP_DATA_MAX_LEN       BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN
-#endif
-
-#define PHY_UNCODED                    (0)
-#define PHY_CODED                      (1)
-#define PHY_NOT_CONFIGURED             (0xFF)
-
-#define BLE_LL_EXT_ADV_MODE_NON_CONN    (0x00)
-#define BLE_LL_EXT_ADV_MODE_CONN        (0x01)
-#define BLE_LL_EXT_ADV_MODE_SCAN        (0x02)
-
-struct ble_ll_scan_params
-{
-    uint8_t phy;
-    uint8_t own_addr_type;
-    uint8_t scan_filt_policy;
-    uint8_t configured;
-    uint8_t scan_type;
-    uint8_t scan_chan;
-    uint16_t scan_itvl;
-    uint16_t scan_window;
-    uint32_t scan_win_start_time;
-    uint32_t next_event_start;
-};
-
-#define BLE_LL_AUX_CHAIN_BIT            0x01
-#define BLE_LL_AUX_INCOMPLETE_BIT       0x02
-#define BLE_LL_AUX_INCOMPLETE_ERR_BIT   0x04
-#define BLE_LL_AUX_HAS_ADDRA            0x08
-
-#define BLE_LL_CHECK_AUX_FLAG(aux_data, flag) (!!((aux_data)->flags & flag))
-
-struct ble_ll_aux_data {
-    uint8_t chan;
-    uint8_t aux_phy;
-    uint8_t aux_primary_phy;
-    uint8_t mode;
-    uint8_t scanning;
-    uint8_t flags;
-    uint16_t did;
-    uint32_t offset;
-    uint8_t offset_units;
-    uint8_t addr[6];
-    uint8_t addr_type;
-    struct ble_ll_sched_item sch;
-};
-
-struct ble_ll_scan_sm
-{
-    uint8_t scan_enabled;
-    uint8_t own_addr_type;
-    uint8_t scan_filt_dups;
-    uint8_t scan_rsp_pending;
-    uint8_t scan_rsp_cons_fails;
-    uint8_t scan_rsp_cons_ok;
-    int8_t scan_rpa_index;
-    uint8_t scan_peer_rpa[BLE_DEV_ADDR_LEN];
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    uint32_t scan_nrpa_timer;
-    uint8_t scan_nrpa[BLE_DEV_ADDR_LEN];
-#endif
-
-    /* XXX: Shall we count backoff per phy? */
-    uint16_t upper_limit;
-    uint16_t backoff_count;
-    uint32_t scan_win_start_time;
-    struct os_mbuf *scan_req_pdu;
-    struct os_event scan_sched_ev;
-    struct hal_timer scan_timer;
-
-    uint16_t duration;
-    uint16_t period;
-
-    uint8_t cur_phy;
-    uint8_t next_phy;
-    uint8_t ext_scanning;
-    uint8_t restart_timer_needed;
-    struct ble_ll_aux_data *cur_aux_data;
-    struct ble_ll_scan_params phy_data[BLE_LL_SCAN_PHY_NUMBER];
-};
-
-/* Scan types */
-#define BLE_SCAN_TYPE_PASSIVE   (BLE_HCI_SCAN_TYPE_PASSIVE)
-#define BLE_SCAN_TYPE_ACTIVE    (BLE_HCI_SCAN_TYPE_ACTIVE)
-#define BLE_SCAN_TYPE_INITIATE  (2)
-
-/*---- HCI ----*/
-/* Set scanning parameters */
-int ble_ll_scan_set_scan_params(uint8_t *cmd);
-
-/* Turn scanning on/off */
-int ble_ll_scan_set_enable(uint8_t *cmd, uint8_t ext);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-int ble_ll_set_ext_scan_params(uint8_t *cmd);
-#endif
-
-/*--- Controller Internal API ---*/
-/* Initialize the scanner */
-void ble_ll_scan_init(void);
-
-/* Reset the scanner */
-void ble_ll_scan_reset(void);
-
-/* Called when Link Layer starts to receive a PDU and is in scanning state */
-int ble_ll_scan_rx_isr_start(uint8_t pdu_type, uint16_t *rxflags);
-
-/* Called when Link Layer has finished receiving a PDU while scanning */
-int ble_ll_scan_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok);
-
-/* Process a scan response PDU */
-void ble_ll_scan_rx_pkt_in(uint8_t pdu_type, struct os_mbuf *om,
-                           struct ble_mbuf_hdr *hdr);
-
-/* Boolean function denoting whether or not the whitelist can be changed */
-int ble_ll_scan_can_chg_whitelist(void);
-
-/* Boolean function returning true if scanning enabled */
-int ble_ll_scan_enabled(void);
-
-/* Boolean function returns true if whitelist is enabled for scanning */
-int ble_ll_scan_whitelist_enabled(void);
-
-/* Initialize the scanner when we start initiating */
-struct hci_create_conn;
-int ble_ll_scan_initiator_start(struct hci_create_conn *hcc,
-                                struct ble_ll_scan_sm **sm);
-
-/* Returns the PDU allocated by the scanner */
-struct os_mbuf *ble_ll_scan_get_pdu(void);
-
-/* Called to set the resolvable private address of the last connected peer */
-void ble_ll_scan_set_peer_rpa(uint8_t *rpa);
-
-/* Returns peer RPA of last connection made */
-uint8_t *ble_ll_scan_get_peer_rpa(void);
-
-/* Returns the local RPA used by the scanner/initiator */
-uint8_t *ble_ll_scan_get_local_rpa(void);
-
-/* Stop the scanning state machine */
-void ble_ll_scan_sm_stop(int chk_disable);
-
-/* Resume scanning */
-void ble_ll_scan_chk_resume(void);
-
-/* Called when wait for response timer expires in scanning mode */
-void ble_ll_scan_wfr_timer_exp(void);
-
-int ble_ll_scan_adv_decode_addr(uint8_t pdu_type, uint8_t *rxbuf,
-                                struct ble_mbuf_hdr *ble_hdr,
-                                uint8_t **addr, uint8_t *addr_type,
-                                uint8_t **inita, uint8_t *init_addr_type,
-                                int *ext_mode);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-/* Get aux ptr from ext advertising */
-int ble_ll_scan_get_aux_data(struct ble_ll_scan_sm *scansm,
-                             struct ble_mbuf_hdr *ble_hdr, uint8_t *rxbuf,
-                             struct ble_ll_aux_data **aux_data);
-
-/* Initialize the extended scanner when we start initiating */
-struct hci_ext_create_conn;
-int ble_ll_scan_ext_initiator_start(struct hci_ext_create_conn *hcc,
-                                    struct ble_ll_scan_sm **sm);
-
-/* Called to parse extended advertising*/
-struct ble_ll_ext_adv;
-int ble_ll_scan_parse_ext_hdr(struct os_mbuf *om, struct ble_mbuf_hdr *ble_hdr,
-                              struct ble_ll_ext_adv *parsed_evt);
-
-void ble_ll_scan_aux_data_free(struct ble_ll_aux_data *aux_scan);
-#endif
-
-/* Called to clean up current aux data */
-void ble_ll_scan_clean_cur_aux_data(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_SCAN_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_sched.h b/net/nimble/controller/include/controller/ble_ll_sched.h
deleted file mode 100644
index 15f750077..000000000
--- a/net/nimble/controller/include/controller/ble_ll_sched.h
+++ /dev/null
@@ -1,210 +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_LL_SCHED_
-#define H_BLE_LL_SCHED_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Time per BLE scheduler slot */
-#define BLE_LL_SCHED_USECS_PER_SLOT         (1250)
-#define BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT   (41)    /* 1 tick = 30.517 usecs */
-
-/*
- * Worst case time needed for scheduled advertising item. This is the longest
- * possible time to receive a scan request and send a scan response (with the
- * appropriate IFS time between them). This number is calculated using the
- * following formula: IFS + SCAN_REQ + IFS + SCAN_RSP = 150 + 176 + 150 + 376.
- * Note: worst case time to tx adv, rx scan req and send scan rsp is 1228 usecs.
- * This assumes maximum sized advertising PDU and scan response PDU.
- *
- * For connectable advertising events no scan request is allowed. In this case
- * we just need to receive a connect request PDU: IFS + CONNECT_REQ = 150 + 352.
- * Note: worst-case is 376 + 150 + 352 = 878 usecs
- *
- * NOTE: The advertising PDU transmit time is NOT included here since we know
- * how long that will take (worst-case is 376 usecs).
- */
-#define BLE_LL_SCHED_ADV_MAX_USECS          (852)
-#define BLE_LL_SCHED_DIRECT_ADV_MAX_USECS   (502)
-#define BLE_LL_SCHED_MAX_ADV_PDU_USECS      (376)
-
-/* We don't know how big aux packet will be. It depends on adv packet len which
- * can be up to 256. For now we fix it to 128 bytes, but we can optimize it.
- */
-#define BLE_LL_SCHED_AUX_PTR_DFLT_BYTES_NUM  (128)
-
-/*
- * This is the offset from the start of the scheduled item until the actual
- * tx/rx should occur, in ticks.
- */
-extern uint8_t g_ble_ll_sched_offset_ticks;
-
-/*
- * This is the number of slots needed to transmit and receive a maximum
- * size PDU, including an IFS time before each. The actual time is
- * 2120 usecs for tx/rx and 150 for IFS = 4540 usecs.
- */
-#define BLE_LL_SCHED_MAX_TXRX_SLOT  (4 * BLE_LL_SCHED_USECS_PER_SLOT)
-
-/* BLE scheduler errors */
-#define BLE_LL_SCHED_ERR_OVERLAP    (1)
-
-/* Types of scheduler events */
-#define BLE_LL_SCHED_TYPE_ADV       (1)
-#define BLE_LL_SCHED_TYPE_SCAN      (2)
-#define BLE_LL_SCHED_TYPE_CONN      (3)
-#define BLE_LL_SCHED_TYPE_AUX_SCAN  (4)
-#define BLE_LL_SCHED_TYPE_DTM       (5)
-
-/* Return values for schedule callback. */
-#define BLE_LL_SCHED_STATE_RUNNING  (0)
-#define BLE_LL_SCHED_STATE_DONE     (1)
-
-/* Callback function */
-struct ble_ll_sched_item;
-typedef int (*sched_cb_func)(struct ble_ll_sched_item *sch);
-
-/*
- * Strict connection scheduling (for the master) is different than how
- * connections are normally scheduled. With strict connection scheduling we
- * introduce the concept of a "period". A period is a collection of slots. Each
- * slot is 1.25 msecs in length. The number of slots in a period is determined
- * by the syscfg value BLE_LL_CONN_INIT_SLOTS. A collection of periods is called
- * an epoch. The length of an epoch is determined by the number of connections
- * (BLE_MAX_CONNECTIONS plus BLE_LL_ADD_STRICT_SCHED_PERIODS). Connections
- * will be scheduled at period boundaries. Any scanning/initiating/advertising
- * will be done in unused periods, if possible.
- */
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-#define BLE_LL_SCHED_PERIODS    (MYNEWT_VAL(BLE_MAX_CONNECTIONS) + \
-                                 MYNEWT_VAL(BLE_LL_ADD_STRICT_SCHED_PERIODS))
-
-struct ble_ll_sched_obj
-{
-    uint8_t sch_num_occ_periods;
-    uint32_t sch_occ_period_mask;
-    uint32_t sch_ticks_per_period;
-    uint32_t sch_ticks_per_epoch;
-    uint32_t sch_epoch_start;
-};
-
-extern struct ble_ll_sched_obj g_ble_ll_sched_data;
-
-/*
- * XXX: TODO:
- * -> How do we know epoch start is up to date? Not wrapped?
- * -> for now, only do this with no more than 32 connections.
- * -> Do not let initiating occur if no empty sched slots
- */
-#endif
-
-/*
- * Schedule item
- *  sched_type: This is the type of the schedule item.
- *  enqueued: Flag denoting if item is on the scheduler list. 0: no, 1:yes
- *  remainder: # of usecs from offset till tx/rx should occur
- *  txrx_offset: Number of ticks from start time until tx/rx should occur.
- *
- */
-struct ble_ll_sched_item
-{
-    uint8_t         sched_type;
-    uint8_t         enqueued;
-    uint8_t         remainder;
-    uint32_t        start_time;
-    uint32_t        end_time;
-    void            *cb_arg;
-    sched_cb_func   sched_cb;
-    TAILQ_ENTRY(ble_ll_sched_item) link;
-};
-
-/* Initialize the scheduler */
-int ble_ll_sched_init(void);
-
-/* Remove item(s) from schedule */
-void ble_ll_sched_rmv_elem(struct ble_ll_sched_item *sch);
-
-/* Schedule a new master connection */
-struct ble_ll_conn_sm;
-int ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm,
-                            struct ble_mbuf_hdr *ble_hdr, uint8_t pyld_len);
-
-/* Schedule a new slave connection */
-int ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm);
-
-struct ble_ll_adv_sm;
-typedef void ble_ll_sched_adv_new_cb(struct ble_ll_adv_sm *advsm,
-                                     uint32_t sch_start, void *arg);
-
-/* Schedule a new advertising event */
-int ble_ll_sched_adv_new(struct ble_ll_sched_item *sch,
-                         ble_ll_sched_adv_new_cb cb, void *arg);
-
-/* Reschedule an advertising event */
-int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch, uint32_t *start,
-                                uint32_t max_delay_ticks);
-
-/* Reschedule and advertising pdu */
-int ble_ll_sched_adv_resched_pdu(struct ble_ll_sched_item *sch);
-
-/* Reschedule a connection that had previously been scheduled or that is over */
-int ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm * connsm);
-
-/**
- * Called to determine when the next scheduled event will occur.
- *
- * If there are not scheduled events this function returns 0; otherwise it
- * returns 1 and *next_event_time is set to the start time of the next event.
- *
- * @param next_event_time cputime at which next scheduled event will occur
- *
- * @return int 0: No events are scheduled 1: there is an upcoming event
- */
-int ble_ll_sched_next_time(uint32_t *next_event_time);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-struct ble_ll_scan_sm;
-struct ble_ll_aux_data;
-int ble_ll_sched_aux_scan(struct ble_mbuf_hdr *ble_hdr,
-                          struct ble_ll_scan_sm *scansm,
-                          struct ble_ll_aux_data *aux_scan);
-
-int ble_ll_sched_scan_req_over_aux_ptr(uint32_t chan, uint8_t phy_mode);
-#endif
-
-/* Stop the scheduler */
-void ble_ll_sched_stop(void);
-
-#ifdef BLE_XCVR_RFCLK
-/* Check if RF clock needs to be restarted */
-void ble_ll_sched_rfclk_chk_restart(void);
-#endif
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-int ble_ll_sched_dtm(struct ble_ll_sched_item *sch);
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_LL_SCHED_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_test.h b/net/nimble/controller/include/controller/ble_ll_test.h
deleted file mode 100644
index 32984c6b3..000000000
--- a/net/nimble/controller/include/controller/ble_ll_test.h
+++ /dev/null
@@ -1,35 +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_LL_TEST_
-#define H_LL_TEST_
-
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int ble_ll_csa2_test_all(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/net/nimble/controller/include/controller/ble_ll_whitelist.h b/net/nimble/controller/include/controller/ble_ll_whitelist.h
deleted file mode 100644
index e9549e964..000000000
--- a/net/nimble/controller/include/controller/ble_ll_whitelist.h
+++ /dev/null
@@ -1,52 +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_LL_WHITELIST_
-#define H_BLE_LL_WHITELIST_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Clear the whitelist */
-int ble_ll_whitelist_clear(void);
-
-/* Read the size of the whitelist */
-int ble_ll_whitelist_read_size(uint8_t *rspbuf, uint8_t *rsplen);
-
-/* Add a device to the whitelist */
-int ble_ll_whitelist_add(uint8_t *addr, uint8_t addr_type);
-
-/* Remove a device fromthe whitelist */
-int ble_ll_whitelist_rmv(uint8_t *addr, uint8_t addr_type);
-
-/* Enable whitelisting */
-void ble_ll_whitelist_enable(void);
-
-/* Disable whitelisting */
-void ble_ll_whitelist_disable(void);
-
-/* Boolean function returning true if address matches a whitelist entry */
-int ble_ll_whitelist_match(uint8_t *addr, uint8_t addr_type, int is_ident);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_WHITELIST_ */
diff --git a/net/nimble/controller/include/controller/ble_ll_xcvr.h b/net/nimble/controller/include/controller/ble_ll_xcvr.h
deleted file mode 100644
index c59b00928..000000000
--- a/net/nimble/controller/include/controller/ble_ll_xcvr.h
+++ /dev/null
@@ -1,48 +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_LL_XCVR_
-#define H_BLE_LL_XCVR_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifdef BLE_XCVR_RFCLK
-
-/* RF clock states */
-#define BLE_RFCLK_STATE_OFF     (0)
-#define BLE_RFCLK_STATE_ON      (1)
-#define BLE_RFCLK_STATE_SETTLED (2)
-
-int ble_ll_xcvr_rfclk_state(void);
-void ble_ll_xcvr_rfclk_start_now(uint32_t now);
-void ble_ll_xcvr_rfclk_stop(void);
-void ble_ll_xcvr_rfclk_enable(void);
-void ble_ll_xcvr_rfclk_disable(void);
-uint32_t ble_ll_xcvr_rfclk_time_till_settled(void);
-void ble_ll_xcvr_rfclk_timer_exp(void *arg);
-void ble_ll_xcvr_rfclk_timer_start(uint32_t cputime);
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_LL_ */
diff --git a/net/nimble/controller/include/controller/ble_phy.h b/net/nimble/controller/include/controller/ble_phy.h
deleted file mode 100644
index d6fdcea93..000000000
--- a/net/nimble/controller/include/controller/ble_phy.h
+++ /dev/null
@@ -1,255 +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_PHY_
-#define H_BLE_PHY_
-
-#include "nimble/hci_common.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) && !(MYNEWT_VAL(BSP_NRF52) || MYNEWT_VAL(BSP_NRF52840))
-#error LE 2M PHY can only be enabled on nRF52xxx
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY) && !MYNEWT_VAL(BSP_NRF52840)
-#error LE Coded PHY can only be enabled on nRF52840
-#endif
-
-/* Forward declarations */
-struct os_mbuf;
-
-/* Channel/Frequency defintions */
-#define BLE_PHY_NUM_CHANS           (40)
-#define BLE_PHY_NUM_DATA_CHANS      (37)
-#define BLE_PHY_CHAN0_FREQ_MHZ      (2402)
-#define BLE_PHY_DATA_CHAN0_FREQ_MHZ (2404)
-#define BLE_PHY_CHAN_SPACING_MHZ    (2)
-#define BLE_PHY_NUM_ADV_CHANS       (3)
-#define BLE_PHY_ADV_CHAN_START      (37)
-
-/* Power */
-#define BLE_PHY_MAX_PWR_DBM         (10)
-
-/* Deviation */
-#define BLE_PHY_DEV_KHZ             (185)
-#define BLE_PHY_BINARY_ZERO         (-BLE_PHY_DEV)
-#define BLE_PHY_BINARY_ONE          (BLE_PHY_DEV)
-
-/* Max. clock drift */
-#define BLE_PHY_MAX_DRIFT_PPM       (50)
-
-/* Data rate */
-#define BLE_PHY_BIT_RATE_BPS        (1000000)
-
-/* Macros */
-#define BLE_IS_ADV_CHAN(chan)       (chan >= BLE_PHY_ADV_CHAN_START)
-#define BLE_IS_DATA_CHAN(chan)      (chan < BLE_PHY_ADV_CHAN_START)
-
-/* PHY states */
-#define BLE_PHY_STATE_IDLE          (0)
-#define BLE_PHY_STATE_RX            (1)
-#define BLE_PHY_STATE_TX            (2)
-
-/* BLE PHY transitions */
-#define BLE_PHY_TRANSITION_NONE     (0)
-#define BLE_PHY_TRANSITION_RX_TX    (1)
-#define BLE_PHY_TRANSITION_TX_RX    (2)
-
-/* PHY error codes */
-#define BLE_PHY_ERR_RADIO_STATE     (1)
-#define BLE_PHY_ERR_INIT            (2)
-#define BLE_PHY_ERR_INV_PARAM       (3)
-#define BLE_PHY_ERR_NO_BUFS         (4)
-#define BLE_PHY_ERR_TX_LATE         (5)
-#define BLE_PHY_ERR_RX_LATE         (6)
-
-/* Maximun PDU length. Includes LL header of 2 bytes and 255 bytes payload. */
-#define BLE_PHY_MAX_PDU_LEN         (257)
-
-/* Wait for response timer */
-typedef void (*ble_phy_tx_end_func)(void *arg);
-
-/* Initialize the PHY */
-int ble_phy_init(void);
-
-/* Reset the PHY */
-int ble_phy_reset(void);
-
-/* Set the PHY channel */
-int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
-
-/* Set transmit start time */
-int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
-
-/* Set receive start time */
-int ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
-
-/* Set the transmit end callback and argument */
-void ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg);
-
-typedef uint8_t (*ble_phy_tx_pducb_t)(uint8_t *dptr, void *pducb_arg,
-                                      uint8_t *hdr_byte);
-
-/* Place the PHY into transmit mode */
-int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans);
-
-/* Place the PHY into receive mode */
-int ble_phy_rx(void);
-
-/* Copies the received PHY buffer into the allocated pdu */
-void ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu);
-
-/* Get an RSSI reading */
-int ble_phy_rssi_get(void);
-
-/* Set the transmit power */
-int ble_phy_txpwr_set(int dbm);
-
-/* Get highest allowed power from range */
-int ble_phy_txpower_round(int dbm);
-
-/* Get the transmit power */
-int ble_phy_txpwr_get(void);
-
-/* Disable the PHY */
-void ble_phy_disable(void);
-
-#define BLE_PHY_WFR_ENABLE_RX       (0)
-#define BLE_PHY_WFR_ENABLE_TXRX     (1)
-
-void ble_phy_stop_usec_timer(void);
-void ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs);
-
-/* Starts rf clock */
-void ble_phy_rfclk_enable(void);
-
-/* Stops rf clock */
-void ble_phy_rfclk_disable(void);
-
-/*
- * Used to restart reception on same channel after wfr timer expiration or
- * frame received.
- */
-void ble_phy_restart_rx(void);
-
-/* Gets the current state of the PHY */
-int ble_phy_state_get(void);
-
-/* Gets current state of transceiver */
-uint8_t ble_phy_xcvr_state_get(void);
-
-/* Returns 'true' if a reception has started */
-int ble_phy_rx_started(void);
-
-/*
- * Returns the maximum supported tx/rx PDU payload size, in bytes, for data
- * channel PDUs (this does not apply to advertising channel PDUs). Note
- * that the data channel PDU is composed of a 2-byte header, the payload, and
- * an optional MIC. The maximum payload is 251 bytes.
- */
-uint8_t ble_phy_max_data_pdu_pyld(void);
-
-/* Gets the current access address */
-uint32_t ble_phy_access_addr_get(void);
-
-/* Enable encryption */
-void ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                            uint8_t is_master);
-
-/* Disable encryption */
-void ble_phy_encrypt_disable(void);
-
-/* Set the packet counters and dir used by LE encyption */
-void ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir);
-
-/* Enable phy resolving list */
-void ble_phy_resolv_list_enable(void);
-
-/* Disable phy resolving list */
-void ble_phy_resolv_list_disable(void);
-
-/*
- * These definitions are used for the 'phy' parameters in the API listed below.
- * These are numbered in a specific order to save code. The HCI definitions for
- * the PHY modes for 1Mbps and 2Mbps are the same here. For the coded phy
- * they need to be translated from the HCI number to either 0 or 3. This
- * was done in order to save code when translating between the HCI phy value
- * and the phy API.
- */
-#define BLE_PHY_MODE_1M             (1)
-#define BLE_PHY_MODE_2M             (2)
-#define BLE_PHY_MODE_CODED_125KBPS  (0)
-#define BLE_PHY_MODE_CODED_500KBPS  (3)
-
-/* The number of different modes */
-#define BLE_PHY_NUM_MODE            (4)
-
-/* PHY numbers (compatible with HCI) */
-#define BLE_PHY_1M                  (BLE_HCI_LE_PHY_1M)
-#define BLE_PHY_2M                  (BLE_HCI_LE_PHY_2M)
-#define BLE_PHY_CODED               (BLE_HCI_LE_PHY_CODED)
-
-/* PHY bitmasks (compatible with HCI) */
-#define BLE_PHY_MASK_1M             (BLE_HCI_LE_PHY_1M_PREF_MASK)
-#define BLE_PHY_MASK_2M             (BLE_HCI_LE_PHY_2M_PREF_MASK)
-#define BLE_PHY_MASK_CODED          (BLE_HCI_LE_PHY_CODED_PREF_MASK)
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY))
-uint32_t ble_phy_mode_pdu_start_off(int phy);
-void ble_phy_mode_set(int cur_phy, int txtorx_phy);
-#else
-#define ble_phy_mode_pdu_start_off(phy)     (40)
-
-#endif
-
-int ble_phy_get_cur_phy(void);
-static inline int ble_ll_phy_to_phy_mode(int phy, int phy_options)
-{
-    int phy_mode;
-
-    /*
-     * Mode values are set in a way that 1M, 2M and Coded(S=2) are equivalent
-     * to 1M, 2M and Coded in HCI. The only conversion is needed for Coded(S=8)
-     * which uses non-HCI value.
-     */
-
-    phy_mode = phy;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    if (phy == BLE_PHY_CODED && phy_options == BLE_HCI_LE_PHY_CODED_S8_PREF) {
-        phy_mode = BLE_PHY_MODE_CODED_125KBPS;
-    }
-#endif
-
-    return phy_mode;
-}
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE)
-void ble_phy_enable_dtm(void);
-void ble_phy_disable_dtm(void);
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_PHY_ */
diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml
index fa5011818..218e4e2a5 100644
--- a/net/nimble/controller/pkg.yml
+++ b/net/nimble/controller/pkg.yml
@@ -18,21 +18,7 @@
 #
 
 pkg.name: net/nimble/controller
-pkg.description: Controller side of the nimble Bluetooth Smart stack.
 pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-    - ble
-    - bluetooth
-
-pkg.req_apis:
-    - ble_driver
-    - ble_transport
-    - stats
-
 pkg.deps:
-    - kernel/os
-    - net/nimble
-
-pkg.init:
-    ble_ll_init: 250
+    - "@apache-mynewt-nimble/nimble/controller"
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
deleted file mode 100644
index a1775c176..000000000
--- a/net/nimble/controller/src/ble_ll.c
+++ /dev/null
@@ -1,1557 +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 "sysinit/sysinit.h"
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "os/os_cputime.h"
-#include "stats/stats.h"
-#include "bsp/bsp.h"
-#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"
-#include "controller/ble_ll_adv.h"
-#include "controller/ble_ll_sched.h"
-#include "controller/ble_ll_scan.h"
-#include "controller/ble_ll_hci.h"
-#include "controller/ble_ll_whitelist.h"
-#include "controller/ble_ll_resolv.h"
-#include "controller/ble_ll_xcvr.h"
-#include "ble_ll_conn_priv.h"
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-#include <ble_ll_dtm_priv.h>
-#endif
-
-/* XXX:
- *
- * 1) use the sanity task!
- * 2) Need to figure out what to do with packets that we hand up that did
- * not pass the filter policy for the given state. Currently I count all
- * packets I think. Need to figure out what to do with this.
- * 3) For the features defined, we need to conditionally compile code.
- * 4) Should look into always disabled the wfr interrupt if we receive the
- * start of a frame. Need to look at the various states to see if this is the
- * right thing to do.
- */
-
-/* Supported states */
-#define BLE_LL_S_NCA                    (0x00000000001)
-#define BLE_LL_S_SA                     (0x00000000002)
-#define BLE_LL_S_CA                     (0x00000000004)
-#define BLE_LL_S_HDCA                   (0x00000000008)
-#define BLE_LL_S_PS                     (0x00000000010)
-#define BLE_LL_S_AS                     (0x00000000020)
-#define BLE_LL_S_INIT                   (0x00000000040)
-#define BLE_LL_S_SLAVE                  (0x00000000080)
-#define BLE_LL_S_NCA_PS                 (0x00000000100)
-#define BLE_LL_S_SA_PS                  (0x00000000200)
-#define BLE_LL_S_CA_PS                  (0x00000000400)
-#define BLE_LL_S_HDCA_PS                (0x00000000800)
-#define BLE_LL_S_NCA_AS                 (0x00000001000)
-#define BLE_LL_S_SA_AS                  (0x00000002000)
-#define BLE_LL_S_CA_AS                  (0x00000004000)
-#define BLE_LL_S_HDCA_AS                (0x00000008000)
-#define BLE_LL_S_NCA_INIT               (0x00000010000)
-#define BLE_LL_S_SA_INIT                (0x00000020000)
-#define BLE_LL_S_NCA_MASTER             (0x00000040000)
-#define BLE_LL_S_SA_MASTER              (0x00000080000)
-#define BLE_LL_S_NCA_SLAVE              (0x00000100000)
-#define BLE_LL_S_SA_SLAVE               (0x00000200000)
-#define BLE_LL_S_PS_INIT                (0x00000400000)
-#define BLE_LL_S_AS_INIT                (0x00000800000)
-#define BLE_LL_S_PS_MASTER              (0x00001000000)
-#define BLE_LL_S_AS_MASTER              (0x00002000000)
-#define BLE_LL_S_PS_SLAVE               (0x00004000000)
-#define BLE_LL_S_AS_SLAVE               (0x00008000000)
-#define BLE_LL_S_INIT_MASTER            (0x00010000000)
-#define BLE_LL_S_LDCA                   (0x00020000000)
-#define BLE_LL_S_LDCA_PS                (0x00040000000)
-#define BLE_LL_S_LDCA_AS                (0x00080000000)
-#define BLE_LL_S_CA_INIT                (0x00100000000)
-#define BLE_LL_S_HDCA_INIT              (0x00200000000)
-#define BLE_LL_S_LDCA_INIT              (0x00400000000)
-#define BLE_LL_S_CA_MASTER              (0x00800000000)
-#define BLE_LL_S_HDCA_MASTER            (0x01000000000)
-#define BLE_LL_S_LDCA_MASTER            (0x02000000000)
-#define BLE_LL_S_CA_SLAVE               (0x04000000000)
-#define BLE_LL_S_HDCA_SLAVE             (0x08000000000)
-#define BLE_LL_S_LDCA_SLAVE             (0x10000000000)
-#define BLE_LL_S_INIT_SLAVE             (0x20000000000)
-
-#define BLE_LL_SUPPORTED_STATES             \
-(                                           \
-    BLE_LL_S_NCA                    |       \
-    BLE_LL_S_SA                     |       \
-    BLE_LL_S_CA                     |       \
-    BLE_LL_S_HDCA                   |       \
-    BLE_LL_S_PS                     |       \
-    BLE_LL_S_AS                     |       \
-    BLE_LL_S_INIT                   |       \
-    BLE_LL_S_SLAVE                  |       \
-    BLE_LL_S_NCA_PS                 |       \
-    BLE_LL_S_SA_PS                  |       \
-    BLE_LL_S_CA_PS                  |       \
-    BLE_LL_S_HDCA_PS                |       \
-    BLE_LL_S_NCA_AS                 |       \
-    BLE_LL_S_SA_AS                  |       \
-    BLE_LL_S_CA_AS                  |       \
-    BLE_LL_S_HDCA_AS                |       \
-    BLE_LL_S_NCA_INIT               |       \
-    BLE_LL_S_SA_INIT                |       \
-    BLE_LL_S_NCA_MASTER             |       \
-    BLE_LL_S_SA_MASTER              |       \
-    BLE_LL_S_NCA_SLAVE              |       \
-    BLE_LL_S_SA_SLAVE               |       \
-    BLE_LL_S_PS_INIT                |       \
-    BLE_LL_S_AS_INIT                |       \
-    BLE_LL_S_PS_MASTER              |       \
-    BLE_LL_S_AS_MASTER              |       \
-    BLE_LL_S_PS_SLAVE               |       \
-    BLE_LL_S_AS_SLAVE               |       \
-    BLE_LL_S_INIT_MASTER            |       \
-    BLE_LL_S_LDCA                   |       \
-    BLE_LL_S_LDCA_PS                |       \
-    BLE_LL_S_LDCA_AS                |       \
-    BLE_LL_S_CA_INIT                |       \
-    BLE_LL_S_HDCA_INIT              |       \
-    BLE_LL_S_LDCA_INIT              |       \
-    BLE_LL_S_CA_MASTER              |       \
-    BLE_LL_S_HDCA_MASTER            |       \
-    BLE_LL_S_LDCA_MASTER            |       \
-    BLE_LL_S_CA_SLAVE               |       \
-    BLE_LL_S_HDCA_SLAVE             |       \
-    BLE_LL_S_LDCA_SLAVE             |       \
-    BLE_LL_S_INIT_SLAVE)
-
-/* The global BLE LL data object */
-struct ble_ll_obj g_ble_ll_data;
-
-/* Global link layer statistics */
-STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
-STATS_NAME_START(ble_ll_stats)
-    STATS_NAME(ble_ll_stats, hci_cmds)
-    STATS_NAME(ble_ll_stats, hci_cmd_errs)
-    STATS_NAME(ble_ll_stats, hci_events_sent)
-    STATS_NAME(ble_ll_stats, bad_ll_state)
-    STATS_NAME(ble_ll_stats, bad_acl_hdr)
-    STATS_NAME(ble_ll_stats, no_bufs)
-    STATS_NAME(ble_ll_stats, rx_adv_pdu_crc_ok)
-    STATS_NAME(ble_ll_stats, rx_adv_pdu_crc_err)
-    STATS_NAME(ble_ll_stats, rx_adv_bytes_crc_ok)
-    STATS_NAME(ble_ll_stats, rx_adv_bytes_crc_err)
-    STATS_NAME(ble_ll_stats, rx_data_pdu_crc_ok)
-    STATS_NAME(ble_ll_stats, rx_data_pdu_crc_err)
-    STATS_NAME(ble_ll_stats, rx_data_bytes_crc_ok)
-    STATS_NAME(ble_ll_stats, rx_data_bytes_crc_err)
-    STATS_NAME(ble_ll_stats, rx_adv_malformed_pkts)
-    STATS_NAME(ble_ll_stats, rx_adv_ind)
-    STATS_NAME(ble_ll_stats, rx_adv_direct_ind)
-    STATS_NAME(ble_ll_stats, rx_adv_nonconn_ind)
-    STATS_NAME(ble_ll_stats, rx_adv_ext_ind)
-    STATS_NAME(ble_ll_stats, rx_scan_reqs)
-    STATS_NAME(ble_ll_stats, rx_scan_rsps)
-    STATS_NAME(ble_ll_stats, rx_connect_reqs)
-    STATS_NAME(ble_ll_stats, rx_scan_ind)
-    STATS_NAME(ble_ll_stats, rx_aux_connect_rsp)
-    STATS_NAME(ble_ll_stats, adv_txg)
-    STATS_NAME(ble_ll_stats, adv_late_starts)
-    STATS_NAME(ble_ll_stats, adv_resched_pdu_fail)
-    STATS_NAME(ble_ll_stats, adv_drop_event)
-    STATS_NAME(ble_ll_stats, sched_state_conn_errs)
-    STATS_NAME(ble_ll_stats, sched_state_adv_errs)
-    STATS_NAME(ble_ll_stats, scan_starts)
-    STATS_NAME(ble_ll_stats, scan_stops)
-    STATS_NAME(ble_ll_stats, scan_req_txf)
-    STATS_NAME(ble_ll_stats, scan_req_txg)
-    STATS_NAME(ble_ll_stats, scan_rsp_txg)
-    STATS_NAME(ble_ll_stats, aux_missed_adv)
-    STATS_NAME(ble_ll_stats, aux_scheduled)
-    STATS_NAME(ble_ll_stats, aux_received)
-    STATS_NAME(ble_ll_stats, aux_fired_for_read)
-    STATS_NAME(ble_ll_stats, aux_conn_req_tx)
-    STATS_NAME(ble_ll_stats, aux_conn_rsp_tx)
-    STATS_NAME(ble_ll_stats, aux_conn_rsp_err)
-    STATS_NAME(ble_ll_stats, aux_scan_req_tx)
-    STATS_NAME(ble_ll_stats, aux_scan_rsp_err)
-    STATS_NAME(ble_ll_stats, aux_chain_cnt)
-    STATS_NAME(ble_ll_stats, aux_chain_err)
-    STATS_NAME(ble_ll_stats, adv_evt_dropped)
-    STATS_NAME(ble_ll_stats, scan_timer_stopped)
-    STATS_NAME(ble_ll_stats, scan_timer_restarted)
-STATS_NAME_END(ble_ll_stats)
-
-static void ble_ll_event_rx_pkt(struct os_event *ev);
-static void ble_ll_event_tx_pkt(struct os_event *ev);
-static void ble_ll_event_dbuf_overflow(struct os_event *ev);
-
-/* The BLE LL task data structure */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-/* TODO: This is for testing. Check it we really need it */
-#define BLE_LL_STACK_SIZE   (128)
-#else
-#define BLE_LL_STACK_SIZE   (90)
-#endif
-
-struct os_task g_ble_ll_task;
-
-OS_TASK_STACK_DEFINE(g_ble_ll_stack, BLE_LL_STACK_SIZE);
-
-/** Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
-
-/** Our random address */
-uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
-
-static const uint16_t g_ble_ll_pdu_header_tx_time[BLE_PHY_NUM_MODE] =
-{
-    [BLE_PHY_MODE_1M] =
-            (BLE_LL_PREAMBLE_LEN + BLE_LL_ACC_ADDR_LEN + BLE_LL_CRC_LEN +
-                    BLE_LL_PDU_HDR_LEN) << 3,
-    [BLE_PHY_MODE_2M] =
-            (BLE_LL_PREAMBLE_LEN * 2 + BLE_LL_ACC_ADDR_LEN + BLE_LL_CRC_LEN +
-                    BLE_LL_PDU_HDR_LEN) << 2,
-    /* For Coded PHY we have exact TX times provided by specification:
-     * - Preamble, Access Address, CI, TERM1 (always coded as S=8)
-     * - PDU, CRC, TERM2 (coded as S=2 or S=8)
-     * (Vol 6, Part B, 2.2).
-     */
-    [BLE_PHY_MODE_CODED_125KBPS] =
-            (80 + 256 + 16 + 24 + 8 * (BLE_LL_PDU_HDR_LEN * 8 + 24 + 3)),
-    [BLE_PHY_MODE_CODED_500KBPS] =
-            (80 + 256 + 16 + 24 + 2 * (BLE_LL_PDU_HDR_LEN * 8 + 24 + 3)),
-};
-
-/* XXX: temporary logging until we transition to real logging */
-#ifdef BLE_LL_LOG
-struct ble_ll_log
-{
-    uint8_t log_id;
-    uint8_t log_a8;
-    uint16_t log_a16;
-    uint32_t log_a32;
-    uint32_t cputime;
-
-};
-
-#define BLE_LL_LOG_LEN  (256)
-
-static bssnz_t struct ble_ll_log g_ble_ll_log[BLE_LL_LOG_LEN];
-static uint8_t g_ble_ll_log_index;
-
-void
-ble_ll_log(uint8_t id, uint8_t arg8, uint16_t arg16, uint32_t arg32)
-{
-    os_sr_t sr;
-    struct ble_ll_log *le;
-
-    OS_ENTER_CRITICAL(sr);
-    le = &g_ble_ll_log[g_ble_ll_log_index];
-    le->cputime = os_cputime_get32();
-    le->log_id = id;
-    le->log_a8 = arg8;
-    le->log_a16 = arg16;
-    le->log_a32 = arg32;
-    ++g_ble_ll_log_index;
-    if (g_ble_ll_log_index == BLE_LL_LOG_LEN) {
-        g_ble_ll_log_index = 0;
-    }
-    OS_EXIT_CRITICAL(sr);
-}
-#endif
-
-/**
- * Counts the number of advertising PDU's received, by type. For advertising
- * PDU's that contain a destination address, we still count these packets even
- * if they are not for us.
- *
- * @param pdu_type
- */
-static void
-ble_ll_count_rx_adv_pdus(uint8_t pdu_type)
-{
-    /* Count received packet types  */
-    switch (pdu_type) {
-    case BLE_ADV_PDU_TYPE_ADV_EXT_IND:
-        STATS_INC(ble_ll_stats, rx_adv_ext_ind);
-        break;
-    case BLE_ADV_PDU_TYPE_ADV_IND:
-        STATS_INC(ble_ll_stats, rx_adv_ind);
-        break;
-    case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND:
-        STATS_INC(ble_ll_stats, rx_adv_direct_ind);
-        break;
-    case BLE_ADV_PDU_TYPE_ADV_NONCONN_IND:
-        STATS_INC(ble_ll_stats, rx_adv_nonconn_ind);
-        break;
-    case BLE_ADV_PDU_TYPE_SCAN_REQ:
-        STATS_INC(ble_ll_stats, rx_scan_reqs);
-        break;
-    case BLE_ADV_PDU_TYPE_SCAN_RSP:
-        STATS_INC(ble_ll_stats, rx_scan_rsps);
-        break;
-    case BLE_ADV_PDU_TYPE_CONNECT_REQ:
-        STATS_INC(ble_ll_stats, rx_connect_reqs);
-        break;
-    case BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP:
-        STATS_INC(ble_ll_stats, rx_aux_connect_rsp);
-        break;
-    case BLE_ADV_PDU_TYPE_ADV_SCAN_IND:
-        STATS_INC(ble_ll_stats, rx_scan_ind);
-        break;
-    default:
-        break;
-    }
-}
-
-/**
- * Allocate a pdu (chain) for reception.
- *
- * @param len
- *
- * @return struct os_mbuf*
- */
-struct os_mbuf *
-ble_ll_rxpdu_alloc(uint16_t len)
-{
-    uint16_t mb_bytes;
-    struct os_mbuf *m;
-    struct os_mbuf *n;
-    struct os_mbuf *p;
-    struct os_mbuf_pkthdr *pkthdr;
-
-    p = os_msys_get_pkthdr(len, sizeof(struct ble_mbuf_hdr));
-    if (!p) {
-        goto rxpdu_alloc_exit;
-    }
-
-    /* Set packet length */
-    pkthdr = OS_MBUF_PKTHDR(p);
-    pkthdr->omp_len = len;
-
-    /*
-     * NOTE: first mbuf in chain will have data pre-pended to it so we adjust
-     * m_data by a word.
-     */
-    p->om_data += 4;
-    mb_bytes = (p->om_omp->omp_databuf_len - p->om_pkthdr_len - 4);
-
-    if (mb_bytes < len) {
-        n = p;
-        len -= mb_bytes;
-        while (len) {
-            m = os_msys_get(len, 0);
-            if (!m) {
-                os_mbuf_free_chain(p);
-                p = NULL;
-                goto rxpdu_alloc_exit;
-            }
-            /* Chain new mbuf to existing chain */
-            SLIST_NEXT(n, om_next) = m;
-            n = m;
-            mb_bytes = m->om_omp->omp_databuf_len;
-            if (mb_bytes >= len) {
-                len = 0;
-            } else {
-                len -= mb_bytes;
-            }
-        }
-    }
-
-
-rxpdu_alloc_exit:
-    if (!p) {
-        STATS_INC(ble_ll_stats, no_bufs);
-    }
-    return p;
-}
-
-int
-ble_ll_chk_txrx_octets(uint16_t octets)
-{
-    int rc;
-
-    if ((octets < BLE_LL_CONN_SUPP_BYTES_MIN) ||
-        (octets > BLE_LL_CONN_SUPP_BYTES_MAX)) {
-        rc = 0;
-    } else {
-        rc = 1;
-    }
-
-    return rc;
-}
-
-int
-ble_ll_chk_txrx_time(uint16_t time)
-{
-    int rc;
-
-    if ((time < BLE_LL_CONN_SUPP_TIME_MIN) ||
-        (time > BLE_LL_CONN_SUPP_TIME_MAX)) {
-        rc = 0;
-    } else {
-        rc = 1;
-    }
-
-    return rc;
-}
-
-/**
- * Checks to see if the address is a resolvable private address.
- *
- * NOTE: the addr_type parameter will be 0 if the address is public;
- * any other value is random (all non-zero values).
- *
- * @param addr
- * @param addr_type Public (zero) or Random (non-zero) address
- *
- * @return int
- */
-int
-ble_ll_is_rpa(uint8_t *addr, uint8_t addr_type)
-{
-    int rc;
-
-    if (addr_type && ((addr[5] & 0xc0) == 0x40)) {
-        rc = 1;
-    } else {
-        rc = 0;
-    }
-    return rc;
-}
-
-/* Checks to see that the device is a valid random address */
-int
-ble_ll_is_valid_random_addr(uint8_t *addr)
-{
-    int i;
-    int rc;
-    uint16_t sum;
-    uint8_t addr_type;
-
-    /* Make sure all bits are neither one nor zero */
-    sum = 0;
-    for (i = 0; i < (BLE_DEV_ADDR_LEN -1); ++i) {
-        sum += addr[i];
-    }
-    sum += addr[5] & 0x3f;
-
-    if ((sum == 0) || (sum == ((5*255) + 0x3f))) {
-        return 0;
-    }
-
-    /* Get the upper two bits of the address */
-    rc = 1;
-    addr_type = addr[5] & 0xc0;
-    if (addr_type == 0xc0) {
-        /* Static random address. No other checks needed */
-    } else if (addr_type == 0x40) {
-        /* Resolvable */
-        sum = addr[3] + addr[4] + (addr[5] & 0x3f);
-        if ((sum == 0) || (sum == (255 + 255 + 0x3f))) {
-            rc = 0;
-        }
-    } else if (addr_type == 0) {
-        /* non-resolvable. Cant be equal to public */
-        if (!memcmp(g_dev_addr, addr, BLE_DEV_ADDR_LEN)) {
-            rc = 0;
-        }
-    } else {
-        /* Invalid upper two bits */
-        rc = 0;
-    }
-
-    return rc;
-}
-
-/**
- * Called from the HCI command parser when the set random address command
- * is received.
- *
- * Context: Link Layer task (HCI command parser)
- *
- * @param addr Pointer to address
- *
- * @return int 0: success
- */
-int
-ble_ll_set_random_addr(uint8_t *addr)
-{
-    int rc;
-
-    rc = BLE_ERR_INV_HCI_CMD_PARMS;
-    if (ble_ll_is_valid_random_addr(addr)) {
-        memcpy(g_random_addr, addr, BLE_DEV_ADDR_LEN);
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    return rc;
-}
-
-/**
- * Checks to see if an address is our device address (either public or
- * random)
- *
- * @param addr
- * @param addr_type
- *
- * @return int 0: not our device address. 1: is our device address
- */
-int
-ble_ll_is_our_devaddr(uint8_t *addr, int addr_type)
-{
-    int rc;
-    uint8_t *our_addr;
-
-    if (addr_type) {
-        our_addr = g_random_addr;
-    } else {
-        our_addr = g_dev_addr;
-    }
-
-    rc = 0;
-    if (!memcmp(our_addr, addr, BLE_DEV_ADDR_LEN)) {
-        rc = 1;
-    }
-
-    return rc;
-}
-
-/**
- * Wait for response timeout function
- *
- * Context: interrupt (ble scheduler)
- *
- * @param arg
- */
-void
-ble_ll_wfr_timer_exp(void *arg)
-{
-    int rx_start;
-    uint8_t lls;
-
-    rx_start = ble_phy_rx_started();
-    lls = g_ble_ll_data.ll_state;
-
-    ble_ll_log(BLE_LL_LOG_ID_WFR_EXP, lls, ble_phy_xcvr_state_get(),
-               (uint32_t)rx_start);
-
-    /* If we have started a reception, there is nothing to do here */
-    if (!rx_start) {
-        switch (lls) {
-        case BLE_LL_STATE_ADV:
-            ble_ll_adv_wfr_timer_exp();
-            break;
-        case BLE_LL_STATE_CONNECTION:
-            ble_ll_conn_wfr_timer_exp();
-            break;
-        case BLE_LL_STATE_SCANNING:
-            ble_ll_scan_wfr_timer_exp();
-            break;
-        case BLE_LL_STATE_INITIATING:
-            ble_ll_conn_init_wfr_timer_exp();
-            break;
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-        case BLE_LL_STATE_DTM:
-            ble_ll_dtm_wfr_timer_exp();
-            break;
-#endif
-        default:
-            break;
-        }
-    }
-}
-
-/**
- * Enable the wait for response timer.
- *
- * Context: Interrupt.
- *
- * @param cputime
- * @param wfr_cb
- * @param arg
- */
-void
-ble_ll_wfr_enable(uint32_t cputime)
-{
-}
-
-/**
- * Disable the wait for response timer
- */
-void
-ble_ll_wfr_disable(void)
-{
-}
-
-/**
- * ll tx pkt in proc
- *
- * Process ACL data packet input from host
- *
- * Context: Link layer task
- *
- */
-static void
-ble_ll_tx_pkt_in(void)
-{
-    uint16_t handle;
-    uint16_t length;
-    uint16_t pb;
-    struct os_mbuf_pkthdr *pkthdr;
-    struct os_mbuf *om;
-
-    /* Drain all packets off the queue */
-    while (STAILQ_FIRST(&g_ble_ll_data.ll_tx_pkt_q)) {
-        /* Get mbuf pointer from packet header pointer */
-        pkthdr = STAILQ_FIRST(&g_ble_ll_data.ll_tx_pkt_q);
-        om = (struct os_mbuf *)((uint8_t *)pkthdr - sizeof(struct os_mbuf));
-
-        /* Remove from queue */
-        STAILQ_REMOVE_HEAD(&g_ble_ll_data.ll_tx_pkt_q, omp_next);
-
-        /* Strip HCI ACL header to get handle and length */
-        handle = get_le16(om->om_data);
-        length = get_le16(om->om_data + 2);
-        os_mbuf_adj(om, sizeof(struct hci_data_hdr));
-
-        /* Do some basic error checking */
-        pb = handle & 0x3000;
-        if ((pkthdr->omp_len != length) || (pb > 0x1000) || (length == 0)) {
-            /* This is a bad ACL packet. Count a stat and free it */
-            STATS_INC(ble_ll_stats, bad_acl_hdr);
-            os_mbuf_free_chain(om);
-            continue;
-        }
-
-        /* Hand to connection state machine */
-        ble_ll_conn_tx_pkt_in(om, handle, length);
-    }
-}
-
-/**
- * Count Link Layer statistics for received PDUs
- *
- * Context: Link layer task
- *
- * @param hdr
- * @param len
- */
-static void
-ble_ll_count_rx_stats(struct ble_mbuf_hdr *hdr, uint16_t len, uint8_t pdu_type)
-{
-    uint8_t crcok;
-    bool connection_data;
-
-    crcok = BLE_MBUF_HDR_CRC_OK(hdr);
-    connection_data = (BLE_MBUF_HDR_RX_STATE(hdr) == BLE_LL_STATE_CONNECTION);
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-    /* Reuse connection stats for DTM */
-    connection_data = (BLE_MBUF_HDR_RX_STATE(hdr) == BLE_LL_STATE_DTM);
-#endif
-
-    if (crcok) {
-        if (connection_data) {
-            STATS_INC(ble_ll_stats, rx_data_pdu_crc_ok);
-            STATS_INCN(ble_ll_stats, rx_data_bytes_crc_ok, len);
-        } else {
-            STATS_INC(ble_ll_stats, rx_adv_pdu_crc_ok);
-            STATS_INCN(ble_ll_stats, rx_adv_bytes_crc_ok, len);
-            ble_ll_count_rx_adv_pdus(pdu_type);
-        }
-    } else {
-        if (connection_data) {
-            STATS_INC(ble_ll_stats, rx_data_pdu_crc_err);
-            STATS_INCN(ble_ll_stats, rx_data_bytes_crc_err, len);
-        } else {
-            STATS_INC(ble_ll_stats, rx_adv_pdu_crc_err);
-            STATS_INCN(ble_ll_stats, rx_adv_bytes_crc_err, len);
-        }
-    }
-}
-
-/**
- * ll rx pkt in
- *
- * Process received packet from PHY.
- *
- * Context: Link layer task
- *
- */
-static void
-ble_ll_rx_pkt_in(void)
-{
-    os_sr_t sr;
-    uint8_t pdu_type;
-    uint8_t *rxbuf;
-    struct os_mbuf_pkthdr *pkthdr;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct os_mbuf *m;
-
-    /* Drain all packets off the queue */
-    while (STAILQ_FIRST(&g_ble_ll_data.ll_rx_pkt_q)) {
-        /* Get mbuf pointer from packet header pointer */
-        pkthdr = STAILQ_FIRST(&g_ble_ll_data.ll_rx_pkt_q);
-        m = (struct os_mbuf *)((uint8_t *)pkthdr - sizeof(struct os_mbuf));
-
-        /* Remove from queue */
-        OS_ENTER_CRITICAL(sr);
-        STAILQ_REMOVE_HEAD(&g_ble_ll_data.ll_rx_pkt_q, omp_next);
-        OS_EXIT_CRITICAL(sr);
-
-        /* Note: pdu type wont get used unless this is an advertising pdu */
-        ble_hdr = BLE_MBUF_HDR_PTR(m);
-        rxbuf = m->om_data;
-        pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
-        ble_ll_count_rx_stats(ble_hdr, pkthdr->omp_len, pdu_type);
-
-        /* Process the data or advertising pdu */
-        /* Process the PDU */
-        switch (BLE_MBUF_HDR_RX_STATE(ble_hdr)) {
-        case BLE_LL_STATE_CONNECTION:
-            ble_ll_conn_rx_data_pdu(m, ble_hdr);
-            /* m is going to be free by function above */
-            m = NULL;
-            break;
-        case BLE_LL_STATE_ADV:
-            ble_ll_adv_rx_pkt_in(pdu_type, rxbuf, ble_hdr);
-            break;
-        case BLE_LL_STATE_SCANNING:
-            ble_ll_scan_rx_pkt_in(pdu_type, m, ble_hdr);
-            break;
-        case BLE_LL_STATE_INITIATING:
-            ble_ll_init_rx_pkt_in(pdu_type, rxbuf, ble_hdr);
-            break;
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-        case BLE_LL_STATE_DTM:
-            ble_ll_dtm_rx_pkt_in(m, ble_hdr);
-            break;
-#endif
-        default:
-            /* Any other state should never occur */
-            STATS_INC(ble_ll_stats, bad_ll_state);
-            break;
-        }
-        if (m) {
-            /* Free the packet buffer */
-            os_mbuf_free_chain(m);
-        }
-    }
-}
-
-/**
- * Called to put a packet on the Link Layer receive packet queue.
- *
- * @param rxpdu Pointer to received PDU
- */
-void
-ble_ll_rx_pdu_in(struct os_mbuf *rxpdu)
-{
-    struct os_mbuf_pkthdr *pkthdr;
-
-    pkthdr = OS_MBUF_PKTHDR(rxpdu);
-    STAILQ_INSERT_TAIL(&g_ble_ll_data.ll_rx_pkt_q, pkthdr, omp_next);
-    os_eventq_put(&g_ble_ll_data.ll_evq, &g_ble_ll_data.ll_rx_pkt_ev);
-}
-
-/**
- * Called to put a packet on the Link Layer transmit packet queue.
- *
- * @param txpdu Pointer to transmit packet
- */
-void
-ble_ll_acl_data_in(struct os_mbuf *txpkt)
-{
-    os_sr_t sr;
-    struct os_mbuf_pkthdr *pkthdr;
-
-    pkthdr = OS_MBUF_PKTHDR(txpkt);
-    OS_ENTER_CRITICAL(sr);
-    STAILQ_INSERT_TAIL(&g_ble_ll_data.ll_tx_pkt_q, pkthdr, omp_next);
-    OS_EXIT_CRITICAL(sr);
-    os_eventq_put(&g_ble_ll_data.ll_evq, &g_ble_ll_data.ll_tx_pkt_ev);
-}
-
-/**
- * Called to post event to Link Layer when a data buffer overflow has
- * occurred.
- *
- * Context: Interrupt
- *
- */
-void
-ble_ll_data_buffer_overflow(void)
-{
-    os_eventq_put(&g_ble_ll_data.ll_evq, &g_ble_ll_data.ll_dbuf_overflow_ev);
-}
-
-/**
- * Called when a HW error occurs.
- *
- * Context: Interrupt
- */
-void
-ble_ll_hw_error(void)
-{
-    os_callout_reset(&g_ble_ll_data.ll_hw_err_timer, 0);
-}
-
-/**
- * Called when the HW error timer expires.
- *
- * @param arg
- */
-static void
-ble_ll_hw_err_timer_cb(struct os_event *ev)
-{
-    if (ble_ll_hci_ev_hw_err(BLE_HW_ERR_HCI_SYNC_LOSS)) {
-        /*
-         * Restart callout if failed to allocate event. Try to allocate an
-         * event every 50 milliseconds (or each OS tick if a tick is longer
-         * than 100 msecs).
-         */
-        os_callout_reset(&g_ble_ll_data.ll_hw_err_timer,
-                         OS_TICKS_PER_SEC / 20);
-    }
-}
-
-/**
- * Called upon start of received PDU
- *
- * Context: Interrupt
- *
- * @param rxpdu
- *        chan
- *
- * @return int
- *   < 0: A frame we dont want to receive.
- *   = 0: Continue to receive frame. Dont go from rx to tx
- *   > 0: Continue to receive frame and go from rx to tx when done
- */
-int
-ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan, struct ble_mbuf_hdr *rxhdr)
-{
-    int rc;
-    uint8_t pdu_type;
-
-    ble_ll_log(BLE_LL_LOG_ID_RX_START, chan, rxhdr->rem_usecs,
-               rxhdr->beg_cputime);
-
-    /* Advertising channel PDU */
-    pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
-
-    switch (g_ble_ll_data.ll_state) {
-        case BLE_LL_STATE_CONNECTION:
-        rc = ble_ll_conn_rx_isr_start(rxhdr, ble_phy_access_addr_get());
-        break;
-    case BLE_LL_STATE_ADV:
-        rc = ble_ll_adv_rx_isr_start(pdu_type);
-        break;
-    case BLE_LL_STATE_INITIATING:
-        rc = ble_ll_init_rx_isr_start(pdu_type, rxhdr);
-        break;
-    case BLE_LL_STATE_SCANNING:
-        rc = ble_ll_scan_rx_isr_start(pdu_type, &rxhdr->rxinfo.flags);
-        break;
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-    case BLE_LL_STATE_DTM:
-        rc = ble_ll_dtm_rx_isr_start(rxhdr, ble_phy_access_addr_get());
-        break;
-#endif
-    default:
-        /* Should not be in this state! */
-        rc = -1;
-        STATS_INC(ble_ll_stats, bad_ll_state);
-        break;
-    }
-
-    return rc;
-}
-
-/**
- * Called by the PHY when a receive packet has ended.
- *
- * NOTE: Called from interrupt context!
- *
- * @param rxbuf Pointer to received PDU data
- *        rxhdr Pointer to BLE header of received mbuf
- *
- * @return int
- *       < 0: Disable the phy after reception.
- *      == 0: Success. Do not disable the PHY.
- *       > 0: Do not disable PHY as that has already been done.
- */
-int
-ble_ll_rx_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
-{
-    int rc;
-    int badpkt;
-    uint8_t pdu_type;
-    uint8_t len;
-    uint8_t crcok;
-    struct os_mbuf *rxpdu;
-
-    /* Get CRC status from BLE header */
-    crcok = BLE_MBUF_HDR_CRC_OK(rxhdr);
-
-    ble_ll_log(BLE_LL_LOG_ID_RX_END, rxbuf[0],
-               ((uint16_t)rxhdr->rxinfo.flags << 8) | rxbuf[1],
-               rxhdr->beg_cputime);
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1
-    if (BLE_MBUF_HDR_RX_STATE(rxhdr) == BLE_LL_STATE_DTM) {
-        rc = ble_ll_dtm_rx_isr_end(rxbuf, rxhdr);
-        return rc;
-    }
-#endif
-
-    if (BLE_MBUF_HDR_RX_STATE(rxhdr) == BLE_LL_STATE_CONNECTION) {
-        rc = ble_ll_conn_rx_isr_end(rxbuf, rxhdr);
-        return rc;
-    }
-
-    /* Get advertising PDU type and length */
-    pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
-    len = rxbuf[1];
-
-    /* If the CRC checks, make sure lengths check! */
-    badpkt = 0;
-    if (crcok) {
-        switch (pdu_type) {
-        case BLE_ADV_PDU_TYPE_SCAN_REQ:
-        case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND:
-            if (len != BLE_SCAN_REQ_LEN) {
-                badpkt = 1;
-            }
-            break;
-        case BLE_ADV_PDU_TYPE_SCAN_RSP:
-        case BLE_ADV_PDU_TYPE_ADV_IND:
-        case BLE_ADV_PDU_TYPE_ADV_SCAN_IND:
-        case BLE_ADV_PDU_TYPE_ADV_NONCONN_IND:
-            if ((len < BLE_DEV_ADDR_LEN) || (len > BLE_ADV_SCAN_IND_MAX_LEN)) {
-                badpkt = 1;
-            }
-            break;
-        case BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP:
-            break;
-        case BLE_ADV_PDU_TYPE_ADV_EXT_IND:
-            break;
-        case BLE_ADV_PDU_TYPE_CONNECT_REQ:
-            if (len != BLE_CONNECT_REQ_LEN) {
-                badpkt = 1;
-            }
-            break;
-        default:
-            badpkt = 1;
-            break;
-        }
-
-        /* If this is a malformed packet, just kill it here */
-        if (badpkt) {
-            STATS_INC(ble_ll_stats, rx_adv_malformed_pkts);
-        }
-    }
-
-    /* Hand packet to the appropriate state machine (if crc ok) */
-    rxpdu = NULL;
-    switch (BLE_MBUF_HDR_RX_STATE(rxhdr)) {
-    case BLE_LL_STATE_ADV:
-        if (!badpkt) {
-            rxpdu = ble_ll_rxpdu_alloc(len + BLE_LL_PDU_HDR_LEN);
-            if (rxpdu) {
-                ble_phy_rxpdu_copy(rxbuf, rxpdu);
-            }
-        }
-        rc = ble_ll_adv_rx_isr_end(pdu_type, rxpdu, crcok);
-        break;
-    case BLE_LL_STATE_SCANNING:
-        if (!badpkt) {
-            rxpdu = ble_ll_rxpdu_alloc(len + BLE_LL_PDU_HDR_LEN);
-            if (rxpdu) {
-                ble_phy_rxpdu_copy(rxbuf, rxpdu);
-            }
-        }
-        rc = ble_ll_scan_rx_isr_end(rxpdu, crcok);
-        break;
-    case BLE_LL_STATE_INITIATING:
-        rc = ble_ll_init_rx_isr_end(rxbuf, crcok, rxhdr);
-        break;
-    default:
-        rc = -1;
-        STATS_INC(ble_ll_stats, bad_ll_state);
-        break;
-    }
-
-    /* Hand packet up to higher layer (regardless of CRC failure) */
-    if (rxpdu) {
-        ble_ll_rx_pdu_in(rxpdu);
-    }
-
-    return rc;
-}
-
-uint8_t
-ble_ll_tx_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct os_mbuf *txpdu;
-    struct ble_mbuf_hdr *ble_hdr;
-
-    txpdu = pducb_arg;
-    assert(txpdu);
-    ble_hdr = BLE_MBUF_HDR_PTR(txpdu);
-
-    os_mbuf_copydata(txpdu, ble_hdr->txinfo.offset, ble_hdr->txinfo.pyld_len,
-                     dptr);
-
-    *hdr_byte = ble_hdr->txinfo.hdr_byte;
-
-    return ble_hdr->txinfo.pyld_len;
-}
-
-static void
-ble_ll_event_rx_pkt(struct os_event *ev)
-{
-    ble_ll_rx_pkt_in();
-}
-
-static void
-ble_ll_event_tx_pkt(struct os_event *ev)
-{
-    ble_ll_tx_pkt_in();
-}
-
-static void
-ble_ll_event_dbuf_overflow(struct os_event *ev)
-{
-    ble_ll_hci_ev_databuf_overflow();
-}
-
-static void
-ble_ll_event_comp_pkts(struct os_event *ev)
-{
-    ble_ll_conn_num_comp_pkts_event_send(NULL);
-}
-
-/**
- * Link Layer task.
- *
- * This is the task that runs the Link Layer.
- *
- * @param arg
- */
-void
-ble_ll_task(void *arg)
-{
-    /* Init ble phy */
-    ble_phy_init();
-
-    /* Set output power to 1mW (0 dBm) */
-    ble_phy_txpwr_set(MYNEWT_VAL(BLE_LL_TX_PWR_DBM));
-
-    /* Tell the host that we are ready to receive packets */
-    ble_ll_hci_send_noop();
-
-    ble_ll_rand_start();
-
-    while (1) {
-        os_eventq_run(&g_ble_ll_data.ll_evq);
-    }
-}
-
-/**
- * ble ll state set
- *
- * Called to set the current link layer state.
- *
- * Context: Interrupt and Link Layer task
- *
- * @param ll_state
- */
-void
-ble_ll_state_set(uint8_t ll_state)
-{
-    g_ble_ll_data.ll_state = ll_state;
-}
-
-/**
- * ble ll state get
- *
- * Called to get the current link layer state.
- *
- * Context: Link Layer task (can be called from interrupt context though).
- *
- * @return ll_state
- */
-uint8_t
-ble_ll_state_get(void)
-{
-    return g_ble_ll_data.ll_state;
-}
-
-/**
- * ble ll event send
- *
- * Send an event to the Link Layer task
- *
- * @param ev Event to add to the Link Layer event queue.
- */
-void
-ble_ll_event_send(struct os_event *ev)
-{
-    os_eventq_put(&g_ble_ll_data.ll_evq, ev);
-}
-
-/**
- * Returns the features supported by the link layer
- *
- * @return uint8_t bitmask of supported features.
- */
-uint64_t
-ble_ll_read_supp_states(void)
-{
-    return BLE_LL_SUPPORTED_STATES;
-}
-
-/**
- * Returns the features supported by the link layer
- *
- * @return uint32_t bitmask of supported features.
- */
-uint32_t
-ble_ll_read_supp_features(void)
-{
-    return g_ble_ll_data.ll_supp_features;
-}
-
-/**
- * Flush a link layer packet queue.
- *
- * @param pktq
- */
-static void
-ble_ll_flush_pkt_queue(struct ble_ll_pkt_q *pktq)
-{
-    struct os_mbuf_pkthdr *pkthdr;
-    struct os_mbuf *om;
-
-    /* FLush all packets from Link layer queues */
-    while (STAILQ_FIRST(pktq)) {
-        /* Get mbuf pointer from packet header pointer */
-        pkthdr = STAILQ_FIRST(pktq);
-        om = OS_MBUF_PKTHDR_TO_MBUF(pkthdr);
-
-        /* Remove from queue and free the mbuf */
-        STAILQ_REMOVE_HEAD(pktq, omp_next);
-        os_mbuf_free_chain(om);
-    }
-}
-
-/**
- * Called to initialize a mbuf used by the controller
- *
- * NOTE: this is only used when the mbuf is created by the controller;
- * it should not be used for data packets (ACL data packets) that come from
- * the host. This routine assumes that the entire pdu length can fit in
- * one mbuf contiguously.
- *
- * @param m
- * @param pdulen
- * @param hdr
- */
-void
-ble_ll_mbuf_init(struct os_mbuf *m, uint8_t pdulen, uint8_t hdr)
-{
-    struct ble_mbuf_hdr *ble_hdr;
-
-    /* Set mbuf length and packet length */
-    m->om_len = pdulen;
-    OS_MBUF_PKTHDR(m)->omp_len = pdulen;
-
-    /* Set BLE transmit header */
-    ble_hdr = BLE_MBUF_HDR_PTR(m);
-    ble_hdr->txinfo.flags = 0;
-    ble_hdr->txinfo.offset = 0;
-    ble_hdr->txinfo.pyld_len = pdulen;
-    ble_hdr->txinfo.hdr_byte = hdr;
-}
-
-/**
- * Called to reset the controller. This performs a "software reset" of the link
- * layer; it does not perform a HW reset of the controller nor does it reset
- * the HCI interface.
- *
- * Context: Link Layer task (HCI command)
- *
- * @return int The ble error code to place in the command complete event that
- * is returned when this command is issued.
- */
-int
-ble_ll_reset(void)
-{
-    int rc;
-    os_sr_t sr;
-
-    /* Stop the phy */
-    ble_phy_disable();
-
-    /* Stop any wait for response timer */
-    OS_ENTER_CRITICAL(sr);
-    ble_ll_wfr_disable();
-    ble_ll_sched_stop();
-    OS_EXIT_CRITICAL(sr);
-
-    /* Stop any scanning */
-    ble_ll_scan_reset();
-
-    /* Stop any advertising */
-    ble_ll_adv_reset();
-
-#if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE)
-    ble_ll_dtm_reset();
-#endif
-
-    /* FLush all packets from Link layer queues */
-    ble_ll_flush_pkt_queue(&g_ble_ll_data.ll_tx_pkt_q);
-    ble_ll_flush_pkt_queue(&g_ble_ll_data.ll_rx_pkt_q);
-
-    /* Reset LL stats */
-    STATS_RESET(ble_ll_stats);
-
-#ifdef BLE_LL_LOG
-    g_ble_ll_log_index = 0;
-    memset(&g_ble_ll_log, 0, sizeof(g_ble_ll_log));
-#endif
-
-    /* Reset any preferred PHYs */
-    g_ble_ll_data.ll_pref_tx_phys = 0;
-    g_ble_ll_data.ll_pref_rx_phys = 0;
-
-    /* Reset connection module */
-    ble_ll_conn_module_reset();
-
-    /* All this does is re-initialize the event masks so call the hci init */
-    ble_ll_hci_init();
-
-    /* Reset scheduler */
-    ble_ll_sched_init();
-
-    /* Set state to standby */
-    ble_ll_state_set(BLE_LL_STATE_STANDBY);
-
-#ifdef BLE_XCVR_RFCLK
-    /* Stops rf clock and rfclock timer */
-    ble_ll_xcvr_rfclk_stop();
-#endif
-
-    /* Reset our random address */
-    memset(g_random_addr, 0, BLE_DEV_ADDR_LEN);
-
-    /* Clear the whitelist */
-    ble_ll_whitelist_clear();
-
-    /* Reset resolving list */
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    ble_ll_resolv_list_reset();
-#endif
-
-    /* Re-initialize the PHY */
-    rc = ble_phy_init();
-
-    return rc;
-}
-
-static void
-ble_ll_seed_prng(void)
-{
-    uint32_t seed;
-    int i;
-
-    /* Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-}
-
-uint32_t
-ble_ll_pdu_tx_time_get(uint16_t payload_len, int phy_mode)
-{
-    uint32_t usecs;
-
-#if (BLE_LL_BT5_PHY_SUPPORTED)
-    if (phy_mode == BLE_PHY_MODE_1M) {
-        /* 8 usecs per byte */
-        usecs = payload_len << 3;
-    } else if (phy_mode == BLE_PHY_MODE_2M) {
-        /* 4 usecs per byte */
-        usecs = payload_len << 2;
-    } else if (phy_mode == BLE_PHY_MODE_CODED_125KBPS) {
-        /* S=8 => 8 * 8 = 64 usecs per byte */
-        usecs = payload_len << 6;
-    } else if (phy_mode == BLE_PHY_MODE_CODED_500KBPS) {
-        /* S=2 => 2 * 8 = 16 usecs per byte */
-        usecs = payload_len << 4;
-    } else {
-        assert(0);
-    }
-
-    usecs += g_ble_ll_pdu_header_tx_time[phy_mode];
-#else
-    usecs = (((payload_len) + BLE_LL_PDU_HDR_LEN + BLE_LL_ACC_ADDR_LEN
-            + BLE_LL_PREAMBLE_LEN + BLE_LL_CRC_LEN) << 3);
-#endif
-
-    return usecs;
-}
-
-uint16_t
-ble_ll_pdu_max_tx_octets_get(uint32_t usecs, int phy_mode)
-{
-    uint32_t header_tx_time;
-    uint16_t octets;
-
-    assert(phy_mode < BLE_PHY_NUM_MODE);
-
-    header_tx_time = g_ble_ll_pdu_header_tx_time[phy_mode];
-
-    /*
-     * Current conn max tx time can be too short to even send a packet header
-     * and this can happen if we changed connection form uncoded to coded phy.
-     * However, the lower bound for conn max tx time (all of them) depends on
-     * current phy (uncoded/coded) but it always allows to send at least 27
-     * bytes of payload thus we alwyas return at least 27 from here.
-     *
-     * Reference:
-     * Core v5.0, Vol 6, Part B, section 4.5.10
-     * see connEffectiveMaxTxTime and connEffectiveMaxRxTime definitions
-     */
-
-    if (usecs < header_tx_time) {
-        return 27;
-    }
-
-    usecs -= header_tx_time;
-
-    if (phy_mode == BLE_PHY_MODE_1M) {
-        /* 8 usecs per byte */
-        octets = usecs >> 3;
-    } else if (phy_mode == BLE_PHY_MODE_2M) {
-        /* 4 usecs per byte */
-        octets = usecs >> 2;
-    } else if (phy_mode == BLE_PHY_MODE_CODED_125KBPS) {
-        /* S=8 => 8 * 8 = 64 usecs per byte */
-        octets = usecs >> 6;
-    } else if (phy_mode == BLE_PHY_MODE_CODED_500KBPS) {
-        /* S=2 => 2 * 8 = 16 usecs per byte */
-        octets = usecs >> 4;
-    } else {
-        assert(0);
-    }
-
-    /* see comment at the beginning */
-    return max(27, octets);
-}
-
-/**
- * Initialize the Link Layer. Should be called only once
- *
- * @return int
- */
-void
-ble_ll_init(void)
-{
-    int rc;
-    uint32_t features;
-#ifdef BLE_XCVR_RFCLK
-    uint32_t xtal_ticks;
-#endif
-    ble_addr_t addr;
-    struct ble_ll_obj *lldata;
-
-    /* Ensure this function only gets called by sysinit. */
-    SYSINIT_ASSERT_ACTIVE();
-
-    /* Retrieve the public device address if not set by syscfg */
-    memcpy(&addr.val[0], MYNEWT_VAL_BLE_PUBLIC_DEV_ADDR, BLE_DEV_ADDR_LEN);
-    if (!memcmp(&addr.val[0], ((ble_addr_t *)BLE_ADDR_ANY)->val,
-                BLE_DEV_ADDR_LEN)) {
-        rc = ble_hw_get_public_addr(&addr);
-        if (!rc) {
-            memcpy(g_dev_addr, &addr.val[0], BLE_DEV_ADDR_LEN);
-        }
-    } else {
-        memcpy(g_dev_addr, &addr.val[0], BLE_DEV_ADDR_LEN);
-    }
-
-#ifdef BLE_XCVR_RFCLK
-    /* Settling time of crystal, in ticks */
-    xtal_ticks = MYNEWT_VAL(BLE_XTAL_SETTLE_TIME);
-    assert(xtal_ticks != 0);
-    g_ble_ll_data.ll_xtal_ticks = os_cputime_usecs_to_ticks(xtal_ticks);
-
-    /* Initialize rf clock timer */
-    os_cputime_timer_init(&g_ble_ll_data.ll_rfclk_timer,
-                          ble_ll_xcvr_rfclk_timer_exp, NULL);
-
-#endif
-
-    /* Get pointer to global data object */
-    lldata = &g_ble_ll_data;
-
-    /* Set acl pkt size and number */
-    lldata->ll_num_acl_pkts = MYNEWT_VAL(BLE_ACL_BUF_COUNT);
-    lldata->ll_acl_pkt_size = MYNEWT_VAL(BLE_ACL_BUF_SIZE);
-
-    /* Initialize eventq */
-    os_eventq_init(&lldata->ll_evq);
-
-    /* Initialize the transmit (from host) and receive (from phy) queues */
-    STAILQ_INIT(&lldata->ll_tx_pkt_q);
-    STAILQ_INIT(&lldata->ll_rx_pkt_q);
-
-    /* Initialize transmit (from host) and receive packet (from phy) event */
-    lldata->ll_rx_pkt_ev.ev_cb = ble_ll_event_rx_pkt;
-    lldata->ll_tx_pkt_ev.ev_cb = ble_ll_event_tx_pkt;
-
-    /* Initialize data buffer overflow event and completed packets */
-    lldata->ll_dbuf_overflow_ev.ev_cb = ble_ll_event_dbuf_overflow;
-    lldata->ll_comp_pkt_ev.ev_cb = ble_ll_event_comp_pkts;
-
-    /* Initialize the HW error timer */
-    os_callout_init(&g_ble_ll_data.ll_hw_err_timer,
-                    &g_ble_ll_data.ll_evq,
-                    ble_ll_hw_err_timer_cb,
-                    NULL);
-
-    /* Initialize LL HCI */
-    ble_ll_hci_init();
-
-    /* Init the scheduler */
-    ble_ll_sched_init();
-
-    /* Initialize advertiser */
-    ble_ll_adv_init();
-
-    /* Initialize a scanner */
-    ble_ll_scan_init();
-
-    /* Initialize the connection module */
-    ble_ll_conn_module_init();
-
-    /* Set the supported features. NOTE: we always support extended reject. */
-    features = BLE_LL_FEAT_EXTENDED_REJ;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
-    features |= BLE_LL_FEAT_DATA_LEN_EXT;
-#endif
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_CONN_PARAM_REQ) == 1)
-    features |= BLE_LL_FEAT_CONN_PARM_REQ;
-#endif
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG) == 1)
-    features |= BLE_LL_FEAT_SLAVE_INIT;
-#endif
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    features |= BLE_LL_FEAT_LE_ENCRYPTION;
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    features |= (BLE_LL_FEAT_LL_PRIVACY | BLE_LL_FEAT_EXT_SCAN_FILT);
-    ble_ll_resolv_init();
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-    features |= BLE_LL_FEAT_LE_PING;
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-    /* CSA2 */
-    features |= BLE_LL_FEAT_CSA2;
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) == 1)
-    features |= BLE_LL_FEAT_LE_2M_PHY;
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY) == 1)
-    features |= BLE_LL_FEAT_LE_CODED_PHY;
-#endif
-
-    /* Initialize random number generation */
-    ble_ll_rand_init();
-
-    /* XXX: This really doesn't belong here, as the address probably has not
-     * been set yet.
-     */
-    ble_ll_seed_prng();
-
-    lldata->ll_supp_features = features;
-
-    /* Initialize the LL task */
-    os_task_init(&g_ble_ll_task, "ble_ll", ble_ll_task, NULL,
-                 MYNEWT_VAL(BLE_LL_PRIO), OS_WAIT_FOREVER, g_ble_ll_stack,
-                 BLE_LL_STACK_SIZE);
-
-    rc = stats_init_and_reg(STATS_HDR(ble_ll_stats),
-                            STATS_SIZE_INIT_PARMS(ble_ll_stats, STATS_SIZE_32),
-                            STATS_NAME_INIT_PARMS(ble_ll_stats),
-                            "ble_ll");
-    SYSINIT_PANIC_ASSERT(rc == 0);
-
-    ble_hci_trans_cfg_ll(ble_ll_hci_cmd_rx, NULL, ble_ll_hci_acl_rx, NULL);
-}
-
-#ifdef BLE_LL_LOG
-void
-ble_ll_log_dump_index(int i)
-{
-    struct ble_ll_log *log;
-
-    log = &g_ble_ll_log[i];
-
-    /* TODO cast is a workaround until this is fixed properly */
-    console_printf("cputime=%lu id=%u a8=%u a16=%u a32=%lu\n",
-                   (unsigned long)log->cputime, log->log_id, log->log_a8,
-                   log->log_a16, (unsigned long)log->log_a32);
-}
-
-void
-ble_ll_log_dump(void)
-{
-    int i;
-
-    for (i = g_ble_ll_log_index; i < BLE_LL_LOG_LEN; ++i) {
-        ble_ll_log_dump_index(i);
-    }
-    for (i = 0; i < g_ble_ll_log_index; ++i) {
-        ble_ll_log_dump_index(i);
-    }
-}
-#endif
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
deleted file mode 100644
index e8a4f718d..000000000
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ /dev/null
@@ -1,3383 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "os/os.h"
-#include "os/os_cputime.h"
-#include "bsp/bsp.h"
-#include "ble/xcvr.h"
-#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"
-#include "controller/ble_ll_hci.h"
-#include "controller/ble_ll_adv.h"
-#include "controller/ble_ll_sched.h"
-#include "controller/ble_ll_scan.h"
-#include "controller/ble_ll_whitelist.h"
-#include "controller/ble_ll_resolv.h"
-#include "ble_ll_conn_priv.h"
-
-/* XXX: TODO
- * 1) Need to look at advertising and scan request PDUs. Do I allocate these
- * once? Do I use a different pool for smaller ones? Do I statically declare
- * them?
- * 3) How do features get supported? What happens if device does not support
- * advertising? (for example)
- * 4) How to determine the advertising interval we will actually use. As of
- * now, we set it to max.
- * 5) How does the advertising channel tx power get set? I dont implement
- * that currently.
- */
-
-/* Scheduling data for secondary channel */
-struct ble_ll_adv_aux {
-    struct ble_ll_sched_item sch;
-    uint32_t start_time;
-    uint16_t aux_data_offset;
-    uint8_t ext_hdr;
-    uint8_t aux_data_len;
-    uint8_t payload_len;
-};
-
-/*
- * Advertising state machine
- *
- * The advertising state machine data structure.
- *
- *  adv_pdu_len
- *      The length of the advertising PDU that will be sent. This does not
- *      include the preamble, access address and CRC.
- *
- *  initiator_addr:
- *      This is the address that we send in directed advertisements (the
- *      INITA field). If we are using Privacy this is a RPA that we need to
- *      generate. We reserve space in the advsm to save time when creating
- *      the ADV_DIRECT_IND. If own address type is not 2 or 3, this is simply
- *      the peer address from the set advertising parameters.
- */
-struct ble_ll_adv_sm
-{
-    uint8_t adv_enabled;
-    uint8_t adv_instance;
-    uint8_t adv_chanmask;
-    uint8_t adv_filter_policy;
-    uint8_t own_addr_type;
-    uint8_t peer_addr_type;
-    uint8_t adv_chan;
-    uint8_t adv_pdu_len;
-    int8_t adv_rpa_index;
-    uint8_t flags;
-    int8_t adv_txpwr;
-    uint16_t props;
-    uint16_t adv_itvl_min;
-    uint16_t adv_itvl_max;
-    uint32_t adv_itvl_usecs;
-    uint32_t adv_event_start_time;
-    uint32_t adv_pdu_start_time;
-    uint32_t adv_end_time;
-    uint32_t adv_rpa_timer;
-    uint8_t adva[BLE_DEV_ADDR_LEN];
-    uint8_t adv_rpa[BLE_DEV_ADDR_LEN];
-    uint8_t peer_addr[BLE_DEV_ADDR_LEN];
-    uint8_t initiator_addr[BLE_DEV_ADDR_LEN];
-    struct os_mbuf *adv_data;
-    struct os_mbuf *scan_rsp_data;
-    uint8_t *conn_comp_ev;
-    struct os_event adv_txdone_ev;
-    struct ble_ll_sched_item adv_sch;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    uint8_t aux_active : 1;
-    uint8_t aux_index : 1;
-    uint8_t aux_first_pdu : 1;
-    uint8_t aux_not_scanned : 1;
-    struct ble_mbuf_hdr *rx_ble_hdr;
-    struct os_mbuf **aux_data;
-    struct ble_ll_adv_aux aux[2];
-    struct os_event adv_sec_txdone_ev;
-    uint16_t duration;
-    uint16_t adi;
-    uint8_t adv_secondary_chan;
-    uint8_t adv_random_addr[BLE_DEV_ADDR_LEN];
-    uint8_t events_max;
-    uint8_t events;
-    uint8_t pri_phy;
-    uint8_t sec_phy;
-#endif
-};
-
-#define BLE_LL_ADV_SM_FLAG_TX_ADD               0x01
-#define BLE_LL_ADV_SM_FLAG_RX_ADD               0x02
-#define BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF       0x04
-#define BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD         0x08
-#define BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK  0x30 /* use helpers! */
-#define BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE  0x40
-
-#define ADV_DATA_LEN(_advsm) \
-                ((_advsm->adv_data) ? OS_MBUF_PKTLEN(advsm->adv_data) : 0)
-#define SCAN_RSP_DATA_LEN(_advsm) \
-                ((_advsm->scan_rsp_data) ? OS_MBUF_PKTLEN(advsm->scan_rsp_data) : 0)
-#define AUX_DATA_LEN(_advsm) \
-                (*(_advsm->aux_data) ? OS_MBUF_PKTLEN(*advsm->aux_data) : 0)
-
-#define AUX_CURRENT(_advsm)     (&(_advsm->aux[_advsm->aux_index]))
-#define AUX_NEXT(_advsm)        (&(_advsm->aux[_advsm->aux_index ^ 1]))
-
-static inline int
-ble_ll_adv_active_chanset_is_pri(struct ble_ll_adv_sm *advsm)
-{
-    return (advsm->flags & BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK) == 0x10;
-}
-
-static inline int
-ble_ll_adv_active_chanset_is_sec(struct ble_ll_adv_sm *advsm)
-{
-    return (advsm->flags & BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK) == 0x20;
-}
-
-static inline void
-ble_ll_adv_active_chanset_clear(struct ble_ll_adv_sm *advsm)
-{
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK;
-}
-
-static inline void
-ble_ll_adv_active_chanset_set_pri(struct ble_ll_adv_sm *advsm)
-{
-    assert((advsm->flags & BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK) == 0);
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK;
-    advsm->flags |= 0x10;
-}
-
-static inline void
-ble_ll_adv_active_chanset_set_sec(struct ble_ll_adv_sm *advsm)
-{
-    assert((advsm->flags & BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK) == 0);
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_ACTIVE_CHANSET_MASK;
-    advsm->flags |= 0x20;
-}
-
-/* The advertising state machine global object */
-struct ble_ll_adv_sm g_ble_ll_adv_sm[BLE_ADV_INSTANCES];
-struct ble_ll_adv_sm *g_ble_ll_cur_adv_sm;
-
-static void ble_ll_adv_make_done(struct ble_ll_adv_sm *advsm, struct ble_mbuf_hdr *hdr);
-static void ble_ll_adv_sm_init(struct ble_ll_adv_sm *advsm);
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-static void
-ble_ll_adv_rpa_update(struct ble_ll_adv_sm *advsm)
-{
-    ble_ll_resolv_gen_rpa(advsm->peer_addr, advsm->peer_addr_type,
-                          advsm->adva, 1);
-
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        ble_ll_resolv_gen_rpa(advsm->peer_addr, advsm->peer_addr_type,
-                              advsm->initiator_addr, 0);
-        if (ble_ll_is_rpa(advsm->initiator_addr, 1)) {
-            advsm->flags |= BLE_LL_ADV_SM_FLAG_RX_ADD;
-        } else {
-            if (advsm->own_addr_type & 1) {
-                advsm->flags |= BLE_LL_ADV_SM_FLAG_RX_ADD;
-            } else {
-                advsm->flags &= ~BLE_LL_ADV_SM_FLAG_RX_ADD;
-            }
-        }
-    }
-
-    /* May have to reset txadd bit */
-    if (ble_ll_is_rpa(advsm->adva, 1)) {
-        advsm->flags |= BLE_LL_ADV_SM_FLAG_TX_ADD;
-    } else {
-        if (advsm->own_addr_type & 1) {
-            advsm->flags |= BLE_LL_ADV_SM_FLAG_TX_ADD;
-        } else {
-            advsm->flags &= ~BLE_LL_ADV_SM_FLAG_TX_ADD;
-        }
-    }
-}
-
-/**
- * Called to change advertisers ADVA and INITA (for directed advertisements)
- * as an advertiser needs to adhere to the resolvable private address generation
- * timer.
- *
- * NOTE: the resolvable private address code uses its own timer to regenerate
- * local resolvable private addresses. The advertising code uses its own
- * timer to reset the INITA (for directed advertisements). This code also sets
- * the appropriate txadd and rxadd bits that will go into the advertisement.
- *
- * Another thing to note: it is possible that an IRK is all zeroes in the
- * resolving list. That is why we need to check if the generated address is
- * in fact a RPA as a resolving list entry with all zeroes will use the
- * identity address (which may be a private address or public).
- *
- * @param advsm
- */
-void
-ble_ll_adv_chk_rpa_timeout(struct ble_ll_adv_sm *advsm)
-{
-    uint32_t now;
-
-    if (advsm->own_addr_type < BLE_HCI_ADV_OWN_ADDR_PRIV_PUB) {
-        return;
-    }
-
-    now = os_time_get();
-    if ((int32_t)(now - advsm->adv_rpa_timer) >= 0) {
-        ble_ll_adv_rpa_update(advsm);
-        advsm->adv_rpa_timer = now + ble_ll_resolv_get_rpa_tmo();
-    }
-}
-#endif
-
-/**
- * Calculate the first channel that we should advertise upon when we start
- * an advertising event.
- *
- * @param advsm
- *
- * @return uint8_t The number of the first channel usable for advertising.
- */
-static uint8_t
-ble_ll_adv_first_chan(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t adv_chan;
-
-    /* Set first advertising channel */
-    if (advsm->adv_chanmask & 0x01) {
-        adv_chan = BLE_PHY_ADV_CHAN_START;
-    } else if (advsm->adv_chanmask & 0x02) {
-        adv_chan = BLE_PHY_ADV_CHAN_START + 1;
-    } else {
-        adv_chan = BLE_PHY_ADV_CHAN_START + 2;
-    }
-
-    return adv_chan;
-}
-
-/**
- * Calculate the final channel that we should advertise upon when we start
- * an advertising event.
- *
- * @param advsm
- *
- * @return uint8_t The number of the final channel usable for advertising.
- */
-static uint8_t
-ble_ll_adv_final_chan(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t adv_chan;
-
-    if (advsm->adv_chanmask & 0x04) {
-        adv_chan = BLE_PHY_ADV_CHAN_START + 2;
-    } else if (advsm->adv_chanmask & 0x02) {
-        adv_chan = BLE_PHY_ADV_CHAN_START + 1;
-    } else {
-        adv_chan = BLE_PHY_ADV_CHAN_START;
-    }
-
-    return adv_chan;
-}
-
-/**
- * Create the advertising legacy PDU
- *
- * @param advsm Pointer to advertisement state machine
- */
-static uint8_t
-ble_ll_adv_legacy_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-    uint8_t     adv_data_len;
-    uint8_t     pdulen;
-    uint8_t     pdu_type;
-
-    advsm = pducb_arg;
-
-    /* assume this is not a direct ind */
-    adv_data_len = ADV_DATA_LEN(advsm);
-    pdulen = BLE_DEV_ADDR_LEN + adv_data_len;
-
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        pdu_type = BLE_ADV_PDU_TYPE_ADV_DIRECT_IND;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-        pdu_type |= BLE_ADV_PDU_HDR_CHSEL;
-#endif
-
-        if (advsm->flags & BLE_LL_ADV_SM_FLAG_RX_ADD) {
-            pdu_type |= BLE_ADV_PDU_HDR_RXADD_RAND;
-        }
-
-        adv_data_len = 0;
-        pdulen = BLE_ADV_DIRECT_IND_LEN;
-    } else if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        pdu_type = BLE_ADV_PDU_TYPE_ADV_IND;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-        pdu_type |= BLE_ADV_PDU_HDR_CHSEL;
-#endif
-    } else if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-        pdu_type = BLE_ADV_PDU_TYPE_ADV_SCAN_IND;
-    } else {
-        pdu_type = BLE_ADV_PDU_TYPE_ADV_NONCONN_IND;
-    }
-
-    /* An invalid advertising data length indicates a memory overwrite */
-    assert(adv_data_len <= BLE_ADV_LEGACY_DATA_MAX_LEN);
-
-    /* Set the PDU length in the state machine (includes header) */
-    advsm->adv_pdu_len = pdulen + BLE_LL_PDU_HDR_LEN;
-
-    /* Set TxAdd to random if needed. */
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        pdu_type |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-
-    *hdr_byte = pdu_type;
-
-    /* Construct advertisement */
-    memcpy(dptr, advsm->adva, BLE_DEV_ADDR_LEN);
-    dptr += BLE_DEV_ADDR_LEN;
-
-    /* For ADV_DIRECT_IND add inita */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        memcpy(dptr, advsm->initiator_addr, BLE_DEV_ADDR_LEN);
-    }
-
-    /* Copy in advertising data, if any */
-    if (adv_data_len != 0) {
-        os_mbuf_copydata(advsm->adv_data, 0, adv_data_len, dptr);
-    }
-
-    return pdulen;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-static void
-ble_ll_adv_put_aux_ptr(struct ble_ll_adv_sm *advsm, uint32_t offset,
-                       uint8_t *dptr)
-{
-    /* in usecs */
-    offset = os_cputime_ticks_to_usecs(offset);
-
-    dptr[0] = advsm->adv_secondary_chan;
-
-    if (offset > 245700) {
-        dptr[0] |= 0x80;
-        offset = offset / 300;
-    } else {
-        offset = offset / 30;
-    }
-
-    dptr[1] = (offset & 0x000000ff);
-    dptr[2] = ((offset >> 8) & 0x0000001f) | (advsm->sec_phy - 1) << 5; //TODO;
-}
-
-/**
- * Create the advertising PDU
- */
-static uint8_t
-ble_ll_adv_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-    uint8_t pdu_type;
-    uint8_t adv_mode;
-    uint8_t ext_hdr_len;
-    uint8_t ext_hdr_flags;
-    uint32_t offset;
-
-    advsm = pducb_arg;
-
-    assert(ble_ll_adv_active_chanset_is_pri(advsm));
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        return ble_ll_adv_legacy_pdu_make(dptr, advsm, hdr_byte);
-    }
-
-    /* only ADV_EXT_IND goes on primary advertising channels */
-    pdu_type = BLE_ADV_PDU_TYPE_ADV_EXT_IND;
-
-    /* Set TxAdd to random if needed. */
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        pdu_type |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-
-    *hdr_byte = pdu_type;
-
-    adv_mode = 0;
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        adv_mode |= BLE_LL_EXT_ADV_MODE_CONN;
-    }
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-        adv_mode |= BLE_LL_EXT_ADV_MODE_SCAN;
-    }
-
-    ext_hdr_len = BLE_LL_EXT_ADV_FLAGS_SIZE + BLE_LL_EXT_ADV_DATA_INFO_SIZE +
-                  BLE_LL_EXT_ADV_AUX_PTR_SIZE;
-    ext_hdr_flags = (1 << BLE_LL_EXT_ADV_DATA_INFO_BIT) |
-                    (1 << BLE_LL_EXT_ADV_AUX_PTR_BIT);
-
-    /* ext hdr len and adv mode */
-    dptr[0] = ext_hdr_len | (adv_mode << 6);
-    dptr += 1;
-
-    /* ext hdr flags */
-    dptr[0] = ext_hdr_flags;
-    dptr += 1;
-
-    /* ADI */
-    dptr[0] = advsm->adi & 0x00ff;
-    dptr[1] = advsm->adi >> 8;
-    dptr += BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-
-    /* AuxPtr */
-    assert(AUX_CURRENT(advsm)->sch.enqueued);
-    offset = AUX_CURRENT(advsm)->start_time - advsm->adv_pdu_start_time;
-    ble_ll_adv_put_aux_ptr(advsm, offset, dptr);
-
-    return BLE_LL_EXT_ADV_HDR_LEN + ext_hdr_len;
-}
-
-/**
- * Create the AUX PDU
- */
-static uint8_t
-ble_ll_adv_aux_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-    struct ble_ll_adv_aux *aux;
-    uint8_t adv_mode;
-    uint8_t pdu_type;
-    uint8_t ext_hdr_len;
-    uint32_t offset;
-
-    advsm = pducb_arg;
-    aux = AUX_CURRENT(advsm);
-
-    assert(!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY));
-    assert(ble_ll_adv_active_chanset_is_sec(advsm));
-
-    /* It's the same for AUX_ADV_IND and AUX_CHAIN_IND */
-    pdu_type = BLE_ADV_PDU_TYPE_AUX_ADV_IND;
-
-    /* Set TxAdd to random if needed. */
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        pdu_type |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-
-    *hdr_byte = pdu_type;
-
-    /* We do not create scannable PDUs here - this is handled separately */
-    adv_mode = 0;
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        adv_mode |= BLE_LL_EXT_ADV_MODE_CONN;
-    }
-
-    ext_hdr_len = aux->payload_len - BLE_LL_EXT_ADV_HDR_LEN - aux->aux_data_len;
-    dptr[0] = (adv_mode << 6) | ext_hdr_len;
-    dptr += 1;
-
-    dptr[0] = aux->ext_hdr;
-    dptr += 1;
-
-    if (aux->ext_hdr & (1 << BLE_LL_EXT_ADV_ADVA_BIT)) {
-        memcpy(dptr, advsm->adva, BLE_LL_EXT_ADV_ADVA_SIZE);
-        dptr += BLE_LL_EXT_ADV_ADVA_SIZE;
-    }
-
-    if (aux->ext_hdr & (1 << BLE_LL_EXT_ADV_TARGETA_BIT)) {
-        memcpy(dptr, advsm->initiator_addr, BLE_LL_EXT_ADV_TARGETA_SIZE);
-        dptr += BLE_LL_EXT_ADV_TARGETA_SIZE;
-    }
-
-    if (aux->ext_hdr & (1 << BLE_LL_EXT_ADV_DATA_INFO_BIT)) {
-        dptr[0] = advsm->adi & 0x00ff;
-        dptr[1] = advsm->adi >> 8;
-        dptr += BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-    }
-
-    if (aux->ext_hdr & (1 << BLE_LL_EXT_ADV_AUX_PTR_BIT)) {
-        assert(AUX_NEXT(advsm)->sch.enqueued);
-
-        if (advsm->rx_ble_hdr) {
-            offset = advsm->rx_ble_hdr->rem_usecs +
-                     ble_ll_pdu_tx_time_get(12, advsm->sec_phy) + BLE_LL_IFS + 30;
-            offset = AUX_NEXT(advsm)->start_time - advsm->rx_ble_hdr->beg_cputime -
-                     os_cputime_usecs_to_ticks(offset);
-        } else {
-            offset = AUX_NEXT(advsm)->start_time - aux->start_time;
-        }
-
-        ble_ll_adv_put_aux_ptr(advsm, offset, dptr);
-
-        dptr += BLE_LL_EXT_ADV_AUX_PTR_SIZE;
-    }
-
-    if (aux->ext_hdr & (1 << BLE_LL_EXT_ADV_TX_POWER_BIT)) {
-        dptr[0] = advsm->adv_txpwr;
-        dptr += BLE_LL_EXT_ADV_TX_POWER_SIZE;
-    }
-
-    if (aux->aux_data_len) {
-        os_mbuf_copydata(*advsm->aux_data, aux->aux_data_offset,
-                         aux->aux_data_len, dptr);
-    }
-
-    return aux->payload_len;
-}
-
-static uint8_t
-ble_ll_adv_aux_scannable_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-    uint8_t pdu_type;
-    uint8_t *ext_hdr_len;
-    uint8_t *ext_hdr;
-    uint8_t pdulen;
-
-    advsm = pducb_arg;
-
-    assert(!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY));
-    assert(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE);
-    assert(advsm->aux_first_pdu);
-    assert(ble_ll_adv_active_chanset_is_sec(advsm));
-
-    pdu_type = BLE_ADV_PDU_TYPE_AUX_ADV_IND;
-
-    /* Set TxAdd to random if needed. */
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        pdu_type |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-
-    *hdr_byte = pdu_type;
-
-    ext_hdr_len = &dptr[0];
-    ext_hdr = &dptr[1];
-    dptr += 2;
-
-    /* Flags always */
-    *ext_hdr_len = BLE_LL_EXT_ADV_FLAGS_SIZE;
-    *ext_hdr = 0;
-
-    /* AdvA always */
-    *ext_hdr_len += BLE_LL_EXT_ADV_ADVA_SIZE;
-    *ext_hdr |= (1 << BLE_LL_EXT_ADV_ADVA_BIT);
-    memcpy(dptr, advsm->adva, BLE_LL_EXT_ADV_ADVA_SIZE);
-    dptr += BLE_LL_EXT_ADV_ADVA_SIZE;
-
-    /* TargetA only for directed */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        *ext_hdr_len += BLE_LL_EXT_ADV_TARGETA_SIZE;
-        *ext_hdr |= (1 << BLE_LL_EXT_ADV_TARGETA_BIT);
-        memcpy(dptr, advsm->initiator_addr, BLE_LL_EXT_ADV_TARGETA_SIZE);
-        dptr += BLE_LL_EXT_ADV_TARGETA_SIZE;
-    }
-
-    /* ADI always */
-    *ext_hdr_len += BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-    *ext_hdr |= (1 << BLE_LL_EXT_ADV_DATA_INFO_BIT);
-    dptr[0] = advsm->adi & 0x00ff;
-    dptr[1] = advsm->adi >> 8;
-    dptr += BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-
-    /* TxPower if configured */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_INC_TX_PWR) {
-        *ext_hdr_len += BLE_LL_EXT_ADV_TX_POWER_SIZE;
-        *ext_hdr |= (1 << BLE_LL_EXT_ADV_TX_POWER_BIT);
-        dptr[0] = advsm->adv_txpwr;
-        dptr += BLE_LL_EXT_ADV_TX_POWER_SIZE;
-    }
-
-    pdulen = BLE_LL_EXT_ADV_HDR_LEN + *ext_hdr_len;
-
-    *ext_hdr_len |= (BLE_LL_EXT_ADV_MODE_SCAN << 6);
-
-    return pdulen;
-}
-#endif
-
-static uint8_t
-ble_ll_adv_scan_rsp_legacy_pdu_make(uint8_t *dptr, void *pducb_arg,
-                                    uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-    uint8_t     scan_rsp_len;
-    uint8_t     pdulen;
-    uint8_t     hdr;
-
-    advsm = pducb_arg;
-
-    /* Make sure that the length is valid */
-    scan_rsp_len = SCAN_RSP_DATA_LEN(advsm);
-    assert(scan_rsp_len <= BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN);
-
-    /* Set BLE transmit header */
-    pdulen = BLE_DEV_ADDR_LEN + scan_rsp_len;
-    hdr = BLE_ADV_PDU_TYPE_SCAN_RSP;
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        hdr |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-
-    *hdr_byte = hdr;
-
-    /*
-     * The adva in this packet will be the same one that was being advertised
-     * and is based on the peer identity address in the set advertising
-     * parameters. If a different peer sends us a scan request (for some reason)
-     * we will reply with an adva that was not generated based on the local irk
-     * of the peer sending the scan request.
-     */
-
-    /* Construct scan response */
-    memcpy(dptr, advsm->adva, BLE_DEV_ADDR_LEN);
-    if (scan_rsp_len != 0) {
-        os_mbuf_copydata(advsm->scan_rsp_data, 0, scan_rsp_len,
-                         dptr + BLE_DEV_ADDR_LEN);
-    }
-
-    return pdulen;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-/**
- * Create a scan response PDU
- *
- * @param advsm
- */
-static uint8_t
-ble_ll_adv_scan_rsp_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
-{
-    struct ble_ll_adv_sm *advsm;
-
-    advsm = pducb_arg;
-
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        return ble_ll_adv_scan_rsp_legacy_pdu_make(dptr, pducb_arg, hdr_byte);
-    }
-
-    return ble_ll_adv_aux_pdu_make(dptr, pducb_arg, hdr_byte);
-}
-
-struct aux_conn_rsp_data {
-    struct ble_ll_adv_sm *advsm;
-    uint8_t *peer;
-    uint8_t rxadd;
-};
-
-/**
- * Create a AUX connect response PDU
- *
- * @param advsm
- */
-static uint8_t
-ble_ll_adv_aux_conn_rsp_pdu_make(uint8_t *dptr, void *pducb_arg,
-                                 uint8_t *hdr_byte)
-{
-    struct aux_conn_rsp_data *rsp_data;
-    uint8_t     pdulen;
-    uint8_t     ext_hdr_len;
-    uint8_t     ext_hdr_flags;
-    uint8_t     hdr;
-
-    rsp_data = pducb_arg;
-
-    /* flags,AdvA and TargetA */
-    ext_hdr_len = BLE_LL_EXT_ADV_FLAGS_SIZE + BLE_LL_EXT_ADV_ADVA_SIZE +
-                  BLE_LL_EXT_ADV_TARGETA_SIZE;
-    ext_hdr_flags = (1 << BLE_LL_EXT_ADV_ADVA_BIT);
-    ext_hdr_flags |= (1 << BLE_LL_EXT_ADV_TARGETA_BIT);
-
-    pdulen = BLE_LL_EXT_ADV_HDR_LEN + ext_hdr_len;
-
-    /* Set BLE transmit header */
-    hdr = BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP | rsp_data->rxadd;
-    if (rsp_data->advsm->flags & BLE_LL_ADV_SM_FLAG_TX_ADD) {
-        hdr |= BLE_ADV_PDU_HDR_TXADD_MASK;
-    }
-
-    *hdr_byte = hdr;
-
-    /* ext hdr len and adv mode (00b) */
-    dptr[0] = ext_hdr_len;
-    dptr += 1;
-
-    /* ext hdr flags */
-    dptr[0] = ext_hdr_flags;
-    dptr += 1;
-
-    memcpy(dptr, rsp_data->advsm->adva, BLE_LL_EXT_ADV_ADVA_SIZE);
-    dptr += BLE_LL_EXT_ADV_ADVA_SIZE;
-
-    memcpy(dptr, rsp_data->peer, BLE_LL_EXT_ADV_TARGETA_SIZE);
-    dptr += BLE_LL_EXT_ADV_ADVA_SIZE;
-
-    return pdulen;
-}
-#endif
-
-/**
- * Called to indicate the advertising event is over.
- *
- * Context: Interrupt
- *
- * @param advsm
- *
- */
-static void
-ble_ll_adv_tx_done(void *arg)
-{
-    struct ble_ll_adv_sm *advsm;
-
-    /* XXX: for now, reset power to max after advertising */
-    ble_phy_txpwr_set(MYNEWT_VAL(BLE_LL_TX_PWR_DBM));
-
-    advsm = (struct ble_ll_adv_sm *)arg;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (ble_ll_adv_active_chanset_is_pri(advsm)) {
-        os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-    } else if (ble_ll_adv_active_chanset_is_sec(advsm)) {
-        os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_sec_txdone_ev);
-    } else {
-        assert(0);
-    }
-#else
-    assert(ble_ll_adv_active_chanset_is_pri(advsm));
-    os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-#endif
-
-    ble_ll_log(BLE_LL_LOG_ID_ADV_TXDONE, ble_ll_state_get(),
-               advsm->adv_instance, 0);
-
-    ble_ll_state_set(BLE_LL_STATE_STANDBY);
-
-    ble_ll_adv_active_chanset_clear(advsm);
-
-    /* We no longer have a current state machine */
-    g_ble_ll_cur_adv_sm = NULL;
-}
-
-/*
- * Called when an advertising event has been removed from the scheduler
- * without being run.
- */
-void
-ble_ll_adv_event_rmvd_from_sched(struct ble_ll_adv_sm *advsm)
-{
-    /*
-     * Need to set advertising channel to final chan so new event gets
-     * scheduled.
-     */
-    advsm->adv_chan = ble_ll_adv_final_chan(advsm);
-    os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-}
-
-/**
- * This is the scheduler callback (called from interrupt context) which
- * transmits an advertisement.
- *
- * Context: Interrupt (scheduler)
- *
- * @param sch
- *
- * @return int
- */
-static int
-ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
-{
-    int rc;
-    uint8_t end_trans;
-    uint32_t txstart;
-    struct ble_ll_adv_sm *advsm;
-
-    /* Get the state machine for the event */
-    advsm = (struct ble_ll_adv_sm *)sch->cb_arg;
-
-    /* Set the current advertiser */
-    g_ble_ll_cur_adv_sm = advsm;
-
-    ble_ll_adv_active_chanset_set_pri(advsm);
-
-    /* Set the power */
-    ble_phy_txpwr_set(advsm->adv_txpwr);
-
-    /* Set channel */
-    rc = ble_phy_setchan(advsm->adv_chan, BLE_ACCESS_ADDR_ADV, BLE_LL_CRCINIT_ADV);
-    assert(rc == 0);
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    /* Set phy mode */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M);
-    } else {
-        ble_phy_mode_set(advsm->pri_phy, advsm->pri_phy);
-    }
-#else
-    ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M);
-#endif
-#endif
-
-    /* Set transmit start time. */
-    txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
-    rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
-    if (rc) {
-        STATS_INC(ble_ll_stats, adv_late_starts);
-        goto adv_tx_done;
-    }
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    /* XXX: automatically do this in the phy based on channel? */
-    ble_phy_encrypt_disable();
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    advsm->adv_rpa_index = -1;
-    if (ble_ll_resolv_enabled()) {
-        ble_phy_resolv_list_enable();
-    } else {
-        ble_phy_resolv_list_disable();
-    }
-#endif
-
-    /* We switch to RX after connectable or scannable legacy packets. */
-    if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) &&
-            ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) ||
-             (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE))) {
-        end_trans = BLE_PHY_TRANSITION_TX_RX;
-        ble_phy_set_txend_cb(NULL, NULL);
-    } else {
-        end_trans = BLE_PHY_TRANSITION_NONE;
-        ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
-    }
-
-    /* Transmit advertisement */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    rc = ble_phy_tx(ble_ll_adv_pdu_make, advsm, end_trans);
-#else
-    rc = ble_phy_tx(ble_ll_adv_legacy_pdu_make, advsm, end_trans);
-#endif
-    if (rc) {
-        goto adv_tx_done;
-    }
-
-    /* Enable/disable whitelisting based on filter policy */
-    if (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE) {
-        ble_ll_whitelist_enable();
-    } else {
-        ble_ll_whitelist_disable();
-    }
-
-    /* Set link layer state to advertising */
-    ble_ll_state_set(BLE_LL_STATE_ADV);
-
-    /* Count # of adv. sent */
-    STATS_INC(ble_ll_stats, adv_txg);
-
-    return BLE_LL_SCHED_STATE_RUNNING;
-
-adv_tx_done:
-    ble_ll_adv_tx_done(advsm);
-    return BLE_LL_SCHED_STATE_DONE;
-}
-
-static void
-ble_ll_adv_set_sched(struct ble_ll_adv_sm *advsm)
-{
-    uint32_t max_usecs;
-    struct ble_ll_sched_item *sch;
-
-    sch = &advsm->adv_sch;
-    sch->cb_arg = advsm;
-    sch->sched_cb = ble_ll_adv_tx_start_cb;
-    sch->sched_type = BLE_LL_SCHED_TYPE_ADV;
-
-    /* Set end time to maximum time this schedule item may take */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        max_usecs = ble_ll_pdu_tx_time_get(advsm->adv_pdu_len, BLE_PHY_MODE_1M);
-
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-            max_usecs += BLE_LL_SCHED_DIRECT_ADV_MAX_USECS;
-        } else if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-            max_usecs += BLE_LL_SCHED_ADV_MAX_USECS;
-        }
-    } else {
-        /*
-         * In ADV_EXT_IND we always set only ADI and AUX so the payload length
-         * is always 7 bytes.
-         */
-        max_usecs = ble_ll_pdu_tx_time_get(7, advsm->pri_phy);
-    }
-#else
-    max_usecs = ble_ll_pdu_tx_time_get(advsm->adv_pdu_len, BLE_PHY_MODE_1M);
-
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        max_usecs += BLE_LL_SCHED_DIRECT_ADV_MAX_USECS;
-    } else if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        max_usecs += BLE_LL_SCHED_ADV_MAX_USECS;
-    }
-#endif
-
-    sch->start_time = advsm->adv_pdu_start_time - g_ble_ll_sched_offset_ticks;
-    sch->remainder = 0;
-    sch->end_time = advsm->adv_pdu_start_time +
-                    ble_ll_usecs_to_ticks_round_up(max_usecs);
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-static int
-ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
-{
-    int rc;
-    uint8_t end_trans;
-    uint32_t txstart;
-    struct ble_ll_adv_sm *advsm;
-    ble_phy_tx_pducb_t pducb;
-
-    /* Get the state machine for the event */
-    advsm = (struct ble_ll_adv_sm *)sch->cb_arg;
-
-    /* Set the current advertiser */
-    g_ble_ll_cur_adv_sm = advsm;
-
-    ble_ll_adv_active_chanset_set_sec(advsm);
-
-    /* Set the power */
-    ble_phy_txpwr_set(advsm->adv_txpwr);
-
-    /* Set channel */
-    rc = ble_phy_setchan(advsm->adv_secondary_chan, BLE_ACCESS_ADDR_ADV,
-                         BLE_LL_CRCINIT_ADV);
-    assert(rc == 0);
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    /* Set phy mode */
-     ble_phy_mode_set(advsm->sec_phy, advsm->sec_phy);
-#endif
-
-    /* Set transmit start time. */
-    txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
-    rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
-    if (rc) {
-        STATS_INC(ble_ll_stats, adv_late_starts);
-        goto adv_tx_done;
-    }
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-    ble_phy_encrypt_disable();
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    advsm->adv_rpa_index = -1;
-    if (ble_ll_resolv_enabled()) {
-        ble_phy_resolv_list_enable();
-    } else {
-        ble_phy_resolv_list_disable();
-    }
-#endif
-
-    /* Set phy mode based on type of advertisement */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        end_trans = BLE_PHY_TRANSITION_TX_RX;
-        ble_phy_set_txend_cb(NULL, NULL);
-        pducb = ble_ll_adv_aux_pdu_make;
-    } else if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) &&
-               advsm->aux_first_pdu) {
-        end_trans = BLE_PHY_TRANSITION_TX_RX;
-        ble_phy_set_txend_cb(NULL, NULL);
-        pducb = ble_ll_adv_aux_scannable_pdu_make;
-    } else {
-        end_trans = BLE_PHY_TRANSITION_NONE;
-        ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
-        pducb = ble_ll_adv_aux_pdu_make;
-    }
-
-    /* Transmit advertisement */
-    rc = ble_phy_tx(pducb, advsm, end_trans);
-    if (rc) {
-        goto adv_tx_done;
-    }
-
-    /* Enable/disable whitelisting based on filter policy */
-    if (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE) {
-        ble_ll_whitelist_enable();
-    } else {
-        ble_ll_whitelist_disable();
-    }
-
-    /* Set link layer state to advertising */
-    ble_ll_state_set(BLE_LL_STATE_ADV);
-
-    /* Count # of adv. sent */
-    STATS_INC(ble_ll_stats, adv_txg);
-
-    return BLE_LL_SCHED_STATE_RUNNING;
-
-adv_tx_done:
-    ble_ll_adv_tx_done(advsm);
-    return BLE_LL_SCHED_STATE_DONE;
-}
-
-static uint8_t
-ble_ll_adv_aux_scannable_pdu_payload_len(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t len;
-
-    /* Flags, AdvA and ADI always */
-    len = BLE_LL_EXT_ADV_HDR_LEN + BLE_LL_EXT_ADV_FLAGS_SIZE +
-          BLE_LL_EXT_ADV_ADVA_SIZE + BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-
-    /* TargetA only for directed */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        len += BLE_LL_EXT_ADV_TARGETA_SIZE;
-    }
-
-    /* TxPower if configured */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_INC_TX_PWR) {
-        len += BLE_LL_EXT_ADV_TX_POWER_SIZE;
-    }
-
-    return len;
-}
-
-static void
-ble_ll_adv_aux_calculate(struct ble_ll_adv_sm *advsm,
-                         struct ble_ll_adv_aux *aux, uint16_t aux_data_offset)
-{
-    uint16_t rem_aux_data_len;
-    uint8_t hdr_len;
-    bool chainable;
-
-    assert(!aux->sch.enqueued);
-    assert((AUX_DATA_LEN(advsm) > aux_data_offset) ||
-           (AUX_DATA_LEN(advsm) == 0 && aux_data_offset == 0));
-
-    aux->aux_data_offset = aux_data_offset;
-    aux->aux_data_len = 0;
-    aux->payload_len = 0;
-
-    rem_aux_data_len = AUX_DATA_LEN(advsm) - aux_data_offset;
-    chainable = !(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE);
-
-    /* Flags and ADI */
-    aux->ext_hdr = (1 << BLE_LL_EXT_ADV_DATA_INFO_BIT);
-    hdr_len = BLE_LL_EXT_ADV_HDR_LEN + BLE_LL_EXT_ADV_FLAGS_SIZE +
-              BLE_LL_EXT_ADV_DATA_INFO_SIZE;
-
-    /* AdvA for 1st PDU in chain (i.e. AUX_ADV_IND or AUX_SCAN_RSP) */
-    if (aux_data_offset == 0) {
-        aux->ext_hdr |= (1 << BLE_LL_EXT_ADV_ADVA_BIT);
-        hdr_len += BLE_LL_EXT_ADV_ADVA_SIZE;
-    }
-
-    /* TargetA for directed connectable */
-    if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) &&
-        (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE)) {
-        aux->ext_hdr |= (1 << BLE_LL_EXT_ADV_TARGETA_BIT);
-        hdr_len += BLE_LL_EXT_ADV_TARGETA_SIZE;
-    }
-
-    /* TxPower if configured */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_INC_TX_PWR) {
-        aux->ext_hdr |= (1 << BLE_LL_EXT_ADV_TX_POWER_BIT);
-        hdr_len += BLE_LL_EXT_ADV_TX_POWER_SIZE;
-    }
-
-    /* AdvData always */
-    aux->aux_data_len = min(BLE_LL_MAX_PAYLOAD_LEN - hdr_len, rem_aux_data_len);
-
-    /* AuxPtr if there are more AdvData remaining that we can fit here */
-    if (chainable && (rem_aux_data_len > aux->aux_data_len)) {
-            aux->ext_hdr |= (1 << BLE_LL_EXT_ADV_AUX_PTR_BIT);
-            hdr_len += BLE_LL_EXT_ADV_AUX_PTR_SIZE;
-            aux->aux_data_len -= BLE_LL_EXT_ADV_AUX_PTR_SIZE;
-
-            /* PDU payload should be full if chained */
-            assert(hdr_len + aux->aux_data_len == BLE_LL_MAX_PAYLOAD_LEN);
-    }
-
-    aux->payload_len = hdr_len + aux->aux_data_len;
-}
-
-static void
-ble_ll_adv_aux_scheduled(struct ble_ll_adv_sm *advsm, uint32_t sch_start,
-                         void *arg)
-{
-    struct ble_ll_adv_aux *aux = arg;
-
-    aux->start_time = sch_start + g_ble_ll_sched_offset_ticks;
-}
-
-static void
-ble_ll_adv_aux_schedule_next(struct ble_ll_adv_sm *advsm)
-{
-    struct ble_ll_adv_aux *aux;
-    struct ble_ll_adv_aux *aux_next;
-    struct ble_ll_sched_item *sch;
-    uint16_t rem_aux_data_len;
-    uint16_t next_aux_data_offset;
-    uint32_t max_usecs;
-
-    assert(advsm->aux_active);
-
-    aux = AUX_CURRENT(advsm);
-    aux_next = AUX_NEXT(advsm);
-
-    assert(aux->sch.enqueued);
-    assert(!aux_next->sch.enqueued);
-
-    /*
-     * In general we do not schedule next aux if current aux does not have
-     * AuxPtr in extended header as this means we do not need subsequent
-     * ADV_CHAIN_IND to be sent.
-     * However, if current aux is scannable we allow to schedule next aux as
-     * this will be 1st ADV_CHAIN_IND of scan response.
-     */
-    if (!(aux->ext_hdr & (1 << BLE_LL_EXT_ADV_AUX_PTR_BIT))) {
-        return;
-    }
-
-    next_aux_data_offset = aux->aux_data_offset + aux->aux_data_len;
-
-    assert(AUX_DATA_LEN(advsm) >= next_aux_data_offset);
-
-    rem_aux_data_len = AUX_DATA_LEN(advsm) - next_aux_data_offset;
-    assert(rem_aux_data_len > 0);
-
-    ble_ll_adv_aux_calculate(advsm, aux_next, next_aux_data_offset);
-    max_usecs = ble_ll_pdu_tx_time_get(aux_next->payload_len, advsm->sec_phy);
-    max_usecs += XCVR_PROC_DELAY_USECS;
-
-    aux_next->start_time = aux->sch.end_time +
-                          ble_ll_usecs_to_ticks_round_up(BLE_LL_MAFS);
-
-    sch = &aux_next->sch;
-    sch->start_time = aux_next->start_time - g_ble_ll_sched_offset_ticks;
-    sch->remainder = 0;
-    sch->end_time = aux_next->start_time + os_cputime_usecs_to_ticks(max_usecs);
-    ble_ll_sched_adv_new(&aux_next->sch, ble_ll_adv_aux_scheduled, aux_next);
-}
-
-static void
-ble_ll_adv_aux_schedule_first(struct ble_ll_adv_sm *advsm)
-{
-    struct ble_ll_adv_aux *aux;
-    struct ble_ll_sched_item *sch;
-    uint32_t max_usecs;
-
-    assert(!advsm->aux_active);
-    assert(!advsm->aux[0].sch.enqueued);
-    assert(!advsm->aux[1].sch.enqueued);
-
-    advsm->aux_active = 1;
-    advsm->aux_index = 0;
-    advsm->aux_first_pdu = 1;
-    advsm->aux_not_scanned = 0;
-
-    aux = AUX_CURRENT(advsm);
-    ble_ll_adv_aux_calculate(advsm, aux, 0);
-
-    /* TODO we could use CSA2 for this
-     * (will be needed for periodic advertising anyway)
-     */
-    advsm->adv_secondary_chan = rand() % BLE_PHY_NUM_DATA_CHANS;
-
-    /* Set end time to maximum time this schedule item may take */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        max_usecs = ble_ll_pdu_tx_time_get(aux->payload_len, advsm->sec_phy) +
-                    BLE_LL_IFS +
-                    /* AUX_CONN_REQ */
-                    ble_ll_pdu_tx_time_get(34 + 14, advsm->sec_phy)  +
-                    BLE_LL_IFS +
-                    /* AUX_CONN_RSP */
-                    ble_ll_pdu_tx_time_get(14, advsm->sec_phy);
-    } else if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-        /* Scheduled aux is calculated for AUX_SCAN_RSP, 1st aux is created separately */
-        max_usecs = ble_ll_pdu_tx_time_get(ble_ll_adv_aux_scannable_pdu_payload_len(advsm),
-                                           advsm->sec_phy) +
-                    BLE_LL_IFS +
-                    /* AUX_SCAN_REQ */
-                    ble_ll_pdu_tx_time_get(12, advsm->sec_phy)  +
-                    BLE_LL_IFS +
-                    /* AUX_SCAN_RSP */
-                    ble_ll_pdu_tx_time_get(aux->payload_len, advsm->sec_phy);
-    } else {
-        max_usecs = ble_ll_pdu_tx_time_get(aux->payload_len, advsm->sec_phy);
-    }
-
-    /*
-     * XXX: For now, just schedule some additional time so we insure we have
-     * enough time to do everything we want.
-     */
-    max_usecs += XCVR_PROC_DELAY_USECS;
-
-    sch = &aux->sch;
-    sch->start_time = aux->start_time - g_ble_ll_sched_offset_ticks;
-    sch->remainder = 0;
-    sch->end_time = aux->start_time + os_cputime_usecs_to_ticks(max_usecs);
-    ble_ll_sched_adv_new(sch, ble_ll_adv_aux_scheduled, aux);
-
-}
-
-static void
-ble_ll_adv_aux_set_start_time(struct ble_ll_adv_sm *advsm)
-{
-    static const uint8_t bits[8] = {0, 1, 1, 2, 1, 2, 2, 3};
-    struct ble_ll_sched_item *sched = &advsm->adv_sch;
-    uint32_t adv_pdu_dur;
-    uint32_t adv_event_dur;
-    uint8_t chans;
-
-    assert(!advsm->aux_active);
-    assert(!advsm->aux[0].sch.enqueued);
-    assert(!advsm->aux[1].sch.enqueued);
-
-    assert(advsm->adv_chanmask > 0 &&
-           advsm->adv_chanmask <= BLE_HCI_ADV_CHANMASK_DEF);
-
-    chans = bits[advsm->adv_chanmask];
-
-    /*
-     * We want to schedule auxiliary packet as soon as possible after the end
-     * of advertising event, but no sooner than T_MAFS. The interval between
-     * advertising packets is 250 usecs (8.19 ticks) on LE Coded and a bit less
-     * on 1M, but it can vary a bit due to scheduling which we can't really
-     * control. Since we round ticks up for both interval and T_MAFS, we still
-     * have some margin here. The worst thing that can happen is that we skip
-     * last advertising packet which is not a bit problem so leave it as-is, no
-     * need to make code more complicated.
-     */
-
-    /*
-     * XXX: this could be improved if phy has TX-TX transition with controlled
-     *      or predefined interval, but since it makes advertising code even
-     *      more complicated let's skip it for now...
-     */
-
-    adv_pdu_dur = (int32_t)(sched->end_time - sched->start_time) -
-                  g_ble_ll_sched_offset_ticks;
-
-    /* 9 is 8.19 ticks rounded up - see comment above */
-    adv_event_dur = (adv_pdu_dur * chans) + (9 * (chans - 1));
-
-    advsm->aux[0].start_time = advsm->adv_event_start_time + adv_event_dur +
-                               ble_ll_usecs_to_ticks_round_up(BLE_LL_MAFS);
-}
-
-static void
-ble_ll_adv_aux_schedule(struct ble_ll_adv_sm *advsm)
-{
-    /*
-     * For secondary channel we always start by scheduling two consecutive
-     * auxiliary packets at once. Then, after sending one packet we try to
-     * schedule another one as long as there are some data left to send. This
-     * is to make sure we can always calculate AuxPtr to subsequent packet
-     * without need to scheduled it in an interrupt.
-     */
-
-    ble_ll_adv_aux_set_start_time(advsm);
-    ble_ll_adv_aux_schedule_first(advsm);
-    ble_ll_adv_aux_schedule_next(advsm);
-}
-#endif
-
-/**
- * Called when advertising need to be halted. This normally should not be called
- * and is only called when a scheduled item executes but advertising is still
- * running.
- *
- * Context: Interrupt
- */
-void
-ble_ll_adv_halt(void)
-{
-    struct ble_ll_adv_sm *advsm;
-
-    if (g_ble_ll_cur_adv_sm != NULL) {
-        advsm = g_ble_ll_cur_adv_sm;
-
-        ble_phy_txpwr_set(MYNEWT_VAL(BLE_LL_TX_PWR_DBM));
-
-        os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_sec_txdone_ev);
-#endif
-
-        ble_ll_log(BLE_LL_LOG_ID_ADV_TXDONE, ble_ll_state_get(),
-                   advsm->adv_instance, 0);
-        ble_ll_state_set(BLE_LL_STATE_STANDBY);
-        ble_ll_adv_active_chanset_clear(g_ble_ll_cur_adv_sm);
-        g_ble_ll_cur_adv_sm = NULL;
-    }
-}
-
-/**
- * Called by the HCI command parser when a set advertising parameters command
- * has been received.
- *
- * Context: Link Layer task (HCI command parser)
- *
- * @param cmd
- *
- * @return int
- */
-int
-ble_ll_adv_set_adv_params(uint8_t *cmd)
-{
-    uint8_t adv_type;
-    uint8_t adv_filter_policy;
-    uint8_t adv_chanmask;
-    uint8_t own_addr_type;
-    uint8_t peer_addr_type;
-    uint16_t adv_itvl_min;
-    uint16_t adv_itvl_max;
-    uint16_t min_itvl;
-    struct ble_ll_adv_sm *advsm;
-    uint16_t props;
-
-    advsm = &g_ble_ll_adv_sm[0];
-    if (advsm->adv_enabled) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* Make sure intervals are OK (along with advertising type */
-    adv_itvl_min = get_le16(cmd);
-    adv_itvl_max = get_le16(cmd + 2);
-    adv_type = cmd[4];
-
-    /*
-     * Get the filter policy now since we will ignore it if we are doing
-     * directed advertising
-     */
-    adv_filter_policy = cmd[14];
-
-    /* Assume min interval based on low duty cycle/indirect advertising */
-    min_itvl = BLE_LL_ADV_ITVL_MIN;
-
-    switch (adv_type) {
-    case BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD:
-        adv_filter_policy = BLE_HCI_ADV_FILT_NONE;
-        memcpy(advsm->peer_addr, cmd + 7, BLE_DEV_ADDR_LEN);
-
-        /* Ignore min/max interval */
-        min_itvl = 0;
-        adv_itvl_min = 0;
-        adv_itvl_max = 0;
-
-        props = BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_HD_DIR ;
-        break;
-    case BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD:
-        adv_filter_policy = BLE_HCI_ADV_FILT_NONE;
-        memcpy(advsm->peer_addr, cmd + 7, BLE_DEV_ADDR_LEN);
-
-        props = BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_LD_DIR ;
-        break;
-    case BLE_HCI_ADV_TYPE_ADV_IND:
-        props = BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_IND;
-        break;
-    case BLE_HCI_ADV_TYPE_ADV_NONCONN_IND:
-        min_itvl = 0;
-        props = BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_NONCONN;
-        break;
-    case BLE_HCI_ADV_TYPE_ADV_SCAN_IND:
-        min_itvl = 0;
-        props = BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_SCAN;
-        break;
-    default:
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Make sure interval minimum is valid for the advertising type */
-    if ((adv_itvl_min > adv_itvl_max) || (adv_itvl_min < min_itvl) ||
-        (adv_itvl_min > BLE_HCI_ADV_ITVL_MAX) ||
-        (adv_itvl_max > BLE_HCI_ADV_ITVL_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check own and peer address type */
-    own_addr_type =  cmd[5];
-    peer_addr_type = cmd[6];
-
-    if ((own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) ||
-        (peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm->adv_txpwr = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        /* Copy peer address */
-        memcpy(advsm->peer_addr, cmd + 7, BLE_DEV_ADDR_LEN);
-
-        /* Reset RPA timer so we generate a new RPA */
-        advsm->adv_rpa_timer = os_time_get();
-    }
-#else
-    /* If we dont support privacy some address types wont work */
-    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        return BLE_ERR_UNSUPPORTED;
-    }
-#endif
-
-    /* There are only three adv channels, so check for any outside the range */
-    adv_chanmask = cmd[13];
-    if (((adv_chanmask & 0xF8) != 0) || (adv_chanmask == 0)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check for valid filter policy */
-    if (adv_filter_policy > BLE_HCI_ADV_FILT_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Fill out rest of advertising state machine */
-    advsm->own_addr_type = own_addr_type;
-    advsm->peer_addr_type = peer_addr_type;
-    advsm->adv_filter_policy = adv_filter_policy;
-    advsm->adv_chanmask = adv_chanmask;
-    advsm->adv_itvl_min = adv_itvl_min;
-    advsm->adv_itvl_max = adv_itvl_max;
-    advsm->props = props;
-
-    return 0;
-}
-
-/**
- * Stop advertising state machine
- *
- * Context: Link Layer task.
- *
- * @param advsm
- */
-static void
-ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
-{
-    os_sr_t sr;
-
-    if (advsm->adv_enabled) {
-        /* Remove any scheduled advertising items */
-        ble_ll_sched_rmv_elem(&advsm->adv_sch);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        advsm->aux_active = 0;
-        ble_ll_sched_rmv_elem(&advsm->aux[0].sch);
-        ble_ll_sched_rmv_elem(&advsm->aux[1].sch);
-#endif
-
-        /* Set to standby if we are no longer advertising */
-        OS_ENTER_CRITICAL(sr);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        if (g_ble_ll_cur_adv_sm == advsm) {
-            ble_phy_disable();
-            ble_ll_wfr_disable();
-            ble_ll_state_set(BLE_LL_STATE_STANDBY);
-            g_ble_ll_cur_adv_sm = NULL;
-            ble_ll_scan_chk_resume();
-        }
-#else
-        if (ble_ll_state_get() == BLE_LL_STATE_ADV) {
-            ble_phy_disable();
-            ble_ll_wfr_disable();
-            ble_ll_state_set(BLE_LL_STATE_STANDBY);
-            g_ble_ll_cur_adv_sm = NULL;
-            ble_ll_scan_chk_resume();
-        }
-#endif
-#ifdef BLE_XCVR_RFCLK
-        ble_ll_sched_rfclk_chk_restart();
-#endif
-        OS_EXIT_CRITICAL(sr);
-
-        os_eventq_remove(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        os_eventq_remove(&g_ble_ll_data.ll_evq, &advsm->adv_sec_txdone_ev);
-#endif
-
-        /* If there is an event buf we need to free it */
-        if (advsm->conn_comp_ev) {
-            ble_hci_trans_buf_free(advsm->conn_comp_ev);
-            advsm->conn_comp_ev = NULL;
-        }
-
-        ble_ll_adv_active_chanset_clear(advsm);
-
-        /* Disable advertising */
-        advsm->adv_enabled = 0;
-    }
-}
-
-static void
-ble_ll_adv_scheduled(struct ble_ll_adv_sm *advsm, uint32_t sch_start, void *arg)
-{
-    /* The event start time is when we start transmission of the adv PDU */
-    advsm->adv_event_start_time = sch_start + g_ble_ll_sched_offset_ticks;
-    advsm->adv_pdu_start_time = advsm->adv_event_start_time;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* this is validated for HD adv so no need to do additional checks here
-     * duration is in 10ms units
-     */
-    if (advsm->duration) {
-        advsm->adv_end_time = advsm->adv_event_start_time +
-                             os_cputime_usecs_to_ticks(advsm->duration * 10000);
-    }
-#else
-    /* Set the time at which we must end directed, high-duty cycle advertising.
-     */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-        advsm->adv_end_time = advsm->adv_event_start_time +
-                     os_cputime_usecs_to_ticks(BLE_LL_ADV_STATE_HD_MAX * 1000);
-    }
-#endif
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-#endif
-
-/**
- * Start the advertising state machine. This is called when the host sends
- * the "enable advertising" command and is not called again while in the
- * advertising state.
- *
- * Context: Link-layer task.
- *
- * @param advsm Pointer to advertising state machine
- *
- * @return int
- */
-static int
-ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t adv_chan;
-    uint8_t *addr;
-    uint8_t *evbuf;
-
-    /* only clear flags that are not set from HCI */
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_TX_ADD;
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_RX_ADD;
-    advsm->flags &= ~BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD;
-
-    if (advsm->own_addr_type == BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        if (!ble_ll_is_valid_random_addr(advsm->adv_random_addr)) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-#else
-        if (!ble_ll_is_valid_random_addr(g_random_addr)) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-#endif
-    }
-
-    /*
-     * Get an event with which to send the connection complete event if
-     * this is connectable
-     */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        /* We expect this to be NULL but if not we wont allocate one... */
-        if (advsm->conn_comp_ev == NULL) {
-            evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-            if (!evbuf) {
-                return BLE_ERR_MEM_CAPACITY;
-            }
-            advsm->conn_comp_ev = evbuf;
-        }
-    }
-
-    /* Set advertising address */
-    if ((advsm->own_addr_type & 1) == 0) {
-        addr = g_dev_addr;
-    } else {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        addr = advsm->adv_random_addr;
-#else
-        addr = g_random_addr;
-#endif
-        advsm->flags |= BLE_LL_ADV_SM_FLAG_TX_ADD;
-    }
-    memcpy(advsm->adva, addr, BLE_DEV_ADDR_LEN);
-
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        memcpy(advsm->initiator_addr, advsm->peer_addr, BLE_DEV_ADDR_LEN);
-        if (advsm->peer_addr_type & 1) {
-            advsm->flags |= BLE_LL_ADV_SM_FLAG_RX_ADD;
-        }
-    }
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    /* This will generate an RPA for both initiator addr and adva */
-    if (advsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        ble_ll_adv_rpa_update(advsm);
-    }
-#endif
-
-    /* Set flag telling us that advertising is enabled */
-    advsm->adv_enabled = 1;
-
-    /* Determine the advertising interval we will use */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-        /* Set it to max. allowed for high duty cycle advertising */
-        advsm->adv_itvl_usecs = BLE_LL_ADV_PDU_ITVL_HD_MS_MAX;
-    } else {
-        advsm->adv_itvl_usecs = (uint32_t)advsm->adv_itvl_max;
-        advsm->adv_itvl_usecs *= BLE_LL_ADV_ITVL;
-    }
-
-    /* Set first advertising channel */
-    adv_chan = ble_ll_adv_first_chan(advsm);
-    advsm->adv_chan = adv_chan;
-
-    /*
-     * XXX: while this may not be the most efficient, schedule the first
-     * advertising event some time in the future (5 msecs). This will give
-     * time to start up any clocks or anything and also avoid a bunch of code
-     * to check if we are currently doing anything. Just makes this simple.
-     *
-     * Might also want to align this on a slot in the future.
-     *
-     * NOTE: adv_event_start_time gets set by the sched_adv_new
-     */
-    advsm->adv_pdu_start_time = os_cputime_get32() +
-                                os_cputime_usecs_to_ticks(5000);
-
-    /*
-     * Schedule advertising. We set the initial schedule start and end
-     * times to the earliest possible start/end.
-     */
-    ble_ll_adv_set_sched(advsm);
-    ble_ll_sched_adv_new(&advsm->adv_sch, ble_ll_adv_scheduled, NULL);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY)) {
-        ble_ll_adv_aux_schedule(advsm);
-    }
-#endif
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Called when the LE HCI command read advertising channel tx power command
- * has been received. Returns the current advertising transmit power.
- *
- * Context: Link Layer task (HCI command parser)
- *
- * @return int
- */
-int
-ble_ll_adv_read_txpwr(uint8_t *rspbuf, uint8_t *rsplen)
-{
-    rspbuf[0] = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
-    *rsplen = 1;
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Turn advertising on/off.
- *
- * Context: Link Layer task
- *
- * @param cmd
- *
- * @return int
- */
-int
-ble_ll_adv_set_enable(uint8_t instance, uint8_t enable, int duration,
-                          uint8_t events)
-{
-    int rc;
-    struct ble_ll_adv_sm *advsm;
-
-    if (instance >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[instance];
-
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    rc = BLE_ERR_SUCCESS;
-    if (enable == 1) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        /* handle specifics of HD dir adv enabled in legacy way */
-        if (duration < 0) {
-            if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-                duration = BLE_LL_ADV_STATE_HD_MAX / 10;
-            } else {
-                duration = 0;
-            }
-        }
-        advsm->duration = duration;
-        advsm->events_max = events;
-        advsm->events = 0;
-#endif
-
-        /* If already enabled, do nothing */
-        if (!advsm->adv_enabled) {
-            /* Start the advertising state machine */
-            rc = ble_ll_adv_sm_start(advsm);
-        }
-    } else if (enable == 0) {
-        ble_ll_adv_sm_stop(advsm);
-    } else {
-        rc = BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    return rc;
-}
-
-static void
-ble_ll_adv_update_data_mbuf(struct os_mbuf **omp, bool new_data, uint16_t maxlen,
-                            const void *data, uint16_t datalen)
-{
-    struct os_mbuf *om;
-    int ret;
-
-    om = *omp;
-
-    if (new_data) {
-        if (om) {
-            os_mbuf_free_chain(om);
-        }
-
-        om = os_msys_get_pkthdr(datalen, 0);
-        if (!om) {
-            goto done;
-        }
-    }
-
-    assert(om);
-
-    if (OS_MBUF_PKTLEN(om) + datalen > maxlen) {
-        os_mbuf_free_chain(om);
-        om = NULL;
-        goto done;
-    }
-
-    ret = os_mbuf_append(om, data, datalen);
-    if (ret) {
-        os_mbuf_free_chain(om);
-        om = NULL;
-    }
-
-done:
-    *omp = om;
-}
-
-/**
- * Set the scan response data that the controller will send.
- *
- * @param cmd
- * @param len
- *
- * @return int
- */
-int
-ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t instance, uint8_t operation)
-{
-    uint8_t datalen;
-    struct ble_ll_adv_sm *advsm;
-    bool new_data;
-
-    if (instance >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[instance];
-    datalen = cmd[0];
-
-    /* check if type of advertising support scan rsp */
-    if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE)) {
-        if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY)) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-    }
-
-    switch (operation) {
-    case BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_COMPLETE:
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            if (datalen > BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN) {
-                return BLE_ERR_INV_HCI_CMD_PARMS;
-            }
-        }
-
-        break;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    case BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_LAST:
-        /* TODO mark scan rsp as complete? */
-        /* fall through */
-    case BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_INT:
-        if (!advsm->scan_rsp_data) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->adv_enabled) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-
-        if (!datalen) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-        break;
-    case BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_FIRST:
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->adv_enabled) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-
-        if (!datalen) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        break;
-#endif
-    default:
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    new_data = (operation == BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_COMPLETE) ||
-               (operation == BLE_HCI_LE_SET_EXT_SCAN_RSP_DATA_OPER_FIRST);
-
-    ble_ll_adv_update_data_mbuf(&advsm->scan_rsp_data, new_data,
-                                BLE_SCAN_RSP_DATA_MAX_LEN, cmd + 1, datalen);
-    if (!advsm->scan_rsp_data) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* DID shall be updated when host provides new scan response data */
-    advsm->adi = (advsm->adi & 0xf000) | (rand() & 0x0fff);
-#endif
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Called by the LL HCI command parser when a set advertising
- * data command has been sent from the host to the controller.
- *
- * @param cmd Pointer to command data
- * @param len Length of command data
- *
- * @return int 0: success; BLE_ERR_INV_HCI_CMD_PARMS otherwise.
- */
-int
-ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t instance, uint8_t operation)
-{
-    uint8_t datalen;
-    struct ble_ll_adv_sm *advsm;
-    bool new_data;
-
-    if (instance >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[instance];
-    datalen = cmd[0];
-
-    /* check if type of advertising support adv data */
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-    } else {
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-    }
-
-    switch (operation) {
-    case BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_COMPLETE:
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            if (datalen > BLE_ADV_LEGACY_DATA_MAX_LEN) {
-                return BLE_ERR_INV_HCI_CMD_PARMS;
-            }
-        }
-
-        advsm->flags &= ~BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE;
-        break;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    case BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_UNCHANGED:
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (!advsm->adv_enabled || !ADV_DATA_LEN(advsm) || datalen) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* update DID only */
-        advsm->adi = (advsm->adi & 0xf000) | (rand() & 0x0fff);
-        return BLE_ERR_SUCCESS;
-    case BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_LAST:
-        advsm->flags &= ~BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE;
-        /* fall through */
-    case BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_INT:
-        if (!advsm->adv_data) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (!datalen) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->adv_enabled) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-        break;
-    case BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_FIRST:
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        if (advsm->adv_enabled) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-
-        if (!datalen) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        advsm->flags |= BLE_LL_ADV_SM_FLAG_ADV_DATA_INCOMPLETE;
-        break;
-#endif
-    default:
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    new_data = (operation == BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_COMPLETE) ||
-               (operation == BLE_HCI_LE_SET_EXT_ADV_DATA_OPER_FIRST);
-
-    ble_ll_adv_update_data_mbuf(&advsm->adv_data, new_data, BLE_ADV_DATA_MAX_LEN,
-                                cmd + 1, datalen);
-    if (!advsm->adv_data) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* DID shall be updated when host provides new advertising data */
-    advsm->adi = (advsm->adi & 0xf000) | (rand() & 0x0fff);
-#endif
-
-    return BLE_ERR_SUCCESS;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-int
-ble_ll_adv_ext_set_param(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
-{
-    uint8_t adv_filter_policy;
-    uint8_t adv_chanmask;
-    uint8_t own_addr_type;
-    uint8_t peer_addr_type;
-    uint32_t adv_itvl_min;
-    uint32_t adv_itvl_max;
-    uint16_t min_itvl = 0;
-    uint16_t props;
-    struct ble_ll_adv_sm *advsm;
-    uint8_t pri_phy;
-    uint8_t sec_phy;
-    uint8_t sid;
-    uint8_t scan_req_notif;
-    int8_t tx_power;
-
-    if (cmdbuf[0] >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[cmdbuf[0]];
-    if (advsm->adv_enabled) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    props = get_le16(&cmdbuf[1]);
-
-    adv_itvl_min = cmdbuf[5] << 16 | cmdbuf[4] << 8 | cmdbuf[3];
-    adv_itvl_max = cmdbuf[8] << 16 | cmdbuf[7] << 8 | cmdbuf[6];
-
-    if (props & ~BLE_HCI_LE_SET_EXT_ADV_PROP_MASK) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        if (ADV_DATA_LEN(advsm) > BLE_ADV_LEGACY_DATA_MAX_LEN ||
-            SCAN_RSP_DATA_LEN(advsm) > BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* if legacy bit is set possible values are limited */
-        switch (props) {
-        case BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_IND:
-        case BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_LD_DIR:
-        case BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_HD_DIR:
-        case BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_SCAN:
-        case BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY_NONCONN:
-            break;
-        default:
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-    } else {
-        /* HD directed advertising allowed only on legacy PDUs */
-        if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* if ext advertising PDUs are used then it shall not be both
-         * connectable and scanable
-         */
-        if ((props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) &&
-            (props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE)) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-    }
-
-    if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-        min_itvl = BLE_LL_ADV_ITVL_MIN;
-    }
-
-    if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-        if (ADV_DATA_LEN(advsm) || SCAN_RSP_DATA_LEN(advsm)) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* Ignore min/max interval */
-        min_itvl = 0;
-        adv_itvl_min = 0;
-        adv_itvl_max = 0;
-    }
-
-    /* Make sure interval minimum is valid for the advertising type
-     * TODO for now limit those to values from legacy advertising
-     */
-    if ((adv_itvl_min > adv_itvl_max) || (adv_itvl_min < min_itvl) ||
-        (adv_itvl_min > BLE_HCI_ADV_ITVL_MAX) ||
-        (adv_itvl_max > BLE_HCI_ADV_ITVL_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* There are only three adv channels, so check for any outside the range */
-    adv_chanmask = cmdbuf[9];
-    if (((adv_chanmask & 0xF8) != 0) || (adv_chanmask == 0)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check own and peer address type */
-    own_addr_type = cmdbuf[10];
-    peer_addr_type = cmdbuf[11];
-
-    if ((own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) ||
-        (peer_addr_type > BLE_HCI_ADV_PEER_ADDR_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        /* Reset RPA timer so we generate a new RPA */
-        advsm->adv_rpa_timer = os_time_get();
-    }
-#else
-    /* If we dont support privacy some address types wont work */
-    if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        return BLE_ERR_UNSUPPORTED;
-    }
-#endif
-
-    adv_filter_policy = cmdbuf[18];
-    /* Check filter policy (valid only for undirected */
-    if (!(props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) &&
-         adv_filter_policy > BLE_HCI_ADV_FILT_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    pri_phy = cmdbuf[20];
-    if (pri_phy != BLE_HCI_LE_PHY_1M && pri_phy != BLE_HCI_LE_PHY_CODED) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    sec_phy = cmdbuf[22];
-    if (sec_phy != BLE_HCI_LE_PHY_1M && sec_phy != BLE_HCI_LE_PHY_2M &&
-            sec_phy != BLE_HCI_LE_PHY_CODED) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    sid = cmdbuf[23];
-    if (sid > 0x0f) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    scan_req_notif = cmdbuf[24];
-    if (scan_req_notif > 0x01) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-        memcpy(advsm->peer_addr, &cmdbuf[12], BLE_DEV_ADDR_LEN);
-    }
-
-    tx_power = (int8_t) rspbuf[21];
-    if (tx_power == 127) {
-        /* no preference */
-        advsm->adv_txpwr = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
-    } else {
-        advsm->adv_txpwr = ble_phy_txpower_round(tx_power);
-    }
-
-    advsm->own_addr_type = own_addr_type;
-    advsm->peer_addr_type = peer_addr_type;
-    advsm->adv_filter_policy = adv_filter_policy;
-    advsm->adv_chanmask = adv_chanmask;
-    advsm->adv_itvl_min = adv_itvl_min;
-    advsm->adv_itvl_max = adv_itvl_max;
-    advsm->pri_phy = pri_phy;
-    advsm->sec_phy = sec_phy;
-    /* Update SID only */
-    advsm->adi = (advsm->adi & 0x0fff) | ((sid << 12));
-
-    advsm->props = props;
-
-    /* Set proper mbuf chain for aux data */
-    if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        advsm->aux_data = NULL;
-    } else if (props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-        advsm->aux_data = &advsm->scan_rsp_data;
-    } else {
-        advsm->aux_data = &advsm->adv_data;
-    }
-
-    if (scan_req_notif) {
-        advsm->flags |= BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF;
-    } else {
-        advsm->flags &= ~BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF;
-    }
-
-    rspbuf[0] = advsm->adv_txpwr;
-    *rsplen = 1;
-
-    return 0;
-}
-
-int
-ble_ll_adv_ext_set_adv_data(uint8_t *cmdbuf, uint8_t cmdlen)
-{
-    /* check if length is correct */
-    if (cmdlen < 4) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* TODO fragment preference ignored for now */
-
-    return ble_ll_adv_set_adv_data(cmdbuf + 3, cmdbuf[0], cmdbuf[1]);
-}
-
-int
-ble_ll_adv_ext_set_scan_rsp(uint8_t *cmdbuf, uint8_t cmdlen)
-{
-    /* check if length is correct */
-    if (cmdlen < 4) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* TODO fragment preference ignored for now */
-
-    return ble_ll_adv_set_scan_rsp_data(cmdbuf + 3, cmdbuf[0], cmdbuf[1]);
-}
-
-struct ext_adv_set {
-    uint8_t handle;
-    uint16_t duration;
-    uint8_t events;
-} __attribute__((packed));
-
-/**
- * HCI LE extended advertising enable command
- *
- * @param cmd Pointer to command data
- * @param len Command data length
- *
- * @return int BLE error code
- */
-int
-ble_ll_adv_ext_set_enable(uint8_t *cmd, uint8_t len)
-{
-    struct ble_ll_adv_sm *advsm;
-    struct ext_adv_set* set;
-    uint8_t enable;
-    uint8_t sets;
-    int i, j, rc;
-
-    if (len < 2) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    enable = cmd[0];
-    sets = cmd[1];
-    cmd += 2;
-
-    /* check if length is correct */
-    if (len != 2 + (sets * sizeof (*set))) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    if (sets > BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    if (sets == 0) {
-        if (enable) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* disable all instances */
-        for (i = 0; i < BLE_ADV_INSTANCES; i++) {
-            ble_ll_adv_set_enable(i, 0, 0, 0);
-        }
-
-        return BLE_ERR_SUCCESS;
-    }
-
-    set = (void *) cmd;
-    /* validate instances */
-    for (i = 0; i < sets; i++) {
-        if (set->handle >= BLE_ADV_INSTANCES) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        /* validate duplicated sets */
-        for (j = 1; j < sets - i; j++) {
-            if (set->handle == set[j].handle) {
-                return BLE_ERR_INV_HCI_CMD_PARMS;
-            }
-        }
-
-        if (enable) {
-            advsm = &g_ble_ll_adv_sm[set->handle];
-
-            if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-                if (set->duration == 0 || le16toh(set->duration) > 128) {
-                    return BLE_ERR_INV_HCI_CMD_PARMS;
-                }
-            }
-        }
-
-        set++;
-    }
-
-    set = (void *) cmd;
-    for (i = 0; i < sets; i++) {
-        rc = ble_ll_adv_set_enable(set->handle, enable, le16toh(set->duration),
-                                   set->events);
-        if (rc) {
-            return rc;
-        }
-
-        set++;
-    }
-
-    return BLE_ERR_SUCCESS;
-}
-
-int
-ble_ll_adv_set_random_addr(uint8_t *addr, uint8_t instance)
-{
-    struct ble_ll_adv_sm *advsm;
-
-    if (instance >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[instance];
-
-    /*
-     * Reject if connectable advertising is on
-     * Core Spec Vol. 2 Part E 7.8.52
-     */
-    if (advsm->adv_enabled &&
-            (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE)) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    memcpy(advsm->adv_random_addr, addr, BLE_DEV_ADDR_LEN);
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * HCI LE extended advertising remove command
- *
- * @param instance Advertising instance to be removed
- *
- * @return int BLE error code
- */
-int
-ble_ll_adv_remove(uint8_t instance)
-{
-    struct ble_ll_adv_sm *advsm;
-
-    /* TODO
-     * Should we allow any value for instance ID?
-     */
-
-    if (instance >= BLE_ADV_INSTANCES) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    advsm = &g_ble_ll_adv_sm[instance];
-
-    if (advsm->adv_enabled) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    if (advsm->adv_data) {
-        os_mbuf_free_chain(advsm->adv_data);
-    }
-    if (advsm->scan_rsp_data) {
-        os_mbuf_free_chain(advsm->scan_rsp_data);
-    }
-
-    ble_ll_adv_sm_init(advsm);
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * HCI LE extended advertising clear command
- *
- * @return int BLE error code
- */
-int
-ble_ll_adv_clear_all(void)
-{
-    int i;
-
-    for (i = 0; i < BLE_ADV_INSTANCES; i++) {
-        if (g_ble_ll_adv_sm[i].adv_enabled) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-    }
-
-    ble_ll_adv_reset();
-
-    return BLE_ERR_SUCCESS;
-}
-#endif
-
-
-/**
- * Called when the LL receives a scan request or connection request
- *
- * Context: Called from interrupt context.
- *
- * @param rxbuf
- *
- * @return -1: request not for us or is a connect request.
- *          0: request (scan) is for us and we successfully went from rx to tx.
- *        > 0: PHY error attempting to go from rx to tx.
- */
-static int
-ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
-{
-    int rc;
-    int resolved;
-    uint8_t chk_wl;
-    uint8_t txadd;
-    uint8_t peer_addr_type;
-    uint8_t *rxbuf;
-    uint8_t *adva;
-    uint8_t *peer;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct ble_ll_adv_sm *advsm;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    struct aux_conn_rsp_data rsp_data;
-#endif
-
-    /* See if adva in the request (scan or connect) matches what we sent */
-    advsm = g_ble_ll_cur_adv_sm;
-    rxbuf = rxpdu->om_data;
-    adva = rxbuf + BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN;
-    if (memcmp(advsm->adva, adva, BLE_DEV_ADDR_LEN)) {
-        return -1;
-    }
-
-    /* Set device match bit if we are whitelisting */
-    if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
-        chk_wl = advsm->adv_filter_policy & 1;
-    } else {
-        chk_wl = advsm->adv_filter_policy & 2;
-    }
-
-    /* Get the peer address type */
-    if (rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK) {
-        txadd = BLE_ADDR_RANDOM;
-    } else {
-        txadd = BLE_ADDR_PUBLIC;
-    }
-
-    ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
-    peer = rxbuf + BLE_LL_PDU_HDR_LEN;
-    peer_addr_type = txadd;
-    resolved = 0;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    if (ble_ll_is_rpa(peer, txadd) && ble_ll_resolv_enabled()) {
-        advsm->adv_rpa_index = ble_hw_resolv_list_match();
-        if (advsm->adv_rpa_index >= 0) {
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_RESOLVED;
-            if (chk_wl) {
-                peer = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_identity_addr;
-                peer_addr_type = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_addr_type;
-                resolved = 1;
-            }
-        } else {
-            if (chk_wl) {
-                return -1;
-            }
-        }
-    }
-#endif
-
-    /* Set device match bit if we are whitelisting */
-    if (chk_wl && !ble_ll_whitelist_match(peer, peer_addr_type, resolved)) {
-        return -1;
-    }
-
-    /*
-     * We set the device match bit to tell the upper layer that we will
-     * accept the request
-     */
-    ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_DEVMATCH;
-
-    /* Setup to transmit the scan response if appropriate */
-    rc = -1;
-
-    if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
-        /* XXX TODO: assume we do not need to change phy mode */
-        ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        if (advsm->flags & BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF) {
-            ble_ll_hci_ev_send_scan_req_recv(advsm->adv_instance, peer,
-                                             peer_addr_type);
-        }
-
-        /*
-         * We need to store current rxed packet header temporarily so AuxPtr
-         * can be calculated (if necessary) relative to AUX_SCAN_RSP instead of
-         * AUX_ADV_IND.
-         */
-
-        advsm->rx_ble_hdr = ble_hdr;
-        rc = ble_phy_tx(ble_ll_adv_scan_rsp_pdu_make, advsm,
-                        BLE_PHY_TRANSITION_NONE);
-        advsm->rx_ble_hdr = NULL;
-#else
-        rc = ble_phy_tx(ble_ll_adv_scan_rsp_legacy_pdu_make, advsm,
-                        BLE_PHY_TRANSITION_NONE);
-#endif
-
-        if (!rc) {
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_SCAN_RSP_TXD;
-            STATS_INC(ble_ll_stats, scan_rsp_txg);
-        }
-    } else if (pdu_type == BLE_ADV_PDU_TYPE_AUX_CONNECT_REQ) {
-        /*
-         * Only accept connect requests from the desired address if we
-         * are doing directed advertising
-         */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-            if (memcmp(advsm->peer_addr, peer, BLE_DEV_ADDR_LEN)) {
-                return -1;
-            }
-        }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            return -1;
-        }
-
-        /* use remote address used over the air */
-        rsp_data.advsm = advsm;
-        rsp_data.peer = rxbuf + BLE_LL_PDU_HDR_LEN;
-        rsp_data.rxadd = rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK;
-
-        ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
-        rc = ble_phy_tx(ble_ll_adv_aux_conn_rsp_pdu_make, &rsp_data,
-                        BLE_PHY_TRANSITION_NONE);
-        if (!rc) {
-            advsm->flags |= BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD;
-            STATS_INC(ble_ll_stats, aux_conn_rsp_tx);
-        }
-#endif
-    }
-
-    return rc;
-}
-
-/**
- * Called when a connect request has been received.
- *
- * Context: Link Layer
- *
- * @param rxbuf
- * @param flags
- *
- * @return 0: no connection started. 1: connection started
- */
-static int
-ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr,
-                        struct ble_ll_adv_sm *advsm)
-{
-    int valid;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    uint8_t resolved;
-#endif
-    uint8_t addr_type;
-    uint8_t *inita;
-    uint8_t *ident_addr;
-
-    /* Don't create connection if AUX_CONNECT_RSP was not send */
-    if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY)) {
-        if (!(advsm->flags & BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD)) {
-            return 0;
-        }
-    }
-
-    /* Check filter policy. */
-    valid = 0;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    resolved = BLE_MBUF_HDR_RESOLVED(hdr);
-#endif
-    inita = rxbuf + BLE_LL_PDU_HDR_LEN;
-    if (hdr->rxinfo.flags & BLE_MBUF_HDR_F_DEVMATCH) {
-
-        valid = 1;
-        if (rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK) {
-            addr_type = BLE_ADDR_RANDOM;
-        } else {
-            addr_type = BLE_ADDR_PUBLIC;
-        }
-
-        /*
-         * Only accept connect requests from the desired address if we
-         * are doing directed advertising
-         */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_DIRECTED) {
-            ident_addr = inita;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-            if (resolved) {
-                ident_addr = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_identity_addr;
-                addr_type = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_addr_type;
-            }
-#endif
-            if ((addr_type != advsm->peer_addr_type) ||
-                memcmp(advsm->peer_addr, ident_addr, BLE_DEV_ADDR_LEN)) {
-                valid = 0;
-            }
-        }
-    }
-
-    if (valid) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-        if (resolved) {
-            /* Retain the resolvable private address that we received. */
-            memcpy(advsm->adv_rpa, inita, BLE_DEV_ADDR_LEN);
-
-            /*
-             * Overwrite received inita with identity address since that
-             * is used from now on.
-             */
-            memcpy(inita,
-                   g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_identity_addr,
-                   BLE_DEV_ADDR_LEN);
-
-            /* Peer address type is an identity address */
-            addr_type = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_addr_type;
-            addr_type += 2;
-        }
-#endif
-
-        /* Try to start slave connection. If successful, stop advertising */
-        valid = ble_ll_conn_slave_start(rxbuf, addr_type, hdr,
-                          !(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY));
-        if (valid) {
-            /* stop advertising only if not transmitting connection response */
-            if (!(advsm->flags & BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD)) {
-                ble_ll_adv_sm_stop(advsm);
-            }
-        }
-    }
-
-    return valid;
-}
-
-/**
- * Called on phy rx pdu end when in advertising state.
- *
- * There are only two pdu types we care about in this state: scan requests
- * and connection requests. When we receive a scan request we must determine if
- * we need to send a scan response and that needs to be acted on within T_IFS.
- *
- * When we receive a connection request, we need to determine if we will allow
- * this device to start a connection with us. However, no immediate response is
- * sent so we handle this at the link layer task.
- *
- * Context: Interrupt
- *
- * @param pdu_type Type of pdu received.
- * @param rxpdu Pointer to received PDU
- *
- * @return int
- *       < 0: Disable the phy after reception.
- *      == 0: Do not disable the PHY
- *       > 0: Do not disable PHY as that has already been done.
- */
-int
-ble_ll_adv_rx_isr_end(uint8_t pdu_type, struct os_mbuf *rxpdu, int crcok)
-{
-    int rc;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    struct ble_mbuf_hdr *rxhdr;
-#endif
-
-    rc = -1;
-    if (rxpdu == NULL) {
-        ble_ll_adv_tx_done(g_ble_ll_cur_adv_sm);
-    } else {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        rxhdr = BLE_MBUF_HDR_PTR(rxpdu);
-        rxhdr->rxinfo.user_data = g_ble_ll_cur_adv_sm;
-        if (ble_ll_adv_active_chanset_is_sec(g_ble_ll_cur_adv_sm)) {
-            rxhdr->rxinfo.flags |= BLE_MBUF_HDR_F_EXT_ADV_SEC;
-        } else {
-            assert(ble_ll_adv_active_chanset_is_pri(g_ble_ll_cur_adv_sm));
-        }
-#endif
-        if (crcok) {
-            if ((pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) ||
-                (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ)) {
-                /* Process request */
-                rc = ble_ll_adv_rx_req(pdu_type, rxpdu);
-            }
-        }
-
-        if (rc) {
-            /* We no longer have a current state machine */
-            g_ble_ll_cur_adv_sm = NULL;
-        }
-    }
-
-    if (rc) {
-        ble_ll_state_set(BLE_LL_STATE_STANDBY);
-    }
-
-    return rc;
-}
-
-/**
- * Process a received packet at the link layer task when in the advertising
- * state
- *
- * Context: Link Layer
- *
- *
- * @param ptype
- * @param rxbuf
- * @param hdr
- *
- * @return int
- */
-void
-ble_ll_adv_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
-{
-    int adv_event_over;
-    struct ble_ll_adv_sm *advsm;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    advsm = (struct ble_ll_adv_sm *)hdr->rxinfo.user_data;
-#else
-    advsm = &g_ble_ll_adv_sm[0];
-#endif
-
-    /*
-     * It is possible that advertising was stopped and a packet plcaed on the
-     * LL receive packet queue. In this case, just ignore the received packet
-     * as the advertising state machine is no longer "valid"
-     */
-    if (!advsm->adv_enabled) {
-        return;
-    }
-
-    /*
-     * If we have received a scan request and we are transmitting a response
-     * or we have received a valid connect request, dont "end" the advertising
-     * event. In the case of a connect request we will stop advertising. In
-     * the case of the scan response transmission we will get a transmit
-     * end callback.
-     */
-    adv_event_over = 1;
-    if (BLE_MBUF_HDR_CRC_OK(hdr)) {
-        if (ptype == BLE_ADV_PDU_TYPE_CONNECT_REQ) {
-            if (ble_ll_adv_conn_req_rxd(rxbuf, hdr, advsm)) {
-                adv_event_over = 0;
-            }
-        } else {
-            if ((ptype == BLE_ADV_PDU_TYPE_SCAN_REQ) &&
-                (hdr->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_RSP_TXD)) {
-                adv_event_over = 0;
-            }
-        }
-    }
-
-    if (adv_event_over) {
-        ble_ll_adv_make_done(advsm, hdr);
-    }
-}
-
-/**
- * Called when a receive PDU has started and we are advertising.
- *
- * Context: interrupt
- *
- * @param pdu_type
- * @param rxpdu
- *
- * @return int
- *   < 0: A frame we dont want to receive.
- *   = 0: Continue to receive frame. Dont go from rx to tx
- *   > 0: Continue to receive frame and go from rx to tx when done
- */
-int
-ble_ll_adv_rx_isr_start(uint8_t pdu_type)
-{
-    int rc;
-    struct ble_ll_adv_sm *advsm;
-
-    /* Assume we will abort the frame */
-    rc = -1;
-
-    /* If we get a scan request we must tell the phy to go from rx to tx */
-    advsm = g_ble_ll_cur_adv_sm;
-    if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
-        /* Only accept scan requests if we are indirect adv or scan adv */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) {
-            rc = 1;
-        }
-    } else {
-        /* Only accept connect requests if connectable advertising event */
-        if (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ) {
-            if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
-                rc = 0;
-            }
-        }
-    }
-
-    /*
-     * If we abort the frame, we need to post the LL task to check if the
-     * advertising event is over.
-     */
-    if (rc < 0) {
-        ble_ll_adv_tx_done(advsm);
-    }
-
-    return rc;
-}
-
-static void
-ble_ll_adv_drop_event(struct ble_ll_adv_sm *advsm)
-{
-    STATS_INC(ble_ll_stats, adv_drop_event);
-
-    ble_ll_sched_rmv_elem(&advsm->adv_sch);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    ble_ll_sched_rmv_elem(&advsm->aux[0].sch);
-    ble_ll_sched_rmv_elem(&advsm->aux[1].sch);
-#endif
-
-    advsm->adv_chan = ble_ll_adv_final_chan(advsm);
-    os_eventq_put(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-}
-
-static void
-ble_ll_adv_reschedule_event(struct ble_ll_adv_sm *advsm)
-{
-    int rc;
-    uint32_t start_time;
-    uint32_t max_delay_ticks;
-
-    assert(advsm->adv_enabled);
-
-    if (!advsm->adv_sch.enqueued) {
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-            max_delay_ticks = 0;
-        } else {
-            max_delay_ticks =
-                    os_cputime_usecs_to_ticks(BLE_LL_ADV_DELAY_MS_MAX * 1000);
-        }
-
-        rc = ble_ll_sched_adv_reschedule(&advsm->adv_sch, &start_time,
-                                         max_delay_ticks);
-        if (rc) {
-            ble_ll_adv_drop_event(advsm);
-            return;
-        }
-
-        start_time += g_ble_ll_sched_offset_ticks;
-        advsm->adv_event_start_time = start_time;
-        advsm->adv_pdu_start_time = start_time;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) &&
-                                                        !advsm->aux_active) {
-        ble_ll_adv_aux_schedule(advsm);
-    }
-#endif
-}
-
-/**
- * Called when an advertising event is over.
- *
- * Context: Link Layer task.
- *
- * @param arg Pointer to advertising state machine.
- */
-static void
-ble_ll_adv_done(struct ble_ll_adv_sm *advsm)
-
-{
-    int rc;
-    int resched_pdu;
-    uint8_t mask;
-    uint8_t final_adv_chan;
-    int32_t delta_t;
-    uint32_t itvl;
-    uint32_t tick_itvl;
-    uint32_t start_time;
-
-    assert(advsm->adv_enabled);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-        /* stop advertising this was due to transmitting connection response */
-        if (advsm->flags & BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD) {
-            ble_ll_adv_sm_stop(advsm);
-            return;
-        }
-    }
-#endif
-
-    /* Remove the element from the schedule if it is still there. */
-    ble_ll_sched_rmv_elem(&advsm->adv_sch);
-
-    os_eventq_remove(&g_ble_ll_data.ll_evq, &advsm->adv_txdone_ev);
-
-    /*
-     * Check if we have ended our advertising event. If our last advertising
-     * packet was sent on the last channel, it means we are done with this
-     * event.
-     */
-    final_adv_chan = ble_ll_adv_final_chan(advsm);
-
-    if (advsm->adv_chan == final_adv_chan) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        if (advsm->events_max) {
-            advsm->events++;
-        }
-#endif
-
-        /* Check if we need to resume scanning */
-        ble_ll_scan_chk_resume();
-
-        /* Turn off the clock if not doing anything else */
-#ifdef BLE_XCVR_RFCLK
-        ble_ll_sched_rfclk_chk_restart();
-#endif
-
-        /* This event is over. Set adv channel to first one */
-        advsm->adv_chan = ble_ll_adv_first_chan(advsm);
-
-        /*
-         * Calculate start time of next advertising event. NOTE: we do not
-         * add the random advDelay as the scheduling code will do that.
-         */
-        itvl = advsm->adv_itvl_usecs;
-        tick_itvl = os_cputime_usecs_to_ticks(itvl);
-        advsm->adv_event_start_time += tick_itvl;
-        advsm->adv_pdu_start_time = advsm->adv_event_start_time;
-
-        /*
-         * The scheduled time better be in the future! If it is not, we will
-         * just keep advancing until we the time is in the future
-         */
-        start_time = advsm->adv_pdu_start_time - g_ble_ll_sched_offset_ticks;
-
-        delta_t = (int32_t)(start_time - os_cputime_get32());
-        if (delta_t < 0) {
-            /*
-             * NOTE: we just the same interval that we calculated earlier.
-             * No real need to keep recalculating a new interval.
-             */
-            while (delta_t < 0) {
-                advsm->adv_event_start_time += tick_itvl;
-                advsm->adv_pdu_start_time = advsm->adv_event_start_time;
-                delta_t += (int32_t)tick_itvl;
-            }
-        }
-        resched_pdu = 0;
-    } else {
-        /*
-         * Move to next advertising channel. If not in the mask, just
-         * increment by 1. We can do this because we already checked if we
-         * just transmitted on the last advertising channel
-         */
-        ++advsm->adv_chan;
-        mask = 1 << (advsm->adv_chan - BLE_PHY_ADV_CHAN_START);
-        if ((mask & advsm->adv_chanmask) == 0) {
-            ++advsm->adv_chan;
-        }
-
-        /*
-         * We will transmit right away. Set next pdu start time to now
-         * plus a xcvr start delay just so we dont count late adv starts
-         */
-        advsm->adv_pdu_start_time = os_cputime_get32() +
-                                    g_ble_ll_sched_offset_ticks;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        /* If we're past aux (unlikely, but can happen), just drop an event */
-        if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) &&
-                advsm->aux_active &&
-                advsm->adv_pdu_start_time > AUX_CURRENT(advsm)->start_time) {
-            ble_ll_adv_drop_event(advsm);
-            return;
-        }
-#endif
-
-        resched_pdu = 1;
-    }
-
-    /* check if advertising timed out */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (advsm->duration &&
-        advsm->adv_pdu_start_time >= advsm->adv_end_time) {
-        /* Legacy PDUs need to be stop here, for ext adv it will be stopped when
-         * AUX is done.
-         */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            ble_ll_hci_ev_send_adv_set_terminated(BLE_ERR_DIR_ADV_TMO,
-                                                  advsm->adv_instance, 0, 0);
-
-            /*
-             * For high duty directed advertising we need to send connection
-             * complete event with proper status
-             */
-            if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-                ble_ll_conn_comp_event_send(NULL, BLE_ERR_DIR_ADV_TMO,
-                                            advsm->conn_comp_ev, advsm);
-                advsm->conn_comp_ev = NULL;
-            }
-
-            /* Disable advertising */
-            advsm->adv_enabled = 0;
-            ble_ll_scan_chk_resume();
-        }
-
-        return;
-    }
-#else
-    if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-        if (advsm->adv_pdu_start_time >= advsm->adv_end_time) {
-            /* Disable advertising */
-            advsm->adv_enabled = 0;
-            ble_ll_conn_comp_event_send(NULL, BLE_ERR_DIR_ADV_TMO,
-                                        advsm->conn_comp_ev, advsm);
-            advsm->conn_comp_ev = NULL;
-            ble_ll_scan_chk_resume();
-            return;
-        }
-    }
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (advsm->events_max && (advsm->events >= advsm->events_max)) {
-        /* Legacy PDUs need to be stop here, for ext adv it will be stopped when
-         * AUX is done.
-         */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) {
-            ble_ll_hci_ev_send_adv_set_terminated(BLE_RR_LIMIT_REACHED,
-                                                  advsm->adv_instance, 0,
-                                                  advsm->events);
-
-            /* Disable advertising */
-            advsm->adv_enabled = 0;
-            ble_ll_scan_chk_resume();
-        }
-
-        return;
-    }
-#endif
-
-    /* We need to regenerate our RPA's if we have passed timeout */
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    ble_ll_adv_chk_rpa_timeout(advsm);
-#endif
-
-    /* Schedule advertising transmit */
-    ble_ll_adv_set_sched(advsm);
-
-    if (!resched_pdu) {
-        ble_ll_adv_reschedule_event(advsm);
-        return;
-    }
-
-    /*
-     * In the unlikely event we cant reschedule this, just post a done
-     * event and we will reschedule the next advertising event
-     */
-    rc = ble_ll_sched_adv_resched_pdu(&advsm->adv_sch);
-    if (rc) {
-        STATS_INC(ble_ll_stats, adv_resched_pdu_fail);
-        ble_ll_adv_drop_event(advsm);
-    }
-}
-
-static void
-ble_ll_adv_event_done(struct os_event *ev)
-{
-    ble_ll_adv_done(ev->ev_arg);
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-/**
- * Called when auxiliary packet is txd on secondary channel
- *
- * Context: Link Layer task.
- *
- * @param ev
- */
-static void
-ble_ll_adv_sec_done(struct ble_ll_adv_sm *advsm)
-{
-    struct ble_ll_adv_aux *aux;
-    struct ble_ll_adv_aux *aux_next;
-
-    assert(advsm->adv_enabled);
-    assert(advsm->aux_active);
-
-    aux = AUX_CURRENT(advsm);
-
-    if (advsm->aux_not_scanned) {
-        aux_next = AUX_NEXT(advsm);
-        ble_ll_sched_rmv_elem(&aux_next->sch);
-    }
-
-    /* Remove anything else scheduled for secondary channel */
-    ble_ll_sched_rmv_elem(&aux->sch);
-    os_eventq_remove(&g_ble_ll_data.ll_evq, &advsm->adv_sec_txdone_ev);
-
-    /* Stop advertising due to transmitting connection response */
-    if (advsm->flags & BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD) {
-        ble_ll_adv_sm_stop(advsm);
-        return;
-    }
-
-    /* If we have next AUX scheduled, try to schedule another one */
-    if (AUX_NEXT(advsm)->sch.enqueued) {
-        advsm->aux_index ^= 1;
-        advsm->aux_first_pdu = 0;
-        ble_ll_adv_aux_schedule_next(advsm);
-        return;
-    }
-
-    /* Check if we need to resume scanning */
-    ble_ll_scan_chk_resume();
-
-    /* Check if advertising timed out */
-    if (advsm->duration &&
-        aux->start_time >= advsm->adv_end_time) {
-        ble_ll_hci_ev_send_adv_set_terminated(BLE_ERR_DIR_ADV_TMO,
-                                              advsm->adv_instance, 0, 0);
-
-        /*
-         * For high duty directed advertising we need to send connection
-         * complete event with proper status
-         */
-        if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_HD_DIRECTED) {
-            ble_ll_conn_comp_event_send(NULL, BLE_ERR_DIR_ADV_TMO,
-                                        advsm->conn_comp_ev, advsm);
-            advsm->conn_comp_ev = NULL;
-        }
-
-        /* Disable advertising */
-        advsm->adv_enabled = 0;
-        return;
-    }
-
-    if (advsm->events_max && (advsm->events >= advsm->events_max)) {
-        ble_ll_hci_ev_send_adv_set_terminated(BLE_RR_LIMIT_REACHED,
-                                              advsm->adv_instance, 0,
-                                              advsm->events);
-         /* Disable advertising */
-         advsm->adv_enabled = 0;
-         return;
-    }
-
-    advsm->aux_active = 0;
-    ble_ll_adv_reschedule_event(advsm);
-}
-
-static void
-ble_ll_adv_sec_event_done(struct os_event *ev)
-{
-    ble_ll_adv_sec_done(ev->ev_arg);
-}
-#endif
-
-static void
-ble_ll_adv_make_done(struct ble_ll_adv_sm *advsm, struct ble_mbuf_hdr *hdr)
-{
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (BLE_MBUF_HDR_EXT_ADV_SEC(hdr)) {
-        assert(!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY));
-        assert(ble_ll_adv_active_chanset_is_sec(advsm));
-        ble_ll_adv_active_chanset_clear(advsm);
-        ble_ll_adv_sec_done(advsm);
-    } else {
-        assert(ble_ll_adv_active_chanset_is_pri(advsm));
-        ble_ll_adv_active_chanset_clear(advsm);
-        ble_ll_adv_done(advsm);
-    }
-#else
-    assert(ble_ll_adv_active_chanset_is_pri(advsm));
-    ble_ll_adv_active_chanset_clear(advsm);
-    ble_ll_adv_done(advsm);
-#endif
-}
-
-/**
- * Checks if the controller can change the whitelist. If advertising is enabled
- * and is using the whitelist the controller is not allowed to change the
- * whitelist.
- *
- * @return int 0: not allowed to change whitelist; 1: change allowed.
- */
-int
-ble_ll_adv_can_chg_whitelist(void)
-{
-    struct ble_ll_adv_sm *advsm;
-    int rc;
-    int i;
-
-    rc = 1;
-    for (i = 0; i < BLE_ADV_INSTANCES; ++i) {
-        advsm = &g_ble_ll_adv_sm[i];
-        if (advsm->adv_enabled &&
-            (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE)) {
-            rc = 0;
-            break;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Sends the connection complete event when advertising a connection starts.
- *
- * @return uint8_t* Pointer to event buffer
- */
-void
-ble_ll_adv_send_conn_comp_ev(struct ble_ll_conn_sm *connsm,
-                             struct ble_mbuf_hdr *rxhdr)
-{
-    uint8_t *evbuf;
-    struct ble_ll_adv_sm *advsm;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    advsm = (struct ble_ll_adv_sm *)rxhdr->rxinfo.user_data;
-#else
-    advsm = &g_ble_ll_adv_sm[0];
-#endif
-
-    evbuf = advsm->conn_comp_ev;
-    assert(evbuf != NULL);
-    advsm->conn_comp_ev = NULL;
-
-    ble_ll_conn_comp_event_send(connsm, BLE_ERR_SUCCESS, evbuf, advsm);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    ble_ll_hci_ev_send_adv_set_terminated(0, advsm->adv_instance,
-                                          connsm->conn_handle, advsm->events);
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-    ble_ll_hci_ev_le_csa(connsm);
-#endif
-}
-
-/**
- * Returns the local resolvable private address currently being using by
- * the advertiser
- *
- * @return uint8_t*
- */
-uint8_t *
-ble_ll_adv_get_local_rpa(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t *rpa;
-
-    rpa = NULL;
-    if (advsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        rpa = advsm->adva;
-    }
-
-    return rpa;
-}
-
-/**
- * Returns the peer resolvable private address of last device connecting to us
- *
- * @return uint8_t*
- */
-uint8_t *
-ble_ll_adv_get_peer_rpa(struct ble_ll_adv_sm *advsm)
-{
-    /* XXX: should this go into IRK list or connection? */
-    return advsm->adv_rpa;
-}
-
-/**
- * Called when the LL wait for response timer expires while in the advertising
- * state. Disables the phy and
- *
- */
-void
-ble_ll_adv_wfr_timer_exp(void)
-{
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    g_ble_ll_cur_adv_sm->aux_not_scanned = 1;
-#endif
-
-    ble_phy_disable();
-    ble_ll_adv_tx_done(g_ble_ll_cur_adv_sm);
-}
-
-/**
- * Reset the advertising state machine.
- *
- * Context: Link Layer task
- *
- */
-void
-ble_ll_adv_reset(void)
-{
-    int i;
-    struct ble_ll_adv_sm *advsm;
-
-    for (i = 0; i < BLE_ADV_INSTANCES; ++i) {
-        /* Stop advertising state machine */
-        advsm = &g_ble_ll_adv_sm[i];
-        ble_ll_adv_sm_stop(advsm);
-    }
-
-    /* re-initialize the advertiser state machine */
-    ble_ll_adv_init();
-}
-
-/* Called to determine if advertising is enabled.
- *
- * NOTE: this currently only applies to the default advertising index.
- */
-uint8_t
-ble_ll_adv_enabled(void)
-{
-    return g_ble_ll_adv_sm[0].adv_enabled;
-}
-
-static void
-ble_ll_adv_sm_init(struct ble_ll_adv_sm *advsm)
-{
-    uint8_t i = advsm->adv_instance;
-
-    memset(advsm, 0, sizeof(struct ble_ll_adv_sm));
-
-    advsm->adv_instance = i;
-    advsm->adv_itvl_min = BLE_HCI_ADV_ITVL_DEF;
-    advsm->adv_itvl_max = BLE_HCI_ADV_ITVL_DEF;
-    advsm->adv_chanmask = BLE_HCI_ADV_CHANMASK_DEF;
-
-    /* Initialize advertising tx done event */
-    advsm->adv_txdone_ev.ev_cb = ble_ll_adv_event_done;
-    advsm->adv_txdone_ev.ev_arg = advsm;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    advsm->adv_sec_txdone_ev.ev_cb = ble_ll_adv_sec_event_done;
-    advsm->adv_sec_txdone_ev.ev_arg = advsm;
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* Initialize aux schedulers */
-    advsm->aux_active = 0;
-    advsm->aux[0].sch.cb_arg = advsm;
-    advsm->aux[0].sch.sched_cb = ble_ll_adv_secondary_tx_start_cb;
-    advsm->aux[0].sch.sched_type = BLE_LL_SCHED_TYPE_ADV;
-    advsm->aux[1].sch.cb_arg = advsm;
-    advsm->aux[1].sch.sched_cb = ble_ll_adv_secondary_tx_start_cb;
-    advsm->aux[1].sch.sched_type = BLE_LL_SCHED_TYPE_ADV;
-#endif
-
-    /*XXX Configure instances to be legacy on start */
-    advsm->props |= BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE;
-    advsm->props |= BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY;
-}
-
-/**
- * Initialize the advertising functionality of a BLE device. This should
- * be called once on initialization
- */
-void
-ble_ll_adv_init(void)
-{
-    int i;
-
-    /* Set default advertising parameters */
-    for (i = 0; i < BLE_ADV_INSTANCES; ++i) {
-        g_ble_ll_adv_sm[i].adv_instance = i;
-        ble_ll_adv_sm_init(&g_ble_ll_adv_sm[i]);
-    }
-}
-
diff --git a/net/nimble/controller/src/ble_ll_conn.c b/net/nimble/controller/src/ble_ll_conn.c
deleted file mode 100644
index 9123cf629..000000000
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ /dev/null
@@ -1,4266 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "bsp/bsp.h"
-#include "os/os.h"
-#include "os/os_cputime.h"
-#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"
-#include "controller/ble_ll_scan.h"
-#include "controller/ble_ll_whitelist.h"
-#include "controller/ble_ll_sched.h"
-#include "controller/ble_ll_ctrl.h"
-#include "controller/ble_ll_resolv.h"
-#include "controller/ble_ll_adv.h"
-#include "controller/ble_phy.h"
-#include "controller/ble_hw.h"
-#include "ble_ll_conn_priv.h"
-
-#if (BLETEST_THROUGHPUT_TEST == 1)
-extern void bletest_completed_pkt(uint16_t handle);
-#endif
-
-/* XXX TODO
- * 1) I think if we are initiating and we already have a connection with
- * a device that we will still try and connect to it. Fix this.
- *  -> This is true. There are a couple things to do
- *      i) When a connection create is issued, if we already are connected
- *      deny it. BLE ERROR = 0x0B (ACL connection exists).
- *      ii) If we receive an advertisement while initiating and want to send
- *      a connect request to the device, make sure we dont have it.
- *      iii) I think I need to do something like this: I am initiating and
- *      advertising. Suppose the device I want to connect to sends me a connect
- *      request because I am advertising? What happens to connection? Deal
- *      with this!
- *
- * 2) Make sure we check incoming data packets for size and all that. You
- * know, supported octets and all that. For both rx and tx.
- *
- * 3) Make sure we are setting the schedule end time properly for both slave
- * and master. We should just set this to the end of the connection event.
- * We might want to guarantee a IFS time as well since the next event needs
- * to be scheduled prior to the start of the event to account for the time it
- * takes to get a frame ready (which is pretty much the IFS time).
- *
- * 4) looks like the current code will allow the 1st packet in a
- * connection to extend past the end of the allocated connection end
- * time. That is not good. Need to deal with that. Need to extend connection
- * end time.
- *
- * 6) Use error code 0x3E correctly! Connection failed to establish. If you
- * read the LE connection complete event, it says that if the connection
- * fails to be established that the connection complete event gets sent to
- * the host that issued the create connection. Need to resolve this.
- *
- * 7) How does peer address get set if we are using whitelist? Look at filter
- * policy and make sure you are doing this correctly.
- *
- * 8) Right now I use a fixed definition for required slots. CHange this.
- *
- * 10) See what connection state machine elements are purely master and
- * purely slave. We can make a union of them.
- *
- * 11) Not sure I am dealing with the connection terminate timeout perfectly.
- * I may extend a connection event too long although if it is always in terms
- * of connection events I am probably fine. Checking at end that the next
- * connection event will occur past terminate timeould would be fine.
- *
- * 12) When a slave receives a data packet in a connection it has to send a
- * response. Well, it should. If this packet will overrun the next scheduled
- * event, what should we do? Transmit anyway? Not transmit? For now, we just
- * transmit.
- *
- * 32kHz crystal
- * 1) When scheduling, I need to make sure I have time between
- * this one and the next. Should I deal with this in the sched. Or
- * is this basically accounted for given a slot? I really just need to
- * make sure everything is over N ticks before the next sched start!
- * Just add to end time?
- *
- * 2) I think one way to handle the problem of losing up to a microsecond
- * every time we call ble_ll_conn_next_event in a loop is to do everything by
- * keeping track of last anchor point. Would need last anchor usecs too. I guess
- * we could also keep last anchor usecs as a uint32 or something and when we
- * do the next event keep track of the residual using a different ticks to
- * usecs calculation. Not sure.
- */
-
-/*
- * XXX: How should we deal with a late connection event? We need to determine
- * what we want to do under the following cases:
- *  1) The current connection event has not ended but a schedule item starts
- */
-
-/* This is a dummy structure we use for the empty PDU */
-struct ble_ll_empty_pdu
-{
-    struct os_mbuf om;
-    struct os_mbuf_pkthdr pkt_hdr;
-    struct ble_mbuf_hdr ble_hdr;
-};
-
-/* We cannot have more than 254 connections given our current implementation */
-#if (MYNEWT_VAL(BLE_MAX_CONNECTIONS) >= 255)
-    #error "Maximum # of connections is 254"
-#endif
-
-/* Sleep clock accuracy table (in ppm) */
-static const uint16_t g_ble_sca_ppm_tbl[8] =
-{
-    500, 250, 150, 100, 75, 50, 30, 20
-};
-
-/* Global connection complete event. Used when initiating */
-uint8_t *g_ble_ll_conn_comp_ev;
-
-/* Global LL connection parameters */
-struct ble_ll_conn_global_params g_ble_ll_conn_params;
-
-/* Pointer to connection state machine we are trying to create */
-struct ble_ll_conn_sm *g_ble_ll_conn_create_sm;
-
-/* Pointer to current connection */
-struct ble_ll_conn_sm *g_ble_ll_conn_cur_sm;
-
-/* Connection state machine array */
-struct ble_ll_conn_sm g_ble_ll_conn_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
-
-/* List of active connections */
-struct ble_ll_conn_active_list g_ble_ll_conn_active_list;
-
-/* List of free connections */
-struct ble_ll_conn_free_list g_ble_ll_conn_free_list;
-
-STATS_SECT_START(ble_ll_conn_stats)
-    STATS_SECT_ENTRY(cant_set_sched)
-    STATS_SECT_ENTRY(conn_ev_late)
-    STATS_SECT_ENTRY(wfr_expirations)
-    STATS_SECT_ENTRY(handle_not_found)
-    STATS_SECT_ENTRY(no_conn_sm)
-    STATS_SECT_ENTRY(no_free_conn_sm)
-    STATS_SECT_ENTRY(rx_data_pdu_no_conn)
-    STATS_SECT_ENTRY(rx_data_pdu_bad_aa)
-    STATS_SECT_ENTRY(slave_rxd_bad_conn_req_params)
-    STATS_SECT_ENTRY(slave_ce_failures)
-    STATS_SECT_ENTRY(data_pdu_rx_dup)
-    STATS_SECT_ENTRY(data_pdu_txg)
-    STATS_SECT_ENTRY(data_pdu_txf)
-    STATS_SECT_ENTRY(conn_req_txd)
-    STATS_SECT_ENTRY(l2cap_enqueued)
-    STATS_SECT_ENTRY(rx_ctrl_pdus)
-    STATS_SECT_ENTRY(rx_l2cap_pdus)
-    STATS_SECT_ENTRY(rx_l2cap_bytes)
-    STATS_SECT_ENTRY(rx_malformed_ctrl_pdus)
-    STATS_SECT_ENTRY(rx_bad_llid)
-    STATS_SECT_ENTRY(tx_ctrl_pdus)
-    STATS_SECT_ENTRY(tx_ctrl_bytes)
-    STATS_SECT_ENTRY(tx_l2cap_pdus)
-    STATS_SECT_ENTRY(tx_l2cap_bytes)
-    STATS_SECT_ENTRY(tx_empty_pdus)
-    STATS_SECT_ENTRY(mic_failures)
-STATS_SECT_END
-STATS_SECT_DECL(ble_ll_conn_stats) ble_ll_conn_stats;
-
-STATS_NAME_START(ble_ll_conn_stats)
-    STATS_NAME(ble_ll_conn_stats, cant_set_sched)
-    STATS_NAME(ble_ll_conn_stats, conn_ev_late)
-    STATS_NAME(ble_ll_conn_stats, wfr_expirations)
-    STATS_NAME(ble_ll_conn_stats, handle_not_found)
-    STATS_NAME(ble_ll_conn_stats, no_conn_sm)
-    STATS_NAME(ble_ll_conn_stats, no_free_conn_sm)
-    STATS_NAME(ble_ll_conn_stats, rx_data_pdu_no_conn)
-    STATS_NAME(ble_ll_conn_stats, rx_data_pdu_bad_aa)
-    STATS_NAME(ble_ll_conn_stats, slave_rxd_bad_conn_req_params)
-    STATS_NAME(ble_ll_conn_stats, slave_ce_failures)
-    STATS_NAME(ble_ll_conn_stats, data_pdu_rx_dup)
-    STATS_NAME(ble_ll_conn_stats, data_pdu_txg)
-    STATS_NAME(ble_ll_conn_stats, data_pdu_txf)
-    STATS_NAME(ble_ll_conn_stats, conn_req_txd)
-    STATS_NAME(ble_ll_conn_stats, l2cap_enqueued)
-    STATS_NAME(ble_ll_conn_stats, rx_ctrl_pdus)
-    STATS_NAME(ble_ll_conn_stats, rx_l2cap_pdus)
-    STATS_NAME(ble_ll_conn_stats, rx_l2cap_bytes)
-    STATS_NAME(ble_ll_conn_stats, rx_malformed_ctrl_pdus)
-    STATS_NAME(ble_ll_conn_stats, rx_bad_llid)
-    STATS_NAME(ble_ll_conn_stats, tx_ctrl_pdus)
-    STATS_NAME(ble_ll_conn_stats, tx_ctrl_bytes)
-    STATS_NAME(ble_ll_conn_stats, tx_l2cap_pdus)
-    STATS_NAME(ble_ll_conn_stats, tx_l2cap_bytes)
-    STATS_NAME(ble_ll_conn_stats, tx_empty_pdus)
-    STATS_NAME(ble_ll_conn_stats, mic_failures)
-STATS_NAME_END(ble_ll_conn_stats)
-
-static void ble_ll_conn_event_end(struct os_event *ev);
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-/**
- * Checks to see if we should start a PHY update procedure
- *
- * If current phy is not one of the preferred we need to start control
- * procedure.
- *
- * XXX: we could also decide to change the PHY if RSSI is really good
- * and we are currently at 1Mbps or lower data rate and we could use
- * a higher data rate.
- *
- * @param connsm
- * @return 0: success; -1: no phy update procedure started
- */
-int
-ble_ll_conn_chk_phy_upd_start(struct ble_ll_conn_sm *csm)
-{
-    int rc;
-
-    /* If no host preferences or  */
-    if (((csm->phy_data.host_pref_tx_phys_mask == 0) &&
-         (csm->phy_data.host_pref_rx_phys_mask == 0)) ||
-        ((csm->phy_data.host_pref_tx_phys_mask & CONN_CUR_TX_PHY_MASK(csm)) &&
-         (csm->phy_data.host_pref_rx_phys_mask & CONN_CUR_RX_PHY_MASK(csm)))) {
-        rc = -1;
-    } else {
-        csm->phy_data.req_pref_tx_phys_mask = csm->phy_data.host_pref_tx_phys_mask;
-        csm->phy_data.req_pref_rx_phys_mask = csm->phy_data.host_pref_rx_phys_mask;
-        ble_ll_ctrl_proc_start(csm, BLE_LL_CTRL_PROC_PHY_UPDATE);
-        rc = 0;
-    }
-
-    return rc;
-}
-#endif
-
-static void
-ble_ll_conn_calc_itvl_ticks(struct ble_ll_conn_sm *connsm)
-{
-    uint32_t ticks;
-    uint32_t usecs;
-
-    /*
-     * Precalculate the number of ticks and remaining microseconds for
-     * the connection interval
-     */
-    usecs = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
-    ticks = os_cputime_usecs_to_ticks(usecs);
-    connsm->conn_itvl_usecs = (uint8_t)(usecs -
-                                        os_cputime_ticks_to_usecs(ticks));
-    if (connsm->conn_itvl_usecs == 31) {
-        connsm->conn_itvl_usecs = 0;
-        ++ticks;
-    }
-    connsm->conn_itvl_ticks = ticks;
-}
-
-/**
- * Get the event buffer allocated to send the connection complete event
- * when we are initiating.
- *
- * @return uint8_t*
- */
-static uint8_t *
-ble_ll_init_get_conn_comp_ev(void)
-{
-    uint8_t *evbuf;
-
-    evbuf = g_ble_ll_conn_comp_ev;
-    assert(evbuf != NULL);
-    g_ble_ll_conn_comp_ev = NULL;
-
-    return evbuf;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-/**
- * Called to determine if the received PDU is an empty PDU or not.
- */
-static int
-ble_ll_conn_is_empty_pdu(uint8_t *rxbuf)
-{
-    int rc;
-    uint8_t llid;
-
-    llid = rxbuf[0] & BLE_LL_DATA_HDR_LLID_MASK;
-    if ((llid == BLE_LL_LLID_DATA_FRAG) && (rxbuf[1] == 0)) {
-        rc = 1;
-    } else {
-        rc = 0;
-    }
-    return rc;
-}
-#endif
-
-/**
- * Called to return the currently running connection state machine end time.
- * Always called when interrupts are disabled.
- *
- * @return int 0: s1 is not least recently used. 1: s1 is least recently used
- */
-int
-ble_ll_conn_is_lru(struct ble_ll_conn_sm *s1, struct ble_ll_conn_sm *s2)
-{
-    int rc;
-
-    /* Set time that we last serviced the schedule */
-    if ((int32_t)(s1->last_scheduled - s2->last_scheduled) < 0) {
-        rc = 1;
-    } else {
-        rc = 0;
-    }
-
-    return rc;
-}
-
-/**
- * Called to return the currently running connection state machine end time.
- * Always called when interrupts are disabled.
- *
- * @return uint32_t
- */
-uint32_t
-ble_ll_conn_get_ce_end_time(void)
-{
-    uint32_t ce_end_time;
-
-    if (g_ble_ll_conn_cur_sm) {
-        ce_end_time = g_ble_ll_conn_cur_sm->ce_end_time;
-    } else {
-        ce_end_time = os_cputime_get32();
-    }
-    return ce_end_time;
-}
-
-/**
- * Called when the current connection state machine is no longer being used.
- * This function will:
- *  -> Disable the PHY, which will prevent any transmit/receive interrupts.
- *  -> Disable the wait for response timer, if running.
- *  -> Remove the connection state machine from the scheduler.
- *  -> Sets the Link Layer state to standby.
- *  -> Sets the current state machine to NULL.
- *
- *  NOTE: the ordering of these function calls is important! We have to stop
- *  the PHY and remove the schedule item before we can set the state to
- *  standby and set the current state machine pointer to NULL.
- */
-static void
-ble_ll_conn_current_sm_over(struct ble_ll_conn_sm *connsm)
-{
-    /* Disable the PHY */
-    ble_phy_disable();
-
-    /* Disable the wfr timer */
-    ble_ll_wfr_disable();
-
-    /* Link-layer is in standby state now */
-    ble_ll_state_set(BLE_LL_STATE_STANDBY);
-
-    /* Set current LL connection to NULL */
-    g_ble_ll_conn_cur_sm = NULL;
-
-    /*
-     * NOTE: the connection state machine may be NULL if we are calling
-     * this when we are ending the connection. In that case, there is no
-     * need to post to the LL the connection event end event
-     */
-    if (connsm) {
-        ble_ll_event_send(&connsm->conn_ev_end);
-    }
-}
-
-/**
- * Given a handle, find an active connection matching the handle
- *
- * @param handle
- *
- * @return struct ble_ll_conn_sm*
- */
-struct ble_ll_conn_sm *
-ble_ll_conn_find_active_conn(uint16_t handle)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = NULL;
-    if ((handle != 0) && (handle <= MYNEWT_VAL(BLE_MAX_CONNECTIONS))) {
-        connsm = &g_ble_ll_conn_sm[handle - 1];
-        if (connsm->conn_state == BLE_LL_CONN_STATE_IDLE) {
-            connsm = NULL;
-        }
-    }
-    return connsm;
-}
-
-/**
- * Get a connection state machine.
- */
-struct ble_ll_conn_sm *
-ble_ll_conn_sm_get(void)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = STAILQ_FIRST(&g_ble_ll_conn_free_list);
-    if (connsm) {
-        STAILQ_REMOVE_HEAD(&g_ble_ll_conn_free_list, free_stqe);
-    } else {
-        STATS_INC(ble_ll_conn_stats, no_free_conn_sm);
-    }
-
-    return connsm;
-}
-
-/**
- * Calculate the amount of window widening for a given connection event. This
- * is the amount of time that a slave has to account for when listening for
- * the start of a connection event.
- *
- * @param connsm Pointer to connection state machine.
- *
- * @return uint32_t The current window widening amount (in microseconds)
- */
-uint32_t
-ble_ll_conn_calc_window_widening(struct ble_ll_conn_sm *connsm)
-{
-    uint32_t total_sca_ppm;
-    uint32_t window_widening;
-    int32_t time_since_last_anchor;
-    uint32_t delta_msec;
-
-    window_widening = 0;
-
-    time_since_last_anchor = (int32_t)(connsm->anchor_point -
-                                       connsm->last_anchor_point);
-    if (time_since_last_anchor > 0) {
-        delta_msec = os_cputime_ticks_to_usecs(time_since_last_anchor) / 1000;
-        total_sca_ppm = g_ble_sca_ppm_tbl[connsm->master_sca] +
-            MYNEWT_VAL(BLE_LL_OUR_SCA);
-        window_widening = (total_sca_ppm * delta_msec) / 1000;
-    }
-
-    return window_widening;
-}
-
-/**
- * Calculates the number of used channels in the channel map
- *
- * @param chmap
- *
- * @return uint8_t Number of used channels
- */
-uint8_t
-ble_ll_conn_calc_used_chans(uint8_t *chmap)
-{
-    int i;
-    int j;
-    uint8_t mask;
-    uint8_t chanbyte;
-    uint8_t used_channels;
-
-    used_channels = 0;
-    for (i = 0; i < BLE_LL_CONN_CHMAP_LEN; ++i) {
-        chanbyte = chmap[i];
-        if (chanbyte) {
-            if (chanbyte == 0xff) {
-                used_channels += 8;
-            } else {
-                mask = 0x01;
-                for (j = 0; j < 8; ++j) {
-                    if (chanbyte & mask) {
-                        ++used_channels;
-                    }
-                    mask <<= 1;
-                }
-            }
-        }
-    }
-    return used_channels;
-}
-
-static uint32_t
-ble_ll_conn_calc_access_addr(void)
-{
-    uint32_t aa;
-    uint16_t aa_low;
-    uint16_t aa_high;
-    uint32_t temp;
-    uint32_t mask;
-    uint32_t prev_bit;
-    uint8_t bits_diff;
-    uint8_t consecutive;
-    uint8_t transitions;
-    uint8_t ones;
-
-    /* Calculate a random access address */
-    aa = 0;
-    while (1) {
-        /* Get two, 16-bit random numbers */
-        aa_low = rand() & 0xFFFF;
-        aa_high = rand() & 0xFFFF;
-
-        /* All four bytes cannot be equal */
-        if (aa_low == aa_high) {
-            continue;
-        }
-
-        /* Upper 6 bits must have 2 transitions */
-        temp = aa_high & 0xFC00;
-        if ((temp == 0) || (temp == 0xFC00)) {
-            continue;
-        }
-
-        /* Cannot be access address or be 1 bit different */
-        aa = aa_high;
-        aa = (aa << 16) | aa_low;
-        bits_diff = 0;
-        temp = aa ^ BLE_ACCESS_ADDR_ADV;
-        for (mask = 0x00000001; mask != 0; mask <<= 1) {
-            if (mask & temp) {
-                ++bits_diff;
-                if (bits_diff > 1) {
-                    break;
-                }
-            }
-        }
-        if (bits_diff <= 1) {
-            continue;
-        }
-
-        /* Cannot have more than 24 transitions */
-        transitions = 0;
-        consecutive = 1;
-        ones = 0;
-        mask = 0x00000001;
-        while (mask < 0x80000000) {
-            prev_bit = aa & mask;
-            mask <<= 1;
-            if (mask & aa) {
-                if (prev_bit == 0) {
-                    ++transitions;
-                    consecutive = 1;
-                } else {
-                    ++consecutive;
-                }
-            } else {
-                if (prev_bit == 0) {
-                    ++consecutive;
-                } else {
-                    ++transitions;
-                    consecutive = 1;
-                }
-            }
-
-            if (prev_bit) {
-                ones++;
-            }
-
-            /* 8 lsb should have at least three 1 */
-            if (mask == 0x00000100 && ones < 3) {
-                break;
-            }
-
-            /* 16 lsb should have no more than 11 transitions */
-            if (mask == 0x00010000 && transitions > 11) {
-                break;
-            }
-
-            /* This is invalid! */
-            if (consecutive > 6) {
-                /* Make sure we always detect invalid sequence below */
-                mask = 0;
-                break;
-            }
-        }
-
-        /* Invalid sequence found */
-        if (mask != 0x80000000) {
-            continue;
-        }
-
-        /* Cannot be more than 24 transitions */
-        if (transitions > 24) {
-            continue;
-        }
-
-        /* We have a valid access address */
-        break;
-    }
-    return aa;
-}
-
-static uint8_t
-ble_ll_conn_remapped_channel(uint8_t remap_index, const uint8_t *chanmap)
-{
-    uint8_t cntr;
-    uint8_t mask;
-    uint8_t usable_chans;
-    uint8_t chan;
-    int i, j;
-
-    /* NOTE: possible to build a map but this would use memory. For now,
-       we just calculate */
-    /* Iterate through channel map to find this channel */
-    chan = 0;
-    cntr = 0;
-    for (i = 0; i < BLE_LL_CONN_CHMAP_LEN; i++) {
-        usable_chans = chanmap[i];
-        if (usable_chans != 0) {
-            mask = 0x01;
-            for (j = 0; j < 8; j++) {
-                if (usable_chans & mask) {
-                    if (cntr == remap_index) {
-                        return (chan + j);
-                    }
-                    ++cntr;
-                }
-                mask <<= 1;
-            }
-        }
-        chan += 8;
-    }
-
-    /* we should never reach here */
-    assert(0);
-    return 0;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-static uint16_t
-ble_ll_conn_csa2_perm(uint16_t in)
-{
-    uint16_t out = 0;
-    int i;
-
-    for (i = 0; i < 8; i++) {
-        out |= ((in >> i) & 0x00000001) << (7 - i);
-    }
-
-    for (i = 8; i < 16; i++) {
-        out |= ((in >> i) & 0x00000001) << (15 + 8 - i);
-    }
-
-    return out;
-}
-
-static uint16_t
-ble_ll_conn_csa2_prng(uint16_t counter, uint16_t ch_id)
-{
-    uint16_t prn_e;
-
-    prn_e = counter ^ ch_id;
-
-    prn_e = ble_ll_conn_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
-
-    prn_e = ble_ll_conn_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
-
-    prn_e = ble_ll_conn_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
-
-    prn_e = prn_e ^ ch_id;
-
-    return prn_e;
-}
-
-static uint8_t
-ble_ll_conn_calc_dci_csa2(struct ble_ll_conn_sm *conn)
-{
-    uint16_t channel_unmapped;
-    uint8_t remap_index;
-
-    uint16_t prn_e;
-    uint8_t bitpos;
-
-    prn_e = ble_ll_conn_csa2_prng(conn->event_cntr, conn->channel_id);
-
-    channel_unmapped = prn_e % 37;
-
-    /*
-     * If unmapped channel is the channel index of a used channel it is used
-     * as channel index.
-     */
-    bitpos = 1 << (channel_unmapped & 0x07);
-    if (conn->chanmap[channel_unmapped >> 3] & bitpos) {
-        return channel_unmapped;
-    }
-
-    remap_index = (conn->num_used_chans * prn_e) / 0x10000;
-
-    return ble_ll_conn_remapped_channel(remap_index, conn->chanmap);
-}
-#endif
-
-static uint8_t
-ble_ll_conn_calc_dci_csa1(struct ble_ll_conn_sm *conn)
-{
-    uint8_t curchan;
-    uint8_t remap_index;
-    uint8_t bitpos;
-
-    /* Get next unmapped channel */
-    curchan = conn->last_unmapped_chan + conn->hop_inc;
-    if (curchan > BLE_PHY_NUM_DATA_CHANS) {
-        curchan -= BLE_PHY_NUM_DATA_CHANS;
-    }
-
-    /* Save unmapped channel */
-    conn->last_unmapped_chan = curchan;
-
-    /* Is this a valid channel? */
-    bitpos = 1 << (curchan & 0x07);
-    if (conn->chanmap[curchan >> 3] & bitpos) {
-        return curchan;
-    }
-
-    /* Calculate remap index */
-    remap_index = curchan % conn->num_used_chans;
-
-    return ble_ll_conn_remapped_channel(remap_index, conn->chanmap);
-}
-
-/**
- * Determine data channel index to be used for the upcoming/current
- * connection event
- *
- * @param conn
- * @param latency Used only for CSA #1
- *
- * @return uint8_t
- */
-uint8_t
-ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency)
-{
-    uint8_t index;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-    if (CONN_F_CSA2_SUPP(conn)) {
-        return ble_ll_conn_calc_dci_csa2(conn);
-    }
-#endif
-
-    index = conn->data_chan_index;
-
-    while (latency > 0) {
-        index = ble_ll_conn_calc_dci_csa1(conn);
-        latency--;
-    }
-
-    return index;
-}
-
-/**
- * Called when we are in the connection state and the wait for response timer
- * fires off.
- *
- * Context: Interrupt
- */
-void
-ble_ll_conn_wfr_timer_exp(void)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = g_ble_ll_conn_cur_sm;
-    ble_ll_conn_current_sm_over(connsm);
-    STATS_INC(ble_ll_conn_stats, wfr_expirations);
-}
-
-void
-ble_ll_conn_reset_pending_aux_conn_rsp(void)
-{
-#if !MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    return;
-#endif
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return;
-    }
-
-    if (CONN_F_AUX_CONN_REQ(connsm)) {
-        STATS_INC(ble_ll_stats, aux_conn_rsp_err);
-        CONN_F_CONN_REQ_TXD(connsm) = 0;
-        CONN_F_AUX_CONN_REQ(connsm) = 0;
-        ble_ll_sched_rmv_elem(&connsm->conn_sch);
-        return;
-    }
-
-    return;
-}
-
-bool
-ble_ll_conn_init_pending_aux_conn_rsp(void)
-{
-#if !MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    return false;
-#endif
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return false;
-    }
-
-    return CONN_F_AUX_CONN_REQ(connsm);
-}
-
-void
-ble_ll_conn_init_wfr_timer_exp(void)
-{
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    struct ble_ll_conn_sm *connsm;
-    struct ble_ll_scan_sm *scansm;
-
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return;
-    }
-
-    ble_ll_conn_reset_pending_aux_conn_rsp();
-
-    scansm = connsm->scansm;
-    if (scansm && scansm->cur_aux_data) {
-        ble_ll_scan_aux_data_free(scansm->cur_aux_data);
-        scansm->cur_aux_data = NULL;
-        STATS_INC(ble_ll_stats, aux_missed_adv);
-        ble_ll_event_send(&scansm->scan_sched_ev);
-    }
-#endif
-}
-/**
- * Callback for slave when it transmits a data pdu and the connection event
- * ends after the transmission.
- *
- * Context: Interrupt
- *
- * @param sch
- *
- */
-static void
-ble_ll_conn_wait_txend(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    ble_ll_conn_current_sm_over(connsm);
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-static void
-ble_ll_conn_start_rx_encrypt(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    CONN_F_ENCRYPTED(connsm) = 1;
-    ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
-                           connsm->enc_data.iv,
-                           connsm->enc_data.enc_block.cipher_text,
-                           !CONN_IS_MASTER(connsm));
-}
-
-static void
-ble_ll_conn_start_rx_unencrypt(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    CONN_F_ENCRYPTED(connsm) = 0;
-    ble_phy_encrypt_disable();
-}
-
-static void
-ble_ll_conn_txend_encrypt(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    CONN_F_ENCRYPTED(connsm) = 1;
-    ble_ll_conn_current_sm_over(connsm);
-}
-
-static void
-ble_ll_conn_rxend_unencrypt(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    CONN_F_ENCRYPTED(connsm) = 0;
-    ble_ll_conn_current_sm_over(connsm);
-}
-
-static void
-ble_ll_conn_continue_rx_encrypt(void *arg)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)arg;
-    ble_phy_encrypt_set_pkt_cntr(connsm->enc_data.rx_pkt_cntr,
-                                 !CONN_IS_MASTER(connsm));
-}
-#endif
-
-/**
- * Returns the cputime of the next scheduled item on the scheduler list or
- * when the current connection will start its next interval (whichever is
- * earlier). This API is called when determining at what time we should end
- * the current connection event. The current connection event must end before
- * the next scheduled item. However, the current connection itself is not
- * in the scheduler list! Thus, we need to calculate the time at which the
- * next connection will start (the schedule start time; not the anchor point)
- * and not overrun it.
- *
- * Context: Interrupt
- *
- * @param connsm
- *
- * @return uint32_t
- */
-static uint32_t
-ble_ll_conn_get_next_sched_time(struct ble_ll_conn_sm *connsm)
-{
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-    uint32_t ce_end;
-    ce_end = connsm->ce_end_time;
-#else
-    uint32_t ce_end;
-    uint32_t next_sched_time;
-
-    /* Calculate time at which next connection event will start */
-    /* NOTE: We dont care if this time is tick short. */
-    ce_end = connsm->anchor_point + connsm->conn_itvl_ticks -
-        g_ble_ll_sched_offset_ticks;
-    if ((connsm->anchor_point_usecs + connsm->conn_itvl_usecs) >= 31) {
-        ++ce_end;
-    }
-
-    if (ble_ll_sched_next_time(&next_sched_time)) {
-        if (CPUTIME_LT(next_sched_time, ce_end)) {
-            ce_end = next_sched_time;
-        }
-    }
-#endif
-
-    return ce_end;
-}
-
-/**
- * Called to check if certain connection state machine flags have been
- * set.
- *
- * @param connsm
- */
-static void
-ble_ll_conn_chk_csm_flags(struct ble_ll_conn_sm *connsm)
-{
-    uint8_t update_status;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    if (connsm->csmflags.cfbit.send_ltk_req) {
-        /*
-         * Send Long term key request event to host. If masked, we need to
-         * send a REJECT_IND.
-         */
-        if (ble_ll_hci_ev_ltk_req(connsm)) {
-            ble_ll_ctrl_reject_ind_send(connsm, BLE_LL_CTRL_ENC_REQ,
-                                        BLE_ERR_PINKEY_MISSING);
-        }
-        connsm->csmflags.cfbit.send_ltk_req = 0;
-    }
-#endif
-
-    /*
-     * There are two cases where this flag gets set:
-     * 1) A connection update procedure was started and the event counter
-     * has passed the instant.
-     * 2) We successfully sent the reject reason.
-     */
-    if (connsm->csmflags.cfbit.host_expects_upd_event) {
-        update_status = BLE_ERR_SUCCESS;
-        if (IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_CONN_UPDATE)) {
-            ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_CONN_UPDATE);
-        } else {
-            if (IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_CONN_PARAM_REQ)) {
-                ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_CONN_PARAM_REQ);
-                update_status = connsm->reject_reason;
-            }
-        }
-        ble_ll_hci_ev_conn_update(connsm, update_status);
-        connsm->csmflags.cfbit.host_expects_upd_event = 0;
-    }
-
-    /* Check if we need to send PHY update complete event */
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    if (CONN_F_PHY_UPDATE_EVENT(connsm)) {
-        if (!ble_ll_hci_ev_phy_update(connsm, BLE_ERR_SUCCESS)) {
-            /* Sent event. Clear flag */
-            CONN_F_PHY_UPDATE_EVENT(connsm) = 0;
-        }
-    }
-#endif
-}
-
-/**
- * Called when we want to send a data channel pdu inside a connection event.
- *
- * Context: interrupt
- *
- * @param connsm
- *
- * @return int 0: success; otherwise failure to transmit
- */
-static uint16_t
-ble_ll_conn_adjust_pyld_len(struct ble_ll_conn_sm *connsm, uint16_t pyld_len)
-{
-    uint16_t phy_max_tx_octets;
-    uint16_t ret;
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    phy_max_tx_octets = ble_ll_pdu_max_tx_octets_get(connsm->eff_max_tx_time,
-                                                     connsm->phy_data.tx_phy_mode);
-#else
-    phy_max_tx_octets = ble_ll_pdu_max_tx_octets_get(connsm->eff_max_tx_time,
-                                                     BLE_PHY_MODE_1M);
-#endif
-
-    ret = pyld_len;
-
-    if (ret > connsm->eff_max_tx_octets) {
-        ret = connsm->eff_max_tx_octets;
-    }
-
-    if (ret > phy_max_tx_octets) {
-        ret = phy_max_tx_octets;
-    }
-
-    return ret;
-}
-
-static int
-ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm)
-{
-    int rc;
-    uint8_t md;
-    uint8_t hdr_byte;
-    uint8_t end_transition;
-    uint8_t cur_txlen;
-    uint8_t next_txlen;
-    uint8_t cur_offset;
-    uint16_t pktlen;
-    uint32_t next_event_time;
-    uint32_t ticks;
-    struct os_mbuf *m;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct os_mbuf_pkthdr *pkthdr;
-    struct os_mbuf_pkthdr *nextpkthdr;
-    struct ble_ll_empty_pdu empty_pdu;
-    ble_phy_tx_end_func txend_func;
-    int tx_phy_mode;
-
-    /* For compiler warnings... */
-    ble_hdr = NULL;
-    m = NULL;
-    md = 0;
-    hdr_byte = BLE_LL_LLID_DATA_FRAG;
-
-    /*
-     * We need to check if we are retrying a pdu or if there is a pdu on
-     * the transmit queue.
-     */
-    pkthdr = STAILQ_FIRST(&connsm->conn_txq);
-    if (!connsm->cur_tx_pdu && !CONN_F_EMPTY_PDU_TXD(connsm) && !pkthdr) {
-        CONN_F_EMPTY_PDU_TXD(connsm) = 1;
-        goto conn_tx_pdu;
-    }
-
-    /*
-     * If we dont have a pdu we have previously transmitted, take it off
-     * the connection transmit queue
-     */
-    cur_offset = 0;
-    if (!connsm->cur_tx_pdu && !CONN_F_EMPTY_PDU_TXD(connsm)) {
-        /* Convert packet header to mbuf */
-        m = OS_MBUF_PKTHDR_TO_MBUF(pkthdr);
-        nextpkthdr = STAILQ_NEXT(pkthdr, omp_next);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-        /*
-         * If we are encrypting, we are only allowed to send certain
-         * kinds of LL control PDU's. If none is enqueued, send empty pdu!
-         */
-        if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
-            if (!ble_ll_ctrl_enc_allowed_pdu(pkthdr)) {
-                CONN_F_EMPTY_PDU_TXD(connsm) = 1;
-                goto conn_tx_pdu;
-            }
-
-            /*
-             * We will allow a next packet if it itself is allowed or we are
-             * a slave and we are sending the START_ENC_RSP. The master has
-             * to wait to receive the START_ENC_RSP from the slave before
-             * packets can be let go.
-             */
-            if (nextpkthdr && !ble_ll_ctrl_enc_allowed_pdu(nextpkthdr)
-                && ((connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) ||
-                    !ble_ll_ctrl_is_start_enc_rsp(m))) {
-                nextpkthdr = NULL;
-            }
-        }
-#endif
-        /* Take packet off queue*/
-        STAILQ_REMOVE_HEAD(&connsm->conn_txq, omp_next);
-        ble_hdr = BLE_MBUF_HDR_PTR(m);
-
-        /*
-         * We dequeued new packet for transmission so need to calculate payload
-         * length we can send over current PHY. Effectively, this determines
-         * fragmentation of packet into PDUs.
-         */
-        pktlen = pkthdr->omp_len;
-        cur_txlen = ble_ll_conn_adjust_pyld_len(connsm, pktlen);
-        ble_hdr->txinfo.pyld_len = cur_txlen;
-
-        /* NOTE: header was set when first enqueued */
-        hdr_byte = ble_hdr->txinfo.hdr_byte;
-        connsm->cur_tx_pdu = m;
-    } else {
-        nextpkthdr = pkthdr;
-        if (connsm->cur_tx_pdu) {
-            m = connsm->cur_tx_pdu;
-            ble_hdr = BLE_MBUF_HDR_PTR(m);
-            pktlen = OS_MBUF_PKTLEN(m);
-            cur_txlen = ble_hdr->txinfo.pyld_len;
-            cur_offset = ble_hdr->txinfo.offset;
-            if (cur_offset == 0) {
-                hdr_byte = ble_hdr->txinfo.hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
-            }
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-            if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
-                /* We will allow a next packet if it itself is allowed */
-                pkthdr = OS_MBUF_PKTHDR(connsm->cur_tx_pdu);
-                if (nextpkthdr && !ble_ll_ctrl_enc_allowed_pdu(nextpkthdr)
-                    && ((connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) ||
-                        !ble_ll_ctrl_is_start_enc_rsp(connsm->cur_tx_pdu))) {
-                    nextpkthdr = NULL;
-                }
-            }
-#endif
-        } else {
-            /* Empty PDU here. NOTE: header byte gets set later */
-            pktlen = 0;
-            cur_txlen = 0;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-            if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
-                /* We will allow a next packet if it itself is allowed */
-                if (nextpkthdr && !ble_ll_ctrl_enc_allowed_pdu(nextpkthdr)) {
-                    nextpkthdr = NULL;
-                }
-            }
-#endif
-        }
-    }
-
-    /*
-     * Set the more data data flag if we have more data to send and we
-     * have not been asked to terminate
-     */
-    if ((nextpkthdr || ((cur_offset + cur_txlen) < pktlen)) &&
-         !connsm->csmflags.cfbit.terminate_ind_rxd) {
-        /* Get next event time */
-        next_event_time = ble_ll_conn_get_next_sched_time(connsm);
-
-        /* XXX: TODO: need to check this with phy update procedure. There are
-           limitations if we have started update */
-
-        /*
-         * Dont bother to set the MD bit if we cannot do the following:
-         *  -> wait IFS, send the current frame.
-         *  -> wait IFS, receive a maximum size frame.
-         *  -> wait IFS, send the next frame.
-         *  -> wait IFS, receive a maximum size frame.
-         *
-         *  For slave:
-         *  -> wait IFS, send current frame.
-         *  -> wait IFS, receive maximum size frame.
-         *  -> wait IFS, send next frame.
-         */
-        if ((cur_offset + cur_txlen) < pktlen) {
-            next_txlen = pktlen - (cur_offset + cur_txlen);
-        } else {
-            if (nextpkthdr->omp_len > connsm->eff_max_tx_octets) {
-                next_txlen = connsm->eff_max_tx_octets;
-            } else {
-                next_txlen = nextpkthdr->omp_len;
-            }
-        }
-
-        /*
-         * XXX: this calculation is based on using the current time
-         * and assuming the transmission will occur an IFS time from
-         * now. This is not the most accurate especially if we have
-         * received a frame and we are replying to it.
-         */
-#if BLE_LL_BT5_PHY_SUPPORTED
-        tx_phy_mode = connsm->phy_data.tx_phy_mode;
-#else
-        tx_phy_mode = BLE_PHY_MODE_1M;
-#endif
-
-        ticks = (BLE_LL_IFS * 3) + connsm->eff_max_rx_time +
-            ble_ll_pdu_tx_time_get(next_txlen, tx_phy_mode) +
-            ble_ll_pdu_tx_time_get(cur_txlen, tx_phy_mode);
-
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-            ticks += (BLE_LL_IFS + connsm->eff_max_rx_time);
-        }
-
-        ticks = os_cputime_usecs_to_ticks(ticks);
-        if ((int32_t)((os_cputime_get32() + ticks) - next_event_time) < 0) {
-            md = 1;
-        }
-     }
-
-    /* If we send an empty PDU we need to initialize the header */
-conn_tx_pdu:
-    if (CONN_F_EMPTY_PDU_TXD(connsm)) {
-        /*
-         * This looks strange, but we dont use the data pointer in the mbuf
-         * when we have an empty pdu.
-         */
-        m = (struct os_mbuf *)&empty_pdu;
-        m->om_data = (uint8_t *)&empty_pdu;
-        m->om_data += BLE_MBUF_MEMBLOCK_OVERHEAD;
-        ble_hdr = &empty_pdu.ble_hdr;
-        ble_hdr->txinfo.flags = 0;
-        ble_hdr->txinfo.offset = 0;
-        ble_hdr->txinfo.pyld_len = 0;
-    }
-
-    /* Set tx seqnum */
-    if (connsm->tx_seqnum) {
-        hdr_byte |= BLE_LL_DATA_HDR_SN_MASK;
-    }
-
-    /* If we have more data, set the bit */
-    if (md) {
-        hdr_byte |= BLE_LL_DATA_HDR_MD_MASK;
-    }
-
-    /* Set NESN (next expected sequence number) bit */
-    if (connsm->next_exp_seqnum) {
-        hdr_byte |= BLE_LL_DATA_HDR_NESN_MASK;
-    }
-
-    /* Set the header byte in the outgoing frame */
-    ble_hdr->txinfo.hdr_byte = hdr_byte;
-
-    /*
-     * If we are a slave, check to see if this transmission will end the
-     * connection event. We will end the connection event if we have
-     * received a valid frame with the more data bit set to 0 and we dont
-     * have more data.
-     *
-     * XXX: for a slave, we dont check to see if we can:
-     *  -> wait IFS, rx frame from master (either big or small).
-     *  -> wait IFS, send empty pdu or next pdu.
-     *
-     *  We could do this. Now, we just keep going and hope that we dont
-     *  overrun next scheduled item.
-     */
-    if ((connsm->csmflags.cfbit.terminate_ind_rxd) ||
-        ((connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) && (md == 0) &&
-         (connsm->cons_rxd_bad_crc == 0) &&
-         ((connsm->last_rxd_hdr_byte & BLE_LL_DATA_HDR_MD_MASK) == 0) &&
-         !ble_ll_ctrl_is_terminate_ind(hdr_byte, m->om_data[0]))) {
-        /* We will end the connection event */
-        end_transition = BLE_PHY_TRANSITION_NONE;
-        txend_func = ble_ll_conn_wait_txend;
-    } else {
-        /* Wait for a response here */
-        end_transition = BLE_PHY_TRANSITION_TX_RX;
-        txend_func = NULL;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    int is_ctrl;
-    uint8_t llid;
-    uint8_t opcode;
-
-    llid = ble_hdr->txinfo.hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
-    if (llid == BLE_LL_LLID_CTRL) {
-        is_ctrl = 1;
-        opcode = m->om_data[0];
-    } else {
-        is_ctrl = 0;
-        opcode = 0;
-    }
-
-    if (is_ctrl && (opcode == BLE_LL_CTRL_START_ENC_RSP)) {
-        /*
-         * Both master and slave send the START_ENC_RSP encrypted and receive
-         * encrypted
-         */
-        CONN_F_ENCRYPTED(connsm) = 1;
-        connsm->enc_data.tx_encrypted = 1;
-        ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                               connsm->enc_data.iv,
-                               connsm->enc_data.enc_block.cipher_text,
-                               CONN_IS_MASTER(connsm));
-    } else if (is_ctrl && (opcode == BLE_LL_CTRL_START_ENC_REQ)) {
-        /*
-         * Only the slave sends this and it gets sent unencrypted but
-         * we receive encrypted
-         */
-        CONN_F_ENCRYPTED(connsm) = 0;
-        connsm->enc_data.enc_state = CONN_ENC_S_START_ENC_RSP_WAIT;
-        connsm->enc_data.tx_encrypted = 0;
-        ble_phy_encrypt_disable();
-        if (txend_func == NULL) {
-            txend_func = ble_ll_conn_start_rx_encrypt;
-        } else {
-            txend_func = ble_ll_conn_txend_encrypt;
-        }
-    } else if (is_ctrl && (opcode == BLE_LL_CTRL_PAUSE_ENC_RSP)) {
-        /*
-         * The slave sends the PAUSE_ENC_RSP encrypted. The master sends
-         * it unencrypted (note that link was already set unencrypted).
-         */
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            CONN_F_ENCRYPTED(connsm) = 1;
-            connsm->enc_data.tx_encrypted = 1;
-            ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                                   connsm->enc_data.iv,
-                                   connsm->enc_data.enc_block.cipher_text,
-                                   CONN_IS_MASTER(connsm));
-            if (txend_func == NULL) {
-                txend_func = ble_ll_conn_start_rx_unencrypt;
-            } else {
-                txend_func = ble_ll_conn_rxend_unencrypt;
-            }
-        } else {
-            CONN_F_ENCRYPTED(connsm) = 0;
-            connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
-            connsm->enc_data.tx_encrypted = 0;
-            ble_phy_encrypt_disable();
-        }
-    } else {
-        /* If encrypted set packet counter */
-        if (CONN_F_ENCRYPTED(connsm)) {
-            connsm->enc_data.tx_encrypted = 1;
-            ble_phy_encrypt_set_pkt_cntr(connsm->enc_data.tx_pkt_cntr,
-                                         CONN_IS_MASTER(connsm));
-            if (txend_func == NULL) {
-                txend_func = ble_ll_conn_continue_rx_encrypt;
-            }
-        }
-    }
-#endif
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    ble_phy_mode_set(connsm->phy_data.tx_phy_mode,connsm->phy_data.rx_phy_mode);
-#endif
-
-    /* Set transmit end callback */
-    ble_phy_set_txend_cb(txend_func, connsm);
-    rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m, end_transition);
-    if (!rc) {
-        /* Log transmit on connection state */
-        cur_txlen = ble_hdr->txinfo.pyld_len;
-        ble_ll_log(BLE_LL_LOG_ID_CONN_TX,
-                   hdr_byte,
-                   ((uint16_t)ble_hdr->txinfo.offset << 8) | cur_txlen,
-                   (uint32_t)m);
-
-        /* Set last transmitted MD bit */
-        CONN_F_LAST_TXD_MD(connsm) = md;
-
-        /* Increment packets transmitted */
-        if (CONN_F_EMPTY_PDU_TXD(connsm)) {
-            STATS_INC(ble_ll_conn_stats, tx_empty_pdus);
-        } else if ((hdr_byte & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_CTRL) {
-            STATS_INC(ble_ll_conn_stats, tx_ctrl_pdus);
-            STATS_INCN(ble_ll_conn_stats, tx_ctrl_bytes, cur_txlen);
-        } else {
-            STATS_INC(ble_ll_conn_stats, tx_l2cap_pdus);
-            STATS_INCN(ble_ll_conn_stats, tx_l2cap_bytes, cur_txlen);
-        }
-    }
-    return rc;
-}
-
-/**
- * Schedule callback for start of connection event.
- *
- * Context: Interrupt
- *
- * @param sch
- *
- * @return int 0: scheduled item is still running. 1: schedule item is done.
- */
-static int
-ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
-{
-    int rc;
-    uint32_t usecs;
-    uint32_t start;
-    struct ble_ll_conn_sm *connsm;
-
-    /* XXX: note that we can extend end time here if we want. Look at this */
-
-    /* Set current connection state machine */
-    connsm = (struct ble_ll_conn_sm *)sch->cb_arg;
-    g_ble_ll_conn_cur_sm = connsm;
-    assert(connsm);
-
-    /* Disable whitelisting as connections do not use it */
-    ble_ll_whitelist_disable();
-
-    /* Set LL state */
-    ble_ll_state_set(BLE_LL_STATE_CONNECTION);
-
-    /* Log connection event start */
-    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_START, (uint8_t)connsm->conn_handle,
-               (uint16_t)connsm->ce_end_time, connsm->csmflags.conn_flags);
-
-    /* Set channel */
-    ble_phy_setchan(connsm->data_chan_index, connsm->access_addr,
-                    connsm->crcinit);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    ble_phy_resolv_list_disable();
-#endif
-
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-        /* Set start time of transmission */
-        start = sch->start_time + g_ble_ll_sched_offset_ticks;
-        rc = ble_phy_tx_set_start_time(start, sch->remainder);
-        if (!rc) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-            if (CONN_F_ENCRYPTED(connsm)) {
-                ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                                       connsm->enc_data.iv,
-                                       connsm->enc_data.enc_block.cipher_text,
-                                       1);
-            } else {
-                ble_phy_encrypt_disable();
-            }
-#endif
-            rc = ble_ll_conn_tx_data_pdu(connsm);
-            if (!rc) {
-                rc = BLE_LL_SCHED_STATE_RUNNING;
-            } else {
-                /* Inform LL task of connection event end */
-                rc = BLE_LL_SCHED_STATE_DONE;
-            }
-        } else {
-            STATS_INC(ble_ll_conn_stats, conn_ev_late);
-            rc = BLE_LL_SCHED_STATE_DONE;
-        }
-    } else {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-        if (CONN_F_ENCRYPTED(connsm)) {
-            ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
-                                   connsm->enc_data.iv,
-                                   connsm->enc_data.enc_block.cipher_text,
-                                   1);
-        } else {
-            ble_phy_encrypt_disable();
-        }
-#endif
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-        ble_phy_mode_set(connsm->phy_data.rx_phy_mode,
-                             connsm->phy_data.rx_phy_mode);
-#endif
-
-        /* XXX: what is this really for the slave? */
-        start = sch->start_time + g_ble_ll_sched_offset_ticks;
-        rc = ble_phy_rx_set_start_time(start, sch->remainder);
-        if (rc) {
-            /* End the connection event as we have no more buffers */
-            STATS_INC(ble_ll_conn_stats, slave_ce_failures);
-            rc = BLE_LL_SCHED_STATE_DONE;
-        } else {
-            /*
-             * Set flag that tells slave to set last anchor point if a packet
-             * has been received.
-             */
-            connsm->csmflags.cfbit.slave_set_last_anchor = 1;
-
-            /*
-             * Set the wait for response time. The anchor point is when we
-             * expect the master to start transmitting. Worst-case, we expect
-             * to hear a reply within the anchor point plus:
-             *  -> current tx window size
-             *  -> current window widening amount (includes +/- 16 usec jitter)
-             *  -> Amount of time it takes to detect packet start.
-             *  -> Some extra time (16 usec) to insure timing is OK
-             */
-
-            /*
-             * For the 32 kHz crystal, the amount of usecs we have to wait
-             * is not from the anchor point; we have to account for the time
-             * from when the receiver is enabled until the anchor point. The
-             * time we start before the anchor point is this:
-             *   -> current window widening.
-             *   -> up to one 32 kHz tick since we discard remainder.
-             *   -> Up to one tick since the usecs to ticks calc can be off
-             *   by up to one tick.
-             * NOTES:
-             * 1) the 61 we add is for the two ticks mentioned above.
-             * 2) The address rx time and jitter is accounted for in the
-             * phy function
-             */
-            usecs = connsm->slave_cur_tx_win_usecs + 61 +
-                (2 * connsm->slave_cur_window_widening);
-            ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_RX, 0, usecs);
-            /* Set next wakeup time to connection event end time */
-            rc = BLE_LL_SCHED_STATE_RUNNING;
-        }
-    }
-
-    if (rc == BLE_LL_SCHED_STATE_DONE) {
-        ble_ll_event_send(&connsm->conn_ev_end);
-        ble_phy_disable();
-        ble_ll_state_set(BLE_LL_STATE_STANDBY);
-        g_ble_ll_conn_cur_sm = NULL;
-    }
-
-    /* Set time that we last serviced the schedule */
-    connsm->last_scheduled = os_cputime_get32();
-    return rc;
-}
-
-/**
- * Called to determine if the device is allowed to send the next pdu in the
- * connection event. This will always return 'true' if we are a slave. If we
- * are a master, we must be able to send the next fragment and get a minimum
- * sized response from the slave.
- *
- * Context: Interrupt context (rx end isr).
- *
- * @param connsm
- * @param begtime   Time at which IFS before pdu transmission starts
- *
- * @return int 0: not allowed to send 1: allowed to send
- */
-static int
-ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime,
-                              uint32_t add_usecs)
-{
-    int rc;
-    uint8_t rem_bytes;
-    uint32_t ticks;
-    uint32_t usecs;
-    uint32_t next_sched_time;
-    struct os_mbuf *txpdu;
-    struct os_mbuf_pkthdr *pkthdr;
-    struct ble_mbuf_hdr *txhdr;
-    uint32_t allowed_usecs;
-    int tx_phy_mode;
-
-#if BLE_LL_BT5_PHY_SUPPORTED
-    tx_phy_mode = connsm->phy_data.tx_phy_mode;
-#else
-    tx_phy_mode = BLE_PHY_MODE_1M;
-#endif
-
-    rc = 1;
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-        /* Get next scheduled item time */
-        next_sched_time = ble_ll_conn_get_next_sched_time(connsm);
-
-        txpdu = connsm->cur_tx_pdu;
-        if (!txpdu) {
-            pkthdr = STAILQ_FIRST(&connsm->conn_txq);
-            if (pkthdr) {
-                txpdu = OS_MBUF_PKTHDR_TO_MBUF(pkthdr);
-            }
-        } else {
-            pkthdr = OS_MBUF_PKTHDR(txpdu);
-        }
-
-        /* XXX: TODO: need to check this with phy update procedure. There are
-           limitations if we have started update */
-        if (txpdu) {
-            txhdr = BLE_MBUF_HDR_PTR(txpdu);
-            rem_bytes = pkthdr->omp_len - txhdr->txinfo.offset;
-            if (rem_bytes > connsm->eff_max_tx_octets) {
-                rem_bytes = connsm->eff_max_tx_octets;
-            }
-            usecs = ble_ll_pdu_tx_time_get(rem_bytes, tx_phy_mode);
-        } else {
-            /* We will send empty pdu (just a LL header) */
-            usecs = ble_ll_pdu_tx_time_get(0, tx_phy_mode);
-        }
-        usecs += (BLE_LL_IFS * 2) + connsm->eff_max_rx_time;
-
-        ticks = (uint32_t)(next_sched_time - begtime);
-        allowed_usecs = os_cputime_ticks_to_usecs(ticks);
-        if ((usecs + add_usecs) >= allowed_usecs) {
-            rc = 0;
-        }
-    }
-
-    return rc;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
-/**
- * Callback for the Authenticated payload timer. This function is called
- * when the authenticated payload timer expires. When the authenticated
- * payload timeout expires, we should
- *  -> Send the authenticated payload timeout event.
- *  -> Start the LE ping procedure.
- *  -> Restart the timer.
- *
- * @param arg
- */
-void
-ble_ll_conn_auth_pyld_timer_cb(struct os_event *ev)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)ev->ev_arg;
-    ble_ll_auth_pyld_tmo_event_send(connsm);
-    ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_LE_PING);
-    ble_ll_conn_auth_pyld_timer_start(connsm);
-}
-
-void
-ble_ll_conn_rd_features_timer_cb(struct os_event *ev)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    connsm = (struct ble_ll_conn_sm *)ev->ev_arg;
-
-    if (!connsm->csmflags.cfbit.pending_hci_rd_features ||
-                                        !connsm->csmflags.cfbit.rxd_features) {
-        return;
-    }
-
-    ble_ll_hci_ev_rd_rem_used_feat(connsm, BLE_ERR_SUCCESS);
-    connsm->csmflags.cfbit.pending_hci_rd_features = 0;
-}
-
-/**
- * Start (or restart) the authenticated payload timer
- *
- * @param connsm
- */
-void
-ble_ll_conn_auth_pyld_timer_start(struct ble_ll_conn_sm *connsm)
-{
-    int32_t tmo;
-
-    /* Timeout in is in 10 msec units */
-    tmo = (int32_t)BLE_LL_CONN_AUTH_PYLD_OS_TMO(connsm->auth_pyld_tmo);
-    os_callout_reset(&connsm->auth_pyld_timer, tmo);
-}
-#endif
-
-static void
-ble_ll_conn_master_common_init(struct ble_ll_conn_sm *connsm)
-{
-
-    /* Set master role */
-    connsm->conn_role = BLE_LL_CONN_ROLE_MASTER;
-
-    /* Set default ce parameters */
-
-    /*
-     * XXX: for now, we need twice the transmit window as our calculations
-     * for the transmit window offset could be off.
-     */
-    connsm->tx_win_size = BLE_LL_CONN_TX_WIN_MIN + 1;
-    connsm->tx_win_off = 0;
-    connsm->master_sca = MYNEWT_VAL(BLE_LL_MASTER_SCA);
-
-    /* Hop increment is a random value between 5 and 16. */
-    connsm->hop_inc = (rand() % 12) + 5;
-
-    /* Set channel map to map requested by host */
-    connsm->num_used_chans = g_ble_ll_conn_params.num_used_chans;
-    memcpy(connsm->chanmap, g_ble_ll_conn_params.master_chan_map,
-           BLE_LL_CONN_CHMAP_LEN);
-
-    /*  Calculate random access address and crc initialization value */
-    connsm->access_addr = ble_ll_conn_calc_access_addr();
-    connsm->crcinit = rand() & 0xffffff;
-
-    /* Set initial schedule callback */
-    connsm->conn_sch.sched_cb = ble_ll_conn_event_start_cb;
-}
-/**
- * Called when a create connection command has been received. This initializes
- * a connection state machine in the master role.
- *
- * NOTE: Must be called before the state machine is started
- *
- * @param connsm
- * @param hcc
- */
-void
-ble_ll_conn_master_init(struct ble_ll_conn_sm *connsm,
-                        struct hci_create_conn *hcc)
-{
-
-    ble_ll_conn_master_common_init(connsm);
-
-    /* Set slave latency and supervision timeout */
-    connsm->slave_latency = hcc->conn_latency;
-    connsm->supervision_tmo = hcc->supervision_timeout;
-
-    /* Set own address type and peer address if needed */
-    connsm->own_addr_type = hcc->own_addr_type;
-    if (hcc->filter_policy == 0) {
-        memcpy(&connsm->peer_addr, &hcc->peer_addr, BLE_DEV_ADDR_LEN);
-        connsm->peer_addr_type = hcc->peer_addr_type;
-    }
-
-    /* XXX: for now, just make connection interval equal to max */
-    connsm->conn_itvl = hcc->conn_itvl_max;
-
-    /* Check the min/max CE lengths are less than connection interval */
-    if (hcc->min_ce_len > (connsm->conn_itvl * 2)) {
-        connsm->min_ce_len = connsm->conn_itvl * 2;
-    } else {
-        connsm->min_ce_len = hcc->min_ce_len;
-    }
-
-    if (hcc->max_ce_len > (connsm->conn_itvl * 2)) {
-        connsm->max_ce_len = connsm->conn_itvl * 2;
-    } else {
-        connsm->max_ce_len = hcc->max_ce_len;
-    }
-}
-
-static void
-ble_ll_update_max_tx_octets_phy_mode(struct ble_ll_conn_sm *connsm)
-{
-    uint32_t usecs;
-
-    usecs = connsm->eff_max_tx_time;
-
-    connsm->max_tx_octets_phy_mode[BLE_PHY_MODE_1M] =
-            ble_ll_pdu_max_tx_octets_get(usecs, BLE_PHY_MODE_1M);
-    connsm->max_tx_octets_phy_mode[BLE_PHY_MODE_2M] =
-            ble_ll_pdu_max_tx_octets_get(usecs, BLE_PHY_MODE_2M);
-    connsm->max_tx_octets_phy_mode[BLE_PHY_MODE_CODED_125KBPS] =
-            ble_ll_pdu_max_tx_octets_get(usecs, BLE_PHY_MODE_CODED_125KBPS);
-    connsm->max_tx_octets_phy_mode[BLE_PHY_MODE_CODED_500KBPS] =
-            ble_ll_pdu_max_tx_octets_get(usecs, BLE_PHY_MODE_CODED_500KBPS);
-}
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-
-static void
-ble_ll_conn_set_phy(struct ble_ll_conn_sm *connsm, int tx_phy, int rx_phy)
-{
-
-    struct ble_ll_conn_phy_data *phy_data = &connsm->phy_data;
-
-    phy_data->rx_phy_mode = ble_ll_phy_to_phy_mode(rx_phy,
-                                                   BLE_HCI_LE_PHY_CODED_ANY);
-    phy_data->cur_rx_phy = rx_phy;
-
-    phy_data->tx_phy_mode = ble_ll_phy_to_phy_mode(tx_phy,
-                                                   BLE_HCI_LE_PHY_CODED_ANY);
-    phy_data->cur_tx_phy = tx_phy;
-
-}
-
-static void
-ble_ll_conn_init_phy(struct ble_ll_conn_sm *connsm, int phy)
-{
-    struct ble_ll_conn_global_params *conngp;
-
-    /* Always initialize symmetric PHY - controller can change this later */
-    ble_ll_conn_set_phy(connsm, phy, phy);
-
-    /* Update data length management to match initial PHY */
-    conngp = &g_ble_ll_conn_params;
-    connsm->max_tx_octets = conngp->conn_init_max_tx_octets;
-    connsm->max_rx_octets = conngp->supp_max_rx_octets;
-    if (phy == BLE_PHY_CODED) {
-        connsm->max_tx_time = conngp->conn_init_max_tx_time_coded;
-        connsm->max_rx_time = BLE_LL_CONN_SUPP_TIME_MAX_CODED;
-        connsm->rem_max_tx_time = BLE_LL_CONN_SUPP_TIME_MIN_CODED;
-        connsm->rem_max_rx_time = BLE_LL_CONN_SUPP_TIME_MIN_CODED;
-    } else {
-        connsm->max_tx_time = conngp->conn_init_max_tx_time_uncoded;
-        connsm->max_rx_time = BLE_LL_CONN_SUPP_TIME_MAX_UNCODED;
-        connsm->rem_max_tx_time = BLE_LL_CONN_SUPP_TIME_MIN_UNCODED;
-        connsm->rem_max_rx_time = BLE_LL_CONN_SUPP_TIME_MIN_UNCODED;
-    }
-    connsm->eff_max_tx_time = connsm->rem_max_tx_time;
-    connsm->eff_max_rx_time = connsm->rem_max_rx_time;
-    connsm->rem_max_tx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->rem_max_rx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->eff_max_tx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->eff_max_rx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-
-    ble_ll_update_max_tx_octets_phy_mode(connsm);
-}
-
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-
-void
-ble_ll_conn_ext_master_init(struct ble_ll_conn_sm *connsm,
-                        struct hci_ext_create_conn *hcc)
-{
-
-    ble_ll_conn_master_common_init(connsm);
-
-    /* Set own address type and peer address if needed */
-    connsm->own_addr_type = hcc->own_addr_type;
-    if (hcc->filter_policy == 0) {
-        memcpy(&connsm->peer_addr, &hcc->peer_addr, BLE_DEV_ADDR_LEN);
-        connsm->peer_addr_type = hcc->peer_addr_type;
-    }
-
-    connsm->initial_params = *hcc;
-}
-
-void
-ble_ll_conn_ext_set_params(struct ble_ll_conn_sm *connsm,
-                           struct hci_ext_conn_params *hcc_params, int phy)
-{
-    /* Set slave latency and supervision timeout */
-    connsm->slave_latency = hcc_params->conn_latency;
-    connsm->supervision_tmo = hcc_params->supervision_timeout;
-
-    /* XXX: for now, just make connection interval equal to max */
-    connsm->conn_itvl = hcc_params->conn_itvl_max;
-
-
-    /* Check the min/max CE lengths are less than connection interval */
-    if (hcc_params->min_ce_len > (connsm->conn_itvl * 2)) {
-        connsm->min_ce_len = connsm->conn_itvl * 2;
-    } else {
-        connsm->min_ce_len = hcc_params->min_ce_len;
-    }
-
-    if (hcc_params->max_ce_len > (connsm->conn_itvl * 2)) {
-        connsm->max_ce_len = connsm->conn_itvl * 2;
-    } else {
-        connsm->max_ce_len = hcc_params->max_ce_len;
-    }
-
-    ble_ll_conn_calc_itvl_ticks(connsm);
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    ble_ll_conn_init_phy(connsm, phy);
-#endif
-}
-
-
-#endif
-
-static void
-ble_ll_conn_set_csa(struct ble_ll_conn_sm *connsm, bool chsel)
-{
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-    if (chsel) {
-        CONN_F_CSA2_SUPP(connsm) = 1;
-        connsm->channel_id = ((connsm->access_addr & 0xffff0000) >> 16) ^
-                              (connsm->access_addr & 0x0000ffff);
-
-        /* calculate the next data channel */
-        connsm->data_chan_index = ble_ll_conn_calc_dci(connsm, 0);
-        return;
-    }
-#endif
-
-    connsm->last_unmapped_chan = 0;
-
-    /* calculate the next data channel */
-    connsm->data_chan_index = ble_ll_conn_calc_dci(connsm, 1);
-}
-
-/**
- * Create a new connection state machine. This is done once per
- * connection when the HCI command "create connection" is issued to the
- * controller or when a slave receives a connect request.
- *
- * Context: Link Layer task
- *
- * @param connsm
- */
-void
-ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm)
-{
-    struct ble_ll_conn_global_params *conn_params;
-
-    /* Reset following elements */
-    connsm->csmflags.conn_flags = 0;
-    connsm->event_cntr = 0;
-    connsm->conn_state = BLE_LL_CONN_STATE_IDLE;
-    connsm->disconnect_reason = 0;
-    connsm->conn_features = 0;
-    memset(connsm->remote_features, 0, sizeof(connsm->remote_features));
-    connsm->vers_nr = 0;
-    connsm->comp_id = 0;
-    connsm->sub_vers_nr = 0;
-    connsm->reject_reason = BLE_ERR_SUCCESS;
-    connsm->conn_rssi = BLE_LL_CONN_UNKNOWN_RSSI;
-    connsm->rpa_index = -1;
-
-    /* XXX: TODO set these based on PHY that started connection */
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    connsm->phy_data.cur_tx_phy = BLE_PHY_1M;
-    connsm->phy_data.cur_rx_phy = BLE_PHY_1M;
-    connsm->phy_data.tx_phy_mode = BLE_PHY_MODE_1M;
-    connsm->phy_data.rx_phy_mode = BLE_PHY_MODE_1M;
-    connsm->phy_data.req_pref_tx_phys_mask = 0;
-    connsm->phy_data.req_pref_rx_phys_mask = 0;
-    connsm->phy_data.host_pref_tx_phys_mask = g_ble_ll_data.ll_pref_tx_phys;
-    connsm->phy_data.host_pref_rx_phys_mask = g_ble_ll_data.ll_pref_rx_phys;
-    connsm->phy_data.phy_options = 0;
-#endif
-
-    /* Reset current control procedure */
-    connsm->cur_ctrl_proc = BLE_LL_CTRL_PROC_IDLE;
-    connsm->pending_ctrl_procs = 0;
-
-    /*
-     * Set handle in connection update procedure to 0. If the handle
-     * is non-zero it means that the host initiated the connection
-     * parameter update request and the rest of the parameters are valid.
-     */
-    connsm->conn_param_req.handle = 0;
-
-    /* Connection end event */
-    connsm->conn_ev_end.ev_arg = connsm;
-    connsm->conn_ev_end.ev_queued = 0;
-    connsm->conn_ev_end.ev_cb = ble_ll_conn_event_end;
-
-    /* Initialize transmit queue and ack/flow control elements */
-    STAILQ_INIT(&connsm->conn_txq);
-    connsm->cur_tx_pdu = NULL;
-    connsm->tx_seqnum = 0;
-    connsm->next_exp_seqnum = 0;
-    connsm->cons_rxd_bad_crc = 0;
-    connsm->last_rxd_sn = 1;
-    connsm->completed_pkts = 0;
-
-    /* initialize data length mgmt */
-    conn_params = &g_ble_ll_conn_params;
-    connsm->max_tx_octets = conn_params->conn_init_max_tx_octets;
-    connsm->max_rx_octets = conn_params->supp_max_rx_octets;
-    connsm->max_tx_time = conn_params->conn_init_max_tx_time;
-    connsm->max_rx_time = conn_params->supp_max_rx_time;
-    connsm->rem_max_tx_time = BLE_LL_CONN_SUPP_TIME_MIN;
-    connsm->rem_max_rx_time = BLE_LL_CONN_SUPP_TIME_MIN;
-    connsm->eff_max_tx_time = BLE_LL_CONN_SUPP_TIME_MIN;
-    connsm->eff_max_rx_time = BLE_LL_CONN_SUPP_TIME_MIN;
-    connsm->rem_max_tx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->rem_max_rx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->eff_max_tx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    connsm->eff_max_rx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-
-    ble_ll_update_max_tx_octets_phy_mode(connsm);
-
-    /* Reset encryption data */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    memset(&connsm->enc_data, 0, sizeof(struct ble_ll_conn_enc_data));
-    connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
-    connsm->auth_pyld_tmo = BLE_LL_CONN_DEF_AUTH_PYLD_TMO;
-    CONN_F_LE_PING_SUPP(connsm) = 1;
-    os_callout_init(&connsm->auth_pyld_timer,
-                    &g_ble_ll_data.ll_evq,
-                    ble_ll_conn_auth_pyld_timer_cb,
-                    connsm);
-#endif
-
-    ble_ll_conn_calc_itvl_ticks(connsm);
-
-    /* Add to list of active connections */
-    SLIST_INSERT_HEAD(&g_ble_ll_conn_active_list, connsm, act_sle);
-}
-
-/**
- * Called when a remotes data length parameters change.
- *
- * Context: Link Layer task
- *
- * @param connsm
- * @param req
- */
-void
-ble_ll_conn_datalen_update(struct ble_ll_conn_sm *connsm,
-                           struct ble_ll_len_req *req)
-{
-    int send_event;
-    uint16_t eff_time;
-    uint16_t eff_bytes;
-
-    /* Update parameters */
-    connsm->rem_max_rx_time = req->max_rx_time;
-    connsm->rem_max_tx_time = req->max_tx_time;
-    connsm->rem_max_rx_octets = req->max_rx_bytes;
-    connsm->rem_max_tx_octets = req->max_tx_bytes;
-
-    /* Assume no event sent */
-    send_event = 0;
-
-    /* See if effective times have changed */
-    eff_time = min(connsm->rem_max_tx_time, connsm->max_rx_time);
-    if (eff_time != connsm->eff_max_rx_time) {
-        connsm->eff_max_rx_time = eff_time;
-        send_event = 1;
-    }
-    eff_time = min(connsm->rem_max_rx_time, connsm->max_tx_time);
-    if (eff_time != connsm->eff_max_tx_time) {
-        connsm->eff_max_tx_time = eff_time;
-        send_event = 1;
-
-        ble_ll_update_max_tx_octets_phy_mode(connsm);
-    }
-    eff_bytes = min(connsm->rem_max_tx_octets, connsm->max_rx_octets);
-    if (eff_bytes != connsm->eff_max_rx_octets) {
-        connsm->eff_max_rx_octets = eff_bytes;
-        send_event = 1;
-    }
-    eff_bytes = min(connsm->rem_max_rx_octets, connsm->max_tx_octets);
-    if (eff_bytes != connsm->eff_max_tx_octets) {
-        connsm->eff_max_tx_octets = eff_bytes;
-        send_event = 1;
-    }
-
-    if (send_event) {
-        ble_ll_hci_ev_datalen_chg(connsm);
-    }
-}
-
-/**
- * Called when a connection is terminated
- *
- * Context: Link Layer task.
- *
- * @param connsm
- * @param ble_err
- */
-void
-ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
-{
-    uint8_t *evbuf;
-    struct os_mbuf *m;
-    struct os_mbuf_pkthdr *pkthdr;
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-    os_sr_t sr;
-#endif
-
-    /* Remove scheduler events just in case */
-    ble_ll_sched_rmv_elem(&connsm->conn_sch);
-
-    /* Stop any control procedures that might be running */
-    os_callout_stop(&connsm->ctrl_proc_rsp_timer);
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
-    os_callout_stop(&connsm->auth_pyld_timer);
-#endif
-
-    /* Remove from the active connection list */
-    SLIST_REMOVE(&g_ble_ll_conn_active_list, connsm, ble_ll_conn_sm, act_sle);
-
-    /* Free the current transmit pdu if there is one. */
-    if (connsm->cur_tx_pdu) {
-        os_mbuf_free_chain(connsm->cur_tx_pdu);
-        connsm->cur_tx_pdu = NULL;
-    }
-
-    /* Free all packets on transmit queue */
-    while (1) {
-        /* Get mbuf pointer from packet header pointer */
-        pkthdr = STAILQ_FIRST(&connsm->conn_txq);
-        if (!pkthdr) {
-            break;
-        }
-        STAILQ_REMOVE_HEAD(&connsm->conn_txq, omp_next);
-
-        m = (struct os_mbuf *)((uint8_t *)pkthdr - sizeof(struct os_mbuf));
-        os_mbuf_free_chain(m);
-    }
-
-    /* Make sure events off queue */
-    os_eventq_remove(&g_ble_ll_data.ll_evq, &connsm->conn_ev_end);
-
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-    /* Remove from occupied periods */
-    OS_ENTER_CRITICAL(sr);
-    assert(g_ble_ll_sched_data.sch_num_occ_periods > 0);
-    assert(g_ble_ll_sched_data.sch_occ_period_mask & connsm->period_occ_mask);
-    --g_ble_ll_sched_data.sch_num_occ_periods;
-    g_ble_ll_sched_data.sch_occ_period_mask &= ~connsm->period_occ_mask;
-    OS_EXIT_CRITICAL(sr);
-#endif
-
-    /* Connection state machine is now idle */
-    connsm->conn_state = BLE_LL_CONN_STATE_IDLE;
-
-    /*
-     * If we have features and there's pending HCI command, send an event before
-     * disconnection event so it does make sense to host.
-     */
-    if (connsm->csmflags.cfbit.pending_hci_rd_features &&
-                                        connsm->csmflags.cfbit.rxd_features) {
-        ble_ll_hci_ev_rd_rem_used_feat(connsm, BLE_ERR_SUCCESS);
-        connsm->csmflags.cfbit.pending_hci_rd_features = 0;
-    }
-
-    /*
-     * We need to send a disconnection complete event or a connection complete
-     * event when the connection ends. We send a connection complete event
-     * only when we were told to cancel the connection creation. If the
-     * ble error is "success" it means that the reset command was received
-     * and we should not send an event
-     */
-    if (ble_err) {
-
-        if ((connsm->csmflags.cfbit.terminate_ind_rxd == 0) &&
-            (ble_err == BLE_ERR_UNK_CONN_ID)) {
-            evbuf = ble_ll_init_get_conn_comp_ev();
-            ble_ll_conn_comp_event_send(connsm, ble_err, evbuf, NULL);
-        } else {
-            ble_ll_disconn_comp_event_send(connsm, ble_err);
-        }
-    }
-
-    /*
-     * If there is still pending read features request HCI command, send an
-     * event to complete it.
-     */
-    if (connsm->csmflags.cfbit.pending_hci_rd_features) {
-        ble_ll_hci_ev_rd_rem_used_feat(connsm, BLE_ERR_UNK_CONN_ID);
-        connsm->csmflags.cfbit.pending_hci_rd_features = 0;
-    }
-
-    /* Put connection state machine back on free list */
-    STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-
-    /* Log connection end */
-    ble_ll_log(BLE_LL_LOG_ID_CONN_END,connsm->conn_handle, ble_err,
-               connsm->event_cntr);
-}
-
-/**
- * Called to move to the next connection event.
- *
- * Context: Link Layer task.
- *
- * @param connsm
- *
- * @return int
- */
-static int
-ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
-{
-    uint16_t latency;
-    uint32_t itvl;
-    uint32_t cur_ww;
-    uint32_t max_ww;
-    struct ble_ll_conn_upd_req *upd;
-    uint32_t ticks;
-    uint32_t usecs;
-
-    /* XXX: deal with connection request procedure here as well */
-    ble_ll_conn_chk_csm_flags(connsm);
-
-    /* If unable to start terminate procedure, start it now */
-    if (connsm->disconnect_reason && !CONN_F_TERMINATE_STARTED(connsm)) {
-        ble_ll_ctrl_terminate_start(connsm);
-    }
-
-    /*
-     * XXX: TODO Probably want to add checks to see if we need to start
-     * a control procedure here as an instant may have prevented us from
-     * starting one.
-     */
-
-    /*
-     * XXX TODO: I think this is technically incorrect. We can allow slave
-     * latency if we are doing one of these updates as long as we
-     * know that the master has received the ACK to the PDU that set
-     * the instant
-     */
-    /* Set event counter to the next connection event that we will tx/rx in */
-    itvl = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
-    latency = 1;
-    if (connsm->csmflags.cfbit.allow_slave_latency      &&
-        !connsm->csmflags.cfbit.conn_update_sched       &&
-        !CONN_F_PHY_UPDATE_SCHED(connsm)                &&
-        !connsm->csmflags.cfbit.chanmap_update_scheduled) {
-        if (connsm->csmflags.cfbit.pkt_rxd) {
-            latency += connsm->slave_latency;
-            itvl = itvl * latency;
-        }
-    }
-    connsm->event_cntr += latency;
-
-    /* Set next connection event start time */
-    /* We can use pre-calculated values for one interval if latency is 1. */
-    if (latency == 1) {
-        connsm->anchor_point += connsm->conn_itvl_ticks;
-        connsm->anchor_point_usecs += connsm->conn_itvl_usecs;
-    } else {
-        uint32_t ticks;
-        ticks = os_cputime_usecs_to_ticks(itvl);
-        connsm->anchor_point += ticks;
-        connsm->anchor_point_usecs += (itvl - os_cputime_ticks_to_usecs(ticks));
-    }
-    if (connsm->anchor_point_usecs >= 31) {
-        ++connsm->anchor_point;
-        connsm->anchor_point_usecs -= 31;
-    }
-
-    /*
-     * If a connection update has been scheduled and the event counter
-     * is now equal to the instant, we need to adjust the start of the
-     * connection by the the transmit window offset. We also copy in the
-     * update parameters as they now should take effect.
-     */
-    if (connsm->csmflags.cfbit.conn_update_sched &&
-        (connsm->event_cntr == connsm->conn_update_req.instant)) {
-
-        /* Set flag so we send connection update event */
-        upd = &connsm->conn_update_req;
-        if ((connsm->conn_role == BLE_LL_CONN_ROLE_MASTER)  ||
-            ((connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) &&
-             IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_CONN_PARAM_REQ)) ||
-            (connsm->conn_itvl != upd->interval)            ||
-            (connsm->slave_latency != upd->latency)         ||
-            (connsm->supervision_tmo != upd->timeout)) {
-            connsm->csmflags.cfbit.host_expects_upd_event = 1;
-        }
-
-        connsm->supervision_tmo = upd->timeout;
-        connsm->slave_latency = upd->latency;
-        connsm->tx_win_size = upd->winsize;
-        connsm->slave_cur_tx_win_usecs =
-            connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
-        connsm->tx_win_off = upd->winoffset;
-        connsm->conn_itvl = upd->interval;
-        ble_ll_conn_calc_itvl_ticks(connsm);
-        if (upd->winoffset != 0) {
-            usecs = upd->winoffset * BLE_LL_CONN_ITVL_USECS;
-            ticks = os_cputime_usecs_to_ticks(usecs);
-            connsm->anchor_point += ticks;
-            usecs = usecs - os_cputime_ticks_to_usecs(ticks);
-            connsm->anchor_point_usecs += usecs;
-            if (connsm->anchor_point_usecs >= 31) {
-                ++connsm->anchor_point;
-                connsm->anchor_point_usecs -= 31;
-            }
-        }
-
-        /* Reset the starting point of the connection supervision timeout */
-        connsm->last_rxd_pdu_cputime = connsm->anchor_point;
-
-        /* Reset update scheduled flag */
-        connsm->csmflags.cfbit.conn_update_sched = 0;
-    }
-
-    /*
-     * If there is a channel map request pending and we have reached the
-     * instant, change to new channel map. Note there is a special case here.
-     * If we received a channel map update with an instant equal to the event
-     * counter, when we get here the event counter has already been
-     * incremented by 1. That is why we do a signed comparison and change to
-     * new channel map once the event counter equals or has passed channel
-     * map update instant.
-     */
-    if (connsm->csmflags.cfbit.chanmap_update_scheduled &&
-        ((int16_t)(connsm->chanmap_instant - connsm->event_cntr) <= 0)) {
-
-        /* XXX: there is a chance that the control packet is still on
-         * the queue of the master. This means that we never successfully
-         * transmitted update request. Would end up killing connection
-           on slave side. Could ignore it or see if still enqueued. */
-        connsm->num_used_chans =
-            ble_ll_conn_calc_used_chans(connsm->req_chanmap);
-        memcpy(connsm->chanmap, connsm->req_chanmap, BLE_LL_CONN_CHMAP_LEN);
-
-        connsm->csmflags.cfbit.chanmap_update_scheduled = 0;
-
-        ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_CHAN_MAP_UPD);
-
-        /* XXX: host could have resent channel map command. Need to
-           check to make sure we dont have to restart! */
-    }
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    if (CONN_F_PHY_UPDATE_SCHED(connsm) &&
-        (connsm->event_cntr == connsm->phy_instant)) {
-
-        /* Set cur phy to new phy */
-        if (connsm->phy_data.new_tx_phy) {
-            connsm->phy_data.cur_tx_phy = connsm->phy_data.new_tx_phy;
-            connsm->phy_data.tx_phy_mode =
-                                ble_ll_phy_to_phy_mode(connsm->phy_data.cur_tx_phy,
-                                                   connsm->phy_data.phy_options);
-        }
-
-        if (connsm->phy_data.new_rx_phy) {
-            connsm->phy_data.cur_rx_phy = connsm->phy_data.new_rx_phy;
-            connsm->phy_data.rx_phy_mode =
-                                ble_ll_phy_to_phy_mode(connsm->phy_data.cur_rx_phy,
-                                                   connsm->phy_data.phy_options);
-        }
-
-        /* Clear flags and set flag to send event at next instant */
-        CONN_F_PHY_UPDATE_SCHED(connsm) = 0;
-        CONN_F_PHY_UPDATE_EVENT(connsm) = 1;
-
-        ble_ll_ctrl_phy_update_proc_complete(connsm);
-    }
-#endif
-
-    /* Calculate data channel index of next connection event */
-    connsm->data_chan_index = ble_ll_conn_calc_dci(connsm, latency);
-
-    /*
-     * If we are trying to terminate connection, check if next wake time is
-     * passed the termination timeout. If so, no need to continue with
-     * connection as we will time out anyway.
-     */
-    if (CONN_F_TERMINATE_STARTED(connsm)) {
-        if ((int32_t)(connsm->terminate_timeout - connsm->anchor_point) <= 0) {
-            return -1;
-        }
-    }
-
-    /*
-     * Calculate ce end time. For a slave, we need to add window widening and
-     * the transmit window if we still have one.
-     */
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-    itvl = g_ble_ll_sched_data.sch_ticks_per_period;
-#else
-    itvl = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT;
-#endif
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-        cur_ww = ble_ll_conn_calc_window_widening(connsm);
-        max_ww = (connsm->conn_itvl * (BLE_LL_CONN_ITVL_USECS/2)) - BLE_LL_IFS;
-        if (cur_ww >= max_ww) {
-            return -1;
-        }
-        cur_ww += BLE_LL_JITTER_USECS;
-        connsm->slave_cur_window_widening = cur_ww;
-        itvl += os_cputime_usecs_to_ticks(cur_ww + connsm->slave_cur_tx_win_usecs);
-    }
-    itvl -= g_ble_ll_sched_offset_ticks;
-    connsm->ce_end_time = connsm->anchor_point + itvl;
-
-    return 0;
-}
-
-/**
- * Called when a connection has been created. This function will
- *  -> Set the connection state to created.
- *  -> Start the connection supervision timer
- *  -> Set the Link Layer state to connection.
- *  -> Send a connection complete event.
- *
- *  See Section 4.5.2 Vol 6 Part B
- *
- *  Context: Link Layer
- *
- * @param connsm
- *
- * @ return 0: connection NOT created. 1: connection created
- */
-static int
-ble_ll_conn_created(struct ble_ll_conn_sm *connsm, struct ble_mbuf_hdr *rxhdr)
-{
-    int rc;
-    uint8_t *evbuf;
-    uint32_t endtime;
-    uint32_t usecs;
-
-    /* XXX: TODO this assumes we received in 1M phy */
-
-    /* Set state to created */
-    connsm->conn_state = BLE_LL_CONN_STATE_CREATED;
-
-    /* Clear packet received flag */
-    connsm->csmflags.cfbit.pkt_rxd = 0;
-
-    /* Consider time created the last scheduled time */
-    connsm->last_scheduled = os_cputime_get32();
-
-    /*
-     * Set the last rxd pdu time since this is where we want to start the
-     * supervision timer from.
-     */
-    connsm->last_rxd_pdu_cputime = connsm->last_scheduled;
-
-    /*
-     * Set first connection event time. If slave the endtime is the receive end
-     * time of the connect request. The actual connection starts 1.25 msecs plus
-     * the transmit window offset from the end of the connection request.
-     */
-    rc = 1;
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-        /*
-         * With a 32.768 kHz crystal we dont care about the remaining usecs
-         * when setting last anchor point. The only thing last anchor is used
-         * for is to calculate window widening. The effect of this is
-         * negligible.
-         */
-        connsm->last_anchor_point = rxhdr->beg_cputime;
-
-        usecs = rxhdr->rem_usecs + 1250 +
-                (connsm->tx_win_off * BLE_LL_CONN_TX_WIN_USECS) +
-                ble_ll_pdu_tx_time_get(BLE_CONNECT_REQ_LEN,
-                                       rxhdr->rxinfo.phy_mode);
-
-        if (rxhdr->rxinfo.channel < BLE_PHY_NUM_DATA_CHANS) {
-            switch (rxhdr->rxinfo.phy) {
-            case BLE_PHY_1M:
-            case BLE_PHY_2M:
-                usecs += 1250;
-                break;
-            case BLE_PHY_CODED:
-                usecs += 2500;
-                break;
-            default:
-                assert(0);
-                break;
-            }
-        }
-
-        /* Anchor point is cputime. */
-        endtime = os_cputime_usecs_to_ticks(usecs);
-        connsm->anchor_point = rxhdr->beg_cputime + endtime;
-        connsm->anchor_point_usecs = usecs - os_cputime_ticks_to_usecs(endtime);
-        if (connsm->anchor_point_usecs == 31) {
-            ++connsm->anchor_point;
-            connsm->anchor_point_usecs = 0;
-        }
-
-        connsm->slave_cur_tx_win_usecs =
-            connsm->tx_win_size * BLE_LL_CONN_TX_WIN_USECS;
-#if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
-        connsm->ce_end_time = connsm->anchor_point +
-            g_ble_ll_sched_data.sch_ticks_per_period +
-            os_cputime_usecs_to_ticks(connsm->slave_cur_tx_win_usecs) + 1;
-
-#else
-        connsm->ce_end_time = connsm->anchor_point +
-            (MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_32KHZ_TICKS_PER_SLOT)
-            + os_cputime_usecs_to_ticks(connsm->slave_cur_tx_win_usecs) + 1;
-#endif
-        connsm->slave_cur_window_widening = BLE_LL_JITTER_USECS;
-
-        /* Start the scheduler for the first connection event */
-        while (ble_ll_sched_slave_new(connsm)) {
-            if (ble_ll_conn_next_event(connsm)) {
-                STATS_INC(ble_ll_conn_stats, cant_set_sched);
-                rc = 0;
-                break;
-            }
-        }
-    }
-
-    /* Send connection complete event to inform host of connection */
-    if (rc) {
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-        /*
-         * If we have default phy preferences and they are different than
-         * the current PHY's in use, start update procedure.
-         */
-        /*
-         * XXX: should we attempt to start this without knowing if
-         * the other side can support it?
-         */
-        if (!ble_ll_conn_chk_phy_upd_start(connsm)) {
-            CONN_F_CTRLR_PHY_UPDATE(connsm) = 1;
-        }
-#endif
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            ble_ll_adv_send_conn_comp_ev(connsm, rxhdr);
-        } else {
-            evbuf = ble_ll_init_get_conn_comp_ev();
-            ble_ll_conn_comp_event_send(connsm, BLE_ERR_SUCCESS, evbuf, NULL);
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-            ble_ll_hci_ev_le_csa(connsm);
-#endif
-
-            /*
-             * Initiate features exchange
-             *
-             * XXX we do this only as a master as it was observed that sending
-             * LL_SLAVE_FEATURE_REQ after connection breaks some recent iPhone
-             * models; for slave just assume master will initiate features xchg
-             * if it has some additional features to use.
-             */
-            ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_FEATURE_XCHG);
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Called upon end of connection event
- *
- * Context: Link-layer task
- *
- * @param void *arg Pointer to connection state machine
- *
- */
-static void
-ble_ll_conn_event_end(struct os_event *ev)
-{
-    uint8_t ble_err;
-    uint32_t tmo;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Better be a connection state machine! */
-    connsm = (struct ble_ll_conn_sm *)ev->ev_arg;
-    assert(connsm);
-
-    /* Check if we need to resume scanning */
-    ble_ll_scan_chk_resume();
-
-#ifdef BLE_XCVR_RFCLK
-    ble_ll_sched_rfclk_chk_restart();
-#endif
-
-    /* If we have transmitted the terminate IND successfully, we are done */
-    if ((connsm->csmflags.cfbit.terminate_ind_txd) ||
-        (connsm->csmflags.cfbit.terminate_ind_rxd)) {
-        if (connsm->csmflags.cfbit.terminate_ind_txd) {
-            ble_err = BLE_ERR_CONN_TERM_LOCAL;
-        } else {
-            /* Make sure the disconnect reason is valid! */
-            ble_err = connsm->rxd_disconnect_reason;
-            if (ble_err == 0) {
-                ble_err = BLE_ERR_REM_USER_CONN_TERM;
-            }
-        }
-        ble_ll_conn_end(connsm, ble_err);
-        return;
-    }
-
-    /* Remove any connection end events that might be enqueued */
-    os_eventq_remove(&g_ble_ll_data.ll_evq, &connsm->conn_ev_end);
-
-    /*
-     * If we have received a packet, we can set the current transmit window
-     * usecs to 0 since we dont need to listen in the transmit window.
-     */
-    if (connsm->csmflags.cfbit.pkt_rxd) {
-        connsm->slave_cur_tx_win_usecs = 0;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
-    /*
-     * If we are encrypted and have passed the authenticated payload timeout
-     * we need to send an event to tell the host. Unfortunately, I think we
-     * need one of these per connection and we have to set this timer
-     * fairly accurately. So we need to another event in the connection.
-     * This sucks.
-     *
-     * The way this works is that whenever the timer expires it just gets reset
-     * and we send the autheticated payload timeout event. Note that this timer
-     * should run even when encryption is paused.
-     * XXX: what should be here? Was there code here that got deleted?
-     */
-#endif
-
-    /* Move to next connection event */
-    if (ble_ll_conn_next_event(connsm)) {
-        ble_ll_conn_end(connsm, BLE_ERR_CONN_TERM_LOCAL);
-        return;
-    }
-
-    /* Reset "per connection event" variables */
-    connsm->cons_rxd_bad_crc = 0;
-    connsm->csmflags.cfbit.pkt_rxd = 0;
-
-    /* See if we need to start any control procedures */
-    ble_ll_ctrl_chk_proc_start(connsm);
-
-    /* Set initial schedule callback */
-    connsm->conn_sch.sched_cb = ble_ll_conn_event_start_cb;
-
-    /* XXX: I think all this fine for when we do connection updates, but
-       we may want to force the first event to be scheduled. Not sure */
-    /* Schedule the next connection event */
-    while (ble_ll_sched_conn_reschedule(connsm)) {
-        if (ble_ll_conn_next_event(connsm)) {
-            ble_ll_conn_end(connsm, BLE_ERR_CONN_TERM_LOCAL);
-            return;
-        }
-    }
-
-    /*
-     * This is definitely not perfect but hopefully will be fine in regards to
-     * the specification. We check the supervision timer at connection event
-     * end. If the next connection event is going to start past the supervision
-     * timeout we end the connection here. I guess this goes against the spec
-     * in two ways:
-     * 1) We are actually causing a supervision timeout before the time
-     * specified. However, this is really a moot point because the supervision
-     * timeout would have expired before we could possibly receive a packet.
-     * 2) We may end the supervision timeout a bit later than specified as
-     * we only check this at event end and a bad CRC could cause us to continue
-     * the connection event longer than the supervision timeout. Given that two
-     * bad CRC's consecutively ends the connection event, I dont regard this as
-     * a big deal but it could cause a slightly longer supervision timeout.
-     */
-    if (connsm->conn_state == BLE_LL_CONN_STATE_CREATED) {
-        tmo = (uint32_t)connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS * 6UL;
-        ble_err = BLE_ERR_CONN_ESTABLISHMENT;
-    } else {
-        tmo = connsm->supervision_tmo * BLE_HCI_CONN_SPVN_TMO_UNITS * 1000UL;
-        ble_err = BLE_ERR_CONN_SPVN_TMO;
-    }
-    /* XXX: Convert to ticks to usecs calculation instead??? */
-    tmo = os_cputime_usecs_to_ticks(tmo);
-    if ((int32_t)(connsm->anchor_point - connsm->last_rxd_pdu_cputime) >= tmo) {
-        ble_ll_conn_end(connsm, ble_err);
-        return;
-    }
-
-    /* Log event end */
-    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_END, connsm->conn_handle,
-               connsm->event_cntr, connsm->conn_sch.start_time);
-
-    /* If we have completed packets, send an event */
-    ble_ll_conn_num_comp_pkts_event_send(connsm);
-
-    /* If we have features and there's pending HCI command, send an event */
-    if (connsm->csmflags.cfbit.pending_hci_rd_features &&
-                                        connsm->csmflags.cfbit.rxd_features) {
-        ble_ll_hci_ev_rd_rem_used_feat(connsm, BLE_ERR_SUCCESS);
-        connsm->csmflags.cfbit.pending_hci_rd_features = 0;
-    }
-}
-
-/**
- * Update the connection request PDU with the address type and address of
- * advertiser we are going to send connect request to.
- *
- * @param m
- * @param adva
- * @param addr_type     Address type of ADVA from received advertisement.
- * @param txoffset      The tx window offset for this connection
- */
-static void
-ble_ll_conn_req_pdu_update(struct os_mbuf *m, uint8_t *adva, uint8_t addr_type,
-                           uint16_t txoffset, int rpa_index)
-{
-    uint8_t hdr;
-    uint8_t *dptr;
-    uint8_t *addr;
-    struct ble_mbuf_hdr *ble_hdr;
-    struct ble_ll_conn_sm *connsm;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    int is_rpa;
-    struct ble_ll_resolv_entry *rl;
-#endif
-
-    assert(m != NULL);
-
-    /* clear txadd/rxadd bits only */
-    ble_hdr = BLE_MBUF_HDR_PTR(m);
-    hdr = ble_hdr->txinfo.hdr_byte &
-          ~(BLE_ADV_PDU_HDR_RXADD_MASK | BLE_ADV_PDU_HDR_TXADD_MASK);
-
-    if (addr_type) {
-        /* Set random address */
-        hdr |= BLE_ADV_PDU_HDR_RXADD_MASK;
-    }
-
-    dptr = m->om_data;
-
-    /* Get pointer to our device address */
-    connsm = g_ble_ll_conn_create_sm;
-    if ((connsm->own_addr_type & 1) == 0) {
-        addr = g_dev_addr;
-    } else {
-        hdr |= BLE_ADV_PDU_HDR_TXADD_RAND;
-        addr = g_random_addr;
-    }
-
-    /* XXX: do this ahead of time? Calculate the local rpa I mean */
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    if (connsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-        rl = NULL;
-        is_rpa = ble_ll_is_rpa(adva, addr_type);
-        if (is_rpa) {
-            if (rpa_index >= 0) {
-                rl = &g_ble_ll_resolv_list[rpa_index];
-            }
-        } else {
-            if (ble_ll_resolv_enabled()) {
-                rl = ble_ll_resolv_list_find(adva, addr_type);
-            }
-        }
-
-        /*
-         * If peer in on resolving list, we use RPA generated with Local IRK
-         * from resolving list entry. In other case, we need to use our identity
-         * address (see  Core 5.0, Vol 6, Part B, section 6.4).
-         */
-        if (rl) {
-            hdr |= BLE_ADV_PDU_HDR_TXADD_RAND;
-            ble_ll_resolv_gen_priv_addr(rl, 1, dptr);
-            addr = NULL;
-        }
-    }
-#endif
-
-    if (addr) {
-        memcpy(dptr, addr, BLE_DEV_ADDR_LEN);
-    }
-    memcpy(dptr + BLE_DEV_ADDR_LEN, adva, BLE_DEV_ADDR_LEN);
-    put_le16(dptr + 20, txoffset);
-
-    /* Set BLE transmit header */
-    ble_hdr->txinfo.hdr_byte = hdr;
-}
-
-/* Returns true if the address matches the connection peer address having in
- * mind privacy mode
- */
-static int
-ble_ll_conn_is_peer_adv(uint8_t addr_type, uint8_t *adva, int index)
-{
-    int rc;
-    uint8_t *peer_addr;
-    struct ble_ll_conn_sm *connsm;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-    struct ble_ll_resolv_entry *rl;
-#endif
-
-    /* XXX: Deal with different types of random addresses here! */
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return 0;
-    }
-
-    switch (connsm->peer_addr_type) {
-    /* Fall-through intentional */
-    case BLE_HCI_CONN_PEER_ADDR_PUBLIC:
-    case BLE_HCI_CONN_PEER_ADDR_RANDOM:
-        if (addr_type == connsm->peer_addr_type) {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-                /* Peer uses its identity address. Let's verify privacy mode*/
-            if (ble_ll_resolv_enabled()) {
-                rl = ble_ll_resolv_list_find(adva, addr_type);
-                if (rl && (rl->rl_priv_mode == BLE_HCI_PRIVACY_NETWORK)) {
-                    return 0;
-                }
-            }
-#endif
-            peer_addr = adva;
-            break;
-        }
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
-        /* Check if peer uses RPA. If so and it match, use it as controller
-         * supports privacy mode
-         */
-        if ((index < 0) ||
-            (g_ble_ll_resolv_list[index].rl_addr_type != connsm->peer_addr_type)) {
-            return 0;
-        }
-
-        peer_addr = g_ble_ll_resolv_list[index].rl_identity_addr;
-
-        break;
-    case BLE_HCI_CONN_PEER_ADDR_PUBLIC_IDENT:
-        if ((index < 0) ||
-            (g_ble_ll_resolv_list[index].rl_addr_type != 0)) {
-            return 0;
-        }
-        peer_addr = g_ble_ll_resolv_list[index].rl_identity_addr;
-        break;
-    case BLE_HCI_CONN_PEER_ADDR_RANDOM_IDENT:
-        if ((index < 0) ||
-            (g_ble_ll_resolv_list[index].rl_addr_type != 1)) {
-            return 0;
-        }
-        peer_addr = g_ble_ll_resolv_list[index].rl_identity_addr;
-        break;
-#endif
-    default:
-        peer_addr = NULL;
-        break;
-    }
-
-    rc = 0;
-    if (peer_addr) {
-        if (!memcmp(peer_addr, connsm->peer_addr, BLE_DEV_ADDR_LEN)) {
-            rc = 1;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Called when a connect request transmission is done.
- *
- * Context: ISR
- *
- * @param arg
- */
-static void
-ble_ll_conn_req_txend(void *arg)
-{
-    ble_ll_state_set(BLE_LL_STATE_STANDBY);
-}
-
-static void
-ble_ll_conn_req_txend_init(void *arg)
-{
-    ble_ll_state_set(BLE_LL_STATE_INITIATING);
-}
-/**
- * Send a connection requestion to an advertiser
- *
- * Context: Interrupt
- *
- * @param addr_type Address type of advertiser
- * @param adva Address of advertiser
- */
-int
-ble_ll_conn_request_send(uint8_t addr_type, uint8_t *adva, uint16_t txoffset,
-                         int rpa_index, uint8_t end_trans)
-{
-    struct os_mbuf *m;
-    int rc;
-
-    /* XXX: TODO: assume we are already on correct phy */
-    m = ble_ll_scan_get_pdu();
-    ble_ll_conn_req_pdu_update(m, adva, addr_type, txoffset, rpa_index);
-    if (end_trans == BLE_PHY_TRANSITION_NONE) {
-        ble_phy_set_txend_cb(ble_ll_conn_req_txend, NULL);
-    } else {
-        ble_phy_set_txend_cb(ble_ll_conn_req_txend_init, NULL);
-    }
-    rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m, end_trans);
-    return rc;
-}
-
-/**
- * Called when a schedule item overlaps the currently running connection
- * event. This generally should not happen, but if it does we stop the
- * current connection event to let the schedule item run.
- *
- * NOTE: the phy has been disabled as well as the wfr timer before this is
- * called.
- */
-void
-ble_ll_conn_event_halt(void)
-{
-    ble_ll_state_set(BLE_LL_STATE_STANDBY);
-    if (g_ble_ll_conn_cur_sm) {
-        g_ble_ll_conn_cur_sm->csmflags.cfbit.pkt_rxd = 0;
-        ble_ll_event_send(&g_ble_ll_conn_cur_sm->conn_ev_end);
-        g_ble_ll_conn_cur_sm = NULL;
-    }
-}
-
-/**
- * Process a received PDU while in the initiating state.
- *
- * Context: Link Layer task.
- *
- * @param pdu_type
- * @param rxbuf
- * @param ble_hdr
- */
-void
-ble_ll_init_rx_pkt_in(uint8_t pdu_type, uint8_t *rxbuf,
-                      struct ble_mbuf_hdr *ble_hdr)
-{
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    int8_t rpa_index;
-#endif
-    uint8_t addr_type;
-    uint8_t *addr;
-    uint8_t *adv_addr;
-    struct ble_ll_conn_sm *connsm;
-    int ext_adv_mode = -1;
-
-    /* Get the connection state machine we are trying to create */
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return;
-    }
-
-    if (!BLE_MBUF_HDR_CRC_OK(ble_hdr)) {
-        goto scan_continue;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (BLE_MBUF_HDR_AUX_INVALID(ble_hdr)) {
-        goto scan_continue;
-    }
-    if (pdu_type == BLE_ADV_PDU_TYPE_ADV_EXT_IND) {
-        if (BLE_MBUF_HDR_WAIT_AUX(ble_hdr)) {
-            /* Just continue scanning. We are waiting for AUX */
-            if (!ble_ll_sched_aux_scan(ble_hdr, connsm->scansm,
-                                      ble_hdr->rxinfo.user_data)) {
-               /* Wait for aux conn response */
-                ble_hdr->rxinfo.user_data = NULL;
-            }
-            goto scan_continue;
-        }
-    }
-
-    if (CONN_F_AUX_CONN_REQ(connsm)) {
-        /* Wait for connection response */
-        if (pdu_type != BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP) {
-            return;
-        }
-        ble_ll_scan_aux_data_free(ble_hdr->rxinfo.user_data);
-    }
-#endif
-
-    /* If we have sent a connect request, we need to enter CONNECTION state */
-    if (connsm && CONN_F_CONN_REQ_TXD(connsm)) {
-        /* Set address of advertiser to which we are connecting. */
-
-        if (ble_ll_scan_adv_decode_addr(pdu_type, rxbuf, ble_hdr,
-                                        &adv_addr, &addr_type,
-                                        NULL, NULL, &ext_adv_mode)) {
-            return;
-        }
-
-        if (ble_ll_scan_whitelist_enabled()) {
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-            /*
-             * Did we resolve this address? If so, set correct peer address
-             * and peer address type.
-             */
-            rpa_index = connsm->rpa_index;
-
-            if (rpa_index >= 0) {
-                addr_type = g_ble_ll_resolv_list[rpa_index].rl_addr_type + 2;
-                addr = g_ble_ll_resolv_list[rpa_index].rl_identity_addr;
-            } else {
-                addr = adv_addr;
-            }
-#else
-            addr = adv_addr;
-#endif
-
-            connsm->peer_addr_type = addr_type;
-            memcpy(connsm->peer_addr, addr, BLE_DEV_ADDR_LEN);
-        }
-
-        if (connsm->rpa_index >= 0) {
-            ble_ll_scan_set_peer_rpa(rxbuf + BLE_LL_PDU_HDR_LEN);
-        }
-
-        /* Connection has been created. Stop scanning */
-        g_ble_ll_conn_create_sm = NULL;
-        ble_ll_scan_sm_stop(0);
-
-        /* For AUX Connect CSA2 is mandatory. Otherwise we need to check bit
-         * mask
-         */
-        if (ble_hdr->rxinfo.channel < BLE_PHY_NUM_DATA_CHANS) {
-            ble_ll_conn_set_csa(connsm, 1);
-        } else {
-            ble_ll_conn_set_csa(connsm, rxbuf[0] & BLE_ADV_PDU_HDR_CHSEL_MASK);
-        }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-        /* Lets take last used phy */
-        ble_ll_conn_init_phy(connsm, ble_hdr->rxinfo.phy);
-#endif
-#endif
-        ble_ll_conn_created(connsm, NULL);
-        return;
-    }
-
-scan_continue:
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    ble_ll_scan_aux_data_free(ble_hdr->rxinfo.user_data);
-#endif
-    ble_ll_scan_chk_resume();
-}
-
-/**
- * Called when a receive PDU has started and we are in the initiating state.
- *
- * Context: Interrupt
- *
- * @param pdu_type
- * @param ble_hdr
- *
- * @return int
- *  0: we will not attempt to reply to this frame
- *  1: we may send a response to this frame.
- */
-int
-ble_ll_init_rx_isr_start(uint8_t pdu_type, struct ble_mbuf_hdr *ble_hdr)
-{
-    struct ble_ll_conn_sm *connsm;
-    struct ble_ll_scan_sm *scansm;
-
-    connsm = g_ble_ll_conn_create_sm;
-    if (!connsm) {
-        return 0;
-    }
-    scansm = connsm->scansm;
-
-    if ((pdu_type == BLE_ADV_PDU_TYPE_ADV_IND) ||
-        (pdu_type == BLE_ADV_PDU_TYPE_ADV_DIRECT_IND ||
-         pdu_type == BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP)) {
-        return 1;
-    }
-
-    if (pdu_type == BLE_ADV_PDU_TYPE_ADV_EXT_IND && scansm->ext_scanning) {
-        if (connsm->scansm->cur_aux_data) {
-            STATS_INC(ble_ll_stats, aux_received);
-        }
-
-        ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_EXT_ADV;
-        return 1;
-    }
-
-    return 0;
-}
-/**
- * Called when a receive PDU has ended and we are in the initiating state.
- *
- * Context: Interrupt
- *
- * @param rxpdu
- * @param crcok
- * @param ble_hdr
- *
- * @return int
- *       < 0: Disable the phy after reception.
- *      == 0: Success. Do not disable the PHY.
- *       > 0: Do not disable PHY as that has already been done.
- */
-int
-ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
-                       struct ble_mbuf_hdr *ble_hdr)
-{
-    int rc;
-    int resolved;
-    int chk_wl;
-    int index;
-    uint8_t pdu_type;
-    uint8_t addr_type;
-    uint8_t peer_addr_type;
-    uint8_t *adv_addr = NULL;
-    uint8_t *peer;
-    uint8_t *init_addr = NULL;
-    uint8_t init_addr_type;
-    uint8_t pyld_len;
-    uint8_t inita_is_rpa;
-    uint8_t conn_req_end_trans;
-    struct os_mbuf *rxpdu;
-    struct ble_ll_conn_sm *connsm;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    struct ble_ll_resolv_entry *rl;
-#endif
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    struct ble_ll_scan_sm *scansm;
-    uint8_t phy;
-    struct ble_ll_aux_data *aux_data = NULL;
-#endif
-    int ext_adv_mode = -1;
-
-    /* Get connection state machine to use if connection to be established */
-    connsm = g_ble_ll_conn_create_sm;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    scansm = connsm->scansm;
-    ble_hdr->rxinfo.user_data =scansm->cur_aux_data;
-#endif
-
-    rc = -1;
-    pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
-    pyld_len = rxbuf[1];
-
-    if (!crcok) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        /* Invalid packet - make sure we do not wait for AUX_CONNECT_RSP */
-        ble_ll_conn_reset_pending_aux_conn_rsp();
-        scansm->cur_aux_data = NULL;
-#endif
-
-        /* Ignore this packet */
-        goto init_rx_isr_exit;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* If we sent AUX_CONNECT_REQ, we only expect AUX_CONNECT_RSP here */
-    if (CONN_F_AUX_CONN_REQ(connsm)) {
-        if (pdu_type != BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP) {
-            STATS_INC(ble_ll_stats, aux_conn_rsp_err);
-            CONN_F_CONN_REQ_TXD(connsm) = 0;
-            CONN_F_AUX_CONN_REQ(connsm) = 0;
-            ble_ll_sched_rmv_elem(&connsm->conn_sch);
-        }
-        goto init_rx_isr_exit;
-    }
-#endif
-
-    inita_is_rpa = 0;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (pdu_type == BLE_ADV_PDU_TYPE_ADV_EXT_IND) {
-        if (!scansm) {
-            goto init_rx_isr_exit;
-        }
-        if (!scansm->ext_scanning) {
-            goto init_rx_isr_exit;
-        }
-
-        rc = ble_ll_scan_get_aux_data(scansm, ble_hdr, rxbuf, &aux_data);
-        if (rc < 0) {
-            /* No memory or broken packet */
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_AUX_INVALID;
-            ble_ll_scan_aux_data_free(scansm->cur_aux_data);
-            scansm->cur_aux_data = NULL;
-            goto init_rx_isr_exit;
-        }
-
-        ble_hdr->rxinfo.user_data = aux_data;
-    }
-#endif
-
-    /* Lets get addresses from advertising report*/
-    if (ble_ll_scan_adv_decode_addr(pdu_type, rxbuf, ble_hdr,
-                                    &adv_addr, &addr_type,
-                                    &init_addr, &init_addr_type,
-                                    &ext_adv_mode)) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-        ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_AUX_INVALID;
-#endif
-        goto init_rx_isr_exit;
-    }
-
-    switch (pdu_type) {
-    case BLE_ADV_PDU_TYPE_ADV_IND:
-        break;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    case BLE_ADV_PDU_TYPE_ADV_EXT_IND:
-        rc = -1;
-
-        /* If this is not connectable adv mode, lets skip it */
-        if (!(ext_adv_mode & BLE_LL_EXT_ADV_MODE_CONN)) {
-            goto init_rx_isr_exit;
-        }
-
-        if (!adv_addr) {
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_AUX_PTR_WAIT;
-            goto init_rx_isr_exit;
-        }
-
-        if (!init_addr) {
-            break;
-        }
-        /* if there is direct address lets fall down and check it.*/
-        // no break
-#endif
-    case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND:
-            /*
-             * If we expect our address to be private and the INITA is not,
-             * we dont respond!
-             */
-            inita_is_rpa = (uint8_t)ble_ll_is_rpa(init_addr, init_addr_type);
-            if (connsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-                if (!inita_is_rpa) {
-                    goto init_rx_isr_exit;
-                }
-            } else {
-                if (!ble_ll_is_our_devaddr(init_addr, addr_type)) {
-                    goto init_rx_isr_exit;
-                }
-            }
-            break;
-    default:
-        goto init_rx_isr_exit;
-    }
-
-    /* Should we send a connect request? */
-    index = -1;
-    peer = adv_addr;
-    peer_addr_type = addr_type;
-
-    resolved = 0;
-    chk_wl = ble_ll_scan_whitelist_enabled();
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    if (ble_ll_is_rpa(adv_addr, addr_type) && ble_ll_resolv_enabled()) {
-        index = ble_hw_resolv_list_match();
-        if (index >= 0) {
-            rl = &g_ble_ll_resolv_list[index];
-
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_RESOLVED;
-            connsm->rpa_index = index;
-            peer = rl->rl_identity_addr;
-            peer_addr_type = rl->rl_addr_type;
-            resolved = 1;
-
-            /* Assure privacy */
-            if ((rl->rl_priv_mode == BLE_HCI_PRIVACY_NETWORK) && init_addr &&
-                !inita_is_rpa) {
-                goto init_rx_isr_exit;
-            }
-        } else {
-            if (chk_wl) {
-                goto init_rx_isr_exit;
-            }
-        }
-    } else if (init_addr && ble_ll_resolv_enabled()) {
-
-        /* Let's see if we have IRK with that peer. If so lets make sure
-         * privacy mode is correct together with initA
-         */
-        rl = ble_ll_resolv_list_find(adv_addr, addr_type);
-        if (rl && !inita_is_rpa &&
-           (rl->rl_priv_mode == BLE_HCI_PRIVACY_NETWORK)) {
-            goto init_rx_isr_exit;
-        }
-    }
-#endif
-
-    /* Check filter policy */
-    if (chk_wl) {
-        if (!ble_ll_whitelist_match(peer, peer_addr_type, resolved)) {
-            goto init_rx_isr_exit;
-        }
-    } else {
-        /* Must match the connection address */
-        if (!ble_ll_conn_is_peer_adv(addr_type, adv_addr, index)) {
-            goto init_rx_isr_exit;
-        }
-    }
-    ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_DEVMATCH;
-
-    /*
-     * If the inita is a RPA, we must see if it resolves based on the
-     * identity address of the resolved ADVA.
-     */
-    if (init_addr && inita_is_rpa) {
-        if ((index < 0) ||
-            !ble_ll_resolv_rpa(init_addr,
-                               g_ble_ll_resolv_list[index].rl_local_irk)) {
-            goto init_rx_isr_exit;
-        }
-    }
-
-    /* For CONNECT_IND we don't go into RX state */
-    conn_req_end_trans = BLE_PHY_TRANSITION_NONE;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    /* Check if we should send AUX_CONNECT_REQ and wait for AUX_CONNECT_RSP */
-    if (ble_hdr->rxinfo.channel < BLE_PHY_NUM_DATA_CHANS) {
-        conn_req_end_trans = BLE_PHY_TRANSITION_TX_RX;
-    }
-
-    if (connsm->scansm->ext_scanning) {
-            phy = ble_hdr->rxinfo.phy;
-
-            /* Update connection state machine with appropriate parameters for
-             * certain PHY
-             */
-            ble_ll_conn_ext_set_params(connsm,
-                                       &connsm->initial_params.params[phy - 1],
-                                       phy);
-
-    }
-#endif
-
-    /* Create the connection request */
-    ble_ll_conn_req_pdu_make(connsm, ble_hdr->rxinfo.channel);
-
-    if (ble_ll_sched_master_new(connsm, ble_hdr, pyld_len)) {
-        STATS_INC(ble_ll_conn_stats, cant_set_sched);
-        goto init_rx_isr_exit;
-    }
-
-    /* Setup to transmit the connect request */
-    rc = ble_ll_conn_request_send(addr_type, adv_addr,
-                                  connsm->tx_win_off, index,
-                                  conn_req_end_trans);
-    if (rc) {
-        ble_ll_sched_rmv_elem(&connsm->conn_sch);
-        goto init_rx_isr_exit;
-    }
-
-    CONN_F_CONN_REQ_TXD(connsm) = 1;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-    if (ble_hdr->rxinfo.channel < BLE_PHY_NUM_DATA_CHANS) {
-        /* Lets wait for AUX_CONNECT_RSP */
-        CONN_F_AUX_CONN_REQ(connsm) = 1;
-        /* Keep aux data until we get scan response */
-        scansm->cur_aux_data = ble_hdr->rxinfo.user_data;
-        ble_hdr->rxinfo.user_data = NULL;
-        STATS_INC(ble_ll_stats, aux_conn_req_tx);
-    }
-#endif
-
-    STATS_INC(ble_ll_conn_stats, conn_req_txd);
-
-init_rx_isr_exit:
-    /*
-     * We have to restart receive if we cant hand up pdu. We return 0 so that
-     * the phy does not get disabled.
-     */
-    rxpdu = ble_ll_rxpdu_alloc(pyld_len + BLE_LL_PDU_HDR_LEN);
-    if (rxpdu == NULL) {
-        /*
-         * XXX: possible allocate the PDU when we start initiating?
-         * I cannot say I like this solution, but if we cannot allocate a PDU
-         * to hand up to the LL, we need to remove the connection we just
-         * scheduled since the connection state machine will not get processed
-         * by link layer properly. For now, just remove it from the scheduler
-         */
-        if (CONN_F_CONN_REQ_TXD(connsm) == 1) {
-            CONN_F_CONN_REQ_TXD(connsm) = 0;
-            CONN_F_AUX_CONN_REQ(connsm) = 0;
-            ble_ll_sched_rmv_elem(&connsm->conn_sch);
-        }
-        ble_phy_restart_rx();
-        rc = 0;
-    } else {
-        ble_phy_rxpdu_copy(rxbuf, rxpdu);
-        ble_ll_rx_pdu_in(rxpdu);
-    }
-
-    if (rc) {
-        ble_ll_state_set(BLE_LL_STATE_STANDBY);
-    }
-
-    return rc;
-}
-
-/**
- * Function called when a timeout has occurred for a connection. There are
- * two types of timeouts: a connection supervision timeout and control
- * procedure timeout.
- *
- * Context: Link Layer task
- *
- * @param connsm
- * @param ble_err
- */
-void
-ble_ll_conn_timeout(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
-{
-    int was_current;
-    os_sr_t sr;
-
-    was_current = 0;
-    OS_ENTER_CRITICAL(sr);
-    if (g_ble_ll_conn_cur_sm == connsm) {
-        ble_ll_conn_current_sm_over(NULL);
-        was_current = 1;
-    }
-    OS_EXIT_CRITICAL(sr);
-
-    /* Check if we need to resume scanning */
-    if (was_current) {
-        ble_ll_scan_chk_resume();
-    }
-
-    ble_ll_conn_end(connsm, ble_err);
-}
-
-/**
- * Called when a data channel PDU has started that matches the access
- * address of the current connection. Note that the CRC of the PDU has not
- * been checked yet.
- *
- * Context: Interrupt
- *
- * @param rxhdr
- */
-int
-ble_ll_conn_rx_isr_start(struct ble_mbuf_hdr *rxhdr, uint32_t aa)
-{
-    struct ble_ll_conn_sm *connsm;
-
-    /*
-     * Disable wait for response timer since we receive a response. We dont
-     * care if this is the response we were waiting for or not; the code
-     * called at receive end will deal with ending the connection event
-     * if needed
-     */
-    ble_ll_wfr_disable();
-    connsm = g_ble_ll_conn_cur_sm;
-    if (connsm) {
-        /* Double check access address. Better match connection state machine */
-        if (aa != connsm->access_addr) {
-            STATS_INC(ble_ll_conn_stats, rx_data_pdu_bad_aa);
-            ble_ll_state_set(BLE_LL_STATE_STANDBY);
-            ble_ll_event_send(&connsm->conn_ev_end);
-            g_ble_ll_conn_cur_sm = NULL;
-            return -1;
-        }
-
-        /* Set connection handle in mbuf header */
-        rxhdr->rxinfo.handle = connsm->conn_handle;
-
-        /* Set flag denoting we have received a packet in connection event */
-        connsm->csmflags.cfbit.pkt_rxd = 1;
-
-        /* Connection is established */
-        connsm->conn_state = BLE_LL_CONN_STATE_ESTABLISHED;
-
-        /* Set anchor point (and last) if 1st rxd frame in connection event */
-        if (connsm->csmflags.cfbit.slave_set_last_anchor) {
-            connsm->csmflags.cfbit.slave_set_last_anchor = 0;
-            connsm->last_anchor_point = rxhdr->beg_cputime;
-            connsm->anchor_point = connsm->last_anchor_point;
-            connsm->anchor_point_usecs = rxhdr->rem_usecs;
-        }
-    }
-    return 1;
-}
-
-/**
- * Called from the Link Layer task when a data PDU has been received
- *
- * Context: Link layer task
- *
- * @param rxpdu Pointer to received pdu
- * @param rxpdu Pointer to ble mbuf header of received pdu
- */
-void
-ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
-{
-    uint8_t hdr_byte;
-    uint8_t rxd_sn;
-    uint8_t *rxbuf;
-    uint16_t acl_len;
-    uint16_t acl_hdr;
-    struct ble_ll_conn_sm *connsm;
-
-    if (BLE_MBUF_HDR_CRC_OK(hdr)) {
-        /* XXX: there is a chance that the connection was thrown away and
-           re-used before processing packets here. Fix this. */
-        /* We better have a connection state machine */
-        connsm = ble_ll_conn_find_active_conn(hdr->rxinfo.handle);
-        if (connsm) {
-            /* Check state machine */
-            ble_ll_conn_chk_csm_flags(connsm);
-
-            /* Validate rx data pdu */
-            rxbuf = rxpdu->om_data;
-            hdr_byte = rxbuf[0];
-            acl_len = rxbuf[1];
-            acl_hdr = hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
-
-            /*
-             * Check that the LLID and payload length are reasonable.
-             * Empty payload is only allowed for LLID == 01b.
-             *  */
-            if ((acl_hdr == 0) ||
-                ((acl_len == 0) && (acl_hdr != BLE_LL_LLID_DATA_FRAG))) {
-                STATS_INC(ble_ll_conn_stats, rx_bad_llid);
-                goto conn_rx_data_pdu_end;
-            }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
-            /*
-             * Reset authenticated payload timeout if valid MIC. NOTE: we dont
-             * check the MIC failure bit as that would have terminated the
-             * connection
-             */
-            if ((connsm->enc_data.enc_state == CONN_ENC_S_ENCRYPTED) &&
-                CONN_F_LE_PING_SUPP(connsm) && (acl_len != 0)) {
-                ble_ll_conn_auth_pyld_timer_start(connsm);
-            }
-#endif
-
-            /* Update RSSI */
-            connsm->conn_rssi = hdr->rxinfo.rssi;
-
-            /*
-             * If we are a slave, we can only start to use slave latency
-             * once we have received a NESN of 1 from the master
-             */
-            if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-                if (hdr_byte & BLE_LL_DATA_HDR_NESN_MASK) {
-                    connsm->csmflags.cfbit.allow_slave_latency = 1;
-                }
-            }
-
-            /*
-             * Discard the received PDU if the sequence number is the same
-             * as the last received sequence number
-             */
-            rxd_sn = hdr_byte & BLE_LL_DATA_HDR_SN_MASK;
-            if (rxd_sn != connsm->last_rxd_sn) {
-                /* Update last rxd sn */
-                connsm->last_rxd_sn = rxd_sn;
-
-                /* No need to do anything if empty pdu */
-                if ((acl_hdr == BLE_LL_LLID_DATA_FRAG) && (acl_len == 0)) {
-                    goto conn_rx_data_pdu_end;
-                }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-                /*
-                 * XXX: should we check to see if we are in a state where we
-                 * might expect to get an encrypted PDU?
-                 */
-                if (BLE_MBUF_HDR_MIC_FAILURE(hdr)) {
-                    STATS_INC(ble_ll_conn_stats, mic_failures);
-                    ble_ll_conn_timeout(connsm, BLE_ERR_CONN_TERM_MIC);
-                    goto conn_rx_data_pdu_end;
-                }
-#endif
-
-                if (acl_hdr == BLE_LL_LLID_CTRL) {
-                    /* Process control frame */
-                    STATS_INC(ble_ll_conn_stats, rx_ctrl_pdus);
-                    if (ble_ll_ctrl_rx_pdu(connsm, rxpdu)) {
-                        STATS_INC(ble_ll_conn_stats, rx_malformed_ctrl_pdus);
-                    }
-                } else {
-                    /* Count # of received l2cap frames and byes */
-                    STATS_INC(ble_ll_conn_stats, rx_l2cap_pdus);
-                    STATS_INCN(ble_ll_conn_stats, rx_l2cap_bytes, acl_len);
-
-                    /* NOTE: there should be at least two bytes available */
-                    assert(OS_MBUF_LEADINGSPACE(rxpdu) >= 2);
-                    os_mbuf_prepend(rxpdu, 2);
-                    rxbuf = rxpdu->om_data;
-
-                    acl_hdr = (acl_hdr << 12) | connsm->conn_handle;
-                    put_le16(rxbuf, acl_hdr);
-                    put_le16(rxbuf + 2, acl_len);
-                    ble_hci_trans_ll_acl_tx(rxpdu);
-                }
-
-                /* NOTE: we dont free the mbuf since we handed it off! */
-                return;
-            } else {
-                STATS_INC(ble_ll_conn_stats, data_pdu_rx_dup);
-            }
-        } else {
-            STATS_INC(ble_ll_conn_stats, no_conn_sm);
-        }
-    }
-
-    /* Free buffer */
-conn_rx_data_pdu_end:
-    os_mbuf_free_chain(rxpdu);
-}
-
-/**
- * Called when a packet has been received while in the connection state.
- *
- * Context: Interrupt
- *
- * @param rxpdu
- * @param crcok
- *
- * @return int
- *       < 0: Disable the phy after reception.
- *      == 0: Success. Do not disable the PHY.
- *       > 0: Do not disable PHY as that has already been done.
- */
-int
-ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
-{
-    int rc;
-    int is_ctrl;
-    uint8_t hdr_byte;
-    uint8_t hdr_sn;
-    uint8_t hdr_nesn;
-    uint8_t conn_sn;
-    uint8_t conn_nesn;
-    uint8_t reply;
-    uint8_t rem_bytes;
-    uint8_t opcode = 0;
-    uint8_t rx_pyld_len;
-    uint32_t endtime;
-    uint32_t add_usecs;
-    struct os_mbuf *txpdu;
-    struct ble_ll_conn_sm *connsm;
-    struct os_mbuf *rxpdu;
-    struct ble_mbuf_hdr *txhdr;
-    int rx_phy_mode;
-
-    /* Retrieve the header and payload length */
-    hdr_byte = rxbuf[0];
-    rx_pyld_len = rxbuf[1];
-
-    /*
-     * We need to attempt to allocate a buffer here. The reason we do this
-     * now is that we should not ack the packet if we have no receive
-     * buffers available. We want to free up our transmit PDU if it was
-     * acked, but we should not ack the received frame if we cant hand it up.
-     * NOTE: we hand up empty pdu's to the LL task!
-     */
-    rxpdu = ble_ll_rxpdu_alloc(rx_pyld_len + BLE_LL_PDU_HDR_LEN);
-
-    /*
-     * We should have a current connection state machine. If we dont, we just
-     * hand the packet to the higher layer to count it.
-     */
-    rc = -1;
-    connsm = g_ble_ll_conn_cur_sm;
-    if (!connsm) {
-        STATS_INC(ble_ll_conn_stats, rx_data_pdu_no_conn);
-        goto conn_exit;
-    }
-
-    /*
-     * Calculate the end time of the received PDU. NOTE: this looks strange
-     * but for the 32768 crystal we add the time it takes to send the packet
-     * to the 'additional usecs' field to save some calculations.
-     */
-    endtime = rxhdr->beg_cputime;
-#if BLE_LL_BT5_PHY_SUPPORTED
-    rx_phy_mode = connsm->phy_data.rx_phy_mode;
-#else
-    rx_phy_mode = BLE_PHY_MODE_1M;
-#endif
-    add_usecs = rxhdr->rem_usecs +
-            ble_ll_pdu_tx_time_get(rx_pyld_len, rx_phy_mode);
-
-    /*
-     * Check the packet CRC. A connection event can continue even if the
-     * received PDU does not pass the CRC check. If we receive two consecutive
-     * CRC errors we end the conection event.
-     */
-    if (!BLE_MBUF_HDR_CRC_OK(rxhdr)) {
-        /*
-         * Increment # of consecutively received CRC errors. If more than
-         * one we will end the connection event.
-         */
-        ++connsm->cons_rxd_bad_crc;
-        if (connsm->cons_rxd_bad_crc >= 2) {
-            reply = 0;
-        } else {
-            if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-                reply = CONN_F_LAST_TXD_MD(connsm);
-            } else {
-                /* A slave always responds with a packet */
-                reply = 1;
-            }
-        }
-    } else {
-        /* Reset consecutively received bad crcs (since this one was good!) */
-        connsm->cons_rxd_bad_crc = 0;
-
-        /* Set last valid received pdu time (resets supervision timer) */
-        connsm->last_rxd_pdu_cputime = endtime;
-
-        /*
-         * Check for valid LLID before proceeding. We have seen some weird
-         * things with the PHY where the CRC is OK but we dont have a valid
-         * LLID. This should really never happen but if it does we will just
-         * bail. An error stat will get incremented at the LL.
-         */
-        if ((hdr_byte & BLE_LL_DATA_HDR_LLID_MASK) == 0) {
-            goto conn_exit;
-        }
-
-        /* Set last received header byte */
-        connsm->last_rxd_hdr_byte = hdr_byte;
-
-        /*
-         * If SN bit from header does not match NESN in connection, this is
-         * a resent PDU and should be ignored.
-         */
-        hdr_sn = hdr_byte & BLE_LL_DATA_HDR_SN_MASK;
-        conn_nesn = connsm->next_exp_seqnum;
-        if (rxpdu && ((hdr_sn && conn_nesn) || (!hdr_sn && !conn_nesn))) {
-            connsm->next_exp_seqnum ^= 1;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-            if (CONN_F_ENCRYPTED(connsm) && !ble_ll_conn_is_empty_pdu(rxbuf)) {
-                ++connsm->enc_data.rx_pkt_cntr;
-            }
-#endif
-        }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-        ble_ll_log(BLE_LL_LOG_ID_CONN_RX,
-                   hdr_byte,
-                   (uint16_t)connsm->tx_seqnum << 8 | conn_nesn,
-                   connsm->enc_data.rx_pkt_cntr);
-#else
-        ble_ll_log(BLE_LL_LOG_ID_CONN_RX,
-                   hdr_byte,
-                   (uint16_t)connsm->tx_seqnum << 8 | conn_nesn, 0);
-#endif
-
-        /*
-         * Check NESN bit from header. If same as tx seq num, the transmission
-         * is acknowledged. Otherwise we need to resend this PDU.
-         */
-        if (CONN_F_EMPTY_PDU_TXD(connsm) || connsm->cur_tx_pdu) {
-            hdr_nesn = hdr_byte & BLE_LL_DATA_HDR_NESN_MASK;
-            conn_sn = connsm->tx_seqnum;
-            if ((hdr_nesn && conn_sn) || (!hdr_nesn && !conn_sn)) {
-                /* We did not get an ACK. Must retry the PDU */
-                STATS_INC(ble_ll_conn_stats, data_pdu_txf);
-            } else {
-                /* Transmit success */
-                connsm->tx_seqnum ^= 1;
-                STATS_INC(ble_ll_conn_stats, data_pdu_txg);
-
-                /* If we transmitted the empty pdu, clear flag */
-                if (CONN_F_EMPTY_PDU_TXD(connsm)) {
-                    CONN_F_EMPTY_PDU_TXD(connsm) = 0;
-                    goto chk_rx_terminate_ind;
-                }
-
-                /*
-                 * Determine if we should remove packet from queue or if there
-                 * are more fragments to send.
-                 */
-                txpdu = connsm->cur_tx_pdu;
-                if (txpdu) {
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-                    if (connsm->enc_data.tx_encrypted) {
-                        ++connsm->enc_data.tx_pkt_cntr;
-                    }
-#endif
-                    txhdr = BLE_MBUF_HDR_PTR(txpdu);
-                    if ((txhdr->txinfo.hdr_byte & BLE_LL_DATA_HDR_LLID_MASK)
-                        == BLE_LL_LLID_CTRL) {
-                        connsm->cur_tx_pdu = NULL;
-                        /* Note: the mbuf is freed by this call */
-                        rc = ble_ll_ctrl_tx_done(txpdu, connsm);
-                        if (rc) {
-                            /* Means we transmitted a TERMINATE_IND */
-                            goto conn_exit;
-                        } else {
-                            goto chk_rx_terminate_ind;
-                        }
-                    }
-
-                    /* Increment offset based on number of bytes sent */
-                    txhdr->txinfo.offset += txhdr->txinfo.pyld_len;
-                    if (txhdr->txinfo.offset >= OS_MBUF_PKTLEN(txpdu)) {
-                        /* If l2cap pdu, increment # of completed packets */
-                        if (txhdr->txinfo.pyld_len != 0) {
-#if (BLETEST_THROUGHPUT_TEST == 1)
-                            bletest_completed_pkt(connsm->conn_handle);
-#endif
-                            ++connsm->completed_pkts;
-                            if (connsm->completed_pkts > 2) {
-                                os_eventq_put(&g_ble_ll_data.ll_evq,
-                                              &g_ble_ll_data.ll_comp_pkt_ev);
-                            }
-                        }
-                        os_mbuf_free_chain(txpdu);
-                        connsm->cur_tx_pdu = NULL;
-                    } else {
-                        /*  XXX: TODO need to check with phy update procedure.
-                         *  There are limitations if we have started update */
-                        rem_bytes = OS_MBUF_PKTLEN(txpdu) - txhdr->txinfo.offset;
-                        /* Adjust payload for max TX time and octets */
-                        rem_bytes = ble_ll_conn_adjust_pyld_len(connsm, rem_bytes);
-                        txhdr->txinfo.pyld_len = rem_bytes;
-                    }
-                }
-            }
-        }
-
-        /* Should we continue connection event? */
-        /* If this is a TERMINATE_IND, we have to reply */
-chk_rx_terminate_ind:
-        is_ctrl = 0;
-        if ((hdr_byte & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_CTRL) {
-            is_ctrl = 1;
-            opcode = rxbuf[2];
-        }
-
-        /* If we received a terminate IND, we must set some flags */
-        if (is_ctrl && (opcode == BLE_LL_CTRL_TERMINATE_IND)) {
-            connsm->csmflags.cfbit.terminate_ind_rxd = 1;
-            connsm->rxd_disconnect_reason = rxbuf[3];
-            reply = 1;
-        } else if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-            reply = CONN_F_LAST_TXD_MD(connsm) || (hdr_byte & BLE_LL_DATA_HDR_MD_MASK);
-        } else {
-            /* A slave always replies */
-            reply = 1;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-            if (is_ctrl && (opcode == BLE_LL_CTRL_PAUSE_ENC_RSP)) {
-                connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
-            }
-#endif
-        }
-    }
-
-    /* If reply flag set, send data pdu and continue connection event */
-    rc = -1;
-    if (rx_pyld_len && CONN_F_ENCRYPTED(connsm)) {
-        rx_pyld_len += BLE_LL_DATA_MIC_LEN;
-    }
-    if (reply && ble_ll_conn_can_send_next_pdu(connsm, endtime, add_usecs)) {
-        rc = ble_ll_conn_tx_data_pdu(connsm);
-    }
-
-conn_exit:
-    /* Copy the received pdu and hand it up */
-    if (rxpdu) {
-        ble_phy_rxpdu_copy(rxbuf, rxpdu);
-        ble_ll_rx_pdu_in(rxpdu);
-    }
-
-    /* Send link layer a connection end event if over */
-    if (rc) {
-        ble_ll_conn_current_sm_over(connsm);
-    }
-
-    return rc;
-}
-
-/**
- * Called to adjust payload length to fit into max effective octets and TX time
- * on current PHY.
- */
-/**
- * Called to enqueue a packet on the transmit queue of a connection. Should
- * only be called by the controller.
- *
- * Context: Link Layer
- *
- *
- * @param connsm
- * @param om
- */
-void
-ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
-                        uint8_t hdr_byte, uint8_t length)
-{
-    os_sr_t sr;
-    struct os_mbuf_pkthdr *pkthdr;
-    struct ble_mbuf_hdr *ble_hdr;
-    int lifo;
-
-    /* Set mbuf length and packet length if a control PDU */
-    if (hdr_byte == BLE_LL_LLID_CTRL) {
-        om->om_len = length;
-        OS_MBUF_PKTHDR(om)->omp_len = length;
-    }
-
-    /* Set BLE transmit header */
-    ble_hdr = BLE_MBUF_HDR_PTR(om);
-    ble_hdr->txinfo.flags = 0;
-    ble_hdr->txinfo.offset = 0;
-    ble_hdr->txinfo.hdr_byte = hdr_byte;
-
-    /*
-     * Initial payload length is calculate when packet is dequeued, there's no
-     * need to do this now.
-     */
-
-    lifo = 0;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-    if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
-        uint8_t llid;
-
-        /*
-         * If this is one of the following types we need to insert it at
-         * head of queue.
-         */
-        llid = ble_hdr->txinfo.hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
-        if (llid == BLE_LL_LLID_CTRL) {
-            switch (om->om_data[0]) {
-            case BLE_LL_CTRL_TERMINATE_IND:
-            case BLE_LL_CTRL_REJECT_IND:
-            case BLE_LL_CTRL_REJECT_IND_EXT:
-            case BLE_LL_CTRL_START_ENC_REQ:
-            case BLE_LL_CTRL_START_ENC_RSP:
-                lifo = 1;
-                break;
-            case BLE_LL_CTRL_PAUSE_ENC_RSP:
-                if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-                    lifo = 1;
-                }
-                break;
-            default:
-                break;
-            }
-        }
-    }
-#endif
-
-    /* Add to transmit queue for the connection */
-    pkthdr = OS_MBUF_PKTHDR(om);
-    OS_ENTER_CRITICAL(sr);
-    if (lifo) {
-        STAILQ_INSERT_HEAD(&connsm->conn_txq, pkthdr, omp_next);
-    } else {
-        STAILQ_INSERT_TAIL(&connsm->conn_txq, pkthdr, omp_next);
-    }
-    OS_EXIT_CRITICAL(sr);
-}
-
-/**
- * Data packet from host.
- *
- * Context: Link Layer task
- *
- * @param om
- * @param handle
- * @param length
- *
- * @return int
- */
-void
-ble_ll_conn_tx_pkt_in(struct os_mbuf *om, uint16_t handle, uint16_t length)
-{
-    uint8_t hdr_byte;
-    uint16_t conn_handle;
-    uint16_t pb;
-    struct ble_ll_conn_sm *connsm;
-
-    /* See if we have an active matching connection handle */
-    conn_handle = handle & 0x0FFF;
-    connsm = ble_ll_conn_find_active_conn(conn_handle);
-    if (connsm) {
-        /* Construct LL header in buffer (NOTE: pb already checked) */
-        pb = handle & 0x3000;
-        if (pb == 0) {
-            hdr_byte = BLE_LL_LLID_DATA_START;
-        } else {
-            hdr_byte = BLE_LL_LLID_DATA_FRAG;
-        }
-
-        /* Add to total l2cap pdus enqueue */
-        STATS_INC(ble_ll_conn_stats, l2cap_enqueued);
-
-        /* Clear flags field in BLE header */
-        ble_ll_conn_enqueue_pkt(connsm, om, hdr_byte, length);
-    } else {
-        /* No connection found! */
-        STATS_INC(ble_ll_conn_stats, handle_not_found);
-        os_mbuf_free_chain(om);
-    }
-}
-
-/**
- * Called to set the global channel mask that we use for all connections.
- *
- * @param num_used_chans
- * @param chanmap
- */
-void
-ble_ll_conn_set_global_chanmap(uint8_t num_used_chans, uint8_t *chanmap)
-{
-    struct ble_ll_conn_sm *connsm;
-    struct ble_ll_conn_global_params *conn_params;
-
-    /* Do nothing if same channel map */
-    conn_params = &g_ble_ll_conn_params;
-    if (!memcmp(conn_params->master_chan_map, chanmap, BLE_LL_CONN_CHMAP_LEN)) {
-        return;
-    }
-
-    /* Change channel map and cause channel map update procedure to start */
-    conn_params->num_used_chans = num_used_chans;
-    memcpy(conn_params->master_chan_map, chanmap, BLE_LL_CONN_CHMAP_LEN);
-
-    /* Perform channel map update */
-    SLIST_FOREACH(connsm, &g_ble_ll_conn_active_list, act_sle) {
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-            ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_CHAN_MAP_UPD);
-        }
-    }
-}
-
-/**
- * Called when a device has received a connect request while advertising and
- * the connect request has passed the advertising filter policy and is for
- * us. This will start a connection in the slave role assuming that we dont
- * already have a connection with this device and that the connect request
- * parameters are valid.
- *
- * Context: Link Layer
- *
- * @param rxbuf Pointer to received Connect Request PDU
- *
- * @return 0: connection not started; 1 connecton started
- */
-int
-ble_ll_conn_slave_start(uint8_t *rxbuf, uint8_t pat, struct ble_mbuf_hdr *rxhdr,
-                        bool force_csa2)
-{
-    int rc;
-    uint32_t temp;
-    uint32_t crcinit;
-    uint8_t *inita;
-    uint8_t *dptr;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Ignore the connection request if we are already connected*/
-    inita = rxbuf + BLE_LL_PDU_HDR_LEN;
-    SLIST_FOREACH(connsm, &g_ble_ll_conn_active_list, act_sle) {
-        if (!memcmp(&connsm->peer_addr, inita, BLE_DEV_ADDR_LEN)) {
-            if (rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK) {
-                if (connsm->peer_addr_type & 1) {
-                    return 0;
-                }
-            } else {
-                if ((connsm->peer_addr_type & 1) == 0) {
-                    return 0;
-                }
-            }
-        }
-    }
-
-    /* Allocate a connection. If none available, dont do anything */
-    connsm = ble_ll_conn_sm_get();
-    if (connsm == NULL) {
-        return 0;
-    }
-
-    /* Set the pointer at the start of the connection data */
-    dptr = rxbuf + BLE_LL_CONN_REQ_ADVA_OFF + BLE_DEV_ADDR_LEN;
-
-    /* Set connection state machine information */
-    connsm->access_addr = get_le32(dptr);
-    crcinit = dptr[6];
-    crcinit = (crcinit << 8) | dptr[5];
-    crcinit = (crcinit << 8) | dptr[4];
-    connsm->crcinit = crcinit;
-    connsm->tx_win_size = dptr[7];
-    connsm->tx_win_off = get_le16(dptr + 8);
-    connsm->conn_itvl = get_le16(dptr + 10);
-    connsm->slave_latency = get_le16(dptr + 12);
-    connsm->supervision_tmo = get_le16(dptr + 14);
-    memcpy(&connsm->chanmap, dptr + 16, BLE_LL_CONN_CHMAP_LEN);
-    connsm->hop_inc = dptr[21] & 0x1F;
-    connsm->master_sca = dptr[21] >> 5;
-
-    /* Error check parameters */
-    if ((connsm->tx_win_off > connsm->conn_itvl) ||
-        (connsm->conn_itvl < BLE_HCI_CONN_ITVL_MIN) ||
-        (connsm->conn_itvl > BLE_HCI_CONN_ITVL_MAX) ||
-        (connsm->tx_win_size < BLE_LL_CONN_TX_WIN_MIN) ||
-        (connsm->slave_latency > BLE_LL_CONN_SLAVE_LATENCY_MAX)) {
-        goto err_slave_start;
-    }
-
-    /* Slave latency cannot cause a supervision timeout */
-    temp = (connsm->slave_latency + 1) * (connsm->conn_itvl * 2) *
-            BLE_LL_CONN_ITVL_USECS;
-    if ((connsm->supervision_tmo * 10000) <= temp ) {
-        goto err_slave_start;
-    }
-
-    /*
-     * The transmit window must be less than or equal to the lesser of 10
-     * msecs or the connection interval minus 1.25 msecs.
-     */
-    temp = connsm->conn_itvl - 1;
-    if (temp > 8) {
-        temp = 8;
-    }
-    if (connsm->tx_win_size > temp) {
-        goto err_slave_start;
-    }
-
-    /* Set the address of device that we are connecting with */
-    memcpy(&connsm->peer_addr, inita, BLE_DEV_ADDR_LEN);
-    connsm->peer_addr_type = pat;
-
-    /* Calculate number of used channels; make sure it meets min requirement */
-    connsm->num_used_chans = ble_ll_conn_calc_used_chans(connsm->chanmap);
-    if (connsm->num_used_chans < 2) {
-        goto err_slave_start;
-    }
-
-    /* Start the connection state machine */
-    connsm->conn_role = BLE_LL_CONN_ROLE_SLAVE;
-    ble_ll_conn_sm_new(connsm);
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-    /* Use the same PHY as we received CONNECT_REQ on */
-    ble_ll_conn_init_phy(connsm, rxhdr->rxinfo.phy);
-#endif
-
-    ble_ll_conn_set_csa(connsm,
-                        force_csa2 || (rxbuf[0] & BLE_ADV_PDU_HDR_CHSEL_MASK));
-
-    /* Set initial schedule callback */
-    connsm->conn_sch.sched_cb = ble_ll_conn_event_start_cb;
-    rc = ble_ll_conn_created(connsm, rxhdr);
-    if (!rc) {
-        SLIST_REMOVE(&g_ble_ll_conn_active_list, connsm, ble_ll_conn_sm, act_sle);
-        STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-    }
-    return rc;
-
-err_slave_start:
-    STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-    STATS_INC(ble_ll_conn_stats, slave_rxd_bad_conn_req_params);
-    return 0;
-}
-
-#define MAX_TIME_UNCODED(_maxbytes) \
-        ble_ll_pdu_tx_time_get(_maxbytes + BLE_LL_DATA_MIC_LEN, \
-                               BLE_PHY_MODE_1M);
-#define MAX_TIME_CODED(_maxbytes) \
-        ble_ll_pdu_tx_time_get(_maxbytes + BLE_LL_DATA_MIC_LEN, \
-                               BLE_PHY_MODE_CODED_125KBPS);
-
-/**
- * Called to reset the connection module. When this function is called the
- * scheduler has been stopped and the phy has been disabled. The LL should
- * be in the standby state.
- *
- * Context: Link Layer task
- */
-void
-ble_ll_conn_module_reset(void)
-{
-    uint8_t max_phy_pyld;
-    uint16_t maxbytes;
-    struct ble_ll_conn_sm *connsm;
-    struct ble_ll_conn_global_params *conn_params;
-
-    /* Kill the current one first (if one is running) */
-    if (g_ble_ll_conn_cur_sm) {
-        connsm = g_ble_ll_conn_cur_sm;
-        g_ble_ll_conn_cur_sm = NULL;
-        ble_ll_conn_end(connsm, BLE_ERR_SUCCESS);
-    }
-
-    /* Free the global connection complete event if there is one */
-    if (g_ble_ll_conn_comp_ev) {
-        ble_hci_trans_buf_free(g_ble_ll_conn_comp_ev);
-        g_ble_ll_conn_comp_ev = NULL;
-    }
-
-    /* Reset connection we are attempting to create */
-    g_ble_ll_conn_create_sm = NULL;
-
-    /* Now go through and end all the connections */
-    while (1) {
-        connsm = SLIST_FIRST(&g_ble_ll_conn_active_list);
-        if (!connsm) {
-            break;
-        }
-        ble_ll_conn_end(connsm, BLE_ERR_SUCCESS);
-    }
-
-    /* Get the maximum supported PHY PDU size from the PHY */
-    max_phy_pyld = ble_phy_max_data_pdu_pyld();
-
-    /* Configure the global LL parameters */
-    conn_params = &g_ble_ll_conn_params;
-
-    maxbytes = min(MYNEWT_VAL(BLE_LL_SUPP_MAX_RX_BYTES), max_phy_pyld);
-    conn_params->supp_max_rx_octets = maxbytes;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    conn_params->supp_max_rx_time = MAX_TIME_CODED(maxbytes);
-#else
-    conn_params->supp_max_rx_time = MAX_TIME_UNCODED(maxbytes);
-#endif
-
-    maxbytes = min(MYNEWT_VAL(BLE_LL_SUPP_MAX_TX_BYTES), max_phy_pyld);
-    conn_params->supp_max_tx_octets = maxbytes;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    conn_params->supp_max_tx_time = MAX_TIME_CODED(maxbytes);
-#else
-    conn_params->supp_max_tx_time = MAX_TIME_UNCODED(maxbytes);
-#endif
-
-    maxbytes = min(MYNEWT_VAL(BLE_LL_CONN_INIT_MAX_TX_BYTES), max_phy_pyld);
-    conn_params->conn_init_max_tx_octets = maxbytes;
-    conn_params->conn_init_max_tx_time = MAX_TIME_UNCODED(maxbytes);
-    conn_params->conn_init_max_tx_time_uncoded = MAX_TIME_UNCODED(maxbytes);
-    conn_params->conn_init_max_tx_time_coded = MAX_TIME_CODED(maxbytes);
-
-    conn_params->sugg_tx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
-    conn_params->sugg_tx_time = BLE_LL_CONN_SUPP_TIME_MIN;
-
-    /* Mask in all channels by default */
-    conn_params->num_used_chans = BLE_PHY_NUM_DATA_CHANS;
-    memset(conn_params->master_chan_map, 0xff, BLE_LL_CONN_CHMAP_LEN - 1);
-    conn_params->master_chan_map[4] = 0x1f;
-
-    /* Reset statistics */
-    STATS_RESET(ble_ll_conn_stats);
-}
-
-/* Initialize the connection module */
-void
-ble_ll_conn_module_init(void)
-{
-    int rc;
-    uint16_t i;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Initialize list of active conections */
-    SLIST_INIT(&g_ble_ll_conn_active_list);
-    STAILQ_INIT(&g_ble_ll_conn_free_list);
-
-    /*
-     * Take all the connections off the free memory pool and add them to
-     * the free connection list, assigning handles in linear order. Note:
-     * the specification allows a handle of zero; we just avoid using it.
-     */
-    connsm = &g_ble_ll_conn_sm[0];
-    for (i = 0; i < MYNEWT_VAL(BLE_MAX_CONNECTIONS); ++i) {
-
-        memset(connsm, 0, sizeof(struct ble_ll_conn_sm));
-        connsm->conn_handle = i + 1;
-        STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-
-        /* Initialize fixed schedule elements */
-        connsm->conn_sch.sched_type = BLE_LL_SCHED_TYPE_CONN;
-        connsm->conn_sch.cb_arg = connsm;
-        ++connsm;
-    }
-
-    /* Register connection statistics */
-    rc = stats_init_and_reg(STATS_HDR(ble_ll_conn_stats),
-                            STATS_SIZE_INIT_PARMS(ble_ll_conn_stats, STATS_SIZE_32),
-                            STATS_NAME_INIT_PARMS(ble_ll_conn_stats),
-                            "ble_ll_conn");
-    assert(rc == 0);
-
-    /* Call reset to finish reset of initialization */
-    ble_ll_conn_module_reset();
-}
diff --git a/net/nimble/controller/src/ble_ll_conn_hci.c b/net/nimble/controller/src/ble_ll_conn_hci.c
deleted file mode 100644
index c6ea897ac..000000000
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ /dev/null
@@ -1,1569 +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 <assert.h>
-#include "syscfg/syscfg.h"
-#include "bsp/bsp.h"
-#include "os/os.h"
-#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"
-#include "controller/ble_ll_ctrl.h"
-#include "controller/ble_ll_scan.h"
-#include "controller/ble_ll_adv.h"
-#include "ble_ll_conn_priv.h"
-
-/*
- * Used to limit the rate at which we send the number of completed packets
- * event to the host. This is the os time at which we can send an event.
- */
-static uint32_t g_ble_ll_last_num_comp_pkt_evt;
-extern uint8_t *g_ble_ll_conn_comp_ev;
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-static const uint8_t ble_ll_valid_conn_phy_mask = (BLE_HCI_LE_PHY_1M_PREF_MASK
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
-                                | BLE_HCI_LE_PHY_2M_PREF_MASK
-#endif
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-                                | BLE_HCI_LE_PHY_CODED_PREF_MASK
-#endif
-                              );
-static const uint8_t ble_ll_conn_required_phy_mask = (BLE_HCI_LE_PHY_1M_PREF_MASK
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-                            | BLE_HCI_LE_PHY_CODED_PREF_MASK
-#endif
-                            );
-#endif
-
-/**
- * Allocate an event to send a connection complete event when initiating
- *
- * @return int 0: success -1: failure
- */
-static int
-ble_ll_init_alloc_conn_comp_ev(void)
-{
-    int rc;
-    uint8_t *evbuf;
-
-    rc = 0;
-    evbuf = g_ble_ll_conn_comp_ev;
-    if (evbuf == NULL) {
-        evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-        if (!evbuf) {
-            rc = -1;
-        } else {
-            g_ble_ll_conn_comp_ev = evbuf;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Called to check that the connection parameters are within range
- *
- * @param itvl_min
- * @param itvl_max
- * @param latency
- * @param spvn_tmo
- *
- * @return int BLE_ERR_INV_HCI_CMD_PARMS if invalid parameters, 0 otherwise
- */
-int
-ble_ll_conn_hci_chk_conn_params(uint16_t itvl_min, uint16_t itvl_max,
-                                uint16_t latency, uint16_t spvn_tmo)
-{
-    uint32_t spvn_tmo_usecs;
-    uint32_t min_spvn_tmo_usecs;
-
-    if ((itvl_min > itvl_max) ||
-        (itvl_min < BLE_HCI_CONN_ITVL_MIN) ||
-        (itvl_max > BLE_HCI_CONN_ITVL_MAX) ||
-        (latency > BLE_HCI_CONN_LATENCY_MAX) ||
-        (spvn_tmo < BLE_HCI_CONN_SPVN_TIMEOUT_MIN) ||
-        (spvn_tmo > BLE_HCI_CONN_SPVN_TIMEOUT_MAX)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /*
-    * Supervision timeout (in msecs) must be more than:
-    *  (1 + connLatency) * connIntervalMax * 1.25 msecs * 2.
-    */
-    spvn_tmo_usecs = spvn_tmo;
-    spvn_tmo_usecs *= (BLE_HCI_CONN_SPVN_TMO_UNITS * 1000);
-    min_spvn_tmo_usecs = (uint32_t)itvl_max * 2 * BLE_LL_CONN_ITVL_USECS;
-    min_spvn_tmo_usecs *= (1 + latency);
-    if (spvn_tmo_usecs <= min_spvn_tmo_usecs) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Make a connect request PDU
- *
- * @param connsm
- */
-void
-ble_ll_conn_req_pdu_make(struct ble_ll_conn_sm *connsm, uint8_t chan)
-{
-    uint8_t pdu_type;
-    uint8_t *dptr;
-    struct os_mbuf *m;
-
-    m = ble_ll_scan_get_pdu();
-    assert(m != NULL);
-
-    /* Construct first PDU header byte */
-    pdu_type = BLE_ADV_PDU_TYPE_CONNECT_REQ;
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2) == 1)
-    /* We need CSA2 bit only for legacy connect */
-    if (chan >= BLE_PHY_NUM_DATA_CHANS) {
-        pdu_type |= BLE_ADV_PDU_HDR_CHSEL;
-    }
-#endif
-
-    /* Set BLE transmit header */
-    ble_ll_mbuf_init(m, BLE_CONNECT_REQ_LEN, pdu_type);
-
-    /* Construct the connect request */
-    dptr = m->om_data;
-
-    /* Skip inita and adva advertiser's address as we dont know that yet */
-    dptr += (2 * BLE_DEV_ADDR_LEN);
-
-    /* Access address */
-    put_le32(dptr, connsm->access_addr);
-    dptr[4] = (uint8_t)connsm->crcinit;
-    dptr[5] = (uint8_t)(connsm->crcinit >> 8);
-    dptr[6] = (uint8_t)(connsm->crcinit >> 16);
-    dptr[7] = connsm->tx_win_size;
-    put_le16(dptr + 8, connsm->tx_win_off);
-    put_le16(dptr + 10, connsm->conn_itvl);
-    put_le16(dptr + 12, connsm->slave_latency);
-    put_le16(dptr + 14, connsm->supervision_tmo);
-    memcpy(dptr + 16, &connsm->chanmap, BLE_LL_CONN_CHMAP_LEN);
-    dptr[21] = connsm->hop_inc | (connsm->master_sca << 5);
-}
-
-/**
- * Send a connection complete event
- *
- * @param status The BLE error code associated with the event
- */
-void
-ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
-                            uint8_t *evbuf, struct ble_ll_adv_sm *advsm)
-{
-    uint8_t peer_addr_type;
-    uint8_t enabled;
-    uint8_t enh_enabled;
-    uint8_t *evdata;
-    uint8_t *rpa;
-
-    enabled = ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_CONN_COMPLETE);
-    enh_enabled = ble_ll_hci_is_le_event_enabled(BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE);
-
-    if (enabled || enh_enabled) {
-        /* Put common elements in event */
-        evbuf[0] = BLE_HCI_EVCODE_LE_META;
-        if (enh_enabled) {
-            evbuf[1] = BLE_HCI_LE_ENH_CONN_COMPLETE_LEN;
-            evbuf[2] = BLE_HCI_LE_SUBEV_ENH_CONN_COMPLETE;
-        } else {
-            evbuf[1] = BLE_HCI_LE_CONN_COMPLETE_LEN;
-            evbuf[2] = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-        }
-        evbuf[3] = status;
-
-        if (connsm) {
-            put_le16(evbuf + 4, connsm->conn_handle);
-
-            evbuf[6] = connsm->conn_role - 1;
-            peer_addr_type = connsm->peer_addr_type;
-
-            evdata = evbuf + 14;
-            if (enh_enabled) {
-                memset(evdata, 0, 2 * BLE_DEV_ADDR_LEN);
-                if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-                    if (connsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
-                        rpa = ble_ll_scan_get_local_rpa();
-                    } else {
-                        rpa = NULL;
-                    }
-                } else {
-                    rpa = ble_ll_adv_get_local_rpa(advsm);
-                }
-                if (rpa) {
-                    memcpy(evdata, rpa, BLE_DEV_ADDR_LEN);
-                }
-
-                if (connsm->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_RANDOM) {
-                    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-                        rpa = ble_ll_scan_get_peer_rpa();
-                    } else {
-                        rpa = ble_ll_adv_get_peer_rpa(advsm);
-                    }
-                    memcpy(evdata + 6, rpa, BLE_DEV_ADDR_LEN);
-                }
-                evdata += 12;
-            } else {
-                if (peer_addr_type > BLE_HCI_CONN_PEER_ADDR_RANDOM) {
-                    peer_addr_type -= 2;
-                }
-            }
-
-            evbuf[7] = peer_addr_type;
-            memcpy(evbuf + 8, connsm->peer_addr, BLE_DEV_ADDR_LEN);
-
-            put_le16(evdata, connsm->conn_itvl);
-            put_le16(evdata + 2, connsm->slave_latency);
-            put_le16(evdata + 4, connsm->supervision_tmo);
-            evdata[6] = connsm->master_sca;
-        }
-        ble_ll_hci_event_send(evbuf);
-    }
-}
-
-
-/**
- * Called to create and send the number of completed packets event to the
- * host.
- */
-void
-ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
-{
-    /** The maximum number of handles that will fit in an event buffer. */
-    static const int max_handles =
-        (BLE_LL_MAX_EVT_LEN - BLE_HCI_EVENT_HDR_LEN - 1) / 4;
-
-    int event_sent;
-    uint8_t *evbuf;
-    uint8_t *handle_ptr;
-    uint8_t handles;
-
-    if (connsm == NULL) {
-        goto skip_conn;
-    }
-
-    /*
-     * At some periodic rate, make sure we go through all active connections
-     * and send the number of completed packet events. We do this mainly
-     * because the spec says we must update the host even though no packets
-     * have completed but there are data packets in the controller buffers
-     * (i.e. enqueued in a connection state machine).
-     */
-    if ((int32_t)(os_time_get() - g_ble_ll_last_num_comp_pkt_evt) <
-                                            MYNEWT_VAL(BLE_NUM_COMP_PKT_RATE)) {
-        /*
-         * If this connection has completed packets, send an event right away.
-         * We do this to increase throughput but we dont want to search the
-         * entire active list every time.
-         */
-        if (connsm->completed_pkts) {
-            evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-            if (evbuf) {
-                evbuf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
-                evbuf[1] = (2 * sizeof(uint16_t)) + 1;
-                evbuf[2] = 1;
-                put_le16(evbuf + 3, connsm->conn_handle);
-                put_le16(evbuf + 5, connsm->completed_pkts);
-                ble_ll_hci_event_send(evbuf);
-                connsm->completed_pkts = 0;
-            }
-        }
-        return;
-    }
-
-    /* Iterate through all the active, created connections */
-skip_conn:
-    evbuf = NULL;
-    handles = 0;
-    handle_ptr = NULL;
-    event_sent = 0;
-    SLIST_FOREACH(connsm, &g_ble_ll_conn_active_list, act_sle) {
-        /*
-         * Only look at connections that we have sent a connection complete
-         * event and that either has packets enqueued or has completed packets.
-         */
-        if ((connsm->conn_state != BLE_LL_CONN_STATE_IDLE) &&
-            (connsm->completed_pkts || !STAILQ_EMPTY(&connsm->conn_txq))) {
-            /* If no buffer, get one, If cant get one, leave. */
-            if (!evbuf) {
-                evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-                if (!evbuf) {
-                    break;
-                }
-                handles = 0;
-                handle_ptr = evbuf + 3;
-            }
-
-            /* Add handle and complete packets */
-            put_le16(handle_ptr, connsm->conn_handle);
-            put_le16(handle_ptr + 2, connsm->completed_pkts);
-            connsm->completed_pkts = 0;
-            handle_ptr += (2 * sizeof(uint16_t));
-            ++handles;
-
-            /* Send now if the buffer is full. */
-            if (handles == max_handles) {
-                evbuf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
-                evbuf[1] = (handles * 2 * sizeof(uint16_t)) + 1;
-                evbuf[2] = handles;
-                ble_ll_hci_event_send(evbuf);
-                evbuf = NULL;
-                handles = 0;
-                event_sent = 1;
-            }
-        }
-    }
-
-    /* Send event if there is an event to send */
-    if (evbuf) {
-        evbuf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
-        evbuf[1] = (handles * 2 * sizeof(uint16_t)) + 1;
-        evbuf[2] = handles;
-        ble_ll_hci_event_send(evbuf);
-        event_sent = 1;
-    }
-
-    if (event_sent) {
-        g_ble_ll_last_num_comp_pkt_evt = os_time_get();
-    }
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-/**
- * Send a authenticated payload timeout event
- *
- * NOTE: we currently only send this event when we have a reason to send it;
- * not when it fails.
- *
- * @param reason The BLE error code to send as a disconnect reason
- */
-void
-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 = 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);
-            put_le16(evbuf + 2, connsm->conn_handle);
-            ble_ll_hci_event_send(evbuf);
-        }
-    }
-}
-#endif
-
-/**
- * Send a disconnection complete event.
- *
- * NOTE: we currently only send this event when we have a reason to send it;
- * not when it fails.
- *
- * @param reason The BLE error code to send as a disconnect reason
- */
-void
-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 = 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;
-            evbuf[2] = BLE_ERR_SUCCESS;
-            put_le16(evbuf + 3, connsm->conn_handle);
-            evbuf[5] = reason;
-            ble_ll_hci_event_send(evbuf);
-        }
-    }
-}
-
-static int
-ble_ll_conn_hci_chk_scan_params(uint16_t itvl, uint16_t window)
-{
-    /* Check interval and window */
-    if ((itvl < BLE_HCI_SCAN_ITVL_MIN) ||
-        (itvl > BLE_HCI_SCAN_ITVL_MAX) ||
-        (window < BLE_HCI_SCAN_WINDOW_MIN) ||
-        (window > BLE_HCI_SCAN_WINDOW_MAX) ||
-        (itvl < window)) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    return 0;
-}
-
-/**
- * Process the HCI command to create a connection.
- *
- * Context: Link Layer task (HCI command processing)
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_create(uint8_t *cmdbuf)
-{
-    int rc;
-    struct hci_create_conn ccdata;
-    struct hci_create_conn *hcc;
-    struct ble_ll_conn_sm *connsm;
-
-    /* If we are already creating a connection we should leave */
-    if (g_ble_ll_conn_create_sm) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* If already enabled, we return an error */
-    if (ble_ll_scan_enabled()) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* Retrieve command data */
-    hcc = &ccdata;
-    hcc->scan_itvl = get_le16(cmdbuf);
-    hcc->scan_window = get_le16(cmdbuf + 2);
-
-    rc = ble_ll_conn_hci_chk_scan_params(hcc->scan_itvl, hcc->scan_window);
-    if (rc) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check filter policy */
-    hcc->filter_policy = cmdbuf[4];
-    if (hcc->filter_policy > BLE_HCI_INITIATOR_FILT_POLICY_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Get peer address type and address only if no whitelist used */
-    if (hcc->filter_policy == 0) {
-        hcc->peer_addr_type = cmdbuf[5];
-        if (hcc->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_MAX) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        memcpy(&hcc->peer_addr, cmdbuf + 6, BLE_DEV_ADDR_LEN);
-    }
-
-    /* Get own address type (used in connection request) */
-    hcc->own_addr_type = cmdbuf[12];
-    if (hcc->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check connection interval, latency and supervision timeoout */
-    hcc->conn_itvl_min = get_le16(cmdbuf + 13);
-    hcc->conn_itvl_max = get_le16(cmdbuf + 15);
-    hcc->conn_latency = get_le16(cmdbuf + 17);
-    hcc->supervision_timeout = get_le16(cmdbuf + 19);
-    rc = ble_ll_conn_hci_chk_conn_params(hcc->conn_itvl_min,
-                                         hcc->conn_itvl_max,
-                                         hcc->conn_latency,
-                                         hcc->supervision_timeout);
-    if (rc) {
-        return rc;
-    }
-
-    /* Min/max connection event lengths */
-    hcc->min_ce_len = get_le16(cmdbuf + 21);
-    hcc->max_ce_len = get_le16(cmdbuf + 23);
-    if (hcc->min_ce_len > hcc->max_ce_len) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Make sure we can allocate an event to send the connection complete */
-    if (ble_ll_init_alloc_conn_comp_ev()) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-    /* Make sure we can accept a connection! */
-    connsm = ble_ll_conn_sm_get();
-    if (connsm == NULL) {
-        return BLE_ERR_CONN_LIMIT;
-    }
-
-    /* Initialize state machine in master role and start state machine */
-    ble_ll_conn_master_init(connsm, hcc);
-    ble_ll_conn_sm_new(connsm);
-    /* CSA will be selected when advertising is received */
-
-    /* Start scanning */
-    rc = ble_ll_scan_initiator_start(hcc, &connsm->scansm);
-    if (rc) {
-        SLIST_REMOVE(&g_ble_ll_conn_active_list,connsm,ble_ll_conn_sm,act_sle);
-        STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-    } else {
-        /* Set the connection state machine we are trying to create. */
-        g_ble_ll_conn_create_sm = connsm;
-    }
-
-    return rc;
-}
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-static void
-ble_ll_conn_init_hcc_params(struct hci_ext_create_conn *hcc,
-                            int valid_param_idx)
-{
-    struct hci_ext_conn_params *hcc_params = &hcc->params[valid_param_idx];
-
-    if (valid_param_idx != 0 && !(hcc->init_phy_mask & BLE_PHY_MASK_1M)) {
-        hcc->params[0] = *hcc_params;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
-    if (valid_param_idx != 1 && !(hcc->init_phy_mask & BLE_PHY_MASK_2M)) {
-        hcc->params[1] = *hcc_params;
-    }
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    if (valid_param_idx != 2 && !(hcc->init_phy_mask & BLE_PHY_MASK_CODED)) {
-        hcc->params[2] = *hcc_params;
-    }
-#endif
-}
-
-int
-ble_ll_ext_conn_create(uint8_t *cmdbuf)
-{
-    int rc;
-    struct hci_ext_create_conn ccdata;
-    struct hci_ext_create_conn *hcc;
-    struct hci_ext_conn_params *hcc_params;
-    struct ble_ll_conn_sm *connsm;
-    int valid_param_idx = -1;
-    int iter;
-
-    /* If we are already creating a connection we should leave */
-    if (g_ble_ll_conn_create_sm) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* If already enabled, we return an error */
-    if (ble_ll_scan_enabled()) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* Retrieve command data */
-    hcc = &ccdata;
-    memset (hcc, 0, sizeof(*hcc));
-
-    /* Check filter policy */
-    hcc->filter_policy = cmdbuf[0];
-    if (hcc->filter_policy > BLE_HCI_INITIATOR_FILT_POLICY_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Get own address type (used in connection request) */
-    hcc->own_addr_type = cmdbuf[1];
-    if (hcc->own_addr_type > BLE_HCI_ADV_OWN_ADDR_MAX) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Get peer address type and address only if no whitelist used */
-    if (hcc->filter_policy == 0) {
-        hcc->peer_addr_type = cmdbuf[2];
-        if (hcc->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_MAX) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        memcpy(&hcc->peer_addr, cmdbuf + 3, BLE_DEV_ADDR_LEN);
-    }
-
-    hcc->init_phy_mask = cmdbuf[9];
-    if (hcc->init_phy_mask & ~ble_ll_valid_conn_phy_mask) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    if (!(hcc->init_phy_mask & ble_ll_conn_required_phy_mask)) {
-        /* At least one of those need to be set */
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    iter = 10;
-    if (hcc->init_phy_mask & BLE_PHY_MASK_1M) {
-        hcc_params = &hcc->params[0];
-        hcc_params->scan_itvl = get_le16(cmdbuf + iter);
-        hcc_params->scan_window = get_le16(cmdbuf + iter + 2);
-        iter += 4;
-
-        rc = ble_ll_conn_hci_chk_scan_params(hcc_params->scan_itvl,
-                                             hcc_params->scan_window);
-        if (rc) {
-            return rc;
-        }
-
-        hcc_params->conn_itvl_min = get_le16(cmdbuf + iter);
-        hcc_params->conn_itvl_max = get_le16(cmdbuf + iter + 2);
-        hcc_params->conn_latency = get_le16(cmdbuf + iter + 4);
-        hcc_params->supervision_timeout = get_le16(cmdbuf + iter + 6);
-        rc = ble_ll_conn_hci_chk_conn_params(hcc_params->conn_itvl_min,
-                                             hcc_params->conn_itvl_max,
-                                             hcc_params->conn_latency,
-                                             hcc_params->supervision_timeout);
-        if (rc) {
-            return rc;
-        }
-        iter += 8;
-
-        /* Min/max connection event lengths */
-        hcc_params->min_ce_len = get_le16(cmdbuf + iter);
-        hcc_params->max_ce_len = get_le16(cmdbuf + iter + 2);
-        if (hcc_params->min_ce_len > hcc_params->max_ce_len) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        iter += 4;
-        valid_param_idx = 0;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
-    if (hcc->init_phy_mask & BLE_PHY_MASK_2M) {
-        /* Move to connection parameters */
-        hcc_params = &hcc->params[1];
-        iter += 4;
-
-        hcc_params->conn_itvl_min = get_le16(cmdbuf + iter);
-        hcc_params->conn_itvl_max = get_le16(cmdbuf + iter + 2);
-        hcc_params->conn_latency = get_le16(cmdbuf + iter + 4);
-        hcc_params->supervision_timeout = get_le16(cmdbuf + iter + 6);
-        rc = ble_ll_conn_hci_chk_conn_params(hcc_params->conn_itvl_min,
-                                             hcc_params->conn_itvl_max,
-                                             hcc_params->conn_latency,
-                                             hcc_params->supervision_timeout);
-        if (rc) {
-            return rc;
-        }
-        iter += 8;
-
-        /* Min/max connection event lengths */
-        hcc_params->min_ce_len = get_le16(cmdbuf + iter);
-        hcc_params->max_ce_len = get_le16(cmdbuf + iter + 2);
-        if (hcc_params->min_ce_len > hcc_params->max_ce_len) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        iter += 4;
-    }
-#endif
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    if (hcc->init_phy_mask & BLE_PHY_MASK_CODED) {
-        hcc_params = &hcc->params[2];
-        hcc_params->scan_itvl = get_le16(cmdbuf + iter);
-        hcc_params->scan_window = get_le16(cmdbuf + iter + 2);
-        iter += 4;
-
-        rc = ble_ll_conn_hci_chk_scan_params(hcc_params->scan_itvl,
-                                             hcc_params->scan_window);
-        if (rc) {
-            return rc;
-        }
-
-        hcc_params->conn_itvl_min = get_le16(cmdbuf + iter);
-        hcc_params->conn_itvl_max = get_le16(cmdbuf + iter + 2);
-        hcc_params->conn_latency = get_le16(cmdbuf + iter + 4);
-        hcc_params->supervision_timeout = get_le16(cmdbuf + iter + 6);
-        rc = ble_ll_conn_hci_chk_conn_params(hcc_params->conn_itvl_min,
-                                             hcc_params->conn_itvl_max,
-                                             hcc_params->conn_latency,
-                                             hcc_params->supervision_timeout);
-        if (rc) {
-            return rc;
-        }
-        iter += 8;
-
-        /* Min/max connection event lengths */
-        hcc_params->min_ce_len = get_le16(cmdbuf + iter);
-        hcc_params->max_ce_len = get_le16(cmdbuf + iter + 2);
-        if (hcc_params->min_ce_len > hcc_params->max_ce_len) {
-            return BLE_ERR_INV_HCI_CMD_PARMS;
-        }
-
-        iter += 4;
-        if (valid_param_idx < 0) {
-            valid_param_idx = 2;
-        }
-    }
-#endif
-
-    /* Make sure we can allocate an event to send the connection complete */
-    if (ble_ll_init_alloc_conn_comp_ev()) {
-        return BLE_ERR_MEM_CAPACITY;
-    }
-
-    /* Make sure we can accept a connection! */
-    connsm = ble_ll_conn_sm_get();
-    if (connsm == NULL) {
-        return BLE_ERR_CONN_LIMIT;
-    }
-
-    ble_ll_conn_init_hcc_params(hcc, valid_param_idx);
-
-    /* Initialize state machine in master role and start state machine */
-    ble_ll_conn_ext_master_init(connsm, hcc);
-    ble_ll_conn_sm_new(connsm);
-
-    /* CSA will be selected when advertising is received */
-
-    /* Start scanning */
-    rc = ble_ll_scan_ext_initiator_start(hcc, &connsm->scansm);
-    if (rc) {
-        SLIST_REMOVE(&g_ble_ll_conn_active_list,connsm,ble_ll_conn_sm,act_sle);
-        STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-    } else {
-        /* Set the connection state machine we are trying to create. */
-        g_ble_ll_conn_create_sm = connsm;
-    }
-
-    return rc;
-}
-#endif
-
-static int
-ble_ll_conn_process_conn_params(uint8_t *cmdbuf, struct ble_ll_conn_sm *connsm)
-{
-    int rc;
-    struct hci_conn_update *hcu;
-
-    /* Retrieve command data */
-    hcu = &connsm->conn_param_req;
-    hcu->handle = connsm->conn_handle;
-    hcu->conn_itvl_min = get_le16(cmdbuf + 2);
-    hcu->conn_itvl_max = get_le16(cmdbuf + 4);
-    hcu->conn_latency = get_le16(cmdbuf + 6);
-    hcu->supervision_timeout = get_le16(cmdbuf + 8);
-    hcu->min_ce_len = get_le16(cmdbuf + 10);
-    hcu->max_ce_len = get_le16(cmdbuf + 12);
-
-    /* Check that parameter values are in range */
-    rc = ble_ll_conn_hci_chk_conn_params(hcu->conn_itvl_min,
-                                         hcu->conn_itvl_max,
-                                         hcu->conn_latency,
-                                         hcu->supervision_timeout);
-
-    /* Check valid min/max ce length */
-    if (rc || (hcu->min_ce_len > hcu->max_ce_len)) {
-        hcu->handle = 0;
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-    return rc;
-}
-
-/**
- * Called when the host issues the read remote features command
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_read_rem_features(uint8_t *cmdbuf)
-{
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    /* If no connection handle exit with error */
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        return BLE_ERR_UNK_CONN_ID;
-    }
-
-    /* If already pending exit with error */
-    if (connsm->csmflags.cfbit.pending_hci_rd_features) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /*
-     * Start control procedure if we did not receive peer's features and did not
-     * start procedure already.
-     */
-    if (!connsm->csmflags.cfbit.rxd_features &&
-                !IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_FEATURE_XCHG)) {
-        if ((connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) &&
-            !(ble_ll_read_supp_features() & BLE_LL_FEAT_SLAVE_INIT)) {
-                return BLE_ERR_CMD_DISALLOWED;
-        }
-
-        ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_FEATURE_XCHG);
-    }
-
-    connsm->csmflags.cfbit.pending_hci_rd_features = 1;
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Called to process a connection update command.
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_update(uint8_t *cmdbuf)
-{
-    int rc;
-    uint8_t ctrl_proc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-    struct hci_conn_update *hcu;
-
-    /*
-     * XXX: must deal with slave not supporting this feature and using
-     * conn update! Right now, we only check if WE support the connection
-     * parameters request procedure. We dont check if the remote does.
-     * We should also be able to deal with sending the parameter request,
-     * getting an UNKOWN_RSP ctrl pdu and resorting to use normal
-     * connection update procedure.
-     */
-
-    /* If no connection handle exit with error */
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        return BLE_ERR_UNK_CONN_ID;
-    }
-
-    /* Better not have this procedure ongoing! */
-    if (IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_CONN_PARAM_REQ) ||
-        IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_CONN_UPDATE)) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /* See if this feature is supported on both sides */
-    if ((connsm->conn_features & BLE_LL_FEAT_CONN_PARM_REQ) == 0) {
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            return BLE_ERR_CMD_DISALLOWED;
-        }
-        ctrl_proc = BLE_LL_CTRL_PROC_CONN_UPDATE;
-    } else {
-        ctrl_proc = BLE_LL_CTRL_PROC_CONN_PARAM_REQ;
-    }
-
-    /*
-     * If we are a slave and the master has initiated the procedure already
-     * we should deny the slave request for now. If we are a master and the
-     * slave has initiated the procedure, we need to send a reject to the
-     * slave.
-     */
-    if (connsm->csmflags.cfbit.awaiting_host_reply) {
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            return BLE_ERR_LMP_COLLISION;
-        } else {
-            connsm->csmflags.cfbit.awaiting_host_reply = 0;
-
-            /* XXX: If this fails no reject ind will be sent! */
-            ble_ll_ctrl_reject_ind_send(connsm, connsm->host_reply_opcode,
-                                        BLE_ERR_LMP_COLLISION);
-        }
-    }
-
-    /*
-     * If we are a slave and the master has initiated the channel map
-     * update procedure we should deny the slave request for now.
-     */
-    if (connsm->csmflags.cfbit.chanmap_update_scheduled) {
-        if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-            return BLE_ERR_DIFF_TRANS_COLL;
-        }
-    }
-
-    /* Retrieve command data */
-    hcu = &connsm->conn_param_req;
-    hcu->handle = handle;
-    hcu->conn_itvl_min = get_le16(cmdbuf + 2);
-    hcu->conn_itvl_max = get_le16(cmdbuf + 4);
-    hcu->conn_latency = get_le16(cmdbuf + 6);
-    hcu->supervision_timeout = get_le16(cmdbuf + 8);
-    hcu->min_ce_len = get_le16(cmdbuf + 10);
-    hcu->max_ce_len = get_le16(cmdbuf + 12);
-    if (hcu->min_ce_len > hcu->max_ce_len) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check that parameter values are in range */
-    rc = ble_ll_conn_hci_chk_conn_params(hcu->conn_itvl_min,
-                                         hcu->conn_itvl_max,
-                                         hcu->conn_latency,
-                                         hcu->supervision_timeout);
-    if (!rc) {
-        /* Start the control procedure */
-        ble_ll_ctrl_proc_start(connsm, ctrl_proc);
-    }
-
-    return rc;
-}
-
-int
-ble_ll_conn_hci_param_reply(uint8_t *cmdbuf, int positive_reply)
-{
-    int rc;
-    uint8_t ble_err;
-    uint8_t *dptr;
-    uint8_t rsp_opcode;
-    uint8_t len;
-    uint16_t handle;
-    struct os_mbuf *om;
-    struct ble_ll_conn_sm *connsm;
-
-    /* See if we support this feature */
-    if ((ble_ll_read_supp_features() & BLE_LL_FEAT_CONN_PARM_REQ) == 0) {
-        return BLE_ERR_UNKNOWN_HCI_CMD;
-    }
-
-    /* If no connection handle exit with error */
-    handle = get_le16(cmdbuf);
-
-    /* If we dont have a handle we cant do anything */
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        return BLE_ERR_UNK_CONN_ID;
-    }
-
-    /* Make sure connection parameters are valid if this is a positive reply */
-    rc = BLE_ERR_SUCCESS;
-    ble_err = cmdbuf[2];
-    if (positive_reply) {
-        rc = ble_ll_conn_process_conn_params(cmdbuf, connsm);
-        if (rc) {
-            ble_err = BLE_ERR_CONN_PARMS;
-        }
-    }
-
-    /* The connection should be awaiting a reply. If not, just discard */
-    if (connsm->csmflags.cfbit.awaiting_host_reply) {
-        /* Get a control packet buffer */
-        if (positive_reply && (rc == BLE_ERR_SUCCESS)) {
-            om = os_msys_get_pkthdr(BLE_LL_CTRL_MAX_PAYLOAD + 1,
-                                    sizeof(struct ble_mbuf_hdr));
-            if (om) {
-                dptr = om->om_data;
-                rsp_opcode = ble_ll_ctrl_conn_param_reply(connsm, dptr,
-                                                          &connsm->conn_cp);
-                dptr[0] = rsp_opcode;
-                len = g_ble_ll_ctrl_pkt_lengths[rsp_opcode] + 1;
-                ble_ll_conn_enqueue_pkt(connsm, om, BLE_LL_LLID_CTRL, len);
-            }
-        } else {
-            /* XXX: check return code and deal */
-            ble_ll_ctrl_reject_ind_send(connsm, connsm->host_reply_opcode,
-                                        ble_err);
-        }
-        connsm->csmflags.cfbit.awaiting_host_reply = 0;
-
-        /* XXX: if we cant get a buffer, what do we do? We need to remember
-         * reason if it was a negative reply. We also would need to have
-           some state to tell us this happened */
-    }
-
-    return rc;
-}
-
-/**
- * Called when HCI command to cancel a create connection command has been
- * received.
- *
- * Context: Link Layer (HCI command parser)
- *
- * @return int
- */
-int
-ble_ll_conn_create_cancel(void)
-{
-    int rc;
-    struct ble_ll_conn_sm *connsm;
-
-    /* XXX: BUG! I send the event before the command complete. Not good. */
-    /*
-     * If we receive this command and we have not got a connection
-     * create command, we have to return disallowed. The spec does not say
-     * what happens if the connection has already been established. We
-     * return disallowed as well
-     */
-    connsm = g_ble_ll_conn_create_sm;
-    if (connsm && (connsm->conn_state == BLE_LL_CONN_STATE_IDLE)) {
-        /* stop scanning and end the connection event */
-        g_ble_ll_conn_create_sm = NULL;
-        ble_ll_scan_sm_stop(1);
-        ble_ll_conn_end(connsm, BLE_ERR_UNK_CONN_ID);
-        rc = BLE_ERR_SUCCESS;
-    } else {
-        /* If we are not attempting to create a connection*/
-        rc = BLE_ERR_CMD_DISALLOWED;
-    }
-
-    return rc;
-}
-
-/**
- * Called to process a HCI disconnect command
- *
- * Context: Link Layer task (HCI command parser).
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_disconnect_cmd(uint8_t *cmdbuf)
-{
-    int rc;
-    uint8_t reason;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Check for valid parameters */
-    handle = get_le16(cmdbuf);
-    reason = cmdbuf[2];
-
-    rc = BLE_ERR_INV_HCI_CMD_PARMS;
-    if (handle <= BLE_LL_CONN_MAX_CONN_HANDLE) {
-        /* Make sure reason is valid */
-        switch (reason) {
-        case BLE_ERR_AUTH_FAIL:
-        case BLE_ERR_REM_USER_CONN_TERM:
-        case BLE_ERR_RD_CONN_TERM_RESRCS:
-        case BLE_ERR_RD_CONN_TERM_PWROFF:
-        case BLE_ERR_UNSUPP_REM_FEATURE:
-        case BLE_ERR_UNIT_KEY_PAIRING:
-        case BLE_ERR_CONN_PARMS:
-            connsm = ble_ll_conn_find_active_conn(handle);
-            if (connsm) {
-                /* Do not allow command if we are in process of disconnecting */
-                if (connsm->disconnect_reason) {
-                    rc = BLE_ERR_CMD_DISALLOWED;
-                } else {
-                    /* This control procedure better not be pending! */
-                    assert(CONN_F_TERMINATE_STARTED(connsm) == 0);
-
-                    /* Record the disconnect reason */
-                    connsm->disconnect_reason = reason;
-
-                    /* Start this control procedure */
-                    ble_ll_ctrl_terminate_start(connsm);
-
-                    rc = BLE_ERR_SUCCESS;
-                }
-            } else {
-                rc = BLE_ERR_UNK_CONN_ID;
-            }
-            break;
-        default:
-            break;
-        }
-    }
-
-    return rc;
-}
-
-/**
- * Called to process a HCI disconnect command
- *
- * Context: Link Layer task (HCI command parser).
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_rd_rem_ver_cmd(uint8_t *cmdbuf)
-{
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Check for valid parameters */
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        return BLE_ERR_UNK_CONN_ID;
-    }
-
-    /* Return error if in progress */
-    if (IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_VERSION_XCHG)) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    /*
-     * Start this control procedure. If we have already done this control
-     * procedure we set the pending bit so that the host gets an event because
-     * it is obviously expecting one (or would not have sent the command).
-     * NOTE: we cant just send the event here. That would cause the event to
-     * be queued before the command status.
-     */
-    if (!connsm->csmflags.cfbit.version_ind_sent) {
-        ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_VERSION_XCHG);
-    } else {
-        connsm->pending_ctrl_procs |= (1 << BLE_LL_CTRL_PROC_VERSION_XCHG);
-    }
-
-    return BLE_ERR_SUCCESS;
-}
-
-/**
- * Called to read the RSSI for a given connection handle
- *
- * @param cmdbuf
- * @param rspbuf
- * @param rsplen
- *
- * @return int
- */
-int
-ble_ll_conn_hci_rd_rssi(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
-{
-    int rc;
-    int8_t rssi;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rssi = 127;
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else {
-        rssi = connsm->conn_rssi;
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    put_le16(rspbuf, handle);
-    rspbuf[2] = (uint8_t)rssi;
-    *rsplen = 3;
-
-    /* Place the RSSI of the connection into the response buffer */
-    return rc;
-}
-
-/**
- * Called to read the current channel map of a connection
- *
- * @param cmdbuf
- * @param rspbuf
- * @param rsplen
- *
- * @return int
- */
-int
-ble_ll_conn_hci_rd_chan_map(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
-{
-    int rc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else {
-        if (connsm->csmflags.cfbit.chanmap_update_scheduled) {
-            memcpy(rspbuf + 2, &connsm->req_chanmap[0], BLE_LL_CONN_CHMAP_LEN);
-        } else {
-            memcpy(rspbuf + 2, &connsm->chanmap[0], BLE_LL_CONN_CHMAP_LEN);
-        }
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    put_le16(rspbuf, handle);
-    *rsplen = sizeof(uint16_t) + BLE_LL_CONN_CHMAP_LEN;
-    return rc;
-}
-
-/**
- * Called when the host issues the LE command "set host channel classification"
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_set_chan_class(uint8_t *cmdbuf)
-{
-    int rc;
-    uint8_t num_used_chans;
-
-    /*
-     * The HCI command states that the host is allowed to mask in just one
-     * channel but the Link Layer needs minimum two channels to operate. So
-     * I will not allow this command if there are less than 2 channels masked.
-     */
-    rc = BLE_ERR_SUCCESS;
-    num_used_chans = ble_ll_conn_calc_used_chans(cmdbuf);
-    if ((num_used_chans < 2) || ((cmdbuf[4] & 0xe0) != 0)) {
-        rc = BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Set the host channel mask */
-    ble_ll_conn_set_global_chanmap(num_used_chans, cmdbuf);
-    return rc;
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
-int
-ble_ll_conn_hci_set_data_len(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
-{
-    int rc;
-    uint16_t handle;
-    uint16_t txoctets;
-    uint16_t txtime;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Find connection */
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else {
-        txoctets = get_le16(cmdbuf + 2);
-        txtime = get_le16(cmdbuf + 4);
-
-        /* Make sure it is valid */
-        if (!ble_ll_chk_txrx_octets(txoctets) ||
-            !ble_ll_chk_txrx_time(txtime)) {
-            rc = BLE_ERR_INV_HCI_CMD_PARMS;
-        } else {
-            rc = BLE_ERR_SUCCESS;
-        }
-
-        /* XXX: should I check against max supported? I think so */
-
-        /*
-         * XXX: For now; we will simply ignore what the host asks as we are
-         * allowed to do so by the spec. If we implement this and something
-         * changes we need to send data length change event.
-         */
-    }
-
-    put_le16(rspbuf, handle);
-    *rsplen = sizeof(uint16_t);
-    return rc;
-}
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-/**
- * LE start encrypt command
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_le_start_encrypt(uint8_t *cmdbuf)
-{
-    int rc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-        rc = BLE_ERR_UNSPECIFIED;
-    } else if (connsm->cur_ctrl_proc == BLE_LL_CTRL_PROC_ENCRYPT) {
-        /*
-         * The specification does not say what to do here but the host should
-         * not be telling us to start encryption while we are in the process
-         * of honoring a previous start encrypt.
-         */
-        rc = BLE_ERR_CMD_DISALLOWED;
-    } else {
-        /* Start the control procedure */
-        connsm->enc_data.host_rand_num = get_le64(cmdbuf + 2);
-        connsm->enc_data.enc_div = get_le16(cmdbuf + 10);
-        swap_buf(connsm->enc_data.enc_block.key, cmdbuf + 12, 16);
-        ble_ll_ctrl_proc_start(connsm, BLE_LL_CTRL_PROC_ENCRYPT);
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    return rc;
-}
-
-/**
- * Called to process the LE long term key reply.
- *
- * Context: Link Layer Task.
- *
- * @param cmdbuf
- * @param rspbuf
- * @param ocf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_le_ltk_reply(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t ocf)
-{
-    int rc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    /* Find connection handle */
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-        goto ltk_key_cmd_complete;
-    }
-
-    /* Should never get this if we are a master! */
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-        rc = BLE_ERR_UNSPECIFIED;
-        goto ltk_key_cmd_complete;
-    }
-
-    /* The connection should be awaiting a reply. If not, just discard */
-    if (connsm->enc_data.enc_state == CONN_ENC_S_LTK_REQ_WAIT) {
-        if (ocf == BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY) {
-            swap_buf(connsm->enc_data.enc_block.key, cmdbuf + 2, 16);
-            ble_ll_calc_session_key(connsm);
-            ble_ll_ctrl_start_enc_send(connsm, BLE_LL_CTRL_START_ENC_REQ);
-        } else {
-            /* We received a negative reply! Send REJECT_IND */
-            ble_ll_ctrl_reject_ind_send(connsm, BLE_LL_CTRL_ENC_REQ,
-                                        BLE_ERR_PINKEY_MISSING);
-            connsm->enc_data.enc_state = CONN_ENC_S_LTK_NEG_REPLY;
-        }
-    }
-    rc = BLE_ERR_SUCCESS;
-
-ltk_key_cmd_complete:
-    put_le16(rspbuf, handle);
-    return rc;
-}
-#endif
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-/**
- * Read authenticated payload timeout (OGF=3, OCF==0x007B)
- *
- * @param cmdbuf
- * @param rsplen
- *
- * @return int
- */
-int
-ble_ll_conn_hci_rd_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp, uint8_t *rsplen)
-{
-    int rc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else {
-        put_le16(rsp + 2, connsm->auth_pyld_tmo);
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    put_le16(rsp, handle);
-    *rsplen = BLE_HCI_RD_AUTH_PYLD_TMO_LEN;
-    return rc;
-}
-
-/**
- * Write authenticated payload timeout (OGF=3, OCF=00x7C)
- *
- * @param cmdbuf
- * @param rsplen
- *
- * @return int
- */
-int
-ble_ll_conn_hci_wr_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp, uint8_t *rsplen)
-{
-    int rc;
-    uint16_t handle;
-    uint16_t tmo;
-    uint32_t min_tmo;
-    struct ble_ll_conn_sm *connsm;
-
-    rc = BLE_ERR_SUCCESS;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-        goto wr_auth_exit;
-    }
-
-    /*
-     * The timeout is in units of 10 msecs. We need to make sure that the
-     * timeout is greater than or equal to connItvl * (1 + slaveLatency)
-     */
-    tmo = get_le16(cmdbuf + 2);
-    min_tmo = (uint32_t)connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
-    min_tmo *= (connsm->slave_latency + 1);
-    min_tmo /= 10000;
-
-    if (tmo < min_tmo) {
-        rc = BLE_ERR_INV_HCI_CMD_PARMS;
-    } else {
-        connsm->auth_pyld_tmo = tmo;
-        if (os_callout_queued(&connsm->auth_pyld_timer)) {
-            ble_ll_conn_auth_pyld_timer_start(connsm);
-        }
-    }
-
-wr_auth_exit:
-    put_le16(rsp, handle);
-    *rsplen = BLE_HCI_WR_AUTH_PYLD_TMO_LEN;
-    return rc;
-}
-#endif
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-/**
- * Read current phy for connection (OGF=8, OCF==0x0030)
- *
- * @param cmdbuf
- * @param rsplen
- *
- * @return int
- */
-int
-ble_ll_conn_hci_le_rd_phy(uint8_t *cmdbuf, uint8_t *rsp, uint8_t *rsplen)
-{
-    int rc;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        rc = BLE_ERR_UNK_CONN_ID;
-    } else {
-        rsp[2] = connsm->phy_data.cur_tx_phy;
-        rsp[3] = connsm->phy_data.cur_rx_phy;
-        rc = BLE_ERR_SUCCESS;
-    }
-
-    put_le16(rsp, handle);
-    *rsplen = BLE_HCI_LE_RD_PHY_RSPLEN;
-    return rc;
-}
-
-/**
- * Set PHY preferences for connection
- *
- * @param cmdbuf
- *
- * @return int
- */
-int
-ble_ll_conn_hci_le_set_phy(uint8_t *cmdbuf)
-{
-    int rc;
-    uint16_t phy_options;
-    uint8_t tx_phys;
-    uint8_t rx_phys;
-    uint16_t handle;
-    struct ble_ll_conn_sm *connsm;
-
-    handle = get_le16(cmdbuf);
-    connsm = ble_ll_conn_find_active_conn(handle);
-    if (!connsm) {
-        return BLE_ERR_UNK_CONN_ID;
-    }
-
-    /*
-     * If host has requested a PHY update and we are not finished do
-     * not allow another one
-     */
-    if (CONN_F_HOST_PHY_UPDATE(connsm)) {
-        return BLE_ERR_CMD_DISALLOWED;
-    }
-
-    phy_options = get_le16(cmdbuf + 5);
-    if (phy_options > BLE_HCI_LE_PHY_CODED_S8_PREF) {
-        return BLE_ERR_INV_HCI_CMD_PARMS;
-    }
-
-    /* Check valid parameters */
-    rc = ble_ll_hci_chk_phy_masks(cmdbuf + 2, &tx_phys, &rx_phys);
-    if (rc) {
-        goto phy_cmd_param_err;
-    }
-
-    connsm->phy_data.phy_options = phy_options & 0x03;
-    connsm->phy_data.host_pref_tx_phys_mask = tx_phys,
-    connsm->phy_data.host_pref_rx_phys_mask = rx_phys;
-
-    /*
-     * The host preferences override the default phy preferences. Currently,
-     * the only reason the controller will initiate a procedure on its own
-     * is due to the fact that the host set default preferences. So if there
-     * is a pending control procedure and it has not yet started, we do not
-     * need to perform the default controller procedure.
-     */
-    if (IS_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_PHY_UPDATE)) {
-        if (connsm->cur_ctrl_proc != BLE_LL_CTRL_PROC_PHY_UPDATE) {
-            CONN_F_CTRLR_PHY_UPDATE(connsm) = 0;
-        }
-        CONN_F_HOST_PHY_UPDATE(connsm) = 1;
-    } else {
-        /*
-         * We could be doing a peer-initiated PHY update procedure. If this
-         * is the case the requested phy preferences will not both be 0. If
-         * we are not done with a peer-initiated procedure we just set the
-         * pending bit but do not start the control procedure.
-         */
-        if (CONN_F_PEER_PHY_UPDATE(connsm)) {
-            connsm->pending_ctrl_procs |= BLE_LL_CTRL_PROC_PHY_UPDATE;
-            CONN_F_HOST_PHY_UPDATE(connsm) = 1;
-        } else {
-            /* Check if we should start phy update procedure */
-            if (!ble_ll_conn_chk_phy_upd_start(connsm)) {
-                CONN_F_HOST_PHY_UPDATE(connsm) = 1;
-            } else {
-                /*
-                 * Set flag to send a PHY update complete event. We set flag
-                 * even if we do not do an update procedure since we have to
-                 * inform the host even if we decide not to change anything.
-                 */
-                CONN_F_PHY_UPDATE_EVENT(connsm) = 1;
-            }
-        }
-    }
-
-phy_cmd_param_err:
-    return rc;
-}
-#endif
diff --git a/net/nimble/controller/src/ble_ll_conn_priv.h b/net/nimble/controller/src/ble_ll_conn_priv.h
deleted file mode 100644
index 02e99ed8a..000000000
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ /dev/null
@@ -1,190 +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_LL_CONN_PRIV_
-#define H_BLE_LL_CONN_PRIV_
-
-#include "controller/ble_ll_conn.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Definitions for min/max RX/TX time/bytes values allowed for connections.
- * Source: Core 5.0 specification, Vol 6, Part B, section 4.5.10
- */
-#define BLE_LL_CONN_SUPP_TIME_MIN           (328)   /* usecs */
-#define BLE_LL_CONN_SUPP_TIME_MAX           (17040) /* usecs */
-#define BLE_LL_CONN_SUPP_TIME_MIN_UNCODED   (328)   /* usecs */
-#define BLE_LL_CONN_SUPP_TIME_MAX_UNCODED   (2120)  /* usecs */
-#define BLE_LL_CONN_SUPP_TIME_MIN_CODED     (2704)  /* usecs */
-#define BLE_LL_CONN_SUPP_TIME_MAX_CODED     (17040) /* usecs */
-#define BLE_LL_CONN_SUPP_BYTES_MIN          (27)    /* bytes */
-#define BLE_LL_CONN_SUPP_BYTES_MAX          (251)   /* bytes */
-
-/* Connection event timing */
-#define BLE_LL_CONN_INITIAL_OFFSET          (1250)
-#define BLE_LL_CONN_ITVL_USECS              (1250)
-#define BLE_LL_CONN_TX_WIN_USECS            (1250)
-#define BLE_LL_CONN_TX_OFF_USECS            (1250)
-#define BLE_LL_CONN_CE_USECS                (625)
-#define BLE_LL_CONN_TX_WIN_MIN              (1)         /* in tx win units */
-#define BLE_LL_CONN_SLAVE_LATENCY_MAX       (499)
-
-/* Connection handle range */
-#define BLE_LL_CONN_MAX_CONN_HANDLE         (0x0EFF)
-
-/* Offset (in bytes) of advertising address in connect request */
-#define BLE_LL_CONN_REQ_ADVA_OFF    (BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN)
-
-/* Default authenticated payload timeout (30 seconds; in 10 msecs increments) */
-#define BLE_LL_CONN_DEF_AUTH_PYLD_TMO       (3000)
-#define BLE_LL_CONN_AUTH_PYLD_OS_TMO(x)     \
-    ((((uint32_t)(x)) * 10 * OS_TICKS_PER_SEC) / 1000)
-
-/* Global Link Layer connection parameters */
-struct ble_ll_conn_global_params
-{
-    uint8_t master_chan_map[BLE_LL_CONN_CHMAP_LEN];
-    uint8_t num_used_chans;
-    uint8_t supp_max_tx_octets;
-    uint8_t supp_max_rx_octets;
-    uint8_t conn_init_max_tx_octets;
-    uint8_t sugg_tx_octets;
-    uint16_t sugg_tx_time;
-    uint16_t conn_init_max_tx_time;
-    uint16_t conn_init_max_tx_time_uncoded;
-    uint16_t conn_init_max_tx_time_coded;
-    uint16_t supp_max_tx_time;
-    uint16_t supp_max_rx_time;
-};
-extern struct ble_ll_conn_global_params g_ble_ll_conn_params;
-
-/* Some data structures used by other LL routines */
-SLIST_HEAD(ble_ll_conn_active_list, ble_ll_conn_sm);
-STAILQ_HEAD(ble_ll_conn_free_list, ble_ll_conn_sm);
-extern struct ble_ll_conn_active_list g_ble_ll_conn_active_list;
-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;
-
-/* Generic interface */
-struct ble_ll_len_req;
-struct hci_create_conn;
-struct ble_mbuf_hdr;
-struct ble_ll_adv_sm;
-
-void ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm);
-void ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
-void ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
-                             uint8_t hdr_byte, uint8_t length);
-struct ble_ll_conn_sm *ble_ll_conn_sm_get(void);
-void ble_ll_conn_master_init(struct ble_ll_conn_sm *connsm,
-                             struct hci_create_conn *hcc);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-void ble_ll_conn_ext_master_init(struct ble_ll_conn_sm *connsm,
-                                 struct hci_ext_create_conn *hcc);
-
-void ble_ll_conn_ext_set_params(struct ble_ll_conn_sm *connsm,
-                                struct hci_ext_conn_params *hcc_params,
-                                int phy);
-#endif
-
-struct ble_ll_conn_sm *ble_ll_conn_find_active_conn(uint16_t handle);
-void ble_ll_conn_datalen_update(struct ble_ll_conn_sm *connsm,
-                                struct ble_ll_len_req *req);
-
-/* Advertising interface */
-int ble_ll_conn_slave_start(uint8_t *rxbuf, uint8_t pat,
-                            struct ble_mbuf_hdr *rxhdr, bool force_csa2);
-
-/* Link Layer interface */
-void ble_ll_conn_module_init(void);
-void ble_ll_conn_set_global_chanmap(uint8_t num_used_chans, uint8_t *chanmap);
-void ble_ll_conn_module_reset(void);
-void ble_ll_conn_tx_pkt_in(struct os_mbuf *om, uint16_t handle, uint16_t len);
-int ble_ll_conn_rx_isr_start(struct ble_mbuf_hdr *rxhdr, uint32_t aa);
-int ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr);
-void ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr);
-void ble_ll_init_rx_pkt_in(uint8_t pdu_type, uint8_t *rxbuf,
-                           struct ble_mbuf_hdr *ble_hdr);
-int ble_ll_init_rx_isr_start(uint8_t pdu_type, struct ble_mbuf_hdr *ble_hdr);
-int ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
-                           struct ble_mbuf_hdr *ble_hdr);
-void ble_ll_conn_wfr_timer_exp(void);
-void ble_ll_conn_init_wfr_timer_exp(void);
-int ble_ll_conn_is_lru(struct ble_ll_conn_sm *s1, struct ble_ll_conn_sm *s2);
-uint32_t ble_ll_conn_get_ce_end_time(void);
-void ble_ll_conn_event_halt(void);
-uint8_t ble_ll_conn_calc_used_chans(uint8_t *chmap);
-void ble_ll_conn_reset_pending_aux_conn_rsp(void);
-bool ble_ll_conn_init_pending_aux_conn_rsp(void);
-/* HCI */
-void ble_ll_disconn_comp_event_send(struct ble_ll_conn_sm *connsm,
-                                    uint8_t reason);
-void ble_ll_auth_pyld_tmo_event_send(struct ble_ll_conn_sm *connsm);
-int ble_ll_conn_hci_disconnect_cmd(uint8_t *cmdbuf);
-int ble_ll_conn_hci_rd_rem_ver_cmd(uint8_t *cmdbuf);
-int ble_ll_conn_create(uint8_t *cmdbuf);
-int ble_ll_conn_hci_update(uint8_t *cmdbuf);
-int ble_ll_conn_hci_set_chan_class(uint8_t *cmdbuf);
-int ble_ll_conn_hci_param_reply(uint8_t *cmdbuf, int negative_reply);
-int ble_ll_conn_create_cancel(void);
-void ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm);
-void ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
-                                 uint8_t *evbuf, struct ble_ll_adv_sm *advsm);
-void ble_ll_conn_timeout(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
-int ble_ll_conn_hci_chk_conn_params(uint16_t itvl_min, uint16_t itvl_max,
-                                    uint16_t latency, uint16_t spvn_tmo);
-int ble_ll_conn_hci_read_rem_features(uint8_t *cmdbuf);
-int ble_ll_conn_hci_read_rem_features_complete(void);
-int ble_ll_conn_hci_rd_rssi(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen);
-int ble_ll_conn_hci_rd_chan_map(uint8_t *cmdbuf, uint8_t *rspbuf,
-                                uint8_t *rsplen);
-int ble_ll_conn_hci_set_data_len(uint8_t *cmdbuf, uint8_t *rspbuf,
-                                 uint8_t *rsplen);
-int ble_ll_conn_hci_le_start_encrypt(uint8_t *cmdbuf);
-int ble_ll_conn_hci_le_ltk_reply(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t ocf);
-int ble_ll_conn_hci_wr_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
-                                     uint8_t *rsplen);
-int ble_ll_conn_hci_rd_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
-                                     uint8_t *rsplen);
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-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);
-
-int ble_ll_conn_hci_le_rd_phy(uint8_t *cmdbuf, uint8_t *rsp, uint8_t *rsplen);
-int ble_ll_conn_hci_le_set_phy(uint8_t *cmdbuf);
-int ble_ll_conn_chk_phy_upd_start(struct ble_ll_conn_sm *connsm);
-void ble_ll_conn_req_pdu_make(struct ble_ll_conn_sm *connsm, uint8_t chan);
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
-int ble_ll_ext_conn_create(uint8_t *cmdbuf);
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_BLE_LL_CONN_PRIV_ */
diff --git a/net/nimble/controller/src/ble_ll_ctrl.c b/net/nimble/controller/src/ble_ll_ctrl.c
deleted file mode 100644
index a528eaae7..000000000
--- a/net/nimble/controller/src/ble_ll_ctrl.c
+++ /dev/null
@@ -1,2451 +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 "syscfg/syscfg.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "nimble/hci_common.h"
-#include "controller/ble_ll.h"
-#include "controller/ble_ll_hci.h"
-#include "controller/ble_ll_ctrl.h"
-#include "controller/ble_hw.h"
-#include "ble_ll_conn_priv.h"
-
-/* To use spec sample data for testing */
-#undef BLE_LL_ENCRYPT_USE_TEST_DATA
-
-/*
- * For console debug to show session key calculation. NOTE: if you define
- * this the stack requirements for the LL task go up considerably. The
- * default stack will not be enough and must be increased.
- */
-#undef BLE_LL_ENCRYPT_DEBUG
-#ifdef BLE_LL_ENCRYPT_DEBUG
-#include "console/console.h"
-#endif
-
-/*
- * XXX:
- *  1) Do I need to keep track of which procedures have already been done?
- *     Do I need to worry about repeating procedures?
- *  2) Should we create pool of control pdu's?. Dont need more
- *  than the # of connections and can probably deal with quite a few less
- *  if we have lots of connections.
- *  3) What about procedures that have been completed but try to restart?
- *  4) NOTE: there is a supported features procedure. However, in the case
- *  of data length extension, if the receiving device does not understand
- *  the pdu or it does not support data length extension, the LL_UNKNOWN_RSP
- *  pdu is sent. That needs to be processed...
- *  5) We are supposed to remember when we do the data length update proc if
- *  the device sent us an unknown rsp. We should not send it another len req.
- *  Implement this how? Through remote supported features?
- *  8) How to count control pdus sent. DO we count enqueued + sent, or only
- *  sent (actually attempted to tx). Do we count failures? How?
- */
-
-/*
- * XXX: I definitely have an issue with control procedures and connection
- * param request procedure and connection update procedure. This was
- * noted when receiving an unknown response. Right now, I am getting confused
- * with connection parameter request and updates regarding which procedures
- * are running. So I need to go look through all the code and see where I
- * used the request procedure and the update procedure and make sure I am doing
- * the correct thing.
- */
-
-/*
- * This array contains the length of the CtrData field in LL control PDU's.
- * Note that there is a one byte opcode which precedes this field in the LL
- * control PDU, so total data channel payload length for the control pdu is
- * one greater.
- */
-const uint8_t g_ble_ll_ctrl_pkt_lengths[BLE_LL_CTRL_OPCODES] =
-{
-    BLE_LL_CTRL_CONN_UPD_REQ_LEN,
-    BLE_LL_CTRL_CHAN_MAP_LEN,
-    BLE_LL_CTRL_TERMINATE_IND_LEN,
-    BLE_LL_CTRL_ENC_REQ_LEN,
-    BLE_LL_CTRL_ENC_RSP_LEN,
-    BLE_LL_CTRL_START_ENC_REQ_LEN,
-    BLE_LL_CTRL_START_ENC_RSP_LEN,
-    BLE_LL_CTRL_UNK_RSP_LEN,
-    BLE_LL_CTRL_FEATURE_LEN,
-    BLE_LL_CTRL_FEATURE_LEN,
-    BLE_LL_CTRL_PAUSE_ENC_REQ_LEN,
-    BLE_LL_CTRL_PAUSE_ENC_RSP_LEN,
-    BLE_LL_CTRL_VERSION_IND_LEN,
-    BLE_LL_CTRL_REJ_IND_LEN,
-    BLE_LL_CTRL_SLAVE_FEATURE_REQ_LEN,
-    BLE_LL_CTRL_CONN_PARAMS_LEN,
-    BLE_LL_CTRL_CONN_PARAMS_LEN,
-    BLE_LL_CTRL_REJECT_IND_EXT_LEN,
-    BLE_LL_CTRL_PING_LEN,
-    BLE_LL_CTRL_PING_LEN,
-    BLE_LL_CTRL_LENGTH_REQ_LEN,
-    BLE_LL_CTRL_LENGTH_REQ_LEN,
-    BLE_LL_CTRL_PHY_REQ_LEN,
-    BLE_LL_CTRL_PHY_RSP_LEN,
-    BLE_LL_CTRL_PHY_UPD_IND_LEN,
-    BLE_LL_CTRL_MIN_USED_CHAN_LEN
-};
-
-/**
- * Called to determine if a LL control procedure with an instant has
- * been initiated.
- *
- * If the function returns a 0 it means no conflicting procedure has
- * been initiated. Otherwise it returns the appropriate BLE error code to
- * send.
- *
- * @param connsm Pointer to connection state machine.
- * @param req_ctrl_proc The procedure that the peer is trying to initiate
- *
- * @return uint8_t
- */
-uint8_t
-ble_ll_ctrl_proc_with_instant_initiated(struct ble_ll_conn_sm *connsm,
-                                        uint8_t req_ctrl_proc)
-{
-    uint8_t err;
-
-    switch (connsm->cur_ctrl_proc) {
-    case BLE_LL_CTRL_PROC_PHY_UPDATE:
-    case BLE_LL_CTRL_PROC_CONN_UPDATE:
-    case BLE_LL_CTRL_PROC_CONN_PARAM_REQ:
-    case BLE_LL_CTRL_PROC_CHAN_MAP_UPD:
-        if (req_ctrl_proc == connsm->cur_ctrl_proc) {
-            err = BLE_ERR_LMP_COLLISION;
-        } else if ((connsm->cur_ctrl_proc == BLE_LL_CTRL_PROC_CONN_UPDATE) &&
-                   (req_ctrl_proc == BLE_LL_CTRL_PROC_CONN_PARAM_REQ)) {
-            err = BLE_ERR_LMP_COLLISION;
-        } else {
-            err = BLE_ERR_DIFF_TRANS_COLL;
-        }
-        break;
-    default:
-        err = 0;
-    }
-
-    return err;
-}
-
-/**
- * Create a LL_REJECT_EXT_IND pdu.
- *
- * @param rej_opcode Opcode to be rejected.
- * @param err: error response
- * @param ctrdata: Pointer to where CtrData starts in pdu
- */
-void
-ble_ll_ctrl_rej_ext_ind_make(uint8_t rej_opcode, uint8_t err, uint8_t *ctrdata)
-{
-    ctrdata[0] = rej_opcode;
-    ctrdata[1] = err;
-}
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-/**
- * Called to cancel a phy update procedure.
- *
- * @param connsm
- * @param ble_err
- */
-void
-ble_ll_ctrl_phy_update_cancel(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
-{
-    /* cancel any pending phy update procedures */
-    CLR_PENDING_CTRL_PROC(connsm, BLE_LL_CTRL_PROC_PHY_UPDATE);
-
-    /* Check if the host wants an event */
-    if (CONN_F_HOST_PHY_UPDATE(connsm)) {
-        ble_ll_hci_ev_phy_update(connsm, ble_err);
-        CONN_F_HOST_PHY_UPDATE(connsm) = 0;
-    }
-
-    /* Clear any bits for phy updates that might be in progress */
-    CONN_F_CTRLR_PHY_UPDATE(connsm) = 0;
-}
-#endif
-
-static int
-ble_ll_ctrl_len_proc(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
-{
-    int rc;
-    struct ble_ll_len_req ctrl_req;
-
-    /* Extract parameters and check if valid */
-    ctrl_req.max_rx_bytes = get_le16(dptr);
-    ctrl_req.max_rx_time = get_le16(dptr + 2);
-    ctrl_req.max_tx_bytes = get_le16(dptr + 4);
-    ctrl_req.max_tx_time = get_le16(dptr + 6);
-
-    if ((ctrl_req.max_rx_bytes < BLE_LL_CONN_SUPP_BYTES_MIN) ||
-        (ctrl_req.max_rx_time < BLE_LL_CONN_SUPP_TIME_MIN) ||
-        (ctrl_req.max_tx_bytes < BLE_LL_CONN_SUPP_BYTES_MIN) ||
-        (ctrl_req.max_tx_time < BLE_LL_CONN_SUPP_TIME_MIN)) {
-        rc = 1;
-    } else {
-        /* Update the connection with the new parameters */
-        ble_ll_conn_datalen_update(connsm, &ctrl_req);
-        rc = 0;
-    }
-
-    return rc;
-}
-
-/**
- * Process a received LL_PING_RSP control pdu.
- *
- * NOTE: we dont have to reset the callout since this packet will have had a
- * valid MIC and that will restart the authenticated payload timer
- *
- * @param connsm
- */
-static void
-ble_ll_ctrl_rx_ping_rsp(struct ble_ll_conn_sm *connsm)
-{
-    /* Stop the control procedure */
-    ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_LE_PING);
-}
-
-/**
- * Called when we receive either a connection parameter request or response.
- *
- * @param connsm
- * @param dptr
- * @param rspbuf
- * @param opcode
- *
- * @return int
- */
-static int
-ble_ll_ctrl_conn_param_pdu_proc(struct ble_ll_conn_sm *connsm, uint8_t *dptr,
-                                uint8_t *rspbuf, uint8_t opcode)
-{
-    int rc;
-    int indicate;
-    uint8_t rsp_opcode;
-    uint8_t ble_err;
-    struct ble_ll_conn_params *req;
-    struct hci_conn_update *hcu;
-
-    /* Extract parameters and check if valid */
-    req = &connsm->conn_cp;
-    req->interval_min = get_le16(dptr);
-    req->interval_max = get_le16(dptr + 2);
-    req->latency = get_le16(dptr + 4);
-    req->timeout = get_le16(dptr + 6);
-    req->pref_periodicity = dptr[8];
-    req->ref_conn_event_cnt  = get_le16(dptr + 9);
-    req->offset0 = get_le16(dptr + 11);
-    req->offset1 = get_le16(dptr + 13);
-    req->offset2 = get_le16(dptr + 15);
-    req->offset3 = get_le16(dptr + 17);
-    req->offset4 = get_le16(dptr + 19);
-    req->offset5 = get_le16(dptr + 21);
-
-    /* Check if parameters are valid */
-    ble_err = BLE_ERR_SUCCESS;
-    rc = ble_ll_conn_hci_chk_conn_params(req->interval_min,
-                                         req->interval_max,
-                                         req->latency,
-                                         req->timeout);
-    if (rc) {
-        ble_err = BLE_ERR_INV_LMP_LL_PARM;
-        goto conn_param_pdu_exit;
-    }
-
-    /*
-     * Check if there is a requested change to either the interval, timeout
-     * or latency. If not, this may just be an anchor point change and we do
-     * not have to notify the host.
-     *  XXX: what if we dont like the parameters? When do we check that out?
-     */
-    indicate = 1;
-    if (opcode == BLE_LL_CTRL_CONN_PARM_REQ) {
-        if ((connsm->conn_itvl >= req->interval_min) &&
-            (connsm->conn_itvl <= req->interval_max) &&
-            (connsm->supervision_tmo == req->timeout) &&
-            (connsm->slave_latency == req->latency)) {
-            indicate = 0;
-            goto conn_parm_req_do_indicate;
-        }
-    }
-
-    /*
-     * A change has been requested. Is it within the values specified by
-     * the host? Note that for a master we will not be processing a
-     * connect param request from a slave if we are currently trying to
-     * update the connection parameters. This means that the previous
-     * check is all we need for a master (when receiving a request).
-     */
-    if ((connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) ||
-        (opcode == BLE_LL_CTRL_CONN_PARM_RSP)) {
-        /*
-         * Not sure what to do about the slave. It is possible that the
-         * current connection parameters are not the same ones as the local host
-         * has provided? Not sure what to do here. Do we need to remember what
-         * host sent us? For now, I will assume that we need to remember what
-         * the host sent us and check it out.
-         */
-        hcu = &connsm->conn_param_req;
-        if (hcu->handle != 0) {
-            if (!((req->interval_min < hcu->conn_itvl_min) ||
-                  (req->interval_min > hcu->conn_itvl_max) ||
-                  (req->interval_max < hcu->conn_itvl_min) ||
-                  (req->interval_max > hcu->conn_itvl_max) ||
-                  (req->latency != hcu->conn_latency) ||
-                  (req->timeout != hcu->supervision_timeout))) {
-                indicate = 0;
-            }
-        }
-    }
-
-conn_parm_req_do_indicate:
-    /*
-     * XXX: are the connection update parameters acceptable? If not, we will
-     * need to know before we indicate to the host that they are acceptable.
-     */
-    if (indicate) {
-        /*
-         * Send event to host. At this point we leave and wait to get
-         * an answer.
-         */
-        /* XXX: what about masked out event? */
-        ble_ll_hci_ev_rem_conn_parm_req(connsm, req);
-        connsm->host_reply_opcode = opcode;
-        connsm->csmflags.cfbit.awaiting_host_reply = 1;
-        rsp_opcode = 255;
-    } else {
-        /* Create reply to connection request */
-        rsp_opcode = ble_ll_ctrl_conn_param_reply(connsm, rspbuf, req);
-    }
-
-conn_param_pdu_exit:
-    if (ble_err) {
-        rsp_opcode = BLE_LL_CTRL_REJECT_IND_EXT;
-        rspbuf[1] = opcode;
-        rspbuf[2] = ble_err;
-    }
-    return rsp_opcode;
-}
-
-/**
- * Called to process and UNKNOWN_RSP LL control packet.
- *
- * Context: Link Layer Task
- *
- * @param dptr
- */
-static void
-ble_ll_ctrl_proc_unk_rsp(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
-{
-    uint8_t ctrl_proc;
-    uint8_t opcode;
-
-    /* Get opcode of unknown LL control frame */
-    opcode = dptr[0];
-
-    /* Convert opcode to control procedure id */
-    switch (opcode) {
-    case BLE_LL_CTRL_LENGTH_REQ:
-        ctrl_proc = BLE_LL_CTRL_PROC_DATA_LEN_UPD;
-        break;
-    case BLE_LL_CTRL_CONN_UPDATE_IND:
-        ctrl_proc = BLE_LL_CTRL_PROC_CONN_UPDATE;
-        break;
-    case BLE_LL_CTRL_SLAVE_FEATURE_REQ:
-        ctrl_proc = BLE_LL_CTRL_PROC_FEATURE_XCHG;
-        break;
-    case BLE_LL_CTRL_CONN_PARM_RSP:
-    case BLE_LL_CTRL_CONN_PARM_REQ:
-        ctrl_proc = BLE_LL_CTRL_PROC_CONN_PARAM_REQ;
-        break;
-    case BLE_LL_CTRL_PING_REQ:
-        CONN_F_LE_PING_SUPP(connsm) = 0;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
-        os_callout_stop(&connsm->auth_pyld_timer);
-#endif
-        ctrl_proc = BLE_LL_CTRL_PROC_LE_PING;
-        break;
-#if (BLE_LL_BT5_PHY_SUPPORTED ==1)
-    case BLE_LL_CTRL_PHY_REQ:
-        ble_ll_ctrl_phy_update_cancel(connsm, BLE_ERR_UNSUPP_REM_FEATURE);
-        ctrl_proc = BLE_LL_CTRL_PROC_PHY_UPDATE;
-        break;
-#endif
-    default:
-        ctrl_proc = BLE_LL_CTRL_PROC_NUM;
-        break;
-    }
-
-    /* If we are running this one currently, stop it */
-    if (connsm->cur_ctrl_proc == ctrl_proc) {
-        /* Stop the control procedure */
-        ble_ll_ctrl_proc_stop(connsm, ctrl_proc);
-        if (ctrl_proc == BLE_LL_CTRL_PROC_CONN_PARAM_REQ) {
-            ble_ll_hci_ev_conn_update(connsm, BLE_ERR_UNSUPP_REM_FEATURE);
-        } else if (ctrl_proc == BLE_LL_CTRL_PROC_FEATURE_XCHG) {
-            if (connsm->csmflags.cfbit.pending_hci_rd_features) {
-                ble_ll_hci_ev_rd_rem_used_feat(connsm,
-                                                   BLE_ERR_UNSUPP_REM_FEATURE);
-            }
-            connsm->csmflags.cfbit.pending_hci_rd_features = 0;
-        }
-    }
-}
-
-#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
-void
-ble_ll_ctrl_phy_update_proc_complete(struct ble_ll_conn_sm *connsm)
-{
-    int chk_proc_stop;
-    int chk_host_phy;
-
-    chk_proc_stop = 1;
-    chk_host_phy = 1;
-
-    if (CONN_F_PEER_PHY_UPDATE(connsm)) {
-        CONN_F_PEER_PHY_UPDATE(connsm) = 0;
-    } else if (CONN_F_CTRLR_PHY_UPDATE(connsm)) {
-        CONN_F_CTRLR_PHY_UPDATE(connsm) = 0;
-    } else {
-        /* Must be a host-initiated update */
-        CONN_F_HOST_PHY_UPDATE(connsm) = 0;
-        chk_host_phy = 0;
-        if (CONN_F_PHY_UPDATE_EVENT(connsm) == 0) {
-            ble_ll_hci_ev_phy_update(connsm, BLE_ERR_SUCCESS);
-        }
-    }
-
-    /* Must check if we need to start host procedure */
-    if (chk_host_phy) {
-        if (CONN_F_HOST_PHY_UPDATE(connsm)) {
-            if (ble_ll_conn_chk_phy_upd_start(connsm)) {
-                CONN_F_HOST_PHY_UPDATE(connsm) = 0;
-            } else {
-                chk_proc_stop = 0;
-            }
-        }
-    }
-
-    if (chk_proc_stop) {
-        ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_PHY_UPDATE);
-    }
-}
-
-/**
- * Convert a phy mask to a numeric phy value.
- *
- * NOTE: only one bit should be set here and there should be at least one.
- * If this function returns a 0 it is an error!
- *
- * @param phy_mask Bitmask of phy
- *
- * @return uint8_t The numeric value associated with the phy mask
- *
- * BLE_HCI_LE_PHY_1M                    (1)
- * BLE_HCI_LE_PHY_2M                    (2)
- * BLE_HCI_LE_PHY_CODED                 (3)
- */
-static uint8_t
-ble_ll_ctrl_phy_from_phy_mask(uint8_t phy_mask)
-{
-    uint8_t phy;
-
-    /*
-     * NOTE: wipe out unsupported PHYs. There should not be an unsupported
-     * in this mask if the other side is working correctly.
-     */
-#if !MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
-    phy_mask &= ~BLE_HCI_LE_PHY_2M_PREF_MASK;
-#endif
-#if !MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
-    phy_mask &= ~BLE_HCI_LE_PHY_CODED_PREF_MASK;
-#endif
-
-    if (phy_mask & BLE_PHY_MASK_1M) {
-        phy = BLE_PHY_1M;
-        phy_mask &= ~BLE_PHY_MASK_1M;
-    } else if (phy_mask & BLE_PHY_MASK_2M) {
-        phy = BLE_PHY_2M;
-        phy_mask &= ~BLE_PHY_MASK_2M;
-    } else if (phy_mask & BLE_PHY_MASK_CODED) {
-        phy = BLE_PHY_CODED;
-        phy_mask &= ~BLE_PHY_MASK_CODED;
-    } else {
-        phy = 0;
-    }
-
-    if (phy_mask != 0) {
-        phy = 0;
-    }
-
-    return phy;
-}
-
-/**
- *
- *  There is probably a better way for the controller to choose which PHY use.
- *  There are no BER metrics and RSSI does not give you S/N so for now we will
- *  choose this heirarchy:
- *     -> if 2Mbps available, use it.
- *     -> If 1Mbps available, use it.
- *     -> otherwise use coded phy.
- *
- * @param prefs The mask of preferred phys
- * @return uint8_t The phy to use (not a mask)
- */
-static uint8_t
-ble_ll_ctrl_find_new_phy(uint8_t phy_mask_prefs)
-{
-    uint8_t new_phy;
-
-    new_phy = phy_mask_prefs;
-    if (new_phy) {
-        if (new_phy & BLE_PHY_MASK_2M) {
-            new_phy = BLE_PHY_2M;
-        } else if (new_phy & BLE_PHY_MASK_1M) {
-            new_phy = BLE_PHY_1M;
-        } else {
-            new_phy = BLE_PHY_CODED;
-        }
-    }
-
-    return new_phy;
-}
-
-/**
- * Create a LL_PHY_UPDATE_IND pdu
- *
- * @param connsm Pointer to connection state machine
- * @param dptr Pointer to PHY_REQ or PHY_RSP data.
- * @param ctrdata: Pointer to where CtrData of UPDATE_IND pdu starts
- * @param slave_req flag denoting if slave requested this. 0: no 1:yes
- */
-static void
-ble_ll_ctrl_phy_update_ind_make(struct ble_ll_conn_sm *connsm, uint8_t *dptr,
-                                uint8_t *ctrdata, int slave_req)
-{
-    uint8_t m_to_s;
-    uint8_t s_to_m;
-    uint8_t tx_phys;
-    uint8_t rx_phys;
-    uint16_t instant;
-
-    /* Get preferences from PDU */
-    tx_phys = dptr[0];
-    rx_phys = dptr[1];
-
-    /* Get m_to_s and s_to_m masks */
-    if (slave_req) {
-        m_to_s = connsm->phy_data.host_pref_tx_phys_mask & rx_phys;
-        s_to_m = connsm->phy_data.host_pref_rx_phys_mask & tx_phys;
-    } else {
-        m_to_s = connsm->phy_data.req_pref_tx_phys_mask & rx_phys;
-        s_to_m = connsm->phy_data.req_pref_rx_phys_mask & tx_phys;
-    }
-
-    /* Find new phys. If not different than current, set to 0 */
-    m_to_s = ble_ll_ctrl_find_new_phy(m_to_s);
-    if (m_to_s == connsm->phy_data.cur_tx_phy) {
-        m_to_s = 0;
-    }
-
-    s_to_m = ble_ll_ctrl_find_new_phy(s_to_m);
-    if (s_to_m == connsm->phy_data.cur_rx_phy) {
-        s_to_m = 0;
-    }
-
-    /* At this point, m_to_s and s_to_m are not masks; they are numeric */
-
-    /*
-     * If not changing we still send update ind. Check if hosts expects
-     * the event and if so send it. Stop control procedure if it is the
-     * one running.
-     */
-    if ((m_to_s == 0) && (s_to_m == 0)) {
-        if (CONN_F_PEER_PHY_UPDATE(connsm)) {
-            CONN_F_PEER_PHY_UPDATE(connsm) = 0;
-        } else if (CONN_F_CTRLR_PHY_UPDATE(connsm)) {
-            CONN_F_CTRLR_PHY_UPDATE(connsm) = 0;
-            ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_PHY_UPDATE);
-        } else {
-            ble_ll_hci_ev_phy_update(connsm, BLE_ERR_SUCCESS);
-            CONN_F_HOST_PHY_UPDATE(connsm) = 0;
-            ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_PHY_UPDATE);
-        }
-        instant = 0;
-    } else {
-        /* Determine instant we will use. 6 more is minimum */
-        instant = connsm->event_cntr + connsm->slave_latency + 6 + 1;
-        connsm->phy_instant = instant;
-        CONN_F_PHY_UPDATE_SCHED(connsm) = 1;
-
-        /* Set new phys to use when instant occurs */
-        connsm->phy_data.new_tx_phy = m_to_s;
-        connsm->phy_data.new_rx_phy = s_to_m;
-
-        /* Convert m_to_s and s_to_m to masks */
-        if (m_to_s) {
-            m_to_s = 1 << (m_to_s - 1);
-        }
-
-        if (s_to_m) {
-            s_to_m = 1 << (s_to_m - 1);
-        }
-    }
-
-    ctrdata[0] = m_to_s;
-    ctrdata[1] = s_to_m;
-    put_le16(ctrdata + 2, instant);
-}
-
-/**
- * Create a LL_PHY_REQ or LL_PHY_RSP pdu
- *
- * @param connsm Pointer to connection state machine
- * @param ctrdata: Pointer to where CtrData starts in pdu
- */
-static void
-ble_ll_ctrl_phy_req_rsp_make(struct ble_ll_conn_sm *connsm, uint8_t *ctrdata)
-{
-    /* If no preference we use current phy */
-    if (connsm->phy_data.host_pref_tx_phys_mask == 0) {
-        ctrdata[0] = CONN_CUR_TX_PHY_MASK(connsm);
-    } else {
-        ctrdata[0] = connsm->phy_data.host_pref_tx_phys_mask;
-    }
-    if (connsm->phy_data.host_pref_rx_phys_mask == 0) {
-        ctrdata[1] = CONN_CUR_RX_PHY_MASK(connsm);
-    } else {
-        ctrdata[1] = connsm->phy_data.host_pref_rx_phys_mask;
-    }
-}
-
-static uint8_t
-ble_ll_ctrl_rx_phy_req(struct ble_ll_conn_sm *connsm, uint8_t *req,
-                       uint8_t *rsp)
-{
-    uint8_t rsp_opcode;
-    uint8_t err;
-
-    /*
-     * XXX: TODO if we have an instant in progress we should end connection.
-     * At least it seems that is the case. Need to figure out more from
-     * the spec here.
-     */
-
-    /* Check if we have already initiated a procedure with an instant */
-    err = ble_ll_ctrl_proc_with_instant_initiated(connsm,
-                                                  BLE_LL_CTRL_PROC_PHY_UPDATE);
-
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-        if (err) {
-            ble_ll_ctrl_rej_ext_ind_make(BLE_LL_CTRL_PHY_REQ, err, rsp);
-            rsp_opcode = BLE_LL_CTRL_REJECT_IND_EXT;
-        } else {
-            /*
-             * NOTE: do not change order of these two lines as the call to
-             * make the LL_PHY_UPDATE_IND pdu might clear the flag.
-             */
-            CONN_F_PEER_PHY_UPDATE(connsm) = 1;
-            ble_ll_ctrl_phy_update_ind_make(connsm, req, rsp, 1);
-            rsp_opcode = BLE_LL_CTRL_PHY_UPDATE_IND;
-        }
-    } else {
-        /* XXX: deal with other control procedures that we need to stop */
-        if (err) {
-            if (connsm->cur_ctrl_proc == BLE_LL_CTRL_PROC_PHY_UPDATE) {
-                os_callout_stop(&connsm->ctrl_proc_rsp_timer);
-                connsm->cur_ctrl_proc = BLE_LL_CTRL_PROC_IDLE;
-            }
-
-            /* If there is a PHY update procedure pending cancel it */
-            ble_ll_ctrl_phy_update_cancel(connsm, err);
-
-            /* XXX: ? Should not be any phy update events */
-            CONN_F_PHY_UPDATE_EVENT(connsm) = 0;
-        }
-
-        /* XXX: TODO: if we started another procedure with an instant
-         * why are we doing this? Need to look into this.*/
-
-        /* Respond to master's phy update procedure */
-        CONN_F_PEER_PHY_UPDATE(connsm) = 1;
-        ble_ll_ctrl_phy_req_rsp_make(connsm, rsp);
-        rsp_opcode = BLE_LL_CTRL_PHY_RSP;
-    }
-    return rsp_opcode;
-}
-
-/**
- * Process a received LL_PHY_RSP pdu
- *
- * @param connsm
- * @param dptr Pointer to LL_PHY_RSP ctrdata
- * @param rsp Pointer to CtrData of PHY_UPDATE_IND.
- *
- * @return uint8_t
- */
-static uint8_t
-ble_ll_ctrl_rx_phy_rsp(struct ble_ll_conn_sm *connsm, uint8_t *dptr,
-                       uint8_t *rsp)
-{
-    uint8_t rsp_opcode;
-
-    rsp_opcode = BLE_ERR_MAX;
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_MASTER) {
-        if (connsm->cur_ctrl_proc == BLE_LL_CTRL_PROC_PHY_UPDATE) {
-            ble_ll_ctrl_phy_update_ind_make(connsm, dptr, rsp, 0);
-            os_callout_stop(&connsm->ctrl_proc_rsp_timer);
-            rsp_opcode = BLE_LL_CTRL_PHY_UPDATE_IND;
-        }
-
-        /*
-         * If not in the process of doing this control procedure something
-         * is wrong. End connection? Assert?
-         *
-         * XXX: TODO count some stat?
-         */
-    }
-
-    /* NOTE: slave should never receive one of these */
-
-    return rsp_opcode;
-}
-
-/**
- * Called when a LL_PHY_UPDATE_IND pdu is received
- *
- * NOTE: slave is the only device that should receive this.
- *
- * @param connsm
- * @param dptr
- */
-void
-ble_ll_ctrl_rx_phy_update_ind(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
-{
-    int no_change;
-    uint8_t new_m_to_s_mask;
-    uint8_t new_s_to_m_mask;
-    uint8_t new_tx_phy;
-    uint8_t new_rx_phy;
-    uint16_t instant;
-    uint16_t delta;
-
-    if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
-        /*
-         * Reception stops the procedure response timer but does not
-         * complete the procedure
-         */
-        if (connsm->cur_ctrl_proc == BLE_LL_CTRL_PROC_PHY_UPDATE) {
-            os_callout_stop(&connsm->ctrl_proc_rsp_timer);
-        }
-
-        /*
-         * XXX: Should we check to see if we are expecting to receive one
-         * of these, and if not, kill connection? Meaning we better be
-         * doing either a PEER, CTRLR, or HOST phy update.
-         */
-        /* get the new phy masks and see if we need to change */
-        new_m_to_s_mask = dptr[0];
-        new_s_to_m_mask = dptr[1];
-        instant = get_le16(dptr + 2);
-
-        if ((new_m_to_s_mask == 0) && (new_s_to_m_mask == 0)) {
-            /* No change in phy */
-            no_change = 1;
-        } else {
-            no_change = 0;
-            /*
-             * NOTE: from the slaves perspective, the m to s phy is the one
-             * that the slave will receive on; s to m is the one it will
-             * transmit on
-             */
-            new_rx_phy = ble_ll_ctrl_phy_from_phy_mask(new_m_to_s_mask);
-            new_tx_phy = ble_ll_ctrl_phy_from_phy_mask(new_s_to_m_mask);
-
-            if ((new_tx_phy == 0) && (new_rx_phy == 0)) {
-                /* XXX: this is an error! What to do??? */
-                no_change = 1;
-            }
-
-            if ((new_tx_phy == connsm->phy_data.cur_tx_phy) &&
-                (new_rx_phy == connsm->phy_data.cur_rx_phy)) {
-                no_change = 1;
-            }
-        }
-
-        if (!no_change) {
-            /* If instant is in the past, we have to end the connection */
-            delta = (instant - connsm->event_cntr) & 0xFFFF;
-            if (delta >= 32767) {
-                ble_ll_conn_timeout(connsm, BLE_ERR_INSTANT_PASSED);
-            } else {
-                connsm->phy_data.new_tx_phy = new_tx_phy;
-                connsm->phy_data.new_rx_phy = new_rx_phy;
-                connsm->phy_instant = instant;
-                CONN_F_PHY_UPDATE_SCHED(connsm) = 1;
-            }
-            return;
-        }
-
-        ble_ll_ctrl_phy_update_proc_complete(connsm);
-    }
-}
-#endif
-
-/**
- * Create a link layer length request or length response PDU.
- *
- * NOTE: this function does not set the LL data pdu header nor does it
- * set the opcode in the buffer.
- *
- * @param connsm
- * @param dptr: Pointer to where control pdu payload starts
- */
-static void
-ble_ll_ctrl_datalen_upd_make(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
-{
-    put_le16(dptr + 1, connsm->max_rx_octets);
-    put_le16(dptr + 3, connsm->max_rx_time);
-    put_le16(dptr + 5, connsm->max_tx_octets);
-    put_le16(dptr + 7, connsm->max_tx_time);
-}
-
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-void
-ble_ll_calc_session_key(struct ble_ll_conn_sm *connsm)
-{
-#ifdef BLE_LL_ENCRYPT_DEBUG
-    int cnt;
-#endif
-
-    /* XXX: possibly have some way out of this if this locks up */
-    while (1) {
-        if (!ble_hw_encrypt_block(&connsm->enc_data.enc_block)) {
-            break;
-        }
-    }
-
-#ifdef BLE_LL_ENCRYPT_DEBUG
-    console_printf("Calculating Session Key for handle=%u",
-                   connsm->conn_handle);
-
-    console_printf("\nLTK:");
-    for (cnt = 0; cnt < 16; ++cnt) {
-        console_printf("%02x", connsm->enc_data.enc_block.key[cnt]);
-    }
-    console_printf("\nSKD:");
-    for (cnt = 0; cnt < 16; ++cnt) {
-        console_printf("%02x", connsm->enc_data.enc_block.plain_text[cnt]);
-    }
-    console_printf("\nSession Key:");
-    for (cnt = 0; cnt < 16; ++cnt) {
-        console_printf("%02x", connsm->enc_data.enc_block.cipher_text[cnt]);
-    }
-    console_printf("\nIV:");

  (This diff was longer than 20,000 lines, and has been truncated...)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services