Merge branch 'bugfix/fix_few_nimble_issues_v5.0' into 'release/v5.0'

fix(nimble): Fix few nimble issues 11012025 (v5.0)

See merge request espressif/esp-idf!36315
This commit is contained in:
Rahul Tank 2025-01-24 12:37:30 +08:00
commit 7de5c22f44
16 changed files with 204 additions and 205 deletions

View File

@ -44,6 +44,7 @@ void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *a
/* Initialise gatt server */ /* Initialise gatt server */
int esp_blufi_gatt_svr_init(void); int esp_blufi_gatt_svr_init(void);
int esp_blufi_gatt_svr_deinit(void);
void esp_blufi_btc_init(void); void esp_blufi_btc_init(void);
void esp_blufi_btc_deinit(void); void esp_blufi_btc_deinit(void);
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -240,6 +240,32 @@ static void init_gatt_values(void)
} }
static void deinit_gatt_values(void)
{
int i = 0;
const struct ble_gatt_svc_def *svc;
const struct ble_gatt_chr_def *chr;
const struct ble_gatt_dsc_def *dsc;
for (svc = gatt_svr_svcs; svc && svc->uuid; svc++) {
for (chr = svc->characteristics; chr && chr->uuid; chr++) {
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
}
++i;
for (dsc = chr->descriptors; dsc && dsc->uuid; dsc++) {
if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) {
os_mbuf_free(gatt_values[i].buf); /* Free the buffer */
gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */
}
++i;
}
}
}
}
int esp_blufi_gatt_svr_init(void) int esp_blufi_gatt_svr_init(void)
{ {
int rc; int rc;
@ -260,6 +286,18 @@ int esp_blufi_gatt_svr_init(void)
return 0; return 0;
} }
int esp_blufi_gatt_svr_deinit(void)
{
deinit_gatt_values();
ble_gatts_free_svcs();
/* Deinitialize BLE GATT and GAP services */
ble_svc_gatt_deinit();
ble_svc_gap_deinit();
return 0;
}
static int static int
esp_blufi_gap_event(struct ble_gap_event *event, void *arg) esp_blufi_gap_event(struct ble_gap_event *event, void *arg)
{ {

View File

@ -176,7 +176,7 @@ config BT_NIMBLE_SMP_ID_RESET
default n default n
help help
There are tracking risks associated with using a fixed or static IRK. There are tracking risks associated with using a fixed or static IRK.
If enabled this option, Bluedroid will assign a new randomly-generated IRK If enabled this option, NimBLE will assign a new randomly-generated IRK
when all pairing and bonding records are deleted. This would decrease the ability when all pairing and bonding records are deleted. This would decrease the ability
of a previously paired peer to be used to determine whether a device of a previously paired peer to be used to determine whether a device
with which it previously shared an IRK is within range. with which it previously shared an IRK is within range.
@ -767,9 +767,17 @@ config BT_NIMBLE_VS_SUPPORT
This option is used to enable support for sending Vendor Specific HCI commands and handling This option is used to enable support for sending Vendor Specific HCI commands and handling
Vendor Specific HCI Events. Vendor Specific HCI Events.
config BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
bool "Gatt-proc preemption protect check"
depends on BT_NIMBLE_ENABLED
default n
help
When BLE and Wireless protocol/IEEE 802.15.4 operate in coexistence, BLE preemption
can disrupt the GATT context,causing the service discovery callback to not be invoked.
A temporary list is maintained to preserve the GATT context and use it in case of preemption.
config BT_NIMBLE_ENC_ADV_DATA config BT_NIMBLE_ENC_ADV_DATA
bool "Encrypted Advertising Data" bool "Encrypted Advertising Data"
depends on BT_NIMBLE_50_FEATURE_SUPPORT
help help
This option is used to enable encrypted advertising data. This option is used to enable encrypted advertising data.

@ -1 +1 @@
Subproject commit 3b34b570d195a6f3757126256c6cea0c22f78821 Subproject commit c031e1441ca38cb89833ad2be7abd6c836ecea22

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -1853,6 +1853,14 @@
#endif #endif
#endif #endif
#ifndef MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT
#ifdef CONFIG_BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
#define MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT CONFIG_BT_NIMBLE_GATTC_PROC_PREEMPTION_PROTECT
#else
#define MYNEWT_VAL_BLE_GATTC_PROC_PREEMPTION_PROTECT (0)
#endif
#endif
#ifndef MYNEWT_VAL_BT_HCI_LOG_INCLUDED #ifndef MYNEWT_VAL_BT_HCI_LOG_INCLUDED
#ifdef CONFIG_BT_HCI_LOG_DEBUG_EN #ifdef CONFIG_BT_HCI_LOG_DEBUG_EN
#define MYNEWT_VAL_BT_HCI_LOG_INCLUDED CONFIG_BT_HCI_LOG_DEBUG_EN #define MYNEWT_VAL_BT_HCI_LOG_INCLUDED CONFIG_BT_HCI_LOG_DEBUG_EN

View File

@ -63,6 +63,9 @@ typedef enum {
WIFI_AUTH_WAPI_PSK, /**< authenticate mode : WAPI_PSK */ WIFI_AUTH_WAPI_PSK, /**< authenticate mode : WAPI_PSK */
WIFI_AUTH_OWE, /**< authenticate mode : OWE */ WIFI_AUTH_OWE, /**< authenticate mode : OWE */
WIFI_AUTH_WPA3_ENT_192, /**< authenticate mode : WPA3_ENT_SUITE_B_192_BIT */ WIFI_AUTH_WPA3_ENT_192, /**< authenticate mode : WPA3_ENT_SUITE_B_192_BIT */
WIFI_AUTH_DUMMY_1, /**< Dummy placeholder authenticate mode for WIFI_AUTH_WPA3_EXT_PSK */
WIFI_AUTH_DUMMY_2, /**< Dummy placeholder authenticate mode for WIFI_AUTH_WPA3_EXT_PSK_MIXED_MODE */
WIFI_AUTH_DUMMY_3, /**< Dummy placeholder authenticate mode for WIFI_AUTH_DPP */
WIFI_AUTH_WPA3_ENTERPRISE, /**< authenticate mode : WPA3-Enterprise Only Mode */ WIFI_AUTH_WPA3_ENTERPRISE, /**< authenticate mode : WPA3-Enterprise Only Mode */
WIFI_AUTH_WPA2_WPA3_ENTERPRISE, /**< authenticate mode : WPA3-Enterprise Transition Mode */ WIFI_AUTH_WPA2_WPA3_ENTERPRISE, /**< authenticate mode : WPA3-Enterprise Transition Mode */
WIFI_AUTH_MAX WIFI_AUTH_MAX

@ -1 +1 @@
Subproject commit 64333e0afdd6fe253ab86550d970129385496f72 Subproject commit 02b64090dad59414d71b7540e30f88cf19f0e74a

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -227,14 +227,17 @@ esp_err_t esp_blufi_host_deinit(void)
{ {
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
esp_blufi_gatt_svr_deinit();
ret = nimble_port_stop(); ret = nimble_port_stop();
if (ret != ESP_OK) {
return ret;
}
if (ret == 0) { if (ret == 0) {
esp_nimble_deinit(); esp_nimble_deinit();
} }
ret = esp_blufi_profile_deinit(); ret = esp_blufi_profile_deinit();
if(ret != ESP_OK) { if (ret != ESP_OK) {
return ret; return ret;
} }

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -10,12 +10,13 @@
#include "nimble/nimble_port_freertos.h" #include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h" #include "host/ble_hs.h"
#define BLE_PAWR_NUM_SUBEVTS (5) #define BLE_PAWR_EVENT_INTERVAL (600)
#define BLE_PAWR_SUB_INTERVAL (12) /*!< Interval between subevents (N * 1.25 ms) */ #define BLE_PAWR_NUM_SUBEVTS (10)
#define BLE_PAWR_RSP_SLOT_DELAY (1) /*!< The first response slot delay (N * 1.25 ms)*/ #define BLE_PAWR_SUB_INTERVAL (44) /*!< Interval between subevents (N * 1.25 ms) */
#define BLE_PAWR_RSP_SLOT_DELAY (20) /*!< The first response slot delay (N * 1.25 ms)*/
#define BLE_PAWR_RSP_SLOT_SPACING (32) /*!< Time between response slots (N * 0.125 ms) */ #define BLE_PAWR_RSP_SLOT_SPACING (32) /*!< Time between response slots (N * 0.125 ms) */
#define BLE_PAWR_NUM_RSP_SLOTS (3) /*!< Number of subevent response slots */ #define BLE_PAWR_NUM_RSP_SLOTS (5) /*!< Number of subevent response slots */
#define BLE_PAWR_SUB_DATA_LEN (10) #define BLE_PAWR_SUB_DATA_LEN (20)
#define TAG "NimBLE_BLE_PAwR" #define TAG "NimBLE_BLE_PAwR"
@ -63,29 +64,20 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
return 0; return 0;
case BLE_GAP_EVENT_PER_SUBEV_RESP: case BLE_GAP_EVENT_PER_SUBEV_RESP:
if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE) {
if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_INCOMPLETE) { ESP_LOGI(TAG, "[Response] subevent:%d, response_slot:%d, data_length:%d",
// ESP_LOGI(TAG,"Incomplete response report received, discarding \n"); event->periodic_adv_response.subevent,
event->periodic_adv_response.response_slot,
event->periodic_adv_response.data_length);
const uint8_t *data = event->periodic_adv_response.data;
ESP_LOGI(TAG, "data: 0x%0x, 0x%0x", data[0], data[1]);
} else {
ESP_LOGE(TAG, "[Response] subevent:%d, response_slot:%d, rsp_data status:%d",
event->periodic_adv_response.subevent,
event->periodic_adv_response.response_slot,
event->periodic_adv_response.data_status);
} }
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_RX_FAILED) {
// ESP_LOGI(TAG,"Controller failed to received the AUX_SYNC_SUBEVENT_RSP\n");
}
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE) {
ESP_LOGI(TAG, "[Response] subevent:%d, response_slot:%d, data_length:%d, data:%x",
event->periodic_adv_response.subevent,
event->periodic_adv_response.response_slot,
event->periodic_adv_response.data_length,
event->periodic_adv_response.data[0]);
}
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_TRUNCATED) {
// ESP_LOGI(TAG,"Truncated response report received, discarding\n");
}
else {
ESP_LOGE(TAG,"Invalid data status\n");
}
return 0; return 0;
default: default:
return 0; return 0;
} }
@ -120,8 +112,8 @@ start_periodic_adv(void)
params.primary_phy = BLE_HCI_LE_PHY_CODED; params.primary_phy = BLE_HCI_LE_PHY_CODED;
params.secondary_phy = BLE_HCI_LE_PHY_1M; params.secondary_phy = BLE_HCI_LE_PHY_1M;
params.sid = 0; params.sid = 0;
params.itvl_min = BLE_GAP_ADV_ITVL_MS(100); params.itvl_min = BLE_PAWR_EVENT_INTERVAL;
params.itvl_max = BLE_GAP_ADV_ITVL_MS(100); params.itvl_max = BLE_PAWR_EVENT_INTERVAL;
rc = ble_gap_ext_adv_configure(instance, &params, NULL, gap_event_cb, NULL); rc = ble_gap_ext_adv_configure(instance, &params, NULL, gap_event_cb, NULL);
assert (rc == 0); assert (rc == 0);
@ -147,9 +139,9 @@ start_periodic_adv(void)
pparams.itvl_max = BLE_GAP_PERIODIC_ITVL_MS(3000); pparams.itvl_max = BLE_GAP_PERIODIC_ITVL_MS(3000);
/* Configure the parameters of PAwR. */ /* Configure the parameters of PAwR. */
pparams.num_subevents = BLE_PAWR_NUM_SUBEVTS; pparams.num_subevents = BLE_PAWR_NUM_SUBEVTS;
pparams.subevent_interval = BLE_GAP_PERIODIC_ITVL_MS(300); pparams.subevent_interval = BLE_PAWR_SUB_INTERVAL;
pparams.response_slot_delay = BLE_GAP_PERIODIC_ITVL_MS(80); pparams.response_slot_delay = BLE_PAWR_RSP_SLOT_DELAY;
pparams.response_slot_spacing = 0xFF; pparams.response_slot_spacing = BLE_PAWR_RSP_SLOT_SPACING;
pparams.num_response_slots = BLE_PAWR_NUM_RSP_SLOTS; pparams.num_response_slots = BLE_PAWR_NUM_RSP_SLOTS;
rc = ble_gap_periodic_adv_configure(instance, &pparams); rc = ble_gap_periodic_adv_configure(instance, &pparams);

