/******************************************************************************
    Function Name    :  hUpdateFormattedMsgStruct
    Input(s)         :
    Output           :
    Functionality    :  Format the requested Msg and save it in Format data
                        structure which is accessible from the User module
    Member of        :  CMsgContainerCAN
    Friend of        :      -
    Author(s)        :  Anish kumar
    Date Created     :  01.04.2010
******************************************************************************/
HRESULT CMsgContainerCAN::hUpdateFormattedMsgStruct(int nListIndex,
        int& nMsgCode,
        BYTE bExprnFlag_Disp,
        __int64 nTimeOffset)
{
    HRESULT hResult = S_FALSE;
    nMsgCode  = 0;
    static STCANDATA sCANCurrData;
    static STCANDATASPL sCANCurrDataSpl;
    memset( &m_sOutFormattedData, 0, sizeof(m_sOutFormattedData) );
    if( IS_MODE_APPEND(bExprnFlag_Disp) )
    {
        //INT nType = 0;
        //INT nSize =sizeof(sCANCurrDataSpl);
        //In append mode providing interpret state is not required
        //hResult = m_ouAppendCanBuf.ReadEntry(nType, (BYTE*)&sCANCurrDataSpl, nSize, nListIndex, 0);
        hResult = m_ouAppendCanBuf.ReadFromBuffer(&sCANCurrDataSpl, nListIndex);
        sCANCurrData = *((STCANDATA*)&sCANCurrDataSpl);
        if (IS_TM_REL_SET(bExprnFlag_Disp))
        {
            //If relative time then the time will be delta time, for formatting
            sCANCurrData.m_lTickCount.QuadPart = sCANCurrDataSpl.m_nDeltime;
        }
    }
    else
    {
        hResult = m_ouOWCanBuf.ReadFromBuffer(&sCANCurrData, nListIndex);
        if (hResult == S_OK)
        {
            if (IS_TM_REL_SET(bExprnFlag_Disp))
            {
                //If relative time then the time will be delta time, for formatting
                sCANCurrData.m_lTickCount.QuadPart -= nTimeOffset;
            }
        }
    }

    if (hResult == S_OK)
    {
        m_ouFormatCAN.vFormatCANDataMsg(&sCANCurrData,
                                        &m_sOutFormattedData,
                                        bExprnFlag_Disp);

        //Now add the name of message if present in database else show the code
        nMsgCode = sCANCurrData.m_uDataInfo.m_sCANMsg.m_unMsgID;

        CString name;
        GetMessageName(sCANCurrData, name, IS_NUM_HEX_SET(bExprnFlag_Disp));
        strcpy(m_sOutFormattedData.m_acMsgDesc, name.GetBuffer(0));
    }
    else
    {
        memset (&m_sOutFormattedData, 0, sizeof(m_sOutFormattedData));
        nMsgCode = -1;
    }

    return hResult;
}
/******************************************************************************
    Function Name    :  hUpdateFormattedMsgStruct
    Input(s)         :
    Output           :
    Functionality    :  Format the requested Msg and save it in Format data
                        structure which is accessible from the User module
    Member of        :  CMsgContainerLIN
    Friend of        :      -
    Author(s)        :  Anish kumar
    Date Created     :  01.04.2010
******************************************************************************/
HRESULT CMsgContainerLIN::hUpdateFormattedMsgStruct(int nListIndex,
        int& nMsgCode,
        BYTE bExprnFlag_Disp,
        __int64 nTimeOffset)
{
    HRESULT hResult = S_FALSE;
    nMsgCode  = 0;
    static STLINDATA sLINCurrData;
    static STLINDATASPL sLINCurrDataSpl;
    memset( &m_sOutFormattedData, 0, sizeof(m_sOutFormattedData) );
    if( IS_MODE_APPEND(bExprnFlag_Disp) )
    {
        //INT nType = 0;
        //INT nSize =sizeof(sLINCurrDataSpl);
        //In append mode providing interpret state is not required
        //hResult = m_ouAppendLinBuf.ReadEntry(nType, (BYTE*)&sLINCurrDataSpl, nSize, nListIndex, 0);
        hResult = m_ouAppendLinBuf.ReadFromBuffer(&sLINCurrDataSpl, nListIndex);
        sLINCurrData = *((STLINDATA*)&sLINCurrDataSpl);
        if (IS_TM_REL_SET(bExprnFlag_Disp))
        {
            //If relative time then the time will be delta time, for formatting
            sLINCurrData.m_lTickCount.QuadPart = sLINCurrDataSpl.m_nDeltime;
        }
    }
    else
    {
        hResult = m_ouOWLinBuf.ReadFromBuffer(&sLINCurrData, nListIndex);
        if (hResult == S_OK)
        {
            if (IS_TM_REL_SET(bExprnFlag_Disp))
            {
                //If relative time then the time will be delta time, for formatting
                sLINCurrData.m_lTickCount.QuadPart -= nTimeOffset;
            }
        }
    }

    if (hResult == S_OK)
    {
        m_ouFormatLIN.vFormatLINDataMsg(&sLINCurrData,
                                        &m_sOutFormattedData,
                                        bExprnFlag_Disp);
        nMsgCode = n64CreateMapIDForLinEvevnt( &sLINCurrData) ;
        //If Message is erroneous, return S_FALSE
        //if(sLINCurrData.m_eLinMsgType == LIN_EVENT)
        //      {
        //  nMsgCode = n64CreateMapIDForLinEvevnt( &sLINCurrData) ;//usProcessCurrErrorEntry(sLINCurrData.m_uDataInfo.m_sErrInfo);
        //          return S_FALSE;
        //      }
        //if(sLINCurrData.m_eLinMsgType == LIN_MSG)
        //{
        //  //Now add the name of message if present in database else show the code
        //  nMsgCode = sLINCurrData.m_uDataInfo.m_sLINMsg.m_ucMsgID;
        //}
    }
    else
    {
        memset (&m_sOutFormattedData, 0, sizeof(m_sOutFormattedData));
        nMsgCode = -1;
    }

    return hResult;
}
/******************************************************************************
    Function Name    :  hUpdateFormattedMsgStruct
    Input(s)         :
    Output           :
    Functionality    :  Format the requested Msg and save it in Format data
                        structure which is accessible from the User module
    Member of        :  CMsgContainerJ1939
    Friend of        :      -
    Author(s)        :  Arun kumar K
    Date Created     :  20.01.2011
******************************************************************************/
HRESULT CMsgContainerJ1939::hUpdateFormattedMsgStruct(int nListIndex,
        int& nMsgCode,
        BYTE bExprnFlag_Disp,
        __int64 nTimeOffset)
{
    HRESULT hResult = S_FALSE;
    INT nSize = 0;
    nMsgCode  = 0;
    //memset( &m_sOutFormattedData, 0, sizeof(m_sOutFormattedData) );
    if( IS_MODE_APPEND(bExprnFlag_Disp) )
    {
        INT nType = 0;
        nSize = MAX_MSG_LEN_J1939 + sizeof(__int64);

        //In append mode providing interpret state is not required
        hResult = m_ouAppendJ1939Buf.ReadEntry(nType, m_pbyJ1939Data, nSize,
                                               nListIndex, FALSE);

        if (hResult == S_OK)
        {
            // Save it into J1939 message structure
            m_sJ1939ReadMsgSpl.vSetDataStream(m_pbyJ1939Data);

            // Copy delta time
            BYTE* pBytTemp = m_pbyJ1939Data;
            pBytTemp += /*m_sJ1939Data.unGetSize()*/nSize - sizeof(__int64);
            memcpy(&m_sJ1939ReadMsgSpl.m_nDeltime, pBytTemp, sizeof(__int64));
        }
    }
    else
    {
        hResult = m_ouOWJ1939Buf.ReadFromBuffer(&m_sJ1939Data,
                                                nListIndex);
        if (hResult == S_OK)
        {
            UINT64 u64DelTS = m_sJ1939Data.m_sMsgProperties.m_un64TimeStamp - nTimeOffset;

            m_sJ1939Data.vGetDataStream(m_pbyJ1939Data);
            m_sJ1939ReadMsgSpl.vSetDataStream(m_pbyJ1939Data);
            m_sJ1939ReadMsgSpl.m_nDeltime = u64DelTS;
        }
    }

    if (hResult == S_OK)
    {
        m_ouFormatJ1939.vFormatJ1939DataMsg(
            (PSTJ1939_MSG) &(m_sJ1939ReadMsgSpl), m_sJ1939ReadMsgSpl.m_nDeltime,
            &m_sOutFormattedData, bExprnFlag_Disp);

        CString name;
        GetMessageName(m_sJ1939ReadMsgSpl, name, IS_NUM_HEX_SET(bExprnFlag_Disp));
        strcpy(m_sOutFormattedData.m_acMsgName, name.GetBuffer(0));

        //Now add the name of message if present in database else show the code
        if (nullptr != m_sJ1939ReadMsgSpl.m_pbyData)
        {
            //nMsgCode = m_sJ1939ReadMsgSpl.m_pbyData[0];
            nMsgCode = m_sJ1939ReadMsgSpl.m_sMsgProperties.m_uExtendedID.m_s29BitId.unGetPGN();
        }
    }
    return hResult;
}