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