Files
esp32-bmc/main/main.c
Valentin Haudiquet 6e88ce1137
All checks were successful
Build ESP32 BMC Firmware / build (push) Successful in 53s
log: add log query API endpoints and display logs on web interface
2026-03-12 16:20:01 +01:00

128 lines
3.6 KiB
C

#include <stdio.h>
#include "esp_log.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
#include "gpio_controller.h"
#include "uart_handler.h"
#include "ethernet_manager.h"
#include "web_server.h"
#include "ota_handler.h"
#include "log_handler.h"
static const char *TAG = TAG_BMC;
// Ethernet event callback
static void eth_event_callback(eth_event_type_t event) {
switch (event) {
case ETH_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet connected");
break;
case ETH_EVENT_DISCONNECTED:
ESP_LOGW(TAG, "Ethernet disconnected");
break;
case ETH_EVENT_GOT_IP: {
char ip[16];
if (ethernet_get_ip(ip) == ESP_OK) {
ESP_LOGI(TAG, "Got IP: %s", ip);
}
} break;
case ETH_EVENT_LOST_IP:
ESP_LOGW(TAG, "Lost IP address");
break;
}
}
void app_main(void) {
ESP_LOGI(TAG, "========================================");
ESP_LOGI(TAG, "ESP32-S3 BMC Firmware Starting...");
ESP_LOGI(TAG, "========================================");
// Initialize log handler first to capture all logs
ESP_LOGI(TAG, "Initializing log handler...");
esp_err_t ret = log_handler_init();
if (ret != ESP_OK) {
ESP_LOGW(TAG, "Failed to initialize log handler: %s", esp_err_to_name(ret));
// Continue anyway - log capture is optional
}
// Initialize GPIO controller
ESP_LOGI(TAG, "Initializing GPIO controller...");
ret = gpio_controller_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize GPIO controller: %s", esp_err_to_name(ret));
// Continue anyway - some GPIOs might still work
}
// Initialize UART handler
ESP_LOGI(TAG, "Initializing UART handler...");
ret = uart_handler_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize UART handler: %s", esp_err_to_name(ret));
// Continue anyway - serial console is optional
}
// Initialize Ethernet manager
ESP_LOGI(TAG, "Initializing Ethernet manager...");
ethernet_register_event_callback(eth_event_callback);
ret = ethernet_manager_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize Ethernet: %s", esp_err_to_name(ret));
// Cannot continue without network
return;
}
// Wait for Ethernet connection (with timeout)
ESP_LOGI(TAG, "Waiting for Ethernet connection...");
int timeout = 30; // 30 seconds timeout
while (!ethernet_is_connected() && timeout > 0) {
vTaskDelay(pdMS_TO_TICKS(1000));
timeout--;
ESP_LOGD(TAG, "Waiting for Ethernet... (%d seconds remaining)", timeout);
}
if (!ethernet_is_connected()) {
ESP_LOGW(TAG, "Ethernet not connected after timeout, starting server anyway...");
}
// Start web server
ESP_LOGI(TAG, "Starting web server...");
ret = web_server_start();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to start web server: %s", esp_err_to_name(ret));
return;
}
// Initialize OTA handler
ESP_LOGI(TAG, "Initializing OTA handler...");
ret = ota_handler_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize OTA handler: %s", esp_err_to_name(ret));
// Continue anyway - OTA is optional
}
// Print network information
char ip[16], netmask[16], gateway[16], mac[18];
if (ethernet_get_network_info(ip, netmask, gateway) == ESP_OK) {
ESP_LOGI(TAG, "Network Configuration:");
ESP_LOGI(TAG, " IP Address: %s", ip);
ESP_LOGI(TAG, " Netmask: %s", netmask);
ESP_LOGI(TAG, " Gateway: %s", gateway);
}
if (ethernet_get_mac(mac) == ESP_OK) {
ESP_LOGI(TAG, " MAC Address: %s", mac);
}
ESP_LOGI(TAG, "========================================");
ESP_LOGI(TAG, "BMC Ready!");
ESP_LOGI(TAG, "Web interface: http://%s/", ip);
ESP_LOGI(TAG, "========================================");
return;
}