#include "wmhcomm.h"

#include "mwwmhcomm.h"
#include "TYW_stdint.h"
#include "string.h"
#include "Task.h"
#include "CAN_Communication_Matrix.h"

extern COMM_DATA_UNION_WI_BOX_0x01 WMHCommDataWiBox_0x01;
const WMH_COMM_INIT_FRAME_T WMHCommRecvInitList[EM_WMH_COMM_RECV_MSG_TOTAL] = {
    {0x80u, 1000u, COMM_DATA_UNION_WI_BOX_LENGTH_0X80, 5000u, 1u, WMHCommDataWiBox_0x80.Bytes, WMHCommFrameCallBack_WiBox_0x80, WMHCommFrameTimeoutFunc_WiBox80, WMHCommFrameCheckSumFunc},
};

const SifProtocolPacket_t WMHCommSendInitList[EM_WMH_COMM_SEND_MSG_TOTAL] = {
    {0x0Du, 0X01u, 3000u, COMM_DATA_UNION_WI_TLV_LENGTH_0X01, WMHCommDataWiBox_0x01.Bytes, 1u, WMHCommFrameCallBack_WiBox_0x01, WMHCommFrameTimeoutFunc_WiBox_01, WMHCommFrameCheckSumFunc},/*车速*/
    

};

uint32_t WMHCommRecvMessageTimeout[EM_WMH_COMM_RECV_MSG_TOTAL] = {0};
uint32_t WMHCommSendMessageTimeout[EM_WMH_COMM_SEND_MSG_TOTAL] = {0};

uint32_t WMHCommAnalysisPos = 0;
void WMHCommInit(void)
{
    uint8_t i = 0;
    // for (i = 0; i < EM_WMH_COMM_RECV_MSG_TOTAL; i++) {
        // (void)memset(WMHCommRecvInitList[i].dataLength, 0, WMHCommRecvInitList[i].dataLength);
        // WMHCommRecvMessageTimeout[i] = WMHCommRecvInitList[i].TimeoutMax;
    // }
    for (i = 0; i < EM_WMH_COMM_SEND_MSG_TOTAL; i++) {
        (void)memset(WMHCommSendInitList[i].tlvArray, 0, WMHCommSendInitList[i].dataLength);
        WMHCommSendMessageTimeout[i] = WMHCommSendInitList[i].TimeoutMax;
    }
    WMHCommAnalysisPos = 0;
}
/*
    协议号   设备号    数据总长度       TLVs       前面数据累加和
    0x0D     0x01     TLV数据总长度    qq         sumcrc
    
*/
void WMHCommFramePackedTransmit(EM_WMH_COMM_SEND_MSG_T MsgIdx)
{
    uint8_t data[64] = {0};
    uint8_t length = 0;
    if ((MsgIdx < EM_WMH_COMM_SEND_MSG_TOTAL) && (MsgIdx > 0)){
        if ((WMHCommSendInitList[MsgIdx].dataLength + 4) <= 64) {
            // if (WMHCommSendInitList[MsgIdx].FrameCallBackFunc != NULL_P) {
            //     WMHCommSendInitList[MsgIdx].FrameCallBackFunc();
            // }
            data[0] = WMHCommSendInitList[MsgIdx].protocolNumber;//协议号
            data[1] = WMHCommSendInitList[MsgIdx].deviceNumber;//设备号
            data[2] = WMHCommSendInitList[MsgIdx].dataLength + 1u;//数据长度
            (void)memcpy(&data[3], WMHCommSendInitList[MsgIdx].tlvArray, WMHCommSendInitList[MsgIdx].dataLength);
            data[3 + WMHCommSendInitList[MsgIdx].dataLength] = WMHCommSendInitList[MsgIdx].FrameGetCheckSum(WMHCommSendInitList[MsgIdx].dataLength, WMHCommSendInitList[MsgIdx].dataLength, data[1], WMHCommSendInitList[MsgIdx].tlvArray);
            length = WMHCommSendInitList[MsgIdx].dataLength + 4u;
            mwWMHCommTransmitBytes(data, length);
            WMHCommSendMessageTimeout[MsgIdx] = WMHCommSendInitList[MsgIdx].TimeoutMax;
        }
    }
    // else if(MsgIdx == 0)
    // {
    //     if ((WMHCommSendInitList[MsgIdx].DLC + 4) <= 64) {
    //         // if (WMHCommSendInitList[MsgIdx].FrameCallBackFunc != NULL_P) {
    //         //     WMHCommSendInitList[MsgIdx].FrameCallBackFunc();
    //         // }
    //         data[0] = WMHCommSendInitList[MsgIdx].ID;//id
    //         data[1] = WMH_COMM_PROTOCOL_VERSION;//主协议版本号
    //         data[2] = WMHCommSendInitList[MsgIdx].DLC + 1u;//数据长度
    //         (void)memcpy(&data[3], WMHCommSendInitList[MsgIdx].SignalList, WMHCommSendInitList[MsgIdx].DLC);
    //         data[3 + WMHCommSendInitList[MsgIdx].DLC] = WMHCommSendInitList[MsgIdx].FrameGetCheckSum(WMHCommSendInitList[MsgIdx].ID, WMHCommSendInitList[MsgIdx].DLC, data[1], WMHCommSendInitList[MsgIdx].SignalList);
    //         length = WMHCommSendInitList[MsgIdx].DLC + 4u;
    //         mwWMHCommTransmitBytes_90(data, length);
    //         WMHCommSendMessageTimeout[MsgIdx] = WMHCommSendInitList[MsgIdx].TimeoutMax;
    //     }
    // }
}

