/* main.c — ESP32-S3 IO board app_main */

#include "uart_ota_recv.h"
#include "config.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

static const char *TAG = "io_main";

static void uart_init(void)
{
    uart_config_t cfg = {
        .baud_rate  = IO_UART_BAUD,
        .data_bits  = UART_DATA_8_BITS,
        .parity     = UART_PARITY_DISABLE,
        .stop_bits  = UART_STOP_BITS_1,
        .flow_ctrl  = UART_HW_FLOWCTRL_DISABLE,
    };
    uart_param_config(IO_UART_PORT, &cfg);
    uart_set_pin(IO_UART_PORT, IO_UART_TX_GPIO, IO_UART_RX_GPIO,
                  UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(IO_UART_PORT, 4096, 0, 0, NULL, 0);
}

void app_main(void)
{
    ESP_LOGI(TAG, "ESP32-S3 IO v%s starting", IO_FW_VERSION);

    /* Mark running image valid (OTA rollback support) */
    esp_ota_mark_app_valid_cancel_rollback();

    uart_init();
    uart_ota_recv_init();

    /* IO board main loop placeholder — RC/motor/sensor tasks added in later beads */
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
