/* SPDX-License-Identifier: GPL-3.0-only */ /* Root-level system lifecycle commands for the physical administration console. */ #include "system_console.h" #include #include #include "admin_ssh_console.h" #include "esp_console.h" #include "esp_heap_caps.h" #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" static void print_heap_region(const char *name, uint32_t capabilities) { printf("%s: free=%u minimum-free=%u largest-block=%u bytes\n", name, (unsigned int)heap_caps_get_free_size(capabilities), (unsigned int)heap_caps_get_minimum_free_size(capabilities), (unsigned int)heap_caps_get_largest_free_block(capabilities)); } static int command_memory(int argc, char **argv) { (void)argv; if (argc != 1) { printf("Usage: memory\n"); return 1; } print_heap_region("Internal 8-bit heap", MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); print_heap_region("Internal DMA heap", MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA | MALLOC_CAP_8BIT); print_heap_region("External PSRAM", MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); printf("Minimum-free is a conservative sum of each matching heap region's lifetime minimum.\n"); return 0; } static int command_reboot(int argc, char **argv) { (void)argv; if (argc != 1) { printf("Usage: reboot\n"); return 1; } if (admin_ssh_console_dispatch_is_remote()) { esp_err_t error = admin_ssh_console_dispatch_defer(ADMIN_SSH_DEFER_REBOOT, 0U); if (error != ESP_OK) { printf("Could not schedule reboot: %s\n", esp_err_to_name(error)); return 1; } printf("Reboot scheduled after console output drains; unsaved changes will be lost.\n"); return 0; } 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 reboot_command = { .command = "reboot", .help = "Restart the ESP32; unsaved RAM-only configuration is lost", .hint = NULL, .func = &command_reboot, .argtable = NULL, }; esp_err_t error = esp_console_cmd_register(&reboot_command); if (error != ESP_OK) { return error; } const esp_console_cmd_t memory_command = { .command = "memory", .help = "Show internal and PSRAM heap availability/low-water marks", .hint = NULL, .func = &command_memory, .argtable = NULL, }; return esp_console_cmd_register(&memory_command); }