ESP32/components/app_console/cmd_system.c

168 lines
5.7 KiB
C

/*
* cmd_system.c
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "esp_console.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "argtable3/argtable3.h"
#include "app_console.h"
// Define default version if not passed by CMake
#ifndef APP_VERSION
#define APP_VERSION "2.0.0-SHELL"
#endif
// --- Helper: Convert Model ID to String ---
static const char* get_chip_model_string(esp_chip_model_t model) {
switch (model) {
case CHIP_ESP32: return "ESP32";
case CHIP_ESP32S2: return "ESP32-S2";
case CHIP_ESP32S3: return "ESP32-S3";
case CHIP_ESP32C3: return "ESP32-C3";
case CHIP_ESP32C2: return "ESP32-C2";
case CHIP_ESP32C6: return "ESP32-C6";
case CHIP_ESP32H2: return "ESP32-H2";
// Explicitly handle ID 23 for C5 if macro is missing
case 23: return "ESP32-C5";
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)) && defined(CHIP_ESP32P4)
case CHIP_ESP32P4: return "ESP32-P4";
#endif
#ifdef CHIP_ESP32C5
case CHIP_ESP32C5: return "ESP32-C5";
#endif
default: return "Unknown";
}
}
// --- Command Handlers ---
static int do_system_reboot(int argc, char **argv) {
printf("Rebooting...\n");
esp_restart();
return 0;
}
static int do_version(int argc, char **argv) {
printf("APP_VERSION: %s\n", APP_VERSION);
printf("IDF_VERSION: %s\n", esp_get_idf_version());
return 0;
}
static int do_system_info(int argc, char **argv) {
esp_chip_info_t info;
esp_chip_info(&info);
const char *model_str = get_chip_model_string(info.model);
printf("IDF Version: %s\n", esp_get_idf_version());
if (strcmp(model_str, "Unknown") == 0) {
printf("Chip Info: Model=%s (ID=%d), Cores=%d, Revision=%d\n",
model_str, info.model, info.cores, info.revision);
} else {
printf("Chip Info: Model=%s, Cores=%d, Revision=%d\n",
model_str, info.cores, info.revision);
}
uint32_t flash_size = 0;
if (esp_flash_get_size(NULL, &flash_size) == ESP_OK) {
printf("Flash Size: %" PRIu32 " MB\n", flash_size / (1024 * 1024));
} else {
printf("Flash Size: Unknown\n");
}
printf("Features: %s%s%s%s%s\n",
(info.features & CHIP_FEATURE_WIFI_BGN) ? "802.11bgn " : "",
(info.features & CHIP_FEATURE_BLE) ? "BLE " : "",
(info.features & CHIP_FEATURE_BT) ? "BT " : "",
(info.features & CHIP_FEATURE_IEEE802154) ? "802.15.4 " : "",
(info.features & CHIP_FEATURE_EMB_FLASH) ? "Embedded-Flash " : "");
return 0;
}
static int do_system_heap(int argc, char **argv) {
printf("Heap Info:\n");
printf(" Free: %" PRIu32 " bytes\n", esp_get_free_heap_size());
printf(" Min Free: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
return 0;
}
static int cmd_system(int argc, char **argv) {
if (argc < 2) {
printf("Usage: system <reboot|info|heap>\n");
return 0;
}
if (strcmp(argv[1], "reboot") == 0) return do_system_reboot(argc - 1, &argv[1]);
if (strcmp(argv[1], "info") == 0) return do_system_info(argc - 1, &argv[1]);
if (strcmp(argv[1], "heap") == 0) return do_system_heap(argc - 1, &argv[1]);
printf("Unknown subcommand '%s'.\n", argv[1]);
return 1;
}
// --- Registration ---
void register_system_cmd(void) {
const esp_console_cmd_t cmd = {
.command = "system",
.help = "System Tools: reboot, info, heap",
.hint = "<subcommand>",
.func = &cmd_system,
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
void register_reset_cmd(void) {
const esp_console_cmd_t cmd = {
.command = "reset",
.help = "Software reset of the device",
.func = &do_system_reboot,
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
void register_version_cmd(void) {
const esp_console_cmd_t cmd = {
.command = "version",
.help = "Get firmware version",
.func = &do_version,
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}