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

incubator-mynewt-larva git commit: MYNEWT-40: first cut at adding ble controller link layer statistics. There are two groups as of now: ble_ll and ble_ll_conn. I dont think this will be the final arrangement as some of the stats may move from ble_ll to b

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/develop e51ff900e -> 598b62931


MYNEWT-40: first cut at adding ble controller link layer statistics. There are two groups as of now: ble_ll and ble_ll_conn. I dont think this will be the final arrangement as some of the stats may move from ble_ll to ble_ll_conn


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/598b6293
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/598b6293
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/598b6293

Branch: refs/heads/develop
Commit: 598b6293101bb6163d2eabd5ff379c6f8a94bd5b
Parents: e51ff90
Author: wes3 <wi...@micosa.io>
Authored: Thu Feb 18 09:56:30 2016 -0800
Committer: wes3 <wi...@micosa.io>
Committed: Thu Feb 18 09:56:30 2016 -0800

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll.h      |  69 ++++---
 .../controller/include/controller/ble_ll_ctrl.h |   2 +-
 net/nimble/controller/pkg.yml                   |   1 +
 net/nimble/controller/src/ble_ll.c              | 130 ++++++++----
 net/nimble/controller/src/ble_ll_adv.c          |  31 +--
 net/nimble/controller/src/ble_ll_conn.c         | 198 +++++++++++--------
 net/nimble/controller/src/ble_ll_ctrl.c         |   7 +-
 net/nimble/controller/src/ble_ll_hci.c          |   6 +-
 net/nimble/controller/src/ble_ll_scan.c         |  22 +--
 net/nimble/controller/src/ble_ll_sched.c        |   4 +-
 project/bletest/src/main.c                      |   3 +-
 11 files changed, 270 insertions(+), 203 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index e9000ab..8c4d479 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -20,6 +20,7 @@
 #ifndef H_BLE_LL_
 #define H_BLE_LL_
 
+#include "stats/stats.h"
 #include "hal/hal_cputime.h"
 
 /* Controller revision. */
@@ -70,41 +71,39 @@ struct ble_ll_obj
 extern struct ble_ll_obj g_ble_ll_data;
 
 /* Link layer statistics */
-struct ble_ll_stats
-{
-    uint32_t hci_cmds;
-    uint32_t hci_cmd_errs;
-    uint32_t hci_events_sent;
-    uint32_t bad_ll_state;
-    uint32_t bad_acl_hdr;
-    uint32_t sched_state_conn_errs;
-    uint32_t sched_state_adv_errs;
-    uint32_t rx_bytes;
-    uint32_t rx_valid_adv_pdus;
-    uint32_t rx_invalid_adv_pdus;
-    uint32_t rx_adv_malformed_pkts;
-    uint32_t rx_adv_unk_pdu_type;
-    uint32_t rx_adv_ind;
-    uint32_t rx_adv_direct_ind;
-    uint32_t rx_adv_nonconn_ind;
-    uint32_t rx_scan_reqs;
-    uint32_t rx_scan_rsps;
-    uint32_t rx_connect_reqs;
-    uint32_t rx_scan_ind;
-    uint32_t rx_unk_pdus;
-    uint32_t rx_valid_data_pdus;
-    uint32_t rx_invalid_data_pdus;
-    uint32_t rx_ctrl_pdus;
-    uint32_t rx_l2cap_pdus;
-    uint32_t rx_malformed_ctrl_pdus;
-    uint32_t rx_bad_llid;
-    uint32_t tx_ctrl_pdus;
-    uint32_t tx_ctrl_bytes;
-    uint32_t tx_l2cap_pdus;
-    uint32_t tx_l2cap_bytes;
-    uint32_t tx_empty_pdus;
-};
-extern struct ble_ll_stats g_ble_ll_stats;
+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(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_scan_reqs)
+    STATS_SECT_ENTRY(rx_scan_rsps)
+    STATS_SECT_ENTRY(rx_connect_reqs)
+    STATS_SECT_ENTRY(rx_scan_ind)
+    STATS_SECT_ENTRY(adv_txg)
+    STATS_SECT_ENTRY(adv_late_starts)
+    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_END
+extern STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
 
 /* States */
 #define BLE_LL_STATE_STANDBY        (0)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/include/controller/ble_ll_ctrl.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_ctrl.h b/net/nimble/controller/include/controller/ble_ll_ctrl.h
