#include "wmhcomm.h"
#include "wmhcommdata.h"
#include "TYW_stdint.h"
#include "string.h"
#include "Check_Ctrl.h"
#include "Display_Info.h"

uint8_t WMHCommFrameCheckSumFunc(uint32_t ID, uint8_t DLC, uint8_t ProtocolVersion, uint8_t *data)
{
    uint32_t ret = 0;
    uint8_t i = 0;
    ret += ID;
    ret += ProtocolVersion;
    ret += DLC + 1;
    for (i = 0; i < DLC; i++) {
        ret += data[i];
    }
    return (uint8_t)(ret & 0xFF);
}

/*******************************************************************************************
 * Recv Message
*******************************************************************************************/
extern const WMH_COMM_INIT_FRAME_T WMHCommRecvInitList[EM_WMH_COMM_RECV_MSG_TOTAL];
COMM_DATA_UNION_WI_BOX_0x80 WMHCommDataWiBox_0x80;
COMM_DATA_UNION_WI_BOX_0x90 WMHCommDataWiBox_0x90;
COMM_DATA_UNION_WI_BOX_0x91 WMHCommDataWiBox_0x91;
COMM_DATA_UNION_WI_BOX_0x92 WMHCommDataWiBox_0x92;

void WMHCommFrameCallBack_WiBox_0x80(void)
{
    uint8_t tmpCond = 0;
}

void WMHCommFrameCallBack_WiBox_0x90(void)
{
    uint8_t tmpCond = 0;
}
void WMHCommFrameCallBack_WiBox_0x91(void)
{
    uint8_t tmpCond = 0;
}

void WMHCommFrameTimeoutFunc_WiBox(void)
{
    // 替代第一个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X90; i++) {
        WMHCommDataWiBox_0x90.Bytes[i] = 0;
    }
    
    // 替代第二个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X91; i++) {
        WMHCommDataWiBox_0x91.Bytes[i] = 0;
    }
    
    // 替代第三个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X92; i++) {
        WMHCommDataWiBox_0x92.Bytes[i] = 0;
    }
}

void WMHCommFrameTimeoutFunc_WiBox80(void)
{
    // 替代第一个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X80; i++) {
        WMHCommDataWiBox_0x80.Bytes[i] = 0;
    }
}

/*******************************************************************************************
 * Send Message
*******************************************************************************************/
COMM_DATA_UNION_ICM WMHCommDataIcm;
uint32_t ODODisplay = 0;
uint8_t check_step;

