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
@@ -16,4 +16,16 @@ menu "Example Configuration"
Use this option to enable extended advertising in the example.
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
@@ -6,6 +6,8 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include <stdio.h>
#include <string.h>
/* BLE */
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
@@ -16,10 +18,25 @@
#include "coc_blecent.h"
static const char *tag = "NimBLE_BLE_CENT_L2CAP_COC";
static char remote_device_name[32];
static int blecent_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_l2cap_coc_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_L2CAP_COC_MAX_NUM) >= 1
#define COC_BUF_COUNT (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
@@ -240,6 +257,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) {
@@ -272,6 +295,24 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
return 0;
}
}
#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 L2CAP COC UUID (0x1812).
*/
do {
@@ -301,6 +342,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
} while (offset < disc->length_data);
return 0;
#endif
}
#else
static int
@@ -308,7 +350,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 &&
@@ -332,14 +373,23 @@ 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 L2CAP COC UUID (0x1812).
*/
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) == 0x1812) {
return 1;
}
}
return 0;
#endif
}
#endif
@@ -366,6 +416,16 @@ blecent_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();
@@ -388,12 +448,6 @@ blecent_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,
blecent_gap_event, NULL);
if (rc != 0) {
@@ -454,6 +508,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));
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) >= 1
conn_handle_coc = event->connect.conn_handle;
@@ -599,6 +655,15 @@ app_main(void)
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
#endif
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(remote_device_name, esp_ble_l2cap_coc_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
#if CONFIG_BT_NIMBLE_GAP_SERVICE
/* Set the default device name. */
rc = ble_svc_gap_device_name_set("blecent-l2coc");
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=41
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=41
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
@@ -59,4 +59,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
@@ -5,6 +5,9 @@
*/
#include "nvs_flash.h"
#include "esp_log.h"
#include <stdio.h>
#include <string.h>
/* BLE */
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
@@ -14,7 +17,7 @@
#include "services/gap/ble_svc_gap.h"
#include "coc_bleprph.h"
#if CONFIG_EXAMPLE_EXTENDED_ADV
#if CONFIG_EXAMPLE_EXTENDED_ADV && !(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,
@@ -24,11 +27,26 @@ static uint8_t ext_adv_pattern_1[] = {
#endif
static const char *tag = "NimBLE_BLE_PRPH_L2CAP_COC";
static char device_name[32] = "bleprph-l2coc";
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t own_addr_type;
void ble_store_config_init(void);
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_l2cap_coc_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_L2CAP_COC_MAX_NUM) >= 1
#define COC_BUF_COUNT (20 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
@@ -81,6 +99,16 @@ ext_bleprph_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)) {
return;
}
/* use defaults for non-set params */
memset (&params, 0, sizeof(params));
@@ -106,6 +134,20 @@ ext_bleprph_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);
@@ -113,6 +155,7 @@ ext_bleprph_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);
@@ -158,20 +201,18 @@ bleprph_advertise(void)
fields.tx_pwr_lvl_is_present = 1;
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
#if !(CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID)
static const ble_uuid16_t adv_uuids16[] = {
BLE_UUID16_INIT(L2CAP_COC_UUID)
};
fields.uuids16 = adv_uuids16;
fields.num_uuids16 = 1;
fields.uuids16_is_complete = 1;
#endif
rc = ble_gap_adv_set_fields(&fields);
if (rc != 0) {
@@ -326,6 +367,11 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
bleprph_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");
@@ -477,9 +523,17 @@ app_main(void)
bleprph_l2cap_coc_mem_init();
#endif
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
strncpy(device_name, esp_ble_l2cap_coc_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("bleprph-l2coc");
rc = ble_svc_gap_device_name_set(device_name);
assert(rc == 0);
#endif
@@ -0,0 +1,4 @@
CONFIG_EXAMPLE_CI_ID=41
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=41
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}