#include "soft_update.h"
#include "soft_obstacle.h"
#include "soft_terrain.h"
#include "string.h"
#include "soft_usart.h"
#include "soft_device.h"
#include "soft_can.h"
#include "main_task.h"
#include "soft_crc.h"
#include "usart_data_handle.h"
#include "config.h"

bool radar_update_flag = false;
Radar Rupdate;
/**
  * @file    can_recv_mocib_updata_read_set_hookfunction
  * @brief   莫之比协议升级
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
bool uavrhup_getr1_ack = false;
void can_recv_mocib_updata_read_set_hookfunction(uint32_t cellCanID, uint8_t data[])
{
    //AG代码 和雷达升级不兼容,优先升级
    if (Rupdate.update_flag == true)
    {
        //避障雷达升级
        if (Rupdate.buf_flag == false)
        {
            memcpy(Rupdate.update_buf, data, 8);
            Rupdate.buf_flag = true;

            switch (cellCanID)
            {
            case 0x7E1:
                Rupdate.U7E1 = true;
                break;
            case 0x7E3:
                Rupdate.U7E3 = true;
                break;
            case 0x7E6:
                Rupdate.U7E6 = true;
                break;
            default:
                break;
            }
        }
    }
    else
    {
        switch (cellCanID)
        {
        //莫之比雷达反馈版本信息
        case 0X7E1:
            uavrhup_getr1_ack = true;
            break;
        //莫之比雷达设置灵敏度及反馈
        case CAN_UAVRH_SENSI_SA:
            if (data[0] == 0x11)
            {
                uavr11_info.set_radar_sensi_ack = true;
                //莫之比大端模式
                uavr11_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
            else if (data[0] == 0x12)
            {
                uavr12_info.set_radar_sensi_ack = true;
                //莫之比大端模式
                uavr12_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
            else if(data[0] == 0x0B)
            {
                uavr56_info.set_radar_sensi_ack = true;
                uavr56_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
            break;
        //莫之比雷达读取灵敏度及反馈
        case CAN_UAVRH_SENSI_RA:
            if (data[0] == 0x11)
            {
                uavr11_info.get_radar_sensi_flag = true;
                //莫之比大端模式
                uavr11_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
            else if (data[0] == 0x12)
            {
                uavr12_info.get_radar_sensi_flag = true; //莫之比大端模式
                uavr12_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
            else if(data[0] == 0x0B)
            {
                uavr56_info.get_radar_sensi_flag = true;
                uavr56_info.get_radar_sensi = ((data[1] << 8) & 0xff00) + data[2];
            }
        default:
            break;
        }
    }
}


/**
  * @file    Vk_Update_Device_Protocol
  * @brief   VK协议设备升级
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
uint8_t Factory_DevId[3] = {0};
Update update_info; 
void Vk_Update_Device_Protocol(void)
{
    static uint32_t send_msgtime_2HZ = 0;

    if(uart_send_is_ok(USART_1) == true)
    {
        if(update_info.step_200_flag == true )
        {
            Update_ack_func(update_info.buf[DEVICE],200,&update_info.buf[PACK_NUM]);
            update_info.step_200_flag = false;
        }
        else if(update_info.step_201_flag == true)
        {
            Update_ack_func(update_info.buf[DEVICE],201,&update_info.buf[PACK_NUM]);
            update_info.step_201_flag = false;
        }
        else if(update_info.step_202_flag == true)
        {
            Update_ack_func(update_info.buf[DEVICE],202,&update_info.buf[PACK_NUM]);
            update_info.step_202_flag = false;
        }
    }

    if(update_info.vk_dev_update_flag == false)
        return;
    if(HAL_GetTick() - send_msgtime_2HZ < 100)
        return;

    send_msgtime_2HZ = HAL_GetTick();

    uint16_t crc = 0;
    uint8_t  can_buf[8] = {0};
    uint8_t i = 0;
    uint8_t dev_id = 0;

    //FMUPMU协议ID号10进制，设备升级协议16进制
    if(update_info.buf[DEVICE] == 56)
        dev_id = 0x56;
    else if(update_info.buf[DEVICE] == 51)
        dev_id = 0x11;
    else if(update_info.buf[DEVICE] == 52)
        dev_id = 0x12;
    else
        dev_id = update_info.buf[DEVICE];

    switch (update_info.buf[UPDATE_STEP])
    {
    case UPDATE_START:
        can_buf[0] = dev_id;
        memcpy(&can_buf[1],&update_info.buf[PAYLOAD - 2],4);

        switch (can_buf[0])
        {
        case UPDATE_OBS_F:
            if(Dev.Radar.facid_F == FAC_MIMO_RF || Dev.Part_radarF.facid == FAC_MIMO_RF)
            {
                memcpy(&can_buf[5],"TR0",3);
            }
            break;
        case UPDATE_OBS_B:
            if(Dev.Radar.facid_B == FAC_MIMO_RB || Dev.Part_radarB.facid == FAC_MIMO_RB)
            {
                memcpy(&can_buf[5],"TR0",3);
            }
            break;
        case UPDATE_TERAIN:
            if(Dev.Radar.facid_T == FAC_MIMO_RT || Dev.Part_radarT.facid == FAC_MIMO_RT)
            {
                memcpy(&can_buf[5],"TR0",3);
            }
            break;
        case UPDATE_OBS360:
            break;
        case UPDATE_SEED:
            break;
        case UPDATE_FLOW:
            break;
        case UPDATE_BMS:
            break;
        case UPDATE_DISTOR:
            break;
        case UPDATE_WEIGHT:
            if(Dev.Weight.facid == FAC_VK)
            {
                memcpy(&can_buf[5],"VK1",3);
            }
            break;
        default:
            break;
        }

        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x381400, CAN_ID_EXT);
        break;

    case UPDATE_ING:
        while (i < (update_info.buf[LEN] - 2 + 8) && update_info.vk_dev_pack_num == (update_info.buf[PACK_NUM] + update_info.buf[PACK_NUM + 1] * 256))
        {
            if(i == 0)
            {
                crc = Get_Crc16( &update_info.buf[PAYLOAD],update_info.buf[LEN] - 2);
                can_buf[0] = 0xFE;
                can_buf[1] = 0xFE;
                can_buf[2] = dev_id;
                can_buf[3] = update_info.buf[LEN] - 2;
                can_buf[4] = update_info.buf[PACK_NUM];
                can_buf[5] = update_info.buf[PACK_NUM + 1];
                can_buf[6] = crc & 0xff; 
                can_buf[7] = (crc >> 8) & 0xff;
            }
            else
            {
                memcpy(&can_buf[0],&update_info.buf[i],8);
            }
            i += 8;
            Can_Send_Msg_Func(CANID1, can_buf, 8, 0x401400, CAN_ID_EXT);
            memset(&can_buf[0],0,8);
        }
        break;
    
    case UPDATE_END:
        can_buf[0] = dev_id;

        Can_Send_Msg_Func(CANID1, can_buf, 8, 0x481400, CAN_ID_EXT);   

        update_info.vk_dev_update_flag = false;

        break;
    default:
        break;
    }
}

void Update_Dev_Bootversion_Function(uint8_t data[])
{
    if(data[4] != 0x01)
        return;

    pmu_send = PMU_SEND_VERSION;
    switch (data[0])
    {
        case UPDATE_OBS_F:
            if(memcmp((char *)&data[1],"TR0",3) == 0)
            {
                mimo_f_info.Link.connect_status = COMP_NORMAL;
                mimo_f_info.Link.recv_time = HAL_GetTick();
                mimo_f_info.Link.boot_flag = true;
                Dev.Radar.facid_F = FAC_MIMO_RF;
            }
            break;
        case UPDATE_OBS_B:
            if(memcmp((char *)&data[1],"TR0",3) == 0)
            {
                mimo_b_info.Link.connect_status = COMP_NORMAL;
                mimo_b_info.Link.recv_time = HAL_GetTick();
                mimo_b_info.Link.boot_flag = true;
                Dev.Radar.facid_B = FAC_MIMO_RB;
            }
            break;
        case UPDATE_TERAIN:
            if(memcmp((char *)&data[1],"TR0",3) == 0)
            {
                mimo_ter_info.Link.connect_status = COMP_NORMAL;
                mimo_ter_info.Link.recv_time = HAL_GetTick();
                mimo_ter_info.Link.boot_flag = true;
                Dev.Radar.facid_T = FAC_MIMO_RT;
            }
            break;
        case UPDATE_OBS360:
            break;
        case UPDATE_SEED:
            break;
        case UPDATE_FLOW:
            break;
        case UPDATE_BMS:
            break;
        case UPDATE_DISTOR:
            break;
        case UPDATE_WEIGHT:
            if(memcmp((char *)&data[1],"VK1",3) == 0)
            {
                
            }
            break;
        default:
            break;
    }
}


/**
  * @file    Can_obstacle_update
  * @brief   莫之比雷达CAN口在线升级函数 
  * @param   0=发送升级请求    1=接收请求升级状态  2=发送格式化指令
  *           3=接收格式化状态 4=向雷达发送升级包 5=发送校验和  6=接收雷达固件更新状态
  * @details 
  * @author  Zhang Sir 
 **/
