fix(nimble): Added support for 1m, 2m, coded phy channels and optimized throughput

This commit is contained in:
Shreeyash
2025-05-06 17:58:17 +05:30
parent 45d95b1bd8
commit 4dfa2beaf1
8 changed files with 422 additions and 68 deletions
@@ -0,0 +1,14 @@
menu "Example Configuration"
config EXAMPLE_EXTENDED_ADV
bool
depends on SOC_BLE_50_SUPPORTED && BT_NIMBLE_50_FEATURE_SUPPORT
default y if SOC_ESP_NIMBLE_CONTROLLER
select BT_NIMBLE_EXT_ADV
prompt "Enable Extended Adv"
help
Use this option to enable extended advertising in the example.
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
also disabled from Nimble stack menuconfig
endmenu
@@ -17,7 +17,20 @@
#include "gatts_sens.h"
#include "../src/ble_hs_hci_priv.h"
#define NOTIFY_THROUGHPUT_PAYLOAD 500
#if CONFIG_EXAMPLE_EXTENDED_ADV
static uint8_t ext_adv_pattern[] = {
0x02, 0x01, 0x06,
0x03, 0x03, 0xab, 0xcd,
0x03, 0x03, 0xAB, 0xF2,
0x0e, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'b', 'l', 'e', 'p', 'r', 'p', 'h'
};
static uint8_t s_current_phy;
#else
static const char *device_name = "nimble_prph";
#endif
#define NOTIFY_THROUGHPUT_PAYLOAD 495
#define MIN_REQUIRED_MBUF 2 /* Assuming payload of 500Bytes and each mbuf can take 292Bytes. */
#define PREFERRED_MTU_VALUE 512
#define LL_PACKET_TIME 2120
@@ -25,7 +38,6 @@
#define MTU_DEF 512
static const char *tag = "bleprph_throughput";
static const char *device_name = "nimble_prph";
static SemaphoreHandle_t notify_sem;
static bool notify_state;
static int notify_test_time = 60;
@@ -36,6 +48,33 @@ static uint8_t gatts_addr_type;
static int gatts_gap_event(struct ble_gap_event *event, void *arg);
#if CONFIG_EXAMPLE_EXTENDED_ADV
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
if (rc == 0) {
ESP_LOGI(tag, "Default LE PHY set successfully");
} else {
ESP_LOGE(tag, "Failed to set default LE PHY");
}
}
static struct os_mbuf *
ext_get_data(uint8_t ext_adv_pattern[], int size)
{
struct os_mbuf *data;
int rc;
data = os_msys_get_pkthdr(size, 0);
assert(data);
rc = os_mbuf_append(data, ext_adv_pattern, size);
assert(rc == 0);
return data;
}
#endif
/**
* Utility function to log an array of bytes.
*/
@@ -83,6 +122,54 @@ bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
desc->sec_state.bonded);
}
#if CONFIG_EXAMPLE_EXTENDED_ADV
/**
* Enables advertising with the following parameters:
* o General discoverable mode.
* o Undirected connectable mode.
*/
static void
ext_bleprph_advertise(void)
{
struct ble_gap_ext_adv_params params;
struct os_mbuf *data = NULL;
uint8_t instance = 0;
int rc;
/* use defaults for non-set params */
memset (&params, 0, sizeof(params));
params.scannable = 1;
params.legacy_pdu = 1;
/*enable connectable advertising for all Phy*/
params.connectable = 1;
/* advertise using random addr */
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
/* Set current phy; get mbuf for scan rsp data; fill mbuf with scan rsp data */
params.primary_phy = BLE_HCI_LE_PHY_1M_PREF_MASK ;
params.secondary_phy = BLE_HCI_LE_PHY_2M_PREF_MASK ;
data = ext_get_data(ext_adv_pattern, sizeof(ext_adv_pattern));
params.sid = 0;
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
/* configure instance 0 */
rc = ble_gap_ext_adv_configure(instance, &params, NULL,
gatts_gap_event, NULL);
assert (rc == 0);
rc = ble_gap_ext_adv_set_data(instance, data);
assert (rc == 0);
/* start advertising */
rc = ble_gap_ext_adv_start(instance, 0, 0);
assert (rc == 0);
}
#else
/*
* Enables advertising with parameters:
* o General discoverable mode
@@ -139,6 +226,7 @@ gatts_advertise(void)
return;
}
}
#endif
/* This function sends notifications to the client */
static void
@@ -189,7 +277,6 @@ notify_task(void *arg)
om = ble_hs_mbuf_from_flat(payload, sizeof(payload));
if (om == NULL) {
/* Memory not available for mbuf */
ESP_LOGE(tag, "No MBUFs available from pool, retry..");
vTaskDelay(100 / portTICK_PERIOD_MS);
}
} while (om == NULL);
@@ -251,7 +338,11 @@ gatts_gap_event(struct ble_gap_event *event, void *arg)
if (event->connect.status != 0) {
/* Connection failed; resume advertising */
#if CONFIG_EXAMPLE_EXTENDED_ADV
ext_bleprph_advertise();
#else
gatts_advertise();
#endif
}
rc = ble_hs_hci_util_set_data_len(event->connect.conn_handle,
@@ -268,7 +359,12 @@ gatts_gap_event(struct ble_gap_event *event, void *arg)
ESP_LOGI(tag, "disconnect; reason = %d", event->disconnect.reason);
/* Connection terminated; resume advertising */
#if CONFIG_EXAMPLE_EXTENDED_ADV
ble_gap_ext_adv_stop(0);
ext_bleprph_advertise();
#else
gatts_advertise();
#endif
break;
case BLE_GAP_EVENT_CONN_UPDATE:
@@ -282,7 +378,11 @@ gatts_gap_event(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_ADV_COMPLETE:
ESP_LOGI(tag, "adv complete ");
#if CONFIG_EXAMPLE_EXTENDED_ADV
ext_bleprph_advertise();
#else
gatts_advertise();
#endif
break;
case BLE_GAP_EVENT_SUBSCRIBE:
@@ -321,7 +421,15 @@ gatts_gap_event(struct ble_gap_event *event, void *arg)
event->mtu.conn_handle,
event->mtu.value);
break;
}
#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE:
ESP_LOGI(tag, "PHY Update Event: Status=%d, Conn_Handle=0x%04X, TX_PHY=%d, RX_PHY=%d",
event->phy_updated.status, event->phy_updated.conn_handle,
event->phy_updated.tx_phy, event->phy_updated.rx_phy);
#endif
}
return 0;
}
@@ -329,6 +437,9 @@ static void
gatts_on_sync(void)
{
int rc;
#if CONFIG_EXAMPLE_EXTENDED_ADV
uint8_t all_phy;
#endif
uint8_t addr_val[6] = {0};
rc = ble_hs_id_infer_auto(0, &gatts_addr_type);
@@ -338,7 +449,18 @@ gatts_on_sync(void)
ESP_LOGI(tag, "Device Address: ");
print_addr(addr_val);
/* Begin advertising */
#if CONFIG_EXAMPLE_EXTENDED_ADV
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;;
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
ext_bleprph_advertise();
#else
gatts_advertise();
#endif
}
static void
@@ -390,9 +512,11 @@ void app_main(void)
rc = gatt_svr_init();
assert(rc == 0);
#if !(CONFIG_EXAMPLE_EXTENDED_ADV)
/* Set the default device name */
rc = ble_svc_gap_device_name_set(device_name);
assert(rc == 0);
#endif
/* Start the task */
nimble_port_freertos_init(gatts_host_task);
@@ -11,6 +11,8 @@ CONFIG_BTDM_CTRL_MODE_BTDM=n
CONFIG_BT_BLUEDROID_ENABLED=n
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_ATT_PREFERRED_MTU=512
CONFIG_BT_NIMBLE_ACL_BUF_COUNT=20
CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=255
CONFIG_BT_NIMBLE_MSYS1_BLOCK_COUNT=50
CONFIG_BT_NIMBLE_TRANSPORT_ACL_FROM_LL_COUNT=20
CONFIG_BT_NIMBLE_TRANSPORT_EVT_SIZE=255
CONFIG_BT_NIMBLE_MSYS_1_BLOCK_COUNT=50
CONFIG_BT_NIMBLE_LOG_LEVEL=4
CONFIG_BT_NIMBLE_LOG_LEVEL_NONE=y