From a1005bd9c5b439bf96fec2461c5a0b08cbc4bfac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Narajowski?= Date: Wed, 9 May 2018 14:16:57 +0200 Subject: [PATCH] porting: Add missing API functions used by Mesh --- nimble/include/nimble/nimble_npl.h | 12 ++++++ .../freertos/include/nimble/nimble_npl_os.h | 31 +++++++++++++++ .../freertos/include/nimble/npl_freertos.h | 5 +++ porting/npl/freertos/src/npl_os_freertos.c | 38 ++++++++++++++++--- .../npl/mynewt/include/nimble/nimble_npl_os.h | 32 ++++++++++++++++ 5 files changed, 113 insertions(+), 5 deletions(-) diff --git a/nimble/include/nimble/nimble_npl.h b/nimble/include/nimble/nimble_npl.h index 21d4c1e59..77e11f573 100644 --- a/nimble/include/nimble/nimble_npl.h +++ b/nimble/include/nimble/nimble_npl.h @@ -68,6 +68,9 @@ struct ble_npl_eventq *ble_npl_eventq_dflt_get(void); void ble_npl_eventq_init(struct ble_npl_eventq *evq); +struct ble_npl_event *ble_npl_eventq_get_tmo(struct ble_npl_eventq *evq, + ble_npl_time_t tmo); + struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq); void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev); @@ -86,6 +89,8 @@ void *ble_npl_event_get_arg(struct ble_npl_event *ev); void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg); +int ble_npl_eventq_is_empty(struct ble_npl_eventq *evq); + /* * Mutexes */ @@ -125,6 +130,11 @@ int ble_npl_callout_queued(struct ble_npl_callout *co); uint32_t ble_npl_callout_get_ticks(struct ble_npl_callout *co); +uint32_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co, + ble_npl_time_t time); + +void ble_npl_callout_set_arg(struct ble_npl_callout *co, + void *arg); /* * Time functions */ @@ -139,6 +149,8 @@ ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms); uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks); +void ble_npl_time_delay(ble_npl_time_t ticks); + /* * Hardware-specific * diff --git a/porting/npl/freertos/include/nimble/nimble_npl_os.h b/porting/npl/freertos/include/nimble/nimble_npl_os.h index 2b24556ae..cea2bc541 100644 --- a/porting/npl/freertos/include/nimble/nimble_npl_os.h +++ b/porting/npl/freertos/include/nimble/nimble_npl_os.h @@ -99,6 +99,12 @@ ble_npl_eventq_init(struct ble_npl_eventq *evq) evq->q = xQueueCreate(32, sizeof(struct ble_npl_eventq *)); } +static inline struct ble_npl_event * +ble_npl_eventq_get_tmo(struct ble_npl_eventq *evq, ble_npl_time_t tmo) +{ + return npl_freertos_eventq_get_tmo(evq, tmo); +} + static inline struct ble_npl_event * ble_npl_eventq_get(struct ble_npl_eventq *evq) { @@ -128,6 +134,12 @@ ble_npl_eventq_run(struct ble_npl_eventq *evq) ev->fn(ev); } +static inline int +ble_npl_eventq_is_empty(struct ble_npl_eventq *evq) +{ + return xQueueIsQueueEmptyFromISR(evq->q); +} + static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg) @@ -228,6 +240,19 @@ ble_npl_callout_get_ticks(struct ble_npl_callout *co) return xTimerGetExpiryTime(co->handle); } +static inline uint32_t +ble_npl_callout_remaining_ticks(struct ble_npl_callout *co, + ble_npl_time_t time) +{ + return npl_freertos_callout_remaining_ticks(co, time); +} + +static inline void +ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg) +{ + co->ev.arg = arg; +} + static inline uint32_t ble_npl_time_get(void) { @@ -258,6 +283,12 @@ ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks) return ticks * 1000 / configTICK_RATE_HZ; } +static inline void +ble_npl_time_delay(ble_npl_time_t ticks) +{ + vTaskDelay(ticks); +} + #if NIMBLE_CFG_CONTROLLER static inline void ble_npl_hw_set_isr(int irqn, uint32_t addr) diff --git a/porting/npl/freertos/include/nimble/npl_freertos.h b/porting/npl/freertos/include/nimble/npl_freertos.h index e9a2f3366..f11976843 100644 --- a/porting/npl/freertos/include/nimble/npl_freertos.h +++ b/porting/npl/freertos/include/nimble/npl_freertos.h @@ -26,6 +26,8 @@ extern "C" { struct ble_npl_eventq *npl_freertos_eventq_dflt_get(void); +struct ble_npl_event *npl_freertos_eventq_get_tmo(struct ble_npl_eventq *evq, ble_npl_time_t tmo); + struct ble_npl_event *npl_freertos_eventq_get(struct ble_npl_eventq *evq); void npl_freertos_eventq_put(struct ble_npl_eventq *evq, @@ -55,6 +57,9 @@ void npl_freertos_callout_init(struct ble_npl_callout *co, int npl_freertos_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks); +ble_npl_time_t npl_freertos_callout_remaining_ticks(struct ble_npl_callout *co, + ble_npl_time_t now); + ble_npl_error_t npl_freertos_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks); diff --git a/porting/npl/freertos/src/npl_os_freertos.c b/porting/npl/freertos/src/npl_os_freertos.c index 226ad6509..006064305 100644 --- a/porting/npl/freertos/src/npl_os_freertos.c +++ b/porting/npl/freertos/src/npl_os_freertos.c @@ -42,19 +42,27 @@ npl_freertos_eventq_dflt_get(void) } struct ble_npl_event * -npl_freertos_eventq_get(struct ble_npl_eventq *evq) +npl_freertos_eventq_get_tmo(struct ble_npl_eventq *evq, ble_npl_time_t tmo) { - struct ble_npl_event *ev; + struct ble_npl_event *ev = NULL; BaseType_t ret; - ret = xQueueReceive(evq->q, &ev, portMAX_DELAY); - assert(ret == pdPASS); + ret = xQueueReceive(evq->q, &ev, tmo); + assert(ret == pdPASS || ret == errQUEUE_EMPTY); - ev->queued = false; + if (ev) { + ev->queued = false; + } return ev; } +struct ble_npl_event * +npl_freertos_eventq_get(struct ble_npl_eventq *evq) +{ + return npl_freertos_eventq_get_tmo(evq, portMAX_DELAY); +} + void npl_freertos_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev) { @@ -281,6 +289,26 @@ npl_freertos_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks) return BLE_NPL_OK; } +ble_npl_time_t +npl_freertos_callout_remaining_ticks(struct ble_npl_callout *co, + ble_npl_time_t now) +{ + ble_npl_time_t rt; + uint32_t exp = xTimerGetExpiryTime(co->handle); + + taskENTER_CRITICAL(); + + if (exp > now) { + rt = exp - now; + } else { + return 0; + } + + taskEXIT_CRITICAL(); + + return rt; +} + ble_npl_error_t npl_freertos_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks) { diff --git a/porting/npl/mynewt/include/nimble/nimble_npl_os.h b/porting/npl/mynewt/include/nimble/nimble_npl_os.h index 9b825ddee..f7a47dd51 100644 --- a/porting/npl/mynewt/include/nimble/nimble_npl_os.h +++ b/porting/npl/mynewt/include/nimble/nimble_npl_os.h @@ -82,6 +82,12 @@ ble_npl_eventq_init(struct ble_npl_eventq *evq) os_eventq_init(&evq->evq); } +static inline struct ble_npl_event * +ble_npl_eventq_get_tmo(struct ble_npl_eventq *evq, ble_npl_time_t tmo) +{ + return (struct ble_npl_event *)os_eventq_poll((struct os_eventq **)&evq, 1, tmo); +} + static inline struct ble_npl_event * ble_npl_eventq_get(struct ble_npl_eventq *evq) { @@ -107,6 +113,12 @@ ble_npl_eventq_run(struct ble_npl_eventq *evq) os_eventq_run(&evq->evq); } +static inline int +ble_npl_eventq_is_empty(struct ble_npl_eventq *evq) +{ + return STAILQ_EMPTY(&evq->evq.evq_list); +} + static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg) @@ -208,6 +220,20 @@ ble_npl_callout_get_ticks(struct ble_npl_callout *co) return co->co.c_ticks; } +static inline ble_npl_time_t +ble_npl_callout_remaining_ticks(struct ble_npl_callout *co, + ble_npl_time_t time) +{ + return os_callout_remaining_ticks(&co->co, time); +} + +static inline void +ble_npl_callout_set_arg(struct ble_npl_callout *co, + void *arg) +{ + co->co.c_ev.ev_arg = arg; +} + static inline uint32_t ble_npl_time_get(void) { @@ -238,6 +264,12 @@ ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks) return os_time_ticks_to_ms32(ticks); } +static inline void +ble_npl_time_delay(ble_npl_time_t ticks) +{ + return os_time_delay(ticks); +} + static inline uint32_t ble_npl_hw_enter_critical(void) {