#include "main_task.h"
#include "soft_led.h"
#include "stdio.h"
#include "stdbool.h"
#include "string.h"
#include "main.h"
#include "soft_usart.h"
#include "usart_data_handle.h"
#include "soft_flash.h"
#include "soft_crc.h"
#include "soft_radar_handle.h"
#include "soft_engine.h"
#include "soft_device.h"
#include "soft_timer.h"
#include "config.h"
#include "soft_can.h"
#include "soft_bms.h"
#include "soft_adc.h"
#include "soft_version.h"
#include "soft_update.h"
#include "soft_obstacle.h"
#include "soft_water.h"
#include "soft_terrain.h"
//#include "can_debug.h"

uint8_t msg_buf[256] = {0};
bool terrain_is_link = false;
bool obs_f_is_link = false;
bool obs_b_is_link = false;


/**
  * @file    Update_ack_func
  * @brief   PMU回复ACK 回复升级工具
  * @param   组件ID，消息ID，ACK数据
  * @details 升级应答FMU透传，协议和升级工具一致,和FMU不一样
  * @author  Zhang Sir 
 **/
void Update_ack_func(uint8_t group_id, uint8_t msg_id, uint8_t *ackbuf)
{
    
    int index = 0;
    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = group_id;
    msg_buf[index++] = _MSGID_ACK;

    msg_buf[index++] = msg_id;
    msg_buf[index++] = *ackbuf;
    msg_buf[index++] = *(ackbuf + 1);
    msg_buf[index++] = 1;

    msg_buf[1] = index - 6;
    uint16_t uart_crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &uart_crc, 2);
    index += 2;

    usart1_send_msg(msg_buf, index);
}


/**
  * @file    pmu_set_ack
  * @brief   PMU应答FMU
  * @param   命令 命令内容 123
  * @details 
  * @author  Zhang Sir 
 **/
char ack_id = 0;
short ack_content = 0;
short ack_content1 = 0;
short ack_content2 = 0;
void pmu_set_ack(uint8_t id,short content1, short content2,short content3)
{
    pmu_send = PMU_SEND_ACK;
    ack_id = id;
    ack_content = content1;
    ack_content1 = content2;
    ack_content2 = content3;
}   


