diff --git a/bsp/imx6ull-artpi-smart/kcomponents/SConscript b/bsp/imx6ull-artpi-smart/kcomponents/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..c7ef7659ecea92b1dd9b71a97736a8552ee02551 --- /dev/null +++ b/bsp/imx6ull-artpi-smart/kcomponents/SConscript @@ -0,0 +1,14 @@ +# for module compiling +import os +from building import * + +cwd = GetCurrentDir() +objs = [] +list = os.listdir(cwd) + +for d in list: + path = os.path.join(cwd, d) + if os.path.isfile(os.path.join(path, 'SConscript')): + objs = objs + SConscript(os.path.join(d, 'SConscript')) + +Return('objs') diff --git a/bsp/imx6ull-artpi-smart/kcomponents/kshell/SConscript b/bsp/imx6ull-artpi-smart/kcomponents/kshell/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..cf97f0d35baa0a9bc391fa4dfca757d41986af89 --- /dev/null +++ b/bsp/imx6ull-artpi-smart/kcomponents/kshell/SConscript @@ -0,0 +1,14 @@ +Import('RTT_ROOT') +Import('rtconfig') +from building import * + +cwd = GetCurrentDir() +src = Split(''' + kshell.c +''') + +CPPPATH = [cwd] + +group = DefineGroup('Kshell', src, depend=[''], CPPPATH=CPPPATH) + +Return('group') diff --git a/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.c b/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.c new file mode 100644 index 0000000000000000000000000000000000000000..9be2c702612a7874ac99e7c2724e7e53efa99fd9 --- /dev/null +++ b/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.c @@ -0,0 +1,104 @@ + + +#include +#include +#include +#include "kshell.h" + +#define DBG_TAG "kernel_shell" +#define DBG_LVL DBG_INFO +#include + + +static rt_thread_t kshell_thread = RT_NULL; + +/* return ret value by replying channel message */ +static rt_err_t __kshell_ch_reply(int ch, rt_channel_msg_t msg, int val) +{ + struct rt_channel_msg msg_text = { + .sender = msg->sender, + .type = RT_CHANNEL_RAW, + .u.d = (void *)val + }; + return rt_channel_reply(ch, &msg_text); +} + +static void kshell_thread_entry(void *param) +{ + struct rt_channel_msg msg_text = { 0 }; + kshell_cmd_t msg_raw_data = RT_NULL; + size_t cmd_len = 0; + char *cmd = RT_NULL; + int shmid = -1; + int ch = -1; + + LOG_I("kshell_thread started."); + + /* create a channel to take requests from userspace */ + ch = rt_channel_open("kshell", O_CREAT); + if (ch < 0) + { + LOG_E("unable to create kshell channel."); + return; + } + + /* keep receiving requests from userspace and executing it */ + while (1) + { + LOG_D("waiting for request..."); + + if (rt_channel_recv(ch, &msg_text) != RT_EOK) + { + LOG_D("failed to get data from channel kshell"); + continue; + } + + shmid = (int)msg_text.u.d; + if (shmid < 0 || (msg_raw_data = lwp_shminfo(shmid)) == RT_NULL) + { + LOG_D("received an invalid shared-memory id."); + __kshell_ch_reply(ch, &msg_text, -1); + continue; + } + + cmd_len = msg_raw_data->len; + if (cmd_len > KSHELL_CMD_LENGTH_MAX) + { + LOG_D("command received are too long. max: %d", + KSHELL_CMD_LENGTH_MAX); + __kshell_ch_reply(ch, &msg_text, -1); + continue; + } + + /* reply first */ + __kshell_ch_reply(ch, &msg_text, 0); + LOG_D("executing command..."); + cmd = msg_raw_data->data; + msh_exec(cmd, cmd_len); + } +} + +int rt_kshell_init(void) +{ + kshell_thread = rt_thread_create("kshell", kshell_thread_entry, + RT_NULL, + KSHELL_THREAD_STACK_SIZE, + KSHELL_THREAD_PRIORITY, + KSHELL_THREAD_TICK); + if (kshell_thread == RT_NULL) + { + LOG_W("unable to create a new thread for kshell."); + return -RT_ERROR; + } + + if (rt_thread_startup(kshell_thread) != RT_EOK) + { + LOG_W("unable to start kshell_thread."); + rt_thread_delete(kshell_thread); + kshell_thread = RT_NULL; + return -RT_ERROR; + } + + return 0; +} +INIT_APP_EXPORT(rt_kshell_init); diff --git a/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.h b/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.h new file mode 100644 index 0000000000000000000000000000000000000000..750a2bdd24c641fcad61cc847fe1728ddc5cefb2 --- /dev/null +++ b/bsp/imx6ull-artpi-smart/kcomponents/kshell/kshell.h @@ -0,0 +1,23 @@ + +#ifndef __KSHELL_H__ +#define __KSHELL_H__ +#include + +/* Maximun length of command passing to kshell */ +#define KSHELL_CMD_LENGTH_MAX 80 + +/* + * Structure to deliver command to kshell. + */ +struct kshell_cmd_struct +{ + rt_uint32_t len; + char data[KSHELL_CMD_LENGTH_MAX + 1]; +}; +typedef struct kshell_cmd_struct *kshell_cmd_t; + +#define KSHELL_THREAD_STACK_SIZE 4096 +#define KSHELL_THREAD_PRIORITY 11 +#define KSHELL_THREAD_TICK 20 + +#endif /* __KSHELL_H__ */