#include "soft_engine.h"
#include "soft_crc.h"
#include "crc.h"
#include "string.h"
#include "soft_p_2_c.h"
#include "soft_test.h"
#include "soft_uart.h"
#include "math.h"

struct ENGINE_DATA engine_data;


//串口接收奥安发动机数据
AOAN_TYPE aoan_data = {0, .engin_warn = 0xffff};
Connect_check aoan_engine_Link;
//解析奥安油电混传感器数据
short decode_aoan_engine(uint8_t *pdata, uint16_t length)
{

    short rst = 0;

    if (pdata[0] == 0x5A && pdata[1] == 0xA5 && pdata[2] == 0x1A && pdata[3] == 0x02)
    {
        unsigned char dNum = pdata[2];

        //校验通过。
        if (Get_Crc16(pdata, dNum + 5) == 0)
        {
            rst = 1;

            aoan_engine_Link.connect_status = COMP_NORMAL;

            //赋值数据
            memcpy(&aoan_data, &pdata[3], dNum);

            aoan_engine_Link.recv_time = HAL_GetTick();

        }
    }

    return rst;
}

// //串口接收VKV3发动机数据
VKV3_TYPE vkv3_data = {0, .engin_warn = 0xffff};
// VKV3_TYPE vkv3_data2 = {0, .engin_warn = 0xffff};
Connect_check vkv3_engine_link;
// Connect_check vkv3_engine2_link;

//解析V3协议油电混传感器数据
short  decode_vkv3_engine(uint8_t *pdata, uint16_t length)
{
    short rst = 0;
    uint32_t engine_sn_temp = 0;
    static uint32_t engine_sn1 = 0;
    VKV3_TYPE *ptr = NULL;


    if (pdata[0] == 0x5A && pdata[1] == 0xA5 && pdata[2] == 0X27 && pdata[4] == 0X03)
    {
        unsigned char dLEN = pdata[2];
        unsigned char comID = pdata[3];

        //校验通过。
        if (Get_Crc16(pdata, dLEN + 6) == 0)
        {
            memcpy(&engine_sn_temp, &pdata[12], 4);
            rst = 1;

            if(engine_sn1 == 0 || engine_sn_temp == engine_sn1)
            {
                ptr = &vkv3_data;
                vkv3_engine_link.connect_status = COMP_NORMAL;
                vkv3_engine_link.recv_time = HAL_GetTick();

                if(engine_sn1 == 0)
                    engine_sn1 = engine_sn_temp;
            }
            else
            {
                // ptr = &vkv3_data2;
                // vkv3_engine2_link.connect_status = COMP_NORMAL;
                // vkv3_engine2_link.recv_time = HAL_GetTick();
            }

            switch (comID)
            {
            case 0X01:
                //赋值数据
                memcpy(&ptr->engin_ver, &pdata[4], dLEN);
                break;
            default:
                break;
            }
            
        }
    }

    return rst;
}







bool geely_set_tcd = false,geely_set_tcd_re = false, 
     geely_set_tlock = false, geely_set_tlock_re = false, geely_set_protect = false,
     geely_send_BJtime = false,geely_get_version = false,geely_get_time = false;
GEELY2 geely_data2;
//解析吉利发动机
GEELY geely_data = {0};
GEELY_MONI geely_moni = {0};


