mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
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 _enabled = 0;
|
||||
|
||||
static bluetil_ad_t _ad;
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
if (_filter_uuid(&ad) && !nimble_netif_conn_connected(addrn)) {
|
||||
nimble_autoconn_disable();
|
||||
nimble_scanner_stop();
|
||||
DEBUG("[autoconn] SCAN success, initiating connection\n");
|
||||
_state = STATE_CONN;
|
||||
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,
|
||||
const uint8_t *addr)
|
||||
{
|
||||
int en = 1;
|
||||
|
||||
switch (event) {
|
||||
case NIMBLE_NETIF_ACCEPTING:
|
||||
en = 0;
|
||||
_evt_dbg("ACCEPTING", handle, addr);
|
||||
break;
|
||||
case NIMBLE_NETIF_ACCEPT_STOP:
|
||||
@ -173,11 +192,9 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
||||
break;
|
||||
case NIMBLE_NETIF_INIT_MASTER:
|
||||
_evt_dbg("CONN_INIT master", handle, addr);
|
||||
en = 0;
|
||||
break;
|
||||
case NIMBLE_NETIF_INIT_SLAVE:
|
||||
_evt_dbg("CONN_INIT slave", handle, addr);
|
||||
en = 0;
|
||||
_state = STATE_CONN;
|
||||
break;
|
||||
case NIMBLE_NETIF_CONNECTED_MASTER:
|
||||
@ -206,7 +223,6 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
||||
break;
|
||||
case NIMBLE_NETIF_CONN_UPDATED:
|
||||
_evt_dbg("UPDATED", handle, addr);
|
||||
en = 0;
|
||||
break;
|
||||
default:
|
||||
/* this should never happen */
|
||||
@ -218,10 +234,8 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event,
|
||||
_eventcb(handle, event, addr);
|
||||
}
|
||||
|
||||
/* search for the next connection possibility */
|
||||
if (en) {
|
||||
nimble_autoconn_enable();
|
||||
}
|
||||
/* if active and if free slots are available, search for new peers */
|
||||
_activate();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (nimble_netif_conn_count(NIMBLE_NETIF_UNUSED) > 0) {
|
||||
DEBUG("[autoconn] ACTIVE\n");
|
||||
/* 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);
|
||||
}
|
||||
_enabled = 1;
|
||||
_activate();
|
||||
}
|
||||
|
||||
void nimble_autoconn_disable(void)
|
||||
{
|
||||
if ((_state == STATE_ADV) || (_state == STATE_SCAN)) {
|
||||
DEBUG("[autoconn] DISABLED\n");
|
||||
_state = STATE_IDLE;
|
||||
ble_npl_callout_stop(&_state_evt);
|
||||
nimble_scanner_stop();
|
||||
nimble_netif_accept_stop();
|
||||
}
|
||||
_enabled = 0;
|
||||
_deactivate();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user