#include "uart_task.h"
#include "uart_interface.h"
#include "uart_table.h"
#include "utils.h"
#include <stdint.h>
#include <time.h>

uint8_t port_cmd_first(uart_type *p_data)
{
    if (p_data->rx_len > 3)
        return 0;

    if (p_data->rx[2] != 0x04)
        return 0;

    p_data->device_type = p_data->rx[1];

    p_data->tx_len = 10;

    p_data->tx[0] = 0x01;

    p_data->tx[1] = 0x03;

    return 1;
}

uint8_t port_cmd_read(uart_type *p_data)
{
    uint8_t len  = 0;
    uint8_t addr = 0;
    // len_check
    if (p_data->rx_len > 6)
        return 0;

    // cec_check
    // if (p_data->rx[p_data->rx_len - 1] != cec8(p_data->rx, p_data->rx_len -1));

    p_data->device_type = p_data->rx[1];

    addr = (p_data->rx[3] << 8) | p_data->rx[2];
    len  = p_data->rx[4];

    if (0 == get_regist_value(p_data, addr, len))
    {
        return 0;
    }

    p_data->tx_len = len + 5;

    p_data->tx[0]                  = 0x02;
    p_data->tx[1]                  = p_data->rx[2];
    p_data->tx[2]                  = p_data->rx[3];
    p_data->tx[3]                  = p_data->rx[4];
    p_data->tx[p_data->tx_len - 1] = 0x99; // cec8(p_data->tx, p_data->tx_len - 1)

    return 1;
}

uint8_t port_cmd_write(uart_type *p_data)
{
    uint8_t len  = 0;
    uint8_t addr = 0;
    // len_check
    if (p_data->rx_len != (p_data->rx[4] + 6))
        return 0;

    // cec_check
    // if (p_data->rx[p_data->rx_len - 1] != cec8(p_data->rx, p_data->rx_len -1));

    p_data->device_type = p_data->rx[1];

    addr = (p_data->rx[3] << 8) | p_data->rx[2];
    len  = p_data->rx[4];

    if (1 == set_regist_value(p_data, addr, len))
    {
        p_data->tx[1] = 0x00;
    }
    else
    {
        p_data->tx[1] = 0xFF;
    }

    p_data->tx_len = 3;
    // cmd
    p_data->tx[0] = 0x03;
    p_data->tx[2] = 0x99; // cec8(p_data->tx, p_data->tx_len - 1)

    return 1;
}

const com_protocol_type com_protocol_table[] = {
    {0x01, port_cmd_first},
    {0x02, port_cmd_read},
    {0x03, port_cmd_write},
};

void uart_conn_check(void)
{
    static uint16_t count = 0;

    if (uart_msg.disconnect_flg)
    {
        count += 10;
        if (count >= 3000)
        {
            count                = 3000;
            uart_msg.device_type = 0;
        }
    }
    else
    {
        if (uart_msg.device_type == 0x02)
        {
            count = 0;
        }
    }
}

uint8_t uart_receive_analysis(uart_type *p_data)
{
    uint8_t i, len;
    len = ARR_SIZE(com_protocol_table);

    for (i = 0; i < len; i++)
    {

        if ((p_data->rx[0] == com_protocol_table[i].cmd) && (com_protocol_table[i].p_func != NULL))
        {
            if (1 == com_protocol_table[i].p_func(p_data))
            {
                return 1;
            }
        }
    }
    return 0;
}

void uart_task(void)
{

    if (uart_msg.rx_finished_flg)
    {
        uart_msg.rx_finished_flg = 0;
        uart_msg.rx_len          = uart_msg.rx_count_u8;
        uart_msg.rx_count_u8     = 0;
        if (1 == uart_receive_analysis(&uart_msg))
        {
            uart_start_send(&uart_msg);
        }
    }
    uart_conn_check();
}
