amiro-os / core / inc / aos_shell.h @ 88449987
History | View | Annotate | Download (11.205 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 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 | 2dd2e257 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) |
33 | 3940ba8a | Thomas Schöpping | |
34 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
35 | /* CONSTANTS */
|
||
36 | /******************************************************************************/
|
||
37 | e545e620 | Thomas Schöpping | |
38 | /**
|
||
39 | * @brief Shell event flag that is emitted when the thread starts.
|
||
40 | */
|
||
41 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_START ((eventflags_t)(1 << 0)) |
42 | e545e620 | Thomas Schöpping | |
43 | /**
|
||
44 | * @brief Shell event flag that is emitted when a command is executed.
|
||
45 | */
|
||
46 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_EXEC ((eventflags_t)(1 << 1)) |
47 | e545e620 | Thomas Schöpping | |
48 | /**
|
||
49 | * @brief Shell event flag that is emitted when a command execution finished.
|
||
50 | */
|
||
51 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_DONE ((eventflags_t)(1 << 2)) |
52 | e545e620 | Thomas Schöpping | |
53 | /**
|
||
54 | * @brief Shell event flag that is emitted when the shread stops.
|
||
55 | */
|
||
56 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_EXIT ((eventflags_t)(1 << 3)) |
57 | e545e620 | Thomas Schöpping | |
58 | /**
|
||
59 | * @brief Shell event flag that is emitted when an I/O error occurred.
|
||
60 | */
|
||
61 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_EVTFLAG_IOERROR ((eventflags_t)(1 << 4)) |
62 | e545e620 | Thomas Schöpping | |
63 | /**
|
||
64 | * @brief Shell input configuration for replacing content by user input.
|
||
65 | */
|
||
66 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_CONFIG_INPUT_OVERWRITE (1 << 0) |
67 | e545e620 | Thomas Schöpping | |
68 | /**
|
||
69 | * @brief Shell prompt configuration print a minimalistic prompt.
|
||
70 | */
|
||
71 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_CONFIG_PROMPT_MINIMAL (1 << 1) |
72 | e545e620 | Thomas Schöpping | |
73 | /**
|
||
74 | * @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
||
75 | */
|
||
76 | ba516b61 | Thomas Schöpping | #define AOS_SHELL_CONFIG_PROMPT_UPTIME (1 << 2) |
77 | e545e620 | Thomas Schöpping | |
78 | /**
|
||
79 | 8399aeae | Thomas Schöpping | * @brief Shell prompt configuration to additionally print the date and time with the prompt.
|
80 | */
|
||
81 | #define AOS_SHELL_CONFIG_PROMPT_DATETIME (2 << 2) |
||
82 | |||
83 | /**
|
||
84 | e545e620 | Thomas Schöpping | * @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
85 | */
|
||
86 | 8399aeae | Thomas Schöpping | #define AOS_SHELL_CONFIG_MATCH_CASE (1 << 4) |
87 | ba516b61 | Thomas Schöpping | |
88 | /**
|
||
89 | * @brief Shell I/O channel flag whether the channel is attached to a list.
|
||
90 | */
|
||
91 | #define AOS_SHELLCHANNEL_ATTACHED (1 << 0) |
||
92 | |||
93 | /**
|
||
94 | * @brief Shell I/O channel flag whether the channel is enabled as input.
|
||
95 | */
|
||
96 | #define AOS_SHELLCHANNEL_INPUT_ENABLED (1 << 1) |
||
97 | |||
98 | /**
|
||
99 | * @brief Shell I/O channel flag whether the channel is enabled as output.
|
||
100 | */
|
||
101 | #define AOS_SHELLCHANNEL_OUTPUT_ENABLED (1 << 2) |
||
102 | |||
103 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
104 | /* SETTINGS */
|
||
105 | /******************************************************************************/
|
||
106 | |||
107 | /******************************************************************************/
|
||
108 | /* CHECKS */
|
||
109 | /******************************************************************************/
|
||
110 | |||
111 | /******************************************************************************/
|
||
112 | /* DATA STRUCTURES AND TYPES */
|
||
113 | /******************************************************************************/
|
||
114 | ba516b61 | Thomas Schöpping | |
115 | /**
|
||
116 | * @brief AosShellChannel specific methods.
|
||
117 | */
|
||
118 | #define _aos_shell_channel_methods \
|
||
119 | _base_asynchronous_channel_methods |
||
120 | |||
121 | /**
|
||
122 | * @brief AosShellChannel specific data.
|
||
123 | */
|
||
124 | #define _aos_shell_channel_data \
|
||
125 | dd8738ea | Thomas Schöpping | /* pointer to a BaseAsynchronousChannel object */ \
|
126 | BaseAsynchronousChannel* asyncchannel; \ |
||
127 | ba516b61 | Thomas Schöpping | /* event listener for the associated BaseAsynchronousChannel */ \
|
128 | event_listener_t listener; \ |
||
129 | /* pointer to the next chennal in a AosShellStream */ \
|
||
130 | f3ac1c96 | Thomas Schöpping | struct aos_shellchannel* next; \
|
131 | ba516b61 | Thomas Schöpping | /* flags related to the channel */ \
|
132 | uint8_t flags; |
||
133 | |||
134 | /**
|
||
135 | * @extends BaseAsynchronousChannelVMT
|
||
136 | *
|
||
137 | * @brief AosShellChannel virtual methods table.
|
||
138 | */
|
||
139 | struct AosShellChannelVMT {
|
||
140 | _aos_shell_channel_methods |
||
141 | }; |
||
142 | |||
143 | /**
|
||
144 | * @extends BaseAsynchronousChannel
|
||
145 | *
|
||
146 | * @brief Shell channel class.
|
||
147 | * @details This class implements an asynchronous I/O channel.
|
||
148 | */
|
||
149 | f3ac1c96 | Thomas Schöpping | typedef struct aos_shellchannel { |
150 | ba516b61 | Thomas Schöpping | /** @brief Virtual Methods Table. */
|
151 | const struct AosShellChannelVMT* vmt; |
||
152 | _aos_shell_channel_data |
||
153 | f3ac1c96 | Thomas Schöpping | } AosShellChannel; |
154 | ba516b61 | Thomas Schöpping | |
155 | /**
|
||
156 | * @brief AosShellStream methods.
|
||
157 | */
|
||
158 | #define _aos_shellstream_methods \
|
||
159 | _base_sequential_stream_methods |
||
160 | |||
161 | /**
|
||
162 | * @brief AosShellStream data.
|
||
163 | */
|
||
164 | #define _aos_shellstream_data \
|
||
165 | /* Pointer to the first channel in a list. */ \
|
||
166 | AosShellChannel* channel; |
||
167 | |||
168 | /**
|
||
169 | * @extends BaseSequentialStream
|
||
170 | *
|
||
171 | * @brief AosShellStream virtual methods table.
|
||
172 | */
|
||
173 | struct AosShellStreamVMT {
|
||
174 | _aos_shellstream_methods |
||
175 | }; |
||
176 | |||
177 | /**
|
||
178 | * @extends BaseSequentialStream
|
||
179 | *
|
||
180 | * @brief Shell Stream class.
|
||
181 | * @details This class implements an base sequential stream.
|
||
182 | * @todo So far only output but no input is supported.
|
||
183 | */
|
||
184 | f3ac1c96 | Thomas Schöpping | typedef struct aos_shellstream { |
185 | ba516b61 | Thomas Schöpping | const struct AosShellStreamVMT* vmt; |
186 | _aos_shellstream_data |
||
187 | f3ac1c96 | Thomas Schöpping | } AosShellStream; |
188 | e545e620 | Thomas Schöpping | |
189 | /**
|
||
190 | * @brief Shell command calback type.
|
||
191 | */
|
||
192 | typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]); |
||
193 | |||
194 | /**
|
||
195 | * @brief Shell command structure.
|
||
196 | */
|
||
197 | typedef struct aos_shellcommand { |
||
198 | /**
|
||
199 | * @brief Command name.
|
||
200 | */
|
||
201 | const char* name; |
||
202 | |||
203 | /**
|
||
204 | * @brief Callback function.
|
||
205 | */
|
||
206 | aos_shellcmdcb_t callback; |
||
207 | |||
208 | /**
|
||
209 | * @brief Pointer to next command in a singly linked list.
|
||
210 | */
|
||
211 | struct aos_shellcommand* next;
|
||
212 | ba516b61 | Thomas Schöpping | |
213 | e545e620 | Thomas Schöpping | } aos_shellcommand_t; |
214 | |||
215 | /**
|
||
216 | * @brief Execution status of a shell command.
|
||
217 | */
|
||
218 | typedef struct aos_shellexecstatus { |
||
219 | ba516b61 | Thomas Schöpping | /**
|
220 | * @brief Pointer to the command that was executed.
|
||
221 | */
|
||
222 | aos_shellcommand_t* command; |
||
223 | |||
224 | /**
|
||
225 | * @brief Return value of the executed command.
|
||
226 | */
|
||
227 | int retval;
|
||
228 | e545e620 | Thomas Schöpping | } aos_shellexecstatus_t; |
229 | |||
230 | /**
|
||
231 | ba516b61 | Thomas Schöpping | * @brief Enumerator to encode shell actions.
|
232 | */
|
||
233 | typedef enum aos_shellaction { |
||
234 | dd8738ea | Thomas Schöpping | AOS_SHELL_ACTION_NONE, |
235 | ba516b61 | Thomas Schöpping | AOS_SHELL_ACTION_READCHAR, |
236 | AOS_SHELL_ACTION_AUTOFILL, |
||
237 | AOS_SHELL_ACTION_SUGGEST, |
||
238 | AOS_SHELL_ACTION_INSERTTOGGLE, |
||
239 | AOS_SHELL_ACTION_DELETEFORWARD, |
||
240 | AOS_SHELL_ACTION_DELETEBACKWARD, |
||
241 | AOS_SHELL_ACTION_RECALLLAST, |
||
242 | AOS_SHELL_ACTION_CLEAR, |
||
243 | AOS_SHELL_ACTION_CURSOR2START, |
||
244 | AOS_SHELL_ACTION_CURSOR2END, |
||
245 | AOS_SHELL_ACTION_CURSORLEFT, |
||
246 | AOS_SHELL_ACTION_CURSORRIGHT, |
||
247 | AOS_SHELL_ACTION_EXECUTE, |
||
248 | AOS_SHELL_ACTION_ESCSTART, |
||
249 | } aos_shellaction_t; |
||
250 | |||
251 | /**
|
||
252 | e545e620 | Thomas Schöpping | * @brief Shell structure.
|
253 | */
|
||
254 | typedef struct aos_shell { |
||
255 | /**
|
||
256 | * @brief Pointer to the thread object.
|
||
257 | */
|
||
258 | thread_t* thread; |
||
259 | |||
260 | /**
|
||
261 | * @brief Event source.
|
||
262 | */
|
||
263 | event_source_t eventSource; |
||
264 | |||
265 | /**
|
||
266 | ba516b61 | Thomas Schöpping | * @brief Struct for OS related events
|
267 | e545e620 | Thomas Schöpping | */
|
268 | ba516b61 | Thomas Schöpping | struct {
|
269 | /**
|
||
270 | * @brief Pointer to the OS' event source.
|
||
271 | */
|
||
272 | event_source_t* eventSource; |
||
273 | |||
274 | /**
|
||
275 | * @brief Listener for OS related events.
|
||
276 | */
|
||
277 | event_listener_t eventListener; |
||
278 | } os; |
||
279 | |||
280 | /**
|
||
281 | * @brief Pointer to the first I/O channel.
|
||
282 | */
|
||
283 | AosShellStream stream; |
||
284 | e545e620 | Thomas Schöpping | |
285 | /**
|
||
286 | * @brief String to printed as prompt.
|
||
287 | */
|
||
288 | const char* prompt; |
||
289 | |||
290 | /**
|
||
291 | * @brief Pointer to the first element of the singly linked list of commands.
|
||
292 | * @details Commands are ordered alphabetically in the list.
|
||
293 | */
|
||
294 | aos_shellcommand_t* commands; |
||
295 | |||
296 | /**
|
||
297 | * @brief Execution status of the most recent command.
|
||
298 | */
|
||
299 | aos_shellexecstatus_t execstatus; |
||
300 | |||
301 | /**
|
||
302 | * @brief Input buffer.
|
||
303 | */
|
||
304 | char* line;
|
||
305 | |||
306 | /**
|
||
307 | * @brief Size of the input buffer.
|
||
308 | */
|
||
309 | size_t linesize; |
||
310 | |||
311 | /**
|
||
312 | ba516b61 | Thomas Schöpping | * @brief Structure containing data for internal input parsing.
|
313 | */
|
||
314 | struct {
|
||
315 | /**
|
||
316 | * @brief The last action executed by the shell.
|
||
317 | */
|
||
318 | aos_shellaction_t lastaction; |
||
319 | |||
320 | /**
|
||
321 | * @brief Number of character in the current escape sequence.
|
||
322 | */
|
||
323 | uint8_t escp; |
||
324 | |||
325 | /**
|
||
326 | * @brief Buffer to store an escape sequence.
|
||
327 | */
|
||
328 | char escseq[5]; |
||
329 | |||
330 | /**
|
||
331 | * @brief Current curso position.
|
||
332 | */
|
||
333 | size_t cursorpos; |
||
334 | |||
335 | /**
|
||
336 | * @brief Current line width.
|
||
337 | */
|
||
338 | size_t lineend; |
||
339 | |||
340 | /**
|
||
341 | * @brief Flag whether there was input since the prompt was printed the last time.
|
||
342 | */
|
||
343 | bool noinput;
|
||
344 | } inputdata; |
||
345 | |||
346 | /**
|
||
347 | e545e620 | Thomas Schöpping | * @brief Argument buffer.
|
348 | */
|
||
349 | char** arglist;
|
||
350 | |||
351 | /**
|
||
352 | * @brief Size of the argument buffer.
|
||
353 | */
|
||
354 | size_t arglistsize; |
||
355 | |||
356 | /**
|
||
357 | * @brief Configuration flags.
|
||
358 | */
|
||
359 | uint8_t config; |
||
360 | } aos_shell_t; |
||
361 | |||
362 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
363 | /* MACROS */
|
||
364 | /******************************************************************************/
|
||
365 | |||
366 | /******************************************************************************/
|
||
367 | /* EXTERN DECLARATIONS */
|
||
368 | /******************************************************************************/
|
||
369 | |||
370 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
371 | e545e620 | Thomas Schöpping | extern "C" { |
372 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
373 | ba516b61 | Thomas Schöpping | void aosShellInit(aos_shell_t* shell, event_source_t* oseventsource, const char* prompt, char* line, size_t linesize, char** arglist, size_t arglistsize); |
374 | void aosShellStreamInit(AosShellStream* stream);
|
||
375 | dd8738ea | Thomas Schöpping | void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
|
376 | e545e620 | Thomas Schöpping | aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd); |
377 | aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
|
||
378 | aed3754b | Thomas Schöpping | unsigned int aosShellCountCommands(aos_shell_t* shell); |
379 | ba516b61 | Thomas Schöpping | void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
|
380 | aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel); |
||
381 | void aosShellChannelInputEnable(AosShellChannel* channel);
|
||
382 | void aosShellChannelInputDisable( AosShellChannel* channel);
|
||
383 | void aosShellChannelOutputEnable(AosShellChannel* channel);
|
||
384 | void aosShellChannelOutputDisable(AosShellChannel* channel);
|
||
385 | e545e620 | Thomas Schöpping | THD_FUNCTION(aosShellThread, shell); |
386 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
387 | e545e620 | Thomas Schöpping | } |
388 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
389 | e545e620 | Thomas Schöpping | |
390 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
391 | /* INLINE FUNCTIONS */
|
||
392 | /******************************************************************************/
|
||
393 | |||
394 | 2dd2e257 | Thomas Schöpping | #endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */ |
395 | ba516b61 | Thomas Schöpping | |
396 | 6ff06bbf | Thomas Schöpping | #endif /* AMIROOS_SHELL_H */ |
397 | 53710ca3 | Marc Rothmann | |
398 | /** @} */ |