1 /**
2  * @file cli.h
3  * @copyright Copyright (C) 2015-2021 Alibaba Group Holding Limited
4  */
5 
6 #ifndef AOS_CLI_H
7 #define AOS_CLI_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /** @defgroup cli_aos_api cli
14  * @{
15  */
16 
17 /* This struct is used to define the cli cmd format */
18 typedef void (*cmd_fun_t)(char *outbuf, int len, int argc, char **argv);
19 
20 struct cli_command
21 {
22     const char *name;
23     const char *help;
24     cmd_fun_t   function;
25 };
26 
27 #define SECTION(x) __attribute__((section(x)))
28 #define USED __attribute__((used))
29 
30 typedef int (*cli_region_func)(int argc, char **argv);
31 
32 /* cli region table */
33 struct cli_region
34 {
35     const char *name;     /* the name of cli cmd*/
36     const char *desc;     /* description of cli cmd */
37     cli_region_func func; /* the cli function */
38 };
39 
40 #define ALIOS_CLI_CMD_REGISTER(name, cmd, desc)                        \
41     const char __clisym_##cmd##_name[] SECTION(".rodata") = #cmd;      \
42     const char __clisym_##cmd##_desc[] SECTION(".rodata") = #desc;     \
43     static void name##_stub(char *buf, int len, int argc, char **argv) \
44     {                                                                  \
45         name(argc, argv);                                              \
46     }                                                                  \
47     USED const struct cli_region __clisym_##cmd SECTION("CliRegion") = \
48         {                                                              \
49             __clisym_##cmd##_name,                                     \
50             __clisym_##cmd##_desc,                                     \
51             (cli_region_func)&name##_stub};
52 
53 
54 /**
55  * @brief Initialize the CLI module
56  *
57  * @return 0 on success, otherwise failed
58  *
59  */
60 int aos_cli_init(void);
61 
62 /**
63  * @brief This function register a command with the command-line interface
64  *
65  * @param[in] cmd the structure to regiter one CLI command
66  *
67  * @return 0 on success, otherwise failed
68  *
69  */
70 int aos_cli_register_command(const struct cli_command *cmd);
71 
72 /**
73  * @brief This function unregister a command from the command-line interface
74  *
75  * @param[in] cmd the structure to unregister one CLI command
76  *
77  * @return 0 on success, otherwise failed
78  *
79  */
80 int aos_cli_unregister_command(const struct cli_command *cmd);
81 
82 /**
83  * @brief This function register multi CLI commands
84  *
85  * @param[in] cmds pointer to an array of commands
86  * @param[in] num  number of commands in the array
87  *
88  * @return 0 on success, otherwise failed
89  *
90  */
91 int aos_cli_register_commands(const struct cli_command *cmds, int num);
92 
93 /**
94  * @brief This function unregisters multi CLI commands
95  *
96  * @param[in] cmds pointer to an array of commands
97  * @param[in] num  number of command in the array
98  *
99  * @return 0 on success, otherwise failed
100  *
101  */
102 int aos_cli_unregister_commands(const struct cli_command *cmds, int num);
103 
104 /**
105  * @brief use aos_cli_printf instead of printf in cli cmd
106  *
107  * @param[in] fmt pointer to a char * buffer
108  *
109  * @return 0 on success, otherwise failed
110  */
111 int aos_cli_printf(const char *fmt, ...);
112 
113 /**
114  * @brief Suspend cli task
115  *
116  * @return 0 on success, otherwise failed
117  *
118  */
119 int aos_cli_suspend(void);
120 
121 /**
122  * @brief Resume cli task
123  *
124  * @return 0 on success, otherwise failed
125  *
126  */
127 int aos_cli_resume(void);
128 
129 /**
130  * @}
131  */
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* AOS_CLI_H */
138 
139