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