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
@@ -36,4 +36,12 @@ menu "Example Configuration"
help
Used for internal test ONLY.
Use this option to advertise in a specific random address.
config EXAMPLE_CI_ID
int "example id for CI test"
default 0
config EXAMPLE_CI_PIPELINE_ID
int "The pipeline id for CI test"
default 0
endmenu
+70 -4
View File
@@ -17,6 +17,8 @@
* under the License.
*/
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
/* BLE */
@@ -44,6 +46,9 @@ static const ble_uuid_t * remote_chr_uuid =
#endif
static const char *tag = "NimBLE_BLE_CENT";
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char remote_device_name[32];
#endif
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM) > 0
@@ -53,6 +58,20 @@ static uint16_t bearers;
void ble_store_config_init(void);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_blecent_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)
/**
* Application Callback. Called when the custom subscribable chatacteristic
@@ -462,6 +481,12 @@ blecent_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,
blecent_gap_event, NULL);
if (rc != 0) {
@@ -504,9 +529,27 @@ ext_blecent_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 Alert Notification
* service (0x1811).
*/
* service (0x1811).
*/
while (offset < disc->length_data) {
ad_struct_len = disc->data[offset];
@@ -527,6 +570,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
}
return 0;
#endif
}
#else
static int
@@ -534,7 +578,6 @@ blecent_should_connect(const struct ble_gap_disc_desc *disc)
{
struct ble_hs_adv_fields fields;
int rc;
int i;
uint8_t test_addr[6];
/* The device has to be advertising connectability. */
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
@@ -567,16 +610,25 @@ blecent_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 Alert Notification
* service (0x1811).
*/
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) == BLECENT_SVC_ALERT_UUID) {
return 1;
}
}
return 0;
#endif
}
#endif
@@ -631,6 +683,10 @@ blecent_connect_if_interesting(void *disc)
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
rc = ble_gap_connect(own_addr_type, addr, 30000, NULL,
blecent_gap_event, NULL);
if (rc != 0) {
@@ -727,6 +783,8 @@ blecent_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);
@@ -1079,6 +1137,14 @@ app_main(void)
return;
}
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(remote_device_name, esp_blecent_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,7 @@
CONFIG_EXAMPLE_CI_ID=39
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_EXTENDED_ADV=n
CONFIG_EXAMPLE_ENCRYPTION=n
CONFIG_BT_NIMBLE_EXT_ADV=n
@@ -0,0 +1,5 @@
CONFIG_EXAMPLE_CI_ID=39
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_EXTENDED_ADV=n
CONFIG_EXAMPLE_ENCRYPTION=n
CONFIG_BT_NIMBLE_EXT_ADV=n