Skip to content

Commit a45e30f

Browse files
nimble/eatt: Implement new auto-connect handling
Implement new way for auto-connect. Add initial delay to avoid collision.
1 parent ee99f8a commit a45e30f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

nimble/host/src/ble_eatt.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ struct ble_eatt {
4545

4646
struct ble_npl_event setup_ev;
4747
struct ble_npl_event wakeup_ev;
48+
49+
#if MYNEWT_VAL(BLE_EATT_AUTO_CONNECT)
50+
struct os_callout auto_conn_delay;
51+
#endif
4852
};
4953

5054
SLIST_HEAD(ble_eatt_list, ble_eatt);
@@ -194,6 +198,17 @@ ble_eatt_wakeup_cb(struct ble_npl_event *ev)
194198
}
195199
}
196200

201+
#if (MYNEWT_VAL(BLE_EATT_AUTO_CONNECT))
202+
void
203+
ble_eatt_auto_conn_cb(struct os_event *ev)
204+
{
205+
struct os_callout *co = (struct os_callout *)ev;
206+
struct ble_eatt *eatt = CONTAINER_OF(co, struct ble_eatt, auto_conn_delay);
207+
208+
ble_eatt_connect(eatt->conn_handle, MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN));
209+
}
210+
#endif
211+
197212
static struct ble_eatt *
198213
ble_eatt_alloc(void)
199214
{
@@ -212,6 +227,11 @@ ble_eatt_alloc(void)
212227
eatt->client_op = 0;
213228

214229
STAILQ_INIT(&eatt->eatt_tx_q);
230+
231+
#if (MYNEWT_VAL(BLE_EATT_AUTO_CONNECT))
232+
os_callout_init(&eatt->auto_conn_delay, os_eventq_dflt_get(),
233+
ble_eatt_auto_conn_cb, NULL);
234+
#endif
215235
ble_npl_event_init(&eatt->setup_ev, ble_eatt_setup_cb, eatt);
216236
ble_npl_event_init(&eatt->wakeup_ev, ble_eatt_wakeup_cb, eatt);
217237

@@ -375,6 +395,50 @@ ble_gatt_eatt_write_cl_cb(uint16_t conn_handle,
375395
return 0;
376396
}
377397

398+
#if (MYNEWT_VAL(BLE_EATT_AUTO_CONNECT))
399+
struct ble_gap_conn_desc desc;
400+
struct ble_eatt *eatt;
401+
uint8_t delay_rand;
402+
uint8_t delay;
403+
int rc;
404+
405+
rc = ble_gap_conn_find(conn_handle, &desc);
406+
assert(rc == 0);
407+
408+
eatt = ble_eatt_find_by_conn_handle(conn_handle);
409+
if (eatt && eatt->used_channels != 0) {
410+
BLE_EATT_LOG_DEBUG("eatt: EATT channels already established"
411+
" for this connection\n");
412+
return 0;
413+
} else {
414+
eatt = ble_eatt_alloc();
415+
if (!eatt) {
416+
return 0;
417+
}
418+
}
419+
420+
/* Add initial delay as peripheral to avoid collision.
421+
* Central is allowed to connect instantly.
422+
* If there is at least one active connection - ommitt.
423+
*/
424+
if (desc.role == BLE_GAP_ROLE_SLAVE && eatt->used_channels == 0) {
425+
rc = ble_hs_hci_rand(&delay_rand, 1);
426+
if (rc != 0) {
427+
return rc;
428+
}
429+
430+
delay = (delay_rand % 5) + 2 * (desc.conn_latency + 1) * desc.conn_itvl;
431+
eatt->conn_handle = conn_handle;
432+
os_callout_reset(&eatt->auto_conn_delay, OS_TICKS_PER_SEC / 1000 * delay);
433+
} else if (desc.role == BLE_GAP_ROLE_MASTER && eatt->used_channels == 0) {
434+
rc = ble_eatt_connect(conn_handle, MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN));
435+
if (rc) {
436+
BLE_EATT_LOG_DEBUG("eatt: EATT connect failed for conn_handle: %d\n",
437+
conn_handle);
438+
}
439+
}
440+
#endif
441+
378442

379443
return 0;
380444
}

0 commit comments

Comments
 (0)