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
@@ -24,4 +24,16 @@ menu "Example Configuration"
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
also disabled from Nimble stack menuconfig
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,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
/* BLE */
@@ -20,8 +22,23 @@
#endif
static const char *tag = "NimBLE_CTS_CENT";
static char remote_device_name[32];
static int ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_cts_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
static char *day_of_week[8] = {
"Unknown",
"Monday",
@@ -186,6 +203,12 @@ ble_cts_cent_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_cts_cent_gap_event, NULL);
if (rc != 0) {
@@ -222,6 +245,24 @@ ext_ble_cts_cent_should_connect(const struct ble_gap_ext_disc_desc *disc)
}
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
while (offset < disc->length_data) {
ad_struct_len = disc->data[offset];
if (ad_struct_len == 0 || offset + ad_struct_len + 1 > disc->length_data) {
break;
}
if (disc->data[offset + 1] == BLE_HS_ADV_TYPE_COMP_NAME ||
disc->data[offset + 1] == BLE_HS_ADV_TYPE_INCOMP_NAME) {
int name_len = ad_struct_len - 1;
if (name_len == (int)strlen(remote_device_name) &&
memcmp(&disc->data[offset + 2], remote_device_name, name_len) == 0) {
return 1;
}
}
offset += ad_struct_len + 1;
}
return 0;
#else
/* The device has to advertise support for the CTS
* service (0x1805).
*/
@@ -254,6 +295,7 @@ ext_ble_cts_cent_should_connect(const struct ble_gap_ext_disc_desc *disc)
}
return 0;
#endif
}
#else
@@ -262,7 +304,6 @@ ble_cts_cent_should_connect(const struct ble_gap_disc_desc *disc)
{
struct ble_hs_adv_fields fields;
int rc;
int i;
uint8_t test_addr[6] = {0};
/* The device has to be advertising connectability. */
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
@@ -287,16 +328,25 @@ ble_cts_cent_should_connect(const struct ble_gap_disc_desc *disc)
}
}
#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 Current Time
* service (0x1805).
*/
for (i = 0; i < fields.num_uuids16; i++) {
for (int i = 0; i < fields.num_uuids16; i++) {
if (ble_uuid_u16(&fields.uuids16[i].u) == BLE_SVC_CTS_UUID16) {
return 1;
}
}
return 0;
#endif
}
#endif
@@ -323,6 +373,16 @@ ble_cts_cent_connect_if_interesting(void *disc)
}
#endif
#if CONFIG_EXAMPLE_EXTENDED_ADV
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
#else
addr = &((struct ble_gap_disc_desc *)disc)->addr;
#endif
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
ESP_LOGI(tag, "Found device: %s, name: %s", addr_str(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();
@@ -345,11 +405,6 @@ ble_cts_cent_connect_if_interesting(void *disc)
/* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
* timeout.
*/
#if CONFIG_EXAMPLE_EXTENDED_ADV
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
#else
addr = &((struct ble_gap_disc_desc *)disc)->addr;
#endif
rc = ble_gap_connect(own_addr_type, addr, 30000, NULL,
ble_cts_cent_gap_event, NULL);
if (rc != 0) {
@@ -413,6 +468,8 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
assert(rc == 0);
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);
@@ -609,6 +666,13 @@ app_main(void)
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(remote_device_name, esp_ble_cts_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_cts_cent_on_reset;
ble_hs_cfg.sync_cb = ble_cts_cent_on_sync;
@@ -0,0 +1,6 @@
CONFIG_EXAMPLE_CI_ID=26
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_ENCRYPTION=n
CONFIG_EXAMPLE_EXTENDED_ADV=n
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=26
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_ENCRYPTION=n
CONFIG_EXAMPLE_EXTENDED_ADV=n
@@ -11,4 +11,16 @@ menu "Example Configuration"
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
also disabled from Nimble stack menuconfig
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,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOSConfig.h"
@@ -27,11 +29,25 @@ static uint8_t ext_adv_pattern_1[] = {
#endif
static const char *tag = "NimBLE_CTS_PRPH";
static const char *device_name = "ble_cts_prph";
static char device_name[32] = "ble_cts_prph";
static int ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t ble_cts_prph_addr_type;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_cts_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
/**
* Utility function to log an array of bytes.
*/
@@ -67,6 +83,11 @@ ext_ble_cts_prph_advertise(void)
struct os_mbuf *data;
uint8_t instance = 0;
int rc;
#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)) {
@@ -96,6 +117,20 @@ ext_ble_cts_prph_advertise(void)
/* in this case only scan response is allowed */
#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
/* get mbuf for scan rsp data */
data = os_msys_get_pkthdr(sizeof(ext_adv_pattern_1), 0);
assert(data);
@@ -103,6 +138,7 @@ ext_ble_cts_prph_advertise(void)
/* fill mbuf with scan rsp 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);
@@ -148,12 +184,14 @@ ble_cts_prph_advertise(void)
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
#if !(CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID)
static const ble_uuid16_t adv_uuids16[] = {
BLE_UUID16_INIT(BLE_SVC_CTS_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) {
@@ -177,6 +215,9 @@ ble_cts_prph_advertise(void)
static int
ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed */
@@ -184,7 +225,16 @@ ble_cts_prph_gap_event(struct ble_gap_event *event, void *arg)
event->connect.status == 0 ? "established" : "failed",
event->connect.status);
if (event->connect.status != 0) {
if (event->connect.status == 0) {
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
if (rc == 0) {
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]);
}
} else {
/* Connection failed; resume advertising */
#if CONFIG_EXAMPLE_EXTENDED_ADV
ext_ble_cts_prph_advertise();
@@ -305,6 +355,13 @@ void 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_cts_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(device_name);
@@ -0,0 +1,5 @@
CONFIG_EXAMPLE_CI_ID=26
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_EXTENDED_ADV=n
@@ -0,0 +1,3 @@
CONFIG_EXAMPLE_CI_ID=26
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_EXTENDED_ADV=n