#ifndef UART_TRANS_H
#define UART_TRANS_H

#include <stdbool.h>
#include <stdint.h>

#include "FreeRTOS.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
    UART_CHANNEL_U0 = 0,
    UART_CHANNEL_U1 = 1,
    UART_CHANNEL_MAX
} uart_channel_t;

typedef enum {
    UART_TRANS_SEND_OK = 0,
    UART_TRANS_SEND_INVALID_INPUT,
    UART_TRANS_SEND_RING_FULL,
    UART_TRANS_SEND_KICK_FAILED
} uart_trans_send_result_t;

typedef struct {
    uint8_t src_id;
    uint8_t dst_mask;
    uint16_t payload_len;
    uint8_t payload[256];
} uart_mux_frame_t;

#define UART_RX_DMA_BUFFER_SIZE   128u
#define UART_TX_DMA_BUFFER_SIZE   128u
#define UART_RX_RING_BUFFER_SIZE  256u
#define UART_TX_RING_BUFFER_SIZE  256u

int uart_trans_init(void);
int uart_trans_start_all(void);
const char *uart_trans_send_result_to_str(uart_trans_send_result_t result);
uart_trans_send_result_t uart_trans_send_buffer(uart_channel_t channel, const uint8_t *data, uint16_t len);
void uart_trans_notify_rx_from_isr(uart_channel_t channel, BaseType_t *xHigherPriorityTaskWoken);
void uart_trans_tx_cplt_handler(uart_channel_t channel);
void UartRxTask(void *argument);
bool uart_mux_try_extract_frame(uart_channel_t channel, uart_mux_frame_t *frame);
bool uart_mux_encode_frame(uint8_t src_id,
                           uint8_t dst_mask,
                           const uint8_t *payload,
                           uint16_t payload_len,
                           uint8_t *out,
                           uint16_t *out_len,
                           uint16_t out_capacity);

#ifdef __cplusplus
}
#endif

#endif