Connect_check geely_engin_link;
int geely_version2_count = 0;
void EngGeelyCanRecvHookFunction(uint32_t cellCanID, uint8_t data[], uint8_t len)
{
    if(cellCanID == 0x100) //第二版协议识别
    {
        geely_engin_link.connect_status = COMP_NORMAL;
        geely_engin_link.recv_time = HAL_GetTick();
    }
    else if (cellCanID == 0x1c0)
    {
        memcpy(&geely_data.engine_ver, &data[0], len);
    }
    else if (cellCanID == 0x1c1)
    {
        //木牛防地雷达会异常进入发动机识别
        geely_engin_link.connect_status = COMP_NORMAL;
        
        memcpy(&geely_data.engine_rev, &data[0], len);
    }
    else if (cellCanID == 0x1c2)
    {
        memcpy(&geely_data.engine_vol, &data[0], len);
    }
    else if (cellCanID == 0x1c3)
    {
        uint8_t checkSum = 0;
        for (uint8_t i = 0; i < 8; i++)
        {
            checkSum += data[i];
        }

        geely_moni.recv_1c3++;

        if (checkSum == 0)
        {
            memcpy(&geely_data.engine_control, &data[0], len);

            if (geely_moni.msg1c3_last + 1 != geely_data.engine_counts)
            {
                geely_moni.msg1c3_indexerr++;
            }
            geely_moni.msg1c3_last = geely_data.engine_counts;
            
            
            if(geely_data.engine_set == 0x11)
            {
                //接收保养时间反馈
                if(geely_set_tcd == true)
                {
                    geely_set_tcd = false;
                    geely_set_counts = 0;
                    
                    geely_set_tcd_re = true;
                }
            }
            if(geely_data.engine_set == 0x12)
            {
                //接收保养时间复位反馈
                if(geely_set_tcd_re == true)
                {
                    geely_set_tcd_re = false;
                    geely_set_counts = 0;
                }
            }
            if(geely_data.engine_set == 0x14)
            {
                //接收锁机时间反馈
                if(geely_set_tlock == true)
                {
                    geely_set_tlock = false;
                    geely_set_counts = 0;
                    
                    geely_set_tlock_re = true;
                }
            }
            if(geely_data.engine_set == 0x15)
            {
                //接收锁机时间复位反馈
                if(geely_set_tlock_re == true)
                {
                    geely_set_tlock_re = false;
                    geely_set_counts = 0;
                }
            }
            if(geely_data.engine_set == 0x17)
            {

            }
        }
        else
        {
            geely_moni.msg1c3_sumerr++;
        }
    }
    else if (cellCanID == 0x1c4)
    {
        memcpy(&geely_data.engine_runtime, &data[0], len);
    }
    else
    {
        if(cellCanID >= 0x1E1 && cellCanID <= 0x1EF)
        {
            geely_engin_link.connect_status = COMP_NORMAL;
            geely_version2_count++;
            if(geely_data2.version2_flag != true && (geely_version2_count > 10))
            {
             geely_data2.version2_flag = true;
            } //作为第二版协议标志
        }
        //第二版协议
        switch (cellCanID)
        {
        case 0x031:
            geely_send_BJtime = true;
            break;
        case 0x1E1:
            
            break;
        case 0x1E2:
            geely_get_version = true;
            geely_count = 10;
            memcpy(&geely_data2.engine_ver,&data[0],8);

            break;
        case 0x1E3:
            geely_get_time = true;
            //geely_count = 10;
            memcpy(&geely_data2.engine_runtime,&data[0],8);
            break;
        case 0x1E4:
            memcpy(&geely_data2.Bit1E4,&data[0],6);
            break;
        case 0x1E8:
            memcpy(&geely_data2.engine_time,&data[0],8);
            geely_data2.engine_time = ((data[4] >> 6) & 0x3);
            break;
        case 0x1E9:
            memcpy(&geely_data2.engine_rev,&data[0],8);
            break;
        case 0x1EA:
            memcpy(&geely_data2.Bit1EA,&data[0],8);
            break;
        case 0x1EB:
            memcpy(&geely_data2.FuelConsumption,&data[0],8);
            break;
        case 0x1EC:
            memcpy(&geely_data2.EmgST0,&data[0],2);
            break;
        case 0x1ED:
            memcpy(&geely_data2.AlmST0,&data[0],8);
            break;
        case 0x1EE:
            memcpy(&geely_data2.Gear1Position,&data[0],7);
            break;    
        
        default:
            break;
        }
    }

    geely_engin_link.recv_time = HAL_GetTick();
}

