#include "usart_data_handle.h"
#include "common.h"
#include "system_init.h"
#include "main.h"
#include "soft_flash.h"
#include "config.h"
#include "string.h"
#include "soft_crc.h"
#include "main_task.h"
#include "soft_radar_handle.h"
#include "soft_device.h"
#include "soft_seed_weight.h"
#include "soft_version.h"
#include "soft_obstacle.h"
#include "soft_terrain.h"
#include "soft_update.h"
#include "soft_adc.h"
#include "soft_flow.h"
#include "soft_can.h"
#include "soft_timer.h"
#include "soft_water.h"

/**
  * @file    check_uart_data
  * @brief   串口数据组包
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
Msg_Rx_Stage recv_step;
Vk_protocol fcu_protocol;
Vk_protocol vk_data = {.head = 0XFE,
						   .system_id = 0,
						   .head_bytes = 6,
						   .check_bytes = 2};
_pmu_pin pmu_pin = {.pump1 = 1000, .pump2 = 1000, .nozz1_fm = 1000,
                    .nozz2_zp = 1000, .nozz3 = 1000, .nozz4 = 1000};
void check_uart_data(void)
{
    uint8_t c = 0;
    static uint32_t vk_recv_time = 0;
    static uint8_t i = 0;

    while (rkfifo_out(&uart_rkfifo, &c, 1) != 0)
    {	
        if(recv_step != RX_HEAD && HAL_GetTick() - vk_recv_time > 200)
        {
            recv_step = RX_HEAD;
        }

        //解析出一包完整的数据
        switch (recv_step)
        {
        case RX_HEAD:
            if(c == vk_data.head)
            {
                vk_recv_time = HAL_GetTick();
                recv_step = RX_PAYLOAD_LEN;
            }
            break;
        case RX_PAYLOAD_LEN:
            vk_data.len = c;
            recv_step = RX_SEQ;
            break;
        case RX_SEQ:
            vk_data.seq = c;
            recv_step = RX_SYSID;
            break;
        case RX_SYSID:
            if(c == vk_data.system_id)
            {
                recv_step = RX_GROUPID;
            }
            else
            {
                recv_step = RX_HEAD;
            }
            break;
        case RX_GROUPID:
            if(c == GROUP_ID_FCU || c == GROUP_ID_PMU_UPDATE || c == GROUP_ID_F_UPDATE ||
            c == GROUP_ID_B_UPDATE || c == GROUP_ID_T_UPDATE|| c == GROUP_ID_WEIGHT_UPDATE ||
            (c >= GROUP_ID_EFTPUMP1_UPDATE && c <= GROUP_IDEND))
            {
                vk_data.group_id = c;
                recv_step = RX_MSGID;
            }
            else
            {
                recv_step = RX_HEAD;
            }
            break;
        case RX_MSGID:
            vk_data.msg_id = c;
            recv_step = RX_PAYLOAD;
            i = 0;
            break;
        case RX_PAYLOAD:
            vk_data.payload[vk_data.head_bytes + i] = c;
            i++;
            if(i == vk_data.len)
            {
                recv_step = RX_CHECK_LOW; 
            }
            else if(vk_data.len == 0) //没有payload
            {
                recv_step = RX_CHECK_HIGH;
            }
            break;
        case RX_CHECK_LOW:
            vk_data.payload[vk_data.head_bytes + i] = c;
            i++;
            recv_step = RX_CHECK_HIGH;
            break;
        case RX_CHECK_HIGH:
            vk_data.payload[vk_data.head_bytes + i] = c;

            vk_data.payload[0] = vk_data.head;
            vk_data.payload[1] = vk_data.len;
            vk_data.payload[2] = vk_data.seq;
            vk_data.payload[3] = vk_data.system_id;
            vk_data.payload[4] = vk_data.group_id;
            vk_data.payload[5] = vk_data.msg_id;
            
            if(0 == Get_Crc16(&vk_data.payload[0],vk_data.len + vk_data.head_bytes + vk_data.check_bytes))
            {
                if(vk_data.group_id == GROUP_ID_FCU || vk_data.group_id == GROUP_ID_PMU_UPDATE)
                {
                    if(fcu_protocol.complete_flag == false)
                    {
                        fcu_protocol.msg_id = vk_data.msg_id;
                        memcpy(&fcu_protocol.payload[0],&vk_data.payload[0],vk_data.len + vk_data.head_bytes + vk_data.check_bytes);
                        fcu_protocol.complete_flag = true;  
                    }
                }
                //莫之比雷达升级
                else if((vk_data.group_id == GROUP_ID_F_UPDATE && uavr11_info.Link.connect_status != COMP_NOEXIST) ||
                        (vk_data.group_id == GROUP_ID_B_UPDATE && uavr12_info.Link.connect_status != COMP_NOEXIST) ||
                        (vk_data.group_id == GROUP_ID_T_UPDATE && uavr56_info.Link.connect_status != COMP_NOEXIST))
                {	 
                    if(update_info.use_update_buf_flag == false)
                    {
                        memcpy(&update_info.buf[0],&vk_data.payload[0],vk_data.len + vk_data.head_bytes + vk_data.check_bytes);
                        update_info.use_update_buf_flag = true;

                        if (vk_data.msg_id == 200)
                        {
                            if(check_radar_update() == true)
                            {
                                radar_update_flag = true;
                                update_count = 0;
                            }
                        }
                    }
                }
                //VK协议设备升级 
                else if (vk_data.group_id == GROUP_ID_WEIGHT_UPDATE ||
                        (vk_data.group_id == GROUP_ID_F_UPDATE /*&& mimo_f_info.Link.connect_status != COMP_NOEXIST*/) ||
                        (vk_data.group_id == GROUP_ID_B_UPDATE /*&& mimo_b_info.Link.connect_status != COMP_NOEXIST*/) ||
                        (vk_data.group_id == GROUP_ID_T_UPDATE /*&& mimo_ter_info.Link.connect_status != COMP_NOEXIST*/))
                {

#ifdef mimo_update
					if(vk_data.msg_id == 200)
					{
						if(EZup_par.step == STEP_DEFAULT)//mimo
						{//mimo
							memset(&EZup_par,0,sizeof(ez_update));//mimo
							memset(&update_info.use_update_buf_flag,0,sizeof(Update));
							memcpy(&update_info.bin_size,&vk_data.payload[6],4);
							update_info.vk_dev_pack_num = 1;
							update_info.vk_dev_update_flag = true;  
							EZup_par.update_flag = true;//mimo

							EZup_par.step = STEP_FIND_NODE;//mimo
						}
					}
#else
					if(vk_data.msg_id == 200)
					{
						memset(&update_info.use_update_buf_flag,0,sizeof(Update));
						memcpy(&update_info.bin_size,&vk_data.payload[6],4);
						update_info.vk_dev_pack_num = 1;
						update_info.vk_dev_update_flag = true;
					}
#endif

                    memcpy(&update_info.buf[0],&vk_data.payload[0],vk_data.len + vk_data.head_bytes + vk_data.check_bytes);
                    //判断是否是需要的包序号
                    if(update_info.vk_dev_pack_num > (update_info.buf[PACK_NUM] + update_info.buf[PACK_NUM + 1] * 256) &&
                        vk_data.msg_id == 201)
                    {
                        Update_ack_func(update_info.buf[DEVICE],201,&update_info.buf[PACK_NUM]);
                    }

                    update_info.use_update_buf_flag = true;
                }
                else if(vk_data.group_id == GROUP_ID_EFTPUMP1_UPDATE || vk_data.group_id == GROUP_ID_EFTPUMP2_UPDATE ||
                        vk_data.group_id == GROUP_ID_EFTNOZZLE1_UPDATE || vk_data.group_id == GROUP_ID_EFTNOZZLE2_UPDATE || 
                        vk_data.group_id == GROUP_ID_EFTNOZZLE3_UPDATE || vk_data.group_id == GROUP_ID_EFTNOZZLE4_UPDATE ||
                        vk_data.group_id == GROUP_ID_EFTFB_UPDATE || vk_data.group_id == GROUP_ID_EFTSEED_UPDATE ||
                        vk_data.group_id == GROUP_ID_Z70FB_UPDATE || vk_data.group_id == GROUP_ID_Z70FBLIFTING_UPDATE || 
                        vk_data.group_id == GROUP_ID_EFT_TURNMOTOR)
                {
                    if(vk_data.msg_id == 200 && eft_update.update_flag == false)
					{
                        memset(&eft_update,0,sizeof(_eft_update));
                        eft_update.dev_id = vk_data.group_id;
                        memcpy(&eft_update.bin_size,&vk_data.payload[6],8);
                        eft_update.step = STEP_START;
                        eft_update.update_flag = true;
                    }
                    memcpy(&eft_update.buf[0],&vk_data.payload[0],vk_data.len + vk_data.head_bytes + vk_data.check_bytes);
                    //判断是否是需要的包序号
                    if(eft_update.pack_num > (eft_update.buf[PACK_NUM] + eft_update.buf[PACK_NUM + 1] * 256) &&
                        vk_data.msg_id == 201)
                    {
                        Update_ack_func(update_info.buf[DEVICE],201,&update_info.buf[PACK_NUM]);
                    }

                }

            }

            recv_step = RX_HEAD;
            break;
        default:
            recv_step = RX_HEAD;

            break;
        }
    }
}


