Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.515 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 <amiroos.h>
31

    
32
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)
33

    
34
/******************************************************************************/
35
/* CONSTANTS                                                                  */
36
/******************************************************************************/
37

    
38
/**
39
 * @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
 * @brief   Shell event flag that is emitted when the thread starts.
47
 */
48
#define AOS_SHELL_EVTFLAG_START                   ((eventflags_t)(1 << 0))
49

    
50
/**
51
 * @brief   Shell event flag that is emitted when a command is executed.
52
 */
53
#define AOS_SHELL_EVTFLAG_EXEC                    ((eventflags_t)(1 << 1))
54

    
55
/**
56
 * @brief   Shell event flag that is emitted when a command execution finished.
57
 */
58
#define AOS_SHELL_EVTFLAG_DONE                    ((eventflags_t)(1 << 2))
59

    
60
/**
61
 * @brief   Shell event flag that is emitted when the shread stops.
62
 */
63
#define AOS_SHELL_EVTFLAG_EXIT                    ((eventflags_t)(1 << 3))
64

    
65
/**
66
 * @brief   Shell event flag that is emitted when an I/O error occurred.
67
 */
68
#define AOS_SHELL_EVTFLAG_IOERROR                 ((eventflags_t)(1 << 4))
69

    
70
/**
71
 * @brief   Shell input configuration for replacing content by user input.
72
 */
73
#define AOS_SHELL_CONFIG_INPUT_OVERWRITE          (1 << 0)
74

    
75
/**
76
 * @brief   Shell prompt configuration print a minimalistic prompt.
77
 */
78
#define AOS_SHELL_CONFIG_PROMPT_MINIMAL           (1 << 1)
79

    
80
/**
81
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
82
 */
83
#define AOS_SHELL_CONFIG_PROMPT_UPTIME            (1 << 2)
84

    
85
/**
86
 * @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
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
92
 */
93
#define AOS_SHELL_CONFIG_MATCH_CASE               (1 << 4)
94

    
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
/******************************************************************************/
111
/* SETTINGS                                                                   */
112
/******************************************************************************/
113

    
114
/******************************************************************************/
115
/* CHECKS                                                                     */
116
/******************************************************************************/
117

    
118
/******************************************************************************/
119
/* DATA STRUCTURES AND TYPES                                                  */
120
/******************************************************************************/
121

    
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
  /* pointer to a BaseAsynchronousChannel object */                         \
133
  BaseAsynchronousChannel* asyncchannel;                                    \
134
  /* event listener for the associated BaseAsynchronousChannel */           \
135
  event_listener_t listener;                                                \
136
  /* pointer to the next chennal in a AosShellStream */                     \
137
  struct aos_shellchannel* next;                                            \
138
  /* 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
typedef struct aos_shellchannel {
157
  /** @brief Virtual Methods Table. */
158
  const struct AosShellChannelVMT* vmt;
159
  _aos_shell_channel_data
160
} AosShellChannel;
161

    
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
typedef struct aos_shellstream {
192
  const struct AosShellStreamVMT* vmt;
193
  _aos_shellstream_data
194
} AosShellStream;
195

    
196
/**
197
 * @brief   Shell command calback type.
198
 */
199
typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]);
200

    
201
/**
202
 * @brief   Shell command structure.
203
 */
204
typedef struct aos_shellcommand {
205
  /**
206
   * @brief   Command name.
207
   */
208
  const char* name;
209

    
210
  /**
211
   * @brief   Callback function.
212
   */
213
  aos_shellcmdcb_t callback;
214

    
215
  /**
216
   * @brief   Pointer to next command in a singly linked list.
217
   */
218
  struct aos_shellcommand* next;
219

    
220
} aos_shellcommand_t;
221

    
222
/**
223
 * @brief   Execution status of a shell command.
224
 */
225
typedef struct aos_shellexecstatus {
226
  /**
227
   * @brief   Pointer to the command that was executed.
228
   */
229
  aos_shellcommand_t* command;
230

    
231
  /**
232
   * @brief   Return value of the executed command.
233
   */
234
  int retval;
235
} aos_shellexecstatus_t;
236

    
237
/**
238
 * @brief   Enumerator to encode shell actions.
239
 */
