@@ -39,6 +39,7 @@ struct ble_eatt {
39
39
uint8_t client_op ;
40
40
uint8_t chan_num ;
41
41
uint8_t used_channels ;
42
+ uint8_t accept_channels ;
42
43
43
44
/* Packet transmit queue */
44
45
STAILQ_HEAD (, os_mbuf_pkthdr ) eatt_tx_q ;
@@ -225,6 +226,8 @@ ble_eatt_alloc(void)
225
226
eatt -> conn_handle = BLE_HS_CONN_HANDLE_NONE ;
226
227
eatt -> chan = NULL ;
227
228
eatt -> client_op = 0 ;
229
+ eatt -> accept_channels = 0 ;
230
+ eatt -> used_channels = 0 ;
228
231
229
232
STAILQ_INIT (& eatt -> eatt_tx_q );
230
233
@@ -257,47 +260,89 @@ ble_eatt_l2cap_event_fn(struct ble_l2cap_event *event, void *arg)
257
260
{
258
261
struct ble_eatt * eatt = arg ;
259
262
struct ble_gap_conn_desc desc ;
263
+ uint8_t free_channels ;
260
264
uint8_t opcode ;
261
265
int rc ;
262
266
263
267
switch (event -> type ) {
264
268
case BLE_L2CAP_EVENT_COC_CONNECTED :
265
- BLE_EATT_LOG_DEBUG ("eatt: Connected \n" );
269
+ BLE_EATT_LOG_DEBUG ("eatt: Connected event | conn_handle: %d |"
270
+ " scid: %d | dcid: %d | status: %d\n" ,
271
+ event -> connect .conn_handle , event -> connect .chan -> scid ,
272
+ event -> connect .chan -> dcid , event -> connect .status );
273
+
266
274
if (event -> connect .status ) {
267
275
ble_eatt_free (eatt );
268
276
return 0 ;
269
277
}
270
278
eatt -> chan = event -> connect .chan ;
279
+ eatt -> conn_handle = event -> connect .conn_handle ;
280
+
271
281
eatt -> used_channels ++ ;
282
+ BLE_EATT_LOG_DEBUG ("eatt: Channels already used for this connection %d\n" ,
283
+ eatt -> used_channels );
272
284
break ;
273
285
case BLE_L2CAP_EVENT_COC_DISCONNECTED :
274
- BLE_EATT_LOG_DEBUG ("eatt: Disconnected \n" );
275
- ble_eatt_free (eatt );
286
+ BLE_EATT_LOG_DEBUG ("eatt: Disconnected event | conn_handle: %d | "
287
+ "scid: %d | dcid: %d\n" , event -> disconnect .conn_handle ,
288
+ event -> disconnect .chan -> scid ,
289
+ event -> disconnect .chan -> dcid );
290
+
291
+ eatt = ble_eatt_find_by_conn_handle (event -> disconnect .conn_handle );
292
+ if (!eatt ) {
293
+ BLE_EATT_LOG_ERROR ("eatt: Disconnected event | No EATT for conn_handle: %d\n" ,
294
+ event -> disconnect .conn_handle );
295
+ return 0 ;
296
+ }
297
+
298
+ /* Decrease number of channels on disconnect event
299
+ * If no channels are left - free the resources
300
+ */
301
+ eatt -> used_channels -- ;
302
+ eatt -> accept_channels -- ;
303
+
304
+ if (eatt -> used_channels == 0 ) {
305
+ ble_eatt_free (eatt );
306
+ }
276
307
break ;
277
308
case BLE_L2CAP_EVENT_COC_ACCEPT :
278
- BLE_EATT_LOG_DEBUG ( "eatt: Accept request\n" );
309
+ /* Lookup if EATT already exsits for this connection */
279
310
eatt = ble_eatt_find_by_conn_handle (event -> accept .conn_handle );
280
- if (eatt ) {
281
- /* For now we accept only one additional coc channel per ACL
282
- * TODO: improve it
283
- */
284
- return BLE_HS_ENOMEM ;
285
- }
286
-
287
- eatt = ble_eatt_alloc ();
288
311
if (!eatt ) {
289
- return BLE_HS_ENOMEM ;
312
+ eatt = ble_eatt_alloc ();
313
+ if (!eatt ) {
314
+ BLE_EATT_LOG_DEBUG ("eatt: Can't allocate EATT for conn_handle: %d\n" ,
315
+ event -> accept .conn_handle );
316
+ return 0 ;
317
+ }
318
+ } else {
319
+ free_channels = MYNEWT_VAL (BLE_EATT_CHAN_PER_CONN ) - ble_eatt_used_channels (eatt -> conn_handle );
320
+
321
+ if (free_channels == 0 ) {
322
+ BLE_EATT_LOG_ERROR ("eatt: Accept event | No free channels for "
323
+ "conn_handle: %d\n" , event -> accept .conn_handle );
324
+ return BLE_HS_ENOMEM ;
325
+ }
290
326
}
291
327
292
328
eatt -> conn_handle = event -> accept .conn_handle ;
293
329
event -> accept .chan -> cb_arg = eatt ;
294
330
331
+ /* Do not increase number of used channels here.
332
+ * Only do it on succesfull connected event &
333
+ * while initiating connection.
334
+ * Instead increase accept channels - yet to be connected */
335
+ eatt -> accept_channels ++ ;
336
+
295
337
rc = ble_eatt_prepare_rx_sdu (event -> accept .chan );
296
338
if (rc ) {
297
339
ble_eatt_free (eatt );
298
340
return rc ;
299
341
}
300
-
342
+ BLE_EATT_LOG_DEBUG ("eatt | Accept event | conn_handle: %d"
343
+ "| scid: %d | dcid: %d\n" ,
344
+ event -> accept .conn_handle , event -> accept .chan -> scid ,
345
+ event -> accept .chan -> dcid );
301
346
break ;
302
347
case BLE_L2CAP_EVENT_COC_TX_UNSTALLED :
303
348
ble_npl_eventq_put (ble_hs_evq_get (), & eatt -> wakeup_ev );
0 commit comments