关于c++:CCTSCMICC

80次阅读

共计 49984 个字符,预计需要花费 125 分钟才能阅读完成。

include <stdio.h>

include “tulip.h”

include “TSCMI.h”

define _CHECK_BUFLEN_DEMAND_RETFALSE(done, total, demand) \

do {\

if ((done+demand) > total) \
{ \
    if (dbg_TSCPrt) \
    { \
        printf("[%s:%d]EPMS TotalLen not enough. total:%u, demand:%u\n", __FUNCTION__, __LINE__, (WORD32)total, (WORD32)demand); \
    } \
    return FALSE; \
} \

} while (0)

ifdef LITTLE_ENDIAN

define _GET_NTOH16(ptrbyte) (WORD16)(((((WORD16 )(ptrbyte))>>8) & 0xff) | (((WORD16 )(ptrbyte))<<8))

define _GET_NTOH32(ptrbyte) (WORD32)(((((WORD32 )(ptrbyte))>>24) & 0xff) | ((((WORD32 )(ptrbyte))>>8) & 0xff00) | ((((WORD32 )(ptrbyte))<<8) & 0xff0000) | (((WORD32 )(ptrbyte))<<24))

define _GET_NTOH64(ptrbyte) (WORD64)(((((WORD64 )(ptrbyte)) & 0xff00000000000000) >> 56) | \

                                             (((*(WORD64 *)(ptrbyte)) & 0x00ff000000000000) >> 40) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x0000ff0000000000) >> 24) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x000000ff00000000) >> 8) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x00000000ff000000) << 8) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x0000000000ff0000) << 24) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x000000000000ff00) << 40) | \
                                             (((*(WORD64 *)(ptrbyte)) & 0x00000000000000ff) << 56))

define _SET_HTON16(ptrbyte, val) do {((WORD16 )(ptrbyte)) = ((((val)>>8) & 0xff) | ((val)<<8));} while (0)

define _SET_HTON32(ptrbyte, val) do {((WORD32 )(ptrbyte)) = ((((val)>>24) & 0xff) | (((val)>>8) & 0xff00) | (((val)<<8) & 0xff0000) | ((val)<<24));} while (0)

define _SET_HTON64(ptrbyte, val) do {((WORD64 )(ptrbyte)) = ((((val) & 0xff00000000000000) >> 56) | \

                                                                 (((val) & 0x00ff000000000000) >> 40) | \
                                                                 (((val) & 0x0000ff0000000000) >> 24) | \
                                                                 (((val) & 0x000000ff00000000) >> 8) | \
                                                                 (((val) & 0x00000000ff000000) << 8) | \
                                                                 (((val) & 0x0000000000ff0000) << 24) | \
                                                                 (((val) & 0x000000000000ff00) << 40) | \
                                                                 (((val) & 0x00000000000000ff) << 56));} while (0)

else

define _GET_NTOH16(ptrbyte) (WORD16)((WORD16 )(ptrbyte))

define _GET_NTOH32(ptrbyte) (WORD32)((WORD32 )(ptrbyte))

define _GET_NTOH64(ptrbyte) (WORD64)((WORD64 )(ptrbyte))

define _SET_HTON16(ptrbyte, val) do {((WORD16 )(ptrbyte)) = (val);} while (0)

define _SET_HTON32(ptrbyte, val) do {((WORD32 )(ptrbyte)) = (val);} while (0)

define _SET_HTON64(ptrbyte, val) do {((WORD64 )(ptrbyte)) = (val);} while (0)

endif

ifndef MAX

define MAX(x, y) (((x)>(y))?(x):(y))

endif

ifndef MIN

define MIN(x, y) (((x)<(y))?(x):(y))

endif

BOOLEAN dbg_TSCPrt = FALSE;

static void printMsgBuf(const BYTE *Buf, WORD32 BufLen)
{

_CHECK_TRUE_RETURN_VOID(!dbg_TSCPrt);
_CHECK_TRUE_RETURN_VOID(NULL == Buf);

printf("\n********** printMsgBuf BufLen:%u *********\n", BufLen);

WORD32      i;
for (i = 0; i < BufLen; i ++)
{printf("%.2X", Buf[i]);

    if (((i+1)%8) == 0)
    {printf("\n");
    }
}

if (((i+1)%8) != 0)
{printf("\n");
}

printf("********** printMsgBuf End *********\n\n");

}

/ EPMS /
static BOOLEAN decode_EPMS_EthernetPortParameter(const BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 4);

WORD16      name = _GET_NTOH16(&Buf[done]);
done        += 2;
WORD16      length = _GET_NTOH16(&Buf[done]);
done        += 2;

_TSC_TRACE("Parameter Name:0x%X, Length:%u\n", name, length);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, length);

done        += length;

return TRUE;

}

static BOOLEAN decode_EPMS_EthernetPortParameter(const BYTE *Buf, WORD32 BufLen, WORD32 &done, EPMS_EthernetPortParameterTL &TL, EPMS_EthernetPortParameterV &V)
{

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 4);

TL.Name     = _GET_NTOH16(&Buf[done]);
done        += 2;
TL.Length   = _GET_NTOH16(&Buf[done]);
done        += 2;

_TSC_TRACE("Parameter Name:0x%X, Length:%u\n", TL.Name, TL.Length);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, TL.Length);

switch (TL.Name)
{
    case EPMS_ETHERNET_PORT_PARAMETER_txPropagationDelay:
    {_CHECK_TRUE_RETURN_VAL(TL.Length != 8, FALSE);
        _CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 8);
        V.txPropagationDelay.Value  = _GET_NTOH64(&Buf[done]);
        _TSC_TRACE("txPropagationDelay value:%llu\n", V.txPropagationDelay.Value);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TraffiClassTable:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 2);
        V.TrafficClassTable.IEI     = Buf[done];
        V.TrafficClassTable.Length  = Buf[done+1];
        _TSC_TRACE("TrafficClassTable Length:%u\n", V.TrafficClassTable.Length);

        _CHECK_BUFLEN_DEMAND_RETFALSE(2, TL.Length, V.TrafficClassTable.Length);

        if (V.TrafficClassTable.Length > sizeof(EPMS_TCContents))
        {
            _TSC_TRACE("EthernetPortParameter TraffiClassTable Length error. parameter.Length:%u, parameter.TrafficClassTable.Length:%u, EPMS_TCContents:%lu\n", 
                        TL.Length, V.TrafficClassTable.Length, sizeof(EPMS_TCContents));
            return FALSE;
        }
        
        memcpy(&V.TrafficClassTable.TCContents, &Buf[done+2], V.TrafficClassTable.Length);
        _TSC_TRACE("TrafficClassTable TCContents NumberOfTrafficClasses:%u\n", V.TrafficClassTable.TCContents.NumberOfTrafficClasses);
        WORD32      i;
        for (i = 0; i < MIN(V.TrafficClassTable.TCContents.NumberOfTrafficClasses, EPMS_TCINFORMATION_MAXNUM); i ++)
        {_TSC_TRACE("TrafficClassTable TCInformation[%u]:TrafficClassValue:%u, bPriorityValue:0x%X\n", 
                        i, V.TrafficClassTable.TCContents.TCInformation[i].TrafficClassValue,
                        V.TrafficClassTable.TCContents.TCInformation[i].bPriorityValue);
        }
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_GateEnabled:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 1);
        V.GateEnabled   = Buf[done];
        _TSC_TRACE("GateEnabled:%u\n", V.GateEnabled);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminBaseTime:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 10);
        V.AdminBaseTime.Seconds_Hi  = _GET_NTOH16(&Buf[done]);
        V.AdminBaseTime.Seconds     = _GET_NTOH32(&Buf[done+2]);
        V.AdminBaseTime.Nanoseconds = _GET_NTOH32(&Buf[done+6]);
        _TSC_TRACE("AdminBaseTime Seconds_Hi:%u, Seconds:%u, Nanoseconds:%u\n", 
                    V.AdminBaseTime.Seconds_Hi, V.AdminBaseTime.Seconds, V.AdminBaseTime.Nanoseconds);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlListLength:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 2);
        V.AdminControlListLength    = _GET_NTOH16(&Buf[done]);
        _TSC_TRACE("AdminControlListLength:%u\n", V.AdminControlListLength);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlList:
    {
        WORD32      i;
        WORD32      offset = done;

        V.AdminControlLists.AdminControlListNum = MIN(TL.Length/sizeof(EPMS_AdminControlList), EPMS_ADMINCONTROLLIST_MAXNUM);

        for (i = 0; i < V.AdminControlLists.AdminControlListNum; i ++)
        {_CHECK_BUFLEN_DEMAND_RETFALSE(i*sizeof(EPMS_AdminControlList), TL.Length, sizeof(EPMS_AdminControlList));
            V.AdminControlLists.AdminControlList[i].GateOperationName   = Buf[offset];
            V.AdminControlLists.AdminControlList[i].Length              = Buf[offset+1];
            V.AdminControlLists.AdminControlList[i].bGateState          = Buf[offset+2];
            V.AdminControlLists.AdminControlList[i].TimeInterval        = _GET_NTOH32(&Buf[offset+3]);
            offset  += sizeof(EPMS_AdminControlList);
            _TSC_TRACE("AdminControlList[%u]: GateOperationName:%u, Length:%u, bGateState:0x%X, TimeInterval:%u\n", 
                        i, V.AdminControlLists.AdminControlList[i].GateOperationName, V.AdminControlLists.AdminControlList[i].Length,
                        V.AdminControlLists.AdminControlList[i].bGateState, V.AdminControlLists.AdminControlList[i].TimeInterval);
        }
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminCycleTime:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 8);
        V.AdminCycleTime.AdminCycleTimeNumerator    = _GET_NTOH32(&Buf[done]);
        V.AdminCycleTime.AdminCycleTimeDenominator  = _GET_NTOH32(&Buf[done+4]);
        _TSC_TRACE("AdminCycleTime AdminCycleTimeNumerator:%u, AdminCycleTimeDenominator:%u\n", 
                    V.AdminCycleTime.AdminCycleTimeNumerator, V.AdminCycleTime.AdminCycleTimeDenominator);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TickGranularity:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 4);
        V.TickGranularity.Value     = _GET_NTOH32(&Buf[done]);
        _TSC_TRACE("TickGranularity value:%u\n", V.TickGranularity.Value);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_PSFPMaxStreamFilterInstances:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 4);
        V.PSFPMaxStreamFilterInstances.Value    = _GET_NTOH32(&Buf[done]);
        _TSC_TRACE("PSFPMaxStreamFilterInstances value:%u\n", V.PSFPMaxStreamFilterInstances.Value);
        break;
    }

    default:
    {_TSC_TRACE("EthernetPortParameter not support! Name:0x%X\n", TL.Name);
        break;
    }
}