index 5bec393..c501044 100644
--- a/net/nimble/controller/include/controller/ble_ll_ctrl.h
+++ b/net/nimble/controller/include/controller/ble_ll_ctrl.h
@@ -227,7 +227,7 @@ struct ble_ll_len_req
 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);
-void ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om);
+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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml
index e4b2912..11aa842 100644
--- a/net/nimble/controller/pkg.yml
+++ b/net/nimble/controller/pkg.yml
@@ -21,6 +21,7 @@ pkg.name: net/nimble/controller
 pkg.vers: 0.1 
 pkg.deps:
     - libs/os
+    - sys/stats
     - net/nimble
 pkg.identities:
     - BLE_DEVICE

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
index 5896918..7a72d37 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -21,6 +21,7 @@
 #include <assert.h>
 #include <string.h>
 #include "os/os.h"
+#include "stats/stats.h"
 #include "bsp/bsp.h"
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
@@ -62,7 +63,39 @@
 struct ble_ll_obj g_ble_ll_data;
 
 /* Global link layer statistics */
-struct ble_ll_stats g_ble_ll_stats;
+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, 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_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, adv_txg)
+    STATS_NAME(ble_ll_stats, adv_late_starts)
+    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_END(ble_ll_stats)
 
 /* The BLE LL task data structure */
 #define BLE_LL_TASK_PRI     (OS_TASK_PRI_HIGHEST)
@@ -121,28 +154,27 @@ ble_ll_count_rx_adv_pdus(uint8_t pdu_type)
     /* Count received packet types  */
     switch (pdu_type) {
     case BLE_ADV_PDU_TYPE_ADV_IND:
-        ++g_ble_ll_stats.rx_adv_ind;
+        STATS_INC(ble_ll_stats, rx_adv_ind);
         break;
     case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND:
-        ++g_ble_ll_stats.rx_adv_direct_ind;
+        STATS_INC(ble_ll_stats, rx_adv_direct_ind);
         break;
     case BLE_ADV_PDU_TYPE_ADV_NONCONN_IND:
-        ++g_ble_ll_stats.rx_adv_nonconn_ind;
+        STATS_INC(ble_ll_stats, rx_adv_nonconn_ind);
         break;
     case BLE_ADV_PDU_TYPE_SCAN_REQ:
-        ++g_ble_ll_stats.rx_scan_reqs;
+        STATS_INC(ble_ll_stats, rx_scan_reqs);
         break;
     case BLE_ADV_PDU_TYPE_SCAN_RSP:
-        ++g_ble_ll_stats.rx_scan_rsps;
+        STATS_INC(ble_ll_stats, rx_scan_rsps);
         break;
     case BLE_ADV_PDU_TYPE_CONNECT_REQ:
-        ++g_ble_ll_stats.rx_connect_reqs;
+        STATS_INC(ble_ll_stats, rx_connect_reqs);
         break;
     case BLE_ADV_PDU_TYPE_ADV_SCAN_IND:
-        ++g_ble_ll_stats.rx_scan_ind;
+        STATS_INC(ble_ll_stats, rx_scan_ind);
         break;
     default:
-        ++g_ble_ll_stats.rx_adv_unk_pdu_type;
         break;
     }
 }
@@ -348,7 +380,7 @@ ble_ll_tx_pkt_in(void)
         pb = handle & 0x3000;
         if ((pkthdr->omp_len != length) || (pb > 0x1000) || (length == 0)) {
             /* This is a bad ACL packet. Count a stat and free it */
-            ++g_ble_ll_stats.bad_acl_hdr;
+            STATS_INC(ble_ll_stats, bad_acl_hdr);
             os_mbuf_free(om);
             continue;
         }
@@ -359,6 +391,42 @@ ble_ll_tx_pkt_in(void)
 }
 
 /**
+ * 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;
+    uint8_t chan;
+
+    crcok = BLE_MBUF_HDR_CRC_OK(hdr);
+    chan = hdr->rxinfo.channel;
+    if (crcok) {
+        if (chan < BLE_PHY_NUM_DATA_CHANS) {
+            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 (chan < BLE_PHY_NUM_DATA_CHANS) {
+            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.
@@ -372,7 +440,6 @@ ble_ll_rx_pkt_in(void)
     os_sr_t sr;
     uint8_t pdu_type;
     uint8_t *rxbuf;
-    uint8_t crcok;
     struct os_mbuf_pkthdr *pkthdr;
     struct ble_mbuf_hdr *ble_hdr;
     struct os_mbuf *m;
@@ -388,28 +455,16 @@ ble_ll_rx_pkt_in(void)
         STAILQ_REMOVE_HEAD(&g_ble_ll_data.ll_rx_pkt_q, omp_next);
         OS_EXIT_CRITICAL(sr);
 
-        /* Count statistics */
-        rxbuf = m->om_data;
+        /* Note: pdu type wont get used unless this is an advertising pdu */
         ble_hdr = BLE_MBUF_HDR_PTR(m); 
