pkg/nimble/autoconn: improve en/disable code
This commit is contained in:
parent
fddcd928d7
commit
6537a04b97
@ -50,6 +50,7 @@ enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static volatile uint8_t _state = STATE_IDLE;
|
static volatile uint8_t _state = STATE_IDLE;
|
||||||
|
static volatile uint8_t _enabled = 0;
|
||||||
|
|
||||||
static bluetil_ad_t _ad;
|
static bluetil_ad_t _ad;
|
||||||
static uint8_t _ad_buf[BLE_HS_ADV_MAX_SZ];
|
static uint8_t _ad_buf[BLE_HS_ADV_MAX_SZ];
|
||||||
@ -94,6 +95,27 @@ static void _on_state_change(struct ble_npl_event *ev)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _activate(void)
|
||||||
|
{
|
||||||
|
if (_enabled && (_state == STATE_IDLE) &&
|
||||||
|
(nimble_netif_conn_count(NIMBLE_NETIF_UNUSED) > 0)) {
|
||||||
|
_state = STATE_SCAN;
|
||||||
|
_on_state_change(NULL);
|
||||||
|
DEBUG("[autoconn] ACTIVATED\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _deactivate(void)
|
||||||
|
{
|
||||||
|
if ((_state == STATE_ADV) || (_state == STATE_SCAN)) {
|
||||||
|
ble_npl_callout_stop(&_state_evt);
|
||||||
|
nimble_scanner_stop();
|
||||||
|
nimble_netif_accept_stop();
|
||||||
|
_state = STATE_IDLE;
|
||||||
|
DEBUG("[autoconn] DEACTIVATED\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int _filter_uuid(const bluetil_ad_t *ad)
|
static int _filter_uuid(const bluetil_ad_t *ad)
|
||||||
{
|
{
|
||||||
bluetil_ad_data_t incomp;
|
bluetil_ad_data_t incomp;
|
||||||
@ -131,7 +153,7 @@ static void _on_scan_evt(uint8_t type, const ble_addr_t *addr, int8_t rssi,
|
|||||||
bluetil_addr_swapped_cp(addr->val, addrn);
|
bluetil_addr_swapped_cp(addr->val, addrn);
|
||||||
|
|
||||||
if (_filter_uuid(&ad) && !nimble_netif_conn_connected(addrn)) {
|
if (_filter_uuid(&ad) && !nimble_netif_conn_connected(addrn)) {
|
||||||
nimble_autoconn_disable();
|
nimble_scanner_stop();
|
||||||
DEBUG("[autoconn] SCAN success, initiating connection\n");
|
DEBUG("[autoconn] SCAN success, initiating connection\n");
|
||||||
_state = STATE_CONN;
|
_state = STATE_CONN;
|
||||||
int res = nimble_netif_connect(addr, &_conn_params, _conn_timeout);
|
int res = nimble_netif_connect(addr, &_conn_params, _conn_timeout);
|
||||||
@ -161,11 +183,8 @@ static void _evt_dbg(const char *msg, int handle, const uint8_t *addr)
|
|||||||
static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
||||||
const uint8_t *addr)
|
const uint8_t *addr)
|
||||||
{
|
{
|
||||||
int en = 1;
|
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case NIMBLE_NETIF_ACCEPTING:
|
case NIMBLE_NETIF_ACCEPTING:
|
||||||
en = 0;
|
|
||||||
_evt_dbg("ACCEPTING", handle, addr);
|
_evt_dbg("ACCEPTING", handle, addr);
|
||||||
break;
|
break;
|
||||||
case NIMBLE_NETIF_ACCEPT_STOP:
|
case NIMBLE_NETIF_ACCEPT_STOP:
|
||||||
@ -173,11 +192,9 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
|||||||
break;
|
break;
|
||||||
case NIMBLE_NETIF_INIT_MASTER:
|
case NIMBLE_NETIF_INIT_MASTER:
|
||||||
_evt_dbg("CONN_INIT master", handle, addr);
|
_evt_dbg("CONN_INIT master", handle, addr);
|
||||||
en = 0;
|
|
||||||
break;
|
break;
|
||||||
case NIMBLE_NETIF_INIT_SLAVE:
|
case NIMBLE_NETIF_INIT_SLAVE:
|
||||||
_evt_dbg("CONN_INIT slave", handle, addr);
|
_evt_dbg("CONN_INIT slave", handle, addr);
|
||||||
en = 0;
|
|
||||||
_state = STATE_CONN;
|
_state = STATE_CONN;
|
||||||
break;
|
break;
|
||||||
case NIMBLE_NETIF_CONNECTED_MASTER:
|
case NIMBLE_NETIF_CONNECTED_MASTER:
|
||||||
@ -206,7 +223,6 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
|||||||
break;
|
break;
|
||||||
case NIMBLE_NETIF_CONN_UPDATED:
|
case NIMBLE_NETIF_CONN_UPDATED:
|
||||||
_evt_dbg("UPDATED", handle, addr);
|
_evt_dbg("UPDATED", handle, addr);
|
||||||
en = 0;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* this should never happen */
|
/* this should never happen */
|
||||||
@ -218,10 +234,8 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
|||||||
_eventcb(handle, event, addr);
|
_eventcb(handle, event, addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* search for the next connection possibility */
|
/* if active and if free slots are available, search for new peers */
|
||||||
if (en) {
|
_activate();
|
||||||
nimble_autoconn_enable();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _conn_update(nimble_netif_conn_t *conn, int handle, void *arg)
|
static int _conn_update(nimble_netif_conn_t *conn, int handle, void *arg)
|
||||||
@ -334,23 +348,12 @@ int nimble_autoconn_update(const nimble_autoconn_params_t *params,
|
|||||||
|
|
||||||
void nimble_autoconn_enable(void)
|
void nimble_autoconn_enable(void)
|
||||||
{
|
{
|
||||||
if (nimble_netif_conn_count(NIMBLE_NETIF_UNUSED) > 0) {
|
_enabled = 1;
|
||||||
DEBUG("[autoconn] ACTIVE\n");
|
_activate();
|
||||||
/* insert a random delay */
|
|
||||||
ble_npl_time_t delay = (ble_npl_time_t)random_uint32_range(0,
|
|
||||||
(uint32_t)_period_jitter);
|
|
||||||
_state = STATE_ADV;
|
|
||||||
ble_npl_callout_reset(&_state_evt, delay);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void nimble_autoconn_disable(void)
|
void nimble_autoconn_disable(void)
|
||||||
{
|
{
|
||||||
if ((_state == STATE_ADV) || (_state == STATE_SCAN)) {
|
_enabled = 0;
|
||||||
DEBUG("[autoconn] DISABLED\n");
|
_deactivate();
|
||||||
_state = STATE_IDLE;
|
|
||||||
ble_npl_callout_stop(&_state_evt);
|
|
||||||
nimble_scanner_stop();
|
|
||||||
nimble_netif_accept_stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user