short geely_set_counts = 0;
#pragma pack(1)
typedef struct
{
    uint32_t time;
    uint8_t nodecode:4;
    uint8_t message_conut0:4;
    int8_t check_sum0;

    uint8_t client_cmd;
    uint8_t syscontrol;
    uint8_t reserve0;
    uint8_t reserve1;
    uint32_t reserve2:20;
    uint8_t message_conut1:4;
    int8_t check_sum1;
}geely_info2;
geely_info2 geely_sendinfo2;
#pragma pack()

uint8_t geely_count = 15;
uint8_t start_engine = 0; // 0x80启动 0停止
void Geely_version2_init(void)
{ 
    uint8_t can_buf[8] = {0};
    static uint8_t geely_init_count = 15;
    if(geely_data2.version2_flag != true && geely_init_count > 1)
    {
        geely_sendinfo2.client_cmd = 0x12;
        geely_sendinfo2.syscontrol = 0;
        geely_sendinfo2.check_sum1 = geely_sendinfo2.client_cmd + geely_sendinfo2.syscontrol + (geely_sendinfo2.message_conut1 << 4);
        geely_sendinfo2.check_sum1 = 0 - geely_sendinfo2.check_sum1;
        memcpy(&can_buf[0],&geely_sendinfo2.client_cmd,8);
        geely_sendinfo2.message_conut1++;

        can_send_msg_normalstd(&can_buf[0],8,0x1E0);

        geely_init_count--;


    }
}

void Geely_version2_send_info( void )
{
    uint8_t can_buf[8] = {0};
    //发送北京时间
    // if(geely_send_BJtime == false && (beijing_time.ui8Year != 0)) //上点发送，CANID不在过滤器中 不等反馈
    // {   
    //     geely_sendinfo2.time += beijing_time.ui8Second / 2;
    //     geely_sendinfo2.time += beijing_time.ui8Minute *( 0x1 << 4);
    //     geely_sendinfo2.time += beijing_time.ui8Hour *( 0x1 << 10);
    //     geely_sendinfo2.time += beijing_time.ui8DayOfMonth *( 0x1 << 15);
    //     geely_sendinfo2.time += beijing_time.ui8Month *( 0x1 << 20);
    //     geely_sendinfo2.time += beijing_time.ui8Year *( 0x1 << 24);

    //     geely_sendinfo2.nodecode = 0;
    //     geely_sendinfo2.check_sum0 = geely_sendinfo2.time + geely_sendinfo2.nodecode + (geely_sendinfo2.message_conut0 << 4);
    //     geely_sendinfo2.check_sum0 = 0 - geely_sendinfo2.check_sum0;
    //     memcpy(&can_buf[0],&geely_sendinfo2,6);
    //     geely_sendinfo2.message_conut0++;

    //     can_send_msg_normalstd(&can_buf[0],6,0x030);
    // }
    //上电读取信息
    if(geely_get_version == false || geely_get_time == false)
    {
        if(geely_get_version == false)
        {
            geely_sendinfo2.client_cmd = 0x16;
        }
        else if(geely_get_time == false)
        {
            geely_sendinfo2.client_cmd = 0x17;
        } 

        geely_sendinfo2.syscontrol = 0;
        geely_sendinfo2.check_sum1 = geely_sendinfo2.client_cmd + geely_sendinfo2.syscontrol + (geely_sendinfo2.message_conut1 << 4);
        geely_sendinfo2.check_sum1 = 0 - geely_sendinfo2.check_sum1;
        memcpy(&can_buf[0],&geely_sendinfo2.client_cmd,8);
        geely_sendinfo2.message_conut1++;

        can_send_msg_normalstd(&can_buf[0],8,0x1E0);

        geely_count--;

        if(geely_count == 0)
        {
            geely_count = 10;
            if(geely_get_version == false)
            {
                geely_get_version = true;
            }
            else if(geely_get_time == false)
            {
                geely_get_time = true;
            }
        }
    }
    //控制发动机启停
    else 
    {
        geely_sendinfo2.client_cmd = 0x12;
        geely_sendinfo2.syscontrol = start_engine;
        geely_sendinfo2.check_sum1 = geely_sendinfo2.client_cmd + geely_sendinfo2.syscontrol + (geely_sendinfo2.message_conut1 << 4) ;
        geely_sendinfo2.check_sum1 = 0 - geely_sendinfo2.check_sum1;
        memcpy(&can_buf[0],&geely_sendinfo2.client_cmd,8);
        geely_sendinfo2.message_conut1++;

        can_send_msg_normalstd(&can_buf[0],8,0x1E0);
    }
    // else
    // {
    //     geely_sendinfo2.client_cmd = 0x11;
    //     geely_sendinfo2.syscontrol = 0;
    //     geely_sendinfo2.check_sum1 = geely_sendinfo2.client_cmd + geely_sendinfo2.syscontrol + (geely_sendinfo2.message_conut1 << 4);
    //     geely_sendinfo2.check_sum1 = 0 - geely_sendinfo2.check_sum1;
    //     memcpy(&can_buf[0],&geely_sendinfo2.client_cmd,8);
    //     geely_sendinfo2.message_conut1++;
    //     can_send_msg_normalstd(&can_buf[0],8,0x1E0);
    // }
}

