From 1604b750fc006933115356b8072fbc7e24d65bbc Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sat, 9 Dec 2023 01:40:13 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=8F=82=E6=95=B0=E6=A8=A1=E5=9D=97=EF=BC=8C=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E5=8F=82=E6=95=B0=E5=AE=9A=E4=B9=89=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/cmd_shell/demo.c | 47 +- Demo/cmd_shell/makefile | 9 +- Demo/cmd_shell/param_demo.c | 246 ++-- Demo/cmd_shell/param_demo.h | 28 +- Param/inc/cot_param.h | 144 +++ Param/inc/{param_cfg.h => cot_param_cfg.h} | 44 +- Param/inc/cot_param_type.h | 153 +++ Param/inc/param.h | 584 --------- Param/inc/param_type.h | 138 -- Param/src/cot_param.c | 1312 ++++++++++++++++++++ test/makefile | 9 +- test/param_test.c | 524 ++++---- 12 files changed, 2126 insertions(+), 1112 deletions(-) create mode 100644 Param/inc/cot_param.h rename Param/inc/{param_cfg.h => cot_param_cfg.h} (65%) create mode 100644 Param/inc/cot_param_type.h delete mode 100644 Param/inc/param.h delete mode 100644 Param/inc/param_type.h create mode 100644 Param/src/cot_param.c diff --git a/Demo/cmd_shell/demo.c b/Demo/cmd_shell/demo.c index a976d63..dda9c69 100644 --- a/Demo/cmd_shell/demo.c +++ b/Demo/cmd_shell/demo.c @@ -1,46 +1,55 @@ - #include "param_demo.h" #include #include +void ShowAllParam_1(void); int main() { - InitParam(true); + InitParam(false); ShowAllParam(); + ShowAllParam_1(); g_test = 80; g_test_3 = -20.5; + sprintf(g_str_des, "sdx"); + + printf("\n\n"); SaveParam(false); ReloadParam(false); - int ret = PARAM_DAT_SET_NEW_VALUE(g_test_6, 50, PARAM_DEF); - printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + ShowAllParam(); + ShowAllParam_1(); + // int ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 50, COT_PARAM_DEF); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - ret = PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, PARAM_DEF); - printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_DEF); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - ret = PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, PARAM_NONE); - printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_NONE); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - ret = PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, PARAM_MIN_MAX); - printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_MIN_MAX); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - ret = PARAM_DAT_SET_NEW_VALUE(g_test_6, 1, PARAM_MIN_MAX); - printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 1, COT_PARAM_MIN_MAX); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa9", PARAM_NONE); - printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 1, COT_PARAM_MIN_MAX); + // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa1245249", PARAM_DEF); - printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa9", COT_PARAM_NONE); + // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); - PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa945457", PARAM_MIN_MAX); - printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa1245249", COT_PARAM_DEF); + // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); - ShowAllParam(); + // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa945457", COT_PARAM_MIN_MAX); + // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + + // ShowAllParam(); return 0; } diff --git a/Demo/cmd_shell/makefile b/Demo/cmd_shell/makefile index 863f05e..935160d 100644 --- a/Demo/cmd_shell/makefile +++ b/Demo/cmd_shell/makefile @@ -1,12 +1,15 @@ -CURR_DIR_PATH=$(shell pwd) +ifeq ($(OS),Windows_NT) + CURR_DIR_PATH = $(CURDIR) +else + CURR_DIR_PATH = $(shell pwd) +endif -CURR_DIR_PATH="C:\Users\const\Desktop\param\param\Demo\cmd_shell" GCC:=gcc INC+=-I${CURR_DIR_PATH}/../../Param/inc INC+=-I${CURR_DIR_PATH}/ -SRC+=${CURR_DIR_PATH}/../../Param/src/param.c +SRC+=${CURR_DIR_PATH}/../../Param/src/cot_param.c SRC+=${CURR_DIR_PATH}/demo.c SRC+=${CURR_DIR_PATH}/param_demo.c diff --git a/Demo/cmd_shell/param_demo.c b/Demo/cmd_shell/param_demo.c index 350509d..3fc6abd 100644 --- a/Demo/cmd_shell/param_demo.c +++ b/Demo/cmd_shell/param_demo.c @@ -51,6 +51,7 @@ int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) if (isFinish) { + printf("********\n"); HEX_PRINTF("\tsave", sg_buf, s_offset + len); s_offset = 0; return 0; @@ -70,62 +71,100 @@ typedef struct { uint16_t test1; float test2; - char str[20]; + char str[12]; }ParamDemo_t; -static ParamDemo_t sg_tTest; - - -PARAM_DEFINE_DAT (g_test, PARAM_INT16, 10); -PARAM_DEFINE_DAT_DEF (g_test_2, PARAM_UINT16, 20); -PARAM_DEFINE_DAT_RANGE (g_test_3, PARAM_DOUBLE, 3.15, -10, 10); -PARAM_DEFINE_STR_RANGE (g_test_str, 10, "abcdef", 5); -PARAM_DEFINE_DAT_RANGE (g_test_4, PARAM_INT8, 8, -10, 10); -PARAM_DEFINE_DAT_RANGE (g_test_5, PARAM_UINT32, 620, 500, 10000); -PARAM_DEFINE_DAT_RANGE (g_test_6, PARAM_UINT8, 45, 5, 100); -PARAM_DEFINE_DAT_RANGE (g_test_7, PARAM_INT64, 5, -542, 5450); -PARAM_DEFINE_BIND_DAT_RANGE(sg_tTest_test1, PARAM_UINT16, 20, 10, 2000); // 为即将绑定的变量定义相关参数信息 -PARAM_DEFINE_BIND_DAT(sg_tTest_test2, PARAM_FLOAT); // 为即将绑定的变量定义相关参数信息,初值为sg_tTest 变量定义时的初值 -PARAM_DEFINE_BIND_STR_RANGE(sg_tTest_str, sizeof(sg_tTest.str), "const-zpc", 6); - -ParamInfo_t sg_ParamTable[] = { - PARAM_ITEM_DAT(1, g_test, PARAM_ATTR_WR), - PARAM_ITEM_DAT_DEF(2, g_test_2, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(3, g_test_3, PARAM_ATTR_WR), - PARAM_ITEM_STR_RANGE(4, g_test_str, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(5, g_test_4, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(6, g_test_5, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(7, g_test_6, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(8, g_test_7, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE_BIND(9, sg_tTest_test1, sg_tTest.test1, PARAM_ATTR_WR), - PARAM_ITEM_DAT_BIND(10, sg_tTest_test2, sg_tTest.test2, PARAM_ATTR_WR), - PARAM_ITEM_STR_RANGE_BIND(11, sg_tTest_str, sg_tTest.str, PARAM_ATTR_WR), +static ParamDemo_t sg_tTest = { + .str = "sdf", + .test1 = 20, + .test2 = 567.4 }; +COT_PARAM_INT16_T g_test = 50; +COT_PARAM_UINT16_T g_test_2 = 20; +COT_PARAM_DOUBLE_T g_test_3 = 3.15; +COT_PARAM_INT8_T g_test_4 = 8; +COT_PARAM_UINT32_T g_test_5 = 620; +COT_PARAM_UINT8_T g_test_6 = 45; +COT_PARAM_INT64_T g_test_7 = 5; +COT_PARAM_INT8_T g_sd = 2; +COT_PARAM_INT64_T g_test_88 = 80; +char g_test_str[15] = "abcdef"; +char g_str_des[13] = "EERR"; +char g_str_des_2[15] = "sdRR"; +char g_str_des_3[15] = "ewRR"; + +// int a[10]; + +cotParamInfo_t sg_ParamTable[] = { + COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND_WITH_NAME(2, "ds", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + // COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), + COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), + COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), + COT_PARAM_ITEM_BIND(6, g_test_5, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 620, 500, 10000), + COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), + COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), + COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), + COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), + COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), + COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(17, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), +}; + +COT_PARAM_UINT64_T g_test_55 = 62055; + +cotParamInfo_t sg_ParamTable1[] = { + COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + // COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), + // COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), + // COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), + COT_PARAM_ITEM_BIND(6, g_test_55, COT_PARAM_UINT64, COT_PARAM_ATTR_WR, 62055, 500, 10000), + COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), + COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), + // COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), + // COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), + COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), + COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), + COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(16, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), +}; -static ParamManager_t sg_tParamManager; -static void ShowSingleParam(const ParamInfo_t *paramInfo); +static cotParamManager_t sg_tParamManager; +static cotParamManager_t sg_tParamManager1; + +static void ShowSingleParam(const cotParamInfo_t *paramInfo); // 数据校验出错时恢复默认处理 -int OnCheckErrorResetHandle(const ParamInfo_t *pParamInfo) +int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult) { printf("\n--------error list start ----------\n"); printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); ShowSingleParam(pParamInfo); printf("--------error list end ------------\n"); - Param_ResetDefaultValue(pParamInfo); + cotParam_SingleParamResetDefValue(pParamInfo); return 0; } void InitParam(bool isReset) { - Param_Init(&sg_tParamManager, sg_ParamTable, PARAM_TABLE_SIZE(sg_ParamTable)); + cotParam_Init(&sg_tParamManager, sg_ParamTable, COT_PARAM_TABLE_SIZE(sg_ParamTable)); + cotParam_Init(&sg_tParamManager1, sg_ParamTable1, COT_PARAM_TABLE_SIZE(sg_ParamTable1)); if (sg_length == 0) // 储存设备中没有储存过参数则首次进行储存 { - printf("frist save param\n"); - Param_Save(&sg_tParamManager, OnSaveCallback, NULL); // 初次储存可以认为数据都是默认值,无需数据校验出错时恢复默认处理 + printf("frist save param: %d\n", cotParam_GetSerializeSize(&sg_tParamManager)); + SaveParam(false); + + // cotParam_Save(&sg_tParamManager, OnSaveCallback, NULL); // 初次储存可以认为数据都是默认值,无需数据校验出错时恢复默认处理 } ReloadParam(isReset); @@ -137,34 +176,45 @@ void ReloadParam(bool isReset) if (isReset) { - Param_Load(&sg_tParamManager, OnLoadCallback, OnCheckErrorResetHandle); + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Load(&sg_tParamManager, OnLoadCallback); + // cotParam_Deserialization(&sg_tParamManager1, sg_buf, sg_length); + // cotParam_Load(&sg_tParamManager1, OnLoadCallback, NULL); } else { - Param_Load(&sg_tParamManager, OnLoadCallback, NULL); + cotParam_Load(&sg_tParamManager, OnLoadCallback); + // cotParam_Deserialization(&sg_tParamManager1, sg_buf, sg_length); + // cotParam_Load(&sg_tParamManager1, OnLoadCallback, NULL); } } void ResetParam(void) { printf("reset param\n"); - Param_ResetParam(&sg_tParamManager); + cotParam_ResetDefault(&sg_tParamManager); } void SaveParam(bool isReset) { + uint8_t buf[300]; + uint32_t bufLength = 0; printf("save param\n"); if (isReset) { - Param_Save(&sg_tParamManager, OnSaveCallback, OnCheckErrorResetHandle); + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Save(&sg_tParamManager, OnSaveCallback); } else { - Param_Save(&sg_tParamManager, OnSaveCallback, NULL); + bufLength = cotParam_Serialize(&sg_tParamManager, buf); + OnSaveCallback(buf, bufLength, false); + OnSaveCallback(buf, 0, true); + // cotParam_Save(&sg_tParamManager, OnSaveCallback); } } -#define ATTR(x) ((x & (PARAM_ATTR_READ | PARAM_ATTR_WRITE)) == (PARAM_ATTR_READ | PARAM_ATTR_WRITE) ? \ +#define ATTR(x) ((x & (PARAM_ATTR_READ | COT_PARAM_ATTR_WRITE)) == (PARAM_ATTR_READ | COT_PARAM_ATTR_WRITE) ? \ "wr" : (x & (PARAM_ATTR_READ) ? "r" : ((x & (PARAM_ATTR_WRITE) ? "w" : "")))) char *Attr(uint8_t attr) @@ -172,22 +222,22 @@ char *Attr(uint8_t attr) static char buf[10]; char *p = buf; - if (attr & PARAM_ATTR_READ) + if (attr & COT_PARAM_ATTR_READ) { p += sprintf(p, "r"); } - if (attr & PARAM_ATTR_WRITE) + if (attr & COT_PARAM_ATTR_WRITE) { p += sprintf(p, "w"); } - if (attr & PARAM_ATTR_RESET) + if (attr & COT_PARAM_ATTR_RESET) { p += sprintf(p, "s"); } - if (attr & PARAM_ATTR_RANGE) + if (attr & COT_PARAM_ATTR_RANGE) { p += sprintf(p, "m"); } @@ -195,22 +245,22 @@ char *Attr(uint8_t attr) return buf; } -void ShowSingleParam(const ParamInfo_t *paramInfo) +void ShowSingleParam(const cotParamInfo_t *paramInfo) { if (paramInfo != NULL) { switch (paramInfo->type) { - case PARAM_INT8: + case COT_PARAM_INT8: printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt8); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10d ", *paramInfo->unDefValuePtr.pInt8); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10d ", *paramInfo->unMinValuePtr.pInt8); printf("%-10d ", *paramInfo->unMaxValuePtr.pInt8); @@ -221,16 +271,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_INT16: + case COT_PARAM_INT16: printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt16); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10d ", *paramInfo->unDefValuePtr.pInt16); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10d ", *paramInfo->unMinValuePtr.pInt16); printf("%-10d ", *paramInfo->unMaxValuePtr.pInt16); @@ -241,16 +291,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_INT32: + case COT_PARAM_INT32: printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt32); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10d ", *paramInfo->unDefValuePtr.pInt32); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10d ", *paramInfo->unMinValuePtr.pInt32); printf("%-10d ", *paramInfo->unMaxValuePtr.pInt32); @@ -261,19 +311,19 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_INT64: - printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + case COT_PARAM_INT64: + printf(" %-4d %-15s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->pszName, "int64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt64); - if (paramInfo->attr & PARAM_ATTR_RESET) - printf("%-10d ", *paramInfo->unDefValuePtr.pInt64); + if (paramInfo->attr & COT_PARAM_ATTR_RESET) + printf("%-10ld ", *paramInfo->unDefValuePtr.pInt64); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { - printf("%-10d ", *paramInfo->unMinValuePtr.pInt64); - printf("%-10d ", *paramInfo->unMaxValuePtr.pInt64); + printf("%-10ld ", *paramInfo->unMinValuePtr.pInt64); + printf("%-10ld ", *paramInfo->unMaxValuePtr.pInt64); } else { @@ -281,16 +331,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_UINT8: + case COT_PARAM_UINT8: printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint8); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10u ", *paramInfo->unDefValuePtr.pUint8); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10u ", *paramInfo->unMinValuePtr.pUint8); printf("%-10u ", *paramInfo->unMaxValuePtr.pUint8); @@ -301,16 +351,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_UINT16: + case COT_PARAM_UINT16: printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint16); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10u ", *paramInfo->unDefValuePtr.pUint16); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10u ", *paramInfo->unMinValuePtr.pUint16); printf("%-10u ", *paramInfo->unMaxValuePtr.pUint16); @@ -321,16 +371,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_UINT32: + case COT_PARAM_UINT32: printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint32); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10u ", *paramInfo->unDefValuePtr.pUint32); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10u ", *paramInfo->unMinValuePtr.pUint32); printf("%-10u ", *paramInfo->unMaxValuePtr.pUint32); @@ -341,19 +391,19 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_UINT64: - printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + case COT_PARAM_UINT64: + printf(" %-4d %-15s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->pszName, "uint64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint64); - if (paramInfo->attr & PARAM_ATTR_RESET) - printf("%-10u ", *paramInfo->unDefValuePtr.pUint64); + if (paramInfo->attr & COT_PARAM_ATTR_RESET) + printf("%-10lu ", *paramInfo->unDefValuePtr.pUint64); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { - printf("%-10u ", *paramInfo->unMinValuePtr.pUint64); - printf("%-10u ", *paramInfo->unMaxValuePtr.pUint64); + printf("%-10lu ", *paramInfo->unMinValuePtr.pUint64); + printf("%-10lu ", *paramInfo->unMaxValuePtr.pUint64); } else { @@ -361,16 +411,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_FLOAT: + case COT_PARAM_FLOAT: printf(" %-4d %-15s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, "float", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pFloat); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10f ", *paramInfo->unDefValuePtr.pFloat); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10f ", *paramInfo->unMinValuePtr.pFloat); printf("%-10f ", *paramInfo->unMaxValuePtr.pFloat); @@ -381,16 +431,16 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_DOUBLE: + case COT_PARAM_DOUBLE: printf(" %-4d %-15s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, "double", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pDouble); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10f ", *paramInfo->unDefValuePtr.pDouble); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10f ", *paramInfo->unMinValuePtr.pDouble); printf("%-10f ", *paramInfo->unMaxValuePtr.pDouble); @@ -401,16 +451,17 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - case PARAM_STRING: +#if COT_PARAM_USE_STRING_TYPE + case COT_PARAM_STRING: printf(" %-4d %-15s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->pszName, "string", paramInfo->length, Attr(paramInfo->attr), paramInfo->unCurValuePtr.pString); - if (paramInfo->attr & PARAM_ATTR_RESET) + if (paramInfo->attr & COT_PARAM_ATTR_RESET) printf("%-10s ", paramInfo->unDefValuePtr.pString); else printf("%-10s ", "-"); - if (paramInfo->attr & PARAM_ATTR_RANGE) + if (paramInfo->attr & COT_PARAM_ATTR_RANGE) { printf("%-10u ", *paramInfo->unMinValuePtr.pStringLength); printf("%-10u ", *paramInfo->unMaxValuePtr.pStringLength); @@ -421,7 +472,7 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) printf("%-10s ", "-"); } break; - +#endif default: break; } @@ -434,13 +485,28 @@ void ShowSingleParam(const ParamInfo_t *paramInfo) void ShowAllParam(void) { size_t idx = 0; - ParamInfo_t *paramInfo; + cotParamInfo_t *paramInfo; + + printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); + + do + { + paramInfo = cotParam_IterateList(&sg_tParamManager, &idx); + + ShowSingleParam(paramInfo); + } while (paramInfo != NULL); +} + +void ShowAllParam_1(void) +{ + size_t idx = 0; + cotParamInfo_t *paramInfo; printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); do { - paramInfo = Param_IterateList(&sg_tParamManager, &idx); + paramInfo = cotParam_IterateList(&sg_tParamManager1, &idx); ShowSingleParam(paramInfo); } while (paramInfo != NULL); diff --git a/Demo/cmd_shell/param_demo.h b/Demo/cmd_shell/param_demo.h index cb94997..61623d9 100644 --- a/Demo/cmd_shell/param_demo.h +++ b/Demo/cmd_shell/param_demo.h @@ -1,22 +1,24 @@ #ifndef _PARAM_DEMO_H_ #define _PARAM_DEMO_H_ -#include "param.h" +#include "cot_param.h" -PARAM_EXTERN_DAT(g_test, PARAM_INT16); -PARAM_EXTERN_DAT(g_test_2, PARAM_UINT16); -PARAM_EXTERN_DAT(g_test_3, PARAM_DOUBLE); -PARAM_EXTERN_STR(g_test_str, 10); -PARAM_EXTERN_DAT(g_test_4, PARAM_INT8); -PARAM_EXTERN_DAT(g_test_5, PARAM_UINT32); -PARAM_EXTERN_DAT(g_test_6, PARAM_UINT8); -PARAM_EXTERN_DAT(g_test_7, PARAM_INT64); -PARAM_EXTERN_BIND_DAT(sg_tTest_test1, PARAM_UINT16); -PARAM_EXTERN_BIND_DAT(sg_tTest_test2, PARAM_FLOAT); -PARAM_EXTERN_BIND_STR(sg_tTest_str, 20); +extern COT_PARAM_INT16_T g_test; +extern COT_PARAM_UINT16_T g_test_2; +extern COT_PARAM_DOUBLE_T g_test_3; +extern COT_PARAM_INT8_T g_test_4; +extern COT_PARAM_UINT32_T g_test_5; +extern COT_PARAM_UINT8_T g_test_6; +extern COT_PARAM_INT64_T g_test_7; +extern COT_PARAM_INT8_T g_sd; +extern COT_PARAM_INT64_T g_test_88; +extern char g_test_str[15]; +extern char g_str_des[13]; +extern char g_str_des_2[15]; +extern char g_str_des_3[15]; -#define PARAM_UPDATE(name, val, ret) {\ +#define COT_PARAM_UPDATE(name, val, ret) {\ param_##name##_t t = val;\ SetParamNewValue(&name, &t);\ } diff --git a/Param/inc/cot_param.h b/Param/inc/cot_param.h new file mode 100644 index 0000000..aa434f7 --- /dev/null +++ b/Param/inc/cot_param.h @@ -0,0 +1,144 @@ +/** + ********************************************************************************************************************** + * @file cot_param.h + * @brief 该文件提供参数管理框架所有函数原型 + * @author const_zpc any question please send mail to const_zpc@163.com + * @date 2023-12-08 + ********************************************************************************************************************** + * + ********************************************************************************************************************** + */ + +/* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/ + +#ifndef _COT_PARAM_H_ +#define _COT_PARAM_H_ + +/* Includes ----------------------------------------------------------------------------------------------------------*/ +#include "cot_param_type.h" + +#ifdef __cplusplus + extern "C" { +#endif + +/** + * @brief 添加参数(以变量名为参数名) + * + * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal) + * @param id 参数表中的唯一ID + * @param variable 变量 + * @param type 参数类型 @enum ParamType_e 取值 + * @param attr 属性 + * @param defVal 默认值(可选) + * @param minVal 最小值(可选) + * @param maxVal 最大值(可选) + */ +#define COT_PARAM_ITEM_BIND(id, variable, type, attr, defVal...) COT_PARAM_ITEM_##type##_IMPL(id, #variable, variable, type, attr, ##defVal) + +/** + * @brief 添加参数 + * + * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal) + * @param id 参数表中的唯一ID + * @param name 参数名 + * @param variable 变量 + * @param type 参数类型 @enum ParamType_e 取值 + * @param attr 属性 + * @param defVal 默认值(可选) + * @param minVal 最小值(可选) + * @param maxVal 最大值(可选) + */ +#define COT_PARAM_ITEM_BIND_WITH_NAME(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_##type##_IMPL(id, name, variable, type, attr, ##defVal) + +#define COT_PARAM_ITEM_BIND_NUM(_id, _name, _variable, _type, _attr) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr), \ + NULL, NULL, NULL) +#define COT_PARAM_ITEM_BIND_NUM_DEF(_id, _name, _variable, _type, _attr, _defVal) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET), \ + (void *)&(_type##_T){_defVal}, NULL, NULL) +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ + (void *)&(_type##_T){_defVal}, (void *)&(_type##_T){_minVal}, (void *)&(_type##_T){_maxVal}) + +#define COT_PARAM_ITEM_BIND_STR(_id, _name, _variable, _type, _attr) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr), \ + NULL, NULL, NULL) +#define COT_PARAM_ITEM_BIND_STR_DEF(_id, _name, _variable, _type, _attr, _defVal) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET), \ + (void *)_defVal, NULL, NULL) +#define COT_PARAM_ITEM_BIND_STR_DEF_RANGE(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ + (void *)_defVal, (void *)&(param_size_t){_minVal}, (void *)&(param_size_t){_maxVal}) + +#if (COT_PARAM_NAME_MAX_LENGTH > 1) +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal) { \ + .pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ + .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} +#else +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal) { \ + .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ + .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} +#endif + + +#define COT_PARAM_ITEM_COT_PARAM_INT8_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_INT16_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_INT32_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_INT64_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_UINT8_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_UINT16_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_UINT32_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_UINT64_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_FLOAT_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_DOUBLE_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) +#define COT_PARAM_ITEM_COT_PARAM_STRING_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ + COT_PARAM_ITEM_BIND_STR_DEF_RANGE, , COT_PARAM_ITEM_BIND_STR_DEF, COT_PARAM_ITEM_BIND_STR)(id, name, variable, type, attr, ##defVal) + +#define COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, _1, _2, _3, FUNC, ...) FUNC + +// 得到参数表的元素数目 +#define COT_PARAM_TABLE_SIZE(tab) (sizeof(tab) / sizeof(cotParamInfo_t)) + + +extern int cotParam_Init(cotParamManager_t* pManager, cotParamInfo_t* pParamTable, uint16_t count); +extern int cotParam_ResetDefault(const cotParamManager_t *pManager); +extern int cotParam_Check(const cotParamManager_t* pManager, pfnCheckError_f pfnCheckError); + +extern int cotParam_Load(const cotParamManager_t* pManager, pfnLoad_f pfnLoadCallback); +extern int cotParam_Save(const cotParamManager_t* pManager, pfnSave_f pfnSaveCallback); + +extern uint32_t cotParam_GetSerializeSize(const cotParamManager_t* pManager); +extern uint32_t cotParam_Serialize(const cotParamManager_t* pManager, uint8_t *pbuf); +extern int cotParam_Deserialization(const cotParamManager_t* pManager, const uint8_t *pbuf, uint32_t length); + +extern cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t *psIdx); + +extern const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName); +extern const cotParamInfo_t *cotParam_FindParamByID(const cotParamManager_t* pManager, uint16_t id); +extern const cotParamInfo_t *cotParam_FindParamByParamPtr(const cotParamManager_t* pManager, const void *pCurParam); + +extern int cotParam_SingleParamCheckProcess(const cotParamInfo_t *pParam, cotParamResetOpt_e eResetOpt); +extern int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewValue, cotParamResetOpt_e eResetOpt); + +extern int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult); +extern int cotParam_SingleParamResetDefValue(const cotParamInfo_t *pParam); +extern int cotParam_SingleParamResetMinValue(const cotParamInfo_t *pParam); +extern int cotParam_SingleParamResetMaxValue(const cotParamInfo_t *pParam); + +#ifdef __cplusplus + } +#endif + +#endif // !_PARAM_H_ diff --git a/Param/inc/param_cfg.h b/Param/inc/cot_param_cfg.h similarity index 65% rename from Param/inc/param_cfg.h rename to Param/inc/cot_param_cfg.h index a0a1a30..af0c7d8 100644 --- a/Param/inc/param_cfg.h +++ b/Param/inc/cot_param_cfg.h @@ -1,9 +1,9 @@ /** ********************************************************************************************************************** - * @file param_cfg.h + * @file cot_param_cfg.h * @brief 该文件提供参数管理框架相关配置选项 * @author const_zpc any question please send mail to const_zpc@163.com - * @date 2023-03-11 + * @date 2023-12-08 ********************************************************************************************************************** * ********************************************************************************************************************** @@ -11,8 +11,8 @@ /* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/ -#ifndef _PARAM_CFG_H_ -#define _PARAM_CFG_H_ +#ifndef _COT_PARAM_CFG_H_ +#define _COT_PARAM_CFG_H_ /* Includes ----------------------------------------------------------------------------------------------------------*/ @@ -20,49 +20,49 @@ * 1,采用键值对, 该方式在参数表更改时可以向下兼容 * 0,禁止, 该方式在参数表更改时可以不一定兼容, 删除/添加表中参数时无法兼容, 即使不使用也不能删除, 只有在参数表末尾不断添加才能保证向下兼容 * */ -#define PARAM_USE_KEY_VALUE 1 +#define COT_PARAM_USE_KEY_VALUE 1 /** * @brief 使用字符串参数类型 * 0,禁止; 1,使能 */ -#define PARAM_USE_STRING_TYPE 1 +#define COT_PARAM_USE_STRING_TYPE 1 /** * @brief 使用64位的参数类型 * 0,禁止; 1,使能 */ -#define PARAM_USE_64_BIT_LENGTH 1 +#define COT_PARAM_USE_64_BIT_LENGTH 1 /** 参数名字最大定义长度(包括结束符'\0'), 因此小于或等于1则禁用参数名字字符串相关功能 */ -#define PARAM_NAME_MAX_LENGTH 15 +#define COT_PARAM_NAME_MAX_LENGTH 15 -#if PARAM_USE_STRING_TYPE +#if COT_PARAM_USE_STRING_TYPE /** 字符串类型的参数取值最大定义长度(包括结束符) */ -#define PARAM_STRING_MAX_LENGTH 15 +#define COT_PARAM_STRING_MAX_LENGTH 15 #endif -#if PARAM_USE_KEY_VALUE +#if COT_PARAM_USE_KEY_VALUE /** 最多支持多少个参数 */ -#define PARAM_SUPPORT_NUM PARAM_SUPPORT_16 -#define PARAM_SUPPORT_16 1 -#define PARAM_SUPPORT_256 2 -#define PARAM_SUPPORT_4096 3 +#define COT_PARAM_SUPPORT_NUM COT_PARAM_SUPPORT_16 +#define COT_PARAM_SUPPORT_16 1 // ID取值范围0-15 +#define COT_PARAM_SUPPORT_256 2 // ID取值范围0-255 +#define COT_PARAM_SUPPORT_4096 3 // ID取值范围0-4095 #endif -#if PARAM_USE_KEY_VALUE - #if (PARAM_SUPPORT_NUM == PARAM_SUPPORT_16) - #if PARAM_STRING_MAX_LENGTH >= 16 +#if COT_PARAM_USE_KEY_VALUE + #if (PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16) + #if COT_PARAM_STRING_MAX_LENGTH >= 16 #error "Param: The maximum length of a string can not over 15" #endif - #elif (PARAM_SUPPORT_NUM == PARAM_SUPPORT_256) - #if PARAM_STRING_MAX_LENGTH >= 256 + #elif (PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256) + #if COT_PARAM_STRING_MAX_LENGTH >= 256 #error "Param: The maximum length of a string can not over 256" #endif - #elif (PARAM_SUPPORT_NUM == PARAM_SUPPORT_4096) - #if PARAM_STRING_MAX_LENGTH >= 4096 + #elif (PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096) + #if COT_PARAM_STRING_MAX_LENGTH >= 4096 #error "Param: The maximum length of a string can not over 4096" #endif #endif diff --git a/Param/inc/cot_param_type.h b/Param/inc/cot_param_type.h new file mode 100644 index 0000000..2cdb640 --- /dev/null +++ b/Param/inc/cot_param_type.h @@ -0,0 +1,153 @@ +/** + ********************************************************************************************************************** + * @file cot_param_type.h + * @brief 该文件提供参数管理框架所有函数原型 + * @author const_zpc any question please send mail to const_zpc@163.com + * @date 2023-12-08 + ********************************************************************************************************************** + * + ********************************************************************************************************************** + */ + +/* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/ + +#ifndef _COT_PARAM_TYPE_H_ +#define _COT_PARAM_TYPE_H_ + +/* Includes ----------------------------------------------------------------------------------------------------------*/ +#include "cot_param_cfg.h" +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** 参数权限属性 */ + +#define COT_PARAM_ATTR_WR (COT_PARAM_ATTR_READ | COT_PARAM_ATTR_WRITE) // 读写权限 +#define COT_PARAM_ATTR_NONE 0X00 // 无读写权限(不使用键值对模式储存参数则用于删除参数的属性) +#define COT_PARAM_ATTR_READ 0X01 // 可读权限属性 +#define COT_PARAM_ATTR_WRITE 0X02 // 可写权限属性 +#define COT_PARAM_ATTR_RESET 0X04 // 可重置权限属性 +#define COT_PARAM_ATTR_RANGE 0X10 // 支持最大/最小值校验 + + +typedef int8_t COT_PARAM_INT8_T; +typedef int16_t COT_PARAM_INT16_T; +typedef int32_t COT_PARAM_INT32_T; +typedef int64_t COT_PARAM_INT64_T; +typedef uint8_t COT_PARAM_UINT8_T; +typedef uint16_t COT_PARAM_UINT16_T; +typedef uint32_t COT_PARAM_UINT32_T; +typedef uint64_t COT_PARAM_UINT64_T; +typedef float COT_PARAM_FLOAT_T; +typedef double COT_PARAM_DOUBLE_T; +typedef char COT_PARAM_STRING_T; + +#if COT_PARAM_STRING_MAX_LENGTH >= 256 +typedef uint16_t param_size_t; +#else +typedef uint8_t param_size_t; +#endif + +typedef enum +{ + COT_PARAM_INT8 = 0, + COT_PARAM_INT16, + COT_PARAM_INT32, +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_INT64, +#endif + COT_PARAM_UINT8, + COT_PARAM_UINT16, + COT_PARAM_UINT32, +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_UINT64, +#endif + COT_PARAM_FLOAT, +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_DOUBLE, +#endif +#if COT_PARAM_USE_STRING_TYPE + COT_PARAM_STRING, +#endif +} ParamType_e; + +typedef enum +{ + COT_PARAM_CHECK_OK = 0, // 校验成功 / 无最大最小值 + COT_PARAM_CHECK_OVER_MIN, // 小于最小值 / 小于最小字符串长度 + COT_PARAM_CHECK_OVER_MAX // 大于最大值 / 大于最小字符串长度 +} cotParamCheckRet_e; + +typedef enum +{ + COT_PARAM_RESET_NONE = 0, // 不做处理 / 不更新 + COT_PARAM_RESET_DEF, // 恢复默认 + COT_PARAM_RESET_MIN, // 恢复最小值 + COT_PARAM_RESET_MAX, // 恢复最大值 + COT_PARAM_RESET_MIN_MAX // 小于最小值则恢复最小值,大于最大值则恢复最大值 +} cotParamResetOpt_e; + +typedef union { + COT_PARAM_INT8_T *pInt8; + COT_PARAM_INT16_T *pInt16; + COT_PARAM_INT32_T *pInt32; +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_INT64_T *pInt64; +#endif + COT_PARAM_UINT8_T *pUint8; + COT_PARAM_UINT16_T *pUint16; + COT_PARAM_UINT32_T *pUint32; +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_UINT64_T *pUint64; +#endif + COT_PARAM_FLOAT_T *pFloat; +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_DOUBLE_T *pDouble; +#endif +#if COT_PARAM_USE_STRING_TYPE + COT_PARAM_STRING_T *pString; + param_size_t *pStringLength; +#endif + void *pVoid; +} cotParamTypePtr_u; + +/** + * @brief 定义无内存的参数结构体信息 + * + */ +typedef struct stuParamInfo +{ +#if ( COT_PARAM_NAME_MAX_LENGTH > 1) + const char *pszName; /*!< 名称 */ +#endif + uint16_t id; /*!< 唯一ID */ + uint8_t type; /*!< 类型, @enum ParamType_e */ + uint16_t length; /*!< 长度(字节数) */ + uint8_t attr; /*!< 属性 */ + cotParamTypePtr_u unCurValuePtr; /*!< 当前值指针 */ + const cotParamTypePtr_u unDefValuePtr; /*!< 默认值指针 */ + const cotParamTypePtr_u unMinValuePtr; /*!< 最小值指针(字符串参数限制最小长度时需要 param_size_t 定义) */ + const cotParamTypePtr_u unMaxValuePtr; /*!< 最大值指针(字符串参数限制最大长度时需要 param_size_t 定义) */ +} cotParamInfo_t; + +typedef int (*pfnCheckError_f)(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult); +typedef int (*pfnLoad_f)(uint8_t *pBuf, uint16_t *len, bool *pisFinish); +typedef int (*pfnSave_f)(const uint8_t *pBuf, uint16_t len, bool isFinish); + +// 定义参数管理结构体 +typedef struct +{ + cotParamInfo_t *pParamTable; /*!< 参数信息数组表 */ + uint16_t count; /*!< 参数信息数组元素个数 */ +} cotParamManager_t; + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Param/inc/param.h b/Param/inc/param.h deleted file mode 100644 index 9d84818..0000000 --- a/Param/inc/param.h +++ /dev/null @@ -1,584 +0,0 @@ -/** - ********************************************************************************************************************** - * @file param.h - * @brief 该文件提供参数管理框架所有函数原型 - * @author const_zpc any question please send mail to const_zpc@163.com - * @version V1.0 - * @date 2023-06-09 - ********************************************************************************************************************** - * - ********************************************************************************************************************** - */ - -/* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/ - -#ifndef _PARAM_H_ -#define _PARAM_H_ - -/* Includes ----------------------------------------------------------------------------------------------------------*/ -#include "param_type.h" - -#ifdef __cplusplus - extern "C" { -#endif - -/** - * @brief 定义普通参数 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - * @param initVal 初始值 - */ -#define PARAM_DEFINE_DAT(name, type, initVal) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = 0};\ - type##_T name = initVal; - -/** - * @brief 定义普通参数, 具有默认值 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - * @param defVal 默认值/初始值 - */ -#define PARAM_DEFINE_DAT_DEF(name, type, defVal) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RESET)};\ - type##_T name = defVal;\ - const type##_T def_##name = defVal; - -/** - * @brief 定义普通参数, 具有默认值和范围校验 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - * @param defVal 默认值/初始值 - * @param minVal 最小值 - * @param maxVal 最大值 - */ -#define PARAM_DEFINE_DAT_RANGE(name, type, defVal, minVal, maxVal) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RANGE | PARAM_ATTR_RESET)};\ - type##_T name = defVal;\ - const type##_T def_##name = defVal;\ - const type##_T min_##name = minVal;\ - const type##_T max_##name = maxVal; - -/** - * @brief 为已定义的变量定义参数相关信息 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - */ -#define PARAM_DEFINE_BIND_DAT(name, type) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = 0}; - -/** - * @brief 为已定义的变量定义参数相关信息, 具有默认值 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - * @param defVal 默认值 - */ -#define PARAM_DEFINE_BIND_DAT_DEF(name, type, defVal) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RESET)};\ - const type##_T def_##name = defVal; - -/** - * @brief 为已定义的变量定义参数相关信息, 具有默认值和范围校验 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - * @param defVal 默认值 - * @param minVal 最小值 - * @param maxVal 最大值 - */ -#define PARAM_DEFINE_BIND_DAT_RANGE(name, type, defVal, minVal, maxVal) \ - enum {PARAM_TYPE_##name = type};\ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RANGE | PARAM_ATTR_RESET)};\ - const type##_T def_##name = defVal;\ - const type##_T min_##name = minVal;\ - const type##_T max_##name = maxVal; - -#if PARAM_USE_STRING_TYPE -/** - * @brief 定义字符串参数 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param initVal 初始值 - */ -#define PARAM_DEFINE_STR(name, length, defVal) \ - enum {PARAM_INIT_ATTR_##name = 0};\ - char name[length] = {defVal};\ - -/** - * @brief 定义字符串参数, 具有默认值 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param defVal 默认值 - */ -#define PARAM_DEFINE_STR_DEF(name, length, defVal) \ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RESET)};\ - char name[length] = {defVal};\ - const char def_##name[] = {defVal}; - -/** - * @brief 定义字符串参数, 具有默认值和长度校验 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param defVal 默认值 - * @param minLength 最小长度 - */ -#define PARAM_DEFINE_STR_RANGE(name, length, defVal, minLength) \ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RANGE | PARAM_ATTR_RESET)};\ - char name[length] = {defVal};\ - const char def_##name[] = {defVal};\ - const param_size_t min_##name = minLength;\ - const param_size_t max_##name = length - 1; - - -/** - * @brief 为已定义的字符串变量定义参数相关信息 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param initVal 初始值 - */ -#define PARAM_DEFINE_BIND_STR(name, length) \ - enum {PARAM_INIT_ATTR_##name = 0}; - -/** - * @brief 为已定义的字符串变量定义参数相关信息, 具有默认值 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param defVal 默认值 - */ -#define PARAM_DEFINE_BIND_STR_DEF(name, length, defVal) \ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RESET)};\ - const char def_##name[] = {defVal}; - -/** - * @brief 为已定义的字符串变量定义参数相关信息, 具有默认值和长度校验 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度(包括结束符'\0') - * @param defVal 默认值 - * @param minLength 最小长度 - */ -#define PARAM_DEFINE_BIND_STR_RANGE(name, length, defVal, minLength) \ - enum {PARAM_INIT_ATTR_##name = (PARAM_ATTR_RANGE | PARAM_ATTR_RESET)};\ - 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) -/** - * @brief 普通参数注册 - * - * @attention 对应的宏定义: PARAM_DEFINE_DAT / PARAM_DEFINE_DAT_DEF / PARAM_DEFINE_DAT_RANGE - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_DAT(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_DEF(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_RANGE(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} - - -/** - * @brief 为数值变量绑定普通参数进行注册 - * - * @attention 对应的宏定义: PARAM_DEFINE_BIND_DAT / PARAM_DEFINE_BIND_DAT_DEF / PARAM_DEFINE_BIND_DAT_RANGE - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param variable 需要绑定的已定义变量 - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_DAT_BIND(_id, _name, _variable, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_variable), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &(_variable), \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_DEF_BIND(_id, _name, _variable, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_variable), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &(_variable), \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_RANGE_BIND(_id, _name, _variable, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_variable), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &(_variable), \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} - -#if PARAM_USE_STRING_TYPE -/** - * @brief 字符串参数注册 - * - * @attention 对应的宏定义: PARAM_DEFINE_STR / PARAM_DEFINE_DAT_STR / PARAM_DEFINE_DAT_STR - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_STR(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_DEF(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_RANGE(_id, _name, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} - -/** - * @brief 为字符串变量绑定字符串参数进行注册 - * - * @attention 对应的宏定义: PARAM_DEFINE_BIND_STR / PARAM_DEFINE_BIND_DAT_STR / PARAM_DEFINE_BIND_DAT_STR - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param variable 需要绑定的已定义字符串变量 - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_STR_BIND(_id, _name, _variabl, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_variabl), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = _variabl, \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_DEF_BIND(_id, _name, _variabl, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_variabl), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = _variabl, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_RANGE_BIND(_id, _name, _variabl, _attr) { .pszName = #_name, \ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_variabl), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = _variabl, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} -#endif -#else - -/** - * @brief 普通参数注册 - * - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_DAT(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_DEF(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_DAT_RANGE(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_TYPE_##_name, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .unCurValuePtr.pVoid = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} -#if PARAM_USE_STRING_TYPE -/** - * @brief 字符串参数注册 - * - * @param _id 参数唯一 ID - * @param _name 参数名(变量名) - * @param _attr 参数读/写属性组合(PARAM_ATTR_READ/PARAM_ATTR_WRITE) - */ -#define PARAM_ITEM_STR(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .pCurValue = &_name, \ - .unDefValuePtr.pVoid = NULL, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_DEF(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .pCurValue = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = NULL, \ - .unMaxValuePtr.pVoid = NULL} - -#define PARAM_ITEM_STR_RANGE(_id, _name, _attr) {\ - .id = _id, \ - .type = PARAM_STRING, \ - .length = sizeof(_name), \ - .attr = (_attr | PARAM_INIT_ATTR_##_name), \ - .pCurValue = &_name, \ - .unDefValuePtr.pVoid = (void *)&def_##_name, \ - .unMinValuePtr.pVoid = (void *)&min_##_name, \ - .unMaxValuePtr.pVoid = (void *)&max_##_name} -#endif -#endif - -/** - * @brief 参数声明 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - */ -#define PARAM_EXTERN_DAT(name, type) \ - typedef type##_T param_##name##_t;\ - extern type##_T name;\ - const extern type##_T def_##name;\ - const extern type##_T min_##name;\ - const extern type##_T max_##name; - -/** - * @brief 绑定的数值参数声明 - * - * @param name 参数名(变量名) - * @param type 参数类型 @enum ParamType_e 取值 - */ -#define PARAM_EXTERN_BIND_DAT(name, type) \ - typedef type##_T param_##name##_t;\ - const extern type##_T def_##name;\ - const extern type##_T min_##name;\ - const extern type##_T max_##name; - -/** - * @brief 字符串参数声明 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度 - */ -#define PARAM_EXTERN_STR(name, length) \ - typedef char* param_##name##_t;\ - extern char name[length];\ - const extern char def_##name[];\ - const extern param_size_t min_##name;\ - const extern param_size_t max_##name; - -/** - * @brief 绑定的字符串参数声明 - * - * @param name 参数名(变量名) - * @param length 字符串预留最大长度 - */ -#define PARAM_EXTERN_BIND_STR(name, length) \ - typedef char* param_##name##_t;\ - const extern char def_##name[];\ - const extern param_size_t min_##name;\ - const extern param_size_t max_##name; - -// 根据参数名获取该参数的类型定义 -#define PARAM_DECLTYPE(name) param_##name##_t - -#define PARAM_DAT_CUR_VALUE(name) name -#define PARAM_DAT_DEF_VALUE(name) def_##name -#define PARAM_DAT_MIN_VALUE(name) min_##name -#define PARAM_DAT_MAX_VALUE(name) max_##name - -#define PARAM_STR_CUR_VALUE(name) name -#define PARAM_STR_DEF_VALUE(name) def_##name -#define PARAM_STR_MIN_LENGTH(name) min_##name -#define PARAM_STR_MAX_LENGTH(name) max_##name - - -#define PARAM_NONE 0 -#define PARAM_DEF 1 -#define PARAM_MIN_MAX 2 - - -#define _PARAM_DAT_PARAM_NONE(name, def, min_or_max) (0) -#define _PARAM_DAT_PARAM_DEF(name, def, min_or_max) (name = def##_##name) -#define _PARAM_DAT_PARAM_MIN_MAX(name, def, min_or_max) (name = min_or_max##_##name) -#define _PARAM_STR_PARAM_NONE(name, def, min_or_max) (0) -#define _PARAM_STR_PARAM_DEF(name, def, min_or_max) (strcpy(name, def##_##name)) -#define _PARAM_STR_PARAM_MIN_MAX(name, def, min_or_max) (0) - -/** - * @brief 使用 PARAM_DEFINE_DAT_RANGE 定义的数值类型参数可以通过该宏进行最新值的范围校验 - * - * @attention 使用该宏请确保在头文件已经使用宏 PARAM_EXTERN_DAT 声明 - * @param name 参数名 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 参数小于最小值则为最小值, 参数大于最大值则为最大值; 但对字符串类型参数该选项无效, 即不做处理 - * @return 0,正常; 1,参数小于最小值; 2,参数大于最大值 - */ -#define PARAM_DAT_CHECK_RANGE(name, opt) \ - (PARAM_DAT_CUR_VALUE(name) < PARAM_DAT_MIN_VALUE(name) ? (_PARAM_DAT_##opt(name, def, min), 1) : \ - PARAM_DAT_CUR_VALUE(name) > PARAM_DAT_MAX_VALUE(name) ? (_PARAM_DAT_##opt(name, def, max), 2) : 0) - -/** - * @brief 使用 PARAM_DEFINE_STR_RANGE 定义的字符串类型参数可以通过该宏进行长度的校验 - * - * @attention 使用该宏请确保在头文件已经使用宏 PARAM_EXTERN_STR 声明 - * @param name 参数名 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 参数小于最小值则为最小值, 参数大于最大值则为最大值; 但对字符串类型参数该选项无效, 即不做处理 - * @return 0,正常; 1,字符串长度小于最小限制长度; 2,字符串长度大于最大限制长度 - */ -#define PARAM_STR_CHECK_RANGE(name, opt) \ - (strlen(PARAM_STR_CUR_VALUE(name)) < PARAM_STR_MIN_LENGTH(name) ? (_PARAM_STR_##opt(name, def, min), 1) : \ - strlen(PARAM_STR_CUR_VALUE(name)) > PARAM_STR_MAX_LENGTH(name) ? (_PARAM_STR_##opt(name, def, max), 2) : 0) - -/** - * @brief 使用 PARAM_DEFINE_DAT_RANGE 定义的参数可以通过该宏修改 - * - * @attention 使用该宏请确保在头文件已经使用宏 PARAM_EXTERN_DAT 声明 - * @param name 参数名 - * @param val 参数新的值 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 参数小于最小值则为最小值, 参数大于最大值则为最大值 - * @return 0,正常; 1,参数小于最小值; 2,参数大于最大值 - */ -#define PARAM_DAT_SET_NEW_VALUE(name, val, opt) \ - (val < PARAM_DAT_MIN_VALUE(name) ? (_PARAM_DAT_##opt(name, def, min), 1) : \ - val > PARAM_DAT_MAX_VALUE(name) ? (_PARAM_DAT_##opt(name, def, max), 2) : \ - (PARAM_DAT_CUR_VALUE(name) = val, 0)) - -/** - * @brief 使用 PARAM_DEFINE_STR_RANGE 定义的参数可以通过该宏修改 - * - * @attention 使用该宏请确保在头文件已经使用宏 PARAM_EXTERN_STR 声明 - * @param name 参数名 - * @param val 参数新的字符串 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 该选项无效, 参数不变, 即不做处理 - * @return 0,正常; 1,字符串长度小于最小限制长度; 2,字符串长度大于最大限制长度 - */ -#define PARAM_STR_SET_NEW_VALUE(name, str, opt) \ - (strlen(str) < PARAM_STR_MIN_LENGTH(name) ? (_PARAM_STR_##opt(name, def, min), 1) : \ - strlen(str) > PARAM_STR_MAX_LENGTH(name) ? (_PARAM_STR_##opt(name, def, max), 2) : \ - (strcpy(PARAM_STR_CUR_VALUE(name), str), 0)) - -// 得到参数表的元素数目 -#define PARAM_TABLE_SIZE(tab) (sizeof(tab) / sizeof(ParamInfo_t)) - - -extern int Param_Init(ParamManager_t* manager, ParamInfo_t* pParamTable, uint16_t count); -extern int Param_ResetParam(const ParamManager_t *manager); - -extern int Param_Load(ParamManager_t* manager, pfnLoad_cb pfnLoadCallback, pfnCheckError_cb pfnCheckError); -extern int Param_Save(ParamManager_t* manager, pfnSave_cb pfnSaveCallback, pfnCheckError_cb pfnCheckError); - -extern ParamInfo_t *Param_IterateList(ParamManager_t *manager, size_t *psIdx); - -extern const ParamInfo_t *Param_FindParamByName(ParamManager_t *manager, const char *pszName); -extern const ParamInfo_t *Param_FindParamByID(ParamManager_t* manager, uint16_t id); -extern const ParamInfo_t *Param_FindParamByParamPtr(ParamManager_t* manager, const void *curParamPtr); - -extern int Param_CheckRange(const ParamInfo_t *param, uint8_t opt); - -extern int Param_SetNewValue(const ParamInfo_t *param, const void *value, uint8_t opt); -extern bool Param_ResetDefaultValue(const ParamInfo_t *param); -extern bool Param_ResetMinValue(const ParamInfo_t *param); -extern bool Param_ResetMaxValue(const ParamInfo_t *param); - -#ifdef __cplusplus - } -#endif - -#endif // !_PARAM_H_ diff --git a/Param/inc/param_type.h b/Param/inc/param_type.h deleted file mode 100644 index f53e963..0000000 --- a/Param/inc/param_type.h +++ /dev/null @@ -1,138 +0,0 @@ -/** - ********************************************************************************************************************** - * @file param_type.h - * @brief 该文件提供参数管理框架所有函数原型 - * @author const_zpc any question please send mail to const_zpc@163.com - * @version V0.1 - * @date 2023-02-07 - ********************************************************************************************************************** - * - ********************************************************************************************************************** - */ - -/* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/ - -#ifndef _PARAM_TYPE_H_ -#define _PARAM_TYPE_H_ - -/* Includes ----------------------------------------------------------------------------------------------------------*/ -#include "param_cfg.h" -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** 参数权限属性 */ - -#define PARAM_ATTR_WR (PARAM_ATTR_READ | PARAM_ATTR_WRITE) // 读写权限 -#define PARAM_ATTR_NONE 0X00 // 无读写权限(不使用键值对模式储存参数则用于删除参数的属性) -#define PARAM_ATTR_READ 0X01 // 可读权限属性 -#define PARAM_ATTR_WRITE 0X02 // 可写权限属性 -#define PARAM_ATTR_RESET 0X04 // 可重置权限属性 -#define PARAM_ATTR_RANGE 0X10 // 支持最大/最小值校验 - - -typedef int8_t PARAM_INT8_T; -typedef int16_t PARAM_INT16_T; -typedef int32_t PARAM_INT32_T; -typedef int64_t PARAM_INT64_T; -typedef uint8_t PARAM_UINT8_T; -typedef uint16_t PARAM_UINT16_T; -typedef uint32_t PARAM_UINT32_T; -typedef uint64_t PARAM_UINT64_T; -typedef float PARAM_FLOAT_T; -typedef double PARAM_DOUBLE_T; -typedef char PARAM_STRING_T; - -#if PARAM_STRING_MAX_LENGTH >= 256 -typedef uint16_t param_size_t; -#else -typedef uint8_t param_size_t; -#endif - -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 -} ParamType_e; - -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; -#endif - void *pVoid; -} ParamTypePtr_u; - -/** - * @brief 定义无内存的参数结构体信息 - * - */ -typedef struct stuParamInfo -{ -#if (PARAM_NAME_MAX_LENGTH > 1) - const char *pszName; /*!< 名称 */ -#endif - uint16_t id; /*!< 唯一ID */ - uint8_t type; /*!< 类型, @enum ParamType_e */ - uint16_t length; /*!< 长度(字节数) */ - uint8_t attr; /*!< 属性 */ - ParamTypePtr_u unCurValuePtr; /*!< 当前值指针 */ - const ParamTypePtr_u unDefValuePtr; /*!< 默认值指针 */ - const ParamTypePtr_u unMinValuePtr; /*!< 最小值指针(字符串参数限制最小长度时需要 param_size_t 定义) */ - const ParamTypePtr_u unMaxValuePtr; /*!< 最大值指针(字符串参数限制最大长度时需要 param_size_t 定义) */ -} ParamInfo_t; - -typedef int (*pfnCheckError_cb)(const ParamInfo_t *pParamInfo); -typedef int (*pfnLoad_cb)(uint8_t *pBuf, uint16_t *len, bool *pisFinish); -typedef int (*pfnSave_cb)(const uint8_t *pBuf, uint16_t len, bool isFinish); - -// 定义参数管理结构体 -typedef struct -{ - ParamInfo_t *pParamTable; /*!< 参数信息数组表 */ - uint16_t count; /*!< 参数信息数组元素个数 */ -} ParamManager_t; - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/Param/src/cot_param.c b/Param/src/cot_param.c new file mode 100644 index 0000000..5ebcce5 --- /dev/null +++ b/Param/src/cot_param.c @@ -0,0 +1,1312 @@ +/** + ********************************************************************************************************************** + * @file cot_param.c + * @brief 该文件提供参数管理框架功能 + * @author const_zpc any question please send mail to const_zpc@163.com + * @version V1.0 + * @date 2023-12-08 + * + * @details 功能详细说明: + * + 参数修改和重置管理 + * + 参数序列化和反序列化 + * + * | 支持数目(1) | 数据1的长度和ID(0.5+0.5) | 数据1(n) | 数据2的长度和ID(1) | 数据2(n) | 数据3的长度和ID(1) | 数据3(n) | + * | 支持数目(1) | 数据1的长度和ID(1.0+1.0) | 数据1(n) | 数据2的长度和ID(2) | 数据2(n) | 数据3的长度和ID(2) | 数据3(n) | + * | 支持数目(1) | 数据1的长度和ID(1.5+1.5) | 数据1(n) | 数据2的长度和ID(3) | 数据2(n) | 数据3的长度和ID(3) | 数据3(n) | + * + ********************************************************************************************************************** + * 源码路径:https://gitee.com/cot_package/cot_param.git 具体问题及建议可在该网址填写 Issue + * + * + ********************************************************************************************************************** + */ + +/* Includes ----------------------------------------------------------------------------------------------------------*/ +#include "cot_param.h" +#include +#include + + +typedef union +{ + uint64_t u64val; + int64_t s64val; + double fVal; +#if COT_PARAM_USE_STRING_TYPE + char str[COT_PARAM_STRING_MAX_LENGTH + 2]; +#endif +} Value_u; + +static cotParamCheckRet_e ValidateRangeByVoid(const cotParamInfo_t *pParam, const void *pval); + +static uint8_t *SerializeUint(uint8_t *ptr, uint64_t value, uint8_t len) +{ + for (int i = 0; i < len; i++) + { + *ptr++ = value >> (i * 8); + } + + return ptr; +} + +static uint8_t *SerializeInt(uint8_t *ptr, int64_t value, uint8_t len) +{ + for (int i = 0; i < len; i++) + { + *ptr++ = value >> (i * 8); + } + + return ptr; +} + +static uint8_t *SerializeFloat(uint8_t *ptr, float value) +{ + uint32_t ivalue = *((uint32_t *)&value); + + for (int i = 0; i < sizeof(ivalue); i++) + { + *ptr++ = ivalue >> (i * 8); + } + + return ptr; +} + +static uint8_t *SerializeDouble(uint8_t *ptr, double value) +{ + uint64_t ivalue = *((uint64_t *)&value); + + for (int i = 0; i < sizeof(ivalue); i++) + { + *ptr++ = ivalue >> (i * 8); + } + + return ptr; +} + +static uint8_t *UnSerializeUint(const uint8_t *ptr, uint64_t *value, uint8_t len) +{ + *value = 0; + + for (int i = 0; i < len; i++) + { + *value |= (uint64_t)(*ptr++) << (i * 8); + } + + return (uint8_t *)ptr; +} + +static uint8_t *UnSerializeInt(const uint8_t *ptr, int64_t *value, uint8_t len) +{ + *value = 0; + + for (int i = 0; i < len; i++) + { + *value |= (int64_t)(*ptr++) << (i * 8); + } + + return (uint8_t *)ptr; +} + +static uint8_t *UnSerializeFloat(const uint8_t *ptr, float *value) +{ + uint32_t ivalue = 0; + + for (int i = 0; i < sizeof(ivalue); i++) + { + ivalue |= (uint32_t)(*ptr++) << (i * 8); + } + + *value = *((float *)&ivalue); + + return (uint8_t *)ptr; +} + +static uint8_t *UnSerializeDouble(const uint8_t *ptr, double *value) +{ + uint64_t ivalue = 0; + + for (int i = 0; i < sizeof(ivalue); i++) + { + ivalue |= (uint64_t)(*ptr++) << (i * 8); + } + + *value = *((double *)&ivalue); + + return (uint8_t *)ptr; +} + +static bool ResetParamValue(const cotParamInfo_t *pParam) +{ + if (pParam != NULL && (pParam->attr & COT_PARAM_ATTR_RESET)) + { +#if COT_PARAM_USE_STRING_TYPE + if (pParam->type != COT_PARAM_STRING) + { + memcpy(pParam->unCurValuePtr.pVoid, pParam->unDefValuePtr.pVoid, pParam->length); + } + else + { + strcpy(pParam->unCurValuePtr.pString, pParam->unDefValuePtr.pString); + } +#else + memcpy(pParam->unCurValuePtr.pVoid, pParam->unDefValuePtr.pVoid, pParam->length); +#endif + return true; + } + + return false; +} + +static bool ResetParamMinValue(const cotParamInfo_t *pParam) +{ + if (pParam != NULL && (pParam->attr & COT_PARAM_ATTR_RANGE)) + { +#if COT_PARAM_USE_STRING_TYPE + if (pParam->type != COT_PARAM_STRING) +#endif + { + memcpy(pParam->unCurValuePtr.pVoid, pParam->unMinValuePtr.pVoid, pParam->length); + } + + return true; + } + + return false; +} + +static bool ResetParamMaxValue(const cotParamInfo_t *pParam) +{ + if (pParam != NULL && (pParam->attr & COT_PARAM_ATTR_RANGE)) + { +#if COT_PARAM_USE_STRING_TYPE + if (pParam->type == COT_PARAM_STRING) + { + pParam->unCurValuePtr.pString[*pParam->unMaxValuePtr.pStringLength] = 0; + } + else +#endif + { + memcpy(pParam->unCurValuePtr.pVoid, pParam->unMaxValuePtr.pVoid, pParam->length); + } + + return true; + } + + return false; +} + +/** + * @brief 参数表初始化 + * + * @param pManager 参数表管理句柄 + * @param pParamTable 参数表 + * @param count 参数表元素数目, 可以通过宏 COT_PARAM_TABLE_SIZE 获取 + * @return 0,成功; -1,失败 + */ +int cotParam_Init(cotParamManager_t *pManager, cotParamInfo_t *pParamTable, uint16_t count) +{ + if (pManager == NULL || pParamTable == NULL) + { + return -1; + } + + for (uint16_t i = 0; i < count; i++) + { + if (pParamTable[i].unMaxValuePtr.pVoid == NULL || pParamTable[i].unMinValuePtr.pVoid == NULL) + { + pParamTable[i].attr &= ~COT_PARAM_ATTR_RANGE; + } + + if (!(pParamTable[i].attr & COT_PARAM_ATTR_READ)) + { + pParamTable[i].attr &= ~COT_PARAM_ATTR_WRITE; + } + + if (pParamTable[i].unDefValuePtr.pVoid == NULL) + { + pParamTable[i].attr &= ~COT_PARAM_ATTR_RESET; + } + + if (pParamTable[i].unCurValuePtr.pVoid == NULL) + { + return -1; + } + } + + pManager->pParamTable = pParamTable; + pManager->count = count; + return 0; +} + +/** + * @brief 重置所有参数,恢复为缺省值 + * + * @attention 无可重置权限的参数不能恢复为缺省值 + * @param pManager 参数表管理句柄 + * @return 0,成功; -1,失败 + */ +int cotParam_ResetDefault(const cotParamManager_t *pManager) +{ + if (pManager == NULL) + { + return -1; + } + + for (uint16_t i = 0; i < pManager->count; ++i) + { + ResetParamValue(&pManager->pParamTable[i]); + } + + return 0; +} + +/** + * @brief 检查所有参数当前值的范围并对超出范围时进行处理 + * + * @param param 参数信息 + * @param pfnCheckError 参数超出范围时的处理函数 + * @return 0,成功; -1,失败 + */ +int cotParam_Check(const cotParamManager_t* pManager, pfnCheckError_f pfnCheckError) +{ + cotParamCheckRet_e eCheckResult; + + if (pManager == NULL) + { + return -1; + } + + for (int id = 0; id < pManager->count; id++) + { + eCheckResult = ValidateRangeByVoid(&pManager->pParamTable[id], pManager->pParamTable[id].unCurValuePtr.pVoid); + + if (COT_PARAM_CHECK_OK != eCheckResult) + { + if (pfnCheckError != NULL) + { + pfnCheckError(&pManager->pParamTable[id], eCheckResult); + } + } + } + + return 0; +} + +static cotParamInfo_t *FindParamByParamPtr(const cotParamManager_t *pManager, const void *curParamPtr) +{ + for (uint16_t i = 0; i < pManager->count; ++i) + { + if (pManager->pParamTable[i].unCurValuePtr.pVoid == curParamPtr) + { + return &pManager->pParamTable[i]; + } + } + + return NULL; +} + +/** + * @brief 参数列表迭代器 + * + * @attention 无读取权限的参数会自动跳过 + * @param pManager 参数表管理句柄 + * @param psIdx 参数表起始索引 + * @return 参数信息 + */ +cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t *psIdx) +{ + cotParamInfo_t *p = NULL; + + if (pManager == NULL || psIdx == NULL || *psIdx > pManager->count) + { + return NULL; + } + + while (*psIdx < pManager->count) + { + if (pManager->pParamTable[*psIdx].attr & COT_PARAM_ATTR_READ) + { + p = &pManager->pParamTable[*psIdx]; + (*psIdx)++; + break; + } + + (*psIdx)++; + } + + return p; +} + +#if ( COT_PARAM_NAME_MAX_LENGTH > 1) +static cotParamInfo_t *FindParamByName(const cotParamManager_t *pManager, const char *pszName) +{ + for (uint16_t i = 0; i < pManager->count; ++i) + { + if (strcmp(pManager->pParamTable[i].pszName, pszName) == 0) + { + return &pManager->pParamTable[i]; + } + } + + return NULL; +} +#endif + +/** + * @brief 根据参数名称查找参数信息 + * + * @attention 无可读权限时会查找失败 + * @param pManager 参数表管理句柄 + * @param pszName 参数名称 + * @return 参数信息 + */ +const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName) +{ +#if ( COT_PARAM_NAME_MAX_LENGTH > 1) + cotParamInfo_t *pInfo; + + if (pManager != NULL) + { + pInfo = FindParamByName(pManager, pszName); + + if (pInfo->attr & COT_PARAM_ATTR_READ) + { + return pInfo; + } + } + +#endif + return NULL; +} + + +static cotParamInfo_t *FindParamByID(const cotParamManager_t *pManager, uint16_t id) +{ + for (uint16_t i = 0; i < pManager->count; ++i) + { + if (pManager->pParamTable[i].id == id) + { + return &pManager->pParamTable[i]; + } + } + + return NULL; +} + +/** + * @brief 根据参数ID查找参数信息 + * + * @attention 无可读权限时会查找失败 + * @param pManager 参数表管理句柄 + * @param id 参数ID + * @return 参数信息 + */ +const cotParamInfo_t *cotParam_FindParamByID(const cotParamManager_t *pManager, uint16_t id) +{ + cotParamInfo_t *pInfo; + + if (pManager != NULL) + { + pInfo = FindParamByID(pManager, id); + + if (pInfo->attr & COT_PARAM_ATTR_READ) + { + return pInfo; + } + } + + return NULL; +} + +/** + * @brief 根据当前参数数据地址指针查找参数信息 + * + * @attention 无可读权限时会查找失败 + * @param pManager 参数表管理句柄 + * @param pCurParam 当前参数数据指针 + * @return 参数信息 + */ +const cotParamInfo_t *cotParam_FindParamByParamPtr(const cotParamManager_t *pManager, const void *pCurParam) +{ + cotParamInfo_t *pInfo; + + if (pManager != NULL) + { + pInfo = FindParamByParamPtr(pManager, pCurParam); + + if (pInfo->attr & COT_PARAM_ATTR_READ) + { + return pInfo; + } + } + + return NULL; +} + + +// 验证参数是否在指定范围内 +static cotParamCheckRet_e ValidateRange(const cotParamInfo_t *pParam, const Value_u *pval) +{ + if (!(pParam->attr & COT_PARAM_ATTR_RANGE)) + { + return COT_PARAM_CHECK_OK; + } + + switch (pParam->type) + { + case COT_PARAM_INT8: + if (pval->s64val < *pParam->unMinValuePtr.pInt8) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->s64val > *pParam->unMaxValuePtr.pInt8) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; + + case COT_PARAM_INT16: + if (pval->s64val < *pParam->unMinValuePtr.pInt16) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->s64val > *pParam->unMaxValuePtr.pInt16) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; + + case COT_PARAM_INT32: + if (pval->s64val < *pParam->unMinValuePtr.pInt32) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->s64val > *pParam->unMaxValuePtr.pInt32) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_INT64: + if (pval->s64val < *pParam->unMinValuePtr.pInt64) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->s64val > *pParam->unMaxValuePtr.pInt64) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#endif + case COT_PARAM_UINT8: + if (pval->u64val < *pParam->unMinValuePtr.pUint8) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->u64val > *pParam->unMaxValuePtr.pUint8) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; + + case COT_PARAM_UINT16: + if (pval->u64val < *pParam->unMinValuePtr.pUint16) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->u64val > *pParam->unMaxValuePtr.pUint16) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; + + case COT_PARAM_UINT32: + if (pval->u64val < *pParam->unMinValuePtr.pUint32) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->u64val > *pParam->unMaxValuePtr.pUint32) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_UINT64: + if (pval->u64val < *pParam->unMinValuePtr.pUint64) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->u64val > *pParam->unMaxValuePtr.pUint64) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#endif + case COT_PARAM_FLOAT: + if (pval->fVal < *pParam->unMinValuePtr.pFloat) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->fVal > *pParam->unMaxValuePtr.pFloat) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_DOUBLE: + if (pval->fVal < *pParam->unMinValuePtr.pDouble) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (pval->fVal > *pParam->unMaxValuePtr.pDouble) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#endif +#if COT_PARAM_USE_STRING_TYPE + case COT_PARAM_STRING: + if (strlen(pval->str) < *pParam->unMinValuePtr.pStringLength) + { + return COT_PARAM_CHECK_OVER_MIN; + } + else if (strlen(pval->str) > *pParam->unMaxValuePtr.pStringLength) + { + return COT_PARAM_CHECK_OVER_MAX; + } + break; +#endif + default: + break; + } + + return COT_PARAM_CHECK_OK; +} + +static cotParamCheckRet_e ValidateRangeByVoid(const cotParamInfo_t *pParam, const void *pval) +{ + Value_u uValue; + + switch (pParam->type) + { + case COT_PARAM_INT8: + uValue.s64val = *(COT_PARAM_INT8_T *)pval; + break; + + case COT_PARAM_INT16: + uValue.s64val = *(COT_PARAM_INT16_T *)pval; + break; + + case COT_PARAM_INT32: + uValue.s64val = *(COT_PARAM_INT32_T *)pval; + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_INT64: + uValue.s64val = *(COT_PARAM_INT64_T *)pval; + break; +#endif + case COT_PARAM_UINT8: + uValue.s64val = *(COT_PARAM_UINT8_T *)pval; + break; + + case COT_PARAM_UINT16: + uValue.s64val = *(COT_PARAM_UINT16_T *)pval; + break; + + case COT_PARAM_UINT32: + uValue.s64val = *(COT_PARAM_UINT32_T *)pval; + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_UINT64: + uValue.s64val = *(COT_PARAM_UINT64_T *)pval; + break; +#endif + case COT_PARAM_FLOAT: + uValue.fVal = *(COT_PARAM_FLOAT_T *)pval; + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_DOUBLE: + uValue.fVal = *(COT_PARAM_DOUBLE_T *)pval; + break; +#endif +#if COT_PARAM_USE_STRING_TYPE + case COT_PARAM_STRING: + memcpy(uValue.str, pval, strlen(pval) > COT_PARAM_STRING_MAX_LENGTH ? COT_PARAM_STRING_MAX_LENGTH + 2 : strlen(pval) + 1); + uValue.str[COT_PARAM_STRING_MAX_LENGTH + 2] = '\0'; + break; +#endif + default: + return COT_PARAM_CHECK_OK; + } + + return ValidateRange(pParam, &uValue); +} + +/** + * @brief 校验当前参数值并进行处理 + * + * @attention 字符串类型参数若设置了最长的长度,超长则会进行截断,但是小于最小长度时则不做处理 + * @param pParam 参数信息 + * @param eResetOpt 参数值超出范围的处理选项 @enum cotParamResetOpt_e + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamCheckProcess(const cotParamInfo_t *pParam, cotParamResetOpt_e eResetOpt) +{ + cotParamCheckRet_e eCheckResult; + + if (pParam == NULL) + { + return -1; + } + + eCheckResult = ValidateRangeByVoid(pParam, pParam->unCurValuePtr.pVoid); + + if (eCheckResult != COT_PARAM_CHECK_OK) + { + if (eResetOpt == COT_PARAM_RESET_DEF) + { + ResetParamValue(pParam); + } + else if (eResetOpt == COT_PARAM_RESET_MIN || (eResetOpt == COT_PARAM_RESET_MIN_MAX && eCheckResult == COT_PARAM_CHECK_OVER_MIN)) + { + ResetParamMinValue(pParam); + } + else if (eResetOpt == COT_PARAM_RESET_MAX || (eResetOpt == COT_PARAM_RESET_MIN_MAX && eCheckResult == COT_PARAM_CHECK_OVER_MAX)) + { + ResetParamMaxValue(pParam); + } + } + + return 0; +} + +/** + * @brief 更新参数值同时进行校验处理 + * + * @param pParam 参数信息 + * @param pNewValue 新的参数值 + * @param eResetOpt 新的参数值超出范围的处理选项 @enum cotParamResetOpt_e + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewValue, cotParamResetOpt_e eResetOpt) +{ + cotParamCheckRet_e eCheckResult; + + if (pParam == NULL) + { + return -1; + } + + eCheckResult = ValidateRangeByVoid(pParam, pNewValue); + + if (eCheckResult != COT_PARAM_CHECK_OK) + { + if (eResetOpt == COT_PARAM_RESET_DEF) + { + ResetParamValue(pParam); + } + else if (eResetOpt == COT_PARAM_RESET_MIN || (eResetOpt == COT_PARAM_RESET_MIN_MAX && eCheckResult == COT_PARAM_CHECK_OVER_MIN)) + { + ResetParamMinValue(pParam); + } + else if (eResetOpt == COT_PARAM_RESET_MAX || (eResetOpt == COT_PARAM_RESET_MIN_MAX && eCheckResult == COT_PARAM_CHECK_OVER_MAX)) + { + ResetParamMaxValue(pParam); + } + } + else + { +#if COT_PARAM_USE_STRING_TYPE + if (pParam->type != COT_PARAM_STRING) + { + memcpy(pParam->unCurValuePtr.pVoid, pNewValue, pParam->length); + } + else + { + strcpy(pParam->unCurValuePtr.pString, pNewValue); + } +#else + memcpy(pParam->unCurValuePtr.pVoid, pNewValue, pParam->length); +#endif + } + + return 0; +} + +/** + * @brief 校验当前参数值 + * + * @param[in] pParam 参数信息 + * @param[out] peCheckResult 校验结果 + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult) +{ + if (pParam == NULL || peCheckResult == NULL) + { + return -1; + } + + *peCheckResult = ValidateRangeByVoid(pParam, pParam->unCurValuePtr.pVoid); + return 0; +} + +/** + * @brief 重置参数为缺省值 + * + * @attention 无缺省值的参数会失败 + * @param pParam 参数信息 + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamResetDefValue(const cotParamInfo_t *pParam) +{ + if (pParam == NULL) + { + return -1; + } + + if (!ResetParamValue(pParam)) + { + return -1; + } + + return 0; +} + +/** + * @brief 重置参数为最小值 + * + * @attention 字符串类型参数该功能无效, 无最大最小值的参数也会失败 + * @param pParam 参数信息 + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamResetMinValue(const cotParamInfo_t *pParam) +{ + if (pParam == NULL) + { + return -1; + } + + if (!ResetParamMinValue(pParam)) + { + return -1; + } + + return 0; +} + +/** + * @brief 重置参数为最大值 + * + * @attention 字符串类型参数该功能无效, 无最大最小值的参数也会失败 + * @param pParam 参数信息 + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamResetMaxValue(const cotParamInfo_t *pParam) +{ + if (pParam == NULL) + { + return -1; + } + + if (!ResetParamMaxValue(pParam)) + { + return -1; + } + + return 0; +} + +static uint16_t ParamInfoFormStream(cotParamInfo_t *pParam, const uint8_t *pbuf) +{ + switch (pParam->type) + { + case COT_PARAM_INT8: + { + int64_t val = 0; + pbuf = UnSerializeInt(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pInt8 = (COT_PARAM_INT8_T)val; + } + break; + + case COT_PARAM_INT16: + { + int64_t val = 0; + pbuf = UnSerializeInt(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pInt16 = (COT_PARAM_INT16_T)val; + } + break; + + case COT_PARAM_INT32: + { + int64_t val = 0; + pbuf = UnSerializeInt(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pInt32 = (COT_PARAM_INT32_T)val; + } + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_INT64: + { + int64_t val = 0; + pbuf = UnSerializeInt(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pInt64 = (COT_PARAM_INT64_T)val; + } + break; +#endif + case COT_PARAM_UINT8: + { + uint64_t val = 0; + pbuf = UnSerializeUint(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pUint8 = (COT_PARAM_UINT8_T)val; + } + break; + + case COT_PARAM_UINT16: + { + uint64_t val = 0; + pbuf = UnSerializeUint(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pUint16 = (COT_PARAM_UINT16_T)val; + } + break; + + case COT_PARAM_UINT32: + { + uint64_t val = 0; + pbuf = UnSerializeUint(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pUint32= (COT_PARAM_UINT32_T)val; + } + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_UINT64: + { + uint64_t val = 0; + pbuf = UnSerializeUint(pbuf, &val, pParam->length); + *pParam->unCurValuePtr.pUint64 = (COT_PARAM_UINT64_T)val; + } + break; +#endif + case COT_PARAM_FLOAT: + pbuf = UnSerializeFloat(pbuf, pParam->unCurValuePtr.pFloat); + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_DOUBLE: + pbuf = UnSerializeDouble(pbuf, pParam->unCurValuePtr.pDouble); + break; +#endif +#if COT_PARAM_USE_STRING_TYPE + case COT_PARAM_STRING: + memcpy(pParam->unCurValuePtr.pString, &pbuf[0], pParam->length); + break; +#endif + default: + return 0; // 不支持的参数类型 + } + + return pParam->length; +} + +static uint8_t *MoveBufToBase(uint8_t *pbuf, uint32_t length) +{ + memmove(pbuf - length, pbuf, length); + return (pbuf - length); +} + +/** + * @brief 加载数据 + * + * @param pManager 参数表管理句柄 + * @param pfnLoadCallback 加载回调函数 + * @return 0,成功; -1,失败 + */ +int cotParam_Load(const cotParamManager_t *pManager, pfnLoad_f pfnLoadCallback) +{ +#if COT_PARAM_USE_STRING_TYPE + uint8_t buf[sizeof(cotParamInfo_t) + COT_PARAM_STRING_MAX_LENGTH]; +#else + uint8_t buf[sizeof(cotParamInfo_t)]; +#endif + uint8_t *ptr = buf; + + if (pManager == NULL || pfnLoadCallback == NULL) + { + return -1; + } + + bool isFinish = false; + uint16_t length = 0; + uint16_t paramLength = 0; + uint16_t id = 0; + cotParamInfo_t *pParamInfo; + uint8_t keyLength = 0; +#if COT_PARAM_USE_KEY_VALUE + uint64_t key = 0; +#endif + + do + { + length = sizeof(buf) - (ptr - buf); + + if (pfnLoadCallback(ptr, &length, &isFinish) != 0) + { + return -2; + } + + if (isFinish || length == 0) + { + continue; + } + + length += (ptr - buf); + ptr = buf; + +#if COT_PARAM_USE_KEY_VALUE + if (keyLength == 0) + { + keyLength = ptr[0]; + ptr++; + length -= 1; + } + + while (length > keyLength) + { + UnSerializeUint(ptr, &key, keyLength); + +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + id = (key >> 4) & 0x0F; + paramLength = key & 0x0F; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + id = (key >> 8) & 0xFF; + paramLength = key & 0xFF; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + id = (key >> 12) & 0xFFF; + paramLength = key & 0xFFF; +#endif + + if (length < (paramLength + keyLength)) + { + break; + } + + ptr += keyLength; + length -= keyLength; + + pParamInfo = (cotParamInfo_t *)FindParamByID(pManager, id); + + if (pParamInfo == NULL || paramLength != pParamInfo->length) + { + ptr += paramLength; + length -= paramLength; + continue; + } + + ParamInfoFormStream(pParamInfo, ptr); + ptr += paramLength; + length -= paramLength; + } + +#else + + while (length >= 1) + { + pParamInfo = &pManager->pParamTable[id]; + + if (length < pParamInfo->length) + { + break; + } + + ParamInfoFormStream(pParamInfo, ptr); + ptr += pParamInfo->length; + length -= pParamInfo->length; + id++; + } + +#endif + ptr = MoveBufToBase(ptr, ptr - buf); + ptr += length; + } while (!isFinish); + + return 0; +} + +static uint16_t ParamInfoToStream(uint8_t *pbuf, cotParamInfo_t *pParam) +{ + switch (pParam->type) + { + case COT_PARAM_INT8: + pbuf = SerializeInt(pbuf, *(COT_PARAM_INT8_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; + + case COT_PARAM_INT16: + pbuf = SerializeInt(pbuf, *(COT_PARAM_INT16_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; + + case COT_PARAM_INT32: + pbuf = SerializeInt(pbuf, *(COT_PARAM_INT32_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_INT64: + pbuf = SerializeInt(pbuf, *(COT_PARAM_INT64_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; +#endif + case COT_PARAM_UINT8: + pbuf = SerializeUint(pbuf, *(COT_PARAM_UINT8_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; + + case COT_PARAM_UINT16: + pbuf = SerializeUint(pbuf, *(COT_PARAM_UINT16_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; + + case COT_PARAM_UINT32: + pbuf = SerializeUint(pbuf, *(COT_PARAM_UINT32_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_UINT64: + pbuf = SerializeUint(pbuf, *(COT_PARAM_UINT64_T *)pParam->unCurValuePtr.pVoid, pParam->length); + break; +#endif + case COT_PARAM_FLOAT: + pbuf = SerializeFloat(pbuf, *(COT_PARAM_FLOAT_T *)pParam->unCurValuePtr.pVoid); + break; +#if COT_PARAM_USE_64_BIT_LENGTH + case COT_PARAM_DOUBLE: + pbuf = SerializeDouble(pbuf, *(COT_PARAM_DOUBLE_T *)pParam->unCurValuePtr.pVoid); + break; +#endif +#if COT_PARAM_USE_STRING_TYPE + case COT_PARAM_STRING: + memcpy(&pbuf[0], pParam->unCurValuePtr.pString, pParam->length); + break; +#endif + default: + return 0; // 不支持的参数类型 + } + + return pParam->length; +} + +/** + * @brief 保存数据 + * + * @param pManager 参数表管理句柄 + * @param pfnSaveCallback 保存回调函数 + * @return 0,成功; -1,失败 + */ +int cotParam_Save(const cotParamManager_t *pManager, pfnSave_f pfnSaveCallback) +{ +#if COT_PARAM_USE_STRING_TYPE + uint8_t buf[sizeof(cotParamInfo_t) + COT_PARAM_STRING_MAX_LENGTH]; +#else + uint8_t buf[sizeof(cotParamInfo_t)]; +#endif + uint8_t *ptr = buf; + uint16_t length = 0; +#if COT_PARAM_USE_KEY_VALUE + uint64_t key = 0; +#endif + + if (pManager == NULL || pfnSaveCallback == NULL) + { + return -1; + } + +#if COT_PARAM_USE_KEY_VALUE + buf[0] = COT_PARAM_SUPPORT_NUM; + + if (pfnSaveCallback(buf, 1, false) != 0) + { + return -2; + } +#endif + + for (int i = 0; i < pManager->count; i++) + { + ptr = buf; + length = 0; +#if COT_PARAM_USE_KEY_VALUE +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + key = (pManager->pParamTable[i].id << 4) | (pManager->pParamTable[i].length & 0x0F); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + key = (pManager->pParamTable[i].id << 8) | (pManager->pParamTable[i].length & 0xFF); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + key = (pManager->pParamTable[i].id << 12) | (pManager->pParamTable[i].length & 0xFFF); +#endif + ptr = SerializeUint(ptr, key, COT_PARAM_SUPPORT_NUM); + length += COT_PARAM_SUPPORT_NUM; +#endif + length = ParamInfoToStream(&buf[length], &pManager->pParamTable[i]); + ptr += length; + + if (pfnSaveCallback(buf, (ptr - buf), false) != 0) + { + return -2; + } + } + + if (pfnSaveCallback(buf, 0, true) != 0) + { + return -2; + } + + return 0; +} + +/** + * @brief 获取参数序列化所需要的内存大小 + * + * @param[in] pManager 参数表管理句柄 + * @return 参数序列化所需要的内存大小 + */ +uint32_t cotParam_GetSerializeSize(const cotParamManager_t* pManager) +{ + uint32_t length = 1 + 10; // 预留 + uint16_t idx = 0; + + if (pManager == NULL) + { + return 0; + } + + while (idx < pManager->count) + { +#if COT_PARAM_USE_KEY_VALUE + length += (pManager->pParamTable[idx++].length + COT_PARAM_SUPPORT_NUM); +#else + length += pManager->pParamTable[idx++].length; +#endif + } + + return length; +} + +/** + * @brief 获取参数序列化后的数据 + * + * @param[in] pManager 参数表管理句柄 + * @param[out] pbuf 存参数序列化后的数据 + * @return 0,成功; -1,失败 + */ +uint32_t cotParam_Serialize(const cotParamManager_t* pManager, uint8_t *pbuf) +{ + if (pManager == NULL) + { + return 0; + } + + uint8_t *ptr = pbuf; +#if COT_PARAM_USE_KEY_VALUE + uint64_t key = 0; +#endif + +#if COT_PARAM_USE_KEY_VALUE + *ptr = COT_PARAM_SUPPORT_NUM; + ptr++; +#endif + + for (int i = 0; i < pManager->count; i++) + { +#if COT_PARAM_USE_KEY_VALUE +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + key = (pManager->pParamTable[i].id << 4) | (pManager->pParamTable[i].length & 0x0F); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + key = (pManager->pParamTable[i].id << 8) | (pManager->pParamTable[i].length & 0xFF); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + key = (pManager->pParamTable[i].id << 12) | (pManager->pParamTable[i].length & 0xFFF); +#endif + ptr = SerializeUint(ptr, key, COT_PARAM_SUPPORT_NUM); +#endif + ptr += ParamInfoToStream(ptr, &pManager->pParamTable[i]); + } + + return ptr - pbuf; +} + +/** + * @brief 参数反序列化 + * + * @param[in] pManager 参数表管理句柄 + * @param[in] pbuf 参数需要反序列化的数据 + * @param[in] length 反序列化的数据长度 + * @return 0,成功; -1,失败 + */ +int cotParam_Deserialization(const cotParamManager_t* pManager, const uint8_t *pbuf, uint32_t length) +{ + if (pManager == NULL) + { + return -1; + } + + const uint8_t *ptr = pbuf; + + uint16_t id = 0; + cotParamInfo_t *pParamInfo; + +#if COT_PARAM_USE_KEY_VALUE + uint8_t keyLength = 0; + uint16_t paramLength = 0; + uint64_t key = 0; +#endif + +#if COT_PARAM_USE_KEY_VALUE + if (keyLength == 0) + { + keyLength = ptr[0]; + ptr++; + length -= 1; + } + + while (length > keyLength) + { + ptr = UnSerializeUint(ptr, &key, keyLength); + +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + id = (key >> 4) & 0x0F; + paramLength = key & 0x0F; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + id = (key >> 8) & 0xFF; + paramLength = key & 0xFF; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + id = (key >> 12) & 0xFFF; + paramLength = key & 0xFFF; +#endif + + if (length < (paramLength + keyLength)) + { + break; + } + + length -= (keyLength + paramLength); + + pParamInfo = (cotParamInfo_t *)FindParamByID(pManager, id); + + if (pParamInfo == NULL || paramLength != pParamInfo->length) + { + ptr += paramLength; + continue; + } + + ParamInfoFormStream(pParamInfo, ptr); + ptr += paramLength; + } + +#else + while (length >= 1) + { + pParamInfo = &pManager->pParamTable[id]; + + if (length < pParamInfo->length) + { + break; + } + + ParamInfoFormStream(pParamInfo, ptr); + ptr += pParamInfo->length; + length -= pParamInfo->length; + id++; + } +#endif + + return 0; +} + + diff --git a/test/makefile b/test/makefile index 2c67998..d3cf3d5 100644 --- a/test/makefile +++ b/test/makefile @@ -1,6 +1,9 @@ -CURR_DIR_PATH=$(shell pwd) +ifeq ($(OS),Windows_NT) + CURR_DIR_PATH = $(CURDIR) +else + CURR_DIR_PATH = $(shell pwd) +endif -CURR_DIR_PATH="C:\Users\const\Desktop\param\param\test" GCC:=gcc INC+=-I${CURR_DIR_PATH}/../Param/inc @@ -9,7 +12,7 @@ INC+=-I${CURR_DIR_PATH}/ SRC+=${CURR_DIR_PATH}/param_test.c SRC+=${CURR_DIR_PATH}/unity/unity.c -SRC+=${CURR_DIR_PATH}/../Param/src/param.c +SRC+=${CURR_DIR_PATH}/../Param/src/cot_param.c AIM_NAME:=param_test diff --git a/test/param_test.c b/test/param_test.c index f73877a..77609ea 100644 --- a/test/param_test.c +++ b/test/param_test.c @@ -2,7 +2,35 @@ #include #include #include "unity.h" -#include "param.h" +#include "cot_param.h" + + +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 +const uint8_t kBuf[200] = {0x01, 0x12, 0x0a, 0x00, 0x22, 0x14, 0x00, 0x34, 0x9a, 0x99, 0x49, + 0x40, 0x48, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x09, 0x40, 0x5d, 0x61, 0x62, 0x63, 0x64, 0x65, + 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x0a, 0x72, 0x64, 0x00, 0x84, 0xe8, 0x03, + 0x00, 0x00, 0x98, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa1, 0x0a, 0xb2, 0x64, 0x00, 0xc4, + 0xe8, 0x03, 0x00, 0x00, 0xd8, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x00, 0x00, 0xf4, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 +const uint8_t kBuf[200] = {0x02, 0x02, 0x01, 0x0a, 0x00, 0x02, 0x02, 0x14, 0x00, 0x04, 0x03, 0x9a, 0x99, + 0x49, 0x40, 0x08, 0x04, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x09, 0x40, 0x0d, 0x05, 0x61, 0x62, 0x63, 0x64, + 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x0a, 0x02, 0x07, 0x64, 0x00, 0x04, 0x08, + 0xe8, 0x03, 0x00, 0x00, 0x08, 0x09, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x0a, 0x02, + 0x0b, 0x64, 0x00, 0x04, 0x0c, 0xe8, 0x03, 0x00, 0x00, 0x08, 0x0d, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x0e, 0x00, 0x00, 0x04, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 +const uint8_t kBuf[200] = {0x03, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x02, 0x20, 0x00, 0x14, 0x00, 0x04, 0x30, 0x00, + 0x9a, 0x99, 0x49, 0x40, 0x08, 0x40, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x09, 0x40, 0x0d, 0x50, 0x00, 0x61, + 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x60, 0x00, 0x0a, 0x02, 0x70, 0x00, + 0x64, 0x00, 0x04, 0x80, 0x00, 0xe8, 0x03, 0x00, 0x00, 0x08, 0x90, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0xa0, 0x00, 0x0a, 0x02, 0xb0, 0x00, 0x64, 0x00, 0x04, 0xc0, 0x00, 0xe8, 0x03, 0x00, 0x00, 0x08, 0xd0, + 0x00, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xe0, 0x00, 0x00, 0x00, 0x04, 0xf0, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +#endif typedef struct { @@ -13,78 +41,61 @@ typedef struct static ParamDemo_t sg_tTest; -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); +COT_PARAM_INT16_T g_test_1 = 10; +COT_PARAM_UINT16_T g_test_2 = 20; +COT_PARAM_FLOAT_T g_test_float = 3.15; +#if COT_PARAM_USE_64_BIT_LENGTH +COT_PARAM_DOUBLE_T g_test_double = 3.15; #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); +#if COT_PARAM_USE_STRING_TYPE +char g_test_str[13] = "abcdef"; #endif +COT_PARAM_INT8_T g_test_s8 = 10; +COT_PARAM_INT16_T g_test_s16 = 100; +COT_PARAM_INT32_T g_test_s32 = 1000; -PARAM_DEFINE_BIND_DAT_RANGE(sg_tTest_test1, PARAM_UINT16, 20, 10, 2000); // 为即将绑定的变量定义相关参数信息 -PARAM_DEFINE_BIND_DAT(sg_tTest_test2, PARAM_FLOAT); // 为即将绑定的变量定义相关参数信息,初值为sg_tTest 变量定义时的初值 -#if PARAM_USE_STRING_TYPE -PARAM_DEFINE_BIND_STR_RANGE(sg_tTest_str, sizeof(sg_tTest.str), "const-zpc", 6); +#if COT_PARAM_USE_64_BIT_LENGTH +COT_PARAM_INT64_T g_test_s64 = 8000; +#endif +COT_PARAM_UINT8_T g_test_u8 = 10; +COT_PARAM_UINT16_T g_test_u16 = 100; +COT_PARAM_UINT32_T g_test_u32 = 1000; +#if COT_PARAM_USE_64_BIT_LENGTH +COT_PARAM_UINT64_T g_test_u64 = 8000; #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), +cotParamInfo_t sg_ParamTable[] = { + COT_PARAM_ITEM_BIND(1, g_test_1, COT_PARAM_INT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_test_float, COT_PARAM_FLOAT, COT_PARAM_ATTR_READ, 3.15, -10, 10), +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_ITEM_BIND(4, g_test_double, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -10, 10), #endif -#if PARAM_USE_STRING_TYPE - PARAM_ITEM_STR_RANGE(5, g_test_str, PARAM_ATTR_WR), +#if COT_PARAM_USE_STRING_TYPE + COT_PARAM_ITEM_BIND(5, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 5, 10), #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), + COT_PARAM_ITEM_BIND(6, g_test_s8, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 10, -10, 15), + COT_PARAM_ITEM_BIND(7, g_test_s16, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -100, 3000), + COT_PARAM_ITEM_BIND(8, g_test_s32, COT_PARAM_INT32, COT_PARAM_ATTR_WR, 1000, -900, 10000), +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_ITEM_BIND(9, g_test_s64, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 8000, -100, 1000000), #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), + + COT_PARAM_ITEM_BIND(10, g_test_u8, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 10, 5, 15), + COT_PARAM_ITEM_BIND(11, g_test_u16, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 100, 90, 3000), + COT_PARAM_ITEM_BIND(12, g_test_u32, COT_PARAM_UINT32, COT_PARAM_ATTR_NONE, 1000, 900, 10000), + +#if COT_PARAM_USE_64_BIT_LENGTH + COT_PARAM_ITEM_BIND(13, g_test_u64, COT_PARAM_UINT64, COT_PARAM_ATTR_WR, 8000, 100, 1000000), #endif - PARAM_ITEM_DAT_RANGE_BIND(14, sg_tTest_test1, sg_tTest.test1, PARAM_ATTR_WR), - PARAM_ITEM_DAT_BIND(15, sg_tTest_test2, sg_tTest.test2, PARAM_ATTR_WR), -#if PARAM_USE_STRING_TYPE - PARAM_ITEM_STR_RANGE_BIND(16, sg_tTest_str, sg_tTest.str, PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(14, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), + COT_PARAM_ITEM_BIND(15, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), +#if COT_PARAM_USE_STRING_TYPE + COT_PARAM_ITEM_BIND(16, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), #endif }; -static ParamManager_t sg_tParamManager; - -PARAM_EXTERN_DAT(g_test_1, PARAM_INT16); -PARAM_EXTERN_DAT(g_test_2, PARAM_UINT16); -PARAM_EXTERN_DAT(g_test_float, PARAM_FLOAT); -PARAM_EXTERN_DAT(g_test_double, PARAM_DOUBLE); -PARAM_EXTERN_STR(g_test_str, 10); -PARAM_EXTERN_DAT(g_test_s8, PARAM_INT8); -PARAM_EXTERN_DAT(g_test_s16, PARAM_INT16); -PARAM_EXTERN_DAT(g_test_s32, PARAM_INT32); -PARAM_EXTERN_DAT(g_test_s64, PARAM_INT64); -PARAM_EXTERN_DAT(g_test_u8, PARAM_UINT8); -PARAM_EXTERN_DAT(g_test_u16, PARAM_UINT16); -PARAM_EXTERN_DAT(g_test_u32, PARAM_UINT32); -PARAM_EXTERN_DAT(g_test_u64, PARAM_UINT64); +static cotParamManager_t sg_tParamManager; void setUp(void) { @@ -98,82 +109,112 @@ void tearDown(void) void test_ParamInit(void) { - Param_Init(&sg_tParamManager, sg_ParamTable, PARAM_TABLE_SIZE(sg_ParamTable)); + cotParam_Init(&sg_tParamManager, sg_ParamTable, COT_PARAM_TABLE_SIZE(sg_ParamTable)); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable, sg_ParamTable); - TEST_ASSERT_EQUAL_UINT(sg_tParamManager.count, PARAM_TABLE_SIZE(sg_ParamTable)); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.count, COT_PARAM_TABLE_SIZE(sg_ParamTable)); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].id, 1); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].length, 2); - TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].type, PARAM_INT16); - TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[0].attr, PARAM_ATTR_WR); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].type, COT_PARAM_INT16); + TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[0].attr, COT_PARAM_ATTR_WR); TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[0].pszName, "g_test_1"); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unCurValuePtr.pVoid , &PARAM_DAT_CUR_VALUE(g_test_1)); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unDefValuePtr.pVoid , NULL); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unMinValuePtr.pVoid , NULL); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unMaxValuePtr.pVoid , NULL); + TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unCurValuePtr.pVoid , &g_test_1); + TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unDefValuePtr.pVoid); + TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unMinValuePtr.pVoid); + TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unMaxValuePtr.pVoid); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].id, 2); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].length, 2); - TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].type, PARAM_UINT16); - TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[1].attr, PARAM_ATTR_WR | PARAM_ATTR_RESET); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].type, COT_PARAM_UINT16); + TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[1].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET); TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[1].pszName, "g_test_2"); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unCurValuePtr.pVoid , &PARAM_DAT_CUR_VALUE(g_test_2)); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unDefValuePtr.pVoid , &PARAM_DAT_DEF_VALUE(g_test_2)); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unMinValuePtr.pVoid , NULL); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unMaxValuePtr.pVoid , NULL); + TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unCurValuePtr.pVoid , &g_test_2); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[1].unDefValuePtr.pVoid); + TEST_ASSERT_NULL(sg_tParamManager.pParamTable[1].unMinValuePtr.pVoid); + TEST_ASSERT_NULL(sg_tParamManager.pParamTable[1].unMaxValuePtr.pVoid); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].id, 3); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].length, 4); - TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].type, PARAM_FLOAT); - TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[2].attr, PARAM_ATTR_READ | PARAM_ATTR_RESET | PARAM_ATTR_RANGE); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].type, COT_PARAM_FLOAT); + TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[2].attr, COT_PARAM_ATTR_READ | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE); TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[2].pszName, "g_test_float"); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unCurValuePtr.pVoid , &PARAM_DAT_CUR_VALUE(g_test_float)); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unDefValuePtr.pVoid , &PARAM_DAT_DEF_VALUE(g_test_float)); - 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)); + TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unCurValuePtr.pVoid , &g_test_float); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unDefValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unMinValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unMaxValuePtr.pVoid); -#if PARAM_USE_STRING_TYPE -#if PARAM_USE_64_BIT_LENGTH +#if COT_PARAM_USE_STRING_TYPE +#if COT_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); - TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[4].attr, PARAM_ATTR_WR | PARAM_ATTR_RESET | PARAM_ATTR_RANGE); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].length, sizeof(g_test_str)); + TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].type, COT_PARAM_STRING); + TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[4].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE); TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[4].pszName, "g_test_str"); - TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unCurValuePtr.pVoid , PARAM_STR_CUR_VALUE(g_test_str)); - 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)); + TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unCurValuePtr.pVoid , g_test_str); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unDefValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unMinValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unMaxValuePtr.pVoid); #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_UINT(sg_tParamManager.pParamTable[3].type, COT_PARAM_STRING); + TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[3].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_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)); + TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unCurValuePtr.pVoid , COT_PARAM_STR_CUR_VALUE(g_test_str)); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unDefValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unMinValuePtr.pVoid); + TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unMaxValuePtr.pVoid); #endif #endif } +// #define HEX_PRINTF(str, hex, len) \ +// printf("%s: [%d] -> ", str, len);\ +// for (int i = 0; i < (len); i++){printf("0x%02x, ", hex[i]);}\ +// printf("\n"); + +void test_ParamSerializeSize(void) +{ + uint32_t length; + uint8_t buf[200]; +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + TEST_ASSERT_EQUAL_UINT(112, cotParam_GetSerializeSize(&sg_tParamManager)); + length = cotParam_Serialize(&sg_tParamManager, buf); + // HEX_PRINTF("", buf, length); + TEST_ASSERT_EQUAL_UINT(102, length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, buf, length); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + TEST_ASSERT_EQUAL_UINT(128, cotParam_GetSerializeSize(&sg_tParamManager)); + length = cotParam_Serialize(&sg_tParamManager, buf); + // HEX_PRINTF("", buf, length); + TEST_ASSERT_EQUAL_UINT(118, length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, buf, length); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + TEST_ASSERT_EQUAL_UINT(144, cotParam_GetSerializeSize(&sg_tParamManager)); + length = cotParam_Serialize(&sg_tParamManager, buf); + // HEX_PRINTF("", buf, length); + TEST_ASSERT_EQUAL_UINT(134, length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, buf, length); +#endif +} + void test_ResetParam(void) { g_test_1 = 80; g_test_2 = 50; g_test_float = 1; -#if PARAM_USE_STRING_TYPE +#if COT_PARAM_USE_STRING_TYPE sprintf(g_test_str, "ABCDFS"); #endif - Param_ResetParam(&sg_tParamManager); + cotParam_ResetDefault(&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)); + TEST_ASSERT_EQUAL_INT(80, g_test_1); + TEST_ASSERT_EQUAL_UINT(20, g_test_2); + TEST_ASSERT_EQUAL_FLOAT(3.15, g_test_float); +#if COT_PARAM_USE_STRING_TYPE + TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str); #endif } @@ -231,70 +272,84 @@ int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) return 0; } -int OnCheckErrorResetHandle(const ParamInfo_t *pParamInfo) +int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult) { - Param_ResetDefaultValue(pParamInfo); + cotParam_SingleParamResetDefValue(pParamInfo); return 0; } void test_SaveAndLoadParam(void) { + cotParam_Save(&sg_tParamManager, OnSaveCallback); +#if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 + TEST_ASSERT_EQUAL_UINT(102, sg_length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, sg_buf, sg_length); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 + TEST_ASSERT_EQUAL_UINT(118, sg_length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, sg_buf, sg_length); +#elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 + TEST_ASSERT_EQUAL_UINT(134, sg_length); + TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, sg_buf, sg_length); +#endif + g_test_1 = 80; g_test_2 = 50; g_test_float = -20; -#if PARAM_USE_STRING_TYPE +#if COT_PARAM_USE_STRING_TYPE sprintf(g_test_str, "12a"); #endif - Param_Save(&sg_tParamManager, OnSaveCallback, NULL); + cotParam_Save(&sg_tParamManager, OnSaveCallback); 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 +#if COT_PARAM_USE_STRING_TYPE TEST_ASSERT_EQUAL_STRING(g_test_str, "12a"); #endif - Param_Save(&sg_tParamManager, OnSaveCallback, OnCheckErrorResetHandle); + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Save(&sg_tParamManager, OnSaveCallback); - 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)); + TEST_ASSERT_EQUAL_INT(80, g_test_1); + TEST_ASSERT_EQUAL_UINT(50, g_test_2); + TEST_ASSERT_EQUAL_FLOAT(3.15, g_test_float); +#if COT_PARAM_USE_STRING_TYPE + TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str); #endif g_test_1 = 60; g_test_2 = 40; g_test_float = -20; -#if PARAM_USE_64_BIT_LENGTH +#if COT_PARAM_USE_64_BIT_LENGTH g_test_double = 5.36; #endif -#if PARAM_USE_STRING_TYPE +#if COT_PARAM_USE_STRING_TYPE sprintf(g_test_str, "12a123"); #endif g_test_s8 = 20; g_test_s16 = 1000; - Param_Load(&sg_tParamManager, OnLoadCallback, OnCheckErrorResetHandle); + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Load(&sg_tParamManager, OnLoadCallback); - 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)); + TEST_ASSERT_EQUAL_INT(80, g_test_1); + TEST_ASSERT_EQUAL_UINT(50, g_test_2); + TEST_ASSERT_EQUAL_FLOAT(3.15, g_test_float); +#if COT_PARAM_USE_STRING_TYPE + TEST_ASSERT_EQUAL_STRING("abcdef", 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); - TEST_ASSERT_EQUAL_INT(PARAM_DAT_DEF_VALUE(g_test_u32), g_test_u32); + TEST_ASSERT_EQUAL_INT(10, g_test_s8); + TEST_ASSERT_EQUAL_INT(100, g_test_s16); + TEST_ASSERT_EQUAL_INT(100, g_test_u16); + TEST_ASSERT_EQUAL_INT(1000, g_test_u32); } void test_IterateParam(void) { size_t idx = 0; - ParamInfo_t *paramInfo; + cotParamInfo_t *paramInfo; size_t cnt = 0; do { - paramInfo = Param_IterateList(&sg_tParamManager, &idx); + paramInfo = cotParam_IterateList(&sg_tParamManager, &idx); if (paramInfo != NULL) { @@ -303,173 +358,163 @@ void test_IterateParam(void) } while (paramInfo != NULL); - TEST_ASSERT_EQUAL_UINT((PARAM_TABLE_SIZE(sg_ParamTable) - 1), cnt); + TEST_ASSERT_EQUAL_UINT((COT_PARAM_TABLE_SIZE(sg_ParamTable) - 1), cnt); } void test_FindParam(void) { - const ParamInfo_t *paramInfo; -#if PARAM_USE_64_BIT_LENGTH - paramInfo = Param_FindParamByName(&sg_tParamManager, "g_test_double"); + const cotParamInfo_t *paramInfo; +#if COT_PARAM_USE_64_BIT_LENGTH + paramInfo = cotParam_FindParamByName(&sg_tParamManager, "g_test_double"); TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[3], paramInfo); #endif - paramInfo = Param_FindParamByID(&sg_tParamManager, 3); + paramInfo = cotParam_FindParamByID(&sg_tParamManager, 3); TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo); - paramInfo = Param_FindParamByParamPtr(&sg_tParamManager, &g_test_float); + paramInfo = cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_float); TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo); - paramInfo = Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u32); + paramInfo = cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u32); TEST_ASSERT_EQUAL_PTR(NULL, paramInfo); } void test_CheckRange(void) { - g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_NONE)); - g_test_u16 = 120; - TEST_ASSERT_EQUAL_INT(0, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_NONE)); - g_test_u16 = 4000; - TEST_ASSERT_EQUAL_INT(2, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_NONE)); + cotParamCheckRet_e eCheckResult; g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); + g_test_u16 = 120; + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); g_test_u16 = 4000; - TEST_ASSERT_EQUAL_INT(2, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_DEF); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); + TEST_ASSERT_EQUAL_UINT(100, g_test_u16); g_test_u16 = 4000; - 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); - snprintf(g_test_str, sizeof(g_test_str), "AB"); - TEST_ASSERT_EQUAL_INT(1, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_STRING("AB", g_test_str); - snprintf(g_test_str, sizeof(g_test_str), "abcdefg123456"); - TEST_ASSERT_EQUAL_INT(0, Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_STRING("abcdefg12", g_test_str); - 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 -} + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_DEF); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); + TEST_ASSERT_EQUAL_UINT(100, g_test_u16); -void test_CheckRangeMacroDefine(void) -{ g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_NONE)); - g_test_u16 = 120; - TEST_ASSERT_EQUAL_INT(0, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_NONE)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 4000; - TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_NONE)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); g_test_u16 = 4000; - TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); g_test_u16 = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_CHECK_RANGE(g_test_u16, PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 4000; - 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)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); -#if PARAM_USE_STRING_TYPE +#if COT_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)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_NONE); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str); + snprintf(g_test_str, sizeof(g_test_str), "AB"); - TEST_ASSERT_EQUAL_INT(1, PARAM_STR_CHECK_RANGE(g_test_str, PARAM_MIN_MAX)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_MIN_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_STRING("AB", g_test_str); - snprintf(g_test_str, sizeof(g_test_str), "abcdefg123456"); - TEST_ASSERT_EQUAL_INT(0, PARAM_STR_CHECK_RANGE(g_test_str, PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_STRING("abcdefg12", g_test_str); + + snprintf(g_test_str, sizeof(g_test_str), "ABCDEF123456"); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_MIN_MAX); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); + TEST_ASSERT_EQUAL_STRING("ABCDEF1234", g_test_str); + snprintf(g_test_str, sizeof(g_test_str), "AB"); - TEST_ASSERT_EQUAL_INT(1, PARAM_STR_CHECK_RANGE(g_test_str, PARAM_DEF)); + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_DEF); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str); #endif } void test_SetNewValue(void) { - PARAM_UINT16_T tmp = 60; - tmp = 60; - TEST_ASSERT_EQUAL_INT(1, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_NONE)); - tmp = 120; - TEST_ASSERT_EQUAL_INT(0, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_NONE)); - tmp = 4000; - TEST_ASSERT_EQUAL_INT(2, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_NONE)); + COT_PARAM_UINT16_T tmp = 60; + g_test_u16 = 200; tmp = 60; - TEST_ASSERT_EQUAL_INT(1, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_NONE)); + TEST_ASSERT_EQUAL_UINT(200, g_test_u16); + tmp = 120; + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_NONE)); + TEST_ASSERT_EQUAL_UINT(120, g_test_u16); tmp = 4000; - TEST_ASSERT_EQUAL_INT(2, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_NONE)); + TEST_ASSERT_EQUAL_UINT(120, g_test_u16); tmp = 60; - TEST_ASSERT_EQUAL_INT(1, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_DEF)); + TEST_ASSERT_EQUAL_UINT(100, g_test_u16); tmp = 4000; - 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)); - TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str); - TEST_ASSERT_EQUAL_INT(2, Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, g_test_str), "abcdefg123456", PARAM_MIN_MAX)); - 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 -} + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_DEF)); + TEST_ASSERT_EQUAL_UINT(100, g_test_u16); -void test_SetNewValueMacroDefine(void) -{ - PARAM_UINT16_T tmp = 60; tmp = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_NONE)); - tmp = 120; - TEST_ASSERT_EQUAL_INT(0, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_NONE)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MIN)); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); tmp = 4000; - TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_NONE)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MIN)); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); tmp = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MAX)); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); tmp = 4000; - TEST_ASSERT_EQUAL_INT(2, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_DEF)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MAX)); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); tmp = 60; - TEST_ASSERT_EQUAL_INT(1, PARAM_DAT_SET_NEW_VALUE(g_test_u16, tmp, PARAM_MIN_MAX)); - TEST_ASSERT_EQUAL_UINT(100, PARAM_DAT_CUR_VALUE(g_test_u16)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MIN_MAX)); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); tmp = 4000; - 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)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, COT_PARAM_RESET_MIN_MAX)); + TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); -#if PARAM_USE_STRING_TYPE - TEST_ASSERT_EQUAL_INT(0, PARAM_STR_SET_NEW_VALUE(g_test_str, "ABCDEF", PARAM_NONE)); +#if COT_PARAM_USE_STRING_TYPE + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_str), "ABCDEF", COT_PARAM_RESET_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)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_str), "AB", COT_PARAM_RESET_MIN_MAX)); TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str); - TEST_ASSERT_EQUAL_INT(2, PARAM_STR_SET_NEW_VALUE(g_test_str, "abcdefg123456", PARAM_MIN_MAX)); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_str), "abcdefg123456", COT_PARAM_RESET_MIN_MAX)); 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_INT(0, cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_str), "abcdefg123456", COT_PARAM_RESET_DEF)); TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str); #endif } @@ -478,13 +523,13 @@ void test_ResetValue(void) { g_test_u16 = 500; - TEST_ASSERT_TRUE(Param_ResetDefaultValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamResetDefValue(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); TEST_ASSERT_EQUAL_UINT(100, g_test_u16); - TEST_ASSERT_TRUE(Param_ResetMinValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); - TEST_ASSERT_EQUAL_UINT(100, g_test_u16); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamResetMinValue(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); + TEST_ASSERT_EQUAL_UINT(90, g_test_u16); - TEST_ASSERT_TRUE(Param_ResetMaxValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); + TEST_ASSERT_EQUAL_INT(0, cotParam_SingleParamResetMaxValue(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16))); TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); } @@ -493,14 +538,13 @@ int main(void) { UNITY_BEGIN(); RUN_TEST(test_ParamInit); - RUN_TEST(test_ResetParam); + RUN_TEST(test_ParamSerializeSize); RUN_TEST(test_SaveAndLoadParam); + RUN_TEST(test_ResetParam); RUN_TEST(test_IterateParam); RUN_TEST(test_FindParam); RUN_TEST(test_CheckRange); - RUN_TEST(test_CheckRangeMacroDefine); RUN_TEST(test_SetNewValue); - RUN_TEST(test_SetNewValueMacroDefine); RUN_TEST(test_ResetValue); UNITY_END(); -- Gitee From 59c18e774c625169d7258f2e68f661c87f81b9c4 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sat, 9 Dec 2023 01:45:51 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=90=B3=20chore:=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BD=8D=E7=BD=AE=E6=8C=AA=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Param/src/param.c | 1095 ----------------- Param/src/cot_param.c => cot_param.c | 0 Param/inc/cot_param.h => cot_param.h | 0 Param/inc/cot_param_cfg.h => cot_param_cfg.h | 0 .../inc/cot_param_type.h => cot_param_type.h | 0 {Demo => examples}/cmd_shell/demo.c | 0 {Demo => examples}/cmd_shell/makefile | 4 +- {Demo => examples}/cmd_shell/param_demo.c | 0 {Demo => examples}/cmd_shell/param_demo.h | 0 test/makefile | 4 +- 10 files changed, 4 insertions(+), 1099 deletions(-) delete mode 100644 Param/src/param.c rename Param/src/cot_param.c => cot_param.c (100%) rename Param/inc/cot_param.h => cot_param.h (100%) rename Param/inc/cot_param_cfg.h => cot_param_cfg.h (100%) rename Param/inc/cot_param_type.h => cot_param_type.h (100%) rename {Demo => examples}/cmd_shell/demo.c (100%) rename {Demo => examples}/cmd_shell/makefile (79%) rename {Demo => examples}/cmd_shell/param_demo.c (100%) rename {Demo => examples}/cmd_shell/param_demo.h (100%) diff --git a/Param/src/param.c b/Param/src/param.c deleted file mode 100644 index 529cecd..0000000 --- a/Param/src/param.c +++ /dev/null @@ -1,1095 +0,0 @@ -/** - ********************************************************************************************************************** - * @file param.c - * @brief 该文件提供参数管理框架功能 - * @author const_zpc any question please send mail to const_zpc@163.com - * @version V1.0 - * @date 2023-06-09 - * - * @details 功能详细说明: - * + 参数修改和重置管理 - * + 参数序列化和反序列化 - * - ********************************************************************************************************************** - * 源码路径:https://gitee.com/const-zpc/param.git 具体问题及建议可在该网址填写 Issue - * - * - ********************************************************************************************************************** - */ - -/* Includes ----------------------------------------------------------------------------------------------------------*/ -#include "param.h" -#include -#include - - -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) -{ - for (int i = 0; i < len; i++) - { - *ptr++ = value >> (i * 8); - } - - return ptr; -} - -static uint8_t *SerializeInt(uint8_t *ptr, int64_t value, uint8_t len) -{ - for (int i = 0; i < len; i++) - { - *ptr++ = value >> (i * 8); - } - - return ptr; -} - -static uint8_t *SerializeFloat(uint8_t *ptr, float value) -{ - uint32_t ivalue = *((uint32_t *)&value); - - for (int i = 0; i < sizeof(ivalue); i++) - { - *ptr++ = ivalue >> (i * 8); - } - - return ptr; -} - -static uint8_t *SerializeDouble(uint8_t *ptr, double value) -{ - uint64_t ivalue = *((uint64_t *)&value); - - for (int i = 0; i < sizeof(ivalue); i++) - { - *ptr++ = ivalue >> (i * 8); - } - - return ptr; -} - -static uint8_t *UnSerializeUint(const uint8_t *ptr, uint64_t *value, uint8_t len) -{ - *value = 0; - - for (int i = 0; i < len; i++) - { - *value |= (uint64_t)(*ptr++) << (i * 8); - } - - return (uint8_t *)ptr; -} - -static uint8_t *UnSerializeInt(const uint8_t *ptr, int64_t *value, uint8_t len) -{ - *value = 0; - - for (int i = 0; i < len; i++) - { - *value |= (int64_t)(*ptr++) << (i * 8); - } - - return (uint8_t *)ptr; -} - -static uint8_t *UnSerializeFloat(const uint8_t *ptr, float *value) -{ - uint32_t ivalue = 0; - - for (int i = 0; i < sizeof(ivalue); i++) - { - ivalue |= (uint32_t)(*ptr++) << (i * 8); - } - - *value = *((float *)&ivalue); - - return (uint8_t *)ptr; -} - -static uint8_t *UnSerializeDouble(const uint8_t *ptr, double *value) -{ - uint64_t ivalue = 0; - - for (int i = 0; i < sizeof(ivalue); i++) - { - ivalue |= (uint64_t)(*ptr++) << (i * 8); - } - - *value = *((double *)&ivalue); - - return (uint8_t *)ptr; -} - -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); - } - else - { - strcpy(param->unCurValuePtr.pString, param->unDefValuePtr.pString); - } -#else - memcpy(param->unCurValuePtr.pVoid, param->unDefValuePtr.pVoid, param->length); -#endif - return true; - } - - return false; -} - -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); - } - - return true; - } - - return false; -} - -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); - } - - return true; - } - - return false; -} - -/** - * @brief 参数表初始化 - * - * @param manager 参数表管理句柄 - * @param pParamTable 参数表 - * @param count 参数表元素数目, 可以通过宏 PARAM_TABLE_SIZE 获取 - * @return 0,成功; -1,失败 - */ -int Param_Init(ParamManager_t *manager, ParamInfo_t *pParamTable, uint16_t count) -{ - if (manager == NULL || pParamTable == NULL) - { - return -1; - } - - for (uint16_t i = 0; i < count; i++) - { - if (pParamTable[i].unMaxValuePtr.pVoid == NULL || pParamTable[i].unMinValuePtr.pVoid == NULL) - { - pParamTable[i].attr &= ~PARAM_ATTR_RANGE; - } - - if (!(pParamTable[i].attr & PARAM_ATTR_READ)) - { - pParamTable[i].attr &= ~PARAM_ATTR_WRITE; - } - - if (pParamTable[i].unDefValuePtr.pVoid == NULL) - { - pParamTable[i].attr &= ~PARAM_ATTR_RESET; - } - - if (pParamTable[i].unCurValuePtr.pVoid == NULL) - { - return -1; - } - } - - manager->pParamTable = pParamTable; - manager->count = count; - return 0; -} - -/** - * @brief 重置所有参数,恢复为缺省值 - * - * @attention 无可重置权限的参数不能恢复为缺省值 - * @param manager 参数表管理句柄 - * @return 0,成功; -1,失败 - */ -int Param_ResetParam(const ParamManager_t *manager) -{ - if (manager == NULL) - { - return -1; - } - - for (uint16_t i = 0; i < manager->count; ++i) - { - ResetParamValue(&manager->pParamTable[i]); - } - - return 0; -} - -static ParamInfo_t *FindParamByParamPtr(ParamManager_t *manager, const void *curParamPtr) -{ - for (uint16_t i = 0; i < manager->count; ++i) - { - if (manager->pParamTable[i].unCurValuePtr.pVoid == curParamPtr) - { - return &manager->pParamTable[i]; - } - } - - return NULL; -} - -/** - * @brief 参数列表迭代器 - * - * @attention 无读取权限的参数会自动跳过 - * @param manager 参数表管理句柄 - * @param psIdx 参数表起始索引 - * @return 参数信息 - */ -ParamInfo_t *Param_IterateList(ParamManager_t *manager, size_t *psIdx) -{ - ParamInfo_t *p = NULL; - - if (manager == NULL || psIdx == NULL || *psIdx > manager->count) - { - return NULL; - } - - while (*psIdx < manager->count) - { - if (manager->pParamTable[*psIdx].attr & PARAM_ATTR_READ) - { - p = &manager->pParamTable[*psIdx]; - (*psIdx)++; - break; - } - - (*psIdx)++; - } - - return p; -} - -#if (PARAM_NAME_MAX_LENGTH > 1) -static ParamInfo_t *FindParamByName(ParamManager_t *manager, const char *pszName) -{ - for (uint16_t i = 0; i < manager->count; ++i) - { - if (strcmp(manager->pParamTable[i].pszName, pszName) == 0) - { - return &manager->pParamTable[i]; - } - } - - return NULL; -} -#endif - -/** - * @brief 根据参数名称查找参数信息 - * - * @attention 无可读权限时会查找失败 - * @param manager 参数表管理句柄 - * @param pszName 参数名称 - * @return 参数信息 - */ -const ParamInfo_t *Param_FindParamByName(ParamManager_t *manager, const char *pszName) -{ -#if (PARAM_NAME_MAX_LENGTH > 1) - ParamInfo_t *pInfo; - - if (manager != NULL) - { - pInfo = FindParamByName(manager, pszName); - - if (pInfo->attr & PARAM_ATTR_READ) - { - return pInfo; - } - } - -#endif - return NULL; -} - - -static ParamInfo_t *FindParamByID(ParamManager_t *manager, uint16_t id) -{ - for (uint16_t i = 0; i < manager->count; ++i) - { - if (manager->pParamTable[i].id == id) - { - return &manager->pParamTable[i]; - } - } - - return NULL; -} - -/** - * @brief 根据参数ID查找参数信息 - * - * @attention 无可读权限时会查找失败 - * @param manager 参数表管理句柄 - * @param id 参数ID - * @return 参数信息 - */ -const ParamInfo_t *Param_FindParamByID(ParamManager_t *manager, uint16_t id) -{ - ParamInfo_t *pInfo; - - if (manager != NULL) - { - pInfo = FindParamByID(manager, id); - - if (pInfo->attr & PARAM_ATTR_READ) - { - return pInfo; - } - } - - return NULL; -} - -/** - * @brief 根据当前参数数据地址指针查找参数信息 - * - * @attention 无可读权限时会查找失败 - * @param manager 参数表管理句柄 - * @param curParamPtr 当前参数数据地址指针 - * @return 参数信息 - */ -const ParamInfo_t *Param_FindParamByParamPtr(ParamManager_t *manager, const void *curParamPtr) -{ - ParamInfo_t *pInfo; - - if (manager != NULL) - { - pInfo = FindParamByParamPtr(manager, curParamPtr); - - if (pInfo->attr & PARAM_ATTR_READ) - { - return pInfo; - } - } - - return NULL; -} - - -// 验证参数是否在指定范围内 -static int ValidateRange(const ParamInfo_t *param, const Value_u *pval) -{ - if (!(param->attr & PARAM_ATTR_RANGE)) - { - return 0; - } - - switch (param->type) - { - case PARAM_INT8: - if (pval->s64val < *param->unMinValuePtr.pInt8) - { - return 1; - } - else if (pval->s64val > *param->unMaxValuePtr.pInt8) - { - return 2; - } - break; - - case PARAM_INT16: - if (pval->s64val < *param->unMinValuePtr.pInt16) - { - return 1; - } - else if (pval->s64val > *param->unMaxValuePtr.pInt16) - { - return 2; - } - break; - - case PARAM_INT32: - if (pval->s64val < *param->unMinValuePtr.pInt32) - { - return 1; - } - else if (pval->s64val > *param->unMaxValuePtr.pInt32) - { - return 2; - } - break; -#if PARAM_USE_64_BIT_LENGTH - case PARAM_INT64: - if (pval->s64val < *param->unMinValuePtr.pInt64) - { - return 1; - } - else if (pval->s64val > *param->unMaxValuePtr.pInt64) - { - return 2; - } - break; -#endif - case PARAM_UINT8: - if (pval->u64val < *param->unMinValuePtr.pUint8) - { - return 1; - } - else if (pval->u64val > *param->unMaxValuePtr.pUint8) - { - return 2; - } - break; - - case PARAM_UINT16: - if (pval->u64val < *param->unMinValuePtr.pUint16) - { - return 1; - } - else if (pval->u64val > *param->unMaxValuePtr.pUint16) - { - return 2; - } - break; - - case PARAM_UINT32: - if (pval->u64val < *param->unMinValuePtr.pUint32) - { - return 1; - } - else if (pval->u64val > *param->unMaxValuePtr.pUint32) - { - return 2; - } - break; -#if PARAM_USE_64_BIT_LENGTH - case PARAM_UINT64: - if (pval->u64val < *param->unMinValuePtr.pUint64) - { - return 1; - } - else if (pval->u64val > *param->unMaxValuePtr.pUint64) - { - return 2; - } - break; -#endif - case PARAM_FLOAT: - if (pval->fVal < *param->unMinValuePtr.pFloat) - { - return 1; - } - else if (pval->fVal > *param->unMaxValuePtr.pFloat) - { - return 2; - } - break; -#if PARAM_USE_64_BIT_LENGTH - case PARAM_DOUBLE: - if (pval->fVal < *param->unMinValuePtr.pDouble) - { - return 1; - } - else if (pval->fVal > *param->unMaxValuePtr.pDouble) - { - return 2; - } - break; -#endif -#if PARAM_USE_STRING_TYPE - case PARAM_STRING: - if (strlen(pval->str) < *param->unMinValuePtr.pStringLength) - { - return 1; - } - else if (strlen(pval->str) > *param->unMaxValuePtr.pStringLength) - { - return 2; - } - break; -#endif - default: - return -1; - } - - return 0; -} - -static int ValidateRangeByVoid(const ParamInfo_t *param, const void *pval) -{ - Value_u uValue; - - switch (param->type) - { - case PARAM_INT8: - uValue.s64val = *(PARAM_INT8_T *)pval; - break; - - case PARAM_INT16: - uValue.s64val = *(PARAM_INT16_T *)pval; - break; - - 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; - - case PARAM_UINT16: - uValue.s64val = *(PARAM_UINT16_T *)pval; - break; - - 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); - uValue.str[PARAM_STRING_MAX_LENGTH + 2] = '\0'; - break; -#endif - default: - return -1; - } - - return ValidateRange(param, &uValue); -} - -/** - * @brief 检查参数最新值的范围并对超出范围时进行处理 - * - * @param param 参数信息 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 参数小于最小值则为最小值, 参数大于最大值则为最大值; 但对字符串类型参数该选项无效, 即不做处理 - * @return 0,正常; 1,参数小于最小值(字符串长度小于最小限制长度); 2,参数大于最大值(字符串长度大于最大限制长度); <0,错误 - */ -int Param_CheckRange(const ParamInfo_t *param, uint8_t opt) -{ - int ret; - - if (param == NULL) - { - return -1; // 参数验证失败 - } - - ret = ValidateRangeByVoid(param, param->unCurValuePtr.pVoid); - - if (ret != 0) - { - if (opt == PARAM_DEF) - { - ResetParamValue(param); - } - else if (opt == PARAM_MIN_MAX) - { - ret == 1 ? ResetParamMinValue(param) : ResetParamMaxValue(param); - } - - return ret; - } - - return 0; -} - -/** - * @brief 设置新的参数值 - * - * @param param 参数信息 - * @param value 新的参数值 - * @param opt 超出范围的处理选项: - * @arg PARAM_NONE, 参数不变, 即不做处理 - * @arg PARAM_DEF, 参数恢复默认 - * @arg PARAM_MIN_MAX, 参数小于最小值则为最小值, 参数大于最大值则为最大值; 但对字符串类型参数该选项无效, 即不做处理 - * @return 0,正常; 1,参数小于最小值(字符串长度小于最小限制长度); 2,参数大于最大值(字符串长度大于最大限制长度); <0,错误 - */ -int Param_SetNewValue(const ParamInfo_t *param, const void *value, uint8_t opt) -{ - int ret; - - if (param == NULL) - { - return -1; - } - - ret = ValidateRangeByVoid(param, value); - - if (ret != 0) - { - if (opt == PARAM_DEF) - { - ResetParamValue(param); - } - else if (opt == PARAM_MIN_MAX) - { - ret == 1 ? ResetParamMinValue(param) : ResetParamMaxValue(param); - } - - return ret; - } -#if PARAM_USE_STRING_TYPE - if (param->type != PARAM_STRING) - { - memcpy(param->unCurValuePtr.pVoid, value, param->length); - } - else - { - strcpy(param->unCurValuePtr.pString, value); - } -#else - memcpy(param->unCurValuePtr.pVoid, value, param->length); -#endif - return 0; -} - -/** - * @brief 重置参数为缺省值 - * - * @attention 无可重置权限的参数不能恢复为缺省值 - * @param param 参数信息 - * @return true 成功 - * @return false 失败 - */ -bool Param_ResetDefaultValue(const ParamInfo_t *param) -{ - return ResetParamValue(param); -} - -/** - * @brief 重置参数为最小值 - * - * @attention 字符串类型参数该功能无效 - * @param param 参数信息 - * @return true 成功 - * @return false 失败 - */ -bool Param_ResetMinValue(const ParamInfo_t *param) -{ - return ResetParamMinValue(param); -} - -/** - * @brief 重置参数为最大值 - * - * @attention 字符串类型参数该功能无效 - * @param param 参数信息 - * @return true 成功 - * @return false 失败 - */ -bool Param_ResetMaxValue(const ParamInfo_t *param) -{ - return ResetParamMaxValue(param); -} - -static uint16_t ParamInfoFormStream(ParamInfo_t *param, const uint8_t *pbuf) -{ - switch (param->type) - { - case PARAM_INT8: - { - int64_t val = 0; - pbuf = UnSerializeInt(pbuf, &val, param->length); - *param->unCurValuePtr.pInt8 = (PARAM_INT8_T)val; - } - break; - - case PARAM_INT16: - { - int64_t val = 0; - pbuf = UnSerializeInt(pbuf, &val, param->length); - *param->unCurValuePtr.pInt16 = (PARAM_INT16_T)val; - } - break; - - case PARAM_INT32: - { - int64_t val = 0; - pbuf = UnSerializeInt(pbuf, &val, param->length); - *param->unCurValuePtr.pInt32 = (PARAM_INT32_T)val; - } - break; -#if PARAM_USE_64_BIT_LENGTH - case PARAM_INT64: - { - int64_t val = 0; - pbuf = UnSerializeInt(pbuf, &val, param->length); - *param->unCurValuePtr.pInt64 = (PARAM_INT64_T)val; - } - break; -#endif - case PARAM_UINT8: - { - uint64_t val = 0; - pbuf = UnSerializeUint(pbuf, &val, param->length); - *param->unCurValuePtr.pUint8 = (PARAM_UINT8_T)val; - } - break; - - case PARAM_UINT16: - { - uint64_t val = 0; - pbuf = UnSerializeUint(pbuf, &val, param->length); - *param->unCurValuePtr.pUint16 = (PARAM_UINT16_T)val; - } - break; - - case PARAM_UINT32: - { - uint64_t val = 0; - pbuf = UnSerializeUint(pbuf, &val, param->length); - *param->unCurValuePtr.pUint32= (PARAM_UINT32_T)val; - } - break; -#if PARAM_USE_64_BIT_LENGTH - case PARAM_UINT64: - { - uint64_t val = 0; - pbuf = UnSerializeUint(pbuf, &val, param->length); - *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->length); - break; -#endif - default: - return 0; // 不支持的参数类型 - } - - return param->length; -} - -static uint8_t *MoveBufToBase(uint8_t *pbuf, uint32_t length) -{ - memmove(pbuf - length, pbuf, length); - return (pbuf - length); -} - -/** - * @brief 加载数据 - * - * @note 该操作会校验参数的合法性(取值范围) - * @param manager 参数表管理句柄 - * @param pfnLoadCallback 加载回调函数 - * @param pfnCheckError 参数不合法时触发回调函数 - * @return 0,成功; -1,失败 - */ -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) - { - return -1; - } - - bool isFinish = false; - uint16_t length = 0; - uint16_t paramLength = 0; - uint16_t id = 0; - ParamInfo_t *pParamInfo; - uint8_t keyLength = 0; -#if PARAM_USE_KEY_VALUE - uint64_t key = 0; -#endif - - do - { - length = sizeof(buf) - (ptr - buf); - - if (pfnLoadCallback(ptr, &length, &isFinish) != 0) - { - return -2; - } - - if (isFinish || length == 0) - { - continue; - } - - length += (ptr - buf); - ptr = buf; - -#if PARAM_USE_KEY_VALUE - if (keyLength == 0) - { - keyLength = ptr[0]; - ptr++; - length -= 1; - } - - while (length > keyLength) - { - UnSerializeUint(ptr, &key, keyLength); - -#if PARAM_SUPPORT_NUM == PARAM_SUPPORT_16 - id = (key >> 4) & 0x0F; - paramLength = key & 0x0F; -#elif PARAM_SUPPORT_NUM == PARAM_SUPPORT_256 - id = (key >> 8) & 0xFF; - paramLength = key & 0xFF; -#elif PARAM_SUPPORT_NUM == PARAM_SUPPORT_4096 - id = (key >> 12) & 0xFFF; - paramLength = key & 0xFFF; -#endif - - if (length < (paramLength + keyLength)) - { - break; - } - - ptr += keyLength; - length -= keyLength; - - pParamInfo = (ParamInfo_t *)FindParamByID(manager, id); - - if (pParamInfo == NULL || paramLength != pParamInfo->length) - { - ptr += paramLength; - length -= paramLength; - continue; - } - - ParamInfoFormStream(pParamInfo, ptr); - ptr += paramLength; - length -= paramLength; - - if (0 != ValidateRangeByVoid(pParamInfo, pParamInfo->unCurValuePtr.pVoid)) - { - if (pfnCheckError != NULL) - { - pfnCheckError(pParamInfo); - } - } - } - -#else - - while (length >= 1) - { - pParamInfo = &manager->pParamTable[id]; - - if (length < pParamInfo->length) - { - break; - } - - ParamInfoFormStream(pParamInfo, ptr); - ptr += pParamInfo->length; - - if (0 != ValidateRangeByVoid(pParamInfo, pParamInfo->unCurValuePtr.pVoid)) - { - if (pfnCheckError != NULL) - { - pfnCheckError(pParamInfo); - } - } - - length -= pParamInfo->length; - id++; - } - -#endif - ptr = MoveBufToBase(ptr, ptr - buf); - ptr += length; - } while (!isFinish); - - return 0; -} - -static uint16_t ParamInfoToStream(uint8_t *pbuf, ParamInfo_t *param) -{ - switch (param->type) - { - case PARAM_INT8: - pbuf = SerializeInt(pbuf, *(PARAM_INT8_T *)param->unCurValuePtr.pVoid, param->length); - break; - - case PARAM_INT16: - pbuf = SerializeInt(pbuf, *(PARAM_INT16_T *)param->unCurValuePtr.pVoid, param->length); - break; - - 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; - - case PARAM_UINT16: - pbuf = SerializeUint(pbuf, *(PARAM_UINT16_T *)param->unCurValuePtr.pVoid, param->length); - break; - - 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->length); - break; -#endif - default: - return 0; // 不支持的参数类型 - } - - return param->length; -} - -/** - * @brief 保存数据 - * - * @note 参数在保存时会对参数校验合法性(取值范围) - * @param manager 参数表管理句柄 - * @param pfnSaveCallback 保存回调函数 - * @param pfnCheckError 参数不合法时触发回调函数 - * @return 0,成功; -1,失败 - */ -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 - uint64_t key = 0; -#endif - - if (manager == NULL || pfnSaveCallback == NULL) - { - return -1; - } - -#if PARAM_USE_KEY_VALUE - buf[0] = PARAM_SUPPORT_NUM; - - if (pfnSaveCallback(buf, 1, false) != 0) - { - return -2; - } -#endif - - for (int i = 0; i < manager->count; i++) - { - ptr = buf; - length = 0; -#if PARAM_USE_KEY_VALUE -#if PARAM_SUPPORT_NUM == PARAM_SUPPORT_16 - key = (manager->pParamTable[i].id << 4) | (manager->pParamTable[i].length & 0x0F); -#elif PARAM_SUPPORT_NUM == PARAM_SUPPORT_256 - key = (manager->pParamTable[i].id << 8) | (manager->pParamTable[i].length & 0xFF); -#elif PARAM_SUPPORT_NUM == PARAM_SUPPORT_4096 - key = (manager->pParamTable[i].id << 12) | (manager->pParamTable[i].length & 0xFFF); -#endif - ptr = SerializeUint(ptr, key, PARAM_SUPPORT_NUM); - length += PARAM_SUPPORT_NUM; -#endif - if (0 != ValidateRangeByVoid(&manager->pParamTable[i], manager->pParamTable[i].unCurValuePtr.pVoid)) - { - if (pfnCheckError != NULL) - { - pfnCheckError(&manager->pParamTable[i]); - } - } - - length = ParamInfoToStream(&buf[length], &manager->pParamTable[i]); - ptr += length; - - if (pfnSaveCallback(buf, (ptr - buf), false) != 0) - { - return -2; - } - } - - if (pfnSaveCallback(buf, 0, true) != 0) - { - return -2; - } - - return 0; -} - - - - diff --git a/Param/src/cot_param.c b/cot_param.c similarity index 100% rename from Param/src/cot_param.c rename to cot_param.c diff --git a/Param/inc/cot_param.h b/cot_param.h similarity index 100% rename from Param/inc/cot_param.h rename to cot_param.h diff --git a/Param/inc/cot_param_cfg.h b/cot_param_cfg.h similarity index 100% rename from Param/inc/cot_param_cfg.h rename to cot_param_cfg.h diff --git a/Param/inc/cot_param_type.h b/cot_param_type.h similarity index 100% rename from Param/inc/cot_param_type.h rename to cot_param_type.h diff --git a/Demo/cmd_shell/demo.c b/examples/cmd_shell/demo.c similarity index 100% rename from Demo/cmd_shell/demo.c rename to examples/cmd_shell/demo.c diff --git a/Demo/cmd_shell/makefile b/examples/cmd_shell/makefile similarity index 79% rename from Demo/cmd_shell/makefile rename to examples/cmd_shell/makefile index 935160d..730620f 100644 --- a/Demo/cmd_shell/makefile +++ b/examples/cmd_shell/makefile @@ -6,10 +6,10 @@ endif GCC:=gcc -INC+=-I${CURR_DIR_PATH}/../../Param/inc +INC+=-I${CURR_DIR_PATH}/../../ INC+=-I${CURR_DIR_PATH}/ -SRC+=${CURR_DIR_PATH}/../../Param/src/cot_param.c +SRC+=${CURR_DIR_PATH}/../../cot_param.c SRC+=${CURR_DIR_PATH}/demo.c SRC+=${CURR_DIR_PATH}/param_demo.c diff --git a/Demo/cmd_shell/param_demo.c b/examples/cmd_shell/param_demo.c similarity index 100% rename from Demo/cmd_shell/param_demo.c rename to examples/cmd_shell/param_demo.c diff --git a/Demo/cmd_shell/param_demo.h b/examples/cmd_shell/param_demo.h similarity index 100% rename from Demo/cmd_shell/param_demo.h rename to examples/cmd_shell/param_demo.h diff --git a/test/makefile b/test/makefile index d3cf3d5..5e47026 100644 --- a/test/makefile +++ b/test/makefile @@ -6,13 +6,13 @@ endif GCC:=gcc -INC+=-I${CURR_DIR_PATH}/../Param/inc +INC+=-I${CURR_DIR_PATH}/../ INC+=-I${CURR_DIR_PATH}/unity INC+=-I${CURR_DIR_PATH}/ SRC+=${CURR_DIR_PATH}/param_test.c SRC+=${CURR_DIR_PATH}/unity/unity.c -SRC+=${CURR_DIR_PATH}/../Param/src/cot_param.c +SRC+=${CURR_DIR_PATH}/../cot_param.c AIM_NAME:=param_test -- Gitee From 523c1d775a38a6b8739719330dcac5bb327ebdfe Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sat, 9 Dec 2023 03:18:14 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=F0=9F=93=83=20docs:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 315 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 178 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 0d9c4c9..b8961a1 100644 --- a/README.md +++ b/README.md @@ -2,174 +2,215 @@ ## 介绍 -1. 采用表驱动方式统一管理所有参数,包括缺省值、最小值和最大值等 - - > - 支持定义普通参数,无缺省值、最小值和最大值限制 - > - 支持定义普通参数,有缺省值,但无最小值和最大值限制 - > - 支持定义普通参数,有缺省值,最小值和最大值限制 - > -2. 采用宏定义快速对参数进行定义、注册和管理 -3. 支持已定义变量做为参数进行管理,如全局变量或者结构体成员变量 -4. 支持基本类型参数和字符串参数 -5. 支持序列化和反序列化操作,可在本地储存设备保存/读取二进制数据 - - > - 支持键值对的方式储存,即使后期版本迭代对参数表删除/插入数据时也能向下兼容 - > - 支持非键值对的方式储存,适合小容量的储存设备,序列化后的数据内容少,但是后期版本迭代对参数表删除或插入数据时不能向下兼容,只有通过在参数表后添加参数才能向下兼容 - > - 通过多次读写储存设备分别加载参数和保存参数,更兼容小内存的平台使用(多次调用回调函数处理) - > - 支持在数据加载或保存时当参数当前值不合法(超出范围)触发错误处理回调函数,有上层应用程序决定如何处理(可以恢复默认值) - > -6. 支持功能配置裁剪 - - > - 根据不同的平台,可以对部分功能裁剪,或者修改配置适用于不同容量的芯片开发 - > - 键值对的方式储存:向下兼容较好 - > - 可以选择只支持基本类型的参数储存功能,如字符串类型参数和64位长度的参数可裁剪 - > - -## 软件架构 +### 管理方式 + +通过将已定义变量添加到参数表进行参数的统一管理,包括缺省值、最小值和最大值等,方便进行参数校验纠正处理。 + +> 已定义变量也包括结构体的成员变量,均为**全局变量**。 + +- 支持将已定义变量绑定为参数,无缺省值、最小值和最大值限制,适合于记录类型的参数 +- 支持将已定义变量绑定为参数,有缺省值,但无最小值和最大值限制,适合于配置类型的参数 +- 支持将已定义变量绑定为参数,有缺省值,最小值和最大值限制,适合于用户设置或者关键性类型的参数 + +除此之外,还有参数名、属性等,方便配合UI或者参数的权限管理。 + +同时若单个参数表无法满足参数数目或者参数分类管理,可定义多张参数表 + +> 每张参数表中的参数ID唯一,不可重复;但不同参数表ID可以相同 + +### 参数类型 + +支持数值和字符串两种类型参数。 + +> - 数值类型:`int`、`float`、`double` 等基本类型的参数 +> - 字符串类型:`char` 定义用来储存字符串的数组 + + + +### 兼容性 + +* [X] 提供了参数表的序列化和反序列化操作。 + +> - 方便在本地储存设备(如flash、eeprom等)保存/读取二进制数据,甚至还可以跨设备传输 +> - 提供了两种方式: +> - 保存/加载:提供参数实际保存/加载的回调函数,通过多次触发回调函数完成参数的序列化保存、加载反序列化功能;适用于小内存的平台使用(不需要申请内存处理) +> - 序列化和反序列化:需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据;一次性完成操作(需要申请较大的内存完成) + +* [X] 支持启用键值对功能 + +> - 每个参数都需要指定唯一的ID,在后期版本迭代对参数表删除、插入或添加参数时也能向下兼容,不会影响其他参数。 +> - 启用键值对后序列化的数据长度也会比较大,因为每个参数序列化时包含了ID和长度信息 + + +### 可裁剪 + +根据不同的平台,可以对部分功能裁剪,或者修改配置适用于不同容量的芯片进行开发。 + +| 配置选项 | 描述 | +| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `COT_PARAM_USE_KEY_VALUE` | 是否采用键值对方式序列化和反序列化参数数据 | +| `COT_PARAM_USE_STRING_TYPE` | 启用字符串参数类型 | +| `COT_PARAM_USE_64_BIT_LENGTH` | 启用64bit的参数类型 | +| `COT_PARAM_NAME_MAX_LENGTH` | 参数名字最大定义长度,小于或等于1则禁用参数名功能 | +| `COT_PARAM_STRING_MAX_LENGTH` | 字符串类型的参数取值最大定义长度(包括结束符),需启用 `COT_PARAM_USE_STRING_TYPE` | +| `COT_PARAM_SUPPORT_NUM` | 单张参数表最多添加多少个参数,需启用 `COT_PARAM_USE_KEY_VALUE`,可选:
`COT_PARAM_SUPPORT_16`:ID取值范围0-15,即最多16个参数
`COT_PARAM_SUPPORT_256`:ID取值范围0-255,即最多256个参数
`COT_PARAM_SUPPORT_4096`:ID取值范围0-4095,即最多4096个参数