-        crcok = BLE_MBUF_HDR_CRC_OK(ble_hdr);
-        if (crcok) {
-            /* The total bytes count the PDU header and PDU payload */
-            g_ble_ll_stats.rx_bytes += pkthdr->omp_len;
-        }
+        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 */
         if (ble_hdr->rxinfo.channel < BLE_PHY_NUM_DATA_CHANS) {
             ble_ll_conn_rx_data_pdu(m, ble_hdr);
         } else {
-            /* Get advertising PDU type */
-            pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK;
-            if (crcok) {
-                /* Count by type only with valid crc */
-                ++g_ble_ll_stats.rx_valid_adv_pdus;
-                ble_ll_count_rx_adv_pdus(pdu_type);
-            } else {
-                ++g_ble_ll_stats.rx_invalid_adv_pdus;
-            }
-
             /* Process the PDU */
             switch (BLE_MBUF_HDR_RX_STATE(ble_hdr)) {
             case BLE_LL_STATE_ADV:
@@ -423,11 +478,10 @@ ble_ll_rx_pkt_in(void)
                 break;
             default:
                 /* Any other state should never occur */
-                ++g_ble_ll_stats.bad_ll_state;
+                STATS_INC(ble_ll_stats, bad_ll_state);
                 break;
             }
 
-
             /* Free the packet buffer */
             os_mbuf_free(m);
         }
@@ -503,7 +557,7 @@ ble_ll_rx_start(struct os_mbuf *rxpdu, uint8_t chan)
             /* Set up to go from rx to tx */
             rc = 1;
         } else {
-            ++g_ble_ll_stats.bad_ll_state;
+            STATS_INC(ble_ll_stats, bad_ll_state);
             rc = 0;
         }
         return rc;
@@ -535,7 +589,7 @@ ble_ll_rx_start(struct os_mbuf *rxpdu, uint8_t chan)
     default:
         /* Should not be in this state! */
         rc = -1;
-        ++g_ble_ll_stats.bad_ll_state;
+        STATS_INC(ble_ll_stats, bad_ll_state);
         break;
     }
 
@@ -641,7 +695,7 @@ ble_ll_rx_end(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *ble_hdr)
 
         /* If this is a malformed packet, just kill it here */
         if (badpkt) {
-            ++g_ble_ll_stats.rx_adv_malformed_pkts;
+            STATS_INC(ble_ll_stats, rx_adv_malformed_pkts);
             os_mbuf_free(rxpdu);
             rxpdu = NULL;
             rc = -1;
@@ -873,7 +927,8 @@ ble_ll_reset(void)
     ble_ll_flush_pkt_queue(&g_ble_ll_data.ll_rx_pkt_q);
 
     /* Reset LL stats */
-    memset(&g_ble_ll_stats, 0, sizeof(struct ble_ll_stats));
+    memset((uint8_t *)&ble_ll_stats + sizeof(struct stats_hdr), 0, 
+           sizeof(struct stats_ble_ll_stats) - sizeof(struct stats_hdr));
 
 #ifdef BLE_LL_LOG
     g_ble_ll_log_index = 0;
@@ -903,6 +958,7 @@ ble_ll_reset(void)
 int
 ble_ll_init(void)
 {
+    int rc;
     uint8_t features;
     struct ble_ll_obj *lldata;
 
@@ -960,6 +1016,10 @@ ble_ll_init(void)
     os_task_init(&g_ble_ll_task, "ble_ll", ble_ll_task, NULL, BLE_LL_TASK_PRI, 
                  OS_WAIT_FOREVER, g_ble_ll_stack, BLE_LL_STACK_SIZE);
 
-    return 0;
+    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");
+    return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
index 9ee2ffa..7abc948 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -53,8 +53,6 @@
  * now, we set it to max.
  * 5) How does the advertising channel tx power get set? I dont implement
  * that currently.
- * 6) The time between transmissions inside an advertising event is set to
- * max. Need to deal with this.
  */
 
 /* 
@@ -96,17 +94,6 @@ struct ble_ll_adv_sm
 /* The advertising state machine global object */
 struct ble_ll_adv_sm g_ble_ll_adv_sm;
 
