feat(nimble): Added support for Enhanced ATT or ATT over Enhanced L2CAP Credit Based Flow Control Mode

This commit is contained in:
Sumeet Singh
2024-03-29 12:41:18 +05:30
parent 1a6060fa3a
commit f265e50ca0
7 changed files with 185 additions and 8 deletions
+74 -5
View File
@@ -48,7 +48,7 @@
#endif
#endif
/*** The UUID of the service containing the subscribable characterstic ***/
/*** The UUID of the service containing the subscribable characteristic ***/
static const ble_uuid_t * remote_svc_uuid =
BLE_UUID128_DECLARE(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
@@ -62,6 +62,11 @@ static const char *tag = "NimBLE_BLE_CENT";
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
static uint16_t cids[MYNEWT_VAL(BLE_EATT_CHAN_NUM)];
static uint16_t bearers;
#endif
void ble_store_config_init(void);
/**
@@ -203,7 +208,7 @@ blecent_custom_gatt_operations(const struct peer* peer)
remote_chr_uuid,
BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16));
if (dsc == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer lacks a CCCD for the subscribable characterstic\n");
MODLOG_DFLT(ERROR, "Error: Peer lacks a CCCD for the subscribable characteristic\n");
goto err;
}
@@ -687,7 +692,7 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);
/* Try to connect to the advertiser if it looks interesting. */
@@ -773,6 +778,14 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
/* Forget about peer. */
peer_delete(event->disconnect.conn.conn_handle);
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
/* Reset EATT config */
bearers = 0;
for (int i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_NUM); i++) {
cids[i] = 0;
}
#endif
/* Resume scanning. */
blecent_scan();
return 0;
@@ -789,14 +802,16 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
assert(rc == 0);
print_conn_desc(&desc);
#if !MYNEWT_VAL(BLE_EATT_CHAN_NUM)
#if CONFIG_EXAMPLE_ENCRYPTION
/*** Go for service discovery after encryption has been successfully enabled ***/
rc = peer_disc_all(event->connect.conn_handle,
rc = peer_disc_all(event->enc_change.conn_handle,
blecent_on_disc_complete, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}
#endif
#endif
return 0;
@@ -840,7 +855,7 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);
blecent_connect_if_interesting(&event->disc);
@@ -868,6 +883,52 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
event->pathloss_threshold.zone_entered);
return 0;
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
case BLE_GAP_EVENT_EATT:
int i;
MODLOG_DFLT(INFO, "EATT %s : conn_handle=%d cid=%d",
event->eatt.status ? "disconnected" : "connected",
event->eatt.conn_handle,
event->eatt.cid);
if (event->eatt.status) {
/* Remove CID from the list of saved CIDs */
for (i = 0; i < bearers; i++) {
if (cids[i] == event->eatt.cid) {
break;
}
}
while (i < (bearers - 1)) {
cids[i] = cids[i + 1];
i += 1;
}
cids[i] = 0;
/* Now Abort */
return 0;
}
cids[bearers] = event->eatt.cid;
bearers += 1;
if (bearers != MYNEWT_VAL(BLE_EATT_CHAN_NUM)) {
/* Wait until all EATT bearers are connected before proceeding */
return 0;
}
/* Set the default bearer to use for further procedures */
rc = ble_att_set_default_bearer_using_cid(event->eatt.conn_handle, cids[0]);
if (rc != 0) {
MODLOG_DFLT(INFO, "Cannot set default EATT bearer, rc = %d\n", rc);
return rc;
}
/* Perform service discovery */
rc = peer_disc_all(event->eatt.conn_handle,
blecent_on_disc_complete, NULL);
if(rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}
#endif
return 0;
default:
return 0;
}
@@ -888,6 +949,7 @@ blecent_on_sync(void)
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
#if !CONFIG_EXAMPLE_INIT_DEINIT_LOOP
/* Begin scanning for a peripheral to connect to. */
blecent_scan();
@@ -979,4 +1041,11 @@ app_main(void)
stack_init_deinit();
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
bearers = 0;
for (int i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_NUM); i++) {
cids[i] = 0;
}
#endif
}
+37 -1
View File
@@ -45,6 +45,11 @@ static uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
static uint8_t own_addr_type;
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
static uint16_t cids[MYNEWT_VAL(BLE_EATT_CHAN_NUM)];
static uint16_t bearers;
#endif
void ble_store_config_init(void);
/**
@@ -416,13 +421,37 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
event->transmit_power.delta);
return 0;
case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d "
"zone_entered =%d",
event->pathloss_threshold.conn_handle,
event->pathloss_threshold.current_path_loss,
event->pathloss_threshold.zone_entered);
return 0;
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
case BLE_GAP_EVENT_EATT:
MODLOG_DFLT(INFO, "EATT %s : conn_handle=%d cid=%d",
event->eatt.status ? "disconnected" : "connected",
event->eatt.conn_handle,
event->eatt.cid);
if (event->eatt.status) {
/* Abort if disconnected */
return 0;
}
cids[bearers] = event->eatt.cid;
bearers += 1;
if (bearers != MYNEWT_VAL(BLE_EATT_CHAN_NUM)) {
/* Wait until all EATT bearers are connected before proceeding */
return 0;
}
/* Set the default bearer to use for further procedures */
rc = ble_att_set_default_bearer_using_cid(event->eatt.conn_handle, cids[0]);
if (rc != 0) {
MODLOG_DFLT(INFO, "Cannot set default EATT bearer, rc = %d\n", rc);
return rc;
}
return 0;
#endif
}
@@ -566,4 +595,11 @@ app_main(void)
if (rc != ESP_OK) {
ESP_LOGE(tag, "scli_init() failed");
}
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM)
bearers = 0;
for (int i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_NUM); i++) {
cids[i] = 0;
}
#endif
}