以下示出C语言中更简洁的实现方式:
/* 获取var变量第bit位,编号从右至左 */
#define GET_BIT(var, bit) (((var) >> (bit)) & 0x1)
const CHAR* paSvrNames[] = {"_INTERNET", "_TR069", "_VOIP", "_OTHER"};
const INT8U ucSvrNameNum = sizeof(paSvrNames) / sizeof(paSvrNames[0]);
VOID SetServerType(CHAR *pszSvrType, INT16U wSvrType)
{
INT8U ucIdx = 0;
for(; ucIdx < ucSvrNameNum; ucIdx++)
{
if(1 == GET_BIT(wSvrType, ucIdx))
strcat(pszSvrType, paSvrNames[ucIdx]);
}
}
新的实现将数据和逻辑分离,维护起来非常方便。只要逻辑(规则)不变,则唯一可能的改动就是数据(paSvrNames)。
1.2.4 值名解析
问题:根据枚举变量取值输出其对应的字符串,如PORT_FE(1)输出“Fe”。
//值名映射表结构体定义,用于数值解析器typedef struct{
INT32U dwElem; //待解析数值,通常为枚举变量
CHAR* pszName; //指向数值所对应解析名字符串的指针
}T_NAME_PARSER;
/******************************************************************************
* 函数名称: NameParser
* 功能说明: 数值解析器,将给定数值转换为对应的具名字符串
* 输入参数: VOID *pvMap :值名映射表数组,含T_NAME_PARSER结构体类型元素
VOID指针允许用户在保持成员数目和类型不变的前提下,
定制更有意义的结构体名和/或成员名。
INT32U dwEntryNum :值名映射表数组条目数
INT32U dwElem :待解析数值,通常为枚举变量
INT8U* pszDefName :缺省具名字符串指针,可为空
* 输出参数: NA
* 返回值 : INT8U *: 数值所对应的具名字符串
当无法解析给定数值时,若pszDefName为空,则返回数值对应的16进制格式
字符串;否则返回pszDefName。
******************************************************************************/
INT8U *NameParser(VOID *pvMap, INT32U dwEntryNum, INT32U dwElem, INT8U* pszDefName)
{
CHECK_SINGLE_POINTER(pvMap, "NullPoniter");
INT32U dwEntryIdx = 0;
for(dwEntryIdx = 0; dwEntryIdx < dwEntryNum; dwEntryIdx++)
{
T_NAME_PARSER *ptNameParser = (T_NAME_PARSER *)pvMap;
if(dwElem == ptNameParser->dwElem)
{
return ptNameParser->pszName;
}
//ANSI标准禁止对void指针进行算法操作;GNU标准则指定void*算法操作与char*一致。
//若考虑移植性,可将pvMap类型改为INT8U*,或定义INT8U*局部变量指向pvMap。
pvMap += sizeof(T_NAME_PARSER);
}
if(NULL != pszDefName)
{
return pszDefName;
}
else
{
static INT8U szName[12] = {0}; //Max:"0xFFFFFFFF"
sprintf(szName, "0x%X", dwElem);
return szName;
}
}
以下给出NameParser的简单应用示例:
//UNI端口类型值名映射表结构体定义
typedef struct{
INT32U dwPortType;
INT8U* pszPortName;
}T_PORT_NAME;
//UNI端口类型解析器
T_PORT_NAME gUniNameMap[] = {
{1, "Fe"},
{3, "Pots"},
{99, "Vuni"}
};
const INT32U UNI_NAM_MAP_NUM = (INT32U)(sizeof(gUniNameMap)/sizeof(T_PORT_NAME));
VOID NameParserTest(VOID)
{
INT8U ucTestIndex = 1;
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("Unknown", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 0, "Unknown")) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("DefName", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 0, "DefName")) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("Fe", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 1, "Unknown")) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("Pots", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 3, "Unknown")) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("Vuni", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 99, NULL)) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("Unknown", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 255, "Unknown")) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("0xABCD", NameParser(gUniNameMap, UNI_NAM_MAP_NUM, 0xABCD, NULL)) ? "ERROR" : "OK");
printf("[%s]<Test Case %u> Result: %s!\n", __FUNCTION__, ucTestIndex++,
strcmp("NullPoniter", NameParser(NULL, UNI_NAM_MAP_NUM, 0xABCD, NULL)) ? "ERROR" : "OK");
}
gUniNameMap在实际项目中有十余个条目,若采用逻辑链实现将非常冗长。
1.2.5 取值映射
问题:不同模块间同一参数枚举值取值可能有所差异,需要适配。
此处不再给出普通的switch…case或if…else if…else结构,而直接示出以下表驱动实现:
typedef struct{
PORTSTATE loopMEState;
PORTSTATE loopMIBState;
}LOOPMAPSTRUCT;
static LOOPMAPSTRUCT s_CesLoop[] = {
{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}};
PORTSTATE ConvertLoopMEStateToMIBState(PORTSTATE vPortState)
{
INT32U num = 0, ii;
num = ARRAY_NUM(s_CesLoop);
for(ii = 0; ii < num; ii++)
{
if(vPortState == s_CesLoop[ii].loopMEState)
return s_CesLoop[ii].loopMIBState;
}
return e_ds1_looptype_noloop;
}
相应地,从loopMIBState映射到loopMEState需要定义一个ConvertLoopMIBStateToMEState函数。更进一步,所有类似的一对一映射关系都必须如上的映射(转换)函数,相当繁琐。
事实上,从抽象层面看,该映射关系非常简单。提取共性后定义带参数宏,如下所示:
/**********************************************************
* 功能描述:进行二维数组映射表的一对一映射,用于参数适配
* 参数说明:map -- 二维数组映射表
elemSrc -- 映射源,即待映射的元素值
elemDest -- 映射源对应的映射结果
direction -- 映射方向字节,表示从数组哪列映射至哪列。
高4位对应映射源列,低4位对应映射结果列。
defaultVal -- 映射失败时置映射结果为缺省值
* 示例:ARRAY_MAPPER(gCesLoopMap, 3, ucLoop, 0x10, NO_LOOP);
则ucLoop = 2(LINE_LOOP)
**********************************************************/
#define ARRAY_MAPPER(map, elemSrc, elemDest, direction, defaultVal) do{\
INT8U ucMapIdx = 0, ucMapNum = 0; \
ucMapNum = sizeof(map)/sizeof(map[0]); \
for(ucMapIdx = 0; ucMapIdx < ucMapNum; ucMapIdx++) \
{ \
if((elemSrc) == map[ucMapIdx][((direction)&0xF0)>>4]) \
{ \
elemDest = map[ucMapIdx][(direction)&0x0F]; \
break; \
} \
} \
if(ucMapIdx == ucMapNum) \
{ \
elemDest = (defaultVal); \
} \
}while(0)
参数取值转换时直接调用统一的映射器宏,如下:
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;
}
1.2.6 版本控制
问题:控制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; |