-struct ble_ll_adv_stats
-{
-    uint32_t late_starts;
-    uint32_t late_tx_done;
-    uint32_t cant_set_sched;
-    uint32_t scan_rsp_txg;
-    uint32_t adv_txg;
-};
-
-struct ble_ll_adv_stats g_ble_ll_adv_stats;
-
 /* 
  * 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
@@ -395,14 +382,14 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
         /* Check if we were late getting here */
         if ((int32_t)(start_time - (advsm->adv_pdu_start_time -
                cputime_usecs_to_ticks(XCVR_TX_START_DELAY_USECS))) > 0) {
-            ++g_ble_ll_adv_stats.late_starts;
+            STATS_INC(ble_ll_stats, adv_late_starts);
         }
 
         /* Set link layer state to advertising */
         ble_ll_state_set(BLE_LL_STATE_ADV);
 
         /* Count # of adv. sent */
-        ++g_ble_ll_adv_stats.adv_txg;
+        STATS_INC(ble_ll_stats, adv_txg);
 
         /* This schedule item is now running */
         rc = BLE_LL_SCHED_STATE_RUNNING;
@@ -897,7 +884,7 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
                         BLE_PHY_TRANSITION_NONE);
         if (!rc) {
             ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_SCAN_RSP_TXD;
-            ++g_ble_ll_adv_stats.scan_rsp_txg;
+            STATS_INC(ble_ll_stats, scan_rsp_txg);
         }
     }
 
@@ -1158,20 +1145,13 @@ ble_ll_adv_event_done(void *arg)
 
         /* 
          * The scheduled time better be in the future! If it is not, we will
-         * count a statistic and close the current advertising event. We will
-         * then setup the next advertising event.
+         * just keep advancing until we the time is in the future
          */
         start_time = advsm->adv_pdu_start_time - 
             cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
 
         delta_t = (int32_t)(start_time - cputime_get32());
         if (delta_t < 0) {
-            /* Count times we were late */
-            ++g_ble_ll_adv_stats.late_tx_done;
-
-            /* Set back to first adv channel */
-            advsm->adv_chan = ble_ll_adv_first_chan(advsm);
-
             /* Calculate start time of next advertising event */
             while (delta_t < 0) {
                 itvl = advsm->adv_itvl_usecs;
@@ -1285,9 +1265,6 @@ ble_ll_adv_reset(void)
     os_mbuf_free(advsm->adv_pdu);
     os_mbuf_free(advsm->scan_rsp_pdu);
 
-    /* Reset advertising state */
-    memset(&g_ble_ll_adv_stats, 0, sizeof(struct ble_ll_adv_stats));
-
     /* re-initialize the advertiser state machine */
     ble_ll_adv_init();
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn.c b/net/nimble/controller/src/ble_ll_conn.c
index 7dd18ad..b220cfa 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -135,28 +135,64 @@ 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;
 
-/* Statistics */
-struct ble_ll_conn_stats
-{
-    uint32_t cant_set_sched;
-    uint32_t conn_ev_late;
-    uint32_t wfr_expirations;
-    uint32_t handle_not_found;
-    uint32_t no_tx_pdu;
-    uint32_t no_conn_sm;
-    uint32_t no_free_conn_sm;
-    uint32_t rx_data_pdu_no_conn;
-    uint32_t rx_data_pdu_bad_aa;
-    uint32_t slave_rxd_bad_conn_req_params;
-    uint32_t slave_ce_failures;
-    uint32_t rx_resent_pdus;
-    uint32_t data_pdu_rx_dup;
-    uint32_t data_pdu_txg;
-    uint32_t data_pdu_txf;
-    uint32_t conn_req_txd;
-    uint32_t l2cap_enqueued;
-};
-struct ble_ll_conn_stats g_ble_ll_conn_stats;
+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_tx_pdu)
+    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(rx_resent_pdus)
+    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_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_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_tx_pdu)
+    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, rx_resent_pdus)
+    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_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_END(ble_ll_conn_stats)
 
 /* Some helpful macros */
 #define BLE_IS_RETRY_M(ble_hdr) ((ble_hdr)->txinfo.flags & BLE_MBUF_HDR_F_TXD)
@@ -279,7 +315,7 @@ ble_ll_conn_sm_get(void)
     if (connsm) {
         STAILQ_REMOVE_HEAD(&g_ble_ll_conn_free_list, free_stqe);
     } else {
-        ++g_ble_ll_conn_stats.no_free_conn_sm;
+        STATS_INC(ble_ll_conn_stats, no_free_conn_sm);
     }
 
     return connsm;