void WMHCommFrameUnpacked(uint8_t *data, uint32_t length)
{
    uint32_t i = EM_WMH_COMM_RECV_MSG_WI_BOX_0x80;
    if (length > 0) {
            if (data[0] == WMHCommRecvInitList[i].ID) {
                if ((data[2] == (WMHCommRecvInitList[i].DLC + 1)) && (length == (WMHCommRecvInitList[i].DLC + 4))) {
                    if (data[length - 1] == WMHCommRecvInitList[i].FrameGetCheckSum(data[0], WMHCommRecvInitList[i].DLC, data[1], &data[3])) {
                        (void)memcpy(WMHCommRecvInitList[i].SignalList, &data[3], WMHCommRecvInitList[i].DLC);
                        if (WMHCommRecvInitList[i].FrameCallBackFunc != NULL_P) {
                            WMHCommRecvInitList[i].FrameCallBackFunc();
                        }
                        WMHCommRecvMessageTimeout[i] = WMHCommRecvInitList[i].TimeoutMax;
                        //Can_Msg_Tx_Overwrite(&CAN_CH4_CanMsgTxOp,0u,1u,1000u);
                    }
                }
 
            }
    }
}

void WMHCommTimeoutService(uint32_t TimerDelta)
{
    uint8_t i = 0;
    for (i = 0; i < EM_WMH_COMM_RECV_MSG_TOTAL; i++) {
        if (WMHCommRecvMessageTimeout[i] > TimerDelta) {
            WMHCommRecvMessageTimeout[i] -= TimerDelta;
        } else {
            if (WMHCommRecvMessageTimeout[i] > 0) {
                if (WMHCommRecvInitList[i].FrameTimeoutFunc != NULL_P) {
                    WMHCommRecvInitList[i].FrameTimeoutFunc();
                }
            }
            WMHCommRecvMessageTimeout[i] = 0;
        }
    }
    for (i = 0; i < EM_WMH_COMM_SEND_MSG_TOTAL; i++) {
        if (WMHCommSendMessageTimeout[i] > TimerDelta) {
            WMHCommSendMessageTimeout[i] -= TimerDelta;
        } else {
            if (WMHCommSendMessageTimeout[i] > 0) {
                if (WMHCommSendInitList[i].FrameTimeoutFunc != NULL_P) {
                    WMHCommSendInitList[i].FrameTimeoutFunc();
                }
            }
            WMHCommSendMessageTimeout[i] = 0;
        }
    }
}

uint32_t WMHCommServiceCnts = 0;
void WMHCommService(uint32_t TimerDelta)
{
    uint8_t data[64] = {0};
    uint32_t length = 0;
    uint32_t tmpPos = 0;
    uint8_t *tmpAddr = NULL_P;
    uint8_t i = 0;

    WMHCommTimeoutService(TimerDelta);
    WMHCommServiceCnts += TimerDelta;
    if (WMHCommServiceCnts >= 50) {
        WMHCommServiceCnts = 0;
        //mwWMHCommTransmitBytes(tmpBuffer, length);
        tmpAddr = mwWMHCommGetRecvData(&length, &tmpPos);
        if (tmpAddr != NULL_P) {
            i = 0;
            while (WMHCommAnalysisPos != tmpPos) {
                data[i++] = (tmpAddr[WMHCommAnalysisPos++]);
                WMHCommAnalysisPos = WMHCommAnalysisPos % length;
            }
            // if (SYS_OPR_STAT_IGN_ON)
            //  {
                WMHCommFrameUnpacked(data, i);
            // }
        }
    }
}

uint8_t WMHCommFrameGetRecvTimeoutSt(uint8_t n)
{
    uint8_t ret = 0;
    if (n < EM_WMH_COMM_RECV_MSG_TOTAL) {
        ret = (uint8_t)(WMHCommRecvMessageTimeout[n] == 0);
    }
    return ret;
}
