From 66eaa96c2fa7ca3ef7916c88eca8dad00044cf17 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 14:28:15 +0200 Subject: [PATCH 1/6] nimble/ll: Fix build with DTM enabled #include<> is used to search for system headers so it does not look in current directory. This failed when building FreeRTOS port, not sure why does not fail on Mynewt. --- nimble/controller/src/ble_ll.c | 2 +- nimble/controller/src/ble_ll_hci.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nimble/controller/src/ble_ll.c b/nimble/controller/src/ble_ll.c index d1d2a2c7d..b0d80fffe 100644 --- a/nimble/controller/src/ble_ll.c +++ b/nimble/controller/src/ble_ll.c @@ -43,7 +43,7 @@ #include "ble_ll_conn_priv.h" #if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1 -#include +#include "ble_ll_dtm_priv.h" #endif /* XXX: diff --git a/nimble/controller/src/ble_ll_hci.c b/nimble/controller/src/ble_ll_hci.c index 91ceb228d..aa0eb32e7 100644 --- a/nimble/controller/src/ble_ll_hci.c +++ b/nimble/controller/src/ble_ll_hci.c @@ -35,7 +35,7 @@ #include "ble_ll_conn_priv.h" #if MYNEWT_VAL(BLE_LL_DIRECT_TEST_MODE) == 1 -#include +#include "ble_ll_dtm_priv.h" #endif static void ble_ll_hci_cmd_proc(struct ble_npl_event *ev); From 421daeff07e677a3bfcdde1fb682feecd18a89c1 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 14:28:29 +0200 Subject: [PATCH 2/6] nimble/ll: Fix build with ext adv enabled rand() needs stdlib.h This failed when building FreeRTOS port. --- nimble/controller/src/ble_ll_adv.c | 1 + 1 file changed, 1 insertion(+) diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c index 3fa0c7706..4654cb267 100644 --- a/nimble/controller/src/ble_ll_adv.c +++ b/nimble/controller/src/ble_ll_adv.c @@ -17,6 +17,7 @@ * under the License. */ #include +#include #include #include #include "syscfg/syscfg.h" From 961fff56dbaec959c24451a9390b72686c1df592 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 14:29:48 +0200 Subject: [PATCH 3/6] nimble/host: Fix missing NPL usage Seems like we never tried to build ported NimBLE with some of features enabled thus there were still some OS calls left. --- nimble/host/src/ble_gap.c | 4 ++-- nimble/host/src/ble_hs.c | 16 ++++++++++------ nimble/host/src/ble_hs_flow.c | 26 +++++++++++++------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c index 7ab326395..cb75e878f 100644 --- a/nimble/host/src/ble_gap.c +++ b/nimble/host/src/ble_gap.c @@ -3528,7 +3528,7 @@ ble_gap_ext_connect(uint8_t own_addr_type, const ble_addr_t *peer_addr, return BLE_HS_ENOTSUP; #endif - uint32_t duration_ticks; + ble_npl_time_t duration_ticks; int rc; STATS_INC(ble_gap_stats, initiate); @@ -3584,7 +3584,7 @@ ble_gap_ext_connect(uint8_t own_addr_type, const ble_addr_t *peer_addr, } if (duration_ms != BLE_HS_FOREVER) { - rc = os_time_ms_to_ticks(duration_ms, &duration_ticks); + rc = ble_npl_time_ms_to_ticks(duration_ms, &duration_ticks); if (rc != 0) { /* Duration too great. */ rc = BLE_HS_EINVAL; diff --git a/nimble/host/src/ble_hs.c b/nimble/host/src/ble_hs.c index 62b7e4855..c4ffd949c 100644 --- a/nimble/host/src/ble_hs.c +++ b/nimble/host/src/ble_hs.c @@ -117,14 +117,18 @@ ble_hs_evq_set(struct ble_npl_eventq *evq) int ble_hs_locked_by_cur_task(void) { +#if MYNEWT struct os_task *owner; - if (!os_started()) { + if (!ble_npl_os_started()) { return ble_hs_dbg_mutex_locked; } owner = ble_hs_mutex.mu.mu_owner; return owner != NULL && owner == os_sched_get_current_task(); +#else + return 1; +#endif } #endif @@ -147,7 +151,7 @@ ble_hs_lock_nested(void) int rc; #if MYNEWT_VAL(BLE_HS_DEBUG) - if (!os_started()) { + if (!ble_npl_os_started()) { ble_hs_dbg_mutex_locked = 1; return; } @@ -166,7 +170,7 @@ ble_hs_unlock_nested(void) int rc; #if MYNEWT_VAL(BLE_HS_DEBUG) - if (!os_started()) { + if (!ble_npl_os_started()) { ble_hs_dbg_mutex_locked = 0; return; } @@ -184,7 +188,7 @@ ble_hs_lock(void) { BLE_HS_DBG_ASSERT(!ble_hs_locked_by_cur_task()); #if MYNEWT_VAL(BLE_HS_DEBUG) - if (!os_started()) { + if (!ble_npl_os_started()) { BLE_HS_DBG_ASSERT(!ble_hs_dbg_mutex_locked); } #endif @@ -199,7 +203,7 @@ void ble_hs_unlock(void) { #if MYNEWT_VAL(BLE_HS_DEBUG) - if (!os_started()) { + if (!ble_npl_os_started()) { BLE_HS_DBG_ASSERT(ble_hs_dbg_mutex_locked); } #endif @@ -517,7 +521,7 @@ void ble_hs_notifications_sched(void) { #if !MYNEWT_VAL(BLE_HS_REQUIRE_OS) - if (!os_started()) { + if (!ble_npl_os_started()) { ble_gatts_tx_notifications(); return; } diff --git a/nimble/host/src/ble_hs_flow.c b/nimble/host/src/ble_hs_flow.c index ca93389e8..1f3e4eee8 100644 --- a/nimble/host/src/ble_hs_flow.c +++ b/nimble/host/src/ble_hs_flow.c @@ -24,7 +24,7 @@ #if MYNEWT_VAL(BLE_HS_FLOW_CTRL) #define BLE_HS_FLOW_ITVL_TICKS \ - (MYNEWT_VAL(BLE_HS_FLOW_CTRL_ITVL) * OS_TICKS_PER_SEC / 1000) + ble_npl_time_ms_to_ticks32(MYNEWT_VAL(BLE_HS_FLOW_CTRL_ITVL)) /** * The number of freed buffers since the most-recent @@ -34,13 +34,11 @@ static uint16_t ble_hs_flow_num_completed_pkts; /** Periodically sends number-of-completed-packets events. */ -static struct os_callout ble_hs_flow_timer; +static struct ble_npl_callout ble_hs_flow_timer; -static os_event_fn ble_hs_flow_event_cb; +static ble_npl_event_fn ble_hs_flow_event_cb; -static struct os_event ble_hs_flow_ev = { - .ev_cb = ble_hs_flow_event_cb, -}; +static struct ble_npl_event ble_hs_flow_ev; static int ble_hs_flow_tx_num_comp_pkts(void) @@ -95,7 +93,7 @@ ble_hs_flow_tx_num_comp_pkts(void) } static void -ble_hs_flow_event_cb(struct os_event *ev) +ble_hs_flow_event_cb(struct ble_npl_event *ev) { int rc; @@ -135,10 +133,10 @@ ble_hs_flow_inc_completed_pkts(struct ble_hs_conn *conn) */ num_free = MYNEWT_VAL(BLE_ACL_BUF_COUNT) - ble_hs_flow_num_completed_pkts; if (num_free <= MYNEWT_VAL(BLE_HS_FLOW_CTRL_THRESH)) { - os_eventq_put(ble_hs_evq_get(), &ble_hs_flow_ev); - os_callout_stop(&ble_hs_flow_timer); + ble_npl_eventq_put(ble_hs_evq_get(), &ble_hs_flow_ev); + ble_npl_callout_stop(&ble_hs_flow_timer); } else if (ble_hs_flow_num_completed_pkts == 1) { - rc = os_callout_reset(&ble_hs_flow_timer, BLE_HS_FLOW_ITVL_TICKS); + rc = ble_npl_callout_reset(&ble_hs_flow_timer, BLE_HS_FLOW_ITVL_TICKS); BLE_HS_DBG_ASSERT_EVAL(rc == 0); } } @@ -229,9 +227,11 @@ ble_hs_flow_startup(void) struct hci_host_buf_size buf_size_cmd; int rc; + ble_npl_event_init(&ble_hs_flow_ev, ble_hs_flow_event_cb, NULL); + /* Assume failure. */ ble_hci_trans_set_acl_free_cb(NULL, NULL); - os_callout_stop(&ble_hs_flow_timer); + ble_npl_callout_stop(&ble_hs_flow_timer); rc = ble_hs_hci_cmd_tx_set_ctlr_to_host_fc(BLE_HCI_CTLR_TO_HOST_FC_ACL); if (rc != 0) { @@ -251,8 +251,8 @@ ble_hs_flow_startup(void) /* Flow control successfully enabled. */ ble_hs_flow_num_completed_pkts = 0; ble_hci_trans_set_acl_free_cb(ble_hs_flow_acl_free, NULL); - os_callout_init(&ble_hs_flow_timer, ble_hs_evq_get(), - ble_hs_flow_event_cb, NULL); + ble_npl_callout_init(&ble_hs_flow_timer, ble_hs_evq_get(), + ble_hs_flow_event_cb, NULL); #endif return 0; From 916cada7b43b0c729cf6d8d2282e4edae58cf2da Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 14:37:35 +0200 Subject: [PATCH 4/6] nimble/host: Fix build with host flow enabled Seems like something was broken when merging... --- nimble/host/src/ble_hs_flow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nimble/host/src/ble_hs_flow.c b/nimble/host/src/ble_hs_flow.c index 1f3e4eee8..c0f9f5097 100644 --- a/nimble/host/src/ble_hs_flow.c +++ b/nimble/host/src/ble_hs_flow.c @@ -82,7 +82,7 @@ ble_hs_flow_tx_num_comp_pkts(void) rc = ble_hs_hci_cmd_send_buf( BLE_HCI_OP(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_HOST_NUM_COMP_PKTS), - buf, off); + buf, sizeof(buf)); if (rc != 0) { return rc; } From 75a8801cd8ec3f804489c9fd515e33b875d0ee28 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 14:32:58 +0200 Subject: [PATCH 5/6] porting: Update os_mempool Perhaps we need to figure out better way to sync these files with core, but for now let's do this manually. Anyway, I do not expect this to happen very often. --- porting/nimble/include/os/os_mempool.h | 10 ++++++ porting/nimble/src/os_mempool.c | 45 ++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/porting/nimble/include/os/os_mempool.h b/porting/nimble/include/os/os_mempool.h index db80d4e40..c69fb3da9 100644 --- a/porting/nimble/include/os/os_mempool.h +++ b/porting/nimble/include/os/os_mempool.h @@ -30,6 +30,7 @@ #include #include "os/os.h" +#include "os/queue.h" #ifdef __cplusplus extern "C" { @@ -188,6 +189,15 @@ os_error_t os_mempool_init(struct os_mempool *mp, uint16_t blocks, os_error_t os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks, uint32_t block_size, void *membuf, char *name); +/** + * Clears a memory pool. + * + * @param mp The mempool to clear. + * + * @return os_error_t + */ +os_error_t os_mempool_clear(struct os_mempool *mp); + /** * Performs an integrity check of the specified mempool. This function * attempts to detect memory corruption in the specified memory pool. diff --git a/porting/nimble/src/os_mempool.c b/porting/nimble/src/os_mempool.c index fa223a7aa..bce78ef81 100644 --- a/porting/nimble/src/os_mempool.c +++ b/porting/nimble/src/os_mempool.c @@ -136,6 +136,45 @@ os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks, return 0; } +os_error_t +os_mempool_clear(struct os_mempool *mp) +{ + struct os_memblock *block_ptr; + int true_block_size; + uint8_t *block_addr; + uint16_t blocks; + + if (!mp) { + return OS_INVALID_PARM; + } + + true_block_size = OS_MEM_TRUE_BLOCK_SIZE(mp->mp_block_size); + + /* cleanup the memory pool structure */ + mp->mp_num_free = mp->mp_num_blocks; + mp->mp_min_free = mp->mp_num_blocks; + os_mempool_poison((void *)mp->mp_membuf_addr, true_block_size); + SLIST_FIRST(mp) = (void *)mp->mp_membuf_addr; + + /* Chain the memory blocks to the free list */ + block_addr = (uint8_t *)mp->mp_membuf_addr; + block_ptr = (struct os_memblock *)block_addr; + blocks = mp->mp_num_blocks; + + while (blocks > 1) { + block_addr += true_block_size; + os_mempool_poison(block_addr, true_block_size); + SLIST_NEXT(block_ptr, mb_next) = (struct os_memblock *)block_addr; + block_ptr = (struct os_memblock *)block_addr; + --blocks; + } + + /* Last one in the list should be NULL */ + SLIST_NEXT(block_ptr, mb_next) = NULL; + + return OS_OK; +} + bool os_mempool_is_sane(const struct os_mempool *mp) { @@ -194,8 +233,6 @@ os_memblock_get(struct os_mempool *mp) /* Get a free block */ block = SLIST_FIRST(mp); - os_mempool_poison_check(block, OS_MEMPOOL_TRUE_BLOCK_SIZE(mp)); - /* Set new free list head */ SLIST_FIRST(mp) = SLIST_NEXT(block, mb_next); @@ -206,6 +243,10 @@ os_memblock_get(struct os_mempool *mp) } } OS_EXIT_CRITICAL(sr); + + if (block) { + os_mempool_poison_check(block, OS_MEMPOOL_TRUE_BLOCK_SIZE(mp)); + } } return (void *)block; From b1855ebb61b7936f6db95da47cc8ce12e30fe5a2 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 24 May 2018 16:04:30 +0200 Subject: [PATCH 6/6] porting: Fix unused parameter warning --- porting/nimble/include/log/log.h | 1 + 1 file changed, 1 insertion(+) diff --git a/porting/nimble/include/log/log.h b/porting/nimble/include/log/log.h index 3e3fc8b42..04e591023 100644 --- a/porting/nimble/include/log/log.h +++ b/porting/nimble/include/log/log.h @@ -27,6 +27,7 @@ extern "C" { static inline void log_dummy(void *log, ...) { + (void)log; } #define LOG_DEBUG(_log, _mod, ...) log_dummy(_log, ## __VA_ARGS__)