#include "wmhcomm.h"
#include "wmhcommdata.h"
#include "TYW_stdint.h"
#include "string.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_WiBox_90(void)
{
    // 替代第一个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X90; i++) {
        WMHCommDataWiBox_0x90.Bytes[i] = 0;
    }
}

void WMHCommFrameTimeoutFunc_WiBox_91(void)
{
    // 替代第二个 memset
    for (int i = 0; i < COMM_DATA_UNION_WI_BOX_LENGTH_0X91; i++) {
        WMHCommDataWiBox_0x91.Bytes[i] = 0;
    }
}

void WMHCommFrameTimeoutFunc_WiBox_92(void)
{
    // 替代第三个 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;
// uint8_t count = 0;
void WMHCommFrameCallBack_Icm(void)
{
    // uint8_t CardKind = 0;
    // //EM_FUEL_STATE_T tmpSt = GetFuelModuleState();
    // uint32_t tmpVal = Data_ODO_Read() / 10u; 

    // WMHCommDataIcm.bits.Mileage_H = 0x11;
    // WMHCommDataIcm.bits.Mileage_M = 0x33;
    // WMHCommDataIcm.bits.Mileage_L = 0x44;
    
    // //if(WMHCommDataWiBox_0x92.bits.Inquire == 0x11)
    // {
    //     if (tmpVal > 9999999) {
    //         tmpVal = 9999999;
    //     }
    //     // WMHCommDataIcm.bits.Mileage_H = (tmpVal >> 16) & 0xFF;
    //     // WMHCommDataIcm.bits.Mileage_M = (tmpVal >> 8) & 0xFF;
    //     // WMHCommDataIcm.bits.Mileage_L = (tmpVal) & 0xFF;    
    //     WMHCommDataIcm.bits.Mileage_H = 0x11;
    //     WMHCommDataIcm.bits.Mileage_M = 0x33;
    //     WMHCommDataIcm.bits.Mileage_L = 0x44;
    // }

    // if(WMHCommDataWiBox_0x92.bits.Inquire == 0x22)
    // {
    //     count = WMHCommDataWiBox_0x92.bits.Card_H / 16;
    //     if(NFC_CARD[count-1][1] == 0)
    //     {
    //         CardKind = 2; 
    //     }
    //     else
    //     {
    //         CardKind = 1;
    //     }
    //     WMHCommDataIcm.bits.CardKind   = CardKind;
    //     WMHCommDataIcm.bits.CardNum    = count*16+count;
    //     WMHCommDataIcm.bits.CardID1    = NFC_CARD[count-1][2];
    //     WMHCommDataIcm.bits.CardID2    = NFC_CARD[count-1][3];
    //     WMHCommDataIcm.bits.CardID3    = NFC_CARD[count-1][4];
    //     WMHCommDataIcm.bits.CardID4    = NFC_CARD[count-1][5];
    //     WMHCommDataIcm.bits.CardTotal  = Study_CardCount;
    // }
    // else
    // {
    //     count = 0;
    //     WMHCommDataIcm.bits.CardKind   = 0;
    //     WMHCommDataIcm.bits.CardNum    = 0;
    //     WMHCommDataIcm.bits.CardID1    = 0;
    //     WMHCommDataIcm.bits.CardID2    = 0;
    //     WMHCommDataIcm.bits.CardID3    = 0;
    //     WMHCommDataIcm.bits.CardID4    = 0;
    //     WMHCommDataIcm.bits.CardTotal  = 0;
    // }
    // if(WMHCommDataWiBox_0x92.bits.NfcOperate == 0x11)
    // {
    //     WMHCommDataIcm.bits.AddCardStatus = Add_Card_Status;
    //     if(WMHCommDataIcm.bits.AddCardStatus == 2)
    //     {
    //         WMHCommDataIcm.bits.CardKind   = NFC_CARD[NFC_Empty_card][1]; 
    //         WMHCommDataIcm.bits.CardNum    = (NFC_Empty_card + 1)*17;
    //         WMHCommDataIcm.bits.CardID1    = NFC_CARD[NFC_Empty_card][2];
    //         WMHCommDataIcm.bits.CardID2    = NFC_CARD[NFC_Empty_card][3];
    //         WMHCommDataIcm.bits.CardID3    = NFC_CARD[NFC_Empty_card][4];
    //         WMHCommDataIcm.bits.CardID4    = NFC_CARD[NFC_Empty_card][5];
    //         WMHCommDataIcm.bits.CardTotal  = Study_CardCount;
    //     }
    //     else if(WMHCommDataIcm.bits.AddCardStatus == 1)
    //     {
    //         WMHCommDataIcm.bits.CardKind   = 0; 
    //         WMHCommDataIcm.bits.CardNum    = (NFC_Empty_card + 1)*17;
    //         WMHCommDataIcm.bits.CardID1    = 0;
    //         WMHCommDataIcm.bits.CardID2    = 0;
    //         WMHCommDataIcm.bits.CardID3    = 0;
    //         WMHCommDataIcm.bits.CardID4    = 0;
    //         WMHCommDataIcm.bits.CardTotal  = Study_CardCount;           
    //     }
    //     else
    //     {
    //         WMHCommDataIcm.bits.CardKind   = 0; 
    //         WMHCommDataIcm.bits.CardNum    = 0;
    //         WMHCommDataIcm.bits.CardID1    = 0;
    //         WMHCommDataIcm.bits.CardID2    = 0;
    //         WMHCommDataIcm.bits.CardID3    = 0;
    //         WMHCommDataIcm.bits.CardID4    = 0;
    //         WMHCommDataIcm.bits.CardTotal  = Study_CardCount;    
    //     }
    //     WMHCommDataIcm.bits.RemoveCardStatus = 0;
    // }
    // else if(WMHCommDataWiBox_0x92.bits.NfcOperate == 0x22)
    // {
    //     WMHCommDataIcm.bits.RemoveCardStatus = 2;
    //     WMHCommDataIcm.bits.AddCardStatus = 0;
    // }
    // else
    // {
    //     WMHCommDataIcm.bits.AddCardStatus = 0xFF;
    //     WMHCommDataIcm.bits.RemoveCardStatus = 0xFF;
    // }
    // WMHCommDataIcm.bits.Reserved = 0xFF;
    // WMHCommDataWiBox_0x90.bits.Espeed_L = 0xDD;
}
void WMHCommFrameTimeoutFunc_Icm(void)
{
    //if((WMHCommDataWiBox_0x92.bits.Inquire != 0)||(WMHCommDataWiBox_0x92.bits.NfcOperate != 0 ))
    {
        //WMHCommFramePackedTransmit(EM_WMH_COMM_SEND_MSG_ICM_0x80);
    }
    
}
