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
@@ -1,4 +1,15 @@
menu "Example Configuration"
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
@@ -20,12 +20,17 @@
"use a supported target from README.md (e.g. esp32h2, esp32c6) and run idf.py set-target."
#endif
#define BLE_PEER_NAME "esp-multi-conn"
#define BLE_PEER_NAME_DEFAULT "esp-multi-conn"
#define BLE_PEER_MAX_NUM (MYNEWT_VAL(BLE_MAX_CONNECTIONS) - 1)
#define BLE_PREF_EVT_LEN_MS (5)
#define BLE_PREF_CONN_ITVL_MS (BLE_PEER_MAX_NUM * BLE_PREF_EVT_LEN_MS)
static const char *TAG = "ESP_MULTI_CONN_CENT";
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char remote_device_name[32];
#else
static char remote_device_name[32] = BLE_PEER_NAME_DEFAULT;
#endif
static const ble_uuid_t *remote_svc_uuid =
BLE_UUID128_DECLARE(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
@@ -44,6 +49,20 @@ static void ble_cent_connect(void *disc);
static uint8_t own_addr_type;
static uint8_t s_ble_multi_conn_num = 0;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_multi_conn_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
/**
* Called when service discovery of the specified peer has completed.
*/
@@ -91,12 +110,26 @@ ble_cent_client_gap_event(struct ble_gap_event *event, void *arg)
switch (event->type) {
case BLE_GAP_EVENT_EXT_DISC:
rc = ble_hs_adv_parse_fields(&fields, event->ext_disc.data, event->ext_disc.length_data);
if (rc != 0 || fields.name == NULL) {
return 0;
}
/* An advertisement report was received during GAP discovery. */
if ((rc == 0) && fields.name && (fields.name_len >= strlen(BLE_PEER_NAME)) &&
!strncmp((const char *)fields.name, BLE_PEER_NAME, strlen(BLE_PEER_NAME))) {
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
if (fields.name_len == strlen(remote_device_name) &&
memcmp(fields.name, remote_device_name, fields.name_len) == 0) {
ESP_LOGI(TAG, "Found device: addr: %s, name: %s",
addr_str(event->ext_disc.addr.val), remote_device_name);
ble_cent_connect(&event->ext_disc);
}
#else
/* An advertisement report was received during GAP discovery. */
if ((fields.name_len >= strlen(remote_device_name)) &&
!strncmp((const char *)fields.name, remote_device_name, strlen(remote_device_name))) {
ESP_LOGI(TAG, "Found device: addr: %s, name: %.*s",
addr_str(event->ext_disc.addr.val), fields.name_len, fields.name);
ble_cent_connect(&event->ext_disc);
}
#endif
return 0;
@@ -312,7 +345,7 @@ ble_cent_scan(void)
/**
* Connects to the sender of the specified advertisement.The advertisement must contain its full
* name which we will compare with 'BLE_PEER_NAME'.
* name which we will compare with 'remote_device_name'.
*/
static void
ble_cent_connect(void *disc)
@@ -430,7 +463,7 @@ blecent_on_sync(void)
}
/* We will function as both the central and peripheral device, connecting to all peripherals
* with the name of BLE_PEER_NAME. Meanwhile, a connectable advertising will be enabled.
* with the name of remote_device_name. Meanwhile, a connectable advertising will be enabled.
* In this example, we register two gap callback functions.
* - ble_cent_client_gap_event: Used by the central.
* - ble_cent_server_gap_event: Used by the peripheral.
@@ -466,6 +499,13 @@ app_main(void)
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(remote_device_name, esp_ble_multi_conn_get_example_name(), sizeof(remote_device_name) - 1);
remote_device_name[sizeof(remote_device_name) - 1] = '\0';
ESP_LOGI(TAG, "DeviceName:%s, CIID:%02X, PipelineID:%05X, ChipID:%02X",
remote_device_name, CONFIG_EXAMPLE_CI_ID, CONFIG_EXAMPLE_CI_PIPELINE_ID, CONFIG_IDF_FIRMWARE_CHIP_ID);
#endif
/* Configure the host. */
ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = blecent_on_sync;
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=29
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=5
CONFIG_BT_NIMBLE_GATT_MAX_PROCS=5
@@ -17,4 +17,16 @@ menu "Example Configuration"
To simulate multiple connections with only one device,
restart the advertisement once a connection has been established.
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
@@ -3,6 +3,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_random.h"
@@ -15,6 +16,7 @@
#include "ble_multi_conn_prph.h"
#if CONFIG_EXAMPLE_EXTENDED_ADV
#if !(CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID)
static uint8_t ext_adv_pattern_1[] = {
0x02, BLE_HS_ADV_TYPE_FLAGS, 0x06,
0x03, BLE_HS_ADV_TYPE_COMP_UUIDS16, 0xab, 0xcd,
@@ -22,14 +24,30 @@ static uint8_t ext_adv_pattern_1[] = {
0x11, BLE_HS_ADV_TYPE_COMP_NAME, 'e', 's', 'p', '-', 'm', 'u', 'l', 't', 'i', '-', 'c', 'o', 'n', 'n', '-', 'e',
};
#endif
#endif
static const char *TAG = "ESP_MULTI_CONN_PRPH";
static volatile uint8_t s_ble_prph_conn_num = 0;
static SemaphoreHandle_t s_sem_restart_adv = NULL;
static char device_name[32] = "esp-multi-conn";
static int ble_prph_gap_event(struct ble_gap_event *event, void *arg);
void ble_store_config_init(void);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_multi_conn_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
/**
* Enables advertising with the following parameters:
* o General discoverable mode.
@@ -49,6 +67,11 @@ ble_prph_advertise(void)
struct ble_gap_ext_adv_params params;
struct os_mbuf *data;
uint8_t instance = 0;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
uint8_t ci_adv_data[32];
uint8_t ci_adv_len;
uint8_t name_len;
#endif
/* First check if any instance is already active */
if(ble_gap_ext_adv_active(instance)) {
@@ -73,10 +96,24 @@ ble_prph_advertise(void)
ble_prph_gap_event, NULL);
assert(rc == 0);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
name_len = strlen(device_name);
memset(ci_adv_data, 0, sizeof(ci_adv_data));
ci_adv_data[0] = name_len + 1;
ci_adv_data[1] = BLE_HS_ADV_TYPE_COMP_NAME;
memcpy(&ci_adv_data[2], device_name, name_len);
ci_adv_len = 2 + name_len;
data = os_msys_get_pkthdr(ci_adv_len, 0);
assert(data);
rc = os_mbuf_append(data, ci_adv_data, ci_adv_len);
assert(rc == 0);
#else
data = os_msys_get_pkthdr(sizeof(ext_adv_pattern_1), 0);
assert(data);
rc = os_mbuf_append(data, ext_adv_pattern_1, sizeof(ext_adv_pattern_1));
assert(rc == 0);
#endif
rc = ble_gap_ext_adv_set_data(instance, data);
assert(rc == 0);
@@ -94,9 +131,6 @@ ble_prph_advertise(void)
#else
struct ble_gap_adv_params adv_params;
struct ble_hs_adv_fields fields;
#if CONFIG_BT_NIMBLE_GAP_SERVICE
const char *name;
#endif
if (ble_gap_adv_active()) {
return;
@@ -134,9 +168,8 @@ ble_prph_advertise(void)
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
#if CONFIG_BT_NIMBLE_GAP_SERVICE
name = ble_svc_gap_device_name();
fields.name = (uint8_t *)name;
fields.name_len = strlen(name);
fields.name = (uint8_t *)device_name;
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
#endif
@@ -293,6 +326,15 @@ app_main(void)
ESP_LOGE(TAG, "Failed to init nimble %d ", ret);
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(device_name, esp_ble_multi_conn_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
ESP_LOGI(TAG, "CONFIG_BT_NIMBLE_MAX_CONNECTIONS=%d.", CONFIG_BT_NIMBLE_MAX_CONNECTIONS);
/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
@@ -306,7 +348,7 @@ app_main(void)
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("esp-multi-conn");
rc = ble_svc_gap_device_name_set(device_name);
assert(rc == 0);
#endif
@@ -0,0 +1,3 @@
CONFIG_EXAMPLE_CI_ID=29
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4