注:若没有启用 `COT_PARAM_USE_KEY_VALUE` 键值对方式,则无限制 | + + +## 软件设计 + +略 + ## 使用说明 -定义参数表 +### 参数表定义 + ```c +typedef struct +{ + uint16_t test1; + float test2; + char str[12]; +}ParamDemo_t; + +static ParamDemo_t sg_tTest = { + .str = "sdf", + .test1 = 20, + .test2 = 567.4 +}; -PARAM_DEFINE_DAT (g_test, PARAM_INT16, 10); -PARAM_DEFINE_DAT_DEF (g_test_2, PARAM_UINT16, 20); -PARAM_DEFINE_DAT_RANGE (g_test_3, PARAM_DOUBLE, 3.15, -10, 10); -PARAM_DEFINE_STR_RANGE (g_test_str, 10, "abcdef", 5); -PARAM_DEFINE_DAT_RANGE (g_test_4, PARAM_INT8, 8, -10, 10); -PARAM_DEFINE_DAT_RANGE (g_test_5, PARAM_UINT32, 620, 500, 10000); -PARAM_DEFINE_DAT_RANGE (g_test_6, PARAM_UINT8, 45, 5, 100); -PARAM_DEFINE_DAT_RANGE (g_test_7, PARAM_INT64, 5, -542, 5450); -PARAM_DEFINE_BIND_DAT_RANGE(sg_tTest_test1, PARAM_UINT16, 20, 10, 2000); // 为即将绑定的变量定义相关参数信息 -PARAM_DEFINE_BIND_DAT(sg_tTest_test2, PARAM_FLOAT); // 为即将绑定的变量定义相关参数信息,初值为sg_tTest 变量定义时的初值 -PARAM_DEFINE_BIND_STR_RANGE(sg_tTest_str, sizeof(sg_tTest.str), "const-zpc", 6);// 为即将绑定的变量定义相关参数信息,初值为sg_tTest 变量定义时的初值 - -ParamInfo_t sg_ParamTable[] = { - PARAM_ITEM_DAT(1, g_test, PARAM_ATTR_WR), - PARAM_ITEM_DAT_DEF(2, g_test_2, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(3, g_test_3, PARAM_ATTR_WR), - PARAM_ITEM_STR_RANGE(4, g_test_str, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(5, g_test_4, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(6, g_test_5, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(7, g_test_6, PARAM_ATTR_WR), - PARAM_ITEM_DAT_RANGE(8, g_test_7, PARAM_ATTR_READ), // 只读 - PARAM_ITEM_DAT_RANGE_BIND(9, sg_tTest_test1, sg_tTest.test1, PARAM_ATTR_WR), - PARAM_ITEM_DAT_BIND(10, sg_tTest_test2, sg_tTest.test2, PARAM_ATTR_WR), - PARAM_ITEM_STR_RANGE_BIND(11, sg_tTest_str, sg_tTest.str, PARAM_ATTR_WR), +int16_t g_test = 50; +uint16_t g_test_2 = 20; +double g_test_3 = 3.15; +COT_PARAM_INT8_T g_test_4 = 8; +COT_PARAM_UINT32_T g_test_5 = 620; +COT_PARAM_UINT8_T g_test_6 = 45; +COT_PARAM_INT64_T g_test_7 = 5; +COT_PARAM_INT8_T g_sd = 2; +COT_PARAM_INT64_T g_test_88 = 80; +char g_test_str[15] = "abcdef"; +char g_str_des[13] = "EERR"; +char g_str_des_2[15] = "sdRR"; +char g_str_des_3[15] = "ewRR"; + +cotParamInfo_t sg_ParamTable[] = { + COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND_WITH_NAME(2, "g_test_2", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), + COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), + COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), + COT_PARAM_ITEM_BIND(6, g_test_5, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 620, 500, 10000), + COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), + COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), + COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), + COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), + COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), + COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), + COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(17, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), }; -``` +static cotParamManager_t sg_tParamManager; -通过宏去操作参数示例片段代码 +int mian() +{ + cotParam_Init(&sg_tParamManager, sg_ParamTable, COT_PARAM_TABLE_SIZE(sg_ParamTable)); +} -```c -// 首先需要在头文件声明 -PARAM_EXTERN_DAT(g_test, PARAM_INT16); -PARAM_EXTERN_DAT(g_test_2, PARAM_UINT16); -PARAM_EXTERN_DAT(g_test_3, PARAM_DOUBLE); -PARAM_EXTERN_STR(g_test_str, 10); -PARAM_EXTERN_DAT(g_test_4, PARAM_INT8); -PARAM_EXTERN_DAT(g_test_5, PARAM_UINT32); -PARAM_EXTERN_DAT(g_test_6, PARAM_UINT8); -PARAM_EXTERN_DAT(g_test_7, PARAM_INT64); +``` -//函数中使用 -int main() -{ -//方法一: - // 对参数g_test_6设置新的值50,如果超出限定范围则不处理 - PARAM_DAT_SET_NEW_VALUE(g_test_6, 50, PARAM_NONE); - // 对参数g_test_6设置新的值50,如果超出限定范围则重置为默认值 - PARAM_DAT_SET_NEW_VALUE(g_test_6, 50, PARAM_DEF); - // 对参数g_test_6设置新的值150,如果超出限定范围则参数小于最小值则为最小值, 参数大于最大值则为最大值 - PARAM_DAT_SET_NEW_VALUE(g_test_6, 150, PARAM_MIN_MAX); +### 参数保存/加载 +1. 保存/加载方式(函数内部完成序列化和反序列化,逐步写入保存/读取加载) -//方法二: - // 对参数g_test_6设置新的值50,如果超出限定范围则不处理 - g_test_6 = 50; - PARAM_DAT_CHECK_RANGE(g_test_6, PARAM_NONE); +```c - // 对参数g_test_6设置新的值50,如果超出限定范围则重置为默认值 - g_test_6 = 50; - PARAM_DAT_CHECK_RANGE(g_test_6, PARAM_DEF); +// 所有参数校验出错时恢复默认处理 +int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult) +{ + cotParam_SingleParamResetDefValue(pParamInfo); + return 0; +} - // 对参数g_test_6设置新的值150,如果超出限定范围则参数小于最小值则为最小值, 参数大于最大值则为最大值 - g_test_6 = 150; - PARAM_DAT_CHECK_RANGE(g_test_6, PARAM_MIN_MAX); +// 从储存设备多次读取 +int OnLoadCallback(uint8_t *pBuf, uint16_t *len, bool *pisFinish) +{ + uint16_t needReadLen = *len; + static uint32_t s_already_read_length = 0; + + if (sg_length == s_offset) + { + *len = 0; + s_already_read_length = 0; + *pisFinish = true; + return 0; + } + else + { + *pisFinish = false; + } + + if (sg_length - s_already_read_length < needReadLen) + { + needReadLen = sg_length - s_already_read_length ; + } + + *len = read(pBuf, needReadLen); + s_already_read_length += (*len); + + return 0; +} +//函数中使用 +int LoadParam() +{ + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Load(&sg_tParamManager, OnLoadCallback); } ``` -通过函数去操作参数示例片段代码 + + +2. 序列化/反序列化方式(一次性写入保存/读取加载) ```c //函数中使用 int main() { - PARAM_UINT16_T tmp; +#if 0 + uint8_t buf[500]; -//方法一: - // 对参数g_test_6设置新的值50,如果超出限定范围则不处理 - tmp = 50; - Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_NONE); - - // 对参数g_test_6设置新的值50,如果超出限定范围则重置为默认值 - tmp = 50; - Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_DEF); - - // 对参数g_test_6设置新的值150,如果超出限定范围则参数小于最小值则为最小值, 参数大于最大值则为最大值 - tmp = 150; - Param_SetNewValue(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &tmp, PARAM_MIN_MAX); - - -//方法二: - // 对参数g_test_6设置新的值50,如果超出限定范围则不处理 - g_test_6 = 50; - Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_NONE); - - // 对参数g_test_6设置新的值50,如果超出限定范围则重置为默认值 - g_test_6 = 50; - Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_DEF); - - // 对参数g_test_6设置新的值150,如果超出限定范围则参数小于最小值则为最小值, 参数大于最大值则为最大值 - g_test_6 = 150; - Param_CheckRange(Param_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), PARAM_MIN_MAX); + uint32_t length = cotParam_Serialize(&sg_tParamManager, buf); + write(buf, length); +#else + uint8_t *p_buf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); + + uint32_t length = cotParam_Serialize(&sg_tParamManager, p_buf); + write(p_buf, length); + free(p_buf); + p_buf = NULL; +#endif } ``` -序列化 Demo 的结果展示 - -```base -# 配置最多支持 15 个参数时加载参数时分多次读取/保存操作打印日志 -load param: - read: [21] -> 12 0a 00 22 14 00 38 33 33 33 33 33 33 09 40 4a 61 62 63 64 65 - read: [21] -> 66 00 00 00 00 51 08 64 6c 02 00 00 71 2d 88 05 00 00 00 00 00 -save param: - save: [3] -> 12 0a 00 - save: [3] -> 22 14 00 - save: [9] -> 38 33 33 33 33 33 33 09 40 - save: [11] -> 4a 61 62 63 64 65 66 00 00 00 00 - save: [2] -> 51 08 - save: [5] -> 64 6c 02 00 00 - save: [2] -> 71 2d - save: [9] -> 88 05 00 00 00 00 00 00 00 - -# 配置最多支持 15 个参数时的序列化内容及长度 -"键值对序列化": [44] -> 12 0a 00 22 14 00 38 33 33 33 33 33 33 09 40 4a 61 62 63 64 65 66 00 00 00 00 51 08 64 6c 02 00 00 71 2d 88 05 00 00 00 00 00 00 00 -"序列化": [36] -> 50 00 14 00 33 33 33 33 33 33 09 40 67 5f 74 65 73 74 5f 36 00 00 00 6c 02 00 00 2d 05 00 00 00 00 00 00 00 - -# 配置最多支持 256 个参数时的序列化内容及长度 -"键值对序列化": [52] -> 02 01 0a 00 02 02 14 00 08 03 33 33 33 33 33 33 09 40 0a 04 61 62 63 64 65 66 00 00 00 00 01 05 08 04 06 6c 02 00 00 01 07 2d 08 08 05 00 00 00 00 00 00 00 -"序列化": [36] -> 50 00 14 00 33 33 33 33 33 33 09 40 67 5f 74 65 73 74 5f 36 00 00 00 6c 02 00 00 2d 05 00 00 00 00 00 00 00 - -# 配置最多支持 4096 个参数时的序列化内容及长度 -"键值对序列化": [60] -> 02 10 00 0a 00 02 20 00 14 00 08 30 00 33 33 33 33 33 33 09 40 0a 40 00 61 62 63 64 65 66 00 00 00 00 01 50 00 08 04 60 00 6c 02 00 00 01 70 00 2d 08 80 00 05 00 00 00 00 00 00 00 -"序列化": [36] -> 50 00 14 00 33 33 33 33 33 33 09 40 67 5f 74 65 73 74 5f 36 00 00 00 6c 02 00 00 2d 05 00 00 00 00 00 00 00 +### 校验处理 + +```c +// 对某个变量参数进行范围校验,得到校验结果 +cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &eCheckResult); + +// 对某个变量参数变更后(当前值已经变化)进行校验处理,若超出范围则恢复默认 +g_test_3 = 1000; +cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), COT_PARAM_RESET_DEF); +// 对某个变量参数在需要变更前(当前值没有变化)进行校验处理,若新的值超出范围则不更新变量参数当前的值 +double tmp = 1000; +cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, COT_PARAM_RESET_NONE) ``` ## demo样式 -- Gitee From 54c9f386700f80460a5c7365f3903678d7db7fad Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sat, 9 Dec 2023 15:29:29 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E5=8F=82=E6=95=B0=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 除了范围校验外,在范围校验成功的前提下才能执行自定义范围校验 --- cot_param.c | 56 ++++++++++++++++++++++++++++++++---- cot_param.h | 73 +++++++++++++++++++++++++++++++---------------- cot_param_cfg.h | 8 ++++++ cot_param_type.h | 8 +++++- test/param_test.c | 34 +++++++++++++++++++++- 5 files changed, 147 insertions(+), 32 deletions(-) diff --git a/cot_param.c b/cot_param.c index 5ebcce5..96419f8 100644 --- a/cot_param.c +++ b/cot_param.c @@ -263,6 +263,7 @@ int cotParam_ResetDefault(const cotParamManager_t *pManager) /** * @brief 检查所有参数当前值的范围并对超出范围时进行处理 * + * @attention 如果参数有自定义校验方式,则只有满足范围校验成功的前提下才会执行自定义校验 * @param param 参数信息 * @param pfnCheckError 参数超出范围时的处理函数 * @return 0,成功; -1,失败 @@ -287,6 +288,21 @@ int cotParam_Check(const cotParamManager_t* pManager, pfnCheckError_f pfnCheckEr pfnCheckError(&pManager->pParamTable[id], eCheckResult); } } + else + { +#if COT_PARAM_USE_CUSTOM_CHECK + if (pManager->pParamTable[id].pfnParamCheck != NULL) + { + if (pManager->pParamTable[id].pfnParamCheck(pManager->pParamTable[id].unCurValuePtr.pVoid) != 0) + { + if (pfnCheckError != NULL) + { + pfnCheckError(&pManager->pParamTable[id], COT_PARAM_CHECK_OTHER_ERR); + } + } + } +#endif + } } return 0; @@ -646,6 +662,7 @@ static cotParamCheckRet_e ValidateRangeByVoid(const cotParamInfo_t *pParam, cons * @brief 校验当前参数值并进行处理 * * @attention 字符串类型参数若设置了最长的长度,超长则会进行截断,但是小于最小长度时则不做处理 + * @attention 该函数不会执行自定义校验 * @param pParam 参数信息 * @param eResetOpt 参数值超出范围的处理选项 @enum cotParamResetOpt_e * @return 0,成功; -1,失败 @@ -683,6 +700,7 @@ int cotParam_SingleParamCheckProcess(const cotParamInfo_t *pParam, cotParamReset /** * @brief 更新参数值同时进行校验处理 * + * @attention 该函数不会执行自定义校验 * @param pParam 参数信息 * @param pNewValue 新的参数值 * @param eResetOpt 新的参数值超出范围的处理选项 @enum cotParamResetOpt_e @@ -734,23 +752,51 @@ int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewVal } /** - * @brief 校验当前参数值 + * @brief 根据参数信息校验输入的值 * - * @param[in] pParam 参数信息 + * @attention 如果参数有自定义校验方式,则只有满足范围校验成功的前提下才会执行自定义校验 + * @param[in] pParam 参数信息 + * @param[in] pValue 输入的参数值 * @param[out] peCheckResult 校验结果 * @return 0,成功; -1,失败 */ -int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult) +int cotParam_SingleParamCheckInput(const cotParamInfo_t *pParam, const void *pValue, cotParamCheckRet_e *peCheckResult) { - if (pParam == NULL || peCheckResult == NULL) + if (pParam == NULL || pValue == NULL || peCheckResult == NULL) { return -1; } - *peCheckResult = ValidateRangeByVoid(pParam, pParam->unCurValuePtr.pVoid); + *peCheckResult = ValidateRangeByVoid(pParam, pValue); + +#if COT_PARAM_USE_CUSTOM_CHECK + if (*peCheckResult == COT_PARAM_CHECK_OK) + { + if (pParam->pfnParamCheck != NULL) + { + if (pParam->pfnParamCheck(pValue) != 0) + { + *peCheckResult = COT_PARAM_CHECK_OTHER_ERR; + } + } + } +#endif return 0; } +/** + * @brief 校验当前参数值 + * + * @attention 如果参数有自定义校验方式,则只有满足范围校验成功的前提下才会执行自定义校验 + * @param[in] pParam 参数信息 + * @param[out] peCheckResult 校验结果 + * @return 0,成功; -1,失败 + */ +int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult) +{ + return cotParam_SingleParamCheckInput(pParam, pParam->unCurValuePtr.pVoid, peCheckResult); +} + /** * @brief 重置参数为缺省值 * diff --git a/cot_param.h b/cot_param.h index aa434f7..a7e53bb 100644 --- a/cot_param.h +++ b/cot_param.h @@ -24,7 +24,7 @@ /** * @brief 添加参数(以变量名为参数名) * - * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal) + * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal, checkFun) * @param id 参数表中的唯一ID * @param variable 变量 * @param type 参数类型 @enum ParamType_e 取值 @@ -32,13 +32,14 @@ * @param defVal 默认值(可选) * @param minVal 最小值(可选) * @param maxVal 最大值(可选) + * @param checkFun 特殊校验回调函数(返回0表示校验成功)(可选) */ #define COT_PARAM_ITEM_BIND(id, variable, type, attr, defVal...) COT_PARAM_ITEM_##type##_IMPL(id, #variable, variable, type, attr, ##defVal) /** * @brief 添加参数 * - * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal) + * @note COT_PARAM_ITEM_BIND(id, name, type, attr, defVal, minVal, maxVal, checkFun) * @param id 参数表中的唯一ID * @param name 参数名 * @param variable 变量 @@ -47,66 +48,87 @@ * @param defVal 默认值(可选) * @param minVal 最小值(可选) * @param maxVal 最大值(可选) + * @param checkFun 特殊校验回调函数(返回0表示校验成功)(可选) */ #define COT_PARAM_ITEM_BIND_WITH_NAME(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_##type##_IMPL(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_BIND_NUM(_id, _name, _variable, _type, _attr) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr), \ - NULL, NULL, NULL) + NULL, NULL, NULL, NULL) #define COT_PARAM_ITEM_BIND_NUM_DEF(_id, _name, _variable, _type, _attr, _defVal) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET), \ - (void *)&(_type##_T){_defVal}, NULL, NULL) + (void *)&(_type##_T){_defVal}, NULL, NULL, NULL) #define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ - (void *)&(_type##_T){_defVal}, (void *)&(_type##_T){_minVal}, (void *)&(_type##_T){_maxVal}) + (void *)&(_type##_T){_defVal}, (void *)&(_type##_T){_minVal}, (void *)&(_type##_T){_maxVal}, NULL) +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal, _check) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, &_variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ + (void *)&(_type##_T){_defVal}, (void *)&(_type##_T){_minVal}, (void *)&(_type##_T){_maxVal}, _check) #define COT_PARAM_ITEM_BIND_STR(_id, _name, _variable, _type, _attr) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr), \ - NULL, NULL, NULL) + NULL, NULL, NULL, NULL) #define COT_PARAM_ITEM_BIND_STR_DEF(_id, _name, _variable, _type, _attr, _defVal) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET), \ - (void *)_defVal, NULL, NULL) + (void *)_defVal, NULL, NULL, NULL) #define COT_PARAM_ITEM_BIND_STR_DEF_RANGE(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal) \ COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ - (void *)_defVal, (void *)&(param_size_t){_minVal}, (void *)&(param_size_t){_maxVal}) + (void *)_defVal, (void *)&(param_size_t){_minVal}, (void *)&(param_size_t){_maxVal}, NULL) +#define COT_PARAM_ITEM_BIND_STR_DEF_RANGE_CHECK(_id, _name, _variable, _type, _attr, _defVal, _minVal, _maxVal, _check) \ + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, sizeof(_variable), (_attr | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE), \ + (void *)_defVal, (void *)&(param_size_t){_minVal}, (void *)&(param_size_t){_maxVal}, _check) -#if (COT_PARAM_NAME_MAX_LENGTH > 1) -#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal) { \ +#if COT_PARAM_USE_CUSTOM_CHECK + #if (COT_PARAM_NAME_MAX_LENGTH > 1) +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ .pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \ .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ - .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} + .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal, .pfnParamCheck = _check} + #else +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ + .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ + .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal, .pfnParamCheck = _check} + + #endif #else -#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal) { \ + #if (COT_PARAM_NAME_MAX_LENGTH > 1) +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ + .pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ + .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} + #else +#define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ .id = _id, .type = _type, .length = _length, .attr = _attr, \ .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} + #endif #endif - #define COT_PARAM_ITEM_COT_PARAM_INT8_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_INT16_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_INT32_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_INT64_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_UINT8_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_UINT16_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_UINT32_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_UINT64_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_FLOAT_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_DOUBLE_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_NUM_DEF_RANGE, , COT_PARAM_ITEM_BIND_NUM_DEF, COT_PARAM_ITEM_BIND_NUM)(id, name, variable, type, attr, ##defVal) #define COT_PARAM_ITEM_COT_PARAM_STRING_IMPL(id, name, variable, type, attr, defVal...) COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, ##defVal, \ - COT_PARAM_ITEM_BIND_STR_DEF_RANGE, , COT_PARAM_ITEM_BIND_STR_DEF, COT_PARAM_ITEM_BIND_STR)(id, name, variable, type, attr, ##defVal) + COT_PARAM_ITEM_BIND_STR_DEF_RANGE_CHECK, COT_PARAM_ITEM_BIND_STR_DEF_RANGE, , COT_PARAM_ITEM_BIND_STR_DEF, COT_PARAM_ITEM_BIND_STR)(id, name, variable, type, attr, ##defVal) -#define COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, _1, _2, _3, FUNC, ...) FUNC +#define COT_PARAM_ITEM_FUN_IMPL(id, name, variable, type, attr, _1, _2, _3, _4, FUNC, ...) FUNC // 得到参数表的元素数目 #define COT_PARAM_TABLE_SIZE(tab) (sizeof(tab) / sizeof(cotParamInfo_t)) @@ -132,6 +154,7 @@ extern const cotParamInfo_t *cotParam_FindParamByParamPtr(const cotParamManager_ extern int cotParam_SingleParamCheckProcess(const cotParamInfo_t *pParam, cotParamResetOpt_e eResetOpt); extern int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewValue, cotParamResetOpt_e eResetOpt); +extern int cotParam_SingleParamCheckInput(const cotParamInfo_t *pParam, const void *pValue, cotParamCheckRet_e *peCheckResult); extern int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult); extern int cotParam_SingleParamResetDefValue(const cotParamInfo_t *pParam); extern int cotParam_SingleParamResetMinValue(const cotParamInfo_t *pParam); diff --git a/cot_param_cfg.h b/cot_param_cfg.h index af0c7d8..592b68e 100644 --- a/cot_param_cfg.h +++ b/cot_param_cfg.h @@ -22,6 +22,14 @@ * */ #define COT_PARAM_USE_KEY_VALUE 1 +/** + * @brief 使用参数自定义校验功能 + * + * @note 在校验取值范围成功后才会执行自定义校验 + * 0,禁止; 1,使能 + */ +#define COT_PARAM_USE_CUSTOM_CHECK 1 + /** * @brief 使用字符串参数类型 * 0,禁止; 1,使能 diff --git a/cot_param_type.h b/cot_param_type.h index 2cdb640..76b15e4 100644 --- a/cot_param_type.h +++ b/cot_param_type.h @@ -79,7 +79,8 @@ typedef enum { COT_PARAM_CHECK_OK = 0, // 校验成功 / 无最大最小值 COT_PARAM_CHECK_OVER_MIN, // 小于最小值 / 小于最小字符串长度 - COT_PARAM_CHECK_OVER_MAX // 大于最大值 / 大于最小字符串长度 + COT_PARAM_CHECK_OVER_MAX, // 大于最大值 / 大于最小字符串长度 + COT_PARAM_CHECK_OTHER_ERR // 其他错误, 即自定义校验出错 } cotParamCheckRet_e; typedef enum @@ -115,6 +116,8 @@ typedef union { void *pVoid; } cotParamTypePtr_u; +typedef int (*cotParamCheck_f)(const void *pCurParam); + /** * @brief 定义无内存的参数结构体信息 * @@ -132,6 +135,9 @@ typedef struct stuParamInfo const cotParamTypePtr_u unDefValuePtr; /*!< 默认值指针 */ const cotParamTypePtr_u unMinValuePtr; /*!< 最小值指针(字符串参数限制最小长度时需要 param_size_t 定义) */ const cotParamTypePtr_u unMaxValuePtr; /*!< 最大值指针(字符串参数限制最大长度时需要 param_size_t 定义) */ +#if COT_PARAM_USE_CUSTOM_CHECK + const cotParamCheck_f pfnParamCheck; /*!< 自定义校验方式回调函数(返回0表示校验成功) */ +#endif } cotParamInfo_t; typedef int (*pfnCheckError_f)(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult); diff --git a/test/param_test.c b/test/param_test.c index 77609ea..c6bbcb4 100644 --- a/test/param_test.c +++ b/test/param_test.c @@ -64,6 +64,18 @@ COT_PARAM_UINT32_T g_test_u32 = 1000; COT_PARAM_UINT64_T g_test_u64 = 8000; #endif +static int CheckTestS16(const void *pCurParam) +{ + const int16_t *p_test_s16 = (const int16_t *)pCurParam; + + if ((*p_test_s16) % 2 != 0) + { + return -1; + } + + return 0; +} + cotParamInfo_t sg_ParamTable[] = { COT_PARAM_ITEM_BIND(1, g_test_1, COT_PARAM_INT16, COT_PARAM_ATTR_WR), COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), @@ -75,7 +87,7 @@ cotParamInfo_t sg_ParamTable[] = { COT_PARAM_ITEM_BIND(5, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 5, 10), #endif COT_PARAM_ITEM_BIND(6, g_test_s8, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 10, -10, 15), - COT_PARAM_ITEM_BIND(7, g_test_s16, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -100, 3000), + COT_PARAM_ITEM_BIND(7, g_test_s16, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -100, 3000, CheckTestS16), COT_PARAM_ITEM_BIND(8, g_test_s32, COT_PARAM_INT32, COT_PARAM_ATTR_WR, 1000, -900, 10000), #if COT_PARAM_USE_64_BIT_LENGTH COT_PARAM_ITEM_BIND(9, g_test_s64, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 8000, -100, 1000000), @@ -464,6 +476,25 @@ void test_CheckRange(void) #endif } +void test_CheckCustomWay(void) +{ + cotParamCheckRet_e eCheckResult; + + g_test_s16 = 200; + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); + +#if COT_PARAM_USE_CUSTOM_CHECK + g_test_s16 = 201; + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OTHER_ERR, eCheckResult); +#endif + + g_test_s16 = 202; + cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); +} + void test_SetNewValue(void) { COT_PARAM_UINT16_T tmp = 60; @@ -544,6 +575,7 @@ int main(void) RUN_TEST(test_IterateParam); RUN_TEST(test_FindParam); RUN_TEST(test_CheckRange); + RUN_TEST(test_CheckCustomWay); RUN_TEST(test_SetNewValue); RUN_TEST(test_ResetValue); -- Gitee From 915ae62e675df0c3c1c2915d3998152bf978d5f5 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sat, 9 Dec 2023 15:30:06 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=F0=9F=93=83=20docs:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 208 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 156 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b8961a1..babc6e7 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,80 @@ # 轻量级参数管理框架(C语言) -## 介绍 +嵌入式软件中的系统数据参数是指在嵌入式系统中用于实现系统功能和控制的各种数据,如用户参数、状态、配置信息等;那么如何管理这些数据对于嵌入式系统的正确运行和维护非常重要。 + +该参数管理框架代码就是如何统一管理软件中的各类系统数据参数。 -### 管理方式 +> 该参数管理并不涉及数据是如何储存的。因为有些系统数据并不需要储存起来,只需要进行管理而已。 -通过将已定义变量添加到参数表进行参数的统一管理,包括缺省值、最小值和最大值等,方便进行参数校验纠正处理。 +## 介绍 -> 已定义变量也包括结构体的成员变量,均为**全局变量**。 +### 参数管理 -- 支持将已定义变量绑定为参数,无缺省值、最小值和最大值限制,适合于记录类型的参数 -- 支持将已定义变量绑定为参数,有缺省值,但无最小值和最大值限制,适合于配置类型的参数 -- 支持将已定义变量绑定为参数,有缺省值,最小值和最大值限制,适合于用户设置或者关键性类型的参数 +* [X] 通过将已定义变量(**全局变量**)添加到参数表进行参数的统一管理,单个参数包括了当前值、缺省值、最小值、最大值、参数名和属性等信息。 -除此之外,还有参数名、属性等,方便配合UI或者参数的权限管理。 + > - 当前值:已定义的变量 + > - 缺省值:默认值,并非变量初值 + > - 最小值:该参数的最小值 + > - 最大值:该参数的最大值 + > - 参数名:对该参数的描述 + > - 属性:有多种属性信息,方便后续功能扩展 + > - 读/写权限:参数模块中无具体作用,可用于UI或者其他方式显示时使用 + > - 重置权限:设置默认值后则存在该属性 + > - 校验权限:设置最大最小值后则存在该属性 + > +* [X] 根据不同的场景管理不同的参数 -同时若单个参数表无法满足参数数目或者参数分类管理,可定义多张参数表 + > - 无缺省值、最小值和最大值限制,适合于记录类型的参数,比如状态数据或历史数据等 + > - 有缺省值,但无最小值和最大值限制,适合于配置类型的参数 + > - 有缺省值,最小值和最大值限制,适合于关键性类型的参数,比如用户参数或者关键的状态数据等 + > +* [X] 同时若单个参数表无法满足参数数目或者参数分类管理,可定义多张参数表 -> 每张参数表中的参数ID唯一,不可重复;但不同参数表ID可以相同 + > - 每张参数表中的参数ID唯一,不可重复; + > - 不同参数表ID可以重复定义 + > ### 参数类型 -支持数值和字符串两种类型参数。 +* [X] 数值类型参数 + + > `int`、`float`、`double` 等基本类型的参数 + > +* [X] 字符串类型参数 + + > `char` 定义用来储存字符串的数组 + > + +### 参数校验 + +为了更好的统一管理参数,可以对参数设置的范围进行校验,防止参数设置超出预期范围,导致程序不可控。 -> - 数值类型:`int`、`float`、`double` 等基本类型的参数 -> - 字符串类型:`char` 定义用来储存字符串的数组 +* [X] 范围校验 + > 根据参数的最大和最小值进行判断,数值类型的参数则根据数值超出范围判断。而字符串则是根据字符串长度超出范围判断。 + > +* [X] 自定义校验 + > 提供回调函数,每个参数可设置自定义的校验方式,比如某个参数需要设置为多少的倍数,或者根据其他参数决定当前参数的取值范围等。 + > + +上述两种校验方式均需要参数设置缺省值,最小值和最大值后才有效。 ### 兼容性 * [X] 提供了参数表的序列化和反序列化操作。 -> - 方便在本地储存设备(如flash、eeprom等)保存/读取二进制数据,甚至还可以跨设备传输 -> - 提供了两种方式: -> - 保存/加载:提供参数实际保存/加载的回调函数,通过多次触发回调函数完成参数的序列化保存、加载反序列化功能;适用于小内存的平台使用(不需要申请内存处理) -> - 序列化和反序列化:需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据;一次性完成操作(需要申请较大的内存完成) - + > - 方便在本地储存设备(如flash、eeprom等)保存/读取二进制数据,甚至还可以跨设备传输使用 + > - 提供了两种方式: + > - 保存/加载:提供参数实际保存/加载的回调函数,通过多次触发回调函数完成参数的序列化保存、加载反序列化功能;适用于小内存的平台使用(不需要申请内存处理) + > - 序列化和反序列化:需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据;一次性完成操作(需要申请较大的内存完成) + > * [X] 支持启用键值对功能 -> - 每个参数都需要指定唯一的ID,在后期版本迭代对参数表删除、插入或添加参数时也能向下兼容,不会影响其他参数。 -> - 启用键值对后序列化的数据长度也会比较大,因为每个参数序列化时包含了ID和长度信息 - + > - 每个参数都需要指定唯一的ID,在后期版本迭代对参数表删除、插入或添加参数时也能向下兼容,不会影响其他参数。 + > - 即使更新了参数表的最大容纳范围,也能保证兼容,比如上个版本配置参数只支持16个,当前版本配置支持了256个,也能正确处理。 + > - 启用键值对后序列化的数据长度也会比较大,因为每个参数序列化时包含了ID和长度信息。 + > ### 可裁剪 @@ -49,23 +83,21 @@ | 配置选项 | 描述 | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `COT_PARAM_USE_KEY_VALUE` | 是否采用键值对方式序列化和反序列化参数数据 | -| `COT_PARAM_USE_STRING_TYPE` | 启用字符串参数类型 | -| `COT_PARAM_USE_64_BIT_LENGTH` | 启用64bit的参数类型 | +| `COT_PARAM_USE_CUSTOM_CHECK` | 是否启用参数自定义校验功能 | +| `COT_PARAM_USE_STRING_TYPE` | 是否启用字符串参数类型 | +| `COT_PARAM_USE_64_BIT_LENGTH` | 是否启用64bit的参数类型 | | `COT_PARAM_NAME_MAX_LENGTH` | 参数名字最大定义长度,小于或等于1则禁用参数名功能 | | `COT_PARAM_STRING_MAX_LENGTH` | 字符串类型的参数取值最大定义长度(包括结束符),需启用 `COT_PARAM_USE_STRING_TYPE` | | `COT_PARAM_SUPPORT_NUM` | 单张参数表最多添加多少个参数,需启用 `COT_PARAM_USE_KEY_VALUE`,可选:
`COT_PARAM_SUPPORT_16`:ID取值范围0-15,即最多16个参数
`COT_PARAM_SUPPORT_256`:ID取值范围0-255,即最多256个参数
`COT_PARAM_SUPPORT_4096`:ID取值范围0-4095,即最多4096个参数

