amiro-os / core / inc / aos_shell.h @ 8516dad6
History | View | Annotate | Download (10.937 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2020 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 <amiroos.h> |
31 |
|
32 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
33 |
|
34 |
/******************************************************************************/
|
35 |
/* CONSTANTS */
|
36 |
/******************************************************************************/
|
37 |
|
38 |
/**
|
39 |
* @brief Shell event flag that is emitted when the thread starts.
|
40 |
*/
|
41 |
#define AOS_SHELL_EVTFLAG_START ((eventflags_t)(1 << 0)) |
42 |
|
43 |
/**
|
44 |
* @brief Shell event flag that is emitted when a command is executed.
|
45 |
*/
|
46 |
#define AOS_SHELL_EVTFLAG_EXECUTE ((eventflags_t)(1 << 1)) |
47 |
|
48 |
/**
|
49 |
* @brief Shell event flag that is emitted when a command execution finished.
|
50 |
*/
|
51 |
#define AOS_SHELL_EVTFLAG_DONE ((eventflags_t)(1 << 2)) |
52 |
|
53 |
/**
|
54 |
* @brief Shell event flag that is emitted when the shread stops.
|
55 |
*/
|
56 |
#define AOS_SHELL_EVTFLAG_EXIT ((eventflags_t)(1 << 3)) |
57 |
|
58 |
/**
|
59 |
* @brief Shell event flag that is emitted when an I/O error occurred.
|
60 |
*/
|
61 |
#define AOS_SHELL_EVTFLAG_IOERROR ((eventflags_t)(1 << 4)) |
62 |
|
63 |
/**
|
64 |
* @brief Shell input configuration for replacing content by user input.
|
65 |
*/
|
66 |
#define AOS_SHELL_CONFIG_INPUT_OVERWRITE (1 << 0) |
67 |
|
68 |
/**
|
69 |
* @brief Shell prompt configuration print a minimalistic prompt.
|
70 |
*/
|
71 |
#define AOS_SHELL_CONFIG_PROMPT_MINIMAL (1 << 1) |
72 |
|
73 |
/**
|
74 |
* @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
75 |
*/
|
76 |
#define AOS_SHELL_CONFIG_PROMPT_UPTIME (1 << 2) |
77 |
|
78 |
/**
|
79 |
* @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 |
* @brief Shell prompt configuration to additionally print the system uptime with the prompt.
|
85 |
*/
|
86 |
#define AOS_SHELL_CONFIG_MATCH_CASE (1 << 4) |
87 |
|
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 |
/******************************************************************************/
|
104 |
/* SETTINGS */
|
105 |
/******************************************************************************/
|
106 |
|
107 |
/******************************************************************************/
|
108 |
/* CHECKS */
|
109 |
/******************************************************************************/
|
110 |
|
111 |
/******************************************************************************/
|
112 |
/* DATA STRUCTURES AND TYPES */
|
113 |
/******************************************************************************/
|
114 |
|
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 |
/* pointer to a BaseAsynchronousChannel object */ \
|
126 |
BaseAsynchronousChannel* asyncchannel; \ |
127 |
/* event listener for the associated BaseAsynchronousChannel */ \
|
128 |
event_listener_t listener; \ |
129 |
/* pointer to the next chennal in a AosShellStream */ \
|
130 |
struct aos_shellchannel* next; \
|
131 |
/* 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 |
typedef struct aos_shellchannel { |
150 |
/** @brief Virtual Methods Table. */
|
151 |
const struct AosShellChannelVMT* vmt; |
152 |
_aos_shell_channel_data |
153 |
} AosShellChannel; |
154 |
|
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 |
typedef struct aos_shellstream { |
185 |
const struct AosShellStreamVMT* vmt; |
186 |
_aos_shellstream_data |
187 |
} AosShellStream; |
188 |
|
189 |
/**
|
190 |
* @brief Shell command calback type.
|
191 |
*
|
192 |
* @param[in] stream Stream to print to.
|
193 |
* @param[in] argc Number of arguments.
|
194 |
* @param[in] argv List of arguments.
|
195 |
*/
|
196 |
typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]); |
197 |
|
198 |
/**
|
199 |
* @brief Shell command structure.
|
200 |
*/
|
201 |
typedef struct aos_shellcommand { |
202 |
/**
|
203 |
* @brief Command name.
|
204 |
*/
|
205 |
const char* name; |
206 |
|
207 |
/**
|
208 |
* @brief Callback function.
|
209 |
*/
|
210 |
aos_shellcmdcb_t callback; |
211 |
|
212 |
/**
|
213 |
* @brief Pointer to next command in a singly linked list.
|
214 |
*/
|
215 |
struct aos_shellcommand* next;
|
216 |
|
217 |
} aos_shellcommand_t; |
218 |
|
219 |
/**
|
220 |
* @brief Execution status of a shell command.
|
221 |
*/
|
222 |
typedef struct aos_shellexecstatus { |
223 |
/**
|
224 |
* @brief Pointer to the command that was executed.
|
225 |
*/
|
226 |
aos_shellcommand_t* command; |
227 |
|
228 |
/**
|
229 |
* @brief Return value of the executed command.
|
230 |
*/
|
231 |
int retval;
|
232 |
} aos_shellexecstatus_t; |
233 |
|
234 |
/**
|
235 |
* @brief Shell structure.
|
236 |
*/
|
237 |
typedef struct aos_shell { |
238 |
/**
|
239 |
* @brief Pointer to the thread object.
|
240 |
*/
|
241 |
thread_t* thread; |
242 |
|
243 |
#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
|
244 |
|
245 |
/**
|
246 |
* @brief Name of the shell and the associated thread.
|
247 |
*/
|
248 |
const char* name; |
249 |
|
250 |
#endif /* (CH_CFG_USE_REGISTRY == TRUE) */ |
251 |
|
252 |
/**
|
253 |
* @brief Event source.
|
254 |
*/
|
255 |
event_source_t eventSource; |
256 |
|
257 |
/**
|
258 |
* @brief Listener for OS related events.
|
259 |
*/
|
260 |
event_listener_t osEventListener; |
261 |
|
262 |
/**
|
263 |
* @brief Pointer to the first I/O channel.
|
264 |
*/
|
265 |
AosShellStream stream; |
266 |
|
267 |
/**
|
268 |
* @brief String to printed as prompt.
|
269 |
*/
|
270 |
const char* prompt; |
271 |
|
272 |
/**
|
273 |
* @brief Pointer to the first element of the singly linked list of commands.
|
274 |
* @details Commands are ordered alphabetically in the list.
|
275 |
*/
|
276 |
aos_shellcommand_t* commands; |
277 |
|
278 |
/**
|
279 |
* @brief Execution status of the most recent command.
|
280 |
*/
|
281 |
aos_shellexecstatus_t execstatus; |
282 |
|
283 |
/**
|
284 |
* @brief Structure containing all input data.
|
285 |
*/
|
286 |
struct {
|
287 |
/**
|
288 |
* @brief Input buffer.
|
289 |
* @details This buffer is interpreted as two dimensional array.
|
290 |
* It contains @p nentries elements of @p linewidth size each.
|
291 |
*/
|
292 |
char* buffer;
|
293 |
|
294 |
/**
|
295 |
* @brief Number of entries in the buffer.
|
296 |
* @note Must be >0.
|
297 |
*/
|
298 |
size_t nentries; |
299 |
|
300 |
/**
|
301 |
* @brief Width of each input line.
|
302 |
* @brief Must be >0.
|
303 |
*/
|
304 |
size_t linewidth; |
305 |
|
306 |
/**
|
307 |
* @brief Size of the argument buffer.
|
308 |
* @note Must be >0.
|
309 |
*/
|
310 |
size_t nargs; |
311 |
} input; |
312 |
|
313 |
/**
|
314 |
* @brief Configuration flags.
|
315 |
*/
|
316 |
uint8_t config; |
317 |
} aos_shell_t; |
318 |
|
319 |
/******************************************************************************/
|
320 |
/* MACROS */
|
321 |
/******************************************************************************/
|
322 |
|
323 |
/**
|
324 |
* @brief Initializes a shell command object.
|
325 |
*
|
326 |
* @param[in] var Name of the object variable to be initialized.
|
327 |
* @param[in] name Shell command name.
|
328 |
* @param[in] callback Pointer to the callback function.
|
329 |
*/
|
330 |
#define AOS_SHELL_COMMAND(var, name, callback) aos_shellcommand_t var = { \
|
331 |
/* name */ name, \
|
332 |
/* callback */ callback, \
|
333 |
/* next */ NULL, \ |
334 |
} |
335 |
|
336 |
/******************************************************************************/
|
337 |
/* EXTERN DECLARATIONS */
|
338 |
/******************************************************************************/
|
339 |
|
340 |
#if defined(__cplusplus)
|
341 |
extern "C" { |
342 |
#endif /* defined(__cplusplus) */ |
343 |
void aosShellInit(aos_shell_t* shell, const char* name, const char* prompt, char inbuf[], size_t entries, size_t linewidth, size_t numargs); |
344 |
void aosShellStreamInit(AosShellStream* stream);
|
345 |
void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
|
346 |
aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd); |
347 |
aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
|
348 |
unsigned int aosShellCountCommands(aos_shell_t* shell); |
349 |
void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
|
350 |
aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel); |
351 |
void aosShellChannelInputEnable(AosShellChannel* channel);
|
352 |
void aosShellChannelInputDisable( AosShellChannel* channel);
|
353 |
void aosShellChannelOutputEnable(AosShellChannel* channel);
|
354 |
void aosShellChannelOutputDisable(AosShellChannel* channel);
|
355 |
THD_FUNCTION(aosShellThread, shell); |
356 |
#if defined(__cplusplus)
|
357 |
} |
358 |
#endif /* defined(__cplusplus) */ |
359 |
|
360 |
/******************************************************************************/
|
361 |
/* INLINE FUNCTIONS */
|
362 |
/******************************************************************************/
|
363 |
|
364 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
365 |
|
366 |
#endif /* AMIROOS_SHELL_H */ |
367 |
|
368 |
/** @} */
|