#define SENDTO_GEELY_STATUS_ID  (0X1A0)

STO_GEELY sendto_geely = {.ExtenderControl = 0,
                          .ExtenderSetting = 0x10,
                          .SettingParameter = 0,
                          .ExtenderBak = 0,
                          .MessageCount = 0
                         };



uint8_t vkv3_buf[20] = {0};
void send_msg_to_vk3(void)
{
    
    if(vkv3_engine_link.connect_status == COMP_NORMAL &&
        uart_send_is_ok(USART_3) == true)
    {
        
        uint16_t crc = 0;
        uint8_t index = 0;

        vkv3_buf[index++] = 0x5A;
        vkv3_buf[index++] = 0xA5;
        vkv3_buf[index++] = 4;
        vkv3_buf[index++] = 0x0A;

        if(start_engine == 0x80)
        {
            vkv3_buf[index++] = 1;
        }
        else
        {
            vkv3_buf[index++] = 0;
        }
        vkv3_buf[index++] = planep.lock_status;
        vkv3_buf[index++] = planep.thr_pwm;
        vkv3_buf[index++] = planep.thr_pwm >> 8;

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

        uart3_send_msg(vkv3_buf,index);
    }
    
}

void send_msg_to_geely(void)
{
    static uint32_t sendto_geely_lasttime = 0;
    static uint32_t sendto_bro_lasttime = 0;

    if (geely_engin_link.connect_status == COMP_NORMAL && engine_data.engine_type == ENGINE_BRO && HAL_GetTick() - sendto_bro_lasttime > 222)
    {
        uint8_t can_buf[8] = {0};
        short temp = 0;

        sendto_bro_lasttime = HAL_GetTick();

        memcpy(&can_buf[0],&planep.alt,2);

        temp = sqrtf(planep.E_vel * planep.E_vel + planep.N_vel * planep.N_vel);
        memcpy(&can_buf[2],&temp,2);

        memcpy(&can_buf[4],&planep.yaw,2);
        memcpy(&can_buf[6],&planep.lock_status,2);

        can_send_msg_normalstd(&can_buf[0],8,0x1F0);

        memcpy(&can_buf[0],&planep.pos_x1,4);
        memcpy(&can_buf[4],&planep.pos_y1,4);
        can_send_msg_normalstd(&can_buf[0],8,0x1F1);

    }
    if (geely_engin_link.connect_status == COMP_NORMAL && HAL_GetTick() - sendto_geely_lasttime > 100)
    {
        uint8_t geely[8] = {0};

        sendto_geely_lasttime = HAL_GetTick();

        Geely_version2_init();
        if(geely_data2.version2_flag == true)
        {
            Geely_version2_send_info(); 
        }
        else
        {
            sendto_geely.ExtenderBak = planep.thr_pwm;
            sendto_geely.DroneStatus = (planep.lock_status == 4? 1: 0); //(thr_lock_status == LOCKED ? 0 : 1);
            sendto_geely.MessageCount++;

            if(geely_set_tcd == true)
            {
                geely_set_counts++;
                if(geely_set_counts > 5){
                    geely_set_tcd = false;
                    geely_set_counts = 0;
                }
                
                sendto_geely.ExtenderSetting = 0x11;
                //sendto_geely.SettingParameter = 0;
            }
            else if(geely_set_tcd_re == true)
            {
                geely_set_counts++;
                if(geely_set_counts > 5){
                    geely_set_tcd_re = false;
                    geely_set_counts = 0;
                }
                
                sendto_geely.ExtenderSetting = 0x12;
                sendto_geely.SettingParameter = 0;
            }
            else if(geely_set_tlock == true)
            {
                geely_set_counts++;
                if(geely_set_counts > 5){
                    geely_set_tlock = false;
                    geely_set_counts = 0;
                }
                
                sendto_geely.ExtenderSetting = 0x14;
                //sendto_geely.SettingParameter = 0;
            }
            else if(geely_set_tlock_re == true)
            {
                geely_set_counts++;
                if(geely_set_counts > 5){
                    geely_set_tlock_re = false;
                    geely_set_counts = 0;
                }
                
                sendto_geely.ExtenderSetting = 0x15;
                sendto_geely.SettingParameter = 0;
            }
            else if(geely_set_protect == true)
            {
                geely_set_counts++;
                if(geely_set_counts > 5){
                    geely_set_protect = false;
                    geely_set_counts = 0;
                }
                sendto_geely.ExtenderSetting = 0x17;
                sendto_geely.SettingParameter = 0;
            }
            else
            {
                sendto_geely.ExtenderSetting = 0x10;
                sendto_geely.SettingParameter = 0;
                
                geely_set_counts = 0;
            }
            
            memcpy(geely, (uint8_t *)&sendto_geely.ExtenderControl, 7);

            sendto_geely.CheckSum = 0;
            for (uint8_t i = 0; i < 7; i++)
            {
                sendto_geely.CheckSum += geely[i];
            }
            sendto_geely.CheckSum = -sendto_geely.CheckSum;

            can_send_msg_normalstd(&sendto_geely.ExtenderControl, sizeof(sendto_geely), SENDTO_GEELY_STATUS_ID);

            geely_moni.send_1a0++;
        }
    }
}