@@ -519,7 +555,7 @@ ble_ll_conn_wfr_timer_exp(void)
     ble_ll_conn_current_sm_over();
     if (connsm) {
         ble_ll_event_send(&connsm->conn_ev_end);
-        ++g_ble_ll_conn_stats.wfr_expirations;
+        STATS_INC(ble_ll_conn_stats, wfr_expirations);
     }
 }
 
@@ -702,10 +738,10 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm, int beg_transition)
     rc = ble_phy_tx(m, beg_transition, 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) | 
-                              ble_hdr->txinfo.pyld_len,
+                   ((uint16_t)ble_hdr->txinfo.offset << 8) | cur_txlen,
                    (uint32_t)m);
 
         /* Set flag denoting we transmitted a pdu */
@@ -716,13 +752,13 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm, int beg_transition)
 
         /* Increment packets transmitted */
         if (tx_empty_pdu) {
-            ++g_ble_ll_stats.tx_empty_pdus;
+            STATS_INC(ble_ll_conn_stats, tx_empty_pdus);
         } else if ((hdr_byte & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_CTRL) {
-            ++g_ble_ll_stats.tx_ctrl_pdus;
-            g_ble_ll_stats.tx_ctrl_bytes += ble_hdr->txinfo.pyld_len; 
+            STATS_INC(ble_ll_conn_stats, tx_ctrl_pdus);
+            STATS_INCN(ble_ll_conn_stats, tx_ctrl_bytes, cur_txlen);
         } else {
-            ++g_ble_ll_stats.tx_l2cap_pdus;
-            g_ble_ll_stats.tx_l2cap_bytes += ble_hdr->txinfo.pyld_len;
+            STATS_INC(ble_ll_conn_stats, tx_l2cap_pdus);
+            STATS_INCN(ble_ll_conn_stats, tx_l2cap_bytes, cur_txlen);
         }
     }
 
@@ -777,7 +813,7 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
         rc = ble_phy_rx();
         if (rc) {
             /* End the connection event as we have no more buffers */
-            ++g_ble_ll_conn_stats.slave_ce_failures;
+            STATS_INC(ble_ll_conn_stats, slave_ce_failures);
             rc = BLE_LL_SCHED_STATE_DONE;
         } else {
             /* 
@@ -1374,7 +1410,7 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime)
         /* Start the scheduler for the first connection event */
         while (ble_ll_sched_slave_new(connsm)) {
             if (ble_ll_conn_next_event(connsm)) {
-                ++g_ble_ll_conn_stats.cant_set_sched;
+                STATS_INC(ble_ll_conn_stats, cant_set_sched);
                 rc = 0;
                 break;
             }
@@ -1742,11 +1778,11 @@ ble_ll_init_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok)
                                           g_ble_ll_conn_create_sm->tx_win_off);
             if (!rc) {
                 ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CONN_REQ_TXD;
-                ++g_ble_ll_conn_stats.conn_req_txd;
+                STATS_INC(ble_ll_conn_stats, conn_req_txd);
             }
         } else {
             /* Count # of times we could not set schedule */
-            ++g_ble_ll_conn_stats.cant_set_sched;
+            STATS_INC(ble_ll_conn_stats, cant_set_sched);
         }
     }
 
@@ -1848,9 +1884,6 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
     struct ble_ll_conn_sm *connsm;
     
     if (BLE_MBUF_HDR_CRC_OK(hdr)) {
-        /* Count valid received data pdus */
-        ++g_ble_ll_stats.rx_valid_data_pdus;
-
         /* 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 */
@@ -1869,7 +1902,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
             /* Check that the LLID is reasonable */
             if ((acl_hdr == 0) || 
                 ((acl_hdr == BLE_LL_LLID_DATA_START) && (acl_len == 0))) {
-                ++g_ble_ll_stats.rx_bad_llid;
+                STATS_INC(ble_ll_conn_stats, rx_bad_llid);
                 goto conn_rx_data_pdu_end;
             }
 
@@ -1902,11 +1935,13 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
 
                 if (acl_hdr == BLE_LL_LLID_CTRL) {
                     /* Process control frame */
-                    ++g_ble_ll_stats.rx_ctrl_pdus;
-                    ble_ll_ctrl_rx_pdu(connsm, rxpdu);
+                    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 data frames */
-                    ++g_ble_ll_stats.rx_l2cap_pdus;
+                    STATS_INC(ble_ll_conn_stats, rx_l2cap_pdus);
 
                     /* NOTE: there should be at least two bytes available */
                     assert(OS_MBUF_LEADINGSPACE(rxpdu) >= 2);
@@ -1922,13 +1957,11 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                 /* NOTE: we dont free the mbuf since we handed it off! */
                 return;
             } else {
-                ++g_ble_ll_conn_stats.data_pdu_rx_dup;
+                STATS_INC(ble_ll_conn_stats, data_pdu_rx_dup);
             }
         } else {
-            ++g_ble_ll_conn_stats.no_conn_sm;
+            STATS_INC(ble_ll_conn_stats, no_conn_sm);
         }
