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