Skip to content

Commit 545db0e

Browse files
eatt: Add support for manual EATT connection
Introduce ble_eatt_connect() API to allow manual setup EATT. This change enables explicit control over EATT channel creation. The function validates the connection handle and requested channel count, supports requesting all channels by passing 0 Add declaration of ble_eatt_connect() to the ble_att.h header.
1 parent 8dcac02 commit 545db0e

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

nimble/host/include/host/ble_att.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ uint16_t ble_att_preferred_mtu(void);
355355
*/
356356
int ble_att_set_preferred_mtu(uint16_t mtu);
357357

358+
/**
359+
* Manually establish L2CAP Enhanced Connection
360+
*
361+
* @param conn_handle ACL connection handle
362+
* @param chan_num Number of channels to establish
363+
*
364+
* @return 0 on success;
365+
* NimBLE host core return code on unexpected
366+
* error.
367+
*/
368+
int ble_eatt_connect(uint16_t conn_handle, uint8_t chan_num);
369+
358370
#ifdef __cplusplus
359371
}
360372
#endif

nimble/host/src/ble_eatt.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ ble_eatt_find(uint16_t conn_handle, uint16_t cid)
130130
if (eatt->conn_handle == conn_handle) {
131131
for (i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN); i++) {
132132
if (eatt->chan[i] && eatt->chan[i]->scid == cid) {
133-
return eatt;
134-
}
135-
}
133+
return eatt;
134+
}
135+
}
136136
}
137137
}
138138
return NULL;
@@ -245,7 +245,7 @@ ble_eatt_wakeup_cb(struct ble_npl_event *ev)
245245
for (i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN); i++) {
246246
if (eatt->chan[i] != NULL) {
247247
ble_l2cap_get_chan_info(eatt->chan[i], &info);
248-
ble_eatt_tx(eatt->conn_handle, info.dcid, txom);
248+
ble_eatt_tx(eatt->conn_handle, info.dcid, txom);
249249
break;
250250
}
251251
}
@@ -641,6 +641,52 @@ ble_eatt_start(uint16_t conn_handle)
641641
ble_npl_eventq_put(ble_hs_evq_get(), &eatt->setup_ev);
642642
}
643643

644+
int
645+
ble_eatt_connect(uint16_t conn_handle, uint8_t chan_num)
646+
{
647+
int rc;
648+
uint8_t free_channels;
649+
struct ble_eatt *eatt;
650+
struct ble_gap_conn_desc desc;
651+
652+
rc = ble_gap_conn_find(conn_handle, &desc);
653+
assert(rc == 0);
654+
655+
eatt = ble_eatt_find_by_conn_handle(conn_handle);
656+
if (!eatt) {
657+
eatt = ble_eatt_alloc();
658+
if (!eatt) {
659+
BLE_EATT_LOG_ERROR("ble_eatt_connect: Can't allocate EATT\n");
660+
return BLE_HS_ENOMEM;
661+
}
662+
eatt->conn_handle = conn_handle;
663+
}
664+
665+
if (chan_num > MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN)) {
666+
BLE_EATT_LOG_WARN("ble_eatt_connect: Invalid channel number\n");
667+
return BLE_HS_EREJECT;
668+
}
669+
670+
/* Get number of free channels for this connection */
671+
free_channels =
672+
MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN) - ble_eatt_used_channels(conn_handle);
673+
674+
/* Connect all channels if invoked with 0 & all channels are free */
675+
if (chan_num == 0 && free_channels == MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN)) {
676+
eatt->channels_to_connect = MYNEWT_VAL(BLE_EATT_CHAN_PER_CONN);
677+
} else if (chan_num < free_channels) {
678+
eatt->channels_to_connect = chan_num;
679+
} else {
680+
BLE_EATT_LOG_ERROR("ble_eatt_connect: Invalid channel number\n");
681+
return BLE_HS_ENOMEM;
682+
}
683+
684+
/* Setup EATT */
685+
ble_npl_eventq_put(ble_hs_evq_get(), &eatt->setup_ev);
686+
687+
return 0;
688+
}
689+
644690
void
645691
ble_eatt_init(ble_eatt_att_rx_fn att_rx_cb)
646692
{

0 commit comments

Comments
 (0)