#include "can_task.h"
#include "J1939.H"
#include "can_cali.h"
#include "can_id_deal.h"
#include "can_interface.h"
#include "iap.h"
#include "queue.h"
#include "utils.h"
#include <stdint.h>
#include <string.h>

queue_entry(pdu_tag, 40) can_rx_queue;
queue_entry(pdu_tag, 40) can_tx_queue;

uint8_t     PGN_00B000H_Ary[20] = {0};
extern void j1939_msg_push_queue(p_pdu_tag rec_msg);
uint8_t     recv_can_id = 0;

uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
{
    pdu_tag      response_msg;
    QUEUE_STATUS result_status;
    response_msg.id.r    = id;
    response_msg.reg.dlc = len;
    memcpy(&response_msg.data.u8_buf[0], p_data, len);

    if (id > 0x7FF)
    {
        response_msg.reg.ide = CAN_ID_EXT;
    }
    else
    {
        response_msg.reg.ide = CAN_ID_STD;
    }

    __disable_irq();
    en_queue(&can_tx_queue, response_msg, result_status);
    __enable_irq();

    if (result_status != Q_OK)
    {
        return 0;
    }
    return 1;
}

static uint8_t can_tx_frame(pdu_tag can_message)
{

    can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
    return 1;
}

void can_tx_callback(void)
{
    pdu_tag      tx_data;
    QUEUE_STATUS result_status;

    de_queue(&can_tx_queue, tx_data, result_status);

    // 返回值为1代表读取成功
    if (result_status == Q_OK)
    {
        can_tx_frame(tx_data);
        CAN_IT_TME_ENABLE();
    }
    else
    {
        CAN_IT_TME_DISABLE();
    }
}

void can_rx_callback(CanRxMsg rx_message)
{
    pdu_tag      data;
    uint8_t      ps;
    uint8_t      pf;
    QUEUE_STATUS result;

    switch (rx_message.ExtId)
    {
    case 0x18011801:
        data.id.r    = 0x18011801;
        data.reg.dlc = rx_message.DLC;
        memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
        en_queue(&can_rx_queue, data, result);
        break;
    case 0x18011802:
        data.id.r    = 0x18011802;
        data.reg.dlc = rx_message.DLC;
        memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
        break;
    default:
        pf = (rx_message.ExtId & CAN_PGN_PF) >> 16;
        if (pf >= 0xF0)
        {
            data.id.r   = rx_message.ExtId;
            recv_can_id = data.id.b.sa;
            memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
            en_queue(&can_rx_queue, data, result);
        }
        else
        {
            ps = (rx_message.ExtId & CAN_PGN_PS) >> 8;
            if (ps == 0xF4)
            {
                data.id.r    = rx_message.ExtId;
                recv_can_id  = data.id.b.sa;
                data.reg.dlc = rx_message.DLC;
                memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);

                if (data.id.b.pf == 0xDF)
                {
                    en_queue(&can_rx_queue, data, result);
                }
                else
                {
                    en_queue(&can_rx_queue, data, result);
                }
            }
            else
            {
                en_queue(&can_rx_queue, data, result);
            }
        }
    }
}

uint8_t tx_flag = 0;

void can_rx_ctl(pdu_tag rec_msg)
{
    J1939_TP_TX_Message(0x00B300, 0xE1, PGN_00B000H_Ary, 20, 1);
}

uint8_t tx_data[8] = {0};
uint8_t flag       = 0;
void    can_rx_param(pdu_tag rec_msg)
{
    tx_data[0] = rec_msg.data.u8_buf[0];
    tx_data[1] = rec_msg.data.u8_buf[1];
    tx_data[2] = rec_msg.data.u8_buf[2];
    tx_data[3] = rec_msg.data.u8_buf[3];
    if (tx_data[1] = 0xFF)
    {
        tx_flag = 1;
    }
    else
    {
        flag = 1;
    }
}

