MeshX 0.3
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
unit_test.c File Reference

Unit test module for the production console. More...

#include "unit_test.h"
#include "interface/logging/meshx_log.h"
#include "esp_console.h"
Include dependency graph for unit_test.c:

Go to the source code of this file.

Macros

#define UT_CMD_MIN_ARGS   4
 

Functions

static meshx_err_t ut_command_handler (int argc, char **argv)
 Handles unit test commands by invoking the appropriate callback based on the module ID.
 
meshx_err_t register_ut_command ()
 Registers the unit test (ut) command with the ESP console.
 
meshx_err_t init_unit_test_console ()
 Initialize the production console.
 
meshx_err_t register_unit_test (module_id_t module_id, module_callback_t callback)
 Register a unit test for a specific module.
 

Variables

static unit_test_callback_t callback_list [MODULE_ID_MAX]
 

Detailed Description

Unit test module for the production console.

Copyright © 2024 - 2025 MeshX

This file contains the implementation of the unit test module for the production console. It provides functions to register unit test commands and initialize the console for production use.

Definition in file unit_test.c.

Macro Definition Documentation

◆ UT_CMD_MIN_ARGS

#define UT_CMD_MIN_ARGS   4

Definition at line 18 of file unit_test.c.

Function Documentation

◆ init_unit_test_console()

meshx_err_t init_unit_test_console ( void )

Initialize the production console.

This function initializes the production console for the application.

Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Initialization failed

Definition at line 100 of file unit_test.c.

100 {
101 // Initialize the console
103 esp_console_repl_t *repl = NULL;
104 esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
105
106 repl_config.prompt = "X>";
107
108 // install console REPL environment
109#if CONFIG_ESP_CONSOLE_UART
110 esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
111 ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
112#endif
113#if CONFIG_ESP_CONSOLE_USB_CDC
114 esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
115 ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &repl));
116#endif
117#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
118 esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
119 ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl));
120#endif
121
122 // Register the unit test command
123 err = register_ut_command();
124 if (err != MESHX_SUCCESS) {
125 return err;
126 }
127
128 err = esp_console_start_repl(repl);
129 if (err != MESHX_SUCCESS) {
130 return err;
131 }
132
133 return MESHX_SUCCESS;
134}
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
meshx_err_t register_ut_command()
Registers the unit test (ut) command with the ESP console.
Definition unit_test.c:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_unit_test()

meshx_err_t register_unit_test ( module_id_t module_id,
module_callback_t callback )

Register a unit test for a specific module.

This function registers a unit test callback for the given module ID.

Parameters
[in]module_idThe ID of the module for which the unit test is being registered.
[in]callbackThe callback function to be called for the unit test.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid arguments
  • MESHX_FAIL: Failed to register the unit test

Definition at line 149 of file unit_test.c.

149 {
150 if(module_id >= MODULE_ID_MAX)
151 return MESHX_INVALID_ARG;
152
153 callback_list[module_id].callback = callback;
154 return MESHX_SUCCESS;
155}
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MODULE_ID_MAX
Definition module_id.h:34
static unit_test_callback_t callback_list[MODULE_ID_MAX]
Definition unit_test.c:20
Here is the caller graph for this function:

◆ register_ut_command()

meshx_err_t register_ut_command ( )

Registers the unit test (ut) command with the ESP console.

This function creates a new console command "ut" which is used for running unit tests. The command is registered with the ESP console using the esp_console_cmd_register function.

Returns
  • MESHX_SUCCESS: Success
  • Other error codes: Failure

Definition at line 81 of file unit_test.c.

81 {
82 const esp_console_cmd_t cmd = {
83 .command = "ut",
84 .help = "Run unit tests",
85 .hint = NULL,
86 .func = (esp_console_cmd_func_t)&ut_command_handler,
87 };
88 return esp_console_cmd_register(&cmd);
89}
static meshx_err_t ut_command_handler(int argc, char **argv)
Handles unit test commands by invoking the appropriate callback based on the module ID.
Definition unit_test.c:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ut_command_handler()

static meshx_err_t ut_command_handler ( int argc,
char ** argv )
static

Handles unit test commands by invoking the appropriate callback based on the module ID.

This function processes unit test commands by parsing the provided arguments and invoking the corresponding callback function registered for the specified module ID.

Parameters
argcThe number of arguments passed to the command.
argvAn array of strings representing the arguments.
Returns
  • MESHX_SUCCESS: If the unit test callback was successfully invoked.
  • MESHX_INVALID_ARG: If the number of arguments is insufficient.
  • MESHX_NOT_FOUND: If no unit test is registered for the specified module ID.

Definition at line 36 of file unit_test.c.

36 {
37 if (argc < UT_CMD_MIN_ARGS) {
38 MESHX_LOGE(MODULE_ID_COMMON, "Insufficient arguments");
39 return MESHX_INVALID_ARG;
40 }
41
42 int cmd_id = UT_GET_ARG(2, uint16_t, argv);
43 int parsed_argc = UT_GET_ARG(3, uint16_t, argv);
44 module_id_t module_id = UT_GET_ARG(1, uint16_t, argv);
45 MESHX_LOGD(MODULE_ID_COMMON, , "Unit Test: Params -> argc: %d, Module: %d, cmd_id: %d", parsed_argc, cmd_id , module_id);
46 if (parsed_argc > (argc - UT_CMD_MIN_ARGS))
47 {
48 MESHX_LOGE(MODULE_ID_COMMON, "Insufficient module arguments");
49 return MESHX_INVALID_ARG;
50 }
51
52 for (size_t i = 0; i < parsed_argc; i++)
53 {
54 MESHX_LOGD("argv[%d]: %s", i, argv[i + UT_CMD_MIN_ARGS]);
55 }
56
57 if (module_id >= MODULE_ID_MAX) {
58 MESHX_LOGE(MODULE_ID_COMMON, "Module ID %d unknown", module_id);
59 return MESHX_INVALID_ARG;
60 }
61
62 if(NULL != callback_list[module_id].callback)
63 {
64 return callback_list[module_id].callback(cmd_id, parsed_argc, (argv + UT_CMD_MIN_ARGS));
65 }
66
67 MESHX_LOGE(MODULE_ID_COMMON, "No unit test registered for module ID %d", module_id);
68 return MESHX_NOT_FOUND;
69}
@ MESHX_NOT_FOUND
Definition meshx_err.h:46
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
module_id_t
Enumeration of module IDs.
Definition module_id.h:23
@ MODULE_ID_COMMON
Definition module_id.h:32
#define UT_CMD_MIN_ARGS
Definition unit_test.c:18
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:27
Here is the caller graph for this function:

Variable Documentation

◆ callback_list

unit_test_callback_t callback_list[MODULE_ID_MAX]
static

Definition at line 20 of file unit_test.c.