done        += TL.Length;

return TRUE;

}

static WORD16 calc_msglen_EPMS_EthernetPortParameter(EPMS_EthernetPortParameterTL &TL, EPMS_EthernetPortParameterV &V)
{

WORD16      msglen = 0;

/* Ethernet port parameter name */
/* Length of Ethernet port parameter value */
msglen  += 4;

switch (TL.Name)
{
    case EPMS_ETHERNET_PORT_PARAMETER_txPropagationDelay:
    {
        TL.Length   = 8;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TraffiClassTable:
    {    
        V.TrafficClassTable.IEI                                 = 0;
        V.TrafficClassTable.TCContents.NumberOfTrafficClasses   = MIN(V.TrafficClassTable.TCContents.NumberOfTrafficClasses, EPMS_TCINFORMATION_MAXNUM);
        V.TrafficClassTable.Length                              = 1 + V.TrafficClassTable.TCContents.NumberOfTrafficClasses * sizeof(EPMS_TCInformation);
        TL.Length                                               = 2 + V.TrafficClassTable.Length;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_GateEnabled:
    {
        TL.Length   = 1;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminBaseTime:
    {
        TL.Length   = 10;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlListLength:
    {
        TL.Length   = 2;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlList:
    {V.AdminControlLists.AdminControlListNum = MIN(V.AdminControlLists.AdminControlListNum, EPMS_ADMINCONTROLLIST_MAXNUM);
        TL.Length                               = V.AdminControlLists.AdminControlListNum * sizeof(EPMS_AdminControlList);
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminCycleTime:
    {
        TL.Length   = 8;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TickGranularity:
    {
        TL.Length   = 4;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_PSFPMaxStreamFilterInstances:
    {
        TL.Length   = 4;
        break;
    }
    
    default:
    {_TSC_TRACE("calc_msglen_EPMS_EthernetPortParameter not support! Name:0x%X\n", TL.Name);
        TL.Length   = 0;
        break;
    }
}

msglen  += TL.Length;

return msglen;

}

static WORD16 calc_msglen_EPMS_EthernetPortParameter(EPMS_EthernetPortParameterTL TL[], EPMS_EthernetPortParameterV &V, WORD32 num)
{

WORD16      msglen = 0;
WORD32      i;

for (i = 0; i < num; i ++)
{msglen  += calc_msglen_EPMS_EthernetPortParameter(TL[i], V);
}

return msglen;

}

static void encode_EPMS_EthernetPortParameter(EPMS_EthernetPortParameterTL &TL, EPMS_EthernetPortParameterV &V, BYTE *Buf, WORD32 &done)
{

/* Ethernet port parameter name and length */
_SET_HTON16(&Buf[done], TL.Name);
done        += 2;
_SET_HTON16(&Buf[done], TL.Length);
done        += 2;
   
switch (TL.Name)
{
    case EPMS_ETHERNET_PORT_PARAMETER_txPropagationDelay:
    {_SET_HTON64(&Buf[done], V.txPropagationDelay.Value);
        done        += 8;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TraffiClassTable:
    {    
        /* Traffic class table IEI */
        Buf[done]   = V.TrafficClassTable.IEI;
        done        ++;
        
        /* Length of traffic class table contents */
        Buf[done]   = V.TrafficClassTable.Length;
        done        ++;

        /* Traffic class table contents */
        memcpy(&Buf[done], &V.TrafficClassTable.TCContents, V.TrafficClassTable.Length);
        done        += V.TrafficClassTable.Length;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_GateEnabled:
    {Buf[done]   = V.GateEnabled;
        done        ++;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminBaseTime:
    {_SET_HTON16(&Buf[done], V.AdminBaseTime.Seconds_Hi);
        done        += 2;

        _SET_HTON32(&Buf[done], V.AdminBaseTime.Seconds);
        done        += 4;

        _SET_HTON32(&Buf[done], V.AdminBaseTime.Nanoseconds);
        done        += 4;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlListLength:
    {_SET_HTON16(&Buf[done], V.AdminControlListLength);
        done        += 2;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminControlList:
    {
        WORD32      i;
        
        for (i = 0; i < MIN(V.AdminControlLists.AdminControlListNum, EPMS_ADMINCONTROLLIST_MAXNUM); i ++)
        {Buf[done++]     = V.AdminControlLists.AdminControlList[i].GateOperationName;
            Buf[done++]     = 5; 
            Buf[done++]     = V.AdminControlLists.AdminControlList[i].bGateState;
            _SET_HTON32(&Buf[done], V.AdminControlLists.AdminControlList[i].TimeInterval);
            done        += 4;
        }
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_AdminCycleTime:
    {_SET_HTON32(&Buf[done], V.AdminCycleTime.AdminCycleTimeNumerator);
        done        += 4;
        
        _SET_HTON32(&Buf[done], V.AdminCycleTime.AdminCycleTimeDenominator);
        done        += 4;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_TickGranularity:
    {_SET_HTON32(&Buf[done], V.TickGranularity.Value);
        done        += 4;
        break;
    }

    case EPMS_ETHERNET_PORT_PARAMETER_PSFPMaxStreamFilterInstances:
    {_SET_HTON32(&Buf[done], V.PSFPMaxStreamFilterInstances.Value);
        done        += 4;
        break;
    }

    default:
    {_TSC_TRACE("EthernetPortParameter not support! Name:0x%X\n", TL.Name);
        break;
    }
}

}

static void encode_EPMS_EthernetPortParameter(EPMS_EthernetPortParameterTL TL[], EPMS_EthernetPortParameterV &V, WORD32 num, BYTE *Buf, WORD32 &done)
{

WORD32      i;

for (i = 0; i < num; i ++)
{encode_EPMS_EthernetPortParameter(TL[i], V, Buf, done);
}

}

static BOOLEAN decode_EPMS_EthernetPortManagementCapability(const BYTE *Buf, WORD32 BufLen, WORD32 &done, EPMS_EthernetPortManagementCapability &Data)
{

WORD32      i;
      
/* Length of Ethernet port management capability contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

Data.SupportedParameterNum  = MIN(contentlen/sizeof(WORD16), EPMS_ETHERNETPORTMANAGEMENTCAPABILITY_MAXNUM);

/* Ethernet port management capability contents */
for (i = 0; i < Data.SupportedParameterNum; i ++)
{Data.SupportedParameter[i]  = _GET_NTOH16(&Buf[done]);
    done    += 2;
}

/* align content len */
done    += contentlen - Data.SupportedParameterNum*sizeof(WORD16);
return TRUE;

}

static BOOLEAN encode_EPMS_EthernetPortManagementCapability(EPMS_EthernetPortManagementCapability &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Ethernet port management capability IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_ETHERNETPORTMANAGEMENTCAPABILITY_IEI;
done        ++;

/* Length of Ethernet port management capability contents */
Data.SupportedParameterNum  = MIN(Data.SupportedParameterNum, EPMS_ETHERNETPORTMANAGEMENTCAPABILITY_MAXNUM);
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2+Data.SupportedParameterNum*2);
_SET_HTON16(&Buf[done], Data.SupportedParameterNum*2);
done        += 2;

/* Ethernet port management capability contents */
for (i = 0; i < Data.SupportedParameterNum; i ++)
{_SET_HTON16(&Buf[done], Data.SupportedParameter[i]);
    done        += 2;
}

return TRUE;

}

static BOOLEAN decode_EPMS_EthernetPortStatus(const BYTE *Buf, WORD32 BufLen, WORD32 &done, EPMS_EthernetPortStatus &Data)
{

WORD32      i;
      
/* Length of Ethernet port status and error contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

/* Number of Ethernet port parameters successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        succRead = Buf[done];
done        ++;
Data.SuccParameterNum   = MIN(succRead, EPMS_ETHERNETPORTSTATUS_SUCC_MAXNUM);

for (i = 0; i < succRead; i ++)
{if (i < Data.SuccParameterNum)
    {if (!decode_EPMS_EthernetPortParameter(Buf, BufLen, done, Data.SuccParameterTL[i], Data.SuccParameterV))
        {_TSC_TRACE("decode_EPMS_EthernetPortParameter fail!\n");
          return FALSE;
        }
    }
    else
    {if (!decode_EPMS_EthernetPortParameter(Buf, BufLen, done))
        {_TSC_TRACE("decode_EPMS_EthernetPortParameter fail!\n");
          return FALSE;
        }
    }
}

/* Number of Ethernet port parameters not successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        NotSuccRead = Buf[done];
done        ++;
Data.ErrorParameterNum  = MIN(NotSuccRead, EPMS_ETHERNETPORTSTATUS_ERROR_MAXNUM);

for (i = 0; i < NotSuccRead; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    WORD16      NotSuccParaName = _GET_NTOH16(&Buf[done]);
    done        += 2;
    BYTE        NotSuccCause = Buf[done];
    done        ++;
    
    if (i < Data.ErrorParameterNum)
    {Data.ErrorParameter[i]  = NotSuccParaName;
        Data.ErrorCause[i]      = NotSuccCause;
    }
}

return TRUE;

}

static BOOLEAN encode_EPMS_EthernetPortStatus(EPMS_EthernetPortStatus &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Ethernet port status IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_ETHERNETPORTSTATUS_IEI;
done        ++;

/* Length of Ethernet port status and error contents */
Data.SuccParameterNum      = MIN(Data.SuccParameterNum, EPMS_ETHERNETPORTSTATUS_SUCC_MAXNUM);
Data.ErrorParameterNum     = MIN(Data.ErrorParameterNum, EPMS_ETHERNETPORTSTATUS_ERROR_MAXNUM);
WORD16  statuslen = calc_msglen_EPMS_EthernetPortParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum);
statuslen   += 2+Data.ErrorParameterNum*3;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
_SET_HTON16(&Buf[done], statuslen);
done        += 2;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, statuslen);

/* Number of Ethernet port parameters successfully read */
Buf[done]   = Data.SuccParameterNum;
done        ++;

/* Ethernet port parameter status */
encode_EPMS_EthernetPortParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum, Buf, done);

/* Number of Ethernet port parameters not successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = Data.ErrorParameterNum;
done        ++;

/* Ethernet port parameter error */
for (i = 0; i < Data.ErrorParameterNum; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    _SET_HTON16(&Buf[done], Data.ErrorParameter[i]);
    done        += 2;

    Buf[done]   = Data.ErrorCause[i];
    done        ++;
}

return TRUE;

}

static BOOLEAN decode_EPMS_EthernetPortUpdateResult(const BYTE *Buf, WORD32 BufLen, WORD32 &done, EPMS_EthernetPortUpdateResult &Data)
{

WORD32      i;
      
/* Length of Ethernet port update and update error contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

/* Number of Ethernet port parameters successfully updated */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        succUpdate = Buf[done];
done        ++;
Data.SuccParameterNum   = MIN(succUpdate, EPMS_ETHERNETPORTSTATUS_SUCC_MAXNUM);

for (i = 0; i < succUpdate; i ++)
{if (i < Data.SuccParameterNum)
    {if (!decode_EPMS_EthernetPortParameter(Buf, BufLen, done, Data.SuccParameterTL[i], Data.SuccParameterV))
        {_TSC_TRACE("decode_EPMS_EthernetPortParameter fail!\n");
          return FALSE;
        }
    }
    else
    {if (!decode_EPMS_EthernetPortParameter(Buf, BufLen, done))
        {_TSC_TRACE("decode_EPMS_EthernetPortParameter fail!\n");
          return FALSE;
        }
    }
}

/* Number of Ethernet port parameters not updated successfully */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        NotSuccUpdate = Buf[done];
done        ++;
Data.ErrorParameterNum  = MIN(NotSuccUpdate, EPMS_ETHERNETPORTSTATUS_ERROR_MAXNUM);

for (i = 0; i < NotSuccUpdate; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    WORD16      NotSuccParaName = _GET_NTOH16(&Buf[done]);
    done        += 2;
    BYTE        NotSuccCause = Buf[done];
    done        ++;
    
    if (i < Data.ErrorParameterNum)
    {Data.ErrorParameter[i]  = NotSuccParaName;
        Data.ErrorCause[i]      = NotSuccCause;
    }
}

return TRUE;

}

static BOOLEAN encode_EPMS_EthernetPortUpdateResult(EPMS_EthernetPortUpdateResult &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Ethernet port update result IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_ETHERNETPORTUPDATERESULT_IEI;
done        ++;

/* Length of Ethernet port update and update error contents */
Data.SuccParameterNum   = MIN(Data.SuccParameterNum, EPMS_ETHERNETPORTUPDATERESULT_SUCC_MAXNUM);
Data.ErrorParameterNum  = MIN(Data.ErrorParameterNum, EPMS_ETHERNETPORTUPDATERESULT_ERROR_MAXNUM);
WORD16  updreslen = calc_msglen_EPMS_EthernetPortParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum);
updreslen   += 2+Data.ErrorParameterNum*3;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
_SET_HTON16(&Buf[done], updreslen);
done        += 2;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, updreslen);

/* Number of Ethernet port parameters successfully updated */
Buf[done]   = Data.SuccParameterNum;
done        ++;

/* Ethernet port parameter update */
encode_EPMS_EthernetPortParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum, Buf, done);

/* Number of Ethernet port parameters not updated successfully */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = Data.ErrorParameterNum;
done        ++;

/* Ethernet port parameter error */
for (i = 0; i < Data.ErrorParameterNum; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    _SET_HTON16(&Buf[done], Data.ErrorParameter[i]);
    done        += 2;

    Buf[done]   = Data.ErrorCause[i];
    done        ++;
}

return TRUE;

}

static BOOLEAN decode_MANAGE_ETHERNET_PORT_COMMAND(const BYTE *Buf, WORD32 BufLen, EPMS_RECV &Data)
{

WORD32      done = 1;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);

/* Ethernet port management list IEI */
done    ++;

/* Length of Ethernet port management list contents */
WORD16  listLen = _GET_NTOH16(&Buf[done]);
done    += 2;
_TSC_TRACE("Length of Ethernet port management list contents:%u\n", listLen);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, listLen);
BufLen = done + listLen;

/* Operations */
EPMS_EthernetPortManagementList &listData = Data.MANAGE_ETHERNET_PORT_COMMAND.EthernetPortManagementList;
for (listData.OperationNum = 0; listData.OperationNum < EPMS_ETHERNETPORTMANAGEMENTLISTOPERATION_MAXNUM; listData.OperationNum ++)
{if (done == BufLen)
    {_TSC_TRACE("deal succ.\n");
        return TRUE;
    }
    
    /* Operation code */
    _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    BYTE    optCode = Buf[done];
    listData.Operation[listData.OperationNum].OperationCode = optCode;
    done    ++;

    _TSC_TRACE("OperationCode:%u\n", optCode);
    
    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_GET_CAPABILITIES == optCode)
    {continue;}

    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_READ_PARAMETER == optCode
            || EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_SUBSCRIBE_NOTIFY_FOR_PARAMETER == optCode
            || EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_UNSUBSCRIBE_FOR_PARAMETER == optCode)
    {_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
        listData.Operation[listData.OperationNum].ParameterTL.Name  = _GET_NTOH16(&Buf[done]);
        done    += 2;
        _TSC_TRACE("Read/Subscribe/UnSubscribe Parameter Name:0x%X\n", listData.Operation[listData.OperationNum].ParameterTL.Name);
        continue;
    }

    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_SET_PARAMETER == optCode)
    {if (!decode_EPMS_EthernetPortParameter(Buf, BufLen, done, listData.Operation[listData.OperationNum].ParameterTL, listData.OpSetParameterV))
        {_TSC_TRACE("decode_EPMS_EthernetPortParameter fail!\n");
            return FALSE;
        }
        continue;
    }
    
    _TSC_TRACE("OperationCode not support!\n");
    return FALSE;
}

_TSC_TRACE("list max!\n");
return TRUE;

}

static BOOLEAN decode_MANAGE_ETHERNET_PORT_COMPLETE(const BYTE *Buf, WORD32 BufLen, EPMS_RECV &Data)
{

WORD32      done = 1;
BYTE        IEI;

while (done < BufLen)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    IEI         = Buf[done];
    done        ++;

    if (EPMS_ETHERNETPORTMANAGEMENTCAPABILITY_IEI == IEI)
    {if (!decode_EPMS_EthernetPortManagementCapability(Buf, BufLen, done, Data.MANAGE_ETHERNET_PORT_COMPLETE.Capability))
        {_TSC_TRACE("decode_EPMS_EthernetPortManagementCapability fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_ETHERNET_PORT_COMPLETE.hasCapability    = TRUE;
    }
    else if (EPMS_ETHERNETPORTSTATUS_IEI == IEI)
    {if (!decode_EPMS_EthernetPortStatus(Buf, BufLen, done, Data.MANAGE_ETHERNET_PORT_COMPLETE.Status))
        {_TSC_TRACE("decode_EPMS_EthernetPortStatus fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_ETHERNET_PORT_COMPLETE.hasStatus        = TRUE;
    }
    else if (EPMS_ETHERNETPORTUPDATERESULT_IEI == IEI)
    {if (!decode_EPMS_EthernetPortUpdateResult(Buf, BufLen, done, Data.MANAGE_ETHERNET_PORT_COMPLETE.UpdateResult))
        {_TSC_TRACE("decode_EPMS_EthernetPortUpdateResult fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_ETHERNET_PORT_COMPLETE.hasUpdateResult  = TRUE;
    }
    else
    {_TSC_TRACE("IEI not support!IEI:%u\n", IEI);
        return FALSE;
    }
};

_TSC_TRACE("deal succ. done:%u,BufLen:%u\n", done, BufLen);
return TRUE;

}

static BOOLEAN decode_ETHERNET_PORT_MANAGEMENT_NOTIFY(const BYTE *Buf, WORD32 BufLen, EPMS_RECV &Data)
{

WORD32      done = 1;

/* Ethernet port status IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
done        ++;

if (!decode_EPMS_EthernetPortStatus(Buf, BufLen, done, Data.ETHERNET_PORT_MANAGEMENT_NOTIFY.Status))
{_TSC_TRACE("decode_EPMS_EthernetPortStatus fail!\n");
    return FALSE;
}

_TSC_TRACE("deal succ.\n");
return TRUE;

}

static BOOLEAN decode_ETHERNET_PORT_MANAGEMENT_CAPABILITY(const BYTE *Buf, WORD32 BufLen, EPMS_RECV &Data)
{

WORD32      done = 1;

/* Ethernet port management capability IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
done        ++;

if (!decode_EPMS_EthernetPortManagementCapability(Buf, BufLen, done, Data.ETHERNET_PORT_MANAGEMENT_CAPABILITY.Capability))
{_TSC_TRACE("decode_EPMS_EthernetPortManagementCapability fail!\n");
    return FALSE;
}

_TSC_TRACE("deal succ.\n");
return TRUE;

}

/ EPMS /

/ BMS /
static BOOLEAN decode_BMS_BridgeParameter(const BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 4);

WORD16      name = _GET_NTOH16(&Buf[done]);
done        += 2;
WORD16      length = _GET_NTOH16(&Buf[done]);
done        += 2;

_TSC_TRACE("Parameter Name:0x%X, Length:%u\n", name, length);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, length);

done        += length;

return TRUE;

}

static BOOLEAN decode_BMS_BridgeParameter(const BYTE *Buf, WORD32 BufLen, WORD32 &done, BMS_BridgeParameterTL &TL, BMS_BridgeParameterV &V)
{

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 4);

TL.Name     = _GET_NTOH16(&Buf[done]);
done        += 2;
TL.Length   = _GET_NTOH16(&Buf[done]);
done        += 2;

_TSC_TRACE("Parameter Name:0x%X, Length:%u\n", TL.Name, TL.Length);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, TL.Length);

switch (TL.Name)
{
    case BMS_BRIDGE_PARAMETER_BridgeAddress:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 6);
        memcpy(V.BridgeAddress.MacAddress ,&Buf[done], 6);
        _TSC_TRACE("BridgeAddress:%X %X %X %X %X %X \n", 
                    V.BridgeAddress.MacAddress[0], V.BridgeAddress.MacAddress[1],
                    V.BridgeAddress.MacAddress[2], V.BridgeAddress.MacAddress[3],
                    V.BridgeAddress.MacAddress[4], V.BridgeAddress.MacAddress[5]);
        break;
    }

    case BMS_BRIDGE_PARAMETER_BridgeID:
    {_CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 8);

ifdef LITTLE_ENDIAN

        V.BridgeID.IDDetail.Priority    = (Buf[done] >> 4) & 0x0F;
        V.BridgeID.IDDetail.SystemID    = ((WORD16)(Buf[done] & 0x0F) << 8) | (WORD16)(Buf[done+1]);
        memcpy(V.BridgeID.IDDetail.MacAddress, &Buf[done+2], 6);

else

        memcpy(&V.BridgeID.ID, &Buf[done], 8);

endif

        _TSC_TRACE("BridgeID Priority:%u, SystemID:%u, MacAddress:%X %X %X %X %X %X \n",
                    V.BridgeID.IDDetail.Priority, V.BridgeID.IDDetail.SystemID,
                    V.BridgeID.IDDetail.MacAddress[0], V.BridgeID.IDDetail.MacAddress[1],
                    V.BridgeID.IDDetail.MacAddress[2], V.BridgeID.IDDetail.MacAddress[3],
                    V.BridgeID.IDDetail.MacAddress[4], V.BridgeID.IDDetail.MacAddress[5]);

        break;
    }

    case BMS_BRIDGE_PARAMETER_NWTTPortNumbers:
    {
        WORD32      offset = done;
        
        _CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 3);
        V.NWTTPortNumbers.IEI       = Buf[offset];
        offset      ++;
        V.NWTTPortNumbers.Length    = _GET_NTOH16(&Buf[offset]);
        V.NWTTPortNumbers.Num       = MIN(V.NWTTPortNumbers.Length/sizeof(WORD16), BMS_NWTTPORTNUMBERS_MAXNUM);
        offset      += 2;
        _TSC_TRACE("NWTTPortNumbers Length:%u, Num:%u\n", V.NWTTPortNumbers.Length, V.NWTTPortNumbers.Num);

        WORD32      i;
        for (i = 0; i < V.NWTTPortNumbers.Num; i ++)
        {_CHECK_BUFLEN_DEMAND_RETFALSE(3+i*sizeof(WORD16), TL.Length, sizeof(WORD16));
            V.NWTTPortNumbers.PortNumber[i] = _GET_NTOH16(&Buf[offset]);
            offset      += 2;
            _TSC_TRACE("NWTTPortNumbers PortNumber[%u]:%u\n", i, V.NWTTPortNumbers.PortNumber[i]);
        }
        break;
    }

    case BMS_BRIDGE_PARAMETER_StaticFilteringEntries:
    {
        WORD32      offset = done;
        
        _CHECK_BUFLEN_DEMAND_RETFALSE(0, TL.Length, 3);
        V.StaticFilteringEntries.IEI    = Buf[offset];
        offset      ++;
        V.StaticFilteringEntries.Length = _GET_NTOH16(&Buf[offset]);
        V.StaticFilteringEntries.Num    = MIN(V.StaticFilteringEntries.Length/sizeof(BMS_StaticFilteringEntry), BMS_STATICFILTERINGENTRY_MAXNUM);
        offset      += 2;
        _TSC_TRACE("StaticFilteringEntries Length:%u, Num:%u\n", V.StaticFilteringEntries.Length, V.StaticFilteringEntries.Num);

        WORD32      i;
        for (i = 0; i < V.StaticFilteringEntries.Num; i ++)
        {_CHECK_BUFLEN_DEMAND_RETFALSE(3+i*sizeof(BMS_StaticFilteringEntry), TL.Length, sizeof(BMS_StaticFilteringEntry));
            memcpy(V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress, &Buf[offset], 6);
            offset      += 6;
            V.StaticFilteringEntries.StaticFilteringEntry[i].VID    = _GET_NTOH16(&Buf[offset]);
            offset      += 2;
            V.StaticFilteringEntries.StaticFilteringEntry[i].Port   = _GET_NTOH16(&Buf[offset]);
            offset      += 2;
            _TSC_TRACE("StaticFilteringEntry[%u] MacAddress:%X %X %X %X %X %X , VID:%u, Port:%u\n", 
                        i, V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[0],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[1],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[2],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[3],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[4],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress[5],
                        V.StaticFilteringEntries.StaticFilteringEntry[i].VID,
                        V.StaticFilteringEntries.StaticFilteringEntry[i].Port);
        }
        break;
    }

    default:
    {_TSC_TRACE("BridgeParameter not support! Name:0x%X\n", TL.Name);
        break;
    }
}

done        += TL.Length;

return TRUE;

}

static WORD16 calc_msglen_BMS_BridgeParameter(BMS_BridgeParameterTL &TL, BMS_BridgeParameterV &V)
{

WORD16      msglen = 0;

/* Bridge parameter name */
/* Length of Bridge parameter value */
msglen  += 4;

switch (TL.Name)
{
    case BMS_BRIDGE_PARAMETER_BridgeAddress:
    {
        TL.Length   = 6;
        break;
    }

    case BMS_BRIDGE_PARAMETER_BridgeID:
    {
        TL.Length   = 8;
        break;
    }

    case BMS_BRIDGE_PARAMETER_NWTTPortNumbers:
    {    
        V.NWTTPortNumbers.IEI       = 0;
        V.NWTTPortNumbers.Num       = MIN(V.NWTTPortNumbers.Num, BMS_NWTTPORTNUMBERS_MAXNUM);
        V.NWTTPortNumbers.Length    = V.NWTTPortNumbers.Num * sizeof(WORD16);
        TL.Length                   = 3 + V.NWTTPortNumbers.Length;
        break;
    }

    case BMS_BRIDGE_PARAMETER_StaticFilteringEntries:
    {
        V.StaticFilteringEntries.IEI    = 0;
        V.StaticFilteringEntries.Num    = MIN(V.StaticFilteringEntries.Num, BMS_STATICFILTERINGENTRY_MAXNUM);
        V.StaticFilteringEntries.Length = V.StaticFilteringEntries.Num * sizeof(BMS_StaticFilteringEntry);
        TL.Length                       = 3 + V.StaticFilteringEntries.Length;
        break;
    }

    default:
    {_TSC_TRACE("calc_msglen_BMS_BridgeParameter not support! Name:0x%X\n", TL.Name);
        TL.Length   = 0;
        break;
    }
}

msglen  += TL.Length;

return msglen;

}

static WORD16 calc_msglen_BMS_BridgeParameter(BMS_BridgeParameterTL TL[], BMS_BridgeParameterV &V, WORD32 num)
{

WORD16      msglen = 0;
WORD32      i;

for (i = 0; i < num; i ++)
{msglen  += calc_msglen_BMS_BridgeParameter(TL[i], V);
}

return msglen;

}

static void encode_BMS_BridgeParameter(BMS_BridgeParameterTL &TL, BMS_BridgeParameterV &V, BYTE *Buf, WORD32 &done)
{

/* Bridge parameter name and length */
_SET_HTON16(&Buf[done], TL.Name);
done        += 2;
_SET_HTON16(&Buf[done], TL.Length);
done        += 2;

switch (TL.Name)
{
    case BMS_BRIDGE_PARAMETER_BridgeAddress:
    {memcpy(&Buf[done], V.BridgeAddress.MacAddress, 6);
        done        += 6;
        break;
    }

    case BMS_BRIDGE_PARAMETER_BridgeID:
    {

ifdef LITTLE_ENDIAN

        Buf[done]       = ((V.BridgeID.IDDetail.Priority << 4) & 0xF0) | ((V.BridgeID.IDDetail.SystemID >> 8) & 0x0F);
        Buf[done+1]     = V.BridgeID.IDDetail.SystemID & 0xFF;
        memcpy(&Buf[done+2], V.BridgeID.IDDetail.MacAddress, 6);

else

        memcpy(&Buf[done], &V.BridgeID.ID, 8);

endif

        done        += 8;
        break;
    }

    case BMS_BRIDGE_PARAMETER_NWTTPortNumbers:
    {Buf[done]   = V.NWTTPortNumbers.IEI;
        done        ++;
        _SET_HTON16(&Buf[done], V.NWTTPortNumbers.Length);
        done        += 2;

        WORD32      i;
        for (i = 0; i < MIN(V.NWTTPortNumbers.Num, BMS_NWTTPORTNUMBERS_MAXNUM); i ++)
        {_SET_HTON16(&Buf[done], V.NWTTPortNumbers.PortNumber[i]);
            done        += 2;
        }   
        break;
    }

    case BMS_BRIDGE_PARAMETER_StaticFilteringEntries:
    {Buf[done]   = V.StaticFilteringEntries.IEI;
        done        ++;
        _SET_HTON16(&Buf[done], V.StaticFilteringEntries.Length);
        done        += 2;

        WORD32      i;
        for (i = 0; i < MIN(V.StaticFilteringEntries.Num, BMS_STATICFILTERINGENTRY_MAXNUM); i ++)
        {memcpy(&Buf[done], V.StaticFilteringEntries.StaticFilteringEntry[i].MacAddress, 6);
            done        += 6;
            _SET_HTON16(&Buf[done],  V.StaticFilteringEntries.StaticFilteringEntry[i].VID);
            done        += 2;
            _SET_HTON16(&Buf[done],  V.StaticFilteringEntries.StaticFilteringEntry[i].Port);
            done        += 2;
        }
        break;
    }

    default:
    {_TSC_TRACE("encode_BMS_BridgeParameter not support! Name:0x%X\n", TL.Name);
        break;
    }
}

}

static void encode_BMS_BridgeParameter(BMS_BridgeParameterTL TL[], BMS_BridgeParameterV &V, WORD32 num, BYTE *Buf, WORD32 &done)
{

WORD32      i;

for (i = 0; i < num; i ++)
{encode_BMS_BridgeParameter(TL[i], V, Buf, done);
}

}

static BOOLEAN decode_BMS_BridgeManagementCapability(const BYTE *Buf, WORD32 BufLen, WORD32 &done, BMS_BridgeManagementCapability &Data)
{

WORD32      i;
      
/* Length of Bridge management capability contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

Data.SupportedParameterNum  = MIN(contentlen/sizeof(WORD16), BMS_BRIDGEMANAGEMENTCAPABILITY_MAXNUM);

/* Bridge management capability contents */
for (i = 0; i < Data.SupportedParameterNum; i ++)
{Data.SupportedParameter[i]  = _GET_NTOH16(&Buf[done]);
    done    += 2;
}

/* align content len */
done    += contentlen - Data.SupportedParameterNum*sizeof(WORD16);
return TRUE;

}

static BOOLEAN encode_BMS_BridgeManagementCapability(BMS_BridgeManagementCapability &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Bridge management capability IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_BRIDGEMANAGEMENTCAPABILITY_IEI;
done        ++;

/* Length of Bridge management capability contents */
Data.SupportedParameterNum  = MIN(Data.SupportedParameterNum, BMS_BRIDGEMANAGEMENTCAPABILITY_MAXNUM);
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2+Data.SupportedParameterNum*2);
_SET_HTON16(&Buf[done], Data.SupportedParameterNum*2);
done        += 2;

/* Bridge management capability contents */
for (i = 0; i < Data.SupportedParameterNum; i ++)
{_SET_HTON16(&Buf[done], Data.SupportedParameter[i]);
    done        += 2;
}

return TRUE;

}

static BOOLEAN decode_BMS_BridgeStatus(const BYTE *Buf, WORD32 BufLen, WORD32 &done, BMS_BridgeStatus &Data)
{

WORD32      i;
      
/* Length of Bridge status and error contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

/* Number of Bridge parameters successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        succRead = Buf[done];
done        ++;
Data.SuccParameterNum   = MIN(succRead, BMS_BRIDGESTATUS_SUCC_MAXNUM);

for (i = 0; i < succRead; i ++)
{if (i < Data.SuccParameterNum)
    {if (!decode_BMS_BridgeParameter(Buf, BufLen, done, Data.SuccParameterTL[i], Data.SuccParameterV))
        {_TSC_TRACE("decode_BMS_BridgeParameter fail!\n");
          return FALSE;
        }
    }
    else
    {if (!decode_BMS_BridgeParameter(Buf, BufLen, done))
        {_TSC_TRACE("decode_BMS_BridgeParameter fail!\n");
          return FALSE;
        }
    }
}

/* Number of Bridge parameters not successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        NotSuccRead = Buf[done];
done        ++;
Data.ErrorParameterNum  = MIN(NotSuccRead, BMS_BRIDGESTATUS_ERROR_MAXNUM);

for (i = 0; i < NotSuccRead; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    WORD16      NotSuccParaName = _GET_NTOH16(&Buf[done]);
    done        += 2;
    BYTE        NotSuccCause = Buf[done];
    done        ++;
    
    if (i < Data.ErrorParameterNum)
    {Data.ErrorParameter[i]  = NotSuccParaName;
        Data.ErrorCause[i]      = NotSuccCause;
    }
}

return TRUE;

}

static BOOLEAN encode_BMS_BridgeStatus(BMS_BridgeStatus &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Bridge status IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_BRIDGESTATUS_IEI;
done        ++;

/* Length of Bridge status and error contents */
Data.SuccParameterNum      = MIN(Data.SuccParameterNum, BMS_BRIDGESTATUS_SUCC_MAXNUM);
Data.ErrorParameterNum     = MIN(Data.ErrorParameterNum, BMS_BRIDGESTATUS_ERROR_MAXNUM);
WORD16  statuslen = calc_msglen_BMS_BridgeParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum);
statuslen   += 2+Data.ErrorParameterNum*3;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
_SET_HTON16(&Buf[done], statuslen);
done        += 2;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, statuslen);

/* Number of Bridge parameters successfully read */
Buf[done]   = Data.SuccParameterNum;
done        ++;

/* Bridge parameter status */
encode_BMS_BridgeParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum, Buf, done);

/* Number of Bridge parameters not successfully read */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = Data.ErrorParameterNum;
done        ++;

/* Bridge parameter error */
for (i = 0; i < Data.ErrorParameterNum; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    _SET_HTON16(&Buf[done], Data.ErrorParameter[i]);
    done        += 2;

    Buf[done]   = Data.ErrorCause[i];
    done        ++;
}

return TRUE;

}

static BOOLEAN decode_BMS_BridgeUpdateResult(const BYTE *Buf, WORD32 BufLen, WORD32 &done, BMS_BridgeUpdateResult &Data)
{

WORD32      i;
      
/* Length of Bridge update and update error contents */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
WORD16      contentlen = _GET_NTOH16(&Buf[done]);
done        += 2;
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, contentlen);

/* Number of Bridge parameters successfully updated */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        succUpdate = Buf[done];
done        ++;
Data.SuccParameterNum   = MIN(succUpdate, BMS_BRIDGESTATUS_SUCC_MAXNUM);

for (i = 0; i < succUpdate; i ++)
{if (i < Data.SuccParameterNum)
    {if (!decode_BMS_BridgeParameter(Buf, BufLen, done, Data.SuccParameterTL[i], Data.SuccParameterV))
        {_TSC_TRACE("decode_BMS_BridgeParameter fail!\n");
          return FALSE;
        }
    }
    else
    {if (!decode_BMS_BridgeParameter(Buf, BufLen, done))
        {_TSC_TRACE("decode_BMS_BridgeParameter fail!\n");
          return FALSE;
        }
    }
    
}

/* Number of Bridge parameters not updated successfully */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
BYTE        NotSuccUpdate = Buf[done];
done        ++;
Data.ErrorParameterNum  = MIN(NotSuccUpdate, BMS_BRIDGESTATUS_ERROR_MAXNUM);

for (i = 0; i < NotSuccUpdate; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    WORD16      NotSuccParaName = _GET_NTOH16(&Buf[done]);
    done        += 2;
    BYTE        NotSuccCause = Buf[done];
    done        ++;
    
    if (i < Data.ErrorParameterNum)
    {Data.ErrorParameter[i]  = NotSuccParaName;
        Data.ErrorCause[i]      = NotSuccCause;
    }
}

return TRUE;

}

static BOOLEAN encode_BMS_BridgeUpdateResult(BMS_BridgeUpdateResult &Data, BYTE *Buf, WORD32 BufLen, WORD32 &done)
{

WORD32      i;

/* Bridge update result IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_BRIDGEUPDATERESULT_IEI;
done        ++;

/* Length of Bridge update and update error contents */
Data.SuccParameterNum   = MIN(Data.SuccParameterNum, BMS_BRIDGEUPDATERESULT_SUCC_MAXNUM);
Data.ErrorParameterNum  = MIN(Data.ErrorParameterNum, BMS_BRIDGEUPDATERESULT_ERROR_MAXNUM);
WORD16  updreslen = calc_msglen_BMS_BridgeParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum);
updreslen   += 2 + Data.ErrorParameterNum * 3;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
_SET_HTON16(&Buf[done], updreslen);
done        += 2;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, updreslen);

/* Number of Bridge parameters successfully updated */
Buf[done]   = Data.SuccParameterNum;
done        ++;

/* Bridge parameter update */
encode_BMS_BridgeParameter(Data.SuccParameterTL, Data.SuccParameterV, Data.SuccParameterNum, Buf, done);

/* Number of Bridge parameters not updated successfully */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = Data.ErrorParameterNum;
done        ++;

/* Bridge parameter error */
for (i = 0; i < Data.ErrorParameterNum; i ++)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
    
    _SET_HTON16(&Buf[done], Data.ErrorParameter[i]);
    done        += 2;

    Buf[done]   = Data.ErrorCause[i];
    done        ++;
}

return TRUE;

}

