feat: 可以配置裁剪掉字符串参数类型的功能

This commit is contained in:
const_zpc@163.com 2023-06-18 00:23:28 +08:00
parent 5eddb6be6b
commit b90d2d2297
5 changed files with 85 additions and 19 deletions

View File

@ -64,6 +64,7 @@
const type##_T min_##name = minVal;\
const type##_T max_##name = maxVal;
#if PARAM_USE_STRING_TYPE
/**
* @brief
*
@ -101,6 +102,7 @@
const char def_##name[] = {defVal};\
const param_size_t min_##name = minLength;\
const param_size_t max_##name = length - 1;
#endif
#if (PARAM_NAME_MAX_LENGTH > 1)
/**
@ -139,7 +141,7 @@
.unDefValuePtr.pVoid = (void *)&def_##_name, \
.unMinValuePtr.pVoid = (void *)&min_##_name, \
.unMaxValuePtr.pVoid = (void *)&max_##_name}
#if PARAM_USE_STRING_TYPE
/**
* @brief
*
@ -176,7 +178,7 @@
.unDefValuePtr.pVoid = (void *)&def_##_name, \
.unMinValuePtr.pVoid = (void *)&min_##_name, \
.unMaxValuePtr.pVoid = (void *)&max_##_name}
#endif
#else
/**
@ -215,7 +217,7 @@
.unDefValuePtr.pVoid = (void *)&def_##_name, \
.unMinValuePtr.pVoid = (void *)&min_##_name, \
.unMaxValuePtr.pVoid = (void *)&max_##_name}
#if PARAM_USE_STRING_TYPE
/**
* @brief
*
@ -252,7 +254,7 @@
.unDefValuePtr.pVoid = (void *)&def_##_name, \
.unMinValuePtr.pVoid = (void *)&min_##_name, \
.unMaxValuePtr.pVoid = (void *)&max_##_name}
#endif
#endif
/**

View File

@ -22,12 +22,19 @@
* */
#define PARAM_USE_KEY_VALUE 1
/**
* @brief 使
* 0,; 1,使
*/
#define PARAM_USE_STRING_TYPE 1
/** 参数名字最大定义长度(包括结束符'\0', 因此小于或等于1则禁用参数名字字符串相关功能 */
#define PARAM_NAME_MAX_LENGTH 15
#if PARAM_USE_STRING_TYPE
/** 字符串类型的参数取值最大定义长度(包括结束符) */
#define PARAM_STRING_MAX_LENGTH 15
#endif
#if PARAM_USE_KEY_VALUE

View File

