mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
feat(esp_local_ctrl): adds basic test app
This commit is contained in:
@@ -4,13 +4,13 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <protocomm_ble.h>
|
||||
#include <esp_local_ctrl.h>
|
||||
#include <protocomm_ble.h>
|
||||
|
||||
#include "esp_local_ctrl_priv.h"
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
|
||||
static const char *TAG = "esp_local_ctrl_transport_ble";
|
||||
|
||||
static esp_err_t start_ble_transport(protocomm_t *pc, const esp_local_ctrl_transport_config_t *config)
|
||||
static esp_err_t
|
||||
start_ble_transport(protocomm_t *pc,
|
||||
const esp_local_ctrl_transport_config_t *config)
|
||||
{
|
||||
if (!config || !config->ble) {
|
||||
ESP_LOGE(TAG, "NULL configuration provided");
|
||||
@@ -32,8 +34,11 @@ static void stop_ble_transport(protocomm_t *pc)
|
||||
protocomm_ble_stop(pc);
|
||||
}
|
||||
|
||||
static esp_err_t copy_ble_config(esp_local_ctrl_transport_config_t *dest_config, const esp_local_ctrl_transport_config_t *src_config)
|
||||
static esp_err_t
|
||||
copy_ble_config(esp_local_ctrl_transport_config_t *dest_config,
|
||||
const esp_local_ctrl_transport_config_t *src_config)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
if (!dest_config || !src_config || !src_config->ble) {
|
||||
ESP_LOGE(TAG, "NULL arguments provided");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
@@ -46,13 +51,11 @@ static esp_err_t copy_ble_config(esp_local_ctrl_transport_config_t *dest_config,
|
||||
}
|
||||
|
||||
/* Copy BLE device name */
|
||||
memcpy(dest_config->ble->device_name,
|
||||
src_config->ble->device_name,
|
||||
memcpy(dest_config->ble->device_name, src_config->ble->device_name,
|
||||
sizeof(src_config->ble->device_name));
|
||||
|
||||
/* Copy Service UUID */
|
||||
memcpy(dest_config->ble->service_uuid,
|
||||
src_config->ble->service_uuid,
|
||||
memcpy(dest_config->ble->service_uuid, src_config->ble->service_uuid,
|
||||
sizeof(src_config->ble->service_uuid));
|
||||
|
||||
dest_config->ble->nu_lookup_count = 0;
|
||||
@@ -65,7 +68,6 @@ static esp_err_t copy_ble_config(esp_local_ctrl_transport_config_t *dest_config,
|
||||
free(dest_config->ble);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
esp_err_t ret = ESP_OK;
|
||||
for (uint16_t i = 0; i < src_config->ble->nu_lookup_count; i++) {
|
||||
dest_config->ble->nu_lookup[i].uuid = src_config->ble->nu_lookup[i].uuid;
|
||||
if (!src_config->ble->nu_lookup[i].name) {
|
||||
@@ -73,7 +75,8 @@ static esp_err_t copy_ble_config(esp_local_ctrl_transport_config_t *dest_config,
|
||||
ret = ESP_ERR_INVALID_ARG;
|
||||
goto err_free_nu_lookup;
|
||||
}
|
||||
dest_config->ble->nu_lookup[i].name = strdup(src_config->ble->nu_lookup[i].name);
|
||||
dest_config->ble->nu_lookup[i].name =
|
||||
strdup(src_config->ble->nu_lookup[i].name);
|
||||
if (!dest_config->ble->nu_lookup[i].name) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for endpoint name");
|
||||
ret = ESP_ERR_NO_MEM;
|
||||
@@ -86,7 +89,7 @@ static esp_err_t copy_ble_config(esp_local_ctrl_transport_config_t *dest_config,
|
||||
|
||||
err_free_nu_lookup:
|
||||
for (uint16_t i = 0; i < dest_config->ble->nu_lookup_count; i++) {
|
||||
free((void *) dest_config->ble->nu_lookup[i].name);
|
||||
free((void *)dest_config->ble->nu_lookup[i].name);
|
||||
}
|
||||
free(dest_config->ble->nu_lookup);
|
||||
free(dest_config->ble);
|
||||
@@ -94,16 +97,17 @@ err_free_nu_lookup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t declare_endpoint(esp_local_ctrl_transport_config_t *config, const char *ep_name, uint16_t ep_uuid)
|
||||
static esp_err_t declare_endpoint(esp_local_ctrl_transport_config_t *config,
|
||||
const char *ep_name, uint16_t ep_uuid)
|
||||
{
|
||||
if (!config || !config->ble) {
|
||||
ESP_LOGE(TAG, "NULL configuration provided");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
protocomm_ble_name_uuid_t *nu_lookup = realloc(config->ble->nu_lookup,
|
||||
(config->ble->nu_lookup_count + 1)
|
||||
* sizeof(protocomm_ble_name_uuid_t));
|
||||
protocomm_ble_name_uuid_t *nu_lookup =
|
||||
realloc(config->ble->nu_lookup, (config->ble->nu_lookup_count + 1) *
|
||||
sizeof(protocomm_ble_name_uuid_t));
|
||||
if (!nu_lookup) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for new endpoint entry");
|
||||
return ESP_ERR_NO_MEM;
|
||||
@@ -123,7 +127,7 @@ static void free_config(esp_local_ctrl_transport_config_t *config)
|
||||
{
|
||||
if (config && config->ble) {
|
||||
for (unsigned int i = 0; i < config->ble->nu_lookup_count; i++) {
|
||||
free((void*) config->ble->nu_lookup[i].name);
|
||||
free((void *)config->ble->nu_lookup[i].name);
|
||||
}
|
||||
free(config->ble->nu_lookup);
|
||||
free(config->ble);
|
||||
@@ -135,10 +139,10 @@ const esp_local_ctrl_transport_t *esp_local_ctrl_get_transport_ble(void)
|
||||
{
|
||||
static const esp_local_ctrl_transport_t tp = {
|
||||
.start_service = start_ble_transport,
|
||||
.stop_service = stop_ble_transport,
|
||||
.copy_config = copy_ble_config,
|
||||
.declare_ep = declare_endpoint,
|
||||
.free_config = free_config
|
||||
.stop_service = stop_ble_transport,
|
||||
.copy_config = copy_ble_config,
|
||||
.declare_ep = declare_endpoint,
|
||||
.free_config = free_config
|
||||
};
|
||||
return &tp;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
|
||||
|
||||
components/esp_local_ctrl/test_apps:
|
||||
disable:
|
||||
- if: IDF_TARGET not in ["esp32c3"]
|
||||
reason: Testing on one target is enough
|
||||
depends_components:
|
||||
- esp_local_ctrl
|
||||
@@ -0,0 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
set(COMPONENTS main)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(esp_local_ctrl_test)
|
||||
@@ -0,0 +1,2 @@
|
||||
| Supported Targets | ESP32-C3 |
|
||||
| ----------------- | -------- |
|
||||
@@ -0,0 +1,8 @@
|
||||
idf_component_register(
|
||||
SRCS "app_main.c" "test_esp_local_ctrl.c"
|
||||
# Access the private transport struct so we can define a null transport
|
||||
# without requiring BLE or HTTPD hardware in unit tests.
|
||||
PRIV_INCLUDE_DIRS "$ENV{IDF_PATH}/components/esp_local_ctrl/src"
|
||||
PRIV_REQUIRES unity esp_local_ctrl
|
||||
WHOLE_ARCHIVE
|
||||
)
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "unity.h"
|
||||
#include "unity_test_runner.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (-512)
|
||||
|
||||
static size_t s_before_free_8bit;
|
||||
static size_t s_before_free_32bit;
|
||||
|
||||
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||
{
|
||||
ssize_t delta = after_free - before_free;
|
||||
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n",
|
||||
type, before_free, after_free, delta);
|
||||
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
s_before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
s_before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
/* Catch heap corruption that would be caused by a double-free */
|
||||
TEST_ASSERT_MESSAGE(heap_caps_check_integrity(MALLOC_CAP_INVALID, true),
|
||||
"heap integrity check failed (possible double-free or corruption)");
|
||||
|
||||
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
check_leak(s_before_free_8bit, after_free_8bit, "8BIT");
|
||||
check_leak(s_before_free_32bit, after_free_32bit, "32BIT");
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("Running esp_local_ctrl component tests\n");
|
||||
unity_run_menu();
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "esp_local_ctrl.h"
|
||||
#include "esp_local_ctrl_priv.h" /* full definition of esp_local_ctrl_transport */
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
/*
|
||||
* Minimal transport with all function pointers NULL.
|
||||
*
|
||||
* esp_local_ctrl_start() guards every transport callback with an `if` before
|
||||
* calling it, so a zero-initialised transport lets the service start with an
|
||||
* in-memory protocomm instance only — no BLE or HTTPD hardware required.
|
||||
*/
|
||||
static const esp_local_ctrl_transport_t s_null_transport = { 0 };
|
||||
|
||||
static esp_err_t dummy_get_prop_values(size_t props_count,
|
||||
const esp_local_ctrl_prop_t props[],
|
||||
esp_local_ctrl_prop_val_t prop_values[],
|
||||
void *usr_ctx)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t dummy_set_prop_values(size_t props_count,
|
||||
const esp_local_ctrl_prop_t props[],
|
||||
const esp_local_ctrl_prop_val_t prop_values[],
|
||||
void *usr_ctx)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_local_ctrl_config_t make_config(size_t max_properties)
|
||||
{
|
||||
esp_local_ctrl_config_t cfg = {
|
||||
.transport = &s_null_transport,
|
||||
.transport_config = { .httpd = NULL },
|
||||
.proto_sec = { .version = PROTOCOM_SEC0 },
|
||||
.handlers = {
|
||||
.get_prop_values = dummy_get_prop_values,
|
||||
.set_prop_values = dummy_set_prop_values,
|
||||
},
|
||||
.max_properties = max_properties,
|
||||
};
|
||||
return cfg;
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: remove a non-last property, then stop.
|
||||
*
|
||||
* 1. Add prop_a (slot 0) and prop_b (slot 1). props_count = 2.
|
||||
* 2. Remove prop_a:
|
||||
* - prop_a is freed.
|
||||
* - Compaction shifts prop_b into slot 0.
|
||||
* 3. esp_local_ctrl_stop() must free prop_b exactly once (slot 0).
|
||||
*/
|
||||
TEST_CASE("remove_property then stop does not double-free", "[esp_local_ctrl]")
|
||||
{
|
||||
esp_local_ctrl_config_t cfg = make_config(5);
|
||||
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
|
||||
|
||||
const esp_local_ctrl_prop_t prop_a = { .name = "prop_a" };
|
||||
const esp_local_ctrl_prop_t prop_b = { .name = "prop_b" };
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&prop_a));
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&prop_b));
|
||||
|
||||
/* Removing the first property triggers compaction. */
|
||||
TEST_ESP_OK(esp_local_ctrl_remove_property("prop_a"));
|
||||
|
||||
/* prop_b must still be reachable after compaction */
|
||||
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("prop_b"));
|
||||
|
||||
TEST_ESP_OK(esp_local_ctrl_stop());
|
||||
}
|
||||
|
||||
/* Test: remove the only property then stop — no compaction, basic sanity. */
|
||||
TEST_CASE("remove sole property then stop is clean", "[esp_local_ctrl]")
|
||||
{
|
||||
esp_local_ctrl_config_t cfg = make_config(3);
|
||||
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
|
||||
|
||||
const esp_local_ctrl_prop_t prop = { .name = "only_prop" };
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&prop));
|
||||
TEST_ESP_OK(esp_local_ctrl_remove_property("only_prop"));
|
||||
|
||||
/* After removing the sole property, stop must find nothing to free. */
|
||||
TEST_ESP_OK(esp_local_ctrl_stop());
|
||||
}
|
||||
|
||||
/* Test: multiple compaction passes (remove head repeatedly) then stop. */
|
||||
TEST_CASE("repeated head removal then stop is clean", "[esp_local_ctrl]")
|
||||
{
|
||||
esp_local_ctrl_config_t cfg = make_config(5);
|
||||
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
|
||||
|
||||
const esp_local_ctrl_prop_t p0 = { .name = "p0" };
|
||||
const esp_local_ctrl_prop_t p1 = { .name = "p1" };
|
||||
const esp_local_ctrl_prop_t p2 = { .name = "p2" };
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&p0));
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&p1));
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&p2));
|
||||
|
||||
/* First removal: p1→slot0, p2→slot1, slot2 stale (fixed to NULL). */
|
||||
TEST_ESP_OK(esp_local_ctrl_remove_property("p0"));
|
||||
/* Second removal: p2→slot0, slot1 stale (fixed to NULL). */
|
||||
TEST_ESP_OK(esp_local_ctrl_remove_property("p1"));
|
||||
|
||||
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("p2"));
|
||||
TEST_ESP_OK(esp_local_ctrl_stop());
|
||||
}
|
||||
|
||||
/* Test: add/remove/re-add cycle — the slot vacated by a remove must accept a
|
||||
* new property without corrupting internal bookkeeping. */
|
||||
TEST_CASE("add remove add cycle is clean", "[esp_local_ctrl]")
|
||||
{
|
||||
esp_local_ctrl_config_t cfg = make_config(3);
|
||||
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
|
||||
|
||||
const esp_local_ctrl_prop_t pa = { .name = "a" };
|
||||
const esp_local_ctrl_prop_t pb = { .name = "b" };
|
||||
const esp_local_ctrl_prop_t pc = { .name = "c" };
|
||||
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&pa));
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&pb));
|
||||
TEST_ESP_OK(esp_local_ctrl_remove_property("a"));
|
||||
|
||||
/* Reuse the freed slot capacity for a new property. */
|
||||
TEST_ESP_OK(esp_local_ctrl_add_property(&pc));
|
||||
|
||||
TEST_ASSERT_NULL(esp_local_ctrl_get_property("a"));
|
||||
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("b"));
|
||||
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("c"));
|
||||
|
||||
TEST_ESP_OK(esp_local_ctrl_stop());
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
# Enable Security Version 0 (no encryption) so esp_local_ctrl_start() succeeds
|
||||
# with a minimal null transport (no BLE/Wi-Fi hardware required).
|
||||
CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0=y
|
||||
CONFIG_ESP_TASK_WDT_INIT=n
|
||||
Reference in New Issue
Block a user