static BOOLEAN decode_MANAGE_BRIDGE_COMMAND(const BYTE *Buf, WORD32 BufLen, BMS_RECV &Data)
{

WORD32      done = 1;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
_CHECK_TRUE_RETURN_VAL(BufLen > 65530, FALSE);

/* Bridge management list IEI */
done    ++;

/* Length of Bridge management list contents */
WORD16  listLen = _GET_NTOH16(&Buf[done]);
done    += 2;
_TSC_TRACE("Length of Bridge management list contents:%u\n", listLen);

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, listLen);
BufLen  = done + listLen;

/* Operations */
BMS_BridgeManagementList    &listData = Data.MANAGE_BRIDGE_COMMAND.BridgeManagementList;
for (listData.OperationNum = 0; listData.OperationNum < BMS_BRIDGEMANAGEMENTLISTOPERATION_MAXNUM; listData.OperationNum ++)
{if (done == BufLen)
    {_TSC_TRACE("deal succ.\n");
        return TRUE;
    }
    
    /* Operation code */
    _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    BYTE    optCode = Buf[done];
    listData.Operation[listData.OperationNum].OperationCode = optCode;
    done    ++;

    _TSC_TRACE("OperationCode:%u\n", optCode);
    
    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_GET_CAPABILITIES == optCode)
    {continue;}

    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_READ_PARAMETER == optCode
            || BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_SUBSCRIBE_NOTIFY_FOR_PARAMETER == optCode
            || BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_UNSUBSCRIBE_FOR_PARAMETER == optCode)
    {_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
        listData.Operation[listData.OperationNum].ParameterTL.Name  = _GET_NTOH16(&Buf[done]);
        done    += 2;
        _TSC_TRACE("Read/Subscribe/UnSubscribe Parameter Name:0x%X\n", listData.Operation[listData.OperationNum].ParameterTL.Name);
        continue;
    }

    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_SET_PARAMETER == optCode)
    {if (!decode_BMS_BridgeParameter(Buf, BufLen, done, listData.Operation[listData.OperationNum].ParameterTL, listData.OpSetParameterV))
        {_TSC_TRACE("decode_BMS_BridgeParameter fail!\n");
            return FALSE;
        }
        continue;
    }
    
    _TSC_TRACE("OperationCode not support!\n");
    return FALSE;
}