void WMHCommFrameCallBack_Icm(void)
{
    ODODisplay = (WMHCommDataWiBox_0x80.bits.Mileage_H << 16 | WMHCommDataWiBox_0x80.bits.Mileage_M << 8 | WMHCommDataWiBox_0x80.bits.Mileage_L);

    WMHCommDataWiBox_0x92.bits.Inquire = 0x11;      //电量
    if(odoclear == 0)
    {
        switch(MENU_CHECK_STEP)
        {
            case 0:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x00;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x00;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x00;          //00

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x00;     //GPS信号

            WMHCommDataWiBox_0x91.bits.Hour = 0x00;
            WMHCommDataWiBox_0x91.bits.Minute = 0x00;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x00;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 1:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x00;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x00;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x00;          //11

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x01;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x02;     //GPS两格

            WMHCommDataWiBox_0x91.bits.Hour = 0x01;
            WMHCommDataWiBox_0x91.bits.Minute = 0x0B;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x00;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 2:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x01;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x00;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0xAB;          //22

            WMHCommDataWiBox_0x90.bits.EstimateMileage_L = 1000;          //P挡状态

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x03;     //GPS三格

            WMHCommDataWiBox_0x91.bits.Hour = 0x02;
            WMHCommDataWiBox_0x91.bits.Minute = 0x16;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x00;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x01;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 3:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x0B;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x01;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x01;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x56;          //22

            WMHCommDataWiBox_0x90.bits.EstimateMileage_L = 2000;          //P挡状态

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x01;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x04;     //GPS四格

            WMHCommDataWiBox_0x91.bits.Hour = 0x03;
            WMHCommDataWiBox_0x91.bits.Minute = 0x21;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x01;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 4:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x15;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x00;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x02;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x01;          //22

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x01;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x04;     //GPS四格

            WMHCommDataWiBox_0x91.bits.Hour = 0x04;
            WMHCommDataWiBox_0x91.bits.Minute = 0x2C;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x02;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 5:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x1F;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x02;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0xAC;          //22

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x00;     //GPS

            WMHCommDataWiBox_0x91.bits.Hour = 0x05;
            WMHCommDataWiBox_0x91.bits.Minute = 0x37;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x03;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x01;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x01;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 6:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x33;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x03;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x57;          //22

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x00;     //GPS

            WMHCommDataWiBox_0x91.bits.Hour = 0x06;
            WMHCommDataWiBox_0x91.bits.Minute = 0x06;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x04;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x01;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x01;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x00;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x00;          //蓝牙指示灯
            break;

            case 7:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x47;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x01;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x05;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x56;          //22

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x00;     //GPS

            WMHCommDataWiBox_0x91.bits.Hour = 0x11;
            WMHCommDataWiBox_0x91.bits.Minute = 0x07;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x05;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x00;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x00;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x00;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x00;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x00;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x01;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x01;          //蓝牙指示灯
            break;

            case 8:
            WMHCommDataWiBox_0x90.bits.Electricity = 0x64;      //电量

            WMHCommDataWiBox_0x90.bits.TCSopen = 0x00;
            WMHCommDataWiBox_0x90.bits.TCSa = 0x00;     //TCS指示灯

            WMHCommDataWiBox_0x90.bits.Espeed_H = 0x06;
            WMHCommDataWiBox_0x90.bits.Espeed_L = 0x02;          //22

            WMHCommDataWiBox_0x90.bits.GearPStatus = 0x00;          //P挡状态

            WMHCommDataWiBox_0x90.bits.CartStatus = 0x00;          //推车状态
            WMHCommDataWiBox_0x90.bits.GEAR = 0x00;          //挡位

            WMHCommDataWiBox_0x91.bits.SignalStrengthOfGps = 0x04;     //GPS

            WMHCommDataWiBox_0x91.bits.Hour = 0x17;
            WMHCommDataWiBox_0x91.bits.Minute = 0x3B;
            WMHCommDataWiBox_0x91.bits.Second = 0x00;     //时间

            WMHCommDataWiBox_0x91.bits.SignalStrengthOf4G = 0x05;     //4G

            WMHCommDataWiBox_0x90.bits.SideBraceStatus = 0x01;          //侧支架
            WMHCommDataWiBox_0x90.bits.FaultStatus = 0x01;          //故障灯
            WMHCommDataWiBox_0x90.bits.ConstantSpeedStatus = 0x01;          //定速巡航灯
            WMHCommDataWiBox_0x90.bits.EBSStatus = 0x01;          //能量回收灯
            WMHCommDataWiBox_0x90.bits.ReadyStatus = 0x01;          //Ready灯
            WMHCommDataWiBox_0x91.bits.PhoneWaittingToAnswer = 0x01;          //来电指示灯
            WMHCommDataWiBox_0x91.bits.BtConnection = 0x01;          //蓝牙指示灯
            break;
        }
    }
    else
    {
        WMHCommDataWiBox_0x90.bits.Electricity = 0x00;      //电量

        WMHCommDataWiBox_0x90.bits.Espeed_H = 0x06;
        WMHCommDataWiBox_0x90.bits.Espeed_L = 0x02;          //车速
    }
}
void WMHCommFrameTimeoutFunc_Icm(void)
{
    //if((WMHCommDataWiBox_0x92.bits.Inquire != 0)||(WMHCommDataWiBox_0x92.bits.NfcOperate != 0 ))
    {
        //WMHCommFramePackedTransmit(EM_WMH_COMM_SEND_MSG_ICM_0x80);
    }
    
}
