Skip to content

Commit

Permalink
Version 0.2.0
Browse files Browse the repository at this point in the history
    1. 增加cmd register mode
    2. 调整demo

Signed-off-by: linyuzhou <[email protected]>
  • Loading branch information
linyuzhou committed Apr 23, 2024
1 parent 0c7f808 commit 8d0c340
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(LiteShell)

set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_BUILD_TYPE Release)
# set(CMAKE_BUILD_TYPE Debug)
Expand Down Expand Up @@ -30,6 +30,7 @@ set(
${DIR_EXAMPLES_SRCS}
)

link_libraries(pthread)
# include path
include_directories(LiteShell examples examples/logger)

Expand Down
64 changes: 55 additions & 9 deletions LiteShell/lite_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
#include <string.h>

/*============================ GLOBAL VARIABLES =============================*/
#if (0 == CONFIG_SHELL_USE_REGISTER_MODE)
extern const lsh_tbl_t shell_command_list[];
#endif
static SHELL_MONITOR_ST shell = {
.state = SHELL_UNINIT,
};

/*========================== FUNCTION PROTOTYPES ============================*/
static void (*(shell_find_callback(const char *cmd_name)))(int argc, char *argv[]);
static void (*(shell_find_callback(const char *cmd_name)))(int argc,
char *argv[]);

/*******************************************************************************
* @brief lite shell main processing function
Expand All @@ -51,15 +55,18 @@ void shell_main_task(void)
memset(shell.argsbuf, 0, sizeof(shell.argsbuf));
for (i = 0; (0 != shell.curr_line.data[i]) && (i < data_len); i++)
{
if ((' ' == shell.curr_line.data[i]) || ('\t' == shell.curr_line.data[i]) ||
if ((' ' == shell.curr_line.data[i]) ||
('\t' == shell.curr_line.data[i]) ||
(',' == shell.curr_line.data[i]))
{
buf_idx = 0;
param_cnt++;
}
else if (('\n' == shell.curr_line.data[i] || '\r' == shell.curr_line.data[i]))
else if (('\n' == shell.curr_line.data[i] ||
'\r' == shell.curr_line.data[i]))
{
if (((i + 1) < data_len) && ('\0' != shell.curr_line.data[i + 1]))
if (((i + 1) < data_len) &&
('\0' != shell.curr_line.data[i + 1]))
{
if (('\n' == shell.curr_line.data[i + 1] ||
'\r' == shell.curr_line.data[i + 1]))
Expand All @@ -71,7 +78,8 @@ void shell_main_task(void)
else
{
if (CONFIG_SHELL_MAX_ARG_LEN > buf_idx)
shell.argsbuf[param_cnt][buf_idx++] = shell.curr_line.data[i];
shell.argsbuf[param_cnt][buf_idx++] =
shell.curr_line.data[i];
}

if (CONFIG_SHELL_MAX_ARGS_NUM <= param_cnt)
Expand Down Expand Up @@ -135,7 +143,8 @@ void shell_init(void)
{
uint32_t i = 0;

for (i = 0; i < CONFIG_SHELL_MAX_ARGS_NUM; i++) shell.argv[i] = shell.argsbuf[i];
for (i = 0; i < CONFIG_SHELL_MAX_ARGS_NUM; i++)
shell.argv[i] = shell.argsbuf[i];
shell.state = SHELL_INIT_OK;
}

Expand All @@ -148,10 +157,11 @@ void shell_init(void)
******************************************************************************/
void shell_help(int argc, char *argv[])
{
uint32_t i = 0;
(void)argc;
(void)argv;
uint32_t i = 0;

#if (0 == CONFIG_SHELL_USE_REGISTER_MODE)
SHELL_DISPLAY("LiteShell " LITE_SHELL_VERSION " [email protected] \r\n");
while ((void *)0 != shell_command_list[i].name)
{
Expand All @@ -162,6 +172,21 @@ void shell_help(int argc, char *argv[])
: shell_command_list[i].description));
i++;
}
#else
uint32_t cmd_items = ((uint32_t)(CONFIG_SHELL_CMD_TBL_END_ADDR) -
(uint32_t)(CONFIG_SHELL_CMD_TBL_START_ADDR)) /
sizeof(lsh_tbl_t);
const lsh_tbl_t *shell_command_list =
(lsh_tbl_t *)(CONFIG_SHELL_CMD_TBL_END_ADDR);
for (i = 0; i < cmd_items; i++)
{
SHELL_DISPLAY("%-10s -- %s \r\n",
shell_command_list[i].name,
((void *)0 == shell_command_list[i].description
? shell_command_list[i].name
: shell_command_list[i].description));
}
#endif
}

