feat(example): add pytest for nimble examples

This commit is contained in:
yuanhonghui
2026-08-12 15:46:14 +08:00
parent b418d72bb0
commit eebeb6b62c
125 changed files with 4249 additions and 162 deletions
@@ -44,4 +44,16 @@ menu "Example Configuration"
prompt "Use Secure Connection feature"
help
Use this option to enable/disable Security Manager Secure Connection 4.2 feature.
config EXAMPLE_CI_ID
int "example id for CI test"
default 0
help
This config the example id for CI test. Only for internal used.
config EXAMPLE_CI_PIPELINE_ID
int "The pipeline id for CI test"
default 0
help
This config the pipeline id for CI test. Only for internal used.
endmenu
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
/* BLE */
@@ -15,6 +16,7 @@
#include "services/gap/ble_svc_gap.h"
#include "phy_prph.h"
#if !(CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID)
static uint8_t ext_adv_pattern_1M[] = {
0x02, BLE_HS_ADV_TYPE_FLAGS, 0x06,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xF2, 0xAB,
@@ -33,14 +35,30 @@ static uint8_t ext_adv_pattern_coded[] = {
0x11, BLE_HS_ADV_TYPE_COMP_NAME, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', 'c', 'o', 'd', 'e',
'd',
};
#endif
static const char *tag = "NimBLE_BLE_PHY_PRPH";
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t own_addr_type;
static char device_name[32] = "bleprph-phy";
static uint8_t s_current_phy;
void ble_store_config_init(void);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_phy_get_example_name(void)
{
static char example_name[32];
memset(example_name, 0, sizeof(example_name));
snprintf(example_name, sizeof(example_name), "BE%02X_%05X_%02X",
CONFIG_EXAMPLE_CI_ID & 0xFF,
CONFIG_EXAMPLE_CI_PIPELINE_ID & 0xFFFFF,
CONFIG_IDF_FIRMWARE_CHIP_ID & 0xFF);
return example_name;
}
#endif
/* Set default LE PHY before establishing connection */
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
@@ -91,6 +109,24 @@ ext_get_data(uint8_t ext_adv_pattern[], int size)
return data;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
/* Build CI advertisement data that contains only the Complete Local Name. */
static struct os_mbuf *
build_ci_adv_data(const char *name)
{
uint8_t buf[32];
uint8_t name_len = (uint8_t)strlen(name);
if (name_len > sizeof(buf) - 2) {
name_len = sizeof(buf) - 2;
}
buf[0] = name_len + 1;
buf[1] = BLE_HS_ADV_TYPE_COMP_NAME;
memcpy(&buf[2], name, name_len);
return ext_get_data(buf, 2 + name_len);
}
#endif
/**
* Enables advertising with the following parameters:
* o General discoverable mode.
@@ -123,21 +159,33 @@ ext_bleprph_advertise(void)
case BLE_HCI_LE_PHY_1M_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_1M;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
data = build_ci_adv_data(device_name);
#else
data = ext_get_data(ext_adv_pattern_1M, sizeof(ext_adv_pattern_1M));
#endif
params.sid = 0;
break;
case BLE_HCI_LE_PHY_2M_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_2M;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
data = build_ci_adv_data(device_name);
#else
data = ext_get_data(ext_adv_pattern_2M, sizeof(ext_adv_pattern_2M));
#endif
params.sid = 1;
break;
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_CODED;
params.secondary_phy = BLE_HCI_LE_PHY_CODED;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
data = build_ci_adv_data(device_name);
#else
data = ext_get_data(ext_adv_pattern_coded, sizeof(ext_adv_pattern_coded));
#endif
params.sid = 2;
break;
}
@@ -324,6 +372,13 @@ app_main(void)
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(device_name, esp_ble_phy_get_example_name(), sizeof(device_name) - 1);
device_name[sizeof(device_name) - 1] = '\0';
ESP_LOGI(tag, "DeviceName:%s, CIID:%02X, PipelineID:%05X, ChipID:%02X",
device_name, CONFIG_EXAMPLE_CI_ID, CONFIG_EXAMPLE_CI_PIPELINE_ID, CONFIG_IDF_FIRMWARE_CHIP_ID);
#endif
/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
@@ -354,7 +409,7 @@ app_main(void)
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("bleprph-phy");
rc = ble_svc_gap_device_name_set(device_name);
assert(rc == 0);
#endif
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=30
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
@@ -0,0 +1,2 @@
CONFIG_EXAMPLE_CI_ID=30
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}