@ -65,7 +65,9 @@ typedef enum
PARAM_UINT64,
PARAM_FLOAT,
PARAM_DOUBLE,
#if PARAM_USE_STRING_TYPE
PARAM_STRING,
#endif
} ParamType_e;
typedef union {
@ -79,8 +81,10 @@ typedef union {
PARAM_UINT64_T *pUint64;
PARAM_FLOAT_T *pFloat;
PARAM_DOUBLE_T *pDouble;
#if PARAM_USE_STRING_TYPE
PARAM_STRING_T *pString;
param_size_t *pStringLength;
#endif
void *pVoid;
} ParamTypePtr_u;

View File

@ -28,7 +28,9 @@ typedef union
uint64_t u64val;
int64_t s64val;
double fVal;
#if PARAM_USE_STRING_TYPE
char str[PARAM_STRING_MAX_LENGTH + 2];
#endif
} Value_u;
static uint8_t *SerializeUint(uint8_t *ptr, uint64_t value, uint8_t len)
@ -131,6 +133,7 @@ static bool ResetParamValue(const ParamInfo_t *param)
{
if (param != NULL && (param->attr & PARAM_ATTR_RESET))
{
#if PARAM_USE_STRING_TYPE
if (param->type != PARAM_STRING)
{
memcpy(param->unCurValuePtr.pVoid, param->unDefValuePtr.pVoid, param->length);
@ -139,7 +142,9 @@ static bool ResetParamValue(const ParamInfo_t *param)
{
strcpy(param->unCurValuePtr.pString, param->unDefValuePtr.pString);
}
#else
memcpy(param->unCurValuePtr.pVoid, param->unDefValuePtr.pVoid, param->length);
#endif
return true;
}
@ -150,7 +155,9 @@ static bool ResetParamMinValue(const ParamInfo_t *param)
{
if (param != NULL && (param->attr & PARAM_ATTR_RANGE))
{
#if PARAM_USE_STRING_TYPE
if (param->type != PARAM_STRING)
#endif
{
memcpy(param->unCurValuePtr.pVoid, param->unMinValuePtr.pVoid, param->length);
}
@ -165,7 +172,9 @@ static bool ResetParamMaxValue(const ParamInfo_t *param)
{
if (param != NULL && (param->attr & PARAM_ATTR_RANGE))
{
#if PARAM_USE_STRING_TYPE
if (param->type != PARAM_STRING)
#endif
{
memcpy(param->unCurValuePtr.pVoid, param->unMaxValuePtr.pVoid, param->length);
}
@ -512,7 +521,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
return 2;
}
break;
#if PARAM_USE_STRING_TYPE
case PARAM_STRING:
if (strlen(pval->str) < *param->unMinValuePtr.pStringLength)
{
@ -523,6 +532,7 @@ static int ValidateRange(const ParamInfo_t *param, const Value_u *pval)
return 2;
}
break;
#endif
default:
return -1;
}
@ -575,11 +585,12 @@ static int ValidateRangeByVoid(const ParamInfo_t *param, const void *pval)
case PARAM_DOUBLE:
uValue.fVal = *(PARAM_DOUBLE_T *)pval;
break;
#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);
uValue.str[PARAM_STRING_MAX_LENGTH + 2] = '\0';
break;
#endif
default:
return -1;
}
@ -660,7 +671,7 @@ int Param_SetNewValue(const ParamInfo_t *param, const void *value, uint8_t opt)
return ret;
}
#if PARAM_USE_STRING_TYPE
if (param->type != PARAM_STRING)
{
memcpy(param->unCurValuePtr.pVoid, value, param->length);
@ -669,7 +680,9 @@ int Param_SetNewValue(const ParamInfo_t *param, const void *value, uint8_t opt)
{
strcpy(param->unCurValuePtr.pString, value);
}
#else
memcpy(param->unCurValuePtr.pVoid, value, param->length);
#endif
return 0;
}
@ -787,11 +800,11 @@ static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf)
case PARAM_DOUBLE:
pbuf = UnSerializeDouble(pbuf, param->unCurValuePtr.pDouble);
break;
#if PARAM_USE_STRING_TYPE
case PARAM_STRING:
memcpy(param->unCurValuePtr.pString, &pbuf[0], PARAM_STRING_MAX_LENGTH);
break;
#endif
default:
return 0; // 不支持的参数类型
}
@ -816,7 +829,11 @@ static uint8_t *MoveBufToBase(uint8_t *pbuf, uint32_t length)
*/
int Param_Load(ParamManager_t *manager, pfnLoad_cb pfnLoadCallback, pfnCheckError_cb pfnCheckError)
{
#if PARAM_USE_STRING_TYPE
uint8_t buf[sizeof(ParamInfo_t) + PARAM_STRING_MAX_LENGTH];
#else
uint8_t buf[sizeof(ParamInfo_t)];
#endif
uint8_t *ptr = buf;
if (manager == NULL || pfnLoadCallback == NULL)
@ -974,11 +991,11 @@ static uint16_t ParamInfoToStream(uint8_t *pbuf, ParamInfo_t *param)
case PARAM_DOUBLE:
pbuf = SerializeDouble(pbuf, *(PARAM_DOUBLE_T *)param->unCurValuePtr.pVoid);
break;
#if PARAM_USE_STRING_TYPE
case PARAM_STRING:
memcpy(&pbuf[0], param->unCurValuePtr.pString, PARAM_STRING_MAX_LENGTH);
break;
#endif
default:
return 0; // 不支持的参数类型
}
@ -997,7 +1014,11 @@ static uint16_t ParamInfoToStream(uint8_t *pbuf, ParamInfo_t *param)
*/
int Param_Save(ParamManager_t *manager, pfnSave_cb pfnSaveCallback, pfnCheckError_cb pfnCheckError)
{
#if PARAM_USE_STRING_TYPE
uint8_t buf[sizeof(ParamInfo_t) + PARAM_STRING_MAX_LENGTH];
#else
uint8_t buf[sizeof(ParamInfo_t)];
#endif
uint8_t *ptr = buf;
uint16_t length = 0;
#if PARAM_USE_KEY_VALUE

View File

@ -8,7 +8,9 @@ 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);
PARAM_DEFINE_DAT_RANGE (g_test_double, PARAM_DOUBLE, 3.15, -10, 10);
#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);
@ -24,7 +26,9 @@ ParamInfo_t sg_ParamTable[] = {
PARAM_ITEM_DAT_DEF(2, g_test_2, PARAM_ATTR_WR),
PARAM_ITEM_DAT_RANGE(3, g_test_float, PARAM_ATTR_READ),
PARAM_ITEM_DAT_RANGE(4, g_test_double, PARAM_ATTR_WR),
#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),
@ -98,6 +102,7 @@ void test_ParamInit(void)
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unMinValuePtr.pVoid , &PARAM_DAT_MIN_VALUE(g_test_float));
TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unMaxValuePtr.pVoid , &PARAM_DAT_MAX_VALUE(g_test_float));
#if PARAM_USE_STRING_TYPE
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);
@ -107,7 +112,7 @@ 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));
#endif
}
void test_ResetParam(void)
@ -115,14 +120,17 @@ void test_ResetParam(void)
g_test_1 = 80;
g_test_2 = 50;
g_test_float = 1;
#if PARAM_USE_STRING_TYPE
sprintf(g_test_str, "ABCDFS");
#endif
Param_ResetParam(&sg_tParamManager);
TEST_ASSERT_EQUAL_INT(g_test_1, 80);
TEST_ASSERT_EQUAL_UINT(g_test_2, PARAM_DAT_DEF_VALUE(g_test_2));
TEST_ASSERT_EQUAL_FLOAT(g_test_float, PARAM_DAT_DEF_VALUE(g_test_float));
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_STRING(g_test_str, PARAM_STR_DEF_VALUE(g_test_str));
#endif
}
@ -190,26 +198,31 @@ void test_SaveAndLoadParam(void)
g_test_1 = 80;
g_test_2 = 50;
g_test_float = -20;
#if PARAM_USE_STRING_TYPE
sprintf(g_test_str, "12a");
#endif
Param_Save(&sg_tParamManager, OnSaveCallback, NULL);
TEST_ASSERT_EQUAL_INT(g_test_1, 80);
TEST_ASSERT_EQUAL_UINT(g_test_2, 50);
TEST_ASSERT_EQUAL_FLOAT(g_test_float, -20);
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_STRING(g_test_str, "12a");
#endif
Param_Save(&sg_tParamManager, OnSaveCallback, OnCheckErrorResetHandle);
TEST_ASSERT_EQUAL_INT(g_test_1, 80);
TEST_ASSERT_EQUAL_UINT(g_test_2, 50);
TEST_ASSERT_EQUAL_FLOAT(g_test_float, PARAM_DAT_DEF_VALUE(g_test_float));
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_STRING(g_test_str, PARAM_STR_DEF_VALUE(g_test_str));
#endif
g_test_1 = 60;
g_test_2 = 40;
g_test_float = -20;
g_test_double = 5.36;
#if PARAM_USE_STRING_TYPE
sprintf(g_test_str, "12a123");
#endif
g_test_s8 = 20;
g_test_s16 = 1000;
@ -218,7 +231,9 @@ void test_SaveAndLoadParam(void)
TEST_ASSERT_EQUAL_INT(g_test_1, 80);
TEST_ASSERT_EQUAL_UINT(g_test_2, 50);
TEST_ASSERT_EQUAL_FLOAT(g_test_float, PARAM_DAT_DEF_VALUE(g_test_float));
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_STRING(g_test_str, PARAM_STR_DEF_VALUE(g_test_str));
#endif
TEST_ASSERT_EQUAL_INT(PARAM_DAT_DEF_VALUE(g_test_s8), g_test_s8);
TEST_ASSERT_EQUAL_INT(PARAM_DAT_DEF_VALUE(g_test_s16), g_test_s16);
TEST_ASSERT_EQUAL_INT(PARAM_DAT_DEF_VALUE(g_test_u16), g_test_u16);
@ -251,13 +266,22 @@ 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);
#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_u32);
TEST_ASSERT_EQUAL_PTR(NULL, paramInfo);
@ -286,6 +310,7 @@ void test_CheckRange(void)
TEST_ASSERT_EQUAL_INT(2, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_MIN_MAX));
TEST_ASSERT_EQUAL_UINT(3000, PARAM_DAT_CUR_VALUE(g_test_u16));
#if PARAM_USE_STRING_TYPE
snprintf(g_test_str, sizeof(g_test_str), "ABCDEF");
TEST_ASSERT_EQUAL_INT(0, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), PARAM_NONE));
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
@ -298,6 +323,7 @@ void test_CheckRange(void)
snprintf(g_test_str, sizeof(g_test_str), "AB");
TEST_ASSERT_EQUAL_INT(1, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), PARAM_DEF));
TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str);
#endif
}
void test_CheckRangeMacroDefine(void)
@ -323,6 +349,7 @@ void test_CheckRangeMacroDefine(void)
TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_MIN_MAX));
TEST_ASSERT_EQUAL_UINT(3000, PARAM_DAT_CUR_VALUE(g_test_u16));
#if PARAM_USE_STRING_TYPE
snprintf(g_test_str, sizeof(g_test_str), "ABCDEF");
TEST_ASSERT_EQUAL_INT(0, PARAM_STR_CHECK_RANGE(g_test_str, PARAM_NONE));
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
@ -335,6 +362,7 @@ void test_CheckRangeMacroDefine(void)
snprintf(g_test_str, sizeof(g_test_str), "AB");
TEST_ASSERT_EQUAL_INT(1, PARAM_STR_CHECK_RANGE(g_test_str, PARAM_DEF));
TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str);
#endif
}
void test_SetNewValue(void)
@ -361,6 +389,7 @@ void test_SetNewValue(void)
TEST_ASSERT_EQUAL_INT(2, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_MIN_MAX));
TEST_ASSERT_EQUAL_UINT(3000, PARAM_DAT_CUR_VALUE(g_test_u16));
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_INT(0, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), "ABCDEF", PARAM_NONE));
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
TEST_ASSERT_EQUAL_INT(1, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), "AB", PARAM_MIN_MAX));
@ -369,6 +398,7 @@ void test_SetNewValue(void)
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
TEST_ASSERT_EQUAL_INT(2, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), "abcdefg123456", PARAM_DEF));
TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str);
#endif
}
void test_SetNewValueMacroDefine(void)
@ -395,6 +425,7 @@ void test_SetNewValueMacroDefine(void)
TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_MIN_MAX));
TEST_ASSERT_EQUAL_UINT(3000, PARAM_DAT_CUR_VALUE(g_test_u16));
#if PARAM_USE_STRING_TYPE
TEST_ASSERT_EQUAL_INT(0, PARAM_STR_SET_NEW_VALUE(g_test_str, "ABCDEF", PARAM_NONE));
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
TEST_ASSERT_EQUAL_INT(1, PARAM_STR_SET_NEW_VALUE(g_test_str, "AB", PARAM_MIN_MAX));
@ -403,6 +434,7 @@ void test_SetNewValueMacroDefine(void)
TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str);
TEST_ASSERT_EQUAL_INT(2, PARAM_STR_SET_NEW_VALUE(g_test_str, "abcdefg123456", PARAM_DEF));
TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str);
#endif
}
void test_ResetValue(void)