#include "UDS_TP.h"
#include "UDS_Service.h"
/*
unsigned int TP_SendTime;

s_UDS_Msg_Buffer UDS_Rx_Buffer[2];
s_UDS_Msg_Buffer AnalysisBuffer;

s_UDS_Msg_Buffer UDS_Tx_Buffer;

e_CommunicateStatus CommunicateStatus;

unsigned int RxLength = 0;

void GetUDSMessage(unsigned char *CAN_BufAddr, unsigned char len)
{
    unsigned char i;
    if ((UDS_Rx_Buffer[0].AnalysisStatus == 0) && (UDS_Rx_Buffer[1].AnalysisStatus == 0))
    {
        for (i = 0; i < len; i++)
        {
            UDS_Rx_Buffer[0].DataMsg.Data[i] = CAN_BufAddr[i];
        }
        UDS_Rx_Buffer[0].AnalysisStatus = 1;
    }
    else if ((UDS_Rx_Buffer[0].AnalysisStatus == 1) && (UDS_Rx_Buffer[1].AnalysisStatus == 0))
    {
        for (i = 0; i < len; i++)
        {
            UDS_Rx_Buffer[1].DataMsg.Data[i] = CAN_BufAddr[i];
        }
        UDS_Rx_Buffer[1].AnalysisStatus = 1;
    }
    else if ((UDS_Rx_Buffer[0].AnalysisStatus == 0) && (UDS_Rx_Buffer[1].AnalysisStatus == 1))
    {
        UDS_Rx_Buffer[0] = UDS_Rx_Buffer[1];
        for (i = 0; i < len; i++)
        {
            UDS_Rx_Buffer[1].DataMsg.Data[i] = CAN_BufAddr[i];
        }
        UDS_Rx_Buffer[1].AnalysisStatus = 1;
    }

    ClearCtrlTime(UDS_3E_Service);
}

unsigned char FillTxBuffer(unsigned char *Data, unsigned char len)
{
    unsigned char i;

    if (UDS_Tx_Buffer.AnalysisStatus)
    {
        return 0;
    }

    for (i = 0; i < 8; i++)
    {
        if (i < len)
        {
            UDS_Tx_Buffer.DataMsg.Data[i] = Data[i];
        }
        else
        {
            UDS_Tx_Buffer.DataMsg.Data[i] = 0x55;
        }
    }

    UDS_Tx_Buffer.AnalysisStatus = 1;

    return 1;
}

void ClearUDS_Msg_Buffer(s_UDS_Msg_Buffer *buffer)
{
    unsigned char i;

    for (i = 0; i < 8; i++)
    {
        buffer->DataMsg.Data[i] = 0xFF;
    }

    buffer->AnalysisStatus = 0;
}

void AnalysisUDSMessage(void)
{
    unsigned char i;
    unsigned int wTemp;
    e_TxType rType;

    if (AnalysisBuffer.AnalysisStatus == 0)
    {
        if (UDS_Rx_Buffer[0].AnalysisStatus == 1)
        {
            AnalysisBuffer = UDS_Rx_Buffer[0];
            UDS_Rx_Buffer[0].AnalysisStatus = 0;
            ClearUDS_Msg_Buffer(&UDS_Rx_Buffer[0]);
        }

        if ((UDS_Rx_Buffer[0].AnalysisStatus == 0) && (UDS_Rx_Buffer[1].AnalysisStatus == 1))
        {
            UDS_Rx_Buffer[0] = UDS_Rx_Buffer[1];
            UDS_Rx_Buffer[1].AnalysisStatus = 0;
            ClearUDS_Msg_Buffer(&UDS_Rx_Buffer[1]);
        }
    }

    if ((UDS_Rx_Buffer[0].DataMsg.FC_Format.N_PCItype == N_PCITYPE_FC) || (UDS_Rx_Buffer[1].DataMsg.FC_Format.N_PCItype == N_PCITYPE_FC))
    {
        wTemp = 9999;
    }

    if (AnalysisBuffer.DataMsg.Data[0] == 0x30)
    {
        wTemp = 9999;
    }

    switch (CommunicateStatus)
    {
    case COMM_Free:
    {
        if (AnalysisBuffer.DataMsg.SF_Format.N_PCItype == N_PCITYPE_SF)
        {
            if ((AnalysisBuffer.DataMsg.SF_Format.SF_DL < 8) && (AnalysisBuffer.DataMsg.SF_Format.SF_DL != 0))
            {
                if (AnalysisBuffer.DataMsg.Data[1] != 0x7E)
                {
                    CommunicateStatus = COMM_WaitServiceHandle;
                    SetRxTransferStatus(TransferStart, AnalysisBuffer.DataMsg.SF_Format.SF_DL);
                    TransferServiceRxInfo(&AnalysisBuffer.DataMsg.Data[1], AnalysisBuffer.DataMsg.SF_Format.SF_DL);
                    SetRxTransferStatus(TransferEnd, 0);
                }
            }
        }
        else if (AnalysisBuffer.DataMsg.FF_Format.N_PCItype == N_PCITYPE_FF)
        {
            RxLength = AnalysisBuffer.DataMsg.FF_Format.FF_DL_H;
            RxLength = RxLength << 8;
            RxLength = RxLength | AnalysisBuffer.DataMsg.FF_Format.FF_DL_L;

            if (RxLength >= 8)
            {
                CommunicateStatus = COMM_Send_FC;
                SetRxTransferStatus(TransferStart, RxLength);
                TransferServiceRxInfo(&AnalysisBuffer.DataMsg.Data[2], 6);
            }
        }
    }
    break;

    case COMM_Send_FC:
    {
        TP_SendFC();
        CommunicateStatus = COMM_Wait_CF;
    }
    break;

    case COMM_Wait_CF:
    {
        if (AnalysisBuffer.DataMsg.CF_Format.N_PCItype == N_PCITYPE_CF)
        {
            TransferServiceRxInfo(&AnalysisBuffer.DataMsg.Data[1], 7);
            if (GetRxTransferDataNumber() > RxLength)
            {
                CommunicateStatus = COMM_WaitServiceHandle;
                RxLength = 0;
                SetRxTransferStatus(TransferEnd, 0);
            }
        }
    }
    break;

    case COMM_Send_Frame:
    {
        rType = TransferServiceTxInfo(&UDS_Tx_Buffer);
        if (rType == Tx_FF)
        {
            CommunicateStatus = COMM_Wait_FC;
        }
        else if ((rType == Tx_End) || (rType == Tx_SF))
        {
            CommunicateStatus = COMM_Free;
        }
    }
    break;

    case COMM_Wait_FC:
    {
        ClearCtrlTime(UDS_3E_Service);
        EnableTimeCtrl(UDS_CF_Send);
        if (GetCtrlTime(UDS_CF_Send) >= 100)
        {
            DisableTimeCtrl(UDS_CF_Send);
            CommunicateStatus = COMM_Send_Frame;
        }
        if (AnalysisBuffer.DataMsg.FC_Format.N_PCItype == N_PCITYPE_FC)
        {
            CommunicateStatus = COMM_Send_Frame;
        }
    }
    break;

    default:;
    }

    AnalysisBuffer.AnalysisStatus = 0;
    ClearUDS_Msg_Buffer(&AnalysisBuffer);
}

void TP_SendFC(void)
{
    unsigned char data[8];
    unsigned char ret;

    data[0] = 0x30;
    data[1] = 0x08;
    data[2] = 0x14;

    ret = FillTxBuffer(data, 3);
    TP_SendTime = 10;
}

void EnableUDSSend(void)
{
    CommunicateStatus = COMM_Send_Frame;
}

void TP_SendServiceMsg(void)
{
    if (UDS_Tx_Buffer.AnalysisStatus)
    {
        UDS_Tx_Buffer.AnalysisStatus = 0;

        CanFD_Send_StandMsg(CANFD0_Type, 0x7C6, BUF_8, TX_ISR_EN, (uint8_t *)&UDS_Tx_Buffer.DataMsg.Data, 8);
        ClearUDS_Msg_Buffer(&UDS_Tx_Buffer);
        ClearCtrlTime(UDS_3E_Service);
    }
}*/