void can_rx_update(pdu_tag rec_msg)
{
    if ((rec_msg.data.u8_buf[0] == 'A') && (rec_msg.data.u8_buf[1] == 'P') && (rec_msg.data.u8_buf[2] == 'P') && (rec_msg.data.u8_buf[3] == 'L') && (rec_msg.data.u8_buf[4] == 'O') && (rec_msg.data.u8_buf[5] == 'A') && (rec_msg.data.u8_buf[6] == 'D') && (rec_msg.data.u8_buf[7] == 0x01))
    {
        __set_FAULTMASK(1); //  关闭所有中断
        NVIC_SystemReset(); //  复位
    }
}

can_rx_tab can_tab[] = {
    {0x18011802, can_rx_param},
    {0x18011801, can_rx_ctl},
    {CAN_IAP, iap_rec_handler},
    {CAN_CALI, can_cali_msg_analysis}

};

void can_start_send(void)
{
    pdu_tag      tx_msg;
    QUEUE_STATUS result_status;

    if (0 == ((CAN_IER) & 0x1))
    {
        de_queue(&can_tx_queue, tx_msg, result_status);

        if (result_status == Q_OK) // 返回值为1代表读取成功
        {
            can_tx_frame(tx_msg);
            CAN_IT_TME_ENABLE();
        }
    }
}

static void can_rx_process(void)
{
    uint8_t      i;
    uint8_t      flg = 0;
    pdu_tag      rec_msg;
    QUEUE_STATUS result;

    for (uint8_t j = 0; j < 15; j++)
    {
        __disable_irq();
        de_queue(&can_rx_queue, rec_msg, result);
        __enable_irq();

        if (Q_OK == result)
        {
            for (i = 0; i < ARR_SIZE(can_tab); i++)
            {
                if (can_tab[i].id == rec_msg.id.r)
                {
                    can_tab[i].p_func(rec_msg);
                    flg = 1;
                    break;
                }
                else if (can_tab[i].id == (uint32_t)(rec_msg.id.b.pf << 8 | rec_msg.id.b.ps))
                {
                    can_tab[i].p_func(rec_msg);
                    flg = 1;
                    break;
                }
                else if (can_tab[i].id == rec_msg.id.b.pf)
                {
                    can_tab[i].p_func(rec_msg);
                    flg = 1;
                    break;
                }
            }

            if (flg == 0)
            {
                j1939_msg_push_queue(&rec_msg);
            }
            flg = 0;
        }
    }
}

void can_tx_0x18010000_message(void)
{
    uint8_t data[8] = {0};
    data[2]         = 0xFF;
    push_can_message_to_queue(0x18010000, 8, data);
}

void can_tx_0x18010001_message(void)
{
    uint8_t data[8] = {0};
    push_can_message_to_queue(0x18010001, 8, data);
}

void can_tx_0x18030001_message(void)
{
    uint8_t data[8] = {0};
    data[1]         = 8;
    push_can_message_to_queue(0x18030001, 8, data);
}

void can_tx_0x18010002_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 0x02;
    push_can_message_to_queue(0x18010002, 8, data);
}

void can_tx_0x18030003_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 0x02;
    push_can_message_to_queue(0x18030003, 8, data);
}

void can_tx_0x18010004_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010004, 8, data);
}

void can_tx_0x18010005_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010005, 8, data);
}

void can_tx_0x18010006_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010006, 8, data);
}

void can_tx_0x18010007_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010007, 8, data);
}

void can_tx_0x18010008_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010008, 8, data);
}

void can_tx_0x18010009_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x18010009, 8, data);
}

void can_tx_0x1801000F_message(void)
{
    uint8_t data[8] = {0};

    data[0] = 1;
    data[1] = 5;
    data[2] = 3000 & 0xFF;
    data[3] = 3000 >> 8;
    data[4] = 3000 & 0xFF;
    data[5] = 3000 >> 8;
    push_can_message_to_queue(0x1801000F, 8, data);
}

