Skip to content

Commit 91bf5e0

Browse files
committed
[Bugfix] Peripheral connection event delay/missing disconnect event
This removes some incorrect upstream changes that would allow a peripheral/server device to recieve events from connected client requests without the application being aware of the connection. The code was changed from the original mynewt event flow in that the connection event is not sent until a response from a request to read the remote version/features where originally the event was sent before these responses were received. This reverts this situaton for the peripheral role because when the connection is made in this case it is fully established and there is no need to wait for the responses to the above requests. In addition the delay caused by these was allowing connected peers to interact without the application knowing of the connection, creating unexpoected issues. One of those issues is the client could disconnect before the peripheral receives a response to the above requests and the changes that were made would prevent the disconnect event from being sent because the connection was not considered established. Finally, the connection reattempt code was checking for a connection establishment failure when the device was acting as a slave, which is incorrect as that condition cannot happen if the spec is followed. The controller should never send this event to a peripheral device waiting for connection, it should just stay advertsing. This patch removes all of the unecessary code that was added and reverts the flow to the original mynewt flow for peripheral devices.
1 parent ea5f2ec commit 91bf5e0

2 files changed

Lines changed: 16 additions & 41 deletions

File tree

src/nimble/nimble/host/src/ble_gap.c

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ static uint8_t pawr_adv_handle;
296296
static uint16_t pawr_sync_handle;
297297
#endif
298298

299-
int slave_conn[MYNEWT_VAL(BLE_MAX_CONNECTIONS) + 1];
300299
static void ble_gap_update_entry_free(struct ble_gap_update_entry *entry);
301300

302301
#if NIMBLE_BLE_CONNECT
@@ -1557,8 +1556,6 @@ ble_gap_conn_broken(uint16_t conn_handle, int reason)
15571556
struct ble_gap_update_entry *entry;
15581557
struct ble_gap_snapshot snap;
15591558
struct ble_gap_event event;
1560-
struct ble_hs_conn *conn;
1561-
bool send = 1;
15621559
int rc;
15631560

15641561
memset(&event, 0, sizeof event);
@@ -1595,19 +1592,6 @@ ble_gap_conn_broken(uint16_t conn_handle, int reason)
15951592
#endif
15961593
ble_hs_flow_connection_broken(conn_handle);;
15971594

