From 16468acadc13f25444adffd946870afae15f4c47 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Wed, 18 Dec 2019 16:21:39 +0100 Subject: [PATCH] pkg/nimble/autoconn: add user event callback --- pkg/nimble/autoconn/include/nimble_autoconn.h | 12 +++++++++ pkg/nimble/autoconn/nimble_autoconn.c | 26 ++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pkg/nimble/autoconn/include/nimble_autoconn.h b/pkg/nimble/autoconn/include/nimble_autoconn.h index d858e75022..c3e28360d7 100644 --- a/pkg/nimble/autoconn/include/nimble_autoconn.h +++ b/pkg/nimble/autoconn/include/nimble_autoconn.h @@ -111,6 +111,8 @@ #include +#include "nimble_netif.h" + #ifdef __cplusplus extern "C" { #endif @@ -168,6 +170,16 @@ typedef struct { int nimble_autoconn_init(const nimble_autoconn_params_t *params, const uint8_t *ad, size_t adlen); +/** + * @brief Register a callback that is called on netif events + * + * The registered callback function is a simple pass-through of nimble_netif + * events. The callback is executed in the context of NimBLE's host thread. + * + * @param[in] cb event callback to register, may be NULL + */ +void nimble_autoconn_eventcb(nimble_netif_eventcb_t cb); + /** * @brief Update the used parameters (timing and node ID) * diff --git a/pkg/nimble/autoconn/nimble_autoconn.c b/pkg/nimble/autoconn/nimble_autoconn.c index f846ab17e1..e755fb1601 100644 --- a/pkg/nimble/autoconn/nimble_autoconn.c +++ b/pkg/nimble/autoconn/nimble_autoconn.c @@ -65,6 +65,8 @@ static ble_npl_time_t _timeout_adv_period; static ble_npl_time_t _timeout_scan_period; static ble_npl_time_t _period_jitter; +static nimble_netif_eventcb_t _eventcb = NULL; + /* this is run inside the NimBLE host thread */ static void _on_state_change(struct ble_npl_event *ev) { @@ -143,39 +145,46 @@ static void _on_scan_evt(uint8_t type, const ble_addr_t *addr, int8_t rssi, static void _on_netif_evt(int handle, nimble_netif_event_t event, const uint8_t *addr) { + int en = 1; + switch (event) { case NIMBLE_NETIF_CONNECTED_MASTER: DEBUG("[autoconn] CONNECTED as master %i\n", handle); assert(_state == STATE_CONN); _state = STATE_IDLE; - nimble_autoconn_enable(); break; case NIMBLE_NETIF_CONNECTED_SLAVE: DEBUG("[autoconn] CONNECTED as slave %i\n", handle); - nimble_autoconn_enable(); break; case NIMBLE_NETIF_CLOSED_MASTER: DEBUG("[autoconn] CLOSED master connection\n"); - nimble_autoconn_enable(); break; case NIMBLE_NETIF_CLOSED_SLAVE: DEBUG("[autoconn] CLOSED slave connection\n"); - nimble_autoconn_enable(); break; case NIMBLE_NETIF_CONNECT_ABORT: DEBUG("[autoconn] CONNECT ABORT\n"); assert(_state == STATE_CONN); _state = STATE_IDLE; - nimble_autoconn_enable(); break; case NIMBLE_NETIF_CONN_UPDATED: DEBUG("[autoconn] CONNECTION UPDATED %i\n", handle); - /* nothing to do here */ + en = 0; break; default: /* this should never happen */ assert(0); } + + /* pass events to high-level user if someone registered for them */ + if (_eventcb) { + _eventcb(handle, event, addr); + } + + /* search for the next connection possibility */ + if (en) { + nimble_autoconn_enable(); + } } static int _conn_update(nimble_netif_conn_t *conn, int handle, void *arg) @@ -197,6 +206,11 @@ int nimble_autoconn_init(const nimble_autoconn_params_t *params, return nimble_autoconn_update(params, ad, adlen); } +void nimble_autoconn_eventcb(nimble_netif_eventcb_t cb) +{ + _eventcb = cb; +} + int nimble_autoconn_update(const nimble_autoconn_params_t *params, const uint8_t *ad, size_t adlen) {