增加rt_strnlen,lua采用单独的线程

This commit is contained in:
zhangjun 2016-08-12 22:14:07 +08:00
parent 891050cb3d
commit 65ac4a0810
2 changed files with 100 additions and 20 deletions

View File

@ -20,8 +20,12 @@ rt_err_t lua_rx_ind(rt_device_t dev, rt_size_t size)
return RT_EOK; return RT_EOK;
} }
struct para{
int argc;
char *argv[3];
};
void finsh_lua(int argc, char **argv) void finsh_lua(struct para *parameters)
{ {
rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
@ -31,12 +35,21 @@ void finsh_lua(int argc, char **argv)
rx_indicate = dev4lua.device->rx_indicate; rx_indicate = dev4lua.device->rx_indicate;
/* set new rx_indicate */ /* set new rx_indicate */
rt_device_set_rx_indicate(dev4lua.device, lua_rx_ind); //rt_device_set_rx_indicate(dev4lua.device, lua_rx_ind);
{ {
/* run lua interpreter */ int argc = parameters->argc;
lua_main(argc, argv); char **argv = parameters->argv;
/*
rt_kprintf("argc =%d, argv[1] =%d\n", argc, argv[1]);
while(1);
*/
/* run lua interpreter */
lua_main(argc, argv);
} }
if (parameters->argc > 1)
rt_free(parameters->argv[1]);
rt_free(parameters);
/* recover old rx_indicate */ /* recover old rx_indicate */
rt_device_set_rx_indicate(dev4lua.device, rx_indicate); rt_device_set_rx_indicate(dev4lua.device, rx_indicate);
@ -44,6 +57,7 @@ void finsh_lua(int argc, char **argv)
static void lua(void *parameters) static void lua(void *parameters)
{ {
rt_thread_t lua_thread;
const char* device_name = finsh_get_device(); const char* device_name = finsh_get_device();
rt_device_t device = rt_device_find(device_name); rt_device_t device = rt_device_find(device_name);
if (device == RT_NULL) if (device == RT_NULL)
@ -52,29 +66,49 @@ static void lua(void *parameters)
return; return;
} }
dev4lua.device = device; dev4lua.device = device;
char *argv[] = {"lua", parameters, NULL};
#if 0 /*prepare parameters*/
struct para *lua_parameters = rt_malloc(sizeof(struct para));
if ( lua_parameters == NULL ){
rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
return;
}
lua_parameters->argc = 2;
char **arg = lua_parameters->argv;
arg[0] = "lua";
if (parameters != NULL){
rt_size_t len = strnlen(parameters, 50);
arg[1] = rt_malloc(len + 1);
if (arg[1] == NULL ){
rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
return;
}
rt_memset(arg[1], 0, len+1);
strncpy(arg[1], parameters, len);
}else{
arg[1] = NULL;
}
arg[2] = NULL;
/* Run lua interpreter in separate thread */ /* Run lua interpreter in separate thread */
lua_thread = rt_thread_create("lua", lua_thread = rt_thread_create("lua",
finsh_lua, (void (*)(void *))finsh_lua,
0, (void*)lua_parameters,
2048, 10240,
rt_thread_self()->current_priority + 1, rt_thread_self()->current_priority + 1,
20); 20);
if (lua_thread != RT_NULL) if (lua_thread != RT_NULL)
{ {
rt_thread_startup(lua_thread); rt_thread_startup(lua_thread);
} }
#else return;
/* Directly run lua interpreter in finsh */
finsh_lua(2, argv);
#endif
} }
FINSH_FUNCTION_EXPORT(lua, lua interpreter); FINSH_FUNCTION_EXPORT(lua, lua interpreter);
static void lua_msh(int argc, char **argv) static void lua_msh(int argc, char **argv)
{ {
rt_thread_t lua_thread;
const char* device_name = finsh_get_device(); const char* device_name = finsh_get_device();
rt_device_t device = rt_device_find(device_name); rt_device_t device = rt_device_find(device_name);
if (device == RT_NULL) if (device == RT_NULL)
@ -85,15 +119,42 @@ static void lua_msh(int argc, char **argv)
dev4lua.device = device; dev4lua.device = device;
/*prepare parameters*/ /*prepare parameters*/
int i; struct para *parameters = rt_malloc(sizeof(struct para));
char **arg = rt_malloc((argc+1)*sizeof(char*)); if ( parameters == NULL ){
for (i=0; i<argc; i++){ rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
arg[i] = argv[i]; return;
} }
arg[argc] = NULL; //parameters->argc = 2;
parameters->argc = argc;
char **arg = parameters->argv;
finsh_lua(argc, arg); arg[0] = "lua";
rt_free(arg); if (argc > 1){
rt_size_t len = strnlen(argv[1], 50);
arg[1] = rt_malloc(len + 1);
if (arg[1] == NULL ){
rt_kprintf("malloc failed at file: %s,line: %d", __FILE__, __LINE__);
return;
}
rt_memset(arg[1], 0, len+1);
strncpy(arg[1], argv[1], len);
}else{
arg[1] = NULL;
}
arg[2] = NULL;
/* Run lua interpreter in separate thread */
lua_thread = rt_thread_create("lua_msh",
(void (*)(void *))(finsh_lua),
(void*)parameters,
10240,
rt_thread_self()->current_priority - 1,
20);
if (lua_thread != RT_NULL)
{
rt_thread_startup(lua_thread);
}
return; return;
} }
MSH_CMD_EXPORT(lua_msh, lua in msh); MSH_CMD_EXPORT(lua_msh, lua in msh);

View File

@ -462,7 +462,26 @@ rt_int32_t rt_strcmp(const char *cs, const char *ct)
return (*cs - *ct); return (*cs - *ct);
} }
RTM_EXPORT(rt_strcmp); RTM_EXPORT(rt_strcmp);
/**
* The strnlen() function returns the number of characters in the
* string pointed to by s, excluding the terminating null byte ('\0'),
* but at most maxlen. In doing this, strnlen() looks only at the
* first maxlen characters in the string pointed to by s and never
* beyond s+maxlen.
*
* @param s the string
* @param maxlen the max size
* @return the length of string
*/
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
{
const char *sc;
for (sc = s; *sc != '\0' && sc - s < maxlen; ++sc) /* nothing */
;
return sc - s;
}
/** /**
* This function will return the length of a string, which terminate will * This function will return the length of a string, which terminate will
* null character. * null character.