注:若没有启用 `COT_PARAM_USE_KEY_VALUE` 键值对方式,则无限制 | - ## 软件设计 略 - ## 使用说明 ### 参数表定义 - ```c typedef struct { @@ -96,7 +128,7 @@ char g_str_des_3[15] = "ewRR"; cotParamInfo_t sg_ParamTable[] = { COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND_WITH_NAME(2, "g_test_2", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND_WITH_NAME(2, "test_2", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), // 另取名字 COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), @@ -123,8 +155,6 @@ int mian() ``` - - ### 参数保存/加载 1. 保存/加载方式(函数内部完成序列化和反序列化,逐步写入保存/读取加载) @@ -139,15 +169,14 @@ int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e } // 从储存设备多次读取 -int OnLoadCallback(uint8_t *pBuf, uint16_t *len, bool *pisFinish) +int OnLoadCallback(uint8_t *pBuf, uint16_t *p_len, bool *pisFinish) { - uint16_t needReadLen = *len; - static uint32_t s_already_read_length = 0; + static uint32_t s_offset = 0; if (sg_length == s_offset) { - *len = 0; - s_already_read_length = 0; + *p_len = 0; + s_offset = 0; *pisFinish = true; return 0; } @@ -156,25 +185,48 @@ int OnLoadCallback(uint8_t *pBuf, uint16_t *len, bool *pisFinish) *pisFinish = false; } - if (sg_length - s_already_read_length < needReadLen) + seek(s_offset); + *p_len= read(pBuf, *p_len); + s_offset += (*len); + + return 0; +} + +// 写数据至储存空间 +int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) +{ + static uint32_t s_offset = 0; + + if (isFinish) { - needReadLen = sg_length - s_already_read_length ; + s_offset = 0; + return 0; } - *len = read(pBuf, needReadLen); - s_already_read_length += (*len); + seek(s_offset); + write(pBuf, len); + s_offset += len; return 0; } -//函数中使用 -int LoadParam() + +// 加载参数 +void LoadParam(void) { - cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); - cotParam_Load(&sg_tParamManager, OnLoadCallback); + cotParam_Load(&sg_tParamManager, OnLoadCallback); + + // 加载后全部参数进行校验 + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); } -``` +void SaveParam(void) +{ + // 保存前全部参数进行校验 + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + cotParam_Save(&sg_tParamManager, OnSaveCallback); +} +``` 2. 序列化/反序列化方式(一次性写入保存/读取加载) @@ -183,25 +235,46 @@ int LoadParam() int main() { #if 0 - uint8_t buf[500]; + // 保存 + uint8_t w_buf[500]; + uint32_t length = cotParam_Serialize(&sg_tParamManager, w_buf); + write(w_buf, length); - uint32_t length = cotParam_Serialize(&sg_tParamManager, buf); - write(buf, length); + // 加载 + uint8_t r_buf[500]; + uint32_t length = read(r_buf); + cotParam_Deserialization(&sg_tParamManager, r_buf, length); #else - uint8_t *p_buf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); + // 保存 + uint8_t *p_wbuf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); - uint32_t length = cotParam_Serialize(&sg_tParamManager, p_buf); - write(p_buf, length); - free(p_buf); - p_buf = NULL; + uint32_t length = cotParam_Serialize(&sg_tParamManager, p_wbuf); + write(p_wbuf, length); + free(p_wbuf); + p_wbuf = NULL; + + // 加载 + uint8_t *p_rbuf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); + + uint32_t length = read(r_buf); + cotParam_Deserialization(&sg_tParamManager, p_rbuf, length); + write(p_rbuf, length); + free(p_rbuf); + p_rbuf = NULL; #endif } ``` ### 校验处理 +校验需要提前在参数表中设置缺省值,最小值和最大值后才有效。 + +#### 范围校验 + +根据参数设置范围进行校验。 + ```c -// 对某个变量参数进行范围校验,得到校验结果 +// 对某个变量当前参数进行范围校验,得到校验结果 cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &eCheckResult); // 对某个变量参数变更后(当前值已经变化)进行校验处理,若超出范围则恢复默认 @@ -213,6 +286,37 @@ double tmp = 1000; cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, COT_PARAM_RESET_NONE) ``` +#### 自定义校验 + +在符合参数范围内,根据参数设置的自定义校验函数进行校验。 + +```c +static int CheckTestS16(const void *pCurParam) +{ + const int16_t *p_test_s16 = (const int16_t *)pCurParam; + + /* 需要校验 g_test_s16 为2的倍数(建议不能直接使用 g_test_s16 去判断,因为回调函数入参不一定时该值, + 虽然类型是一样的)*/ + if ((*p_test_s16) % 2 != 0) + { + return -1; + } + + return 0; // 0表示自定义校验成功,其他表示失败 +} + +cotParamInfo_t sg_ParamTable[] = { + COT_PARAM_ITEM_BIND(1, g_test_1, COT_PARAM_INT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_test_float, COT_PARAM_FLOAT, COT_PARAM_ATTR_READ, 3.15, -10, 10), + COT_PARAM_ITEM_BIND(6, g_test_s8, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 10, -10, 15), + COT_PARAM_ITEM_BIND(7, g_test_s16, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -100, 3000, CheckTestS16), // 设置自定义校验 + COT_PARAM_ITEM_BIND(8, g_test_s32, COT_PARAM_INT32, COT_PARAM_ATTR_WR, 1000, -900, 10000), +} + +``` + + ## demo样式 博客: -- Gitee From 2423ce01fb6b87f46a6c439e7b7d2272f5651198 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:27:56 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E6=95=B0=E6=8D=AE=E4=BF=9D=E5=AD=98=E5=92=8C=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cot_param.c | 27 +++++++++++------------ cot_param.h | 6 ++--- cot_param_type.h | 6 ++--- test/param_test.c | 56 ++++++++++++++++++++++++----------------------- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/cot_param.c b/cot_param.c index 96419f8..dc9a3cf 100644 --- a/cot_param.c +++ b/cot_param.c @@ -268,7 +268,7 @@ int cotParam_ResetDefault(const cotParamManager_t *pManager) * @param pfnCheckError 参数超出范围时的处理函数 * @return 0,成功; -1,失败 */ -int cotParam_Check(const cotParamManager_t* pManager, pfnCheckError_f pfnCheckError) +int cotParam_Check(const cotParamManager_t* pManager, cotParamError_f pfnCheckError) { cotParamCheckRet_e eCheckResult; @@ -962,9 +962,9 @@ static uint8_t *MoveBufToBase(uint8_t *pbuf, uint32_t length) * * @param pManager 参数表管理句柄 * @param pfnLoadCallback 加载回调函数 - * @return 0,成功; -1,失败 + * @return 0,成功; -1,失败; -2,加载回调函数返回失败 */ -int cotParam_Load(const cotParamManager_t *pManager, pfnLoad_f pfnLoadCallback) +int cotParam_Load(const cotParamManager_t *pManager, cotParamLoad_f pfnLoadCallback) { #if COT_PARAM_USE_STRING_TYPE uint8_t buf[sizeof(cotParamInfo_t) + COT_PARAM_STRING_MAX_LENGTH]; @@ -978,7 +978,6 @@ int cotParam_Load(const cotParamManager_t *pManager, pfnLoad_f pfnLoadCallback) return -1; } - bool isFinish = false; uint16_t length = 0; uint16_t paramLength = 0; uint16_t id = 0; @@ -992,14 +991,14 @@ int cotParam_Load(const cotParamManager_t *pManager, pfnLoad_f pfnLoadCallback) { length = sizeof(buf) - (ptr - buf); - if (pfnLoadCallback(ptr, &length, &isFinish) != 0) + if (pfnLoadCallback(ptr, length, &length) != 0) { return -2; } - if (isFinish || length == 0) + if (length == 0) { - continue; + break; } length += (ptr - buf); @@ -1070,7 +1069,7 @@ int cotParam_Load(const cotParamManager_t *pManager, pfnLoad_f pfnLoadCallback) #endif ptr = MoveBufToBase(ptr, ptr - buf); ptr += length; - } while (!isFinish); + } while (1); return 0; } @@ -1136,9 +1135,9 @@ static uint16_t ParamInfoToStream(uint8_t *pbuf, cotParamInfo_t *pParam) * * @param pManager 参数表管理句柄 * @param pfnSaveCallback 保存回调函数 - * @return 0,成功; -1,失败 + * @return 0,成功; -1,失败; -2,保存回调函数返回失败 */ -int cotParam_Save(const cotParamManager_t *pManager, pfnSave_f pfnSaveCallback) +int cotParam_Save(const cotParamManager_t *pManager, cotParamSave_f pfnSaveCallback) { #if COT_PARAM_USE_STRING_TYPE uint8_t buf[sizeof(cotParamInfo_t) + COT_PARAM_STRING_MAX_LENGTH]; @@ -1159,7 +1158,7 @@ int cotParam_Save(const cotParamManager_t *pManager, pfnSave_f pfnSaveCallback) #if COT_PARAM_USE_KEY_VALUE buf[0] = COT_PARAM_SUPPORT_NUM; - if (pfnSaveCallback(buf, 1, false) != 0) + if (pfnSaveCallback(buf, 1) != 0) { return -2; } @@ -1183,13 +1182,13 @@ int cotParam_Save(const cotParamManager_t *pManager, pfnSave_f pfnSaveCallback) length = ParamInfoToStream(&buf[length], &pManager->pParamTable[i]); ptr += length; - if (pfnSaveCallback(buf, (ptr - buf), false) != 0) + if (pfnSaveCallback(buf, (ptr - buf)) != 0) { return -2; } } - if (pfnSaveCallback(buf, 0, true) != 0) + if (pfnSaveCallback(buf, 0) != 0) { return -2; } @@ -1205,7 +1204,7 @@ int cotParam_Save(const cotParamManager_t *pManager, pfnSave_f pfnSaveCallback) */ uint32_t cotParam_GetSerializeSize(const cotParamManager_t* pManager) { - uint32_t length = 1 + 10; // 预留 + uint32_t length = 1; uint16_t idx = 0; if (pManager == NULL) diff --git a/cot_param.h b/cot_param.h index a7e53bb..806dd07 100644 --- a/cot_param.h +++ b/cot_param.h @@ -136,10 +136,10 @@ extern int cotParam_Init(cotParamManager_t* pManager, cotParamInfo_t* pParamTable, uint16_t count); extern int cotParam_ResetDefault(const cotParamManager_t *pManager); -extern int cotParam_Check(const cotParamManager_t* pManager, pfnCheckError_f pfnCheckError); +extern int cotParam_Check(const cotParamManager_t* pManager, cotParamError_f pfnCheckError); -extern int cotParam_Load(const cotParamManager_t* pManager, pfnLoad_f pfnLoadCallback); -extern int cotParam_Save(const cotParamManager_t* pManager, pfnSave_f pfnSaveCallback); +extern int cotParam_Load(const cotParamManager_t* pManager, cotParamLoad_f pfnLoadCallback); +extern int cotParam_Save(const cotParamManager_t* pManager, cotParamSave_f pfnSaveCallback); extern uint32_t cotParam_GetSerializeSize(const cotParamManager_t* pManager); extern uint32_t cotParam_Serialize(const cotParamManager_t* pManager, uint8_t *pbuf); diff --git a/cot_param_type.h b/cot_param_type.h index 76b15e4..97a00b2 100644 --- a/cot_param_type.h +++ b/cot_param_type.h @@ -140,9 +140,9 @@ typedef struct stuParamInfo #endif } cotParamInfo_t; -typedef int (*pfnCheckError_f)(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult); -typedef int (*pfnLoad_f)(uint8_t *pBuf, uint16_t *len, bool *pisFinish); -typedef int (*pfnSave_f)(const uint8_t *pBuf, uint16_t len, bool isFinish); +typedef int (*cotParamError_f)(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult); +typedef int (*cotParamLoad_f)(uint8_t *pBuf, uint16_t bufSize, uint16_t *pLength); +typedef int (*cotParamSave_f)(const uint8_t *pBuf, uint16_t len); // 定义参数管理结构体 typedef struct diff --git a/test/param_test.c b/test/param_test.c index c6bbcb4..0cadfe8 100644 --- a/test/param_test.c +++ b/test/param_test.c @@ -192,19 +192,19 @@ void test_ParamSerializeSize(void) uint32_t length; uint8_t buf[200]; #if COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_16 - TEST_ASSERT_EQUAL_UINT(112, cotParam_GetSerializeSize(&sg_tParamManager)); + TEST_ASSERT_EQUAL_UINT(102, cotParam_GetSerializeSize(&sg_tParamManager)); length = cotParam_Serialize(&sg_tParamManager, buf); // HEX_PRINTF("", buf, length); TEST_ASSERT_EQUAL_UINT(102, length); TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, buf, length); #elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_256 - TEST_ASSERT_EQUAL_UINT(128, cotParam_GetSerializeSize(&sg_tParamManager)); + TEST_ASSERT_EQUAL_UINT(118, cotParam_GetSerializeSize(&sg_tParamManager)); length = cotParam_Serialize(&sg_tParamManager, buf); // HEX_PRINTF("", buf, length); TEST_ASSERT_EQUAL_UINT(118, length); TEST_ASSERT_EQUAL_HEX8_ARRAY(kBuf, buf, length); #elif COT_PARAM_SUPPORT_NUM == COT_PARAM_SUPPORT_4096 - TEST_ASSERT_EQUAL_UINT(144, cotParam_GetSerializeSize(&sg_tParamManager)); + TEST_ASSERT_EQUAL_UINT(134, cotParam_GetSerializeSize(&sg_tParamManager)); length = cotParam_Serialize(&sg_tParamManager, buf); // HEX_PRINTF("", buf, length); TEST_ASSERT_EQUAL_UINT(134, length); @@ -237,50 +237,52 @@ static uint8_t sg_buf[500]; static uint32_t sg_length = 0; // 从储存空间读取数据 -int OnLoadCallback(uint8_t *pBuf, uint16_t *len, bool *pisFinish) +int OnLoadCallback(uint8_t *pBuf, uint16_t bufSize, uint16_t *pLength) { - uint16_t needReadLen = *len; + uint16_t length; static uint32_t s_offset = 0; - if (sg_length == s_offset) + if (s_offset < sg_length) { - *len = 0; - s_offset = 0; - *pisFinish = true; - return 0; + if (s_offset + bufSize <= sg_length) + { + length = bufSize; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } + else + { + length = sg_length - s_offset; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } } else { - *pisFinish = false; - } - - if (sg_length - s_offset < needReadLen) - { - needReadLen = sg_length - s_offset; + length = 0; + s_offset = 0; } - memcpy(pBuf, &sg_buf[s_offset], needReadLen); - *len = needReadLen; - s_offset += needReadLen; - + *pLength = length; return 0; } // 写数据至储存空间 -int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) +int OnSaveCallback(const uint8_t *pBuf, uint16_t len) { static uint32_t s_offset = 0; - if (isFinish) + if (len > 0) + { + memcpy(&sg_buf[s_offset], pBuf, len); + s_offset += len; + sg_length = s_offset; + } + else { s_offset = 0; - return 0; } - memcpy(&sg_buf[s_offset], pBuf, len); - s_offset += len; - sg_length = s_offset; - return 0; } -- Gitee From 601f5cc47610d86a8d6d40ecfb91e29f7f255dd8 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:30:46 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=F0=9F=8C=88=20style:=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=91=BD=E5=90=8D=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cot_param.c | 4 ++-- cot_param.h | 2 +- test/param_test.c | 36 ++++++++++++++++++------------------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cot_param.c b/cot_param.c index dc9a3cf..b09ea53 100644 --- a/cot_param.c +++ b/cot_param.c @@ -756,7 +756,7 @@ int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewVal * * @attention 如果参数有自定义校验方式,则只有满足范围校验成功的前提下才会执行自定义校验 * @param[in] pParam 参数信息 - * @param[in] pValue 输入的参数值 + * @param[in] pValue 输入的参数值,该变量的类型需要和该参数的类型保持一致 * @param[out] peCheckResult 校验结果 * @return 0,成功; -1,失败 */ @@ -792,7 +792,7 @@ int cotParam_SingleParamCheckInput(const cotParamInfo_t *pParam, const void *pVa * @param[out] peCheckResult 校验结果 * @return 0,成功; -1,失败 */ -int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult) +int cotParam_SingleParamSelfCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult) { return cotParam_SingleParamCheckInput(pParam, pParam->unCurValuePtr.pVoid, peCheckResult); } diff --git a/cot_param.h b/cot_param.h index 806dd07..7c2e3da 100644 --- a/cot_param.h +++ b/cot_param.h @@ -155,7 +155,7 @@ extern int cotParam_SingleParamCheckProcess(const cotParamInfo_t *pParam, cotPar extern int cotParam_SingleParamUpdate(const cotParamInfo_t *pParam, const void *pNewValue, cotParamResetOpt_e eResetOpt); extern int cotParam_SingleParamCheckInput(const cotParamInfo_t *pParam, const void *pValue, cotParamCheckRet_e *peCheckResult); -extern int cotParam_SingleParamCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult); +extern int cotParam_SingleParamSelfCheck(const cotParamInfo_t *pParam, cotParamCheckRet_e *peCheckResult); extern int cotParam_SingleParamResetDefValue(const cotParamInfo_t *pParam); extern int cotParam_SingleParamResetMinValue(const cotParamInfo_t *pParam); extern int cotParam_SingleParamResetMaxValue(const cotParamInfo_t *pParam); diff --git a/test/param_test.c b/test/param_test.c index 0cadfe8..bca5e9d 100644 --- a/test/param_test.c +++ b/test/param_test.c @@ -398,80 +398,80 @@ void test_CheckRange(void) cotParamCheckRet_e eCheckResult; g_test_u16 = 60; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); g_test_u16 = 120; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); g_test_u16 = 4000; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); g_test_u16 = 60; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_DEF); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_UINT(100, g_test_u16); g_test_u16 = 4000; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_DEF); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); TEST_ASSERT_EQUAL_UINT(100, g_test_u16); g_test_u16 = 60; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 4000; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 60; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); g_test_u16 = 4000; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); g_test_u16 = 60; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_UINT(90, g_test_u16); g_test_u16 = 4000; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_u16), COT_PARAM_RESET_MIN_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); TEST_ASSERT_EQUAL_UINT(3000, g_test_u16); #if COT_PARAM_USE_STRING_TYPE snprintf(g_test_str, sizeof(g_test_str), "ABCDEF"); - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_NONE); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); TEST_ASSERT_EQUAL_STRING("ABCDEF", g_test_str); snprintf(g_test_str, sizeof(g_test_str), "AB"); - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_MIN_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_STRING("AB", g_test_str); snprintf(g_test_str, sizeof(g_test_str), "ABCDEF123456"); - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_MIN_MAX); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MAX, eCheckResult); TEST_ASSERT_EQUAL_STRING("ABCDEF1234", g_test_str); snprintf(g_test_str, sizeof(g_test_str), "AB"); - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), &eCheckResult); cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, g_test_str), COT_PARAM_RESET_DEF); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OVER_MIN, eCheckResult); TEST_ASSERT_EQUAL_STRING("abcdef", g_test_str); @@ -483,17 +483,17 @@ void test_CheckCustomWay(void) cotParamCheckRet_e eCheckResult; g_test_s16 = 200; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); #if COT_PARAM_USE_CUSTOM_CHECK g_test_s16 = 201; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OTHER_ERR, eCheckResult); #endif g_test_s16 = 202; - cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_s16), &eCheckResult); TEST_ASSERT_EQUAL_INT(COT_PARAM_CHECK_OK, eCheckResult); } -- Gitee From 84f0aee88576292009c289de7c3d3db9a5f3c0c2 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:41:35 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=8C=88=20style:=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=96=87=E4=BB=B6=E5=A4=B4=E4=BF=A1=E6=81=AF=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cot_param.c | 6 +++--- cot_param.h | 1 - cot_param_cfg.h | 1 - cot_param_type.h | 1 - 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/cot_param.c b/cot_param.c index b09ea53..1a081a2 100644 --- a/cot_param.c +++ b/cot_param.c @@ -3,13 +3,13 @@ * @file cot_param.c * @brief 该文件提供参数管理框架功能 * @author const_zpc any question please send mail to const_zpc@163.com - * @version V1.0 - * @date 2023-12-08 + * @version V2.0 + * @date 2023-12-10 * * @details 功能详细说明: * + 参数修改和重置管理 * + 参数序列化和反序列化 - * + * 数据序列化格式: * | 支持数目(1) | 数据1的长度和ID(0.5+0.5) | 数据1(n) | 数据2的长度和ID(1) | 数据2(n) | 数据3的长度和ID(1) | 数据3(n) | * | 支持数目(1) | 数据1的长度和ID(1.0+1.0) | 数据1(n) | 数据2的长度和ID(2) | 数据2(n) | 数据3的长度和ID(2) | 数据3(n) | * | 支持数目(1) | 数据1的长度和ID(1.5+1.5) | 数据1(n) | 数据2的长度和ID(3) | 数据2(n) | 数据3的长度和ID(3) | 数据3(n) | diff --git a/cot_param.h b/cot_param.h index 7c2e3da..927ce62 100644 --- a/cot_param.h +++ b/cot_param.h @@ -3,7 +3,6 @@ * @file cot_param.h * @brief 该文件提供参数管理框架所有函数原型 * @author const_zpc any question please send mail to const_zpc@163.com - * @date 2023-12-08 ********************************************************************************************************************** * ********************************************************************************************************************** diff --git a/cot_param_cfg.h b/cot_param_cfg.h index 592b68e..3062b50 100644 --- a/cot_param_cfg.h +++ b/cot_param_cfg.h @@ -3,7 +3,6 @@ * @file cot_param_cfg.h * @brief 该文件提供参数管理框架相关配置选项 * @author const_zpc any question please send mail to const_zpc@163.com - * @date 2023-12-08 ********************************************************************************************************************** * ********************************************************************************************************************** diff --git a/cot_param_type.h b/cot_param_type.h index 97a00b2..1898ed8 100644 --- a/cot_param_type.h +++ b/cot_param_type.h @@ -3,7 +3,6 @@ * @file cot_param_type.h * @brief 该文件提供参数管理框架所有函数原型 * @author const_zpc any question please send mail to const_zpc@163.com - * @date 2023-12-08 ********************************************************************************************************************** * ********************************************************************************************************************** -- Gitee From 9e994fe4a4a507c9a426180f8d6803e880dc2cd9 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:42:00 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=F0=9F=A7=AA=20test:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/cmd_shell/demo.c | 56 +++---- examples/cmd_shell/param_demo.c | 278 ++++++++++++++++---------------- examples/cmd_shell/param_demo.h | 44 ++--- 3 files changed, 183 insertions(+), 195 deletions(-) diff --git a/examples/cmd_shell/demo.c b/examples/cmd_shell/demo.c index dda9c69..b71238f 100644 --- a/examples/cmd_shell/demo.c +++ b/examples/cmd_shell/demo.c @@ -2,54 +2,38 @@ #include #include -void ShowAllParam_1(void); - int main() { - InitParam(false); - - ShowAllParam(); - ShowAllParam_1(); - - g_test = 80; - g_test_3 = -20.5; - sprintf(g_str_des, "sdx"); - - printf("\n\n"); - - SaveParam(false); - ReloadParam(false); + InitParam(true); ShowAllParam(); - ShowAllParam_1(); - // int ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 50, COT_PARAM_DEF); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); - // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_DEF); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + g_tTestVal.fValue = 20.05; - // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_NONE); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + g_tTestVal.uiValue = 50; + SingleParamCheckProcess(&g_tTestVal.uiValue, COT_PARAM_RESET_MIN_MAX); // 修改后检查并处理:如果小于最小值则恢复最小值,大于最大值则恢复最大值 - // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 101, COT_PARAM_MIN_MAX); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + g_tTestVal.uiValue = 50; + if (SingleParamSelfCheck(&g_tTestVal.uiValue) != COT_PARAM_CHECK_OK) // 修改后检查 + { + SingleParamResetResetDefValue(&g_tTestVal.uiValue); // 如果校验失败,则恢复为默认值 + } - // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 1, COT_PARAM_MIN_MAX); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + uint32_t tmp = 50; - // ret = COT_PARAM_DAT_SET_NEW_VALUE(g_test_6, 1, COT_PARAM_MIN_MAX); - // printf("ret = %d, g_test_6 = %d\n", ret, g_test_6); + if (SingleParamCheck(&g_tTestVal.uiValue, &tmp) == COT_PARAM_CHECK_OK) // 修改前检查(参数和被检查变量值类型需要一样) + { + g_tTestVal.uiValue = tmp;// 如果校验成功,则修改 + } - // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa9", COT_PARAM_NONE); - // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + // g_test = 80; + // g_test_3 = -20.5; + sprintf(g_szString, "sd"); - // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa1245249", COT_PARAM_DEF); - // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); + SaveParam(true); + ReloadParam(true); - // COT_PARAM_STR_SET_NEW_VALUE(g_test_str, "123aa945457", COT_PARAM_MIN_MAX); - // printf("ret = %d, g_test_str = %s\n", ret, g_test_str); - - // ShowAllParam(); + ShowAllParam(); return 0; } diff --git a/examples/cmd_shell/param_demo.c b/examples/cmd_shell/param_demo.c index 3fc6abd..1d665b6 100644 --- a/examples/cmd_shell/param_demo.c +++ b/examples/cmd_shell/param_demo.c @@ -3,6 +3,9 @@ #include #include +// 0 使用一次性完成参数保存/加载 1 使用分次完成参数保存/加载 +#define USE_PARAM_SAVE_LOAD_API 0 + /****************************************** 模拟储存设备操作 ******************************************************/ // 储存空间定义 uint8_t sg_buf[500]; @@ -14,132 +17,112 @@ uint32_t sg_length = 0; printf("\n"); // 从储存空间读取数据 -int OnLoadCallback(uint8_t *pBuf, uint16_t *len, bool *pisFinish) +int OnLoadCallback(uint8_t *pBuf, uint16_t bufSize, uint16_t *pLength) { - uint16_t needReadLen = *len; + uint16_t length; static uint32_t s_offset = 0; - if (sg_length == s_offset) + if (s_offset < sg_length) { - *len = 0; - s_offset = 0; - *pisFinish = true; - return 0; + if (s_offset + bufSize <= sg_length) + { + length = bufSize; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } + else + { + length = sg_length - s_offset; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } } else { - *pisFinish = false; + length = 0; + s_offset = 0; } - if (sg_length - s_offset < needReadLen) + if (length > 0) { - needReadLen = sg_length - s_offset; + printf("************************************************************************************************\n"); + HEX_PRINTF("\tread", pBuf, length); + printf("************************************************************************************************\n"); } - memcpy(pBuf, &sg_buf[s_offset], needReadLen); - HEX_PRINTF("\tread", pBuf, needReadLen); - *len = needReadLen; - s_offset += needReadLen; - + *pLength = length; return 0; } // 写数据至储存空间 -int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) +int OnSaveCallback(const uint8_t *pBuf, uint16_t len) { static uint32_t s_offset = 0; - if (isFinish) + if (len > 0) { - printf("********\n"); - HEX_PRINTF("\tsave", sg_buf, s_offset + len); + memcpy(&sg_buf[s_offset], pBuf, len); + HEX_PRINTF("\twrite", pBuf, len); + s_offset += len; + sg_length = s_offset; + } + else + { + printf("************************************************************************************************\n"); + HEX_PRINTF("\tSave", sg_buf, sg_length); + printf("************************************************************************************************\n"); s_offset = 0; - return 0; } - memcpy(&sg_buf[s_offset], pBuf, len); - HEX_PRINTF("\tsave", pBuf, len); - s_offset += len; - sg_length = s_offset; - return 0; } /****************************************** 模拟储存设备操作 ******************************************************/ -typedef struct -{ - uint16_t test1; - float test2; - char str[12]; -}ParamDemo_t; - -static ParamDemo_t sg_tTest = { - .str = "sdf", - .test1 = 20, - .test2 = 567.4 +ParamDemo_t g_tTestVal = { + .usValue = 20, + .ucValue = 10, + .uiValue = 1000, + .fValue = 3.14, + .szString_1 = "abcd", + .dValue = 5.12, + .sValue = -100, + .cValue = -2, + .iValue = 300, + .szString_2 = "12234", }; -COT_PARAM_INT16_T g_test = 50; -COT_PARAM_UINT16_T g_test_2 = 20; -COT_PARAM_DOUBLE_T g_test_3 = 3.15; -COT_PARAM_INT8_T g_test_4 = 8; -COT_PARAM_UINT32_T g_test_5 = 620; -COT_PARAM_UINT8_T g_test_6 = 45; -COT_PARAM_INT64_T g_test_7 = 5; -COT_PARAM_INT8_T g_sd = 2; -COT_PARAM_INT64_T g_test_88 = 80; -char g_test_str[15] = "abcdef"; -char g_str_des[13] = "EERR"; -char g_str_des_2[15] = "sdRR"; -char g_str_des_3[15] = "ewRR"; - -// int a[10]; +int8_t g_cTest = 50; +char g_szString[10] = "qwer"; + +static int CheckSValue(const void *pCurParam); cotParamInfo_t sg_ParamTable[] = { - COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND_WITH_NAME(2, "ds", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), - // COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), - COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), - COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), - COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), - COT_PARAM_ITEM_BIND(6, g_test_5, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 620, 500, 10000), - COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), - COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), - COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), - COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), - COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), - COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(17, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), + COT_PARAM_ITEM_BIND(1, g_tTestVal.usValue, COT_PARAM_UINT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(2, g_tTestVal.ucValue, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_tTestVal.uiValue, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 1000, 1000, 10000), + COT_PARAM_ITEM_BIND(4, g_tTestVal.fValue, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR, 10, -10.5, 10.5), + COT_PARAM_ITEM_BIND(5, g_tTestVal.szString_1, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcd", 3, sizeof(g_tTestVal.szString_1)), + COT_PARAM_ITEM_BIND(6, g_tTestVal.dValue, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 0, -90.10, 100.10), + COT_PARAM_ITEM_BIND(7, g_tTestVal.sValue, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -200, 200, CheckSValue), // 添加自定义校验 + COT_PARAM_ITEM_BIND_WITH_NAME(8, "g_cTest", g_cTest, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 50, -100, 100), // 另取参数名 + COT_PARAM_ITEM_BIND(9, g_szString, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "XXX", 3, 6), }; -COT_PARAM_UINT64_T g_test_55 = 62055; - -cotParamInfo_t sg_ParamTable1[] = { - COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(2, g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), - // COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), - // COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), - // COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), - COT_PARAM_ITEM_BIND(6, g_test_55, COT_PARAM_UINT64, COT_PARAM_ATTR_WR, 62055, 500, 10000), - COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), - COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), - // COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), - // COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), - COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), - COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), - COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(16, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), -}; +static int CheckSValue(const void *pCurParam) +{ + const int16_t *p_sValue = (const int16_t *)pCurParam; + + if ((*p_sValue) % 2 != 0) + { + return -1; + } + + return 0; +} static cotParamManager_t sg_tParamManager; -static cotParamManager_t sg_tParamManager1; static void ShowSingleParam(const cotParamInfo_t *paramInfo); @@ -147,7 +130,7 @@ static void ShowSingleParam(const cotParamInfo_t *paramInfo); int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e eCheckResult) { printf("\n--------error list start ----------\n"); - printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); + printf("%4s %-24s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); ShowSingleParam(pParamInfo); printf("--------error list end ------------\n"); cotParam_SingleParamResetDefValue(pParamInfo); @@ -157,14 +140,11 @@ int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e void InitParam(bool isReset) { cotParam_Init(&sg_tParamManager, sg_ParamTable, COT_PARAM_TABLE_SIZE(sg_ParamTable)); - cotParam_Init(&sg_tParamManager1, sg_ParamTable1, COT_PARAM_TABLE_SIZE(sg_ParamTable1)); if (sg_length == 0) // 储存设备中没有储存过参数则首次进行储存 { printf("frist save param: %d\n", cotParam_GetSerializeSize(&sg_tParamManager)); SaveParam(false); - - // cotParam_Save(&sg_tParamManager, OnSaveCallback, NULL); // 初次储存可以认为数据都是默认值,无需数据校验出错时恢复默认处理 } ReloadParam(isReset); @@ -172,46 +152,49 @@ void InitParam(bool isReset) void ReloadParam(bool isReset) { - printf("load param\n"); + uint8_t buf[300]; + uint16_t length; + + printf("\n============================ load param start ==========================\n"); if (isReset) { cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); - cotParam_Load(&sg_tParamManager, OnLoadCallback); - // cotParam_Deserialization(&sg_tParamManager1, sg_buf, sg_length); - // cotParam_Load(&sg_tParamManager1, OnLoadCallback, NULL); - } - else - { - cotParam_Load(&sg_tParamManager, OnLoadCallback); - // cotParam_Deserialization(&sg_tParamManager1, sg_buf, sg_length); - // cotParam_Load(&sg_tParamManager1, OnLoadCallback, NULL); } -} -void ResetParam(void) -{ - printf("reset param\n"); - cotParam_ResetDefault(&sg_tParamManager); +#if USE_PARAM_SAVE_LOAD_API + cotParam_Load(&sg_tParamManager, OnLoadCallback); +#else + // OnLoadCallback(sg_buf, sg_length, true); + cotParam_Deserialization(&sg_tParamManager, sg_buf, sg_length); +#endif + printf("============================ load param end ============================\n\n"); } void SaveParam(bool isReset) { uint8_t buf[300]; uint32_t bufLength = 0; - printf("save param\n"); + printf("\n============================ save param start ==========================\n"); if (isReset) { cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); - cotParam_Save(&sg_tParamManager, OnSaveCallback); - } - else - { - bufLength = cotParam_Serialize(&sg_tParamManager, buf); - OnSaveCallback(buf, bufLength, false); - OnSaveCallback(buf, 0, true); - // cotParam_Save(&sg_tParamManager, OnSaveCallback); } + +#if USE_PARAM_SAVE_LOAD_API + cotParam_Save(&sg_tParamManager, OnSaveCallback); +#else + bufLength = cotParam_Serialize(&sg_tParamManager, buf); + OnSaveCallback(buf, bufLength); + OnSaveCallback(buf, 0); +#endif + printf("============================ save param end ============================\n\n"); +} + +void ResetParam(void) +{ + printf("reset param\n"); + cotParam_ResetDefault(&sg_tParamManager); } #define ATTR(x) ((x & (PARAM_ATTR_READ | COT_PARAM_ATTR_WRITE)) == (PARAM_ATTR_READ | COT_PARAM_ATTR_WRITE) ? \ @@ -252,7 +235,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) switch (paramInfo->type) { case COT_PARAM_INT8: - printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt8); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -272,7 +255,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT16: - printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt16); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -292,7 +275,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT32: - printf(" %-4d %-15s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, "int32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt32); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -312,7 +295,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT64: - printf(" %-4d %-15s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->pszName, "int64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt64); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -332,7 +315,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT8: - printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint8); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -352,7 +335,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT16: - printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint16); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -372,7 +355,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT32: - printf(" %-4d %-15s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, "uint32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint32); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -392,7 +375,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT64: - printf(" %-4d %-15s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->pszName, "uint64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint64); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -412,7 +395,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_FLOAT: - printf(" %-4d %-15s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, "float", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pFloat); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -432,7 +415,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_DOUBLE: - printf(" %-4d %-15s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, "double", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pDouble); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -453,7 +436,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) break; #if COT_PARAM_USE_STRING_TYPE case COT_PARAM_STRING: - printf(" %-4d %-15s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->pszName, "string", paramInfo->length, Attr(paramInfo->attr), paramInfo->unCurValuePtr.pString); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -482,32 +465,47 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } } -void ShowAllParam(void) +int SingleParamCheckProcess(const void *pCurParam, cotParamResetOpt_e eResetOpt) { - size_t idx = 0; - cotParamInfo_t *paramInfo; + return cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, pCurParam), eResetOpt); +} - printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); +cotParamCheckRet_e SingleParamCheck(const void *pCurParam, const void *pCheckValue) +{ + cotParamCheckRet_e eCheckResult; - do - { - paramInfo = cotParam_IterateList(&sg_tParamManager, &idx); + cotParam_SingleParamCheckInput(cotParam_FindParamByParamPtr(&sg_tParamManager, pCurParam), pCheckValue, &eCheckResult); - ShowSingleParam(paramInfo); - } while (paramInfo != NULL); + return eCheckResult; +} + +cotParamCheckRet_e SingleParamSelfCheck(const void *pCurParam) +{ + cotParamCheckRet_e eCheckResult; + + cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, pCurParam), &eCheckResult); + + return eCheckResult; +} + +void SingleParamResetResetDefValue(const void *pCurParam) +{ + cotParam_SingleParamResetDefValue(cotParam_FindParamByParamPtr(&sg_tParamManager, pCurParam)); } -void ShowAllParam_1(void) +void ShowAllParam(void) { size_t idx = 0; cotParamInfo_t *paramInfo; - printf("%4s %-15s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); + printf("%4s %-24s %-8s %-8s %-8s %-10s %-10s %-10s %-10s\n", "id", "name", "type", "length", "attr", "val", "def", "min", "max"); do { - paramInfo = cotParam_IterateList(&sg_tParamManager1, &idx); + paramInfo = cotParam_IterateList(&sg_tParamManager, &idx); ShowSingleParam(paramInfo); } while (paramInfo != NULL); + printf("\n"); } + diff --git a/examples/cmd_shell/param_demo.h b/examples/cmd_shell/param_demo.h index 61623d9..3b93d0a 100644 --- a/examples/cmd_shell/param_demo.h +++ b/examples/cmd_shell/param_demo.h @@ -3,25 +3,24 @@ #include "cot_param.h" +typedef struct +{ + uint16_t usValue; + uint8_t ucValue; + uint32_t uiValue; + float fValue; + char szString_1[12]; + double dValue; + int16_t sValue; + int8_t cValue; + int32_t iValue; + char szString_2[10]; +}ParamDemo_t; + +extern ParamDemo_t g_tTestVal; +extern int8_t g_cTest; +extern char g_szString[10]; -extern COT_PARAM_INT16_T g_test; -extern COT_PARAM_UINT16_T g_test_2; -extern COT_PARAM_DOUBLE_T g_test_3; -extern COT_PARAM_INT8_T g_test_4; -extern COT_PARAM_UINT32_T g_test_5; -extern COT_PARAM_UINT8_T g_test_6; -extern COT_PARAM_INT64_T g_test_7; -extern COT_PARAM_INT8_T g_sd; -extern COT_PARAM_INT64_T g_test_88; -extern char g_test_str[15]; -extern char g_str_des[13]; -extern char g_str_des_2[15]; -extern char g_str_des_3[15]; - -#define COT_PARAM_UPDATE(name, val, ret) {\ - param_##name##_t t = val;\ - SetParamNewValue(&name, &t);\ -} void InitParam(bool isReset); void ReloadParam(bool isReset); @@ -29,6 +28,13 @@ void ReloadParam(bool isReset); void ResetParam(void); void SaveParam(bool isReset); -extern const bool SetParamNewValue(const void *curParamPtr, const void *newValue); +int SingleParamCheckProcess(const void *pCurParam, cotParamResetOpt_e eResetOpt); + +cotParamCheckRet_e SingleParamCheck(const void *pCurParam, const void *pCheckValue); + +cotParamCheckRet_e SingleParamSelfCheck(const void *pCurParam); + +void SingleParamResetResetDefValue(const void *pCurParam); + void ShowAllParam(void); #endif -- Gitee From f2e0d16026a3b2b7c2d8efc5f2cc754cfb2b49d8 Mon Sep 17 00:00:00 2001 From: constzpc <66413177+constzpc@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:42:31 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=F0=9F=93=83=20docs:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 244 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 166 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index babc6e7..bfc4942 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,8 @@ > - 方便在本地储存设备(如flash、eeprom等)保存/读取二进制数据,甚至还可以跨设备传输使用 > - 提供了两种方式: - > - 保存/加载:提供参数实际保存/加载的回调函数,通过多次触发回调函数完成参数的序列化保存、加载反序列化功能;适用于小内存的平台使用(不需要申请内存处理) - > - 序列化和反序列化:需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据;一次性完成操作(需要申请较大的内存完成) + > - 第一种:只需要提供参数数据保存/加载的回调函数,调用相关接口函数完成参数的序列化保存和反序列化加载;该方式会多次触发回调函数保存数据和读取数据,适用于小内存的平台使用(不需要申请内存) + > - 第二种:需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据;一次性完成操作(需要申请较大的内存完成) > * [X] 支持启用键值对功能 @@ -101,51 +101,60 @@ ```c typedef struct { - uint16_t test1; - float test2; - char str[12]; + uint16_t usValue; + uint8_t ucValue; + uint32_t uiValue; + float fValue; + char szString_1[12]; + double dValue; + int16_t sValue; + int8_t cValue; + int32_t iValue; + char szString_2[10]; }ParamDemo_t; -static ParamDemo_t sg_tTest = { - .str = "sdf", - .test1 = 20, - .test2 = 567.4 +ParamDemo_t g_tTestVal = { + .usValue = 20, + .ucValue = 10, + .uiValue = 1000, + .fValue = 3.14, + .szString_1 = "abcd", + .dValue = 5.12, + .sValue = -100, + .cValue = -2, + .iValue = 300, + .szString_2 = "12234", }; -int16_t g_test = 50; -uint16_t g_test_2 = 20; -double g_test_3 = 3.15; -COT_PARAM_INT8_T g_test_4 = 8; -COT_PARAM_UINT32_T g_test_5 = 620; -COT_PARAM_UINT8_T g_test_6 = 45; -COT_PARAM_INT64_T g_test_7 = 5; -COT_PARAM_INT8_T g_sd = 2; -COT_PARAM_INT64_T g_test_88 = 80; -char g_test_str[15] = "abcdef"; -char g_str_des[13] = "EERR"; -char g_str_des_2[15] = "sdRR"; -char g_str_des_3[15] = "ewRR"; +int8_t g_cTest = 50; +char g_szString[10] = "qwer"; + +static int CheckSValue(const void *pCurParam); cotParamInfo_t sg_ParamTable[] = { - COT_PARAM_ITEM_BIND(1, g_test, COT_PARAM_INT16, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND_WITH_NAME(2, "test_2", g_test_2, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20), // 另取名字 - COT_PARAM_ITEM_BIND(3, g_test_3, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 3.15, -2.15, 5.12), - COT_PARAM_ITEM_BIND(4, g_test_str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcdef", 0, sizeof(g_test_str)), - COT_PARAM_ITEM_BIND(5, g_test_4, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 8, -10, 10), - COT_PARAM_ITEM_BIND(6, g_test_5, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 620, 500, 10000), - COT_PARAM_ITEM_BIND(7, g_test_6, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 45, 5, 100), - COT_PARAM_ITEM_BIND(8, g_test_7, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(9, sg_tTest.test1, COT_PARAM_UINT16, COT_PARAM_ATTR_WR, 20, 10, 2000), - COT_PARAM_ITEM_BIND(10, sg_tTest.test2, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(11, sg_tTest.str, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "const-zpc", 6, sizeof(sg_tTest.str)), - COT_PARAM_ITEM_BIND(12, g_test_88, COT_PARAM_INT64, COT_PARAM_ATTR_WR, 5, -542, 5450), - COT_PARAM_ITEM_BIND(13, g_str_des, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WER45", 10, sizeof(g_str_des)), - COT_PARAM_ITEM_BIND(14, g_str_des_2, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "WTG"), - COT_PARAM_ITEM_BIND(15, g_str_des_3, COT_PARAM_STRING, COT_PARAM_ATTR_WR), - COT_PARAM_ITEM_BIND(17, g_sd, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 5), + COT_PARAM_ITEM_BIND(1, g_tTestVal.usValue, COT_PARAM_UINT16, COT_PARAM_ATTR_WR), + COT_PARAM_ITEM_BIND(2, g_tTestVal.ucValue, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 20), + COT_PARAM_ITEM_BIND(3, g_tTestVal.uiValue, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 1000, 1000, 10000), + COT_PARAM_ITEM_BIND(4, g_tTestVal.fValue, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR, 10, -10.5, 10.5), + COT_PARAM_ITEM_BIND(5, g_tTestVal.szString_1, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcd", 3, sizeof(g_tTestVal.szString_1)), + COT_PARAM_ITEM_BIND(6, g_tTestVal.dValue, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 0, -90.10, 100.10), + COT_PARAM_ITEM_BIND(7, g_tTestVal.sValue, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -200, 200, CheckSValue), // 添加自定义校验 + COT_PARAM_ITEM_BIND_WITH_NAME(8, "g_cTest", g_cTest, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 50, -100, 100), // 另取参数名 + COT_PARAM_ITEM_BIND(9, g_szString, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "XXX", 3, 6), }; -static cotParamManager_t sg_tParamManager; +static int CheckSValue(const void *pCurParam) +{ + const int16_t *p_sValue = (const int16_t *)pCurParam; + + if ((*p_sValue) % 2 != 0) + { + return -1; + } + + return 0; +} + int mian() { @@ -157,7 +166,15 @@ int mian() ### 参数保存/加载 -1. 保存/加载方式(函数内部完成序列化和反序列化,逐步写入保存/读取加载) +将参数表的部分信息(包括当前值)进行序列化,方便进行数据保存,等下次开机再读取数据后反序列化,还原成参数表的这部分信息。 + +> 甚至将这部分数据传输给另一台设备,进行参数数据备份或同步等功能 + +将参数表序列化完成后,可以对数据进行二次封装,比如可以增加头信息,crc校验等再进行数据保存,同样在加载时也需要解析,将额外添加的信息去除后才能进行反序列化。 + +##### 第一种 + +提供参数数据保存/加载的回调函数,调用相关接口函数完成参数的序列化保存和反序列化加载;该方式会多次触发回调函数保存数据和读取数据,适用于小内存的平台使用(不需要额外申请内存) ```c @@ -168,45 +185,53 @@ int OnCheckErrorResetHandle(const cotParamInfo_t *pParamInfo, cotParamCheckRet_e return 0; } -// 从储存设备多次读取 -int OnLoadCallback(uint8_t *pBuf, uint16_t *p_len, bool *pisFinish) +// 从储存空间读取数据 +int OnLoadCallback(uint8_t *pBuf, uint16_t bufSize, uint16_t *pLength) { + uint16_t length; static uint32_t s_offset = 0; - if (sg_length == s_offset) + if (s_offset < sg_length) { - *p_len = 0; - s_offset = 0; - *pisFinish = true; - return 0; + if (s_offset + bufSize <= sg_length) + { + length = bufSize; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } + else + { + length = sg_length - s_offset; + memcpy(pBuf, &sg_buf[s_offset], length); + s_offset += length; + } } else { - *pisFinish = false; + length = 0; + s_offset = 0; } - seek(s_offset); - *p_len= read(pBuf, *p_len); - s_offset += (*len); - + *pLength = length; return 0; } // 写数据至储存空间 -int OnSaveCallback(const uint8_t *pBuf, uint16_t len, bool isFinish) +int OnSaveCallback(const uint8_t *pBuf, uint16_t len) { static uint32_t s_offset = 0; - if (isFinish) + if (len > 0) + { + memcpy(&sg_buf[s_offset], pBuf, len); + s_offset += len; + sg_length = s_offset; + } + else { s_offset = 0; - return 0; } - seek(s_offset); - write(pBuf, len); - s_offset += len; - return 0; } @@ -215,36 +240,84 @@ void LoadParam(void) { cotParam_Load(&sg_tParamManager, OnLoadCallback); - // 加载后全部参数进行校验 + // 加载后全部参数进行校验(可选) cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); } +// 保存参数 void SaveParam(void) { - // 保存前全部参数进行校验 + // 保存前全部参数进行校验(可选) cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); cotParam_Save(&sg_tParamManager, OnSaveCallback); } ``` -2. 序列化/反序列化方式(一次性写入保存/读取加载) +#### 第二种 + +需要提前申请内存用来保存参数表序列化的数据或者读取即将反序列化的数据。 + +1. 定义buf ```c -//函数中使用 -int main() +// 加载参数 +void LoadParam(void) { -#if 0 - // 保存 - uint8_t w_buf[500]; - uint32_t length = cotParam_Serialize(&sg_tParamManager, w_buf); - write(w_buf, length); + uint8_t buf[500]; + uint32_t length; + + length = read(buf); + cotParam_Deserialization(&sg_tParamManager, buf, length); - // 加载 - uint8_t r_buf[500]; - uint32_t length = read(r_buf); - cotParam_Deserialization(&sg_tParamManager, r_buf, length); -#else + // 加载后全部参数进行校验(可选) + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); +} + +// 保存参数 +void SaveParam(void) +{ + uint8_t buf[500]; + uint32_t length; + + // 保存前全部参数进行校验(可选) + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + length = cotParam_Serialize(&sg_tParamManager, buf); + write(buf, length); +} + +``` + +2. buf通过内存申请 + +```c +// 加载参数 +void LoadParam(void) +{ + uint8_t pBuf = (uint8_t *)malloc(500); // 申请足够大的内存 + uint32_t length; + + length = read(pBuf, length); // 获取数据的实际长度 + cotParam_Deserialization(&sg_tParamManager, pBuf, length); + + // 加载后全部参数进行校验(可选) + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); +} + +// 保存参数 +void SaveParam(void) +{ + uint8_t pBuf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); + uint32_t length; + + // 保存前全部参数进行校验(可选) + cotParam_Check(&sg_tParamManager, OnCheckErrorResetHandle); + length = cotParam_Serialize(&sg_tParamManager, pBuf); + write(buf, length); +} + +int main() +{ // 保存 uint8_t *p_wbuf = (uint8_t *)malloc(cotParam_GetSerializeSize(&sg_tParamManager)); @@ -261,11 +334,10 @@ int main() write(p_rbuf, length); free(p_rbuf); p_rbuf = NULL; -#endif } ``` -### 校验处理 +### 校验方式 校验需要提前在参数表中设置缺省值,最小值和最大值后才有效。 @@ -274,16 +346,32 @@ int main() 根据参数设置范围进行校验。 ```c -// 对某个变量当前参数进行范围校验,得到校验结果 -cotParam_SingleParamCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &eCheckResult); +// 对某个变量当前参数进行范围校验,得到校验结果后自行处理 +cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &eCheckResult); + +if (eCheckResult != COT_PARAM_CHECK_OK) // 修改后检查 +{ + cotParam_SingleParamResetDefValue(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3)); // 如果校验失败,则恢复为默认值 +} + // 对某个变量参数变更后(当前值已经变化)进行校验处理,若超出范围则恢复默认 g_test_3 = 1000; cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), COT_PARAM_RESET_DEF); +// 对某个变量参数在需要变更前(当前值没有变化)进行校验处理,得到校验结果后自行处理 +double tmp = 1000; +cotParam_SingleParamCheckInput(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, &eCheckResult); + +if (eCheckResult == COT_PARAM_CHECK_OK) +{ + g_test_3 = tmp;// 如果校验成功,则修改 +} + // 对某个变量参数在需要变更前(当前值没有变化)进行校验处理,若新的值超出范围则不更新变量参数当前的值 double tmp = 1000; cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, COT_PARAM_RESET_NONE) + ``` #### 自定义校验 -- Gitee