_TSC_TRACE("list max!\n");
return TRUE;

}

static BOOLEAN decode_MANAGE_BRIDGE_COMPLETE(const BYTE *Buf, WORD32 BufLen, BMS_RECV &Data)
{

WORD32      done = 1;
BYTE        IEI;

while (done < BufLen)
{_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    IEI         = Buf[done];
    done        ++;

    if (BMS_BRIDGEMANAGEMENTCAPABILITY_IEI == IEI)
    {if (!decode_BMS_BridgeManagementCapability(Buf, BufLen, done, Data.MANAGE_BRIDGE_COMPLETE.Capability))
        {_TSC_TRACE("decode_BMS_BridgeManagementCapability fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_BRIDGE_COMPLETE.hasCapability   = TRUE;
    }
    else if (BMS_BRIDGESTATUS_IEI == IEI)
    {if (!decode_BMS_BridgeStatus(Buf, BufLen, done, Data.MANAGE_BRIDGE_COMPLETE.Status))
        {_TSC_TRACE("decode_BMS_BridgeStatus fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_BRIDGE_COMPLETE.hasStatus       = TRUE;
    }
    else if (BMS_BRIDGEUPDATERESULT_IEI == IEI)
    {if (!decode_BMS_BridgeUpdateResult(Buf, BufLen, done, Data.MANAGE_BRIDGE_COMPLETE.UpdateResult))
        {_TSC_TRACE("decode_BMS_BridgeUpdateResult fail!\n");
            return FALSE;
        }
        
        Data.MANAGE_BRIDGE_COMPLETE.hasUpdateResult = TRUE;
    }
    else
    {_TSC_TRACE("IEI not support!IEI:%u\n", IEI);
        return FALSE;
    }
};

_TSC_TRACE("deal succ. done:%u,BufLen:%u\n", done, BufLen);
return TRUE;

}

static BOOLEAN decode_BRIDGE_MANAGEMENT_NOTIFY(const BYTE *Buf, WORD32 BufLen, BMS_RECV &Data)
{

WORD32      done = 1;

/* Bridge status IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
done        ++;

if (!decode_BMS_BridgeStatus(Buf, BufLen, done, Data.BRIDGE_MANAGEMENT_NOTIFY.Status))
{_TSC_TRACE("decode_BMS_BridgeStatus fail!\n");
    return FALSE;
}

_TSC_TRACE("deal succ.\n");
return TRUE;

}

/ BMS /

extern “C” {

BOOLEAN Encode_EPMS_MANAGE_ETHERNET_PORT_COMMAND(EPMS_MANAGE_ETHERNET_PORT_COMMAND Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;
WORD32      i;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_MANAGE_ETHERNET_PORT_COMMAND;
done        ++;

/* Ethernet port management list IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
Buf[done]   = 0;
done        ++;

/* Length of Ethernet port management list contents */
WORD32      listLenOffset = done;
_SET_HTON16(&Buf[done], 0); /* set after process */
done        += 2;

/* Ethernet port management list contents */
EPMS_EthernetPortManagementList &listData = Data->EthernetPortManagementList;
listData.OperationNum   = MIN(listData.OperationNum, EPMS_ETHERNETPORTMANAGEMENTLISTOPERATION_MAXNUM);

for (i = 0; i < listData.OperationNum; i ++)
{
    /* Operation code */
    _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    BYTE    optCode = listData.Operation[i].OperationCode;
    Buf[done]   = optCode;
    done        ++;

    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_GET_CAPABILITIES == optCode)
    {continue;}
    
    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_READ_PARAMETER == optCode
            || EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_SUBSCRIBE_NOTIFY_FOR_PARAMETER == optCode
            || EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_UNSUBSCRIBE_FOR_PARAMETER == optCode)
    {_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
        _SET_HTON16(&Buf[done], listData.Operation[i].ParameterTL.Name);
        done    += 2;
        continue;
    }
    
    if (EPMS_ETHERNET_PORT_MANAGEMENT_LIST_OPTCODE_SET_PARAMETER == optCode)
    {WORD16  statuslen = calc_msglen_EPMS_EthernetPortParameter(listData.Operation[i].ParameterTL, listData.OpSetParameterV);
        _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, statuslen);

        encode_EPMS_EthernetPortParameter(listData.Operation[i].ParameterTL, listData.OpSetParameterV, Buf, done);
        continue;
    }
}

/* set real list contents */
_SET_HTON16(&Buf[listLenOffset], done-listLenOffset-2); 

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_EPMS_MANAGE_ETHERNET_PORT_COMPLETE(EPMS_MANAGE_ETHERNET_PORT_COMPLETE Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_MANAGE_ETHERNET_PORT_COMPLETE;
done        ++;

if (Data->hasCapability)
{if (!encode_EPMS_EthernetPortManagementCapability(Data->Capability, Buf, BufLen, done))
    {_TSC_TRACE("encode_EPMS_EthernetPortManagementCapability fail!\n");
        return FALSE;
    }  
}

if (Data->hasStatus)
{if (!encode_EPMS_EthernetPortStatus(Data->Status, Buf, BufLen, done))
    {_TSC_TRACE("encode_EPMS_EthernetPortStatus fail!\n");
        return FALSE;
    }
}

if (Data->hasUpdateResult)
{if (!encode_EPMS_EthernetPortUpdateResult(Data->UpdateResult, Buf, BufLen, done))
    {_TSC_TRACE("encode_EPMS_EthernetPortUpdateResult fail!\n");
        return FALSE;
    }
}

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY(EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY;
done        ++;

if (!encode_EPMS_EthernetPortStatus(Data->Status, Buf, BufLen, done))
{_TSC_TRACE("encode_EPMS_EthernetPortStatus fail!\n");
    return FALSE;
}

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY_ACK(EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY_ACK Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY_ACK;
done        ++;

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY_COMPLETE(EPMS_ETHERNET_PORT_MANAGEMENT_NOTIFY_COMPLETE Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY_COMPLETE;
done        ++;

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_EPMS_ETHERNET_PORT_MANAGEMENT_CAPABILITY(EPMS_ETHERNET_PORT_MANAGEMENT_CAPABILITY Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_CAPABILITY;
done        ++;

if (!encode_EPMS_EthernetPortManagementCapability(Data->Capability, Buf, BufLen, done))
{_TSC_TRACE("encode_EPMS_EthernetPortManagementCapability fail!\n");
    return FALSE;
}

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Decode_EPMS_RecvData(const BYTE Buf, WORD32 BufLen, EPMS_RECV Data)
{

_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);

_TSC_TRACE("recv EPMS\n");
printMsgBuf(Buf, BufLen);

memset(Data, 0, sizeof(EPMS_RECV));

/* message identity */
_CHECK_BUFLEN_DEMAND_RETFALSE(0, BufLen, 1);
Data->MessageIdentity   = Buf[0];
_TSC_TRACE("recv MessageIdentity:%u\n", Data->MessageIdentity);

switch (Buf[0])
{
    case EPMS_MESSAGE_TYPE_MANAGE_ETHERNET_PORT_COMMAND:
    {if (!decode_MANAGE_ETHERNET_PORT_COMMAND(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case EPMS_MESSAGE_TYPE_MANAGE_ETHERNET_PORT_COMPLETE:
    {if (!decode_MANAGE_ETHERNET_PORT_COMPLETE(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY:
    {if (!decode_ETHERNET_PORT_MANAGEMENT_NOTIFY(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY_ACK:
    case EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_NOTIFY_COMPLETE:
    {break;}

    case EPMS_MESSAGE_TYPE_ETHERNET_PORT_MANAGEMENT_CAPABILITY:
    {if (!decode_ETHERNET_PORT_MANAGEMENT_CAPABILITY(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    default:
    {_TSC_TRACE("recv EPMS MessageIdentity not support. MessageIdentity:%u\n", Buf[0]);
        return FALSE;
        break;
    }
}

_TSC_TRACE("deal succ.\n");
return TRUE;

}

BOOLEAN Encode_BMS_MANAGE_BRIDGE_COMMAND(BMS_MANAGE_BRIDGE_COMMAND Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;
WORD32      i;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_MESSAGE_TYPE_MANAGE_BRIDGE_COMMAND;
done        ++;

/* Bridge management list IEI */
_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 3);
Buf[done]   = 0;
done        ++;

/* Length of Bridge management list contents */
WORD32      listLenOffset = done;
_SET_HTON16(&Buf[done], 0); /* set after process */
done        += 2;

/* Bridge management list contents */
BMS_BridgeManagementList &listData = Data->BridgeManagementList;
listData.OperationNum   = MIN(listData.OperationNum, BMS_BRIDGEMANAGEMENTLISTOPERATION_MAXNUM);

for (i = 0; i < listData.OperationNum; i ++)
{
    /* Operation code */
    _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
    BYTE    optCode = listData.Operation[i].OperationCode;
    Buf[done]   = optCode;
    done        ++;

    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_GET_CAPABILITIES == optCode)
    {continue;}
    
    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_READ_PARAMETER == optCode
            || BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_SUBSCRIBE_NOTIFY_FOR_PARAMETER == optCode
            || BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_UNSUBSCRIBE_FOR_PARAMETER == optCode)
    {_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 2);
        _SET_HTON16(&Buf[done], listData.Operation[i].ParameterTL.Name);
        done    += 2;
        continue;
    }
    
    if (BMS_BRIDGE_MANAGEMENT_LIST_OPTCODE_SET_PARAMETER == optCode)
    {WORD16  statuslen = calc_msglen_BMS_BridgeParameter(listData.Operation[i].ParameterTL, listData.OpSetParameterV);
        _CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, statuslen);

        encode_BMS_BridgeParameter(listData.Operation[i].ParameterTL, listData.OpSetParameterV, Buf, done);
        continue;
    }
}

/* set real list contents */
_SET_HTON16(&Buf[listLenOffset], done-listLenOffset-2); 

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_BMS_MANAGE_BRIDGE_COMPLETE(BMS_MANAGE_BRIDGE_COMPLETE Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_MESSAGE_TYPE_MANAGE_BRIDGE_COMPLETE;
done        ++;

if (Data->hasCapability)
{if (!encode_BMS_BridgeManagementCapability(Data->Capability, Buf, BufLen, done))
    {_TSC_TRACE("encode_BMS_BridgeManagementCapability fail!\n");
        return FALSE;
    }  
}

if (Data->hasStatus)
{if (!encode_BMS_BridgeStatus(Data->Status, Buf, BufLen, done))
    {_TSC_TRACE("encode_BMS_BridgeStatus fail!\n");
        return FALSE;
    }
}

if (Data->hasUpdateResult)
{if (!encode_BMS_BridgeUpdateResult(Data->UpdateResult, Buf, BufLen, done))
    {_TSC_TRACE("encode_BMS_BridgeUpdateResult fail!\n");
        return FALSE;
    }
}

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_BMS_BRIDGE_MANAGEMENT_NOTIFY(BMS_BRIDGE_MANAGEMENT_NOTIFY Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_MESSAGE_TYPE_BRIDGE_MANAGEMENT_NOTIFY;
done        ++;

if (!encode_BMS_BridgeStatus(Data->Status, Buf, BufLen, done))
{_TSC_TRACE("encode_BMS_BridgeStatus fail!\n");
    return FALSE;
}

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Encode_BMS_BRIDGE_MANAGEMENT_NOTIFY_ACK(BMS_BRIDGE_MANAGEMENT_NOTIFY_ACK Data, BYTE Buf, WORD32 BufLen, WORD32 *MsgLen)
{

_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == MsgLen, FALSE);

/* message identity */
WORD32      done = 0;

_CHECK_BUFLEN_DEMAND_RETFALSE(done, BufLen, 1);
Buf[done]   = BMS_MESSAGE_TYPE_BRIDGE_MANAGEMENT_NOTIFY_ACK;
done        ++;

*MsgLen = done;

_TSC_TRACE("deal succ.\n");
printMsgBuf(Buf, *MsgLen);

return TRUE;

}

BOOLEAN Decode_BMS_RecvData(const BYTE Buf, WORD32 BufLen, BMS_RECV Data)
{

_CHECK_TRUE_RETURN_VAL(NULL == Buf, FALSE);
_CHECK_TRUE_RETURN_VAL(NULL == Data, FALSE);

_TSC_TRACE("recv BMS\n");
printMsgBuf(Buf, BufLen);

memset(Data, 0, sizeof(BMS_RECV));

/* message identity */
_CHECK_BUFLEN_DEMAND_RETFALSE(0, BufLen, 1);
Data->MessageIdentity   = Buf[0];
_TSC_TRACE("recv MessageIdentity:%u\n", Data->MessageIdentity);

switch (Buf[0])
{
    case BMS_MESSAGE_TYPE_MANAGE_BRIDGE_COMMAND:
    {if (!decode_MANAGE_BRIDGE_COMMAND(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case BMS_MESSAGE_TYPE_MANAGE_BRIDGE_COMPLETE:
    {if (!decode_MANAGE_BRIDGE_COMPLETE(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case BMS_MESSAGE_TYPE_BRIDGE_MANAGEMENT_NOTIFY:
    {if (!decode_BRIDGE_MANAGEMENT_NOTIFY(Buf, BufLen, *Data))
        {_TSC_TRACE("decode fail!\n");
            return FALSE;
        }
        break;
    }

    case BMS_MESSAGE_TYPE_BRIDGE_MANAGEMENT_NOTIFY_ACK:
    {break;}

    default:
    {_TSC_TRACE("recv BMS MessageIdentity not support. MessageIdentity:%u\n", Buf[0]);
        return FALSE;
        break;
    }
}

_TSC_TRACE("deal succ.\n");
return TRUE;

}

void tscopen(void)
{

dbg_TSCPrt    = TRUE;

}

void tscclose(void)
{

dbg_TSCPrt    = FALSE;

}

}

正文完
 0