ESP32/main/main.c

136 lines
3.8 KiB
C

#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_event.h"
// Components
#include "status_led.h"
#include "board_config.h"
#include "wifi_controller.h"
#include "wifi_cfg.h"
#include "app_console.h"
#include "iperf.h"
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
#include "csi_log.h"
#include "csi_manager.h"
#endif
#define APP_VERSION "2.0.0-SHELL"
static const char *TAG = "MAIN";
// --- Global Prompt Buffer (Mutable) ---
static char s_cli_prompt[32] = "esp32> ";
// --- Prompt Updater ---
// This is called by app_console.c commands whenever a setting is changed.
void app_console_update_prompt(void) {
bool dirty = false;
// Check if any component has unsaved changes (RAM != NVS)
if (wifi_ctl_param_is_unsaved()) dirty = true;
if (iperf_param_is_unsaved()) dirty = true;
if (dirty) {
snprintf(s_cli_prompt, sizeof(s_cli_prompt), "esp32*> ");
} else {
snprintf(s_cli_prompt, sizeof(s_cli_prompt), "esp32> ");
}
}
// --- System Commands ---
static int cmd_restart(int argc, char **argv) {
ESP_LOGI(TAG, "Restarting...");
esp_restart();
return 0;
}
static int cmd_version(int argc, char **argv) {
printf("APP_VERSION: %s\n", APP_VERSION);
printf("IDF_VERSION: %s\n", esp_get_idf_version());
return 0;
}
static void register_system_common(void) {
const esp_console_cmd_t restart_cmd = {
.command = "reset",
.help = "Software reset of the device",
.func = &cmd_restart
};
ESP_ERROR_CHECK(esp_console_cmd_register(&restart_cmd));
const esp_console_cmd_t version_cmd = {
.command = "version",
.help = "Get firmware version",
.func = &cmd_version
};
ESP_ERROR_CHECK(esp_console_cmd_register(&version_cmd));
}
// --- Main Application ---
void app_main(void) {
// 1. Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// 2. Initialize Netif & Event Loop
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
// 3. Hardware Init
status_led_init(RGB_LED_GPIO, HAS_RGB_LED);
#ifdef CONFIG_ESP_WIFI_CSI_ENABLED
ESP_ERROR_CHECK(csi_log_init());
csi_mgr_init();
#endif
// 4. Initialize WiFi Controller (Loads config from NVS automatically)
wifi_ctl_init();
iperf_param_init();
// 5. Initialize Console
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
// CRITICAL: Point the prompt to our mutable buffer
repl_config.prompt = s_cli_prompt;
repl_config.max_cmdline_length = 1024;
// Install UART driver for Console (Standard IO)
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
// 6. Register Commands
register_system_common();
app_console_register_commands();
// 7. Initial Prompt State Check
// (Sets it to "esp32*>" immediately if NVS is empty/dirty on boot)
app_console_update_prompt();
// 8. Start Shell
printf("\n ==================================================\n");
printf(" | ESP32 iPerf Shell - Ready |\n");
printf(" | Type 'help' for commands |\n");
printf(" ==================================================\n");
// This function runs the REPL loop and does not return
ESP_ERROR_CHECK(esp_console_start_repl(repl));
}