void can_tx_0x18010010_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 1;
    data[1]         = 1;
    data[4]         = 0;
    data[5]         = 0;
    push_can_message_to_queue(0x18010010, 8, data);
}

void can_tx_0x1801000A_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x1801000A, 8, data);
}

void can_tx_0x1801000B_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x1801000B, 8, data);
}

void can_tx_0x1801000C_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x1801000C, 8, data);
}

void can_tx_0x1801000D_message(void)
{
    uint8_t data[8] = {0};
    data[4]         = 33;
    data[5]         = 22;
    push_can_message_to_queue(0x1801000D, 8, data);
}

void can_tx_0x18010012_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 24;
    data[1]         = 1;
    data[2]         = 3;
    data[3]         = 11;
    data[4]         = 12;
    data[5]         = 13;
    data[6]         = 12;
    data[7]         = 13;
    push_can_message_to_queue(0x18010012, 8, data);
}

void can_tx_0x18010017_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 24;
    data[1]         = 1;
    data[0]         = 3;
    data[1]         = 11;
    data[0]         = 12;
    data[1]         = 13;
    push_can_message_to_queue(0x18010017, 8, data);
}

void can_tx_0x18010018_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 2;
    data[1]         = 0xFF;
    push_can_message_to_queue(0x18010018, 8, data);
}

void can_tx_0x18010019_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 2;
    data[1]         = 0xFF;
    push_can_message_to_queue(0x18010019, 8, data);
}

void can_tx_0x1801001A_message(void)
{
    volatile static int i       = 0;
    uint8_t             data[8] = {0};

    data[0] = 2 + i;
    data[1] = 0x12 >> i;
    i++;
    if (i > 7)
    {
        i = 0;
    }
    push_can_message_to_queue(0x1801001A, 8, data);
}

void can_tx_0x1801001B_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 2;
    data[1]         = 0xFF;
    push_can_message_to_queue(0x1801001B, 8, data);
}

void can_tx_0x1801001C_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 2;
    data[4]         = 0xFF;
    push_can_message_to_queue(0x1801001C, 8, data);
}

void can_tx_0x1801001D_message(void)
{
    uint8_t data[8] = {0};
    data[0]         = 3;
    data[2]         = 0xFF;
    push_can_message_to_queue(0x1801001D, 8, data);
}

void can_tx_0x18010023_message(void)
{
    uint8_t send_data[8] = {0};
    data_bit_move(0, 8, 'A');
    data_bit_move(8, 8, 'A');
    data_bit_move(16, 8, 'A');
    data_bit_move(24, 8, 'C');
    data_bit_move(32, 8, 'A');
    data_bit_move(40, 8, '-');
    data_bit_move(48, 8, 'A');
    data_bit_move(56, 8, 'B');
    product_array(send_data);
    push_can_message_to_queue(0x18010023, 8, send_data);
}

void can_tx_0x18011802_message(void)
{
    static uint8_t fdf = 0;
    if (flag == 1)
    {
        push_can_message_to_queue(0x18011802, 8, tx_data);
        flag = 0;
    }
    uint8_t  data[8] = {0};
    uint16_t addr    = 0xA000;
    if (tx_flag == 1)
    {
        data[0] = (addr + fdf) & 0xFF;
        data[1] = (addr + fdf) >> 8;
        data[2] = 0xFF;
        data[3] = 8;
        fdf++;
        if (fdf == 80)
        {
            tx_flag = 0;
        }
        push_can_message_to_queue(0x18011802, 8, data);
    }
}

void can_tx_0x18011803_message(void)
{
    uint8_t        data[8] = {0};
    uint16_t       addr    = 0xB000;
    static uint8_t fdf     = 0;
    static int16_t a       = -263;
    data[0]                = (addr + fdf) & 0xFF;
    data[1]                = (addr + fdf) >> 8;
    data[2]                = a & 0xFF;
    data[3]                = a >> 8;
    fdf++;
    a++;
    if (fdf > 6)
    {
        fdf = 0;
    }
    push_can_message_to_queue(0x18011803, 8, data);
}

