mirror of
https://gitee.com/const-zpc/param.git
synced 2025-01-30 04:32:52 +08:00
✨ feat: 可以配置裁剪掉64位长度参数的选项
This commit is contained in:
parent
b90d2d2297
commit
6f16527e64
@ -28,6 +28,12 @@
|
||||
*/
|
||||
#define PARAM_USE_STRING_TYPE 1
|
||||
|
||||
/**
|
||||
* @brief 使用64位的参数类型
|
||||
* 0,禁止; 1,使能
|
||||
*/
|
||||
#define PARAM_USE_64_BIT_LENGTH 1
|
||||
|
||||
/** 参数名字最大定义长度(包括结束符'\0'), 因此小于或等于1则禁用参数名字字符串相关功能 */
|
||||
#define PARAM_NAME_MAX_LENGTH 15
|
||||
|
||||
|
@ -58,13 +58,19 @@ typedef enum
|
||||
PARAM_INT8 = 0,
|
||||
PARAM_INT16,
|
||||
PARAM_INT32,
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_INT64,
|
||||
#endif
|
||||
PARAM_UINT8,
|
||||
PARAM_UINT16,
|
||||
PARAM_UINT32,
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_UINT64,
|
||||
#endif
|
||||
PARAM_FLOAT,
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_DOUBLE,
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
PARAM_STRING,
|
||||
#endif
|
||||
@ -74,13 +80,19 @@ typedef union {
|
||||
PARAM_INT8_T *pInt8;
|
||||
PARAM_INT16_T *pInt16;
|
||||
PARAM_INT32_T *pInt32;
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_INT64_T *pInt64;
|
||||
#endif
|
||||
PARAM_UINT8_T *pUint8;
|
||||
PARAM_UINT16_T *pUint16;
|
||||
PARAM_UINT32_T *pUint32;
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_UINT64_T *pUint64;
|
||||
#endif
|
||||
PARAM_FLOAT_T *pFloat;
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_DOUBLE_T *pDouble;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
PARAM_STRING_T *pString;
|
||||
param_size_t *pStringLength;
|
||||
|
@ -444,7 +444,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_INT64:
|
||||
if (pval->s64val < *param->unMinValuePtr.pInt64)
|
||||
{
|
||||
@ -455,7 +455,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_UINT8:
|
||||
if (pval->u64val < *param->unMinValuePtr.pUint8)
|
||||
{
|
||||
@ -488,7 +488,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_UINT64:
|
||||
if (pval->u64val < *param->unMinValuePtr.pUint64)
|
||||
{
|
||||
@ -499,7 +499,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_FLOAT:
|
||||
if (pval->fVal < *param->unMinValuePtr.pFloat)
|
||||
{
|
||||
@ -510,7 +510,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_DOUBLE:
|
||||
if (pval->fVal < *param->unMinValuePtr.pDouble)
|
||||
{
|
||||
@ -521,6 +521,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
case PARAM_STRING:
|
||||
if (strlen(pval->str) < *param->unMinValuePtr.pStringLength)
|
||||
@ -557,11 +558,11 @@ static int ValidateRangeByVoid(const ParamInfo_t *param, const void *pval)
|
||||
case PARAM_INT32:
|
||||
uValue.s64val = *(PARAM_INT32_T *)pval;
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_INT64:
|
||||
uValue.s64val = *(PARAM_INT64_T *)pval;
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_UINT8:
|
||||
uValue.s64val = *(PARAM_UINT8_T *)pval;
|
||||
break;
|
||||
@ -573,18 +574,19 @@ static int ValidateRangeByVoid(const ParamInfo_t *param, const void *pval)
|
||||
case PARAM_UINT32:
|
||||
uValue.s64val = *(PARAM_UINT32_T *)pval;
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_UINT64:
|
||||
uValue.s64val = *(PARAM_UINT64_T *)pval;
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_FLOAT:
|
||||
uValue.fVal = *(PARAM_FLOAT_T *)pval;
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_DOUBLE:
|
||||
uValue.fVal = *(PARAM_DOUBLE_T *)pval;
|
||||
break;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
case PARAM_STRING:
|
||||
memcpy(uValue.str, pval, strlen(pval) > PARAM_STRING_MAX_LENGTH ? PARAM_STRING_MAX_LENGTH + 2 : strlen(pval) + 1);
|
||||
@ -752,7 +754,7 @@ static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf)
|
||||
*param->unCurValuePtr.pInt32 = (PARAM_INT32_T)val;
|
||||
}
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_INT64:
|
||||
{
|
||||
int64_t val = 0;
|
||||
@ -760,7 +762,7 @@ static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf)
|
||||
*param->unCurValuePtr.pInt64 = (PARAM_INT64_T)val;
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_UINT8:
|
||||
{
|
||||
uint64_t val = 0;
|
||||
@ -784,7 +786,7 @@ static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf)
|
||||
*param->unCurValuePtr.pUint32= (PARAM_UINT32_T)val;
|
||||
}
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_UINT64:
|
||||
{
|
||||
uint64_t val = 0;
|
||||
@ -792,14 +794,15 @@ static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf)
|
||||
*param->unCurValuePtr.pUint64 = (PARAM_UINT64_T)val;
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_FLOAT:
|
||||
pbuf = UnSerializeFloat(pbuf, param->unCurValuePtr.pFloat);
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_DOUBLE:
|
||||
pbuf = UnSerializeDouble(pbuf, param->unCurValuePtr.pDouble);
|
||||
break;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
case PARAM_STRING:
|
||||
memcpy(param->unCurValuePtr.pString, &pbuf[0], PARAM_STRING_MAX_LENGTH);
|
||||
@ -963,11 +966,11 @@ static uint16_t ParamInfoToStream(uint8_t *pbuf, ParamInfo_t *param)
|
||||
case PARAM_INT32:
|
||||
pbuf = SerializeInt(pbuf, *(PARAM_INT32_T *)param->unCurValuePtr.pVoid, param->length);
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_INT64:
|
||||
pbuf = SerializeInt(pbuf, *(PARAM_INT64_T *)param->unCurValuePtr.pVoid, param->length);
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_UINT8:
|
||||
pbuf = SerializeUint(pbuf, *(PARAM_UINT8_T *)param->unCurValuePtr.pVoid, param->length);
|
||||
break;
|
||||
@ -979,18 +982,19 @@ static uint16_t ParamInfoToStream(uint8_t *pbuf, ParamInfo_t *param)
|
||||
case PARAM_UINT32:
|
||||
pbuf = SerializeUint(pbuf, *(PARAM_UINT32_T *)param->unCurValuePtr.pVoid, param->length);
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_UINT64:
|
||||
pbuf = SerializeUint(pbuf, *(PARAM_UINT64_T *)param->unCurValuePtr.pVoid, param->length);
|
||||
break;
|
||||
|
||||
#endif
|
||||
case PARAM_FLOAT:
|
||||
pbuf = SerializeFloat(pbuf, *(PARAM_FLOAT_T *)param->unCurValuePtr.pVoid);
|
||||
break;
|
||||
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
case PARAM_DOUBLE:
|
||||
pbuf = SerializeDouble(pbuf, *(PARAM_DOUBLE_T *)param->unCurValuePtr.pVoid);
|
||||
break;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
case PARAM_STRING:
|
||||
memcpy(&pbuf[0], param->unCurValuePtr.pString, PARAM_STRING_MAX_LENGTH);
|
||||
|
@ -7,36 +7,48 @@
|
||||
PARAM_DEFINE_DAT (g_test_1, PARAM_INT16, 10);
|
||||
PARAM_DEFINE_DAT_DEF (g_test_2, PARAM_UINT16, 20);
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_float, PARAM_FLOAT, 3.15, -10, 10);
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_double, PARAM_DOUBLE, 3.15, -10, 10);
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
PARAM_DEFINE_STR_RANGE (g_test_str, 10, "abcdef", 5);
|
||||
#endif
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_s8, PARAM_INT8, 10, -10, 15);
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_s16, PARAM_INT16, 100, -100, 3000);
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_s32, PARAM_INT32, 1000, -900, 10000);
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_s64, PARAM_INT64, 8000, -100, 1000000);
|
||||
#endif
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_u8, PARAM_UINT8, 10, 5, 15);
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_u16, PARAM_UINT16, 100, 100, 3000);
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_u32, PARAM_UINT32, 1000, 900, 10000);
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_DEFINE_DAT_RANGE (g_test_u64, PARAM_UINT64, 8000, 100, 1000000);
|
||||
#endif
|
||||
|
||||
|
||||
ParamInfo_t sg_ParamTable[] = {
|
||||
PARAM_ITEM_DAT(1, g_test_1, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_DEF(2, g_test_2, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_RANGE(3, g_test_float, PARAM_ATTR_READ),
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_ITEM_DAT_RANGE(4, g_test_double, PARAM_ATTR_WR),
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
PARAM_ITEM_STR_RANGE(5, g_test_str, PARAM_ATTR_WR),
|
||||
#endif
|
||||
PARAM_ITEM_DAT_RANGE(6, g_test_s8, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_RANGE(7, g_test_s16, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_RANGE(8, g_test_s32, PARAM_ATTR_WR),
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_ITEM_DAT_RANGE(9, g_test_s64, PARAM_ATTR_WR),
|
||||
#endif
|
||||
PARAM_ITEM_DAT_RANGE(10, g_test_u8, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_RANGE(11, g_test_u16, PARAM_ATTR_WR),
|
||||
PARAM_ITEM_DAT_RANGE(12, g_test_u32, PARAM_ATTR_NONE),
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
PARAM_ITEM_DAT_RANGE(13, g_test_u64, PARAM_ATTR_WR),
|
||||
#endif
|
||||
};
|
||||
|
||||
static ParamManager_t sg_tParamManager;
|
||||
@ -103,6 +115,7 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unMaxValuePtr.pVoid , &PARAM_DAT_MAX_VALUE(g_test_float));
|
||||
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].id, 5);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].length, 10);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].type, PARAM_STRING);
|
||||
@ -112,6 +125,18 @@ void test_ParamInit(void)
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unDefValuePtr.pVoid , PARAM_STR_DEF_VALUE(g_test_str));
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unMinValuePtr.pVoid , &PARAM_STR_MIN_LENGTH(g_test_str));
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unMaxValuePtr.pVoid , &PARAM_STR_MAX_LENGTH(g_test_str));
|
||||
#else
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].id, 5);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].length, 10);
|
||||
TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].type, PARAM_STRING);
|
||||
TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[3].attr, PARAM_ATTR_WR | PARAM_ATTR_RESET | PARAM_ATTR_RANGE);
|
||||
TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[3].pszName, "g_test_str");
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unCurValuePtr.pVoid , PARAM_STR_CUR_VALUE(g_test_str));
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unDefValuePtr.pVoid , PARAM_STR_DEF_VALUE(g_test_str));
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unMinValuePtr.pVoid , &PARAM_STR_MIN_LENGTH(g_test_str));
|
||||
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unMaxValuePtr.pVoid , &PARAM_STR_MAX_LENGTH(g_test_str));
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -219,7 +244,9 @@ void test_SaveAndLoadParam(void)
|
||||
g_test_1 = 60;
|
||||
g_test_2 = 40;
|
||||
g_test_float = -20;
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
g_test_double = 5.36;
|
||||
#endif
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
sprintf(g_test_str, "12a123");
|
||||
#endif
|
||||
@ -264,24 +291,15 @@ void test_IterateParam(void)
|
||||
void test_FindParam(void)
|
||||
{
|
||||
const ParamInfo_t *paramInfo;
|
||||
|
||||
paramInfo = Param_FindParamByName(&sg_tParamManager, "g_test_s16");
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[6], paramInfo);
|
||||
#else
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[5], paramInfo);
|
||||
#if PARAM_USE_64_BIT_LENGTH
|
||||
paramInfo = Param_FindParamByName(&sg_tParamManager, "g_test_double");
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[3], paramInfo);
|
||||
#endif
|
||||
|
||||
|
||||
paramInfo = Param_FindParamByID(&sg_tParamManager, 3);
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo);
|
||||
|
||||
paramInfo = Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16);
|
||||
#if PARAM_USE_STRING_TYPE
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[10], paramInfo);
|
||||
#else
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[9], paramInfo);
|
||||
#endif
|
||||
paramInfo = Param_FindParamByParamPtr(&sg_tParamManager, &g_test_float);
|
||||
TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo);
|
||||
|
||||
paramInfo = Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u32);
|
||||
TEST_ASSERT_EQUAL_PTR(NULL, paramInfo);
|
||||
|
Loading…
x
Reference in New Issue
Block a user