uint32_t update_count = 0;
void Can_obstacle_update()
{

  static uint32_t update_i = 0;
  static uint32_t check_sum = 0;
  static uint16_t pack_count = 1;
  uint16_t size_count = 0;
  uint32_t for_i = 0;
  uint8_t obstacle_buf[8] = {0};

  switch (update_count)
  {
  case 0:
    //向雷达发送请求升级
    if (update_info.buf[4] == 51)
    {
      obstacle_buf[0] = 0x11; // 前雷达
    }
    else if (update_info.buf[4] == 52)
    {
      obstacle_buf[0] = 0x12; //后雷达
    }
    else if (update_info.buf[4] == 56)
    {
      obstacle_buf[0] = 0x0B;
    }

    HAL_Delay(100);
    Can_Send_Msg_Func(CANID1, obstacle_buf, 1, 0x7E0, CAN_ID_EXT);

    update_count = 1;
    
    break;

  case 1:
    
    if (Rupdate.buf_flag == true && Rupdate.U7E1 == true)
    {
      if (Rupdate.update_buf[1] == 0x00) //radar_update_buf[0] 0x11 0x12
      {
        //雷达进入刷写模式
        update_count = 2;
      }
      else if (Rupdate.update_buf[1] == 0x01)
      {                            //退出刷写模式
        radar_update_flag = false; //关闭升级
        update_count = 0;

      }
      Rupdate.U7E1 = false;
      Rupdate.buf_flag = false;
      memset(Rupdate.update_buf,0,8);
    }
    break;

  case 2:
    memset(obstacle_buf, 0, 8);

    obstacle_buf[0] = 0x01;
    obstacle_buf[1] = 0x09;
    obstacle_buf[2] = 0x09;
    obstacle_buf[3] = 0x04;
    obstacle_buf[4] = 0x00;
    obstacle_buf[5] = 0x09;
    obstacle_buf[6] = 0x02;
    obstacle_buf[7] = 0x06;

    HAL_Delay(100);
    Can_Send_Msg_Func(CANID1, obstacle_buf, 8, 0x7E2, CAN_ID_EXT);

    update_count = 3;
    break;

  case 3:
    if (Rupdate.buf_flag == true && Rupdate.U7E3 == true)
    {
      if (Rupdate.update_buf[0] == 0xA6)
      { //格式化成功
        update_count = 4;
        update_i = 16;
        Update_ack_func(update_info.buf[4], 200, &update_info.buf[6]);
        update_info.use_update_buf_flag = false;
        //防止连续升级
        check_sum = 0;
      }
      else if (Rupdate.update_buf[0] == 0xA8)
      { //格式化失败
        radar_update_flag = false;
        Rupdate.update_flag = false;
        update_count = 0;
      }
      Rupdate.U7E3 = false;
      Rupdate.buf_flag = false;
    }
    break;

  case 4:
    if (update_info.use_update_buf_flag == true) 
    {
      if (update_info.buf[5] == 201)
      {
        memcpy(&size_count, &update_info.buf[6], 2);
        if (pack_count == size_count)
        { //防止发送相同包

          // 第一包前8字节不要 第二包开始发送全部128字节
          Can_Send_Msg_Func(CANID1, &update_info.buf[update_i], 8, 0x7E4, CAN_ID_EXT);

          for (for_i = 0; for_i < 8; for_i++) // 计算校验
          {
            check_sum += update_info.buf[update_i + for_i];
          }
        
          update_i += 8;

          if (update_i >= 136)
          {
            update_i = 8;
            pack_count++;
            Update_ack_func(update_info.buf[4], 201, &update_info.buf[6]);
            memset(&update_info.buf[0], 0, 150);
            update_info.use_update_buf_flag = false;
          }
        }
        else if(pack_count > size_count)
        {
            Update_ack_func(update_info.buf[4], 201, &update_info.buf[6]);
            update_info.use_update_buf_flag = false;
        }
      }
      else if (update_info.buf[5] == 202)
      {
        //串口结束标志
        Update_ack_func(update_info.buf[4], 202, &update_info.buf[6]);
        update_count = 5;
        update_info.use_update_buf_flag = false;
      }
      else if (update_info.buf[5] == 200)
      {
        Update_ack_func(update_info.buf[4], 200, &update_info.buf[6]);
        update_info.use_update_buf_flag = false;
      }
      else
      {
        update_info.use_update_buf_flag = false;
      }
    }
    break;

  case 5:
    memset(obstacle_buf, 0, 8);

    obstacle_buf[0] = (check_sum >> 24) & 0xff;
    obstacle_buf[1] = (check_sum >> 16) & 0xff;
    obstacle_buf[2] = (check_sum >> 8) & 0xff;
    obstacle_buf[3] = (check_sum)&0xff;

    Can_Send_Msg_Func(CANID1, obstacle_buf, 4, 0x7E5, CAN_ID_EXT);
    update_count = 6;

    break;

  case 6:
    if (Rupdate.buf_flag == true && Rupdate.U7E6 == true)
    {
      if (Rupdate.update_buf[0] == 0x66)
      {
        update_count = 0; //更新成功
      }
      else if (Rupdate.update_buf[0] == 0x88)
      {
        update_count = 0; //校验失败
      }
      else if (Rupdate.update_buf[0] == 0xff)
      {
        update_count = 0; //表示固件超出最大存储空间， bootloader将重新格式化扇区，并将版本回退到出厂固件??
      }
      pack_count = 1;
      Rupdate.buf_flag = false;
      radar_update_flag = false;
      Rupdate.update_flag = false;
      Rupdate.U7E6 = false;

      //重新发送版本信息
      uavr11_info.get_radar_ver_flag = false;
      uavr12_info.get_radar_ver_flag = false;
      uavr56_info.get_radar_ver_flag = false;
    }
    break;
  default:
    break;
  }

}


