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
@@ -0,0 +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
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
/* BLE */
@@ -18,7 +19,7 @@
#define PEER_ADDR_VAL_SIZE 6
static const char *tag = "NimBLE_SPP_BLE_CENT";
static const char *tag = "NimBLE_SPP_CLIENT";
static int ble_spp_client_gap_event(struct ble_gap_event *event, void *arg);
QueueHandle_t spp_common_uart_queue = NULL;
void ble_store_config_init(void);
@@ -27,6 +28,22 @@ static SemaphoreHandle_t g_attr_handle_mutex = NULL;
static void ble_spp_client_scan(void);
static ble_addr_t connected_addr[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char remote_device_name[32];
static char *esp_ble_spp_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
#if MYNEWT_VAL(BLE_GATTC)
static void ble_spp_client_write_subscribe(const struct peer *peer)
{
@@ -156,6 +173,12 @@ ble_spp_client_scan(void)
disc_params.filter_policy = 0;
disc_params.limited = 0;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
/* Full scan improves discovery reliability in multi-board CI environments. */
disc_params.itvl = BLE_GAP_SCAN_ITVL_MS(50);
disc_params.window = BLE_GAP_SCAN_ITVL_MS(50);
#endif
rc = ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params,
ble_spp_client_gap_event, NULL);
if (rc != 0) {
@@ -196,6 +219,14 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
return 0;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
if (fields.name != NULL &&
fields.name_len == strlen(remote_device_name) &&
memcmp(fields.name, remote_device_name, fields.name_len) == 0) {
return 1;
}
return 0;
#else
/* The device has to advertise support for the SPP
* service (0xABF0).
*/
@@ -205,6 +236,7 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
}
}
return 0;
#endif
}
/**
@@ -223,6 +255,10 @@ ble_spp_client_connect_if_interesting(const struct ble_gap_disc_desc *disc)
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
ESP_LOGI(tag, "Found device: %s, name: %s", addr_str(disc->addr.val), remote_device_name);
#endif
#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
@@ -308,6 +344,8 @@ ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
}
print_conn_desc(&desc);
MODLOG_DFLT(INFO, "\n");
ESP_LOGI(tag, "Connected, conn_handle %d, remote %s",
event->connect.conn_handle, addr_str(desc.peer_ota_addr.val));
/* Remember peer. */
rc = peer_add(event->connect.conn_handle);
@@ -486,7 +524,8 @@ void ble_client_uart_task(void *pvParameters)
static void ble_spp_uart_init(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
/* Keep console baud (e.g. 74880 on ESP32-C2 26MHz XTAL) when sharing UART0. */
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
@@ -536,6 +575,14 @@ app_main(void)
/* Initialize UART driver and start uart task */
ble_spp_uart_init();
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
/* Log after UART0 init so DeviceName is not corrupted by uart_driver_install. */
strncpy(remote_device_name, esp_ble_spp_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 = ble_spp_client_on_reset;
ble_hs_cfg.sync_cb = ble_spp_client_on_sync;
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=28
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=28
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
@@ -4,4 +4,16 @@ menu "Example Configuration"
int "IO Type"
default 3
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 */
@@ -17,15 +18,31 @@
#include "ble_spp_server.h"
#include "driver/uart.h"
static const char *tag = "NimBLE_SPP_SERVER";
static int ble_spp_server_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t own_addr_type;
int gatt_svr_register(void);
QueueHandle_t spp_common_uart_queue = NULL;
static bool conn_handle_subs[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
static uint16_t ble_spp_svc_gatt_read_val_handle;
static char device_name[32] = "nimble-ble-spp-svr";
void ble_store_config_init(void);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_spp_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
/**
* Logs information about a connection to the console.
*/
@@ -90,18 +107,19 @@ ble_spp_server_advertise(void)
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
#if CONFIG_BT_NIMBLE_GAP_SERVICE
const char *name;
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
fields.uuids16 = (ble_uuid16_t[]) {
#if !(CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID)
static const ble_uuid16_t adv_uuids16[] = {
BLE_UUID16_INIT(BLE_SVC_SPP_UUID16)
};
fields.uuids16 = adv_uuids16;
fields.num_uuids16 = 1;
fields.uuids16_is_complete = 1;
#endif
rc = ble_gap_adv_set_fields(&fields);
if (rc != 0) {
@@ -152,6 +170,11 @@ ble_spp_server_gap_event(struct ble_gap_event *event, void *arg)
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
ble_spp_server_print_conn_desc(&desc);
ESP_LOGI(tag, "Connected, conn_handle %d, remote %02x:%02x:%02x:%02x:%02x:%02x",
event->connect.conn_handle,
desc.peer_ota_addr.val[5], desc.peer_ota_addr.val[4],
desc.peer_ota_addr.val[3], desc.peer_ota_addr.val[2],
desc.peer_ota_addr.val[1], desc.peer_ota_addr.val[0]);
}
MODLOG_DFLT(INFO, "\n");
if (event->connect.status != 0 || CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1) {
@@ -250,7 +273,7 @@ ble_spp_server_on_sync(void)
void ble_spp_server_host_task(void *param)
{
MODLOG_DFLT(INFO, "BLE Host Task Started");
ESP_LOGI(tag, "BLE Host Task Started");
/* This function will return only when nimble_port_stop() is executed */
nimble_port_run();
@@ -404,7 +427,8 @@ void ble_server_uart_task(void *pvParameters)
static void ble_spp_uart_init(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
/* Keep console baud (e.g. 74880 on ESP32-C2 26MHz XTAL) when sharing UART0. */
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
@@ -480,9 +504,16 @@ app_main(void)
rc = gatt_svr_init();
assert(rc == 0);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(device_name, esp_ble_spp_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
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("nimble-ble-spp-svr");
rc = ble_svc_gap_device_name_set(device_name);
assert(rc == 0);
#endif
#endif
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=28
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=28
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}