/**
  * @file    pmu_to_con_voltage_data
  * @brief   PMU发送电压、温度信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_voltage_data()
{
    int index = 0;
    uint16_t temp_16t = 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_VOL;

    temp_16t = Adc_Get(ADC_Bms) / 10.0f + cur_par.voltage * 10; // 获取当前板子电压
    memcpy(&msg_buf[index], &temp_16t, 2);
    index += 2;

    temp_16t = Adc_Get(ADC_Tempture);
    memcpy(&msg_buf[index], &temp_16t, 2);
    index += 2;

    msg_buf[index++] = 0;
    msg_buf[index++] = 0;

    temp_16t = (uint16_t)stor_par.abnormal_outage_flag;
    memcpy(&msg_buf[index], &temp_16t, 2);
    index += 2;

    msg_buf[1] = index - 6;

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

    usart1_send_msg(msg_buf, index);
}
/**
  * @file    pmu_to_con_heart_data
  * @brief   PMU_发送心跳
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_heart_data()
{
    int index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00; 
    msg_buf[index++] = 21;

    msg_buf[index++] = _MSGID_HEART;
    ack_content = 0x56;
    memcpy(&msg_buf[index],&ack_content,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content1,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content2,2);
    index += 2;  
    
    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    usart1_send_msg(msg_buf, index);
}

/**
  * @file    pmu_to_con_engin_data
  * @brief   PMU发送发动机信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_engine_data(void)
{
    uint8_t index = 0;
    if(engine_link_status == COMP_NORMAL)
    {
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0; //组件计数
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSG_ENGIN;
        
        memcpy(&msg_buf[index],&engine_data.engine_type,sizeof(engine_data));
        index += (sizeof(engine_data));

        msg_buf[1] = index - 6;

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

        usart1_send_msg(msg_buf, index);
    }
}



/**
  * @file    pmu_to_con_devtype_data
  * @brief   PMU发送播撒、称重、水泵、离心喷头、在位传感器信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void check_and_put_msg(uint8_t *buf, uint16_t len)
{
    buf[1] = len - 6;

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

    usart1_send_msg(msg_buf, len);
}

void pmu_to_con_devtype_data(void)
{
    uint16_t index = 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_DEV_INFO;

    if(send_devinfo_time.flow == true)
    {
        msg_buf[index++] = DEV_FLOW;
        memcpy(&msg_buf[index],&Dev.Flow.facid,sizeof(Flow_info));
        index += sizeof(Flow_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.flow = false;
    }
    if (send_devinfo_time.radar == true)
    {
        msg_buf[index++] = DEV_RADAR;
        memcpy(&msg_buf[index],&Dev.Radar.facid_T,sizeof(Radar_info));
        index += sizeof(Radar_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.radar = false;
    }
    if(send_devinfo_time.checklow == true)
    {
        msg_buf[index++] = DEV_CHECKLOW;
        memcpy(&msg_buf[index],&Dev.Checklow.facid,sizeof(CheckLow_info));
        index += sizeof(CheckLow_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.checklow = false;
    }
    if(Dev.L_pump1_Link.connect_status == COMP_NORMAL && send_devinfo_time.L_pump1 == true)
    {
        msg_buf[index++] = DEV_L_PUMP1;
        memcpy(&msg_buf[index],&Dev.L_pump1.facid,sizeof(Linear_pump_info));
        index += sizeof(Linear_pump_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.L_pump1 = false;
    }
    if(Dev.L_pump2_Link.connect_status == COMP_NORMAL && send_devinfo_time.L_pump2 == true)
    {
        msg_buf[index++] = DEV_L_PUMP2;
        memcpy(&msg_buf[index],&Dev.L_pump2.facid,sizeof(Linear_pump_info));
        index += sizeof(Linear_pump_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.L_pump2 = false;
    }
    if(Dev.Weight_Link.connect_status == COMP_NORMAL && send_devinfo_time.weight == true)
    {
        msg_buf[index++] = DEV_WEIGHT;
        memcpy(&msg_buf[index],&Dev.Weight.facid,sizeof(Weight_info));
        index += sizeof(Weight_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.weight = false;
    }
    if(Dev.Part_Tradar_Link.connect_status == COMP_NORMAL && send_devinfo_time.part_radar == true)
    {
        msg_buf[index++] = DEV_PART_RADAR;
        memcpy(&msg_buf[index],&Dev.Part_radarT.facid,sizeof(Part_Tradar));
        index += sizeof(Part_Tradar);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.part_radar = false;
    }
    if ((Dev.Part_Fradar_Link.connect_status == COMP_NORMAL || Dev.DM_Part_Fradar_Link.connect_status == COMP_NORMAL) && send_devinfo_time.part_Fradar == true)
    {
        if (Dev.Part_Fradar_Link.connect_status == COMP_NORMAL)
        {
            msg_buf[index++] = DEV_PART_FRADAR;
            memcpy(&msg_buf[index], &Dev.Part_radarF.facid, sizeof(Part_FBradar));
            index += sizeof(Part_FBradar);
            check_and_put_msg(msg_buf, index);
            index = 6;
            send_devinfo_time.part_Fradar = false;
        }
        else if (Dev.DM_Part_Fradar_Link.connect_status == COMP_NORMAL)
        {
            msg_buf[index++] = DEV_PART_FRADAR;
            memcpy(&msg_buf[index], &Dev.DM_Part_radarF.facid, sizeof(Part_Fradar));
            index += sizeof(Part_Fradar);
            check_and_put_msg(msg_buf, index);
            index = 6;
            send_devinfo_time.part_Fradar = false;
        }
    }
    if((Dev.Part_Bradar_Link.connect_status == COMP_NORMAL || Dev.DM_Part_Bradar_Link.connect_status == COMP_NORMAL) && send_devinfo_time.part_Bradar == true)
    {
        if (Dev.Part_Bradar_Link.connect_status == COMP_NORMAL)
        {
            msg_buf[index++] = DEV_PART_BRADAR;
            memcpy(&msg_buf[index], &Dev.Part_radarB.facid, sizeof(Part_FBradar));
            index += sizeof(Part_FBradar);
            check_and_put_msg(msg_buf, index);
            index = 6;
            send_devinfo_time.part_Bradar = false;
        }
        else if (Dev.DM_Part_Bradar_Link.connect_status == COMP_NORMAL)
        {
            msg_buf[index++] = DEV_PART_BRADAR;
            memcpy(&msg_buf[index], &Dev.DM_Part_radarB.facid, sizeof(Part_Fradar));
            index += sizeof(Part_Fradar);
            check_and_put_msg(msg_buf, index);
            index = 6;
            send_devinfo_time.part_Bradar = false;
        }
    }
    if(Dev.Bms_Link.connect_status == COMP_NORMAL && send_devinfo_time.bms == true)
    {
        msg_buf[index++] = DEV_BMS;
        memcpy(&msg_buf[index],&Dev.Bms.facid,Dev.Bms.index + 1);
        index += Dev.Bms.index + 1;
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.bms = false;
    }
    if(Dev.Seed_Link.connect_status == COMP_NORMAL && send_devinfo_time.seed == true)
    {
        msg_buf[index++] = DEV_SEED;
        memcpy(&msg_buf[index],&Dev.Seed.facid,sizeof(Seed_info));
        index += sizeof(Seed_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.seed = false;
    }
    if (Dev.Pump_Link.connect_status == COMP_NORMAL && send_devinfo_time.pump == true)
    {
        msg_buf[index++] = DEV_PUMP;
        memcpy(&msg_buf[index],&Dev.Pump.facid,sizeof(Pump_info));
        index += sizeof(Pump_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.pump = false;
    }
    if (Dev.Nozzle_Link.connect_status == COMP_NORMAL && send_devinfo_time.nozzle == true)
    {
        msg_buf[index++] = DEV_NOZZLE;
        memcpy(&msg_buf[index],&Dev.Nozzle.facid,sizeof(Nozzle_info));
        index += sizeof(Nozzle_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.nozzle = false;
    }
    if (Dev.Arm_Link.connect_status == COMP_NORMAL && send_devinfo_time.arm == true)
    {
        msg_buf[index++] = DEV_ARM;
        memcpy(&msg_buf[index],&Dev.Arm.facid,sizeof(Arm_info));
        index += sizeof(Arm_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.arm = false;
    }
    if(Dev.Temperature_Sensor_Link.connect_status == COMP_NORMAL && send_devinfo_time.temperature == true)
    {
        msg_buf[index++] = DEV_TEMP_SENSOR;
        memcpy(&msg_buf[index],&Dev.Temperature_Sensor.facid,sizeof(Temperature_Sensor_info));
        index += sizeof(Temperature_Sensor_info);
        check_and_put_msg(msg_buf, index);
        index = 6;
        send_devinfo_time.temperature = false;
    }
    // else if(Dev.L_pump2_Link.connect_status == COMP_NORMAL && send_devinfo_time.L_pump2 == true)
    // {
    //     msg_buf[index++] = DEV_L_PUMP2;
    //     memcpy(&msg_buf[index],&Dev.L_pump2.facid,sizeof(Linear_pump_info));
    //     index += sizeof(Linear_pump_info);  
    //     send_devinfo_time.L_pump2 = false;
    // }

    // msg_buf[1] = index - 6;

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

    // usart1_send_msg(msg_buf, index);
}



/**
  * @file    pmu_to_con_version_data
  * @brief   PMU发送版本信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/

void pmu_to_con_version_data()
{
    //版本信息 0 - 5  硬件版本 IAP版本 APP版本
    uint32_t ver_msg_buf[4] = {0};
    
    int index = 0;
    ver_msg_buf[0] = cur_par.pmu_serial; //硬件版本号
    ver_msg_buf[1] = IAP_VERSION;  //IAP版本号
    ver_msg_buf[2] = APP_VERSION;  //APP版本号
    ver_msg_buf[3] = cur_par.pmu_serial;//serial.num;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = MSGID_REQ_VERSION;

    radar_version_check();

    memcpy(&msg_buf[index], ver_msg_buf, 16);
    index += 16;

    memcpy(&msg_buf[index], &radar_version[0][0], 10);
    index += 10;
    memcpy(&msg_buf[index], &radar_version[1][0], 10);
    index += 10;
    memcpy(&msg_buf[index], &radar_version[2][0], 10);
    index += 10;


    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    usart1_send_msg(msg_buf, index);

}


/**
  * @file    pmu_to_fcu_version_data
  * @brief   版本信息发送，新协议还没用
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/

void pmu_to_fcu_version_data()
{    
    
    //short dev_num = 40;
    uint8_t i = 0;

    dev_version_content *ptr = NULL;

    for(i = 0;i < dev_num;i++)
    {
        ptr = dev_ptr[i];
        if(ptr->send_times > 0)
        {
            break;
        }
        if(i == dev_num - 1)
        {
            return;
        }
    }

    uint8_t index = 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_DEV_LIST;

    memcpy(&msg_buf[index],&ptr->num,sizeof(dev_version_content) - sizeof(regist_type) - 1);
    index += sizeof(dev_version_content) - sizeof(regist_type) - 1;

    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;

    usart1_send_msg(msg_buf, index);
}


/**
  * @file    pmu_to_fcu_key_data
  * @brief   PMU发送秘钥信息
  * @param   none
  * @details 格式电池秘钥匹配
  * @author  Zhang Sir 
 **/