240
typedef enum aos_shellaction {
241
  AOS_SHELL_ACTION_NONE,
242
  AOS_SHELL_ACTION_READCHAR,
243
  AOS_SHELL_ACTION_AUTOFILL,
244
  AOS_SHELL_ACTION_SUGGEST,
245
  AOS_SHELL_ACTION_INSERTTOGGLE,
246
  AOS_SHELL_ACTION_DELETEFORWARD,
247
  AOS_SHELL_ACTION_DELETEBACKWARD,
248
  AOS_SHELL_ACTION_RECALLLAST,
249
  AOS_SHELL_ACTION_CLEAR,
250
  AOS_SHELL_ACTION_CURSOR2START,
251
  AOS_SHELL_ACTION_CURSOR2END,
252
  AOS_SHELL_ACTION_CURSORLEFT,
253
  AOS_SHELL_ACTION_CURSORRIGHT,
254
  AOS_SHELL_ACTION_CURSORWORDLEFT,
255
  AOS_SHELL_ACTION_CURSORWORDRIGHT,
256
  AOS_SHELL_ACTION_EXECUTE,
257
  AOS_SHELL_ACTION_ESCSTART,
258
  AOS_SHELL_ACTION_PRINTUNKNOWNSEQUENCE,
259
} aos_shellaction_t;
260

    
261
/**
262
 * @brief   Shell structure.
263
 */
264
typedef struct aos_shell {
265
  /**
266
   * @brief   Pointer to the thread object.
267
   */
268
  thread_t* thread;
269

    
270
  /**
271
   * @brief   Event source.
272
   */
273
  event_source_t eventSource;
274

    
275
  /**
276
   * @brief   Struct for OS related events
277
   */
278
  struct {
279
    /**
280
     * @brief   Pointer to the OS' event source.
281
     */
282
    event_source_t* eventSource;
283

    
284
    /**
285
     * @brief   Listener for OS related events.
286
     */
287
    event_listener_t eventListener;
288
  } os;
289

    
290
  /**
291
     * @brief   Pointer to the first I/O channel.
292
     */
293
  AosShellStream stream;
294

    
295
  /**
296
   * @brief   String to printed as prompt.
297
   */
298
  const char* prompt;
299

    
300
  /**
301
   * @brief   Pointer to the first element of the singly linked list of commands.
302
   * @details Commands are ordered alphabetically in the list.
303
   */
304
  aos_shellcommand_t* commands;
305

    
306
  /**
307
   * @brief   Execution status of the most recent command.
308
   */
309
  aos_shellexecstatus_t execstatus;
310

    
311
  /**
312
   * @brief   Structure containing all input data.
313
   */
314
  struct {
315
    /**
316
     * @brief   Input buffer.
317
     */
318
    char* line;
319

    
320
    /**
321
     * @brief   Size of the input buffer.
322
     */
323
    size_t width;
324
  } input;
325

    
326
  /**
327
   * @brief   Structure containing data for internal input parsing.
328
   */
329
  struct {
330
    /**
331
     * @brief   The last action executed by the shell.
332
     */
333
    aos_shellaction_t lastaction;
334

    
335
    /**
336
     * @brief   Buffer to store an escape sequence.
337
     */
338
    char escseq[AOS_SHELL_ESCSEQUENCE_LENGTH];
339

    
340
    /**
341
     * @brief   Current curso position.
342
     */
343
    size_t cursorpos;
344

    
345
    /**
346
     * @brief   Current line width.
347
     */
348
    size_t lineend;
349

    
350
    /**
351
     * @brief   Flag whether there was input since the prompt was printed the last time.
352
     */
353
    bool noinput;
354
  } inputdata;
355

    
356
  /**
357
   * @brief   Argument buffer.
358
   */
359
  char** arglist;
360

    
361
  /**
362
   * @brief   Size of the argument buffer.
363
   */
364
  size_t arglistsize;
365

    
366
  /**
367
   * @brief   Configuration flags.
368
   */
369
  uint8_t config;
370
} aos_shell_t;
371

    
372
/******************************************************************************/
373
/* MACROS                                                                     */
374
/******************************************************************************/
375

    
376
/******************************************************************************/
377
/* EXTERN DECLARATIONS                                                        */
378
/******************************************************************************/
379

    
380
#if defined(__cplusplus)
381
extern "C" {
382
#endif /* defined(__cplusplus) */
383
  void aosShellInit(aos_shell_t* shell, event_source_t* oseventsource, const char* prompt, char* line, size_t linesize, char** arglist, size_t arglistsize);
384
  void aosShellStreamInit(AosShellStream* stream);
385
  void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
386
  aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd);
387
  aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
388
  unsigned int aosShellCountCommands(aos_shell_t* shell);
389
  void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
390
  aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel);
391
  void aosShellChannelInputEnable(AosShellChannel* channel);
392
  void aosShellChannelInputDisable( AosShellChannel* channel);
393
  void aosShellChannelOutputEnable(AosShellChannel* channel);
394
  void aosShellChannelOutputDisable(AosShellChannel* channel);
395
  THD_FUNCTION(aosShellThread, shell);
396
#if defined(__cplusplus)
397
}
398
#endif /* defined(__cplusplus) */
399

    
400
/******************************************************************************/
401
/* INLINE FUNCTIONS                                                           */
402
/******************************************************************************/
403

    
404
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
405

    
406
#endif /* AMIROOS_SHELL_H */
407

    
408
/** @} */