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