void pmu_to_fcu_key_data(void)
{
    uint8_t index = 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_SHA1;

    msg_buf[index++] = start_msg.Dev_type;
    msg_buf[index++] = start_msg.Id;
    msg_buf[index++] = start_msg.Id_content;
    // if(start_msg.Id == 4)
    // {
    //     start_msg.key_info_checking = false; //发送状态后结束发送
    // }

    memcpy(&msg_buf[index],&start_msg.key_data[0],20);
    index += 20;

    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;

    usart1_send_msg(msg_buf, index);

}


/**
  * @file    pmu_to_con_request_data
  * @brief   PMU发送请求信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
char request_id = 0;
short request_1_content = 0;
int request_2_content = 0;
void pmu_to_con_request_data()
{
    int index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 20;

    msg_buf[index++] = request_id;
    memcpy(&msg_buf[index],&request_1_content,2);
    index += 2;
    memcpy(&msg_buf[index],&request_2_content,4);
    index += 4;

    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    usart1_send_msg(msg_buf, index);
}


/**
  * @file    pmu_to_con_radar360_data
  * @brief   PMU发送360信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_radar360_data(void)
{
    uint8_t index = 0;
    uint32_t send_byte = 0;
    uint16_t crc = 0;

    if(mimo_360_info.connect_status == COMP_NORMAL)
    {
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSGID_360RADAR;
        
        radar360_proflag = 1;

        send_byte = sizeof(mimo_360_data) * fmu_360info.total_tar + 2;
        
        memcpy(&msg_buf[index],&fmu_360info,send_byte);
        index += send_byte;
        radar360_proflag = 0;

        msg_buf[1] = index - 6;

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

        usart1_send_msg(msg_buf, index);
    }
}

/**
  * @file    pmu_to_con_request_data
  * @brief   PMU发送应答信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_ack_data()
{
    int index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 21;

    msg_buf[index++] = ack_id;
    memcpy(&msg_buf[index],&ack_content,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content1,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content2,2);
    index += 2;  
    
    msg_buf[1] = index - 6;

    uint16_t crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    usart1_send_msg(msg_buf, index);
}
//4D雷达回复fmu
void pmu_to_con_DM4DBradar_msg(void)
{
    uint8_t index = 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_GET4D;
    msg_buf[index++] = 'V';
    msg_buf[index++] = 'K';
    msg_buf[index++] = 'Z';
    msg_buf[index++] = '1';
    memcpy(&msg_buf[index],&DM_4DRADARMAG.angel_4DF,18);
    index += 18;
    msg_buf[1] = index - 6;
    uint16_t crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;
    usart1_send_msg(msg_buf, index);
}
//4D仿地雷达分段高度
void pmu_to_con_DM4DTradar_data(void)
{
    uint8_t index = 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_FT4DRADAR;
    DM4dt_recv_flag = 1;
    memcpy(&msg_buf[index], &D4T_tofmu.target_num, 1);
    index += 1;
    memcpy(&msg_buf[index], &D4T_tofmu.time_delay, 2);
    index += 2;
    memcpy(&msg_buf[index], &D4T_tofmu.err_num, 2);
    index += 2;
    memcpy(&msg_buf[index], &D4T_tofmu.RawData, D4T_tofmu.target_num * 2);
    index += D4T_tofmu.target_num * 2;
    msg_buf[1] = index - 6;
    uint16_t crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;

    usart1_send_msg(msg_buf, index);
    DM4dt_recv_flag = 0;
}
void pmu_to_con_DM4DBTradar_data(void)
{
    uint8_t index = 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_BT4DRADAR;
    DM4dbt_recv_flag = 1;
    memcpy(&msg_buf[index], &D4BT_tofmu.target_num, 1);
    index += 1;
    memcpy(&msg_buf[index], &D4BT_tofmu.time_delay, 2);
    index += 2;
    memcpy(&msg_buf[index], &D4BT_tofmu.err_num, 2);
    index += 2;
    memcpy(&msg_buf[index], &D4BT_tofmu.RawData, D4BT_tofmu.target_num * 2);
    index += D4BT_tofmu.target_num * 2;
    msg_buf[1] = index - 6;
    uint16_t crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;

    usart1_send_msg(msg_buf, index);
    DM4dbt_recv_flag = 0;
}
/**
  * @file    Can_send_debug_to_app
  * @brief   can模拟
  * @param   none
  * @details UART2
  * @author  Zhang Sir 
 **/
