#include "can_debug.h"
#include "stm32f1xx_hal.h"
#include "string.h"
#include "stdbool.h"
#include "soft_p_2_c.h"
#include "crc.h"
#include "soft_uart.h"

rkfifo_t candebug_rkfifo;
Debug_buf debug_can;

void check_can_dev_connect(void)
{
    if(debug_can.Total_Dev_num != 0)
    {
        for(uint8_t i=0;i<debug_can.Total_Dev_num;i++)
        {
            if(HAL_GetTick() -  debug_can.ID_buf[i].ID_time > 5000)
            {
                debug_can.ID_buf[i].connect_status = 2;
            }
            else
            {
                debug_can.ID_buf[i].connect_status = 1;
            }

        }
    }
}


int get_data_total_len(uint8_t i)
{   
    uint8_t len = 0;
    len += 4; //ID
    len++;  //len
    len++; //status
    len++; //send_fre
    len += i;
    return len;
}

void Can_send_debug_to_app(void)
{
    int index = 0;
    uint16_t crc = 0;
    uint8_t send_time = debug_can.Total_Dev_num / 16 + 1;
    uint8_t element_num = 0,total_pack_num = 0;

    check_can_dev_connect();

    for(uint8_t i = 0;i < send_time; i++)
    {
        index = 0;
        crc = 0;

        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0; //组件计数
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSGID_CANDEBUG;

        debug_can.Len = get_data_total_len;

        if(send_time > 1)
        {
            element_num = i * 15;
            total_pack_num = (send_time - 1 - i) == 0? debug_can.Total_Dev_num - (i *15) : 15;
        }
        else
        {
            element_num = 0;
            total_pack_num = debug_can.Total_Dev_num;
        }
        msg_buf[index++] = total_pack_num;
        
        for(uint8_t i=0;i<total_pack_num;i++)
        {
            memcpy(&msg_buf[index], &debug_can.ID_buf[i + element_num].ID, debug_can.Len(debug_can.ID_buf[i + element_num].len));
            index += debug_can.Len(debug_can.ID_buf[i + element_num].len);
        }

        msg_buf[1] = index - 6;

        crc = Get_Crc16(msg_buf, index);
        msg_buf[index++] = crc;
        msg_buf[index++] = (crc >> 8) & 0xff;

        uart2_send_msg(msg_buf, index);
        //uart3_send_msg(msg_buf, index);
    }

}

void register_can_dev_func(uint8_t *buf)
{   
    #pragma pack(1)
    struct can_dev_temp
    {
        uint32_t ID;
        uint8_t len;
        uint8_t data[8];
        uint32_t time;
    };
    #pragma pack(0)

    struct can_dev_temp temp_a;
    uint8_t i = 0;

    memcpy(&temp_a.ID,buf,sizeof(struct can_dev_temp));

    if(debug_can.Total_Dev_num != 0)
    {
        for(i=0;i<debug_can.Total_Dev_num;i++)
        {
            if(debug_can.ID_buf[i].ID == temp_a.ID)
            {
                break;
            }
            if((i + 1 == debug_can.Total_Dev_num) && (debug_can.Total_Dev_num < MAX_ID_NUM - 1)) //防止超buf
            {
                debug_can.Total_Dev_num++;
                i++;
                break;
            }
        }
    }
    else
    {
        debug_can.Total_Dev_num++;
    }

    debug_can.ID_buf[i].ID = temp_a.ID;
    debug_can.ID_buf[i].len = temp_a.len;
    debug_can.ID_buf[i].send_fre = 1000 / (HAL_GetTick() - debug_can.ID_buf[i].ID_time);
    debug_can.ID_buf[i].ID_time = HAL_GetTick();
    memcpy(&debug_can.ID_buf[i].data[0],&temp_a.data[0],temp_a.len);
}


void seek_can_debug_buf_adr(void)
{   
    uint8_t temp_buf[128] = {0}, c = 0, seek_i = 0;

    while (rkfifo_out(&candebug_rkfifo, &c, 1) != 0)
    {
        temp_buf[seek_i] = c;

        if(temp_buf[seek_i] == 'K' && temp_buf[seek_i - 1] == 'V')
        {
            temp_buf[seek_i] = c;
            register_can_dev_func(temp_buf);
            seek_i = 0;
            break;
        }
        else
        {
            seek_i++;
        }
    }

}

void put_candata_to_rkfifo_rx(CAN_RxHeaderTypeDef rxhead,uint8_t *data)
{   
    uint32_t cur_time = HAL_GetTick();
    uint8_t tem_buf[30] = {0},i = 0;

    if(rxhead.IDE == CAN_ID_STD)
        memcpy(&tem_buf[i],&rxhead.StdId,4);
    else
        memcpy(&tem_buf[i],&rxhead.ExtId,4);

    i += 4;
    tem_buf[i++] = rxhead.DLC;
    memcpy(&tem_buf[i],&data[0],8);
    i += 8;
    memcpy(&tem_buf[i],&cur_time,4);
    i += 4;
    tem_buf[i++] = 'V';
    tem_buf[i++] = 'K';

    rkfifo_in(&candebug_rkfifo,tem_buf,i);
}

void put_candata_to_rkfifo_tx(CAN_TxHeaderTypeDef txhead,uint8_t *data)
{   
    uint32_t cur_time = HAL_GetTick();
    uint8_t tem_buf[30] = {0},i = 0;

    if(txhead.IDE == CAN_ID_STD)
        memcpy(&tem_buf[i],&txhead.StdId,4);
    else
        memcpy(&tem_buf[i],&txhead.ExtId,4);

    i += 4;
    tem_buf[i++] = txhead.DLC;
    memcpy(&tem_buf[i],&data[0],8);
    i += 8;
    memcpy(&tem_buf[i],&cur_time,4);
    i += 4;
    tem_buf[i++] = 'V';
    tem_buf[i++] = 'K';

    rkfifo_in(&candebug_rkfifo,tem_buf,i);
}