-    } else {
-        ++g_ble_ll_stats.rx_invalid_data_pdus;
     }
 
     /* Free buffer */
@@ -1973,13 +2006,13 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu, uint32_t aa)
     rc = -1;
     connsm = g_ble_ll_conn_cur_sm;
     if (!connsm) {
-        ++g_ble_ll_conn_stats.rx_data_pdu_no_conn;
+        STATS_INC(ble_ll_conn_stats, rx_data_pdu_no_conn);
         goto conn_exit;
     }
 
     /* Double check access address. Better match connection state machine! */
     if (aa != connsm->access_addr) {
-        ++g_ble_ll_conn_stats.rx_data_pdu_bad_aa;
+        STATS_INC(ble_ll_conn_stats, rx_data_pdu_bad_aa);
         goto conn_exit;
     }
 
@@ -2029,7 +2062,7 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu, uint32_t aa)
             connsm->next_exp_seqnum ^= 1;
         } else {
             /* Count re-sent PDUs. */
-            ++g_ble_ll_conn_stats.rx_resent_pdus;
+            STATS_INC(ble_ll_conn_stats, rx_resent_pdus);
         }
 
         /* 
@@ -2041,11 +2074,11 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu, uint32_t aa)
             conn_sn = connsm->tx_seqnum;
             if ((hdr_nesn && conn_sn) || (!hdr_nesn && !conn_sn)) {
                 /* We did not get an ACK. Must retry the PDU */
-                ++g_ble_ll_conn_stats.data_pdu_txf;
+                STATS_INC(ble_ll_conn_stats, data_pdu_txf);
             } else {
                 /* Transmit success */
                 connsm->tx_seqnum ^= 1;
-                ++g_ble_ll_conn_stats.data_pdu_txg;
+                STATS_INC(ble_ll_conn_stats, data_pdu_txg);
 
                 /* 
                  * Determine if we should remove packet from queue or if there
@@ -2095,7 +2128,7 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu, uint32_t aa)
                     }
                 } else {
                     /* No packet on queue? This is an error! */
-                    ++g_ble_ll_conn_stats.no_tx_pdu;
+                    STATS_INC(ble_ll_conn_stats, no_tx_pdu);
                 }
             }
         }
@@ -2225,13 +2258,13 @@ ble_ll_conn_tx_pkt_in(struct os_mbuf *om, uint16_t handle, uint16_t length)
         }
 
         /* Add to total l2cap pdus enqueue */
-        ++g_ble_ll_conn_stats.l2cap_enqueued;
+        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! */
-        ++g_ble_ll_conn_stats.handle_not_found;
+        STATS_INC(ble_ll_conn_stats, handle_not_found);
         os_mbuf_free(om);
     }
 }
@@ -2389,7 +2422,7 @@ ble_ll_conn_slave_start(uint8_t *rxbuf, uint32_t conn_req_end)
 
 err_slave_start:
     STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
-    ++g_ble_ll_conn_stats.slave_rxd_bad_conn_req_params;
+    STATS_INC(ble_ll_conn_stats, slave_rxd_bad_conn_req_params);
     return 0;
 }
 
@@ -2403,7 +2436,9 @@ err_slave_start:
 void
 ble_ll_conn_reset(void)
 {
+    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) {
@@ -2421,19 +2456,38 @@ ble_ll_conn_reset(void)
         ble_ll_conn_end(connsm, BLE_ERR_SUCCESS);
     }
 
