From 9f543f7065f34bcb37c042f23dd9970b8b50ab03 Mon Sep 17 00:00:00 2001 From: WillianChan Date: Fri, 23 Jul 2021 19:48:30 +0800 Subject: [add][components][utilities]Add the Var Export component Signed-off-by: WillianChan --- components/utilities/var_export/var_export_cmd.c | 166 +++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 components/utilities/var_export/var_export_cmd.c (limited to 'components/utilities/var_export/var_export_cmd.c') diff --git a/components/utilities/var_export/var_export_cmd.c b/components/utilities/var_export/var_export_cmd.c new file mode 100644 index 0000000000..85f43507f1 --- /dev/null +++ b/components/utilities/var_export/var_export_cmd.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-05 WillianChan first version + */ + +#include + +static int ve_cmd_help(int argc, char **argv); +static int ve_find_module(int argc, char **argv); +static int ve_find_value(int argc, char **argv); + + +struct ve_cmd_des +{ + const char *cmd; + int (*fun)(int argc, char **argv); +}; + +/* dcm cmd table */ +static const struct ve_cmd_des cmd_tab[] = +{ + {"module", ve_find_module}, + {"value", ve_find_value}, +}; + +static int ve_cmd_help(int argc, char **argv) +{ + rt_kprintf("Usage:\n"); + rt_kprintf("ve_find module - Find by module name\n"); + rt_kprintf("ve_find value - Find accurately\n"); + + return RT_EOK; +} + +rt_inline void ve_object_split(int len) +{ + while (len--) rt_kprintf("-"); +} + +static int ve_find_module(int argc, char **argv) +{ + ve_iterator_t iter; + const ve_exporter_t *exporter; + ve_module_t module; + int maxlen = (RT_NAME_MAX * 2); + const char *item_title = "ve_module"; + + rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen); + rt_kprintf(" ---------------- -----\n"); + + if (!ve_module_init(&module, argv[2])) + { + ve_iter_init(&module, &iter); + } + else + { + return -RT_ERROR; + } + + while (1) + { + exporter = ve_iter_next(&iter); + if (exporter == RT_NULL) + { + return RT_EOK; + } + else + { + rt_kprintf("%-*.s %-*.s %d\n", + maxlen, exporter->module, + maxlen, exporter->identifier, + exporter->value); + } + } +} + +static int ve_find_value(int argc, char **argv) +{ + ve_iterator_t iter; + const ve_exporter_t *exporter; + ve_module_t module; + int maxlen = (RT_NAME_MAX * 2); + const char *item_title = "ve_module"; + + rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen); + rt_kprintf(" ---------------- -----\n"); + + if (!ve_module_init(&module, argv[2])) + { + ve_iter_init(&module, &iter); + } + else + { + return -RT_ERROR; + } + + while (1) + { + exporter = ve_iter_next(&iter); + if (exporter == RT_NULL) + { + return RT_EOK; + } + else + { + if (!rt_strcmp(exporter->identifier, argv[3])) + { + rt_kprintf("%-*.s %-*.s %d\n", + maxlen, exporter->module, + maxlen, exporter->identifier, + exporter->value); + + return RT_EOK; + } + } + } +} + +static int ve_find(int argc, char **argv) +{ + int i, resule = RT_EOK; + const struct ve_cmd_des *run_cmd = RT_NULL; + + if (argc == 1) + { + ve_cmd_help(argc, argv); + return RT_EOK; + } + + /* find command function */ + for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++) + { + if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0) + { + run_cmd = &cmd_tab[i]; + break; + } + } + + /* not find command function, print help information */ + if (run_cmd == RT_NULL) + { + rt_kprintf("There is no command option named %s.\n", argv[1]); + ve_cmd_help(argc, argv); + return RT_EOK; + } + + /* run command function */ + if (run_cmd->fun != RT_NULL) + { + resule = run_cmd->fun(argc, argv); + } + + if (resule) + { + ve_cmd_help(argc, argv); + } + + return RT_EOK; +} +MSH_CMD_EXPORT(ve_find, find the specified export variable); -- cgit v1.3.1 From e53ae3ddfa0e0ca9857283c934d03304578545aa Mon Sep 17 00:00:00 2001 From: WillianChan Date: Thu, 2 Sep 2021 20:02:08 +0800 Subject: [modify][components][utilities]check if the ve_exporter_num is out of bounds. Signed-off-by: WillianChan --- components/utilities/var_export/var_export.c | 3 +++ components/utilities/var_export/var_export_cmd.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'components/utilities/var_export/var_export_cmd.c') diff --git a/components/utilities/var_export/var_export.c b/components/utilities/var_export/var_export.c index d90323edde..f4623dec04 100644 --- a/components/utilities/var_export/var_export.c +++ b/components/utilities/var_export/var_export.c @@ -69,6 +69,9 @@ int var_export_init(void) ve_exporter_table = (const ve_exporter_t *)ptr_begin; ve_exporter_num = (ptr_end - ptr_begin) / (sizeof(struct ve_exporter) / sizeof(unsigned int)) + 1; + /* check if the ve_exporter_num is out of bounds */ + RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t))); + for (index_i = 0; index_i < ve_exporter_num; index_i++) { ve_exporter_tab[index_i] = ve_exporter_table[index_i]; diff --git a/components/utilities/var_export/var_export_cmd.c b/components/utilities/var_export/var_export_cmd.c index 85f43507f1..fba3cc92bb 100644 --- a/components/utilities/var_export/var_export_cmd.c +++ b/components/utilities/var_export/var_export_cmd.c @@ -67,7 +67,7 @@ static int ve_find_module(int argc, char **argv) exporter = ve_iter_next(&iter); if (exporter == RT_NULL) { - return RT_EOK; + return -RT_ERROR; } else { @@ -104,7 +104,7 @@ static int ve_find_value(int argc, char **argv) exporter = ve_iter_next(&iter); if (exporter == RT_NULL) { - return RT_EOK; + return -RT_ERROR; } else { -- cgit v1.3.1