1598-
ble_hs_lock();
1599-
conn = ble_hs_conn_find(conn_handle);
1600-
ble_hs_unlock();
1601-
1602-
// Send disconnect event in slave role if connect was sent
1603-
if ((conn != NULL) && !(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
1604-
if (slave_conn[conn_handle]) {
1605-
slave_conn[conn_handle] = 0;
1606-
} else {
1607-
send = 0;
1608-
}
1609-
}
1610-
16111595
ble_hs_atomic_conn_delete(conn_handle);
16121596

16131597
g_max_tx_time[conn_handle] = 0;
@@ -1618,10 +1602,8 @@ ble_gap_conn_broken(uint16_t conn_handle, int reason)
16181602
event.type = BLE_GAP_EVENT_DISCONNECT;
16191603
event.disconnect.reason = reason;
16201604

1621-
if (send) {
1622-
ble_gap_event_listener_call(&event);
1623-
ble_gap_call_event_cb(&event, snap.cb, snap.cb_arg);
1624-
}
1605+
ble_gap_event_listener_call(&event);
1606+
ble_gap_call_event_cb(&event, snap.cb, snap.cb_arg);
16251607

16261608
STATS_INC(ble_gap_stats, disconnect);
16271609
#endif
@@ -2770,9 +2752,11 @@ ble_gap_rx_conn_complete(struct ble_gap_conn_complete *evt, uint8_t instance)
27702752
pawr_adv_handle = evt->adv_handle;
27712753
#endif
27722754

2755+
27732756
if (evt->role == BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE) {
2757+
ble_gap_event_connect_call(evt->connection_handle, evt->status);
27742758
ble_gap_rd_rem_ver_tx(evt->connection_handle);
2775-
} else {
2759+
} else { // master
27762760
ble_gap_rd_rem_sup_feat_tx(evt->connection_handle);
27772761
}
27782762

@@ -2832,17 +2816,10 @@ ble_gap_rx_rd_rem_sup_feat_complete(const struct ble_hci_ev_le_subev_rd_rem_used
28322816

28332817
ble_hs_unlock();
28342818

2835-
if ((conn != NULL) && (conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
2819+
if ((conn != NULL) && ev->status == 0) {
28362820
conn->supported_feat = get_le32(ev->features);
2837-
ble_gap_rd_rem_ver_tx(ev->conn_handle);
2838-
} else {
2839-
if ((conn != NULL) && (ev->status == 0)) {
2840-
conn->supported_feat = get_le32(ev->features);
2841-
}
2842-
2843-
if (conn != NULL) {
2844-
ble_gap_event_connect_call(ev->conn_handle, ev->status);
2845-
slave_conn[ev->conn_handle] = 1;
2821+
if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
2822+
ble_gap_rd_rem_ver_tx(ev->conn_handle);
28462823
}
28472824
}
28482825
#endif
@@ -2864,11 +2841,11 @@ ble_gap_rx_rd_rem_ver_info_complete(const struct ble_hci_ev_rd_rem_ver_info_cmp
28642841
conn->bhc_rd_rem_ver_params.manufacturer = ev->manufacturer;
28652842
conn->bhc_rd_rem_ver_params.subversion = ev->subversion;
28662843

2867-
if ((conn != NULL) && !(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
2868-
ble_gap_rd_rem_sup_feat_tx(ev->conn_handle);
2869-
} else {
2870-
if ((conn != NULL) && (ev->status == 0)) {
2871-
ble_gap_event_connect_call(ev->conn_handle, ev->status);
2844+
if ((conn != NULL)) {
2845+
if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
2846+
ble_gap_event_connect_call(ev->conn_handle, 0);
2847+
} else {
2848+
ble_gap_rd_rem_sup_feat_tx(ev->conn_handle);
28722849
}
28732850
}
28742851
#endif

src/nimble/nimble/host/src/ble_hs_hci_evt.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ struct ble_gap_reattempt_ctxt {
3737

3838
extern int ble_gap_master_connect_reattempt(uint16_t conn_handle);
3939
extern int ble_gap_slave_adv_reattempt(void);
40-
extern int slave_conn[MYNEWT_VAL(BLE_MAX_CONNECTIONS) + 1];
4140
#endif
4241

4342
#if MYNEWT_VAL(BLE_QUEUE_CONG_CHECK)
@@ -285,17 +284,16 @@ ble_hs_hci_evt_disconn_complete(uint8_t event_code, const void *data,
285284
memset(&reattempt_conn, 0x0, sizeof (struct ble_gap_reattempt_ctxt));
286285
}
287286
}
288-
else if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER) && \
289-
((ev->reason == BLE_ERR_CONN_ESTABLISHMENT) || \
290-
(!slave_conn[ev->conn_handle] && ev->reason == BLE_ERR_CONN_SPVN_TMO))) { //slave
287+
else if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER) &&
288+
ev->reason == BLE_ERR_CONN_SPVN_TMO) { //slave
291289

292290
BLE_HS_LOG(INFO, "Reattempt advertising; reason: 0x%x, status = %x",
293291
ev->reason, ev->status);
294292
ble_l2cap_sig_conn_broken(ev->conn_handle, BLE_ERR_CONN_ESTABLISHMENT);
295293
ble_sm_connection_broken(ev->conn_handle);
296294
ble_gatts_connection_broken(ev->conn_handle);
297295
ble_gattc_connection_broken(ev->conn_handle);
298-
ble_hs_flow_connection_broken(ev->conn_handle);;
296+
ble_hs_flow_connection_broken(ev->conn_handle);
299297
#if MYNEWT_VAL(BLE_GATT_CACHING)
300298
ble_gattc_cache_conn_broken(ev->conn_handle);
301299
#endif

0 commit comments

Comments
 (0)