-    /* Call conn module init */
-    ble_ll_conn_module_init();
+    /* Configure the global LL parameters */
+    conn_params = &g_ble_ll_conn_params;
+    maxbytes = BLE_LL_CFG_SUPP_MAX_RX_BYTES + BLE_LL_DATA_MIC_LEN;
+    conn_params->supp_max_rx_time = BLE_TX_DUR_USECS_M(maxbytes);
+    conn_params->supp_max_rx_octets = BLE_LL_CFG_SUPP_MAX_RX_BYTES;
+
+    maxbytes = BLE_LL_CFG_SUPP_MAX_TX_BYTES + BLE_LL_DATA_MIC_LEN;
+    conn_params->supp_max_tx_time = BLE_TX_DUR_USECS_M(maxbytes);
+    conn_params->supp_max_tx_octets = BLE_LL_CFG_SUPP_MAX_TX_BYTES;
+
+    maxbytes = BLE_LL_CFG_CONN_INIT_MAX_TX_BYTES + BLE_LL_DATA_MIC_LEN;
+    conn_params->conn_init_max_tx_time = BLE_TX_DUR_USECS_M(maxbytes);
+    conn_params->conn_init_max_tx_octets = BLE_LL_CFG_CONN_INIT_MAX_TX_BYTES;
+
+    /* 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 */
+    memset((uint8_t *)&ble_ll_conn_stats + sizeof(struct stats_hdr), 0, 
+           sizeof(struct stats_ble_ll_conn_stats) - sizeof(struct stats_hdr));
 }
 
 /* Initialize the connection module */
 void
 ble_ll_conn_module_init(void)
 {
+    int rc;
     uint16_t i;
-    uint16_t maxbytes;
     struct os_mbuf *m;
     struct ble_ll_conn_sm *connsm;
-    struct ble_ll_conn_global_params *conn_params;
 
     /* Initialize list of active conections */
     SLIST_INIT(&g_ble_ll_conn_active_list);
@@ -2468,23 +2522,13 @@ ble_ll_conn_module_init(void)
         ++connsm;
     }
 
-    /* Configure the global LL parameters */
-    conn_params = &g_ble_ll_conn_params;
-    maxbytes = BLE_LL_CFG_SUPP_MAX_RX_BYTES + BLE_LL_DATA_MIC_LEN;
-    conn_params->supp_max_rx_time = BLE_TX_DUR_USECS_M(maxbytes);
-    conn_params->supp_max_rx_octets = BLE_LL_CFG_SUPP_MAX_RX_BYTES;
-
-    maxbytes = BLE_LL_CFG_SUPP_MAX_TX_BYTES + BLE_LL_DATA_MIC_LEN;
-    conn_params->supp_max_tx_time = BLE_TX_DUR_USECS_M(maxbytes);
-    conn_params->supp_max_tx_octets = BLE_LL_CFG_SUPP_MAX_TX_BYTES;
-
-    maxbytes = BLE_LL_CFG_CONN_INIT_MAX_TX_BYTES + BLE_LL_DATA_MIC_LEN;
-    conn_params->conn_init_max_tx_time = BLE_TX_DUR_USECS_M(maxbytes);
-    conn_params->conn_init_max_tx_octets = BLE_LL_CFG_CONN_INIT_MAX_TX_BYTES;
+    /* 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);
 
-    /* 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;
+    /* Call reset to finish reset of initialization */
+    ble_ll_conn_reset();
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_ctrl.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_ctrl.c b/net/nimble/controller/src/ble_ll_ctrl.c
index 7925a64..c79b380 100644
--- a/net/nimble/controller/src/ble_ll_ctrl.c
+++ b/net/nimble/controller/src/ble_ll_ctrl.c
@@ -1049,7 +1049,7 @@ ble_ll_ctrl_chk_proc_start(struct ble_ll_conn_sm *connsm)
  * @param om 
  * @param connsm 
  */
-void
+int
 ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om)
 {
     uint8_t features;
@@ -1231,12 +1231,11 @@ ll_ctrl_send_rsp:
         len = g_ble_ll_ctrl_pkt_lengths[rsp_opcode] + 1;
         ble_ll_conn_enqueue_pkt(connsm, om, BLE_LL_LLID_CTRL, len);
     }
-    return;
+    return 0;
 
 rx_malformed_ctrl:
     os_mbuf_free(om);
-    ++g_ble_ll_stats.rx_malformed_ctrl_pdus;
-    return;
+    return -1;
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c b/net/nimble/controller/src/ble_ll_hci.c
index a508871..0e97fdf 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -62,7 +62,7 @@ ble_ll_hci_event_send(uint8_t *evbuf)
     int rc;
 
     /* Count number of events sent */
-    ++g_ble_ll_stats.hci_events_sent;
+    STATS_INC(ble_ll_stats, hci_events_sent);
 
     /* Send the event to the host */
     rc = ble_hci_transport_ctlr_event_send(evbuf);
