/*******************************************************************************
 * @file    uart_handler.c
 * @brief   UART Communication Handler
 * @author  Charles KWON <charleskwon@medithings.co.kr>
 * @date    2025-01-30
 * @copyright (c) 2025 Medithings Inc. All rights reserved.
 *
 * @details UART initialization and event handling for serial communication.
 ******************************************************************************/

#include "uart_handler.h"
#include "app_uart.h"
#include "app_error.h"
#include "boards.h"
#include "ble_nus.h"
#include "main.h"
#include <cmd_parse.h>

#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

/*==============================================================================
 * EXTERNAL DECLARATIONS
 *============================================================================*/

extern uint16_t m_ble_nus_max_data_len;
extern which_cmd_t cmd_type_t;

/*==============================================================================
 * UART EVENT HANDLER
 *============================================================================*/

static void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN] = {0,};
    static uint8_t index = 0;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {
                    cmd_type_t = CMD_UART;
                    received_command_process(data_array, CMD_UART, index);
                }
                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

/*==============================================================================
 * UART INITIALIZATION
 *============================================================================*/

void uart_handler_init(void)
{
    uint32_t err_code;
    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
#if defined (UART_PRESENT)
        .baud_rate    = NRF_UART_BAUDRATE_115200
#else
        .baud_rate    = NRF_UARTE_BAUDRATE_115200
#endif
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);
    APP_ERROR_CHECK(err_code);
}