void can_tx_0x18011803_2_message(void)
{
    uint8_t        data[8] = {0};
    static uint8_t fdf_2   = 0;

    uint16_t addr_vol = 0xB110;
    data[0]           = (addr_vol + fdf_2) & 0xFF;
    data[1]           = (addr_vol + fdf_2) >> 8;
    data[2]           = 1;
    data[3]           = -2 & 0xFF;
    data[4]           = -2 >> 8;
    fdf_2++;
    if (fdf_2 > 80)
    {
        fdf_2 = 0;
    }
    push_can_message_to_queue(0x18011803, 8, data);
}

void can_tx_0x18180003_message(void)
{
    uint8_t data[8] = {0};
    data_bit_move(0, 16, 0);
    data_bit_move(16, 16, 0x2710);
    data_bit_move(32, 16, 0x4E20);
    data_bit_move(48, 16, 0x7530);
    product_array(data);
    push_can_message_to_queue(0x18190006, 8, data);
}

void can_tx_0x18190007_message(void)
{
    uint8_t data[8] = {0};
    data_bit_move(0, 16, 0xDFFF);
    data_bit_move(16, 16, 0xF000);
    data_bit_move(32, 16, 0xE800);
    data_bit_move(48, 16, 0xE13D);
    product_array(data);
    push_can_message_to_queue(0x18190005, 8, data);
}

void can_tx_0x18190009_message(void)
{
    uint8_t data[8] = {0};
    data_bit_move(0, 16, 0xDFFF);
    data_bit_move(16, 16, 0xF000);
    data_bit_move(32, 16, 0xE800);
    data_bit_move(48, 16, 0xE13D);
    product_array(data);
    push_can_message_to_queue(0x18190005, 0, data);
}

void send_message(void)
{
    static volatile uint8_t times = 0;

    if (times >= 100)
    {
        times = 0;
    }
    if (times % 100 == 0)
    {
        can_tx_0x18010001_message();
        can_tx_0x18010002_message();
        can_tx_0x18010004_message();
        can_tx_0x18010005_message();
    }
    else if (times % 100 == 5)
    {
        can_tx_0x18010006_message();
        can_tx_0x18010007_message();
        can_tx_0x18010008_message();
        can_tx_0x18010009_message();
        can_tx_0x1801000A_message();
    }
    else if (times % 100 == 10)
    {
        can_tx_0x1801000B_message();
        can_tx_0x1801000C_message();
        can_tx_0x1801000D_message();
        can_tx_0x18010023_message();
        can_tx_0x18010010_message();
    }
    else if (times % 100 == 15)
    {
        can_tx_0x18010018_message();
        can_tx_0x18010019_message();
        can_tx_0x1801001A_message();
        can_tx_0x1801001B_message();
        can_tx_0x1801001C_message();
    }
    else if (times % 100 == 20)
    {
        can_tx_0x1801001D_message();
        can_tx_0x1801000F_message();
        can_tx_0x18030003_message();
        can_tx_0x18030001_message();
        can_tx_0x18010000_message();
    }
    else if (times % 100 == 25)
    {
        can_tx_0x18010017_message();
        can_tx_0x18010012_message();
        can_tx_0x18011802_message();
        can_tx_0x18011803_message();
        can_tx_0x18011803_2_message();
        can_tx_0x18180003_message();
        can_tx_0x18190007_message();
    }
    else
    {
        // return;
    }
    times++;
}

void can_network_init(void)
{
    queue_init(&can_rx_queue);
    queue_init(&can_tx_queue);
    can_rx_back_init(can_rx_callback);
    can_tx_back_init(can_tx_callback);
}

void can_process(void)
{
    can_rx_process();
    send_message();
    can_start_send();
}