/**
  * @file    thread_usart_task_entry
  * @brief   FMU串口数据解析
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
Connect_check fcu;
Start_info start_msg;
Set_info msgidset;
__attribute__ ( ( aligned ( 4 ) ) ) plane_para planep = {.Candebug_flag = 0};
void thread_usart_task_entry(void *param)
{
    while(1)
    {
        rt_sem_take(&uart1_sem, RT_WAITING_FOREVER);

        check_uart_data();

        if (fcu_protocol.complete_flag == true)
        {  
            uint32_t tem_32t = 0;

            Check_dev_link(&fcu,3000,NULL,0);

            switch (fcu_protocol.msg_id)
            {
            case _MSGID_PWM:
                memcpy(&pmu_pin.pump1, &fcu_protocol.payload[6], sizeof(_pmu_pin));

                if(set_HWesc_ESCid == true && setESCidInfo.setESCidStep == SETESCID_SET)
                {
                    memset(&pmu_pin.pump1,0,8);
                    pmu_pin.pump1 = 1000;
                }
                //自动配置CANID
                if(auto_set_canid_flag == NEED_SET)
                {
                    if((auto_eft_dev_status & 1) != 1)
                        pmu_pin.pump1 = 100;
                    if(((auto_eft_dev_status >> 1) & 1) != 1)    
                        pmu_pin.pump2 = 200;
                    if(((auto_eft_dev_status >> 2) & 1) != 1)   
                        pmu_pin.nozz1_fm = 300;
                    if(((auto_eft_dev_status >> 3) & 1) != 1)   
                        pmu_pin.nozz2_zp = 400;
                    if(((auto_eft_dev_status >> 4) & 1) != 1)   
                        pmu_pin.nozz3 = 200;
                    if(((auto_eft_dev_status >> 5) & 1) != 1)   
                        pmu_pin.nozz4 = 100;
                }

                // if(Dev.Eft_CanDev_Link[0].connect_status == COMP_NORMAL)
                // {
                    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pmu_pin.pump1);
                    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pmu_pin.pump2);
                // }
                // else
                // {
                //     __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pmu_pin.pump1);
                //     __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pmu_pin.pump2);
                // }
                
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pmu_pin.nozz1_fm);
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, pmu_pin.nozz2_zp);
                __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, pmu_pin.nozz3);
                __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, pmu_pin.nozz4);
                
                break;
            case _MSGID_LED:
                // for(uint8_t i = 0; i < 7; i++)
                // {
                //     if(planep.UAV_type != VK_ALL_IN_ONE ) //青岛机型不用LED
                //     {
                //         led_set_blink(i, fcu_protocol.payload[i*3+6], fcu_protocol.payload[i*3+7], fcu_protocol.payload[i*3+8]);
                //     }
                // }
                break;
            //飞机姿态信息
            case _MSGID_ATTITUDE:
                memcpy(&planep.yaw, &fcu_protocol.payload[6], sizeof(plane_para));

                if(cur_par.uavtype != planep.UAV_type)
                {
                    stor_par.uavtype = planep.UAV_type;
                    write_flash_flag = true;
                }
                break;
            case _MSGID_TIME:
                //tem_32t = 1684136124;
                memcpy(&utc_time, &fcu_protocol.payload[6], 4);
                covUnixTimeStp2Beijing(utc_time,&beijing_time);
                pmu_set_ack(_MSGID_TIME,0,0,0);
                break;
            //eft播撒器
            case _MSGID_EFT_CON:
                recv_fmu_seed_info = true;
                memcpy(&spread_par.pwm_turntable, &fcu_protocol.payload[6], 2);
                memcpy(&spread_par.pwm_valve, &fcu_protocol.payload[8], 2);
                break;
            //请求信息包
            case _MSGID_REQ:
                switch (fcu_protocol.payload[6])
                {
                case MSGID_REQ_VERSION:
                    pmu_send = PMU_SEND_VERSION;
                    break;
                default:
                    break;
                }
                break;
            //主控应答
            case _MSGID_ACK:
                msgidset.content1 = fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8];
                switch (fcu_protocol.payload[6])
                {
                case MSGID_ACK_HEART:
                    // stor_par.abnormal_outage_flag = 0;
                    // write_flash_flag = true;
                    break;
                case MSGID_ACK_VERSION:
                    start_msg.version_info = true;
                    break;
                case MSGID_ACK_DEV:
                    {
                        if(msgidset.content1 < DEVICE_END - 1)
                        {
                            dev_version_content *Pt = dev_ptr[msgidset.content1];
                            if(Pt->send_times > 0)
                                Pt->send_times--;
                        }
                    }
                    break;
                default:
                    break;
                }
                break;
            //设置雷达灵敏度
            case _MSGID_SET:
                msgidset.num  = fcu_protocol.payload[6];
                msgidset.content1 = fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8];
                msgidset.content2 = fcu_protocol.payload[9] + 256 * fcu_protocol.payload[10];
                switch (msgidset.num)
                {
                //前雷达
                case MSGID_SET_F_RADAR:
                    if(uavr12_info.fcu_set_sensi_flag != true && uavr56_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                    {
                        uavr11_info.fcu_set_sensi_flag = true;
                    }
                    memcpy(&obsfradar_sensitivity, &fcu_protocol.payload[7], 2);
                    if(obsfradar_sensitivity == uavr11_info.get_radar_sensi)
                    {
                        uavr11_info.fcu_set_sensi_flag = false;
                        pmu_set_ack(_MSGID_SET,MSGID_SET_F_RADAR,uavr11_info.get_radar_sensi,0);
                    }
                    break;
                //后雷达
                case MSGID_SET_B_RADAR:
                    if(uavr11_info.fcu_set_sensi_flag != true && uavr56_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                    {
                        uavr12_info.fcu_set_sensi_flag = true;
                    }
                    memcpy(&obsbradar_sensitivity, &fcu_protocol.payload[7], 2);
                    if(obsbradar_sensitivity == uavr12_info.get_radar_sensi)
                    {
                        uavr12_info.fcu_set_sensi_flag = false;
                        pmu_set_ack(_MSGID_SET,MSGID_SET_B_RADAR,uavr12_info.get_radar_sensi,0);
                    }
                    break;
                //仿地
                case MSGID_SET_T_RADAR:
                    if(uavr11_info.fcu_set_sensi_flag != true && uavr12_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                    {
                        uavr56_info.fcu_set_sensi_flag = true;
                    }
                    memcpy(&uavr56_info.fcu_set_sensi, &fcu_protocol.payload[7], 2);
                    if(uavr56_info.fcu_set_sensi == uavr56_info.get_radar_sensi)
                    {
                        uavr56_info.fcu_set_sensi_flag = false;
                        pmu_set_ack(_MSGID_SET,MSGID_SET_T_RADAR,uavr56_info.get_radar_sensi,0);
                    }
                    break;
                case MSGID_SET_VOL:
                    stor_par.voltage = (fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8]) - (int)(Adc_Get(ADC_Bms)/100.0f) ;
                    write_flash_flag = true;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_VOL,0,0);
                    break;
                case MSGID_SET_MIMO_FLOW:
                    if(msgidset.content1 != 0)
                    {
                        if(Dev.Flow.facid == FAC_MIMO_SIG || Dev.Flow.facid == FAC_MIMO_DOU)
                        {
                            tem_32t = flow_mimo1.flow_k * msgidset.content1 / 100;
                            
                            if(tem_32t >= 500 && tem_32t <= 20000)
                            {
                                flow_mimo1.flow_calk = tem_32t;
                            }
                            else{
                                flow_mimo1.flow_calk = 10000;
                            }
                            flow_mimo1.set_k = true;
                        }
                        else if(Dev.Flow.facid == FAC_QIFEI_SIG || Dev.Flow.facid == FAC_QIFEI_DOU)
                        {
                            tem_32t = flow_inf.ch1.k * msgidset.content1 / 100;
                            
                            if(tem_32t >= 500 && tem_32t <= 20000)
                            {
                                flow_inf.ch1.cal_k = tem_32t;
                            }
                            else{
                                flow_inf.ch1.cal_k = 10000;
                            }
                            flow_inf.ch1.set_k = true;
                        }
                    }

                    if(msgidset.content2 != 0)
                    {
                        if(Dev.Flow.facid == FAC_MIMO_SIG || Dev.Flow.facid == FAC_MIMO_DOU)
                        {
                            tem_32t = flow_mimo2.flow_k * msgidset.content2 / 100;

                            if(tem_32t >= 500 && tem_32t <= 20000)
                            {
                                flow_mimo2.flow_calk = tem_32t;
                            }
                            else{
                                flow_mimo2.flow_calk = 10000;
                            }
                            flow_mimo2.set_k = true;
                        }
                        else if(Dev.Flow.facid == FAC_QIFEI_SIG || Dev.Flow.facid == FAC_QIFEI_DOU)
                        {
                            tem_32t = flow_inf.ch2.k * msgidset.content2 / 100;
                            
                            if(tem_32t >= 500 && tem_32t <= 20000)
                            {
                                flow_inf.ch2.cal_k = tem_32t;
                            }
                            else{
                                flow_inf.ch2.cal_k = 10000;
                            }
                            flow_inf.ch1.set_k = true;;
                        }
                    }
                    break;
                case MSGID_SET_FLOW_BACKGROUND:
                    if(Dev.Flow.facid == FAC_MIMO_SIG)
                    {
                        flow_inf.ch1.clear_background = true;
                    }
                    if(Dev.Flow.facid == FAC_MIMO_DOU)
                    {
                        flow_inf.ch1.clear_background = true;
                        flow_inf.ch2.clear_background = true;
                    }
                    break;
                case MSGID_SET_WEIGHT_K:
                    weight_order.type = Weight_Set_K;
                    weight_order.order_con1 = msgidset.content1;
                    weight_order.order_con2 = msgidset.content2;
                    break;
                case MSGID_SET_SEED_OUT_TYPE:
                    recv_fmu_seed_info = true;
                    spread_par.output_mode = msgidset.content1;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_SEED_OUT_TYPE,0,0);
                    break;
                case MSGID_SET_SEED_CAL_TYPE:
                    switch (msgidset.content1)
                    {
                    case Cal_Remove_Peel:
                        weight_order.type = Weight_Peer; 
                        break;
                    case Cal_Weight:
                        weight_order.type = Weight_Kg;
                        weight_order.order_con2 =  msgidset.content2;
                        break;
                    case Cal_Seed_Back:
                        weight_order.type = Weight_Bcak;
                        break;
                    default:
                        break;
                    }
                    break;
                case MSGID_SET_WEIGHT_MODE:
                    weight_order.type = Weight_Mode;
                    weight_order.order_con1 = msgidset.content1;
                    break;
                case MSGID_SET_MAX_RATE:
                    weight_order.type = Weight_Drug_Rate;
                    weight_order.order_con1 = msgidset.content1;
                    break;    
                case MSGID_SET_PMU_SERIAL:
                    tem_32t = msgidset.content1 + ((msgidset.content2 << 16) & 0xffff0000);
                    if(cur_par.pmu_serial == PMU_SERIAL || tem_32t == PMU_SERIAL)
                    {
                        stor_par.pmu_serial = tem_32t;
                        write_flash_flag = true;
                        pmu_set_ack(_MSGID_SET,MSGID_SET_PMU_SERIAL,0,0);
                        dev_pmu.send_times += 5;
                    }
                    else if(tem_32t == cur_par.pmu_serial)
                    {
                        pmu_set_ack(_MSGID_SET,MSGID_SET_PMU_SERIAL,0,0);
                    }     
                    break;
                case MSGID_SET_RADAR_FB:
                    {
                    uint8_t can_buf[8] = {0};
                    if(msgidset.content1 == 0x11)
                    {   
                        put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X03,0X01,0X65,0X00);
                        Can_Send_Msg_Func(CANID1, can_buf, 7, 0xFA, CAN_ID_STD);
                    }
                    else if (msgidset.content1 == 0x12)
                    {
                        put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X03,0X02,0X66,0X00);
                        Can_Send_Msg_Func(CANID1, can_buf, 7, 0xFA, CAN_ID_STD);
                    }
                    else if(msgidset.content1 == 0x100)
                    {
                        put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X00,0X03,0X64,0X00);
                        Can_Send_Msg_Func(CANID1, can_buf, 7, 0xFA, CAN_ID_STD);
                    }
                    }
                    break;
                case MSGID_SET_LACKLOSS_CAL:
                    {
                    uint8_t can_buf[8] = {0};
                    if(Dev.Lackloss_Link.connect_status == COMP_NORMAL)
                    {
                        put_date_to_can(can_buf,0xFA,0x03,0x00,0xB2,0XE1,0x00,0X00,0X00);
                        can_buf[7] = (can_buf[1]+can_buf[2]+can_buf[3]+can_buf[4]+can_buf[5]+can_buf[6]) & 0xff;
                        Can_Send_Msg_Func(CANID2, can_buf, 8, 0xFA, CAN_ID_STD);
                    }
                    }
                    break;
                case MSGID_SET_PNCANID:
                    set_eftdev_canid_flag = true;
                    set_eftdev_canid_time = HAL_GetTick();
                    pmu_set_ack(_MSGID_SET,1,0,0);
                    break;
                case MSGID_SET_RESIWIRE_BLOWN:
                    {
                    uint8_t can_buf[8] = {0};
                    put_date_to_can(can_buf,0xF7,fcu_protocol.payload[7],0x00,0x00,0x00,0xF1,0XF3,0X00);
                    Can_Send_Msg_Func(CANID2, can_buf, 8, 0x88BB, CAN_ID_EXT);
                    }
                    break;
                case MSGID_SET_CHURN_RESET:
                    {
                        uint8_t can_buf[8] = {0};
                        put_date_to_can(can_buf,0xE6,0x00,0x00,0x00,0X00,0xF1,0XF3,0X00);
                        Can_Send_Msg_Func(CANID2, can_buf, 8, 0x7011, CAN_ID_EXT);
                    }
                    break;
                /*case MSGID_SET_CHURN_SIZE:
                    {
                        uint8_t can_buf[8] = {0};
                        uint8_t size = 0;
                        size = (uint8_t)msgidset.content1;
                        put_date_to_can(can_buf,0xE7,size,0x00,0x00,0X00,0xF1,0XF3,0X00);
                        Can_Send_Msg_Func(CANID2, can_buf, 8, 0x7011, CAN_ID_EXT);
                    }
                    break;*/
                case MSGID_SET_4DFRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XA81300, CAN_ID_EXT);
                    DM_f_info.get_radar_ver_flag = false;
                    dev_obsf.regist.sn = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_4DFRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
            case MSGID_SET_4DBRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XB81300, CAN_ID_EXT);
                    dev_obsb.regist.sn = false;
                    DM_4DB_info.get_radar_ver_flag = false;
                    //DM_b_info.get_radar_ver_flag = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_4DBRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
            case MSGID_SET_4DTRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0X981300, CAN_ID_EXT);
                    DM_ter_info.get_radar_ver_flag = false;
                    dev_ter.regist.sn = false; //重新获取sn
                    pmu_set_ack(_MSGID_SET,MSGID_SET_4DTRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
                default:
                    break;
                }
                break;
            case _MSGID_SHA1:
                //智能电池秘钥
                if(fcu_protocol.payload[6] == 1)
                {
                    //有秘钥
                    if(fcu_protocol.payload[7] == 2 && fcu_protocol.payload[8] == 1)
                    {
                        if(start_msg.key_info_checking == true)
                        {
                            memcpy(&start_msg.key_data[0],&fcu_protocol.payload[9],20);
                            start_msg.key_to_bms = true;//给电池发送秘钥
                            start_msg.key_info_checking = false;
                        }
                    }
                    //无秘钥
                    else if(fcu_protocol.payload[7] == 2 && fcu_protocol.payload[8] == 2)
                    {
                        start_msg.key_info_checking = false;
                    }
                    //回馈，防止PMU一直发
                    else if(fcu_protocol.payload[7] == 3 || fcu_protocol.payload[7] == 4)
                    {
                        start_msg.key_info_checking = false;
                    }
                }
                break;
            case _MSGID_SET4D:
            {
                uint16_t radar_id = 0;
                uint8_t can_buf[8] = {0};
                int radar_msg = 0;
                uint8_t size = 0;
                radar_id = fcu_protocol.payload[6] + fcu_protocol.payload[7] * 256;
                memcpy(&radar_msg, &fcu_protocol.payload[8], 4);
                switch (radar_id)
                {
                    case 1:
                    can_buf[0] = 0XC;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XA81300, CAN_ID_EXT);
                    break;
                case 2:
                    can_buf[0] = 0XE;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XA81300, CAN_ID_EXT);
                    break;
                case 3:
                    can_buf[0] = 0XA;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XA81300, CAN_ID_EXT);
                    break;
                case 4:
                    can_buf[0] = 0XC;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XB81300, CAN_ID_EXT);
                    break;
                case 5:
                    can_buf[0] = 0XE;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XB81300, CAN_ID_EXT);
                    break;
                case 6:
                    can_buf[0] = 0XA;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XB81300, CAN_ID_EXT);
                    break;
                case 7:
                    can_buf[0] = 0X5;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XA81300, CAN_ID_EXT);
                    break;
                case 8:
                    can_buf[0] = 0X5;
                    memcpy(&can_buf[1],&radar_msg,2);
                    can_buf[7] = 7;
                    Can_Send_Msg_Func(CANID1, can_buf, 8, 0XB81300, CAN_ID_EXT);
                    break;
                case 9:
                {
                    size = (uint8_t)radar_msg;
                    put_date_to_can(can_buf, 0xE7, size, 0x00, 0x00, 0X00, 0xF1, 0XF3, 0X00);
                    Can_Send_Msg_Func(CANID2, can_buf, 8, 0x7011, CAN_ID_EXT);
                }
                break;
                default:
                    break;
                }
            }
            break;
            case _MSGID_HEART:
                pmu_heart_flag = true;
                //pmu_set_ack(_MSGID_HEART,0x56,0,0);
                break;
            //升级固件标志
            case _MSGID_UPDATA:
            {
                __disable_irq();
                HAL_FLASH_Unlock();

                __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | 
                          FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

                uint32_t sector = 10;
                sector = GetSectorFromAddress(FLASH_UPDATE_ADDR);//获取地址所在的扇区

                FLASH_Erase_Sector(sector,FLASH_VOLTAGE_RANGE_3);//擦除指定的闪存扇区(0~11)
                
                uint16_t TempBuf = 0xABCD;
                HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_UPDATE_ADDR, TempBuf);

                HAL_FLASH_Lock();
                __enable_irq();

                HAL_NVIC_DisableIRQ(USART1_IRQn);
                HAL_Delay(100);

                //软件复位
                HAL_NVIC_SystemReset();
            }
                break;
            default:
                break;
            }
            fcu_protocol.complete_flag = false;
            memset(fcu_protocol.payload, 0, MAX_UART_BUF);
        }
        
    }
}


