参数取值转换时直接调用统一的映射器宏,如下: static INT8U gCesLoopMap[][2] = {
{NO_LOOP, e_ds1_looptype_noloop},
{PAYLOAD_LOOP, e_ds1_looptype_PayloadLoop},
{LINE_LOOP, e_ds1_looptype_LineLoop},
{PON_LOOP, e_ds1_looptype_OtherLoop},
{CES_LOOP, e_ds1_looptype_InwardLoop}};
ARRAY_MAPPER(gCesLoopMap, tPara.dwParaVal[0], dwLoopConf, 0x01, e_ds1_looptype_noloop);
另举一例: #define CES_DEFAULT_JITTERBUF (INT32U)2000 /* 默认jitterbuf为2000us,而1帧=125us */
#define CES_JITTERBUF_STEP (INT32U)125 /* jitterbuf步长为125us,即1帧 */
#define CES_DEFAULT_QUEUESIZE (INT32U)5
#define CES_DEFAULT_MAX_QUEUESIZE (INT32U)7
#define ARRAY_NUM(array) (sizeof(array) / sizeof((array)[0])) /* 数组元素个数 */
typedef struct{
INT32U dwJitterBuffer;
INT32U dwFramePerPkt;
INT32U dwQueueSize;
}QUEUE_SIZE_MAP;
/* gCesQueueSizeMap也可以(JitterBuffer / FramePerPkt)值为索引,更加紧凑 */
static QUEUE_SIZE_MAP gCesQueueSizeMap[]= {
{1,1,1}, {1,2,1}, {2,1,2}, {2,2,1},
{3,1,3}, {3,2,1}, {4,1,3}, {4,2,1},
{5,1,4}, {5,2,3}, {6,1,4}, {6,2,3},
{7,1,4}, {7,2,3}, {8,1,4}, {8,2,3},
{9,1,5}, {9,2,4}, {10,1,5}, {10,2,4},
{11,1,5}, {11,2,4}, {12,1,5}, {12,2,4},
{13,1,5}, {13,2,4}, {14,1,5}, {14,2,4},
{15,1,5}, {15,2,4}, {16,1,5}, {16,2,4},
{17,1,6}, {17,2,5}, {18,1,6}, {18,2,5},
{19,1,6}, {19,2,5}, {20,1,6}, {20,2,5},
{21,1,6}, {21,2,5}, {22,1,6}, {22,2,5},
{23,1,6}, {23,2,5}, {24,1,6}, {24,2,5},
{25,1,6}, {25,2,5}, {26,1,6}, {26,2,5},
{27,1,6}, {27,2,5}, {28,1,6}, {28,2,5},
{29,1,6}, {29,2,5}, {30,1,6}, {30,2,5},
{31,1,6}, {31,2,5}, {32,1,6}, {32,2,5}};
/**********************************************************
* 函数名称:CalcQueueSize
* 功能描述:根据JitterBuffer和FramePerPkt计算QueueSize
* 注意事项:配置的最大缓存深度
* = 2 * JitterBuffer / FramePerPkt
* = 2 * N Packet = 2 ^ QueueSize
* JitterBuffer为125us帧速率的倍数,
* FramePerPkt为每个分组的帧数,
* QueueSize向上取整,最大为7。
**********************************************************/
INT32U CalcQueueSize(INT32U dwJitterBuffer, INT32U dwFramePerPkt)
{
INT8U ucIdx = 0, ucNum = 0;
//本函数暂时仅考虑E1
ucNum = ARRAY_NUM(gCesQueueSizeMap);
for(ucIdx = 0; ucIdx < ucNum; ucIdx++)
{
if((dwJitterBuffer == gCesQueueSizeMap[ucIdx].dwJitterBuffer) &&
(dwFramePerPkt == gCesQueueSizeMap[ucIdx].dwFramePerPkt))
{
return gCesQueueSizeMap[ucIdx].dwQueueSize;
}
}
return CES_DEFAULT_MAX_QUEUESIZE;
}
版本控制问题:控制OLT与ONU之间的版本协商。ONU本地设置三比特控制字,其中bit2(MSB)~bit0(LSB)分别对应0x21、0x30和0xAA版本号;且bitX为0表示上报对应版本号,bitX为1表示不上报对应版本号。其他版本号如0x20、0x13和0x1必须上报,即不受控制。 最初的实现采用if…else if…else结构,代码非常冗长,如下: pstSendTlv->ucLength = 0x1f;
if (gOamCtrlCode == 0)
{
vosMemCpy(pstSendTlv->aucVersionList, ctc_oui, 3);
pstSendTlv->aucVersionList[3] = 0x30;
vosMemCpy(&(pstSendTlv->aucVersionList[4]), ctc_oui, 3);
pstSendTlv->aucVersionList[7] = 0x21;
vosMemCpy(&(pstSendTlv->aucVersionList[8]), ctc_oui, 3);
pstSendTlv->aucVersionList[11] = 0x20;
vosMemCpy(&(pstSendTlv->aucVersionList[12]), ctc_oui, 3);
pstSendTlv->aucVersionList[15] = 0x13;
vosMemCpy(&(pstSendTlv->aucVersionList[16]), ctc_oui, 3);
pstSendTlv->aucVersionList[19] = 0x01;
vosMemCpy(&(pstSendTlv->aucVersionList[20]), ctc_oui, 3);
pstSendTlv->aucVersionList[23] = 0xaa;
}
else if (gOamCtrlCode == 1)
{
vosMemCpy(pstSendTlv->aucVersionList, ctc_oui, 3);
pstSendTlv->aucVersionList[3] = 0x30;
vosMemCpy(&(pstSendTlv->aucVersionList[4]), ctc_oui, 3);
pstSendTlv->aucVersionList[7] = 0x21;
vosMemCpy(&(pstSendTlv->aucVersionList[8]), ctc_oui, 3);
pstSendTlv->aucVersionList[11] = 0x20;
vosMemCpy(&(pstSendTlv->aucVersionList[12]), ctc_oui, 3);
pstSendTlv->aucVersionList[15] = 0x13;
vosMemCpy(&(pstSendTlv->aucVersionList[16]), ctc_oui, 3);
pstSendTlv->aucVersionList[19] = 0x01;
}
//此处省略gOamCtrlCode == 2~6的处理代码
else if (gOamCtrlCode == 7)
{
vosMemCpy(&(pstSendTlv->aucVersionList), ctc_oui, 3);
pstSendTlv->aucVersionList[3] = 0x20;
vosMemCpy(&(pstSendTlv->aucVersionList[4]), ctc_oui, 3);
pstSendTlv->aucVersionList[7] = 0x13;
vosMemCpy(&(pstSendTlv->aucVersionList[8]), ctc_oui, 3);
pstSendTlv->aucVersionList[11] = 0x01;
}
以下示出C语言中更简洁的实现方式(基于二维数组): /**********************************************************************
* 版本控制字数组定义
* gOamCtrlCode: Bitmap控制字。Bit-X为0时上报对应版本,Bit-X为1时屏蔽对应版本。
* CTRL_VERS_NUM: 可控版本个数。
* CTRL_CODE_NUM: 控制字个数。与CTRL_VERS_NUM有关。
* gOamVerCtrlMap: 版本控制字数组。行对应控制字,列对应可控版本。
元素值为0时不上报对应版本,元素值非0时上报该元素值。
* Note: 该数组旨在实现“数据与控制隔离”。后续若要新增可控版本,只需修改
-- CTRL_VERS_NUM
-- gOamVerCtrlMap新增行(控制字)
-- gOamVerCtrlMap新增列(可控版本)
**********************************************************************/
#define CTRL_VERS_NUM 3
#define CTRL_CODE_NUM (1<<CTRL_VERS_NUM)
u8_t gOamVerCtrlMap[CTRL_CODE_NUM][CTRL_VERS_NUM] = {
/* Ver21 Ver30 VerAA */
{0x21, 0x30, 0xaa}, /*gOamCtrlCode = 0*/
{0x21, 0x30, 0 }, /*gOamCtrlCode = 1*/
{0x21, 0, 0xaa}, /*gOamCtrlCode = 2*/
{0x21, 0, 0 }, /*gOamCtrlCode = 3*/
{ 0, 0x30, 0xaa}, /*gOamCtrlCode = 4*/
{ 0, 0x30, 0 }, /*gOamCtrlCode = 5*/
{ 0, 0, 0xaa}, /*gOamCtrlCode = 6*/
{ 0, 0, 0 } /*gOamCtrlCode = 7*/
};
#define INFO_TYPE_VERS_LEN 7 /* InfoType + Length + OUI + ExtSupport + Version */
u8_t verIdx = 0;
u8_t index = 0;
for(verIdx = 0; verIdx < CTRL_VERS_NUM; verIdx++)
{
if(gOamVerCtrlMap[gOamCtrlCode][verIdx] != 0)
{
vosMemCpy(&pstSendTlv->aucVersionList[index], ctc_oui, 3);
index += 3;
pstSendTlv->aucVersionList[index++] = gOamVerCtrlMap[gOamCtrlCode][verIdx];
}
}
vosMemCpy(&pstSendTlv->aucVersionList[index], ctc_oui, 3);
index += 3;
pstSendTlv->aucVersionList[index++] = 0x20;
vosMemCpy(&pstSendTlv->aucVersionList[index], ctc_oui, 3);
index += 3;
pstSendTlv->aucVersionList[index++] = 0x13;
vosMemCpy(&pstSendTlv->aucVersionList[index], ctc_oui, 3);
index += 3;
pstSendTlv->aucVersionList[index++] = 0x01;
pstSendTlv->ucLength = INFO_TYPE_VERS_LEN + index;
消息处理问题:终端输入不同的打印命令,调用相应的打印函数,以控制不同级别的打印。 这是一段消息(事件)驱动程序。本模块接收其他模块(如串口驱动)发送的消息,根据消息中的打印级别字符串和开关模式,调用不同函数进行处理。常见的实现方法如下: void logall(void)
{
g_log_control[0] = 0xFFFFFFFF;
}
void noanylog(void)
{
g_log_control[0] = 0;
}
void **am(void)
{
g_log_control[0] |= (0x01 << FUNCTION_Oam);
}
void no**am(void)
{
g_log_control[0] &= ~(0x01 << FUNCTION_Oam);
}
//... ...
void logExec(char *name, INT8U enable)
{
CtcOamLog(FUNCTION_Oam,"log %s %d\n",name,enable);
if (enable == 1) /*log*/
{
if (strcasecmp(name,"all") == 0) { /*字符串比较,不区分大小写*/
logall();
} else if (strcasecmp(name,"oam") == 0) {
**am();
} else if (strcasecmp(name,"pon") == 0) {
logPon();
//... ...
} else if (strcasecmp(name,"version") == 0) {
logVersion();
}
else if (enable == 0) /*nolog*/
{
if (strcasecmp(name,"all") == 0) {
noanylog();
} else if (strcasecmp(name,"oam") == 0) {
no**am();
} else if (strcasecmp(name,"pon") == 0) {
nologPon();
//... ...
} else if (strcasecmp(name,"version") == 0) {
nologVersion();
}
else
{
printf("bad log para\n");
}
}
以下示出C语言中更简洁的实现方式: typedef struct{
OAM_LOG_OFF = (INT8U)0,
OAM_LOG_ON = (INT8U)1
}E_OAM_LOG_MODE;
typedef FUNC_STATUS (*OamLogHandler)(VOID);
typedef struct{
CHAR *pszLogCls; /* 打印级别 */
E_OAM_LOG_MODE eLogMode; /* 打印模式 */
OamLogHandler fnLogHandler; /* 打印函数 */
}T_OAM_LOG_MAP;
T_OAM_LOG_MAP gOamLogMap[] = {
{"all", OAM_LOG_OFF, noanylog},
{"oam", OAM_LOG_OFF, no**am},
//... ...
{"version", OAM_LOG_OFF, nologVersion},
{"all", OAM_LOG_ON, logall},
{"oam", OAM_LOG_ON, **am},
//... ...
{"version", OAM_LOG_ON, logVersion}
};
INT32U gOamLogMapNum = sizeof(gOamLogMap) / sizeof(T_OAM_LOG_MAP);
VOID logExec(CHAR *pszName, INT8U ucSwitch)
{
INT8U ucIdx = 0;
for(; ucIdx < gOamLogMapNum; ucIdx++)
{
if((ucSwitch == gOamLogMap[ucIdx].eLogMode) &&
(!strcasecmp(pszName, gOamLogMap[ucIdx].pszLogCls));
{
gOamLogMap[ucIdx].fnLogHandler();
return;
}
}
if(ucIdx == gOamLogMapNum)
{
printf("Unknown LogClass(%s) or LogMode(%d)!\n", pszName, ucSwitch);
return;
}
}
|