amiro-os / core / inc / aos_shell.h @ ab5cad1b
History | View | Annotate | Download (10.73 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 96621a83 | Thomas Schöpping | Copyright (C) 2016..2020 Thomas Schöpping et al.
|
4 | e545e620 | Thomas Schöpping | |
5 | This program is free software: you can redistribute it and/or modify
|
||
6 | it under the terms of the GNU General Public License as published by
|
||
7 | the Free Software Foundation, either version 3 of the License, or
|
||
8 | (at your option) any later version.
|
||
9 | |||
10 | This program is distributed in the hope that it will be useful,
|
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | GNU General Public License for more details.
|
||
14 | |||
15 | You should have received a copy of the GNU General Public License
|
||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
17 | */
|
||
18 | |||
19 | 53710ca3 | Marc Rothmann | /**
|
20 | * @file aos_shell.h
|
||
21 | * @brief Shell macros and structures.
|
||
22 | *
|
||
23 | * @addtogroup aos_shell
|
||
24 | * @{
|
||
25 | */
|
||
26 | |||
27 | 6ff06bbf | Thomas Schöpping | #ifndef AMIROOS_SHELL_H
|
28 | #define AMIROOS_SHELL_H
|
||
29 | e545e620 | Thomas Schöpping | |
30 | 3940ba8a | Thomas Schöpping | #include <amiroos.h> |
31 | |||
32 | cda14729 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
33 | 3940ba8a | Thomas Schöpping | |
34 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
35 | /* CONSTANTS */
|
||
36 | /******************************************************************************/
|
||
37 | e545e620 | Thomas Schöpping | |
38 | ab5cad1b | Thomas Schöpping | |
39 | cc33217b | Thomas Schöpping | |
40 | /**
|
||
41 | e545e620 | Thomas Schöpping | * @brief Shell event flag that is emitted when the thread starts.
|
42 | */
|
||
43 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_START ((eventflags_t)(1 << 0)) |
44 | e545e620 | Thomas Schöpping | |
45 | /**
|
||
46 | * @brief Shell event flag that is emitted when a command is executed.
|
||
47 | */
|
||
48 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_EXECUTE ((eventflags_t)(1 << 1)) |
49 | e545e620 | Thomas Schöpping | |
50 | /**
|
||
51 | * @brief Shell event flag that is emitted when a command execution finished.
|
||
52 | */
|
||
53 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_DONE ((eventflags_t)(1 << 2)) |
54 | e545e620 | Thomas Schöpping | |
55 | /**
|
||
56 | * @brief Shell event flag that is emitted when the shread stops.
|
||
57 | */
|
||
58 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_EXIT ((eventflags_t)(1 << 3)) |
59 | e545e620 | Thomas Schöpping | |
60 | /**
|
||
61 | * @brief Shell event flag that is emitted when an I/O error occurred.
|
||
62 | */
|
||
63 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_IOERROR ((eventflags_t)(1 << 4)) |
64 | e545e620 | Thomas Schöpping | |
65 | /**
|
||
66 | * @brief Shell input configuration for replacing content by user input.
|
||
67 | */
|
||
68 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_CONFIG_INPUT_OVERWRITE (1 << 0) |
69 | e545e620 | Thomas Schöpping | |
70 | /**
|
||
71 | * @brief Shell prompt configuration print a minimalistic prompt.
|
||
72 | */
|
||
73 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_CONFIG_PROMPT_MINIMAL (1 << 1) |
74 | e545e620 | Thomas Schöpping | |
75 | /**
|
||
76 | * @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
||
77 | */
|
||
78 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_CONFIG_PROMPT_UPTIME (1 << 2) |
79 | e545e620 | Thomas Schöpping | |
80 | /**
|
||
81 | 8399aeae | Thomas Schöpping | * @brief Shell prompt configuration to additionally print the date and time with the prompt.
|
82 | */
|
||
83 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_CONFIG_PROMPT_DATETIME (2 << 2) |
84 | 8399aeae | Thomas Schöpping | |
85 | /**
|
||
86 | e545e620 | Thomas Schöpping | * @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
87 | */
|
||
88 | ab5cad1b | Thomas Schöpping | #define AOS_SHELL_CONFIG_MATCH_CASE (1 << 4) |
89 | ba516b61 | Thomas Schöpping | |
90 | /**
|
||
91 | * @brief Shell I/O channel flag whether the channel is attached to a list.
|
||
92 | */
|
||
93 | ab5cad1b | Thomas Schöpping | #define AOS_SHELLCHANNEL_ATTACHED (1 << 0) |
94 | ba516b61 | Thomas Schöpping | |
95 | /**
|
||
96 | * @brief Shell I/O channel flag whether the channel is enabled as input.
|
||
97 | */
|
||
98 | ab5cad1b | Thomas Schöpping | #define AOS_SHELLCHANNEL_INPUT_ENABLED (1 << 1) |
99 | ba516b61 | Thomas Schöpping | |
100 | /**
|
||
101 | * @brief Shell I/O channel flag whether the channel is enabled as output.
|
||
102 | */
|
||
103 | ab5cad1b | Thomas Schöpping | #define AOS_SHELLCHANNEL_OUTPUT_ENABLED (1 << 2) |
104 | ba516b61 | Thomas Schöpping | |
105 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
106 | /* SETTINGS */
|
||
107 | /******************************************************************************/
|
||
108 | |||
109 | /******************************************************************************/
|
||
110 | /* CHECKS */
|
||
111 | /******************************************************************************/
|
||
112 | |||
113 | /******************************************************************************/
|
||
114 | /* DATA STRUCTURES AND TYPES */
|
||
115 | /******************************************************************************/
|
||
116 | ba516b61 | Thomas Schöpping | |
117 | /**
|
||
118 | * @brief AosShellChannel specific methods.
|
||
119 | */
|
||
120 | #define _aos_shell_channel_methods \
|
||
121 | _base_asynchronous_channel_methods |
||
122 | |||
123 | /**
|
||
124 | * @brief AosShellChannel specific data.
|
||
125 | */
|
||
126 | #define _aos_shell_channel_data \
|
||
127 | dd8738ea | Thomas Schöpping | /* pointer to a BaseAsynchronousChannel object */ \
|
128 | BaseAsynchronousChannel* asyncchannel; \ |
||
129 | ba516b61 | Thomas Schöpping | /* event listener for the associated BaseAsynchronousChannel */ \
|
130 | event_listener_t listener; \ |
||
131 | /* pointer to the next chennal in a AosShellStream */ \
|
||
132 | f3ac1c96 | Thomas Schöpping | struct aos_shellchannel* next; \
|
133 | ba516b61 | Thomas Schöpping | /* flags related to the channel */ \
|
134 | uint8_t flags; |
||
135 | |||
136 | /**
|
||
137 | * @extends BaseAsynchronousChannelVMT
|
||
138 | *
|
||
139 | * @brief AosShellChannel virtual methods table.
|
||
140 | */
|
||
141 | struct AosShellChannelVMT {
|
||
142 | _aos_shell_channel_methods |
||
143 | }; |
||
144 | |||
145 | /**
|
||
146 | * @extends BaseAsynchronousChannel
|
||
147 | *
|
||
148 | * @brief Shell channel class.
|
||
149 | * @details This class implements an asynchronous I/O channel.
|
||
150 | */
|
||
151 | f3ac1c96 | Thomas Schöpping | typedef struct aos_shellchannel { |
152 | ba516b61 | Thomas Schöpping | /** @brief Virtual Methods Table. */
|
153 | const struct AosShellChannelVMT* vmt; |
||
154 | _aos_shell_channel_data |
||
155 | f3ac1c96 | Thomas Schöpping | } AosShellChannel; |
156 | ba516b61 | Thomas Schöpping | |
157 | /**
|
||
158 | * @brief AosShellStream methods.
|
||
159 | */
|
||
160 | #define _aos_shellstream_methods \
|
||
161 | _base_sequential_stream_methods |
||
162 | |||
163 | /**
|
||
164 | * @brief AosShellStream data.
|
||
165 | */
|
||
166 | #define _aos_shellstream_data \
|
||
167 | /* Pointer to the first channel in a list. */ \
|
||
168 | AosShellChannel* channel; |
||
169 | |||
170 | /**
|
||
171 | * @extends BaseSequentialStream
|
||
172 | *
|
||
173 | * @brief AosShellStream virtual methods table.
|
||
174 | */
|
||
175 | struct AosShellStreamVMT {
|
||
176 | _aos_shellstream_methods |
||
177 | }; |
||
178 | |||
179 | /**
|
||
180 | * @extends BaseSequentialStream
|
||
181 | *
|
||
182 | * @brief Shell Stream class.
|
||
183 | * @details This class implements an base sequential stream.
|
||
184 | * @todo So far only output but no input is supported.
|
||
185 | */
|
||
186 | f3ac1c96 | Thomas Schöpping | typedef struct aos_shellstream { |
187 | ba516b61 | Thomas Schöpping | const struct AosShellStreamVMT* vmt; |
188 | _aos_shellstream_data |
||
189 | f3ac1c96 | Thomas Schöpping | } AosShellStream; |
190 | e545e620 | Thomas Schöpping | |
191 | /**
|
||
192 | * @brief Shell command calback type.
|
||
193 | 4c72a54c | Thomas Schöpping | *
|
194 | * @param[in] stream Stream to print to.
|
||
195 | * @param[in] argc Number of arguments.
|
||
196 | * @param[in] argv List of arguments.
|
||
197 | e545e620 | Thomas Schöpping | */
|
198 | typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]); |
||
199 | |||
200 | /**
|
||
201 | * @brief Shell command structure.
|
||
202 | */
|
||
203 | typedef struct aos_shellcommand { |
||
204 | /**
|
||
205 | * @brief Command name.
|
||
206 | */
|
||
207 | const char* name; |
||
208 | |||
209 | /**
|
||
210 | * @brief Callback function.
|
||
211 | */
|
||
212 | aos_shellcmdcb_t callback; |
||
213 | |||
214 | /**
|
||
215 | * @brief Pointer to next command in a singly linked list.
|
||
216 | */
|
||
217 | struct aos_shellcommand* next;
|
||
218 | ba516b61 | Thomas Schöpping | |
219 | e545e620 | Thomas Schöpping | } aos_shellcommand_t; |
220 | |||
221 | /**
|
||
222 | * @brief Execution status of a shell command.
|
||
223 | */
|
||
224 | typedef struct aos_shellexecstatus { |
||
225 | ba516b61 | Thomas Schöpping | /**
|
226 | * @brief Pointer to the command that was executed.
|
||
227 | */
|
||
228 | aos_shellcommand_t* command; |
||
229 | |||
230 | /**
|
||
231 | * @brief Return value of the executed command.
|
||
232 | */
|
||
233 | int retval;
|
||
234 | e545e620 | Thomas Schöpping | } aos_shellexecstatus_t; |
235 | |||
236 | /**
|
||
237 | * @brief Shell structure.
|
||
238 | */
|
||
239 | typedef struct aos_shell { |
||
240 | /**
|
||
241 | * @brief Pointer to the thread object.
|
||
242 | */
|
||
243 | thread_t* thread; |
||
244 | |||
245 | /**
|
||
246 | * @brief Event source.
|
||
247 | */
|
||
248 | event_source_t eventSource; |
||
249 | |||
250 | /**
|
||
251 | 697dba3c | Thomas Schöpping | * @brief Listener for OS related events.
|
252 | e545e620 | Thomas Schöpping | */
|
253 | 697dba3c | Thomas Schöpping | event_listener_t osEventListener; |
254 | ba516b61 | Thomas Schöpping | |
255 | /**
|
||
256 | * @brief Pointer to the first I/O channel.
|
||
257 | */
|
||
258 | AosShellStream stream; |
||
259 | e545e620 | Thomas Schöpping | |
260 | /**
|
||
261 | * @brief String to printed as prompt.
|
||
262 | */
|
||
263 | const char* prompt; |
||
264 | |||
265 | /**
|
||
266 | * @brief Pointer to the first element of the singly linked list of commands.
|
||
267 | * @details Commands are ordered alphabetically in the list.
|
||
268 | */
|
||
269 | aos_shellcommand_t* commands; |
||
270 | |||
271 | /**
|
||
272 | * @brief Execution status of the most recent command.
|
||
273 | */
|
||
274 | aos_shellexecstatus_t execstatus; |
||
275 | |||
276 | /**
|
||
277 | cc33217b | Thomas Schöpping | * @brief Structure containing all input data.
|
278 | e545e620 | Thomas Schöpping | */
|
279 | cc33217b | Thomas Schöpping | struct {
|
280 | /**
|
||
281 | * @brief Input buffer.
|
||
282 | 697dba3c | Thomas Schöpping | * @details This buffer is interpreted as two dimensional array.
|
283 | * It contains @p nentries elements of @p linewidth size each.
|
||
284 | cc33217b | Thomas Schöpping | */
|
285 | 697dba3c | Thomas Schöpping | char* buffer;
|
286 | ba516b61 | Thomas Schöpping | |
287 | /**
|
||
288 | 697dba3c | Thomas Schöpping | * @brief Number of entries in the buffer.
|
289 | * @note Must be >0.
|
||
290 | ba516b61 | Thomas Schöpping | */
|
291 | 697dba3c | Thomas Schöpping | size_t nentries; |
292 | ba516b61 | Thomas Schöpping | |
293 | /**
|
||
294 | 697dba3c | Thomas Schöpping | * @brief Width of each input line.
|
295 | * @brief Must be >0.
|
||
296 | ba516b61 | Thomas Schöpping | */
|
297 | 697dba3c | Thomas Schöpping | size_t linewidth; |
298 | ba516b61 | Thomas Schöpping | |
299 | /**
|
||
300 | 697dba3c | Thomas Schöpping | * @brief Size of the argument buffer.
|
301 | * @note Must be >0.
|
||
302 | ba516b61 | Thomas Schöpping | */
|
303 | 697dba3c | Thomas Schöpping | size_t nargs; |
304 | } input; |
||
305 | e545e620 | Thomas Schöpping | |
306 | /**
|
||
307 | * @brief Configuration flags.
|
||
308 | */
|
||
309 | uint8_t config; |
||
310 | } aos_shell_t; |
||
311 | |||
312 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
313 | /* MACROS */
|
||
314 | /******************************************************************************/
|
||
315 | |||
316 | 4c72a54c | Thomas Schöpping | /**
|
317 | * @brief Initializes a shell command object.
|
||
318 | *
|
||
319 | * @param[in] var Name of the object variable to be initialized.
|
||
320 | * @param[in] name Shell command name.
|
||
321 | * @param[in] callback Pointer to the callback function.
|
||
322 | */
|
||
323 | #define AOS_SHELL_COMMAND(var, name, callback) aos_shellcommand_t var = { \
|
||
324 | /* name */ name, \
|
||
325 | /* callback */ callback, \
|
||
326 | /* next */ NULL, \ |
||
327 | } |
||
328 | |||
329 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
330 | /* EXTERN DECLARATIONS */
|
||
331 | /******************************************************************************/
|
||
332 | |||
333 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
334 | e545e620 | Thomas Schöpping | extern "C" { |
335 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
336 | 697dba3c | Thomas Schöpping | void aosShellInit(aos_shell_t* shell, const char* prompt, char inbuf[], size_t entries, size_t linewidth, size_t numargs); |
337 | ba516b61 | Thomas Schöpping | void aosShellStreamInit(AosShellStream* stream);
|
338 | dd8738ea | Thomas Schöpping | void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
|
339 | e545e620 | Thomas Schöpping | aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd); |
340 | aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
|
||
341 | aed3754b | Thomas Schöpping | unsigned int aosShellCountCommands(aos_shell_t* shell); |
342 | ba516b61 | Thomas Schöpping | void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
|
343 | aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel); |
||
344 | void aosShellChannelInputEnable(AosShellChannel* channel);
|
||
345 | void aosShellChannelInputDisable( AosShellChannel* channel);
|
||
346 | void aosShellChannelOutputEnable(AosShellChannel* channel);
|
||
347 | void aosShellChannelOutputDisable(AosShellChannel* channel);
|
||
348 | e545e620 | Thomas Schöpping | THD_FUNCTION(aosShellThread, shell); |
349 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
350 | e545e620 | Thomas Schöpping | } |
351 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
352 | e545e620 | Thomas Schöpping | |
353 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
354 | /* INLINE FUNCTIONS */
|
||
355 | /******************************************************************************/
|
||
356 | |||
357 | cda14729 | Thomas Schöpping | #endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
358 | ba516b61 | Thomas Schöpping | |
359 | 6ff06bbf | Thomas Schöpping | #endif /* AMIROOS_SHELL_H */ |
360 | 53710ca3 | Marc Rothmann | |
361 | /** @} */ |