Merge pull request #4885 from willianchanlovegithub/WillianChan

[add][components][utilities]Add the Var Export component
This commit is contained in:
guo
2021-10-11 11:02:14 +08:00
committed by GitHub
7 changed files with 553 additions and 0 deletions
+5
View File
@@ -26,7 +26,12 @@ SECTIONS
__vsymtab_start = .;
KEEP(*(VSymTab))
__vsymtab_end = .;
/* section information for var export */
. = ALIGN(4);
__ve_table_start = .;
KEEP(*(SORT(*.VarExpTab.*)))
__ve_table_end = .;
/* section information for modules */
. = ALIGN(4);
+4
View File
@@ -205,6 +205,10 @@ config RT_USING_UTEST
default 20
endif
config RT_USING_VAR_EXPORT
bool "Enable Var Export"
default n
source "$RTT_DIR/components/utilities/rt-link/Kconfig"
endmenu
@@ -0,0 +1,8 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('var_export', src, depend = ['RT_USING_VAR_EXPORT'], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-04 WillianChan first version
* 2021-06-08 WillianChan support to MS VC++ compiler
*/
#ifndef _VAR_EXPORT_H__
#define _VAR_EXPORT_H__
#include <rtthread.h>
/* exported object */
struct ve_exporter
{
const char *module; /* module name */
const char *identifier; /* module identifier */
rt_base_t value; /* module value */
};
typedef struct ve_exporter ve_exporter_t;
/* module object */
struct ve_module
{
const ve_exporter_t *begin; /* the first module of the same name */
const ve_exporter_t *end; /* the last module of the same */
};
typedef struct ve_module ve_module_t;
/* iterator object */
struct ve_iterator
{
const ve_exporter_t *exp_index; /* iterator index */
const ve_exporter_t *exp_end; /* iterate over exporter */
};
typedef struct ve_iterator ve_iterator_t;
#define VE_NOT_FOUND (0xFFFFFFFFu) /* not found */
/* exporter's export command */
#if defined(__GNUC__)
#define VAR_EXPORT(module, identi, value) \
const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
RT_USED const struct ve_exporter _vexp_##module##identi \
RT_SECTION(#module".VarExpTab."#identi) = \
{ \
_vexp_##identi##_module, \
_vexp_##identi##_identi, \
value, \
}
#elif defined(_MSC_VER)
#pragma section("VarExpTab$f",read)
#define VAR_EXPORT(module, identi, value) \
const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
__declspec(allocate("VarExpTab$f")) \
RT_USED const struct ve_exporter _vexp_##module##identi = \
{ \
_vexp_##identi##_module, \
_vexp_##identi##_identi, \
value, \
}
#else
#define VAR_EXPORT(module, identi, value) \
const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
RT_USED const struct ve_exporter _vexp_##module##identi \
RT_SECTION("1."#module".VarExpTab."#identi) = \
{ \
_vexp_##identi##_module, \
_vexp_##identi##_identi, \
value, \
}
#endif
/* initialize var export */
int ve_exporter_init(void);
/* initialize module */
int ve_module_init(ve_module_t *mod, const char *module);
/* initialize iterator */
void ve_iter_init(ve_module_t *mod, ve_iterator_t *iter);
/* iterate backward */
const ve_exporter_t *ve_iter_next(ve_iterator_t *iter);
/* get the value by identifier */
rt_base_t ve_value_get(ve_module_t *mod, const char *identifier);
/* check if this value exists in the module*/
rt_bool_t ve_value_exist(ve_module_t *mod, const char *identifier);
#endif /* _VAR_EXPORT_H__ */
@@ -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 <var_export.h>
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 <module> - Find by module name\n");
rt_kprintf("ve_find value <module> <identifier> - 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_ERROR;
}
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_ERROR;
}
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);
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-09-02 WillianChan add var_export example code
*/
#include <var_export.h>
/* use VAR_EXPOR() to export 10 pieces of data into sections */
VAR_EXPORT(module0, identi0, 0);
VAR_EXPORT(module1, identi1, 1);
VAR_EXPORT(module2, identi2, 2);
VAR_EXPORT(module3, identi3, 3);
VAR_EXPORT(module4, identi4, 4);
VAR_EXPORT(module5, identi5, 5);
VAR_EXPORT(module6, identi6, 6);
VAR_EXPORT(module7, identi7, 7);
VAR_EXPORT(module8, identi8, 8);
VAR_EXPORT(module9, identi9, 9);
void found_by_module(void)
{
ve_iterator_t iter;
const ve_exporter_t* exporter;
ve_module_t module;
rt_base_t value;
/* query all exporters with the same name as module1 */
if (!ve_module_init(&module, "module1"))
/* initialize the iterator */
ve_iter_init(&module, &iter);
else
return;
while (1)
{
/* start iterating */
exporter = ve_iter_next(&iter);
if (exporter == RT_NULL)
break;
else
{
/* checks whether the value exists */
if (ve_value_exist(&module, "identi1") == RT_TRUE)
{
value = ve_value_get(&module, "identi1");
rt_kprintf("[ve_example] value = %dn", value);
return;
}
else
{
rt_kprintf("[ve_example] value not exist.\n");
return;
}
}
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
MSH_CMD_EXPORT(found_by_module, found by module);
#endif /* RT_USING_FINSH */