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
Go to the documentation of this file.
1
11
12#include "unit_test.h"
14#include "esp_console.h"
15
16#if CONFIG_ENABLE_UNIT_TEST
17
18#define UT_CMD_MIN_ARGS 4
19
21
36static meshx_err_t ut_command_handler(int argc, char **argv) {
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}
70
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}
90
91 /* @brief Registers the unit test (ut) command with the ESP console.
92 *
93 * This function creates a new console command "ut" which is used for running unit tests.
94 * The command is registered with the ESP console using the esp_console_cmd_register function.
95 *
96 * @return
97 * - MESHX_SUCCESS: Success
98 * - Other error codes: Failure
99 */
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}
135
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}
156
157#endif /* CONFIG_ENABLE_UNIT_TEST */
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_NOT_FOUND
Definition meshx_err.h:46
Logging interface for MeshX with color-coded output.
#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_MAX
Definition module_id.h:34
@ MODULE_ID_COMMON
Definition module_id.h:32
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
meshx_err_t register_ut_command()
Registers the unit test (ut) command with the ESP console.
Definition unit_test.c:81
#define UT_CMD_MIN_ARGS
Definition unit_test.c:18
meshx_err_t init_unit_test_console()
Initialize the production console.
Definition unit_test.c:100
meshx_err_t register_unit_test(module_id_t module_id, module_callback_t callback)
Register a unit test for a specific module.
Definition unit_test.c:149
static unit_test_callback_t callback_list[MODULE_ID_MAX]
Definition unit_test.c:20
Header file for the production console unit test functionality.
meshx_err_t(* module_callback_t)(int cmd_id, int argc, char **argv)
Callback function for unit test modules.
Definition unit_test.h:44
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:27
struct callback_node unit_test_callback_t
Structure to hold the unit test callback function.