/*
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;

        usart1_send_msg(msg_buf, index);
        //uart3_send_msg(msg_buf, index);

        if(send_time > 1)
        {
            HAL_Delay(50);
        }
    }

}
    
*/
uint32_t DM4d_to_fmu10s_flag = 0;
/**
  * @file    pmu_to_fcu
  * @brief   PMU发送信息给FMU
  * @param   none
  * @details UART2
  * @author  Zhang Sir 
 **/
uint8_t pmu_send = PMU_SEND_YAOCE;
void pmu_to_fcu()
{
    //串口阻塞 和雷达升级不发送
    if(update_info.vk_dev_update_flag != true && EZup_par.update_flag != true)
    {
        switch (pmu_send)
        {
        case PMU_SEND_YAOCE:
            //心跳包单独发
            if(pmu_heart_flag == true)
            {
                pmu_to_con_heart_data();
                pmu_heart_flag = false;
            }
            //发送电压信息
            else if (vol_flag == true)
            {
                pmu_to_con_voltage_data();
                vol_flag = false;
            }
            // //发动机信息
            // else if (engine_flag == true)
            // {
            //     pmu_to_con_engine_data();
            //     engine_flag = false;
            // }
            // //mimo360测试
            // else if (mimo360_radar_flag == true)
            // {
            //     pmu_to_con_radar360_data();
            //     mimo360_radar_flag = false;
            // }
            else if(devtype_flag == true)
            {
                pmu_to_con_devtype_data();
                devtype_flag = false;
            }
            //CAN调试信息
            // else if(can_debug_flag == true)
            // {
            //     //Can_send_debug_to_app();
            //     can_debug_flag = false;
            // }
            //设备SN号,软硬件号
            else if (dev_version_flag == true)
            {
                pmu_to_fcu_version_data();
                dev_version_flag = false;
            }
            
            break;
        case PMU_SEND_REQINFO:
            pmu_to_con_request_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_ACK:
            pmu_to_con_ack_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_VERSION:
            pmu_to_con_version_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_SHA1:
            pmu_to_fcu_key_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        default:
            pmu_send = PMU_SEND_YAOCE;
            break;
        }
        if (F4DT_send_flag == true)
        {
            pmu_to_con_DM4DTradar_data();
            F4DT_send_flag = false;
        }
        if(F4DB_send_flag == true)
        {   
            pmu_to_con_DM4DBTradar_data();
            F4DB_send_flag = false;
        }
        if(DM4Dmsg_send_fmu == true || ((pmu_to_DM4Dmsg_flag == true && DM4d_to_fmu10s_flag <= 10000) && 
           (Dev.DM_Part_Fradar_Link.connect_status == COMP_NORMAL || Dev.DM_Part_Bradar_Link.connect_status == COMP_NORMAL)))
        {
            pmu_to_con_DM4DBradar_msg();
            pmu_to_DM4Dmsg_flag = false;
            DM4d_to_fmu10s_flag = HAL_GetTick();
            DM4Dmsg_send_fmu = false;
        }
    }
}

