Add reboot command and late terminal activation

This commit is contained in:
2026-08-23 16:14:19 +02:00
parent f9d3e9ece5
commit 0dcc0d20d4
8 changed files with 86 additions and 2 deletions
+2
View File
@@ -3,6 +3,7 @@ idf_component_register(
"main.c"
"console_completion.c"
"network_console.c"
"system_console.c"
"status_led.c"
"rs232_hw_test.c"
"rs232_port_owner.c"
@@ -25,6 +26,7 @@ idf_component_register(
esp_event
esp_netif
esp_psram
esp_system
esp_timer
esp_tinyusb
esp_wifi
+19
View File
@@ -5,6 +5,7 @@
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include "esp_console.h"
#include "linenoise/linenoise.h"
@@ -109,6 +110,22 @@ static const char *const s_completion_candidates[] = {
"wifi traceroute",
};
static ssize_t console_read_with_late_terminal_upgrade(int file_descriptor,
void *buffer,
size_t size)
{
ssize_t received = read(file_descriptor, buffer, size);
if (received > 0 && linenoiseIsDumbMode()) {
/*
* The current line remains in the safe dumb reader. The next call to
* linenoise() starts enhanced mode only after a terminal has proven it
* can send data, so its cursor-position query cannot block unattended.
*/
linenoiseSetDumbMode(0);
}
return received;
}
static void console_completion_callback(const char *buffer, linenoiseCompletions *completions)
{
/* Preserve ESP-IDF completion for registered root command names. */
@@ -134,5 +151,7 @@ static void console_completion_callback(const char *buffer, linenoiseCompletions
void console_completion_install(void)
{
/* Preserve ESP-IDF's safe boot-time mode until actual UART input arrives. */
linenoiseSetReadFunction(&console_read_with_late_terminal_upgrade);
linenoiseSetCompletionCallback(&console_completion_callback);
}
+1 -1
View File
@@ -6,7 +6,7 @@
extern "C" {
#endif
/* Install project-specific nested command completion for the console. */
/* Install late-terminal upgrade handling and project-specific completion. */
void console_completion_install(void);
#ifdef __cplusplus
+3 -1
View File
@@ -13,6 +13,7 @@
#include "session_broker.h"
#include "session_console.h"
#include "status_led.h"
#include "system_console.h"
#include "usb_cdc_transport.h"
#include "usb_console.h"
#include "wifi_config.h"
@@ -132,7 +133,8 @@ void app_main(void)
ESP_ERROR_CHECK(usb_console_register_commands());
ESP_ERROR_CHECK(wifi_console_register_commands());
ESP_ERROR_CHECK(network_console_register_root_commands());
/* ESP-IDF handles root completion; this wrapper adds nested subcommands. */
ESP_ERROR_CHECK(system_console_register_commands());
/* Upgrade late UART terminals safely and add nested completion. */
console_completion_install();
ESP_ERROR_CHECK(esp_console_start_repl(repl));
+39
View File
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: GPL-3.0-only */
/* Root-level system lifecycle commands for the physical administration console. */
#include "system_console.h"
#include <stdio.h>
#include "esp_console.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static int command_reboot(int argc, char **argv)
{
(void)argv;
if (argc != 1) {
printf("Usage: reboot\n");
return 1;
}
printf("Rebooting now; unsaved RAM-only configuration changes will be lost.\n");
fflush(stdout);
/* Give the UART driver time to transmit the acknowledgement before reset. */
vTaskDelay(pdMS_TO_TICKS(100U));
esp_restart();
return 0;
}
esp_err_t system_console_register_commands(void)
{
const esp_console_cmd_t command = {
.command = "reboot",
.help = "Restart the ESP32; unsaved RAM-only configuration is lost",
.hint = NULL,
.func = &command_reboot,
.argtable = NULL,
};
return esp_console_cmd_register(&command);
}
+8
View File
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-3.0-only */
#pragma once
#include "esp_err.h"
/* Register root-level system lifecycle commands such as `reboot`. */
esp_err_t system_console_register_commands(void);