Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_shell.h @ f606e2bf

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