/**
  * @file    timer_check_other_func
  * @brief   FMU异常断电检测
  * @param   none
  * @details UART2
  * @author  Zhang Sir 
 **/
void timer_check_other_func()
{
    //static uint32_t time1 = 0;
    static uint32_t time2 = 0;

    // if(Check_Timer_Ready(&time1,_1_HZ_))
    // {
    //     if((planep.lock_status != STA_LOCK) && (fcu.connect_status == COMP_NORMAL) && 
    //         (HAL_GetTick() - fcu.recv_time > 500))
    //     {
    //         stor_par.abnormal_outage_flag = 1;
    //         write_flash_flag = true;
    //         fcu.connect_status = COMP_LOST;
    //     }
    // }

    if(Check_Timer_Ready(&time2,_2_HZ_))
    {
        if(HAL_GetTick() > 10000 && start_msg.version_info == false)
        {
            pmu_send = PMU_SEND_VERSION;
            start_msg.version_info = true;
        }
        else if (start_msg.key_info_checking == true && Device1.Vkbms_Link.connect_status == COMP_NORMAL)
        {
            pmu_send = PMU_SEND_SHA1;
        }
        else
        {
            //给FMU发送雷达灵敏度信息
            send_mocib_radar_sensi();
        }
    }
}





extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
void thread_main_task_handle(void *param)
{
    // if(Check_Chip_Verified != true)
    // {
    //     while (1)
    //     {
    //         rt_thread_mdelay(5);
    //     }
        
    // }

    while(1)
    {
        //数据发送定时器
        timer_function();

        //ADC采集
        ADC_GET_DATA();

        //点亮pmu内部led灯
        pmu_inside_led();

//        /*测试发送rkprintf*/
//        static uint32_t lastPrintTime = 0;
//        if( HAL_GetTick() - lastPrintTime > 1000){
//            rt_kprintf("this is a test!\r\n");
//            lastPrintTime = HAL_GetTick();
//        }

        //发送数据给主控制器 雷达升级不发送数据
        pmu_to_fcu();

        //更新播撒，称重，水泵，离心喷头,流量计等设备信息
        update_device_type_data(); 

        //智能电池相关功能
        bms_function();

        //雷达相关功能
        lidar_function();

        //判断异常断电，上电发送版本信息
        timer_check_other_func();

        //液位计相关功能
        //L1L2_GPIO_check();

        //获取设备版本和SN号
        get_device_version_and_sn();
        //电目雷达
        DM_obs_test();
        //写flash
        write_flash_function();

#ifdef mimo_update
        mimo_obs_update_func();
#else
        //设备升级
        Vk_Update_Device_Protocol();
#endif 

        eft_dev_update_func();
        send_uartfifo_msg();
        //CAN DEBUG
        // if(planep.Candebug_flag == true)
        // {
        //     seek_can_debug_buf_adr();
        // }

        rt_thread_mdelay(2);
    }
}