View File

@ -13,23 +13,4 @@ CONFIG_BT_NIMBLE_MAX_PERIODIC_SYNCS=1
CONFIG_BT_NIMBLE_ROLE_CENTRAL=y CONFIG_BT_NIMBLE_ROLE_CENTRAL=y
CONFIG_BT_NIMBLE_ROLE_OBSERVER=n CONFIG_BT_NIMBLE_ROLE_OBSERVER=n
CONFIG_BT_CONTROLLER_DISABLED=y CONFIG_BT_CONTROLLER_ENABLED=y
#
# Host-controller Transport
#
CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT=1
CONFIG_UART_BAUDRATE_115200=y
CONFIG_BT_NIMBLE_UART_TX_PIN=20
CONFIG_BT_NIMBLE_UART_RX_PIN=21
CONFIG_UART_HW_FLOWCTRL_CTS_RTS=n
# CONFIG_BT_NIMBLE_HCI_UART_RTS_PIN=22
# CONFIG_BT_NIMBLE_HCI_UART_CTS_PIN=23
# end of Host-controller Transport
# end of NimBLE Options
# C6 Nordic
# TX: 20 ---- RX
# RX: 21 ---- TX
# RTS: 22 ---- CTS
# CTS: 23 ---- RTS

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -13,7 +13,7 @@
#define TAG "NimBLE_BLE_PAwR" #define TAG "NimBLE_BLE_PAwR"
#define TARGET_NAME "Nimble_PAwR" #define TARGET_NAME "Nimble_PAwR"
#define BLE_PAWR_RSP_DATA_LEN (20) #define BLE_PAWR_RSP_DATA_LEN (16)
static uint8_t sub_data_pattern[BLE_PAWR_RSP_DATA_LEN] = {0}; static uint8_t sub_data_pattern[BLE_PAWR_RSP_DATA_LEN] = {0};
static int create_periodic_sync(struct ble_gap_ext_disc_desc *disc); static int create_periodic_sync(struct ble_gap_ext_disc_desc *disc);
@ -22,6 +22,7 @@ static void start_scan(void);
static struct ble_hs_adv_fields fields; static struct ble_hs_adv_fields fields;
static bool synced = false; static bool synced = false;
uint8_t rsp_slot_idx = 0;
static int static int
gap_event_cb(struct ble_gap_event *event, void *arg) gap_event_cb(struct ble_gap_event *event, void *arg)
{ {
@ -51,20 +52,17 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
return 0; return 0;
case BLE_GAP_EVENT_PERIODIC_REPORT: case BLE_GAP_EVENT_PERIODIC_REPORT:
if (event->periodic_report.event_counter % 10 == 0) { ESP_LOGI(TAG, "[Periodic Adv Report] handle:%d, event_counter(%d), subevent(%d)",
// print every 10th event event->periodic_report.sync_handle,
ESP_LOGI(TAG, "[Periodic Adv Report] handle:%d, rssi:%d, data status:0x%x", event->periodic_report.event_counter,
event->periodic_report.sync_handle, event->periodic_report.rssi, event->periodic_report.subevent);
event->periodic_report.data_status);
ESP_LOGI(TAG, "[Periodic Adv Report] event_counter(%d), subevent(%d)",
event->periodic_report.event_counter, event->periodic_report.subevent);
}
rsp_slot_idx += 1;
struct ble_gap_periodic_adv_response_params param = { struct ble_gap_periodic_adv_response_params param = {
.request_event = event->periodic_report.event_counter, .request_event = event->periodic_report.event_counter,
.request_subevent = event->periodic_report.subevent, .request_subevent = event->periodic_report.subevent,
.response_subevent = event->periodic_report.subevent, .response_subevent = event->periodic_report.subevent,
.response_slot = 0 .response_slot = rsp_slot_idx,
}; };
struct os_mbuf *data = os_msys_get_pkthdr(BLE_PAWR_RSP_DATA_LEN, 0); struct os_mbuf *data = os_msys_get_pkthdr(BLE_PAWR_RSP_DATA_LEN, 0);
@ -79,8 +77,12 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
rc = ble_gap_periodic_adv_set_response_data(event->periodic_report.sync_handle, &param, data); rc = ble_gap_periodic_adv_set_response_data(event->periodic_report.sync_handle, &param, data);
if (rc) { if (rc) {
ESP_LOGE(TAG, "Set response data failed, subev(%x), rsp_slot(%d), rc(0x%x)", ESP_LOGE(TAG, "Set response data failed, sync handle: %d, subev(%x), rsp_slot(%d), rc(0x%x)",
sub_data_pattern[0], event->periodic_report.subevent, rc); event->periodic_report.sync_handle, param.response_subevent, param.response_slot, rc);
}
else{
ESP_LOGI(TAG, "[RSP Data Set] sync handle: %d, subev(%x), rsp_slot(%d), rc(0x%x)",
event->periodic_report.sync_handle, param.response_subevent, param.response_slot, rc);
} }
os_mbuf_free_chain(data); os_mbuf_free_chain(data);

View File

@ -13,23 +13,4 @@ CONFIG_BT_NIMBLE_MAX_PERIODIC_SYNCS=1
CONFIG_BT_NIMBLE_ROLE_CENTRAL=n CONFIG_BT_NIMBLE_ROLE_CENTRAL=n
CONFIG_BT_NIMBLE_ROLE_OBSERVER=y CONFIG_BT_NIMBLE_ROLE_OBSERVER=y
CONFIG_BT_CONTROLLER_DISABLED=y CONFIG_BT_CONTROLLER_ENABLED=y
#
# Host-controller Transport
#
CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT=1
CONFIG_UART_BAUDRATE_115200=y
CONFIG_BT_NIMBLE_UART_TX_PIN=20
CONFIG_BT_NIMBLE_UART_RX_PIN=21
CONFIG_UART_HW_FLOWCTRL_CTS_RTS=n
# CONFIG_BT_NIMBLE_HCI_UART_RTS_PIN=22
# CONFIG_BT_NIMBLE_HCI_UART_CTS_PIN=23
# end of Host-controller Transport
# end of NimBLE Options
# C6 Nordic
# TX: 20 ---- RX
# RX: 21 ---- TX
# RTS: 22 ---- CTS
# CTS: 23 ---- RTS

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -10,14 +10,15 @@
#include "nimble/nimble_port_freertos.h" #include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h" #include "host/ble_hs.h"
#define BLE_PAWR_NUM_SUBEVTS (5) #define BLE_PAWR_EVENT_INTERVAL (520)
#define BLE_PAWR_SUB_INTERVAL (12) /*!< Interval between subevents (N * 1.25 ms) */ #define BLE_PAWR_NUM_SUBEVTS (10)
#define BLE_PAWR_RSP_SLOT_DELAY (1) /*!< The first response slot delay (N * 1.25 ms)*/ #define BLE_PAWR_SUB_INTERVAL (52) /*!< Interval between subevents (N * 1.25 ms) */
#define BLE_PAWR_RSP_SLOT_SPACING (32) /*!< Time between response slots (N * 0.125 ms) */ #define BLE_PAWR_RSP_SLOT_DELAY (5) /*!< The first response slot delay (N * 1.25 ms)*/
#define BLE_PAWR_NUM_RSP_SLOTS (3) /*!< Number of subevent response slots */ #define BLE_PAWR_RSP_SLOT_SPACING (10) /*!< Time between response slots (N * 0.125 ms) */
#define BLE_PAWR_SUB_DATA_LEN (10) #define BLE_PAWR_NUM_RSP_SLOTS (25) /*!< Number of subevent response slots */
#define BLE_PAWR_SUB_DATA_LEN (20)
#define TAG "NimBLE_BLE_PAwR" #define TAG "NimBLE_BLE_PAwR_CONN"
static struct ble_gap_set_periodic_adv_subev_data_params sub_data_params[BLE_PAWR_NUM_SUBEVTS]; static struct ble_gap_set_periodic_adv_subev_data_params sub_data_params[BLE_PAWR_NUM_SUBEVTS];
static uint8_t sub_data_pattern[BLE_PAWR_SUB_DATA_LEN] = {0}; static uint8_t sub_data_pattern[BLE_PAWR_SUB_DATA_LEN] = {0};
@ -71,18 +72,28 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
uint8_t phy_mask; uint8_t phy_mask;
switch (event->type) { switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
printf("\n");
ESP_LOGI(TAG, "Connection established, conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n",event->connect.conn_handle,event->connect.adv_handle, event->connect.status);
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
if(rc == 0){
print_conn_desc(&desc);
}
else{
ESP_LOGE(TAG,"Failed to find Conn Information");
}
return 0; case BLE_GAP_EVENT_LINK_ESTAB:
if (event->link_estab.status == 0) {
ESP_LOGI(TAG, "[Connection established], conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n",event->link_estab.conn_handle, event->link_estab.adv_handle, event->link_estab.status);
rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
if (rc == 0) {
print_conn_desc(&desc);
}
else{
ESP_LOGE(TAG,"Failed to find Conn Information");
}
} else {
ESP_LOGW(TAG, "[Connection Failed], conn_handle = 0x%02x, Adv handle = 0x%0x, status = 0x%0x\n",event->link_estab.conn_handle, event->link_estab.adv_handle, event->link_estab.status);
conn = 0;
}
return 0;
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGI(TAG, "[Disconnected], conn_handle = 0x%02x, status = 0x%0x\n",event->disconnect.conn.conn_handle,event->disconnect.reason);
conn = 0;
return 0;
case BLE_GAP_EVENT_PER_SUBEV_DATA_REQ: case BLE_GAP_EVENT_PER_SUBEV_DATA_REQ:
ESP_LOGI(TAG, "[Request] data: %x, subevt start:%d, subevt count:%d", ESP_LOGI(TAG, "[Request] data: %x, subevt start:%d, subevt count:%d",
sub_data_pattern[0], sub_data_pattern[0],
@ -115,20 +126,17 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_PER_SUBEV_RESP: case BLE_GAP_EVENT_PER_SUBEV_RESP:
if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_INCOMPLETE) { if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE) {
// ESP_LOGI(TAG,"Incomplete response report received, discarding \n"); ESP_LOGI(TAG, "[Response] subevent:%d, response_slot:%d, data_length:%d",
}
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_RX_FAILED) {
// ESP_LOGI(TAG,"Controller failed to received the AUX_SYNC_SUBEVENT_RSP\n");
}
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_COMPLETE) {
ESP_LOGI(TAG, "[Response] subevent:%d, response_slot:%d, data_length:%d, data:%x",
event->periodic_adv_response.subevent, event->periodic_adv_response.subevent,
event->periodic_adv_response.response_slot, event->periodic_adv_response.response_slot,
event->periodic_adv_response.data_length, event->periodic_adv_response.data_length);
event->periodic_adv_response.data[0]); const uint8_t *data = event->periodic_adv_response.data;
ESP_LOGI(TAG, "data: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x",
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9]);
peer_addr.type=0; peer_addr.type=0;
memcpy(peer_addr.val,&event->periodic_adv_response.data[1],6); memcpy(peer_addr.val,&event->periodic_adv_response.data[2],6);
adv_handle = event->periodic_adv_response.adv_handle; adv_handle = event->periodic_adv_response.adv_handle;
subevent = event->periodic_adv_response.subevent; subevent = event->periodic_adv_response.subevent;
@ -138,15 +146,16 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
rc = ble_gap_connect_with_synced(0,adv_handle,subevent,&peer_addr,30000,phy_mask,NULL,NULL,NULL,gap_event_cb,NULL); rc = ble_gap_connect_with_synced(0,adv_handle,subevent,&peer_addr,30000,phy_mask,NULL,NULL,NULL,gap_event_cb,NULL);
if (rc != 0 ) { if (rc != 0 ) {
ESP_LOGI(TAG,"Error: Failed to connect to device , rc = %d\n",rc); ESP_LOGI(TAG,"Error: Failed to connect to device , rc = %d\n",rc);
}else {
ESP_LOGI(TAG,"Connection create sent, adv handle = %d, subevent = %d", adv_handle, subevent);
} }
conn = 1; conn = 1;
} }
} } else {
else if (event->periodic_adv_response.data_status == BLE_GAP_PER_ADV_DATA_STATUS_TRUNCATED) { ESP_LOGE(TAG, "[Response] subevent:%d, response_slot:%d, rsp_data status:%d",
// ESP_LOGI(TAG,"Truncated response report received, discarding\n"); event->periodic_adv_response.subevent,
} event->periodic_adv_response.response_slot,
else { event->periodic_adv_response.data_status);
ESP_LOGE(TAG,"Invalid data status\n");
} }
return 0; return 0;
@ -185,14 +194,14 @@ start_periodic_adv(void)
params.primary_phy = BLE_HCI_LE_PHY_CODED; params.primary_phy = BLE_HCI_LE_PHY_CODED;
params.secondary_phy = BLE_HCI_LE_PHY_1M; params.secondary_phy = BLE_HCI_LE_PHY_1M;
params.sid = 0; params.sid = 0;
params.itvl_min = BLE_GAP_ADV_ITVL_MS(100); params.itvl_min = BLE_GAP_ADV_ITVL_MS(50);
params.itvl_max = BLE_GAP_ADV_ITVL_MS(100); params.itvl_max = BLE_GAP_ADV_ITVL_MS(50);
rc = ble_gap_ext_adv_configure(instance, &params, NULL, gap_event_cb, NULL); rc = ble_gap_ext_adv_configure(instance, &params, NULL, gap_event_cb, NULL);
assert (rc == 0); assert (rc == 0);
memset(&adv_fields, 0, sizeof(adv_fields)); memset(&adv_fields, 0, sizeof(adv_fields));
adv_fields.name = (const uint8_t *)"Nimble_PAwR"; adv_fields.name = (const uint8_t *)"Nimble_PAwR_CONN";
adv_fields.name_len = strlen((char *)adv_fields.name); adv_fields.name_len = strlen((char *)adv_fields.name);
/* mbuf chain will be increased if needed */ /* mbuf chain will be increased if needed */
@ -212,9 +221,9 @@ start_periodic_adv(void)
pparams.itvl_max = BLE_GAP_PERIODIC_ITVL_MS(3000); pparams.itvl_max = BLE_GAP_PERIODIC_ITVL_MS(3000);
/* Configure the parameters of PAwR. */ /* Configure the parameters of PAwR. */
pparams.num_subevents = BLE_PAWR_NUM_SUBEVTS; pparams.num_subevents = BLE_PAWR_NUM_SUBEVTS;
pparams.subevent_interval = BLE_GAP_PERIODIC_ITVL_MS(300); pparams.subevent_interval = BLE_PAWR_SUB_INTERVAL;
pparams.response_slot_delay = BLE_GAP_PERIODIC_ITVL_MS(80); pparams.response_slot_delay = BLE_PAWR_RSP_SLOT_DELAY;
pparams.response_slot_spacing = 0xFF; pparams.response_slot_spacing = BLE_PAWR_RSP_SLOT_SPACING;
pparams.num_response_slots = BLE_PAWR_NUM_RSP_SLOTS; pparams.num_response_slots = BLE_PAWR_NUM_RSP_SLOTS;
rc = ble_gap_periodic_adv_configure(instance, &pparams); rc = ble_gap_periodic_adv_configure(instance, &pparams);

