diff --git a/pkg/nimble/autoconn/nimble_autoconn.c b/pkg/nimble/autoconn/nimble_autoconn.c index 9fff497653..fc5b510037 100644 --- a/pkg/nimble/autoconn/nimble_autoconn.c +++ b/pkg/nimble/autoconn/nimble_autoconn.c @@ -134,11 +134,11 @@ static void _on_scan_evt(uint8_t type, const ble_addr_t *addr, int8_t rssi, if (_filter_uuid(&ad) && !nimble_netif_conn_connected(addrn)) { nimble_autoconn_disable(); + DEBUG("[autoconn] SCAN success, initiating connection\n"); _state = STATE_CONN; int res = nimble_netif_connect(addr, &_conn_params, _conn_timeout); assert(res >= 0); (void)res; - DEBUG("[autoconn] SCAN success, initiating connection\n"); } } @@ -146,7 +146,12 @@ static void _evt_dbg(const char *msg, int handle, const uint8_t *addr) { #if ENABLE_DEBUG printf("[autoconn] %s (%i|", msg, handle); - bluetil_addr_print(addr); + if (addr) { + bluetil_addr_print(addr); + } + else { + printf("n/a"); + } puts(")"); #else (void)msg; @@ -161,6 +166,17 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event, int en = 1; switch (event) { + case NIMBLE_NETIF_ACCEPTING: + en = 0; + 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; + break; case NIMBLE_NETIF_CONNECTED_MASTER: _evt_dbg("CONNECTED master", handle, addr); assert(_state == STATE_CONN); @@ -175,11 +191,14 @@ static void _on_netif_evt(int handle, nimble_netif_event_t event, case NIMBLE_NETIF_CLOSED_SLAVE: _evt_dbg("CLOSED slave", handle, addr); break; - case NIMBLE_NETIF_CONNECT_ABORT: - _evt_dbg("ABORTED", handle, addr); + case NIMBLE_NETIF_ABORT_MASTER: + _evt_dbg("ABORT master", handle, addr); assert(_state == STATE_CONN); _state = STATE_IDLE; break; + case NIMBLE_NETIF_ABORT_SLAVE: + _evt_dbg("[autoconn] ABORT slave", handle, addr); + break; case NIMBLE_NETIF_CONN_UPDATED: _evt_dbg("UPDATED", handle, addr); en = 0; diff --git a/pkg/nimble/netif/include/nimble_netif.h b/pkg/nimble/netif/include/nimble_netif.h index 1e8635cfb9..9ff6af513d 100644 --- a/pkg/nimble/netif/include/nimble_netif.h +++ b/pkg/nimble/netif/include/nimble_netif.h @@ -119,11 +119,15 @@ enum { * @brief Event types triggered by the NimBLE netif module */ typedef enum { + NIMBLE_NETIF_ACCEPTING, /**< accepting incoming connections */ + NIMBLE_NETIF_INIT_MASTER, /**< conn. procedure started (as mater) */ + NIMBLE_NETIF_INIT_SLAVE, /**< conn. procedure started (as slave) */ NIMBLE_NETIF_CONNECTED_MASTER, /**< connection established as master */ NIMBLE_NETIF_CONNECTED_SLAVE, /**< connection established as slave */ NIMBLE_NETIF_CLOSED_MASTER, /**< connection closed (we were master) */ NIMBLE_NETIF_CLOSED_SLAVE, /**< connection closed (we were slave) */ - NIMBLE_NETIF_CONNECT_ABORT, /**< connection establishment aborted */ + NIMBLE_NETIF_ABORT_MASTER, /**< connection est. abort (as master) */ + NIMBLE_NETIF_ABORT_SLAVE, /**< connection est. abort (as slave) */ NIMBLE_NETIF_CONN_UPDATED, /**< connection parameter update done */ } nimble_netif_event_t; diff --git a/pkg/nimble/netif/nimble_netif.c b/pkg/nimble/netif/nimble_netif.c index d41274d189..f38d926b37 100644 --- a/pkg/nimble/netif/nimble_netif.c +++ b/pkg/nimble/netif/nimble_netif.c @@ -339,8 +339,7 @@ static int _on_l2cap_client_evt(struct ble_l2cap_event *event, void *arg) _notify(handle, NIMBLE_NETIF_CONNECTED_MASTER, conn->addr); break; case BLE_L2CAP_EVENT_COC_DISCONNECTED: - assert(conn->coc); - conn->coc = NULL; + assert(conn->state & NIMBLE_NETIF_L2CAP_CLIENT); conn->state &= ~NIMBLE_NETIF_L2CAP_CONNECTED; break; case BLE_L2CAP_EVENT_COC_ACCEPT: @@ -378,8 +377,7 @@ static int _on_l2cap_server_evt(struct ble_l2cap_event *event, void *arg) break; case BLE_L2CAP_EVENT_COC_DISCONNECTED: conn = nimble_netif_conn_from_gaphandle(event->disconnect.conn_handle); - assert(conn && conn->coc); - conn->coc = NULL; + assert(conn && (conn->state & NIMBLE_NETIF_L2CAP_SERVER)); conn->state &= ~NIMBLE_NETIF_L2CAP_CONNECTED; break; case BLE_L2CAP_EVENT_COC_ACCEPT: { @@ -427,7 +425,7 @@ static int _on_gap_master_evt(struct ble_gap_event *event, void *arg) if (event->connect.status != 0) { uint8_t addr[BLE_ADDR_LEN]; nimble_netif_conn_free(handle, addr); - _notify(handle, NIMBLE_NETIF_CONNECT_ABORT, addr); + _notify(handle, NIMBLE_NETIF_ABORT_MASTER, addr); return 0; } _on_gap_connected(conn, event->connect.conn_handle); @@ -444,9 +442,12 @@ static int _on_gap_master_evt(struct ble_gap_event *event, void *arg) break; } case BLE_GAP_EVENT_DISCONNECT: { + nimble_netif_event_t type; + type = (conn->coc != NULL) ? NIMBLE_NETIF_CLOSED_MASTER + : NIMBLE_NETIF_ABORT_MASTER; uint8_t addr[BLE_ADDR_LEN]; nimble_netif_conn_free(handle, addr); - _notify(handle, NIMBLE_NETIF_CLOSED_MASTER, addr); + _notify(handle, type, addr); break; } case BLE_GAP_EVENT_CONN_UPDATE: @@ -474,18 +475,22 @@ static int _on_gap_slave_evt(struct ble_gap_event *event, void *arg) if (event->connect.status != 0) { uint8_t addr[BLE_ADDR_LEN]; nimble_netif_conn_free(handle, addr); - _notify(handle, NIMBLE_NETIF_CONNECT_ABORT, addr); + _notify(handle, NIMBLE_NETIF_ABORT_SLAVE, addr); break; } _on_gap_connected(conn, event->connect.conn_handle); assert(conn->state == NIMBLE_NETIF_ADV); conn->state = NIMBLE_NETIF_GAP_SLAVE; + _notify(handle, NIMBLE_NETIF_INIT_SLAVE, conn->addr); break; } case BLE_GAP_EVENT_DISCONNECT: { + nimble_netif_event_t type; + type = (conn->coc != NULL) ? NIMBLE_NETIF_CLOSED_SLAVE + : NIMBLE_NETIF_ABORT_SLAVE; uint8_t addr[BLE_ADDR_LEN]; nimble_netif_conn_free(handle, addr); - _notify(handle, NIMBLE_NETIF_CLOSED_SLAVE, addr); + _notify(handle, type, addr); break; } case BLE_GAP_EVENT_CONN_UPDATE: @@ -557,6 +562,8 @@ int nimble_netif_connect(const ble_addr_t *addr, assert(res == 0); (void)res; + _notify(handle, NIMBLE_NETIF_INIT_MASTER, addrn); + return handle; } @@ -602,6 +609,8 @@ int nimble_netif_accept(const uint8_t *ad, size_t ad_len, adv_params, _on_gap_slave_evt, (void *)handle); assert(res == 0); + _notify(handle, NIMBLE_NETIF_ACCEPTING, NULL); + return NIMBLE_NETIF_OK; } diff --git a/sys/shell/commands/sc_nimble_netif.c b/sys/shell/commands/sc_nimble_netif.c index 8337fcd882..0776bdd713 100644 --- a/sys/shell/commands/sc_nimble_netif.c +++ b/sys/shell/commands/sc_nimble_netif.c @@ -84,7 +84,8 @@ static void _on_ble_evt(int handle, nimble_netif_event_t event, case NIMBLE_NETIF_CLOSED_SLAVE: _print_evt("CONNECTION CLOSED", handle, addr); break; - case NIMBLE_NETIF_CONNECT_ABORT: + case NIMBLE_NETIF_ABORT_MASTER: + case NIMBLE_NETIF_ABORT_SLAVE: _print_evt("CONNECTION ABORT", handle, addr); break; case NIMBLE_NETIF_CONN_UPDATED: