Skip to content

Commit 295a6e5

Browse files
nimble/eatt: Handle multiple channels in event handler
Add support for multiple-channels in event function. Refactor logging within event handler function. Introduce accepted channels field in eatt structure.
1 parent a45e30f commit 295a6e5

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

nimble/host/src/ble_eatt.c

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct ble_eatt {
3939
uint8_t client_op;
4040
uint8_t chan_num;
4141
uint8_t used_channels;
42+
uint8_t accept_channels;
4243

4344
/* Packet transmit queue */
4445
STAILQ_HEAD(, os_mbuf_pkthdr) eatt_tx_q;
@@ -225,6 +226,8 @@ ble_eatt_alloc(void)
225226
eatt->conn_handle = BLE_HS_CONN_HANDLE_NONE;
226227
eatt->chan = NULL;
227228
eatt->client_op = 0;
229+
eatt->accept_channels = 0;
230+
eatt->used_channels = 0;
228231

229232
STAILQ_INIT(&eatt->eatt_tx_q);
230233

@@ -257,47 +260,89 @@ ble_eatt_l2cap_event_fn(struct ble_l2cap_event *event, void *arg)
257260
{
258261
struct ble_eatt *eatt = arg;
259262
struct ble_gap_conn_desc desc;
263+
uint8_t free_channels;
260264
uint8_t opcode;
261265
int rc;
262266

263267
switch (event->type) {
264268
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+
266274
if (event->connect.status) {
267275
ble_eatt_free(eatt);
268276
return 0;
269277
}
270278
eatt->chan = event->connect.chan;
279+
eatt->conn_handle = event->connect.conn_handle;
280+
271281
eatt->used_channels++;
282+
BLE_EATT_LOG_DEBUG("eatt: Channels already used for this connection %d\n",
283+
eatt->used_channels);
272284
break;
273285
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+
}
276307
break;
277308
case BLE_L2CAP_EVENT_COC_ACCEPT:
278-
BLE_EATT_LOG_DEBUG("eatt: Accept request\n");
309+
/* Lookup if EATT already exsits for this connection */
279310
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();
288311
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+
}
290326
}
291327

292328
eatt->conn_handle = event->accept.conn_handle;
293329
event->accept.chan->cb_arg = eatt;
294330

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+
295337
rc = ble_eatt_prepare_rx_sdu(event->accept.chan);
296338
if (rc) {
297339
ble_eatt_free(eatt);
298340
return rc;
299341
}
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);
301346
break;
302347
case BLE_L2CAP_EVENT_COC_TX_UNSTALLED:
303348
ble_npl_eventq_put(ble_hs_evq_get(), &eatt->wakeup_ev);

0 commit comments

Comments
 (0)