feat(ble_audio): Support using PAST in the cap/acceptor example

This commit is contained in:
Linyan Liu
2026-05-14 20:43:07 +08:00
committed by Liu Linyan
parent 0f96f27d35
commit 3b70c1a856
50 changed files with 1210 additions and 562 deletions
@@ -32,6 +32,8 @@ int example_audio_gap_event_cb(struct ble_gap_event *event, void *arg)
event->type == BLE_GAP_EVENT_PERIODIC_SYNC ||
event->type == BLE_GAP_EVENT_PERIODIC_REPORT ||
event->type == BLE_GAP_EVENT_PERIODIC_SYNC_LOST ||
event->type == BLE_GAP_EVENT_PERIODIC_TRANSFER ||
event->type == BLE_GAP_EVENT_PERIODIC_TRANSFER_V2 ||
event->type == BLE_GAP_EVENT_CONNECT ||
event->type == BLE_GAP_EVENT_DISCONNECT ||
event->type == BLE_GAP_EVENT_ENC_CHANGE) {
@@ -41,11 +43,49 @@ int example_audio_gap_event_cb(struct ble_gap_event *event, void *arg)
event->type == BLE_GAP_EVENT_NOTIFY_TX ||
event->type == BLE_GAP_EVENT_SUBSCRIBE) {
esp_ble_audio_gatt_app_post_event(event->type, event);
} else if (event->type == BLE_GAP_EVENT_REPEAT_PAIRING) {
/* Peer wants to re-pair on top of an existing bond (e.g. phone
* cleared its LTK via "Forget device" but our NVS still has it).
* Delete our stale bond and tell NimBLE to retry pairing.
*/
struct ble_gap_conn_desc desc = {0};
int rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
if (rc == 0) {
ble_store_util_delete_peer(&desc.peer_id_addr);
}
return BLE_GAP_REPEAT_PAIRING_RETRY;
}
return 0;
}
void example_audio_security_failed_recover(const char *tag, uint16_t conn_handle, uint8_t status)
{
struct ble_gap_conn_desc desc = {0};
int rc;
/* status 13 = BLE_HS_ETIMEOUT: SMP exchange did not complete. Typical
* cause is asymmetric bond state — the two sides bonded previously,
* then one side erased its NVS while the other kept the LTK. The side
* that still has the bond tries encryption with the cached key; the
* other side has nothing to match it against, so SMP times out. Drop
* our entry for this peer and disconnect; the next connection runs
* fresh pairing (peer side recovers via REPEAT_PAIRING).
*/
ESP_LOGE(tag, "Security change failed, status %u, clearing local bond and reconnecting", status);
rc = ble_gap_conn_find(conn_handle, &desc);
if (rc == 0) {
ble_store_util_delete_peer(&desc.peer_id_addr);
}
/* Tear down the link: SMP cannot be retried on the same connection
* after a failed/timed-out exchange. The next reconnect starts a
* fresh SMP procedure with the now-cleared bond state.
*/
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}
static void print_hex(const uint8_t *ptr, size_t len)
{
while (len--) {
@@ -21,6 +21,7 @@
#include "host/ble_gap.h"
#include "host/ble_hs_adv.h"
#include "host/ble_store.h"
#include "esp_ble_audio_bap_api.h"
@@ -53,6 +54,8 @@
int example_audio_gap_event_cb(struct ble_gap_event *event, void *arg);
void example_audio_security_failed_recover(const char *tag, uint16_t conn_handle, uint8_t status);
void example_print_codec_cfg(const char *tag, const esp_ble_audio_codec_cfg_t *codec_cfg);
void example_print_codec_cap(const char *tag, const esp_ble_audio_codec_cap_t *codec_cap);