comp_status engine_link_status = COMP_NOEXIST;

/*
发动机类型	    uint8		1-瑞深 2-中飞
发动机转速	    uint16_t		RPM
发动机油门	    uint16_t
发动机电压	    uint16_t	0.1v
充电电流	    uint16_t	0.1A
发动机运行时间	uint32_t	Min	总运行时间不清零
剩余保养时间	uint16_t	Min	保养后清零
剩余锁机时间	uint16_t	Min	锁机倒计时
运行状态 	    uint8_t		0:停止，1:等待，2:运行, 3:锁机
报警信息	    uint16_t		按位来表示可同时容纳16种警报
剩余油量	    uint8_t	%	0%-100%
气缸1温度	    uint16_t	℃	0℃~300℃
气缸2温度	    uint16_t	℃	0℃~300℃
PCB温度	        uint8_t	℃	0℃~255℃
*/


/*
检查智能电池的连接及数据获取,主要解决兼容多厂家设备接入时的选用优先级问题。

更新速度至少要 5hz

缺少放电电流数据
*/
void update_engine_data(void)
{

    //监测中飞发动机
    Check_dev_link(&aoan_engine_Link,5000,(char *)&aoan_data,sizeof(AOAN_TYPE));
    //监测VKV3的发动机
    Check_dev_link(&vkv3_engine_link,5000,(char *)&vkv3_data,sizeof(VKV3_TYPE));
    //监测VKV3 2的发动机
    //Check_dev_link(&vkv3_engine2_link,5000,(char *)&vkv3_data,sizeof(VKV3_TYPE));
    //检测GEELY发动机
    Check_dev_link(&geely_engin_link,5000,(char *)&geely_data,sizeof(GEELY));

    if (aoan_engine_Link.connect_status == COMP_NORMAL)
    {
        engine_link_status = COMP_NORMAL;

        //数字显示,显示9位数
        engine_data.engine_type = ENGINE_AOAN;
        engine_data.engine_rev = aoan_data.engin_rev;
        engine_data.engine_thr = aoan_data.engin_thr;
        engine_data.engine_vol = aoan_data.engin_vol;
        engine_data.engine_elect = aoan_data.engin_elect;
        engine_data.engine_rtime = aoan_data.engin_rtime;
        engine_data.engine_tcd = aoan_data.engin_tcd;
        engine_data.engine_lcd = aoan_data.engin_lcd;
        engine_data.engine_rsta = aoan_data.engin_rsta;
        engine_data.engine_warn = aoan_data.engin_warn;
        engine_data.engine_roil = aoan_data.engin_roil;
        engine_data.engine_cyl1temp = aoan_data.engin_cyl1temp;
        engine_data.engine_cyl2temp = aoan_data.engin_cyl2temp;
        engine_data.engine_pcbtemp = aoan_data.engin_pcbtemp;
        memcpy(engine_data.engine_brand, "AOAN", 4);
    }
    else if (vkv3_engine_link.connect_status == COMP_NORMAL)
    {
        engine_link_status = COMP_NORMAL;

        //数字显示,显示9位数
        engine_data.engine_type = ENGINE_VK;
        engine_data.engine_rev = vkv3_data.engin_rev;
        engine_data.engine_thr = vkv3_data.engin_thr;
        engine_data.engine_vol = vkv3_data.engin_vol;
        engine_data.engine_elect = vkv3_data.engin_elect;
        engine_data.engine_rtime = vkv3_data.engin_rtime;
        engine_data.engine_tcd = vkv3_data.engin_tcd;
        engine_data.engine_lcd = vkv3_data.engin_lcd;
        engine_data.engine_rsta = vkv3_data.engin_rsta;
        engine_data.engine_warn = vkv3_data.engin_warn;
        engine_data.engine_roil = vkv3_data.engin_roil;
        engine_data.engine_cyl1temp = vkv3_data.engin_cyl1temp;
        engine_data.engine_cyl2temp = vkv3_data.engin_cyl2temp;
        engine_data.engine_pcbtemp = vkv3_data.engin_pcbtemp;
        engine_data.engine_sn = vkv3_data.engin_sn;
        memcpy(engine_data.engine_brand, vkv3_data.engin_brand, 8);

        // if(vkv3_engine2_link.connect_status == COMP_NORMAL)
        // {
            
        // }
    }
    else if (geely_engin_link.connect_status == COMP_NORMAL)
    {
        engine_link_status = COMP_NORMAL;

        engine_data.engine_type = ENGINE_GEELY;

        //数字显示,显示9位数
        if(geely_data2.version2_flag != true)
        {
            engine_data.engine_rev = geely_data.engine_rev;
            engine_data.engine_thr = geely_data.engine_thrposition * 0.1f;
            engine_data.engine_vol = geely_data.engine_vol * 2;
            engine_data.engine_elect = (short)(geely_data.engine_elect - 1000) * 2;
            engine_data.engine_rtime = geely_data.engine_runtime;
            engine_data.engine_tcd = geely_data.engine_tcd;
            engine_data.engine_lcd = geely_data.engine_tlock;
            engine_data.engine_rsta = geely_data.engine_status;
            engine_data.engine_warn = geely_data.engine_warn & 0xffef;//屏蔽总线系统异常
            engine_data.engine_roil = geely_data.engine_fuelposition * 0.5f;
            engine_data.engine_cyl1temp = geely_data.engine_cyltemp - 40;
            engine_data.engine_cyl2temp = 0;
            engine_data.engine_pcbtemp = geely_data.engine_cooltemp - 40;
            memcpy(engine_data.engine_brand, geely_data.engine_brand, 3);
            memcpy(&engine_data.engine_brand[3],"-", 1);
            snprintf((char *)&engine_data.engine_brand[4],4,"%d",(int)geely_data.engine_ver);
            engine_data.engine_sn = geely_data.engine_sn[0] * 10000000 + geely_data.engine_sn[1] * 100000 +
                                    geely_data.engine_sn[2] * 1000 + geely_data.engine_sn[3];
            engine_data.engine_lockstatus = geely_data.engine_control;
            engine_data.engine_fault = geely_data.engine_fault;
        }
        else
        {
            if(geely_data2.engine_brand[0] == 'B' && geely_data2.engine_brand[1] =='R' && geely_data2.engine_brand[2] =='O')
            {   
                engine_data.engine_type = ENGINE_BRO;
            }
            engine_data.engine_rev = geely_data2.engine_rev;
            engine_data.engine_thr = geely_data2.Bit1EA.engine_ATP1 * 0.05f;
            engine_data.engine_vol = geely_data2.engine_vol * 0.1f;
            engine_data.engine_elect = (short)(geely_data2.engine_elect * 0.05f - 400) * 10;
            engine_data.engine_rtime = geely_data2.engine_runtime;
            engine_data.engine_tcd = geely_data2.engine_tcd;
            engine_data.engine_lcd = geely_data2.engine_tlock;
            engine_data.engine_rsta = geely_data2.engine_state;
            engine_data.engine_warn = geely_data2.EmgST0;
            engine_data.engine_roil = geely_data2.GPS * 0.5f;
            engine_data.engine_cyl1temp = geely_data2.engine_cooltemp - 40; 
            engine_data.engine_cyl2temp = geely_data2. engine_cooltemp1 - 40;
            engine_data.engine_pcbtemp = geely_data2.engine_GWT - 40;
            memcpy(engine_data.engine_brand, geely_data2.engine_brand, 3);
            memcpy(&engine_data.engine_brand[3],"-", 1);
            snprintf((char *)&engine_data.engine_brand[4],4,"%d",(int)geely_data2.engine_ver);
            engine_data.engine_sn = geely_data2.engine_sn[0] * 10000000 + geely_data2.engine_sn[1] * 100000 +
                                    geely_data2.engine_sn[2] * 1000 + geely_data2.engine_sn[3];
            //engine_data.engine_lockstatus = geely_data2.engine_state == 0? 0 : 1;
            engine_data.engine_lockstatus = geely_data2.engine_controlcmd;
            //engine_data.engine_fault = geely_data2.engine_fault;
        }
    }
    else
    {
        if (engine_link_status == COMP_NORMAL)
        {
            engine_link_status = COMP_LOST;

            memset(&engine_data, 0, sizeof(engine_data));
            //状态未置异常
            engine_data.engine_type = 0xFF;
        }
    }
}


/******************void engine_function(void)********************
 * ****************发动机检测函数*********************************
 * **************************************************************/
void engine_function(void)
{
    static uint32_t engine_time = 0;

    if(Check_Timer_Ready(&engine_time,_10_HZ_))
	{
		update_engine_data();

        //发送飞控状态给发动机
        send_msg_to_vk3();
	}

    //给吉利发动机发送信息
    send_msg_to_geely();
}
