/*
 * UART 通信驱动实现
 * 用于 Release 模式下接管「APP_DEBUG_UART_NUM」对应 UART 作为普通通信串口
 */

#include "uart_comm.h"
#include "core/build_config.h"

#if defined(CONFIG_APP_UART_MODE_DEBUG) && defined(CONFIG_ROBO_APP_FW_RELEASE)

#include "driver/uart.h"
#include <string.h>

#define UART_COMM_PORT      ((uart_port_t)CONFIG_APP_DEBUG_UART_NUM)

static int initialized_s = 0;

esp_err_t uart_comm_init(int baudrate)
{
    if (initialized_s) {
        return ESP_OK;
    }

    if (baudrate <= 0) {
        baudrate = UART_COMM_DEFAULT_BAUDRATE;
    }

    uart_config_t uart_cfg = {
        .baud_rate = baudrate,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };

    esp_err_t err = uart_param_config(UART_COMM_PORT, &uart_cfg);
    if (err != ESP_OK) {
        return err;
    }

    /* 设置 TX/RX 引脚（UART1/UART2 必须显式设置，UART0 有默认 GPIO43/44） */
    int tx_gpio = CONFIG_APP_DEBUG_UART_TX_GPIO;
    int rx_gpio = CONFIG_APP_DEBUG_UART_RX_GPIO;
    if (tx_gpio >= 0 && rx_gpio >= 0) {
        err = uart_set_pin(UART_COMM_PORT, tx_gpio, rx_gpio,
                           UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
        if (err != ESP_OK) {
            return err;
        }
    }

    err = uart_driver_install(UART_COMM_PORT, UART_COMM_BUFFER_SIZE,
                              UART_COMM_BUFFER_SIZE, UART_COMM_QUEUE_SIZE,
                              NULL, 0);
    if (err != ESP_OK) {
        return err;
    }

    initialized_s = 1;
    return ESP_OK;
}

void uart_comm_deinit(void)
{
    if (!initialized_s) {
        return;
    }
    uart_driver_delete(UART_COMM_PORT);
    initialized_s = 0;
}

int uart_comm_send(const uint8_t *data, size_t len)
{
    if (!initialized_s || !data || len == 0) {
        return -1;
    }
    int written = uart_write_bytes(UART_COMM_PORT, data, len);
    return written;
}

int uart_comm_send_string(const char *str)
{
    if (!str) {
        return -1;
    }
    return uart_comm_send((const uint8_t *)str, strlen(str));
}

int uart_comm_receive(uint8_t *buf, size_t buf_size, uint32_t timeout_ms)
{
    if (!initialized_s || !buf || buf_size == 0) {
        return -1;
    }
    int len = uart_read_bytes(UART_COMM_PORT, buf, buf_size - 1,
                              pdMS_TO_TICKS(timeout_ms));
    if (len > 0) {
        buf[len] = '\0';
    }
    return len;
}

int uart_comm_is_initialized(void)
{
    return initialized_s;
}

void uart_comm_flush(void)
{
    if (!initialized_s) {
        return;
    }
    uart_flush(UART_COMM_PORT);
}

#else /* !(CONFIG_APP_UART_MODE_DEBUG && CONFIG_ROBO_APP_FW_RELEASE) */

/* 当功能未启用时，提供空实现（避免编译错误） */

esp_err_t uart_comm_init(int baudrate)
{
    (void)baudrate;
    return ESP_ERR_NOT_SUPPORTED;
}

void uart_comm_deinit(void) {}

int uart_comm_send(const uint8_t *data, size_t len)
{
    (void)data;
    (void)len;
    return -1;
}

int uart_comm_send_string(const char *str)
{
    (void)str;
    return -1;
}

int uart_comm_receive(uint8_t *buf, size_t buf_size, uint32_t timeout_ms)
{
    (void)buf;
    (void)buf_size;
    (void)timeout_ms;
    return -1;
}

int uart_comm_is_initialized(void)
{
    return 0;
}

void uart_comm_flush(void) {}

#endif /* CONFIG_APP_UART_MODE_DEBUG && CONFIG_ROBO_APP_FW_RELEASE */