/**
  * @file    mimo_obs_update_func
  * @brief   EZ雷达升级
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
uint8_t radar_node_id = 0;
ez_update EZup_par;
void mimo_obs_update_func(void)
{
    static uint32_t send_msgtime_5HZ = 0;

    if(uart_send_is_ok(USART_1) == true)
    {
        if(update_info.step_200_flag == true )
        {
            Update_ack_func(update_info.buf[DEVICE],200,&update_info.buf[PACK_NUM]);
            update_info.step_200_flag = false;
        }
        else if(update_info.step_201_flag == true)
        {
            Update_ack_func(update_info.buf[DEVICE],201,&update_info.buf[PACK_NUM]);
            update_info.step_201_flag = false;
        }
        else if(update_info.step_202_flag == true)
        {
            Update_ack_func(update_info.buf[DEVICE],202,&update_info.buf[PACK_NUM]);
            update_info.step_202_flag = false;
        }

    }

    if(EZup_par.update_flag != true)
        return;
    if(Check_Timer_Ready(&send_msgtime_5HZ,_20_HZ_) != true)
         return;
    
    uint8_t ez_can[8] = {0};
    static uint8_t frame_i = 1;
    static uint16_t data_count = 0,ez_crc = 0;
    
    if(UPDATE_END == update_info.buf[UPDATE_STEP])
    {
        EZup_par.step = STEP_ENDING;
        update_info.step_202_flag = true;
    }

    switch (EZup_par.step)
    {
    case STEP_FIND_NODE:
        for(uint8_t i = 0;i < 126;i++)
        {
            put_date_to_can(ez_can,i,0x06,0xB2,0X80,0XFF,0XFF,0XFF,0XFF);
            Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);
        }

        break;
    case STEP_JUMP_BOOT:
        put_date_to_can(ez_can,EZup_par.node_id,0x06,0xB3,0XC1,0X42,0X4F,0X4F,0X54);
        Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);

        put_date_to_can(ez_can,EZup_par.node_id,0x06,0xB2,0X80,0XFF,0XFF,0XFF,0XFF); //确定雷达所在区域
        Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);

        break;
    case STEP_EASER_FLASH:
        HAL_Delay(500);
        put_date_to_can(ez_can,EZup_par.node_id,0x06,0xB4,0X42,0X00,0X00,0X00,0X00);
        ez_can[4] = (update_info.bin_size >> 24) & 0xff;
        ez_can[5] = (update_info.bin_size >> 16) & 0xff;
        ez_can[6] = (update_info.bin_size >> 8) & 0xff;
        ez_can[7] = update_info.bin_size & 0xff;
        Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);
        break;
    case STEP_SET_OFFSET_ADR:
        put_date_to_can(ez_can,EZup_par.node_id,0x06,0xB4,0X03,0X00,0X01,0X17,0X3C);
        ez_can[4] = (EZup_par.adr_offset>> 24) & 0xff;
        ez_can[5] = (EZup_par.adr_offset >> 16) & 0xff;
        ez_can[6] = (EZup_par.adr_offset >> 8) & 0xff;
        ez_can[7] = EZup_par.adr_offset & 0xff;

        Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);
        break;

    case STEP_SEND_DATE:
        while (update_info.vk_dev_pack_num == (update_info.buf[PACK_NUM] + update_info.buf[PACK_NUM + 1] * 256) &&
            EZup_par.pack_content != PACK_WAIT)
        {
            switch (EZup_par.pack_content)
            {
            case PACK_HEAD:
                EZup_par.frame_pci = 0x10 + (((128 + 2) >> 8) & 0x7);
                EZup_par.frame_len = (128+2) & 0xff;
                put_date_to_can(ez_can,EZup_par.node_id,EZup_par.frame_pci,EZup_par.frame_len,0XB4,0XC4,0X00,0X00,0X00);
                memcpy(&ez_can[5],&update_info.buf[PAYLOAD],3);
                EZup_par.pack_content = PACK_CONT;
                data_count += 3;
                break;

            case PACK_CONT:
                EZup_par.frame_pci = 0x20 + frame_i;
                ez_can[0] = EZup_par.node_id;
                ez_can[1] = EZup_par.frame_pci;
                frame_i++;

                memcpy(&ez_can[2],&update_info.buf[PAYLOAD + data_count],6);
                
                data_count += 6;
                if(data_count >= 128)
                {
                    EZup_par.pack_content = PACK_CRC;
                    ez_can[7] = 0xff;
                }
                if(frame_i > 15)
                    frame_i = 0;
                break;  

            case PACK_TAIL:
                
                EZup_par.pack_content = PACK_CRC;
                break;

            case PACK_CRC:
                data_count = 0;
                frame_i = 1;
                ez_crc = crc16_ccitt(&update_info.buf[PAYLOAD],128);
                put_date_to_can(ez_can,EZup_par.node_id,0X06,0xB4,0X85,(ez_crc >> 8) & 0xff,(ez_crc & 0xff),0XFF,0XFF);
                EZup_par.pack_content = PACK_WAIT; //只发一次
                break;

            default:
                break;
            }
            Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);
            HAL_Delay(1);
        }
        break;
    case STEP_ENDING:
        put_date_to_can(ez_can,EZup_par.node_id,0x06,0xB4,0X06,0X41,0X50,0X50,0XFF);
        Can_Send_Msg_Func(CANID1, ez_can, 8, 0x3c, CAN_ID_STD);
        EZup_par.step = STEP_DEFAULT;
        EZup_par.update_flag = false;
        mimo_f_info.get_radar_ver_flag = false;
        mimo_b_info.get_radar_ver_flag = false;
        mimo_ter_info.get_radar_ver_flag = false;
        break;
    default:
        break;
    }
}

void EZ_Radar_UpdateCanRecvHookFunction(uint32_t CanID, uint8_t data[], uint8_t len)
{
    if(data[1] == 0x06 && data[2] == 0xF2 && data[3] == 0xAA) //搜索节点
    {
        EZup_par.node_id = data[0];
        EZup_par.step = STEP_JUMP_BOOT;
    }
    else if(data[1] == 0x06 && data[2] == 0xF2 && data[3] == 0x55) //确定在boot
    {
        EZup_par.node_id = data[0];
        EZup_par.step = STEP_EASER_FLASH;
    }
    else if(data[0] == EZup_par.node_id && data[1] == 0x06 && data[2] == 0xF4 && //擦FLASH  //CRC校验回复一样？
        data[3] == 0x00 && data[4] == 0xFF && data[5] == 0xFF && data[6] == 0xFF && data[7] == 0xFF)
    {
        if(EZup_par.step == STEP_EASER_FLASH )
        {
            EZup_par.step = STEP_SET_OFFSET_ADR;
            update_info.step_200_flag = true;
        }
        else if(EZup_par.step == STEP_SEND_DATE || EZup_par.step == STEP_WAIT)
        {
            EZup_par.adr_offset += 128;
            update_info.vk_dev_pack_num++;
            EZup_par.step = STEP_SET_OFFSET_ADR;
            update_info.step_201_flag = true;
        }
    }
    else if(data[0] == EZup_par.node_id && data[1] == 0x06 && data[2] == 0xF4 &&
        data[3] == 0x00 && (data[4] == 0x04 || data[4] == 0x08) && data[5] == 0x00 && data[6] == 0xFF && data[7] == 0xFF) //设置地址偏移    
    {
        EZup_par.step = STEP_SEND_DATE;
        EZup_par.pack_content = PACK_HEAD;
    }
    else if(data[0] == EZup_par.node_id && data[1] == 0x06 && data[2] == 0xF4 && //校验错误
        data[3] == 0x07 && data[4] == 0xFF && data[5] == 0xFF && data[6] == 0xFF && data[7] == 0xFF)
    {
        EZup_par.step = STEP_SET_OFFSET_ADR;
    }

}



uint8_t WordXorCrc(uint8_t *pData, uint32_t len) 
{ 
    uint8_t crc = 0; 
    for (uint32_t i = 0; i < len; i++) 
    { 
        crc ^= pData[i]; 
    } 
    return crc;
}

/**
  * @file    eft_dev_update_func
  * @brief   efg设备升级
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
_eft_update eft_update = {.pack_num = 1};
void eft_dev_update_func(void)
{
    if(uart_send_is_ok(USART_1) == true)
    {
        if(eft_update.step_200_flag == true )
        {
            Update_ack_func(eft_update.buf[DEVICE],200,&eft_update.buf[PACK_NUM]);
            eft_update.step_200_flag = false;
        }
        else if(eft_update.step_201_flag == true)
        {
            Update_ack_func(eft_update.buf[DEVICE],201,&eft_update.buf[PACK_NUM]);
            eft_update.step_201_flag = false;
        }
        else if(eft_update.step_202_flag == true)
        {
            Update_ack_func(eft_update.buf[DEVICE],202,&eft_update.buf[PACK_NUM]);
            eft_update.step_202_flag = false;
        }
    }

    static uint32_t send_msgtime_5HZ = 0;

    if(eft_update.update_flag != true)
        return;
    if(Check_Timer_Ready(&send_msgtime_5HZ,_20_HZ_) != true)
         return;

    uint8_t canN = 0;
    uint8_t can_buf[8] = {0};
    int can_id = 0;

    if(eft_update.dev_id == GROUP_ID_EFTFB_UPDATE)
    {
        canN = CANID1;
    }
    else if(eft_update.dev_id == GROUP_ID_EFTPUMP1_UPDATE || eft_update.dev_id == GROUP_ID_EFTPUMP2_UPDATE ||
         eft_update.dev_id == GROUP_ID_EFTNOZZLE1_UPDATE || eft_update.dev_id == GROUP_ID_EFTNOZZLE2_UPDATE ||
         eft_update.dev_id == GROUP_ID_EFTNOZZLE3_UPDATE || eft_update.dev_id == GROUP_ID_EFTNOZZLE4_UPDATE ||
        eft_update.dev_id == GROUP_ID_EFTSEED_UPDATE || eft_update.dev_id == GROUP_ID_Z70FB_UPDATE)
    {
        canN = CANID2;
    }

    if(eft_update.dev_id == GROUP_ID_EFTPUMP1_UPDATE)
        can_id = 0x8811;
    else if(eft_update.dev_id == GROUP_ID_EFTPUMP2_UPDATE)
        can_id = 0x8822;
    else if(eft_update.dev_id == GROUP_ID_EFTNOZZLE1_UPDATE)
        can_id = 0x8833;
    else if(eft_update.dev_id == GROUP_ID_EFTNOZZLE2_UPDATE)
        can_id = 0x8844;
    else if(eft_update.dev_id == GROUP_ID_EFTNOZZLE3_UPDATE)
        can_id = 0x8855;
    else if(eft_update.dev_id == GROUP_ID_EFTNOZZLE4_UPDATE)
        can_id = 0x8866;
    else if(eft_update.dev_id == GROUP_ID_EFTFB_UPDATE)
        can_id = 0x8877;
    else if(eft_update.dev_id == GROUP_ID_EFTSEED_UPDATE)
        can_id = 0x8899;
    else if(eft_update.dev_id == GROUP_ID_Z70FB_UPDATE)
        can_id = 0x7011;
    else if(eft_update.dev_id == GROUP_ID_Z70FBLIFTING_UPDATE)
        can_id = 0x88BB;


    switch (eft_update.step)
    {
    case STEP_START:
        put_date_to_can(can_buf,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF); //停止广播
        Can_Send_Msg_Func(canN, can_buf, 8, 0x88FF, CAN_ID_EXT);

        put_date_to_can(can_buf,0xEB,0x90,0xEB,0x90,0xEB,0x90,0x10,0x06);
        Can_Send_Msg_Func(canN,can_buf, 8, can_id, CAN_ID_EXT);
        memcpy(&can_buf,&eft_update.bin_size,4);
        memcpy(&can_buf[4],&eft_update.bin_crc,4);
        Can_Send_Msg_Func(canN,can_buf, 8, can_id, CAN_ID_EXT);

        if(eft_update.bin_size % 128 > 0)
            eft_update.totol_packNum = (eft_update.bin_size / 128) + 1;
        else if(eft_update.bin_size % 128 == 0)
            eft_update.totol_packNum = eft_update.bin_size / 128;
        break;
    case STEP_UPDATING:
        if(eft_update.pack_num == (eft_update.buf[PACK_NUM] + eft_update.buf[PACK_NUM + 1] * 256) && 
           eft_update.send_pack_complete == false && eft_update.buf[UPDATE_STEP] == 201)
        {
            uint8_t i = 0;
            //首帧
            put_date_to_can(can_buf,0xEB,0x90,0xEB,0x90,0xEB,0x90,128+10,0x2B);
            Can_Send_Msg_Func(canN, can_buf, 8, can_id, CAN_ID_EXT);

            //第二帧
            if(eft_update.pack_num != eft_update.totol_packNum)
                can_buf[0] = 1;
            else
                can_buf[0] = 0;
            memcpy(&can_buf[1],&eft_update.buf[PAYLOAD + i],7);
            i += 7;
            Can_Send_Msg_Func(canN, can_buf, 8, can_id, CAN_ID_EXT);

            //中间帧
            while( i < 127)
            {
                memcpy(can_buf,&eft_update.buf[PAYLOAD + i],8);
                Can_Send_Msg_Func(canN, can_buf, 8, can_id, CAN_ID_EXT);
                i += 8;
            }
            //结束 帧
            eft_update.pack_crc = WordXorCrc(&eft_update.buf[PAYLOAD],128);
            can_buf[0] = eft_update.buf[PAYLOAD + i];
            can_buf[1] = eft_update.pack_crc;
            can_buf[2] = 0xFF;
            can_buf[3] = 0xFF;
            can_buf[4] = 0xFF;
            can_buf[5] = 0xFF;
            can_buf[6] = 0xFF;
            can_buf[7] = 0xFF;
            Can_Send_Msg_Func(canN, can_buf, 8, can_id, CAN_ID_EXT);

            eft_update.send_pack_complete = true;
        }

        break;
    case STEP_END:
        put_date_to_can(can_buf,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE); //开始广播
        Can_Send_Msg_Func(canN, can_buf, 8, 0x88FF, CAN_ID_EXT);
        eft_update.update_flag = false;
        eft_update.step_202_flag = true;
        break;
    case STEP_WAIT_ACK:

        break;
    default:
        break;
    }
}


void eft_dev_update_wait_ereseflash(uint8_t data[])
{
    if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x07)
    {
        eft_update.step = STEP_WAIT_ACK;
    }
    else if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x17)
    {
        eft_update.step_200_flag = true;
        if(eft_update.step != STEP_UPDATING)
        {
            eft_update.step = STEP_UPDATING;
            eft_update.pack_num++;
        }
    }
    //下一包数据
    else if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x0B)
    {
        eft_update.pack_num++;
        eft_update.send_pack_complete = false;
        eft_update.step_201_flag = true;
    }
    //重新发送此包
    else if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x09)
    {
        eft_update.send_pack_complete = false;
    }
    //升级成功
     else if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x0C)
    {
        eft_update.step = STEP_END;
    }
    //重新升级
    else if(data[0] == 0xEB && data[1] == 0x90 && data[2] == 0xEB && data[3] == 0x90 &&
        data[4] == 0xEB && data[5] == 0x90 && data[6] == 0x08 && data[7] == 0x0D)
    {

    }
}