@@ -627,9 +627,9 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
 
     /* Count commands and those in error */
     if (rc) {
-        ++g_ble_ll_stats.hci_cmd_errs;
+        STATS_INC(ble_ll_stats, hci_cmd_errs);
     } else {
-        ++g_ble_ll_stats.hci_cmds;
+        STATS_INC(ble_ll_stats, hci_cmds);
     }
 
     /* Send the event (events cannot be masked) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_scan.c b/net/nimble/controller/src/ble_ll_scan.c
index 78320b8..292436e 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -53,17 +53,6 @@
 /* The scanning state machine global object */
 struct ble_ll_scan_sm g_ble_ll_scan_sm;
 
-/* Scanning stats */
-struct ble_ll_scan_stats
-{
-    uint32_t scan_starts;
-    uint32_t scan_stops;
-    uint32_t scan_req_txf;
-    uint32_t scan_req_txg;
-};
-
-struct ble_ll_scan_stats g_ble_ll_scan_stats;
-
 /* 
  * Structure used to store advertisers. This is used to limit sending scan
  * requests to the same advertiser and also to filter duplicate events sent
@@ -102,7 +91,7 @@ ble_ll_scan_req_backoff(struct ble_ll_scan_sm *scansm, int success)
                 scansm->upper_limit >>= 1;
             }
         }
-        ++g_ble_ll_scan_stats.scan_req_txg;
+        STATS_INC(ble_ll_stats, scan_req_txg);
     } else {
         scansm->scan_rsp_cons_ok = 0;
         ++scansm->scan_rsp_cons_fails;
@@ -112,7 +101,7 @@ ble_ll_scan_req_backoff(struct ble_ll_scan_sm *scansm, int success)
                 scansm->upper_limit <<= 1;
             }
         }
-        ++g_ble_ll_scan_stats.scan_req_txf;
+        STATS_INC(ble_ll_stats, scan_req_txf);
     }
 
     scansm->backoff_count = rand() & (scansm->upper_limit - 1);
@@ -591,7 +580,7 @@ ble_ll_scan_sm_stop(int chk_disable)
     scansm->scan_enabled = 0;
 
     /* Count # of times stopped */
-    ++g_ble_ll_scan_stats.scan_stops;
+    STATS_INC(ble_ll_stats, scan_stops);
 
     /* Only set state if we are currently in a scan window */
     if (chk_disable) {
@@ -639,7 +628,7 @@ ble_ll_scan_sm_start(struct ble_ll_scan_sm *scansm)
     }
 
     /* Count # of times started */
-    ++g_ble_ll_scan_stats.scan_starts;
+    STATS_INC(ble_ll_stats, scan_starts);
 
     /* Set flag telling us that scanning is enabled */
     scansm->scan_enabled = 1;
@@ -1278,9 +1267,6 @@ ble_ll_scan_reset(void)
         ble_ll_scan_sm_stop(0);
     }
 
-    /* Reset all statistics */
-    memset(&g_ble_ll_scan_stats, 0, sizeof(struct ble_ll_scan_stats));
-
     /* Free the scan request pdu */
     os_mbuf_free(scansm->scan_req_pdu);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/net/nimble/controller/src/ble_ll_sched.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_sched.c b/net/nimble/controller/src/ble_ll_sched.c
index c3b0436..dae0947 100644
--- a/net/nimble/controller/src/ble_ll_sched.c
+++ b/net/nimble/controller/src/ble_ll_sched.c
@@ -629,11 +629,11 @@ ble_ll_sched_execute_item(struct ble_ll_sched_item *sch)
             (lls == BLE_LL_STATE_INITIATING)) {
             ble_ll_state_set(BLE_LL_STATE_STANDBY);
         } else if (lls == BLE_LL_STATE_ADV) {
-            ++g_ble_ll_stats.sched_state_adv_errs;
+            STATS_INC(ble_ll_stats, sched_state_adv_errs);
             ble_ll_adv_halt();
         } else {
+            STATS_INC(ble_ll_stats, sched_state_conn_errs);
             ble_ll_conn_event_halt();
-            ++g_ble_ll_stats.sched_state_conn_errs;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/598b6293/project/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/project/bletest/src/main.c b/project/bletest/src/main.c
index 0000ffa..5625141 100755
--- a/project/bletest/src/main.c
+++ b/project/bletest/src/main.c
@@ -803,7 +803,8 @@ init_tasks(void)
     assert(rc == 0);
 
     /* Initialize the BLE LL */
-    ble_ll_init();
+    rc = ble_ll_init();
+    assert(rc == 0);
 
     return 0;
 }