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
@@ -10,4 +10,17 @@ 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
@@ -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"
@@ -28,12 +30,26 @@ static uint8_t ext_adv_pattern_1[] = {
static const char *tag = "NimBLE_HTP_PRPH";
static TimerHandle_t ble_htp_prph_tx_timer;
static uint16_t conn_handle;
static const char *device_name = "ble_htp_prph";
static char device_name[32] = "ble_htp_prph";
static int ble_htp_prph_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t ble_htp_prph_addr_type;
#if CONFIG_EXAMPLE_CI_ID && CONFIG_EXAMPLE_CI_PIPELINE_ID
static char *esp_ble_htp_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.
*/
@@ -69,6 +85,11 @@ ext_ble_htp_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)) {
@@ -98,6 +119,20 @@ ext_ble_htp_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);
@@ -105,6 +140,7 @@ ext_ble_htp_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);
@@ -150,12 +186,14 @@ ble_htp_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_HTP_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) {
@@ -226,6 +264,9 @@ ble_htp_prph_tx(TimerHandle_t ev)
static int
ble_htp_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 */
@@ -233,7 +274,16 @@ ble_htp_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_htp_prph_advertise();
@@ -377,6 +427,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_htp_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=27
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=27
CONFIG_EXAMPLE_CI_PIPELINE_ID=${CI_PIPELINE_ID}
CONFIG_EXAMPLE_EXTENDED_ADV=n