/******************************************************************************
Expand All @@ -171,20 +196,41 @@ void shell_help(int argc, char *argv[])
*
* @return p_func: a function pointer
******************************************************************************/
static void (*(shell_find_callback(const char *cmd_name)))(int argc, char *argv[])
static void (*(shell_find_callback(const char *cmd_name)))(int argc,
char *argv[])
{
uint32_t i = 0;
void (*p_func)(int argc, char *argv[]) = (void *)0;

#if (0 == CONFIG_SHELL_USE_REGISTER_MODE)
while ((void *)0 != shell_command_list[i].name)
{
if (0 == strncmp(cmd_name, shell_command_list[i].name, CONFIG_SHELL_MAX_ARG_LEN))
if (0 == strncmp(cmd_name,
shell_command_list[i].name,
CONFIG_SHELL_MAX_ARG_LEN))
{
p_func = shell_command_list[i].func;
break;
}
i++;
}
#else
uint32_t cmd_items = ((uint32_t)(CONFIG_SHELL_CMD_TBL_END_ADDR) -
(uint32_t)(CONFIG_SHELL_CMD_TBL_START_ADDR)) /
sizeof(lsh_tbl_t);
const lsh_tbl_t *shell_command_list =
(lsh_tbl_t *)(CONFIG_SHELL_CMD_TBL_END_ADDR);
for (i = 0; i < cmd_items; i++)
{
if (0 == strncmp(cmd_name,
shell_command_list[i].name,
CONFIG_SHELL_MAX_ARG_LEN))
{
p_func = shell_command_list[i].func;
break;
}
}
#endif

return p_func;
}
Expand Down
32 changes: 32 additions & 0 deletions LiteShell/lite_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ extern "C" {
/*============================= INCLUDE FILES ================================*/
#include <stdint.h>
#include <stddef.h>
#include "lite_shell_cfg.h"

/*=========================== STRUCTURE DEFINES =============================*/

typedef struct SHELL_CMD_TYPE_STRU
{
const char *name;
void (*func)(int argc, char *argv[]);
const char *description;
} SHELL_CMD_TYPE_ST, lsh_tbl_t;

/*============================= EXPORTED MACRO ===============================*/
#if defined(__TASKING__)
#define CMD_LINK_ENTRY_DECLARE(_section, _class, _type, _id) \
_type lsh##_##_id \
__attribute__((protect, section("." #_section "." #_class "." #_id)))
#elif (defined(__ghs__) || defined(__GNUC__))
#define CMD_LINK_ENTRY_DECLARE(_section, _class, _type, _id) \
_type lsh##_##_id \
__attribute__((section("." #_section "." #_class "." #_id)))
#else
#define CMD_LINK_ENTRY_DECLARE(_section, _class, _type, _id)
#endif

#define CMD_MKENT_COMPLETE(_cmd, _callback, _help_info) \
{ \
(char *)#_cmd, _callback, (char *)_help_info \
}

#define SHELL_CMD_REGISTER(_cmd, _callback, _help_info) \
CMD_LINK_ENTRY_DECLARE(rodata, lite_shell, lsh_tbl_t, _cmd) = \
CMD_MKENT_COMPLETE(_cmd, _callback, _help_info)

/*========================== FUNCTION PROTOTYPES =============================*/

Expand Down
29 changes: 18 additions & 11 deletions LiteShell/lite_shell_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern "C" {
#include <stdint.h>
#include "lite_shell_cfg.h"

/*========================= PERIPHERAL DECLARATION ==========================*/
#define LITE_SHELL_VERSION "0.2.0"

/*============================ TYPE DEFINITIONS =============================*/

/*!< the max bytes limit for a cmd line */
Expand All @@ -50,6 +53,21 @@ extern "C" {
#define CONFIG_SHELL_USER_NAME "SalyKid:"
#endif

/*!< lite shell cmd register mode */
#ifndef CONFIG_SHELL_USE_REGISTER_MODE
#define CONFIG_SHELL_USE_REGISTER_MODE 0
#endif

#if (0 != CONFIG_SHELL_USE_REGISTER_MODE)
#ifndef CONFIG_SHELL_CMD_TBL_START_ADDR
#define CONFIG_SHELL_CMD_TBL_START_ADDR 0
#endif

#ifndef CONFIG_SHELL_CMD_TBL_END_ADDR
#define CONFIG_SHELL_CMD_TBL_END_ADDR 0
#endif
#endif

/*!< lite shell public print */
#ifndef SHELL_LOG
#define SHELL_LOG(fmt, args...)
Expand All @@ -60,8 +78,6 @@ extern "C" {
#define SHELL_DISPLAY(fmt, args...)
#endif

#define LITE_SHELL_VERSION "0.1.1"

/*============================= ENUM DEFINES ================================*/

typedef enum SHELL_NUM_TYPE_ENUM
Expand All @@ -84,14 +100,6 @@ typedef enum SHELL_STATE_ENUM
} SHELL_STATE_EM;

/*=========================== STRUCTURE DEFINES =============================*/

typedef struct SHELL_CMD_TYPE_STRU
{
const char *name;
void (*func)(int argc, char *argv[]);
const char *description;
} SHELL_CMD_TYPE_ST;

typedef struct SHELL_MONITOR_STRU
{
SHELL_STATE_EM state;
Expand All @@ -105,7 +113,6 @@ typedef struct SHELL_MONITOR_STRU
} SHELL_MONITOR_ST;

/*============================ GLOBAL VARIABLES =============================*/
extern const SHELL_CMD_TYPE_ST shell_command_list[];

#ifdef __cplusplus
}
Expand Down
12 changes: 11 additions & 1 deletion examples/lite_shell_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@

/*============================ GLOBAL VARIABLES =============================*/

#if (1 != CONFIG_SHELL_USE_REGISTER_MODE)
static void shell_test_str(int argc, char *argv[]);
static void shell_test_ch(int argc, char *argv[]);
static void shell_test_num(int argc, char *argv[]);
static void shell_test_float(int argc, char *argv[]);

const SHELL_CMD_TYPE_ST shell_command_list[] = {
const lsh_tbl_t shell_command_list[] = {
{"getstr", shell_test_str, "get a string and print it"},
{"getch", shell_test_ch, "get a char and print it"},
{"getval", shell_test_num, "get a num and print it"},
{"getfloat", shell_test_float, "get a float num and print it"},
{"help", shell_help, "Print a list of all commands."},
{(void *)0, (void *)0, (void *)0},
};
#endif

/*========================== FUNCTION PROTOTYPES ============================*/

Expand Down Expand Up @@ -98,3 +100,11 @@ static void shell_test_float(int argc, char *argv[])
num.int_val = shell_arg_parse(argv[1]);
SHELL_LOG("shell get float { %f } \r\n", num.flt_val);
}

#if (1 == CONFIG_SHELL_USE_REGISTER_MODE)
SHELL_CMD_REGISTER(getstr, shell_test_str, "get a string and print it");
SHELL_CMD_REGISTER(getch, shell_test_ch, "get a char and print it");
SHELL_CMD_REGISTER(getval, shell_test_num, "get a num and print it");
SHELL_CMD_REGISTER(getfloat, shell_test_float, "get a float num and print it");
SHELL_CMD_REGISTER(help, shell_help, "Print a list of all commands.");
#endif
25 changes: 19 additions & 6 deletions examples/lite_shell_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ extern "C" {
#include "logger.h"

/*========================= PERIPHERAL DECLARATION ==========================*/
#define CONFIG_SHELL_MAX_LINE_LEN 128 /*!< the max bytes limit for a cmd line */
#define CONFIG_SHELL_MAX_ARG_LEN 8 /*!< the max byte limit for a arg */
#define CONFIG_SHELL_MAX_ARGS_NUM 5 /*!< the max num of args supported */
#define CONFIG_SHELL_USER_NAME "SaltyKid:" /*!< lite shell user name */
/*!< the max bytes limit for a cmd line */
#define CONFIG_SHELL_MAX_LINE_LEN 128
/*!< the max byte limit for a arg */
#define CONFIG_SHELL_MAX_ARG_LEN 32
/*!< the max num of args supported */
#define CONFIG_SHELL_MAX_ARGS_NUM 5
/*!< lite shell user name */
#define CONFIG_SHELL_USER_NAME "SaltyKid:"
/*!< lite shell cmd register mode */
#define CONFIG_SHELL_USE_REGISTER_MODE 0
/*!< lite shell cmd list table start addr */
#define CONFIG_SHELL_CMD_TBL_START_ADDR 0
/*!< lite shell cmd list table end addr */
#define CONFIG_SHELL_CMD_TBL_END_ADDR 0

/*============================= EXPORTED MACRO ==============================*/
#define SHELL_LOG(fmt, args...) LOG("[sh]: " fmt, ##args) /*!< lite shell public print */
#define SHELL_DISPLAY(fmt, args...) LOG(fmt, ##args) /*!< lite shell private print */

/*!< lite shell public print */
#define SHELL_LOG(fmt, args...) LOG("[sh]: " fmt, ##args)
/*!< lite shell private print */
#define SHELL_DISPLAY(fmt, args...) LOG(fmt, ##args)

#ifdef __cplusplus
}
Expand Down
9 changes: 6 additions & 3 deletions examples/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ extern "C" {
#endif

/*============================= EXPORTED MACRO ===============================*/
#define LOG(...) printf(__VA_ARGS__)
#define LOG(...) \
do \
{ \
printf(__VA_ARGS__); \
fflush(stdout); \
} while (0);

// #define LOG_I(fmt, a...) LOG("%8s %4d %-8s (I) " fmt, FILE_NAME(__FILE__), __LINE__,
// __FUNCTION__, ##a)
#define LOG_I(fmt, a...) LOG("I " fmt, ##a)
#define LOG_D(fmt, a...) LOG("D " fmt, ##a)
#define LOG_W(fmt, a...) LOG("W " fmt, ##a)
Expand Down
Loading

0 comments on commit 8d0c340

Please sign in to comment.