@@ -45,6 +45,10 @@ struct ble_eatt {
45
45
46
46
struct ble_npl_event setup_ev ;
47
47
struct ble_npl_event wakeup_ev ;
48
+
49
+ #if MYNEWT_VAL (BLE_EATT_AUTO_CONNECT )
50
+ struct os_callout auto_conn_delay ;
51
+ #endif
48
52
};
49
53
50
54
SLIST_HEAD (ble_eatt_list , ble_eatt );
@@ -194,6 +198,17 @@ ble_eatt_wakeup_cb(struct ble_npl_event *ev)
194
198
}
195
199
}
196
200
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
+
197
212
static struct ble_eatt *
198
213
ble_eatt_alloc (void )
199
214
{
@@ -212,6 +227,11 @@ ble_eatt_alloc(void)
212
227
eatt -> client_op = 0 ;
213
228
214
229
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
215
235
ble_npl_event_init (& eatt -> setup_ev , ble_eatt_setup_cb , eatt );
216
236
ble_npl_event_init (& eatt -> wakeup_ev , ble_eatt_wakeup_cb , eatt );
217
237
@@ -375,6 +395,50 @@ ble_gatt_eatt_write_cl_cb(uint16_t conn_handle,
375
395
return 0 ;
376
396
}
377
397
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
+
378
442
379
443
return 0 ;
380
444
}
0 commit comments