View File

@ -10,26 +10,7 @@ CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_EXT_ADV=y CONFIG_BT_NIMBLE_EXT_ADV=y
CONFIG_BT_NIMBLE_PERIODIC_ADV_WITH_RESPONSES=y CONFIG_BT_NIMBLE_PERIODIC_ADV_WITH_RESPONSES=y
CONFIG_BT_NIMBLE_MAX_PERIODIC_SYNCS=1 CONFIG_BT_NIMBLE_MAX_PERIODIC_SYNCS=1
CONFIG_BT_NIMBLE_ROLE_CENTRAL=n CONFIG_BT_NIMBLE_ROLE_CENTRAL=y
CONFIG_BT_NIMBLE_ROLE_OBSERVER=n CONFIG_BT_NIMBLE_ROLE_OBSERVER=n
CONFIG_BT_CONTROLLER_DISABLED=y CONFIG_BT_CONTROLLER_ENABLED=y
#
# Host-controller Transport
#
CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT=1
CONFIG_UART_BAUDRATE_115200=y
CONFIG_BT_NIMBLE_UART_TX_PIN=20
CONFIG_BT_NIMBLE_UART_RX_PIN=21
CONFIG_UART_HW_FLOWCTRL_CTS_RTS=n
# CONFIG_BT_NIMBLE_HCI_UART_RTS_PIN=22
# CONFIG_BT_NIMBLE_HCI_UART_CTS_PIN=23
# end of Host-controller Transport
# end of NimBLE Options
# C6 Nordic
# TX: 20 ---- RX
# RX: 21 ---- TX
# RTS: 22 ---- CTS
# CTS: 23 ---- RTS

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -11,9 +11,9 @@
#include "host/ble_hs.h" #include "host/ble_hs.h"
#include "host/util/util.h" #include "host/util/util.h"
#define TAG "NimBLE_BLE_PAwR" #define TAG "NimBLE_BLE_PAwR_CONN"
#define TARGET_NAME "Nimble_PAwR" #define TARGET_NAME "Nimble_PAwR_CONN"
#define BLE_PAWR_RSP_DATA_LEN (20) #define BLE_PAWR_RSP_DATA_LEN (10)
static uint8_t sub_data_pattern[BLE_PAWR_RSP_DATA_LEN] = {0}; static uint8_t sub_data_pattern[BLE_PAWR_RSP_DATA_LEN] = {0};
static int create_periodic_sync(struct ble_gap_ext_disc_desc *disc); static int create_periodic_sync(struct ble_gap_ext_disc_desc *disc);
@ -65,16 +65,25 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
struct ble_gap_ext_disc_desc *disc; struct ble_gap_ext_disc_desc *disc;
switch (event->type) { switch (event->type) {
case BLE_GAP_EVENT_CONNECT: case BLE_GAP_EVENT_LINK_ESTAB:
ESP_LOGI(TAG, "Connection established, conn_handle = 0x%0x, sync handle= 0x%02x, status = 0x%0x\n",event->connect.conn_handle, event->connect.sync_handle, event->connect.status); if (event->link_estab.status == 0) {
rc = ble_gap_conn_find(event->connect.conn_handle, &desc); ESP_LOGI(TAG, "Connection established, conn_handle = 0x%0x, sync handle= 0x%02x, status = 0x%0x\n",event->link_estab.conn_handle, event->link_estab.sync_handle, event->link_estab.status);
if(rc == 0){ rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
if (rc == 0) {
print_conn_desc(&desc); print_conn_desc(&desc);
} }
else{ else{
ESP_LOGE(TAG,"Failed to find Conn Information"); ESP_LOGE(TAG,"Failed to find Conn Information");
} }
return 0; } else{
ESP_LOGW(TAG, "[Connection Failed], conn_handle = 0x%02x, sync handle = 0x%0x, status = 0x%0x\n",event->link_estab.conn_handle, event->link_estab.sync_handle, event->link_estab.status);
}
return 0;
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGW(TAG, "[Disconnected], conn_handle = 0x%02x, status = 0x%0x\n",event->disconnect.conn.conn_handle,event->disconnect.reason);
return 0;
case BLE_GAP_EVENT_EXT_DISC: case BLE_GAP_EVENT_EXT_DISC:
disc = &event->ext_disc; disc = &event->ext_disc;
addr = disc->addr.val; addr = disc->addr.val;
@ -90,26 +99,22 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
return 0; return 0;
} }
if (disc->periodic_adv_itvl && fields.name_len && !memcmp(fields.name, TARGET_NAME, strlen(TARGET_NAME))) { if (fields.name_len && !memcmp(fields.name, TARGET_NAME, strlen(TARGET_NAME))) {
create_periodic_sync(disc); create_periodic_sync(disc);
} }
return 0; return 0;
case BLE_GAP_EVENT_PERIODIC_REPORT: case BLE_GAP_EVENT_PERIODIC_REPORT:
if (event->periodic_report.event_counter % 10 == 0) { ESP_LOGI(TAG, "[Periodic Adv Report] handle:%d, event_counter(%d), subevent(%d)",
// print every 10th event event->periodic_report.sync_handle,
ESP_LOGI(TAG, "[Periodic Adv Report] handle:%d, rssi:%d, data status:0x%x", event->periodic_report.event_counter,
event->periodic_report.sync_handle, event->periodic_report.rssi, event->periodic_report.subevent);
event->periodic_report.data_status);
ESP_LOGI(TAG, "[Periodic Adv Report] event_counter(%d), subevent(%d)",
event->periodic_report.event_counter, event->periodic_report.subevent);
}
struct ble_gap_periodic_adv_response_params param = { struct ble_gap_periodic_adv_response_params param = {
.request_event = event->periodic_report.event_counter, .request_event = event->periodic_report.event_counter,
.request_subevent = event->periodic_report.subevent, .request_subevent = event->periodic_report.subevent,
.response_subevent = event->periodic_report.subevent, .response_subevent = event->periodic_report.subevent,
.response_slot = 0 .response_slot = 2,
}; };
struct os_mbuf *data = os_msys_get_pkthdr(BLE_PAWR_RSP_DATA_LEN, 0); struct os_mbuf *data = os_msys_get_pkthdr(BLE_PAWR_RSP_DATA_LEN, 0);
@ -118,16 +123,22 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
return 0; return 0;
} }
// create a special data for checking manually in ADV side // create a special data for checking manually in ADV side
sub_data_pattern[0] = event->periodic_report.data[0];
sub_data_pattern[0] = event->periodic_report.subevent;
rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, device_addr, NULL); rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, device_addr, NULL);
memset(sub_data_pattern + 1, event->periodic_report.subevent, BLE_PAWR_RSP_DATA_LEN - 1); sub_data_pattern[1] = param.response_slot;
memcpy(sub_data_pattern + 1,device_addr,BLE_DEV_ADDR_LEN); memcpy(&sub_data_pattern[2],device_addr,BLE_DEV_ADDR_LEN);
os_mbuf_append(data, sub_data_pattern, BLE_PAWR_RSP_DATA_LEN); os_mbuf_append(data, sub_data_pattern, BLE_PAWR_RSP_DATA_LEN);
rc = ble_gap_periodic_adv_set_response_data(event->periodic_report.sync_handle, &param, data); rc = ble_gap_periodic_adv_set_response_data(event->periodic_report.sync_handle, &param, data);
if (rc) { if (rc) {
ESP_LOGE(TAG, "Set response data failed, subev(%x), rsp_slot(%d), rc(0x%x)", ESP_LOGE(TAG, "Set response data failed, sync handle: %d, subev(%x), rsp_slot(%d), rc(0x%x)",
sub_data_pattern[0], event->periodic_report.subevent, rc); event->periodic_report.sync_handle, param.response_subevent, param.response_slot, rc);
} else {
ESP_LOGW(TAG, "[RSP Data Set] sync handle: %d, subev(%x), rsp_slot(%d), rc(0x%x)",
event->periodic_report.sync_handle, param.response_subevent, param.response_slot, rc);
} }
os_mbuf_free_chain(data); os_mbuf_free_chain(data);
@ -151,7 +162,7 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
ble_gap_disc_cancel(); ble_gap_disc_cancel();
// choose subevents in range 0 to (num_subevents - 1) // choose subevents in range 0 to (num_subevents - 1)
uint8_t subevents[] = {0, 1, 2, 3, 4}; uint8_t subevents[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int result = ble_gap_periodic_adv_sync_subev(event->periodic_sync.sync_handle, 0, sizeof(subevents), subevents); int result = ble_gap_periodic_adv_sync_subev(event->periodic_sync.sync_handle, 0, sizeof(subevents), subevents);
if (result == ESP_OK) { if (result == ESP_OK) {
ESP_LOGI(TAG, "[Subevent Sync OK] sync handle:%d, sync_subevents:%d\n", event->periodic_sync.sync_handle, sizeof(subevents)); ESP_LOGI(TAG, "[Subevent Sync OK] sync handle:%d, sync_subevents:%d\n", event->periodic_sync.sync_handle, sizeof(subevents));

View File

@ -13,23 +13,4 @@ CONFIG_BT_NIMBLE_MAX_PERIODIC_SYNCS=1
CONFIG_BT_NIMBLE_ROLE_CENTRAL=n CONFIG_BT_NIMBLE_ROLE_CENTRAL=n
CONFIG_BT_NIMBLE_ROLE_OBSERVER=y CONFIG_BT_NIMBLE_ROLE_OBSERVER=y
CONFIG_BT_CONTROLLER_DISABLED=y CONFIG_BT_CONTROLLER_ENABLED=y
#
# Host-controller Transport
#
CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT=1
CONFIG_UART_BAUDRATE_115200=y
CONFIG_BT_NIMBLE_UART_TX_PIN=20
CONFIG_BT_NIMBLE_UART_RX_PIN=21
CONFIG_UART_HW_FLOWCTRL_CTS_RTS=n
# CONFIG_BT_NIMBLE_HCI_UART_RTS_PIN=22
# CONFIG_BT_NIMBLE_HCI_UART_CTS_PIN=23
# end of Host-controller Transport
# end of NimBLE Options
# C6 Nordic
# TX: 20 ---- RX
# RX: 21 ---- TX
# RTS: 22 ---- CTS
# CTS: 23 ---- RTS