Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.2 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) || (AMIROOS_CFG_TESTS_ENABLE == true)
32
#include <hal.h>
33
#include <aos_types.h>
34
#include <aos_debug.h>
35
/******************************************************************************/
36
/* CONSTANTS                                                                  */
37
/******************************************************************************/
38

    
39
/**
40
 * @brief   Shell event flag that is emitted when the thread starts.
41
 */
42
#define AOS_SHELL_EVTFLAG_START                   ((eventflags_t)(1 << 0))
43

    
44
/**
45
 * @brief   Shell event flag that is emitted when a command is executed.
46
 */
47
#define AOS_SHELL_EVTFLAG_EXEC                    ((eventflags_t)(1 << 1))
48

    
49
/**
50
 * @brief   Shell event flag that is emitted when a command execution finished.
51
 */
52
#define AOS_SHELL_EVTFLAG_DONE                    ((eventflags_t)(1 << 2))
53

    
54
/**
55
 * @brief   Shell event flag that is emitted when the shread stops.
56
 */
57
#define AOS_SHELL_EVTFLAG_EXIT                    ((eventflags_t)(1 << 3))
58

    
59
/**
60
 * @brief   Shell event flag that is emitted when an I/O error occurred.
61
 */
62
#define AOS_SHELL_EVTFLAG_IOERROR                 ((eventflags_t)(1 << 4))
63

    
64
/**
65
 * @brief   Shell input configuration for replacing content by user input.
66
 */
67
#define AOS_SHELL_CONFIG_INPUT_OVERWRITE          (1 << 0)
68

    
69
/**
70
 * @brief   Shell prompt configuration print a minimalistic prompt.
71
 */
72
#define AOS_SHELL_CONFIG_PROMPT_MINIMAL           (1 << 1)
73

    
74
/**
75
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
76
 */
77
#define AOS_SHELL_CONFIG_PROMPT_UPTIME            (1 << 2)
78

    
79
/**
80
 * @brief   Shell prompt configuration to additionally print the date and time with the prompt.
81
 */
82
#define AOS_SHELL_CONFIG_PROMPT_DATETIME          (2 << 2)
83

    
84
/**
85
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
86
 */
87
#define AOS_SHELL_CONFIG_MATCH_CASE               (1 << 4)
88

    
89
/**
90
 * @brief   Shell I/O channel flag whether the channel is attached to a list.
91
 */
92
#define AOS_SHELLCHANNEL_ATTACHED                 (1 << 0)
93

    
94
/**
95
 * @brief   Shell I/O channel flag whether the channel is enabled as input.
96
 */
97
#define AOS_SHELLCHANNEL_INPUT_ENABLED            (1 << 1)
98

    
99
/**
100
 * @brief   Shell I/O channel flag whether the channel is enabled as output.
101
 */
102
#define AOS_SHELLCHANNEL_OUTPUT_ENABLED           (1 << 2)
103

    
104
/******************************************************************************/
105
/* SETTINGS                                                                   */
106
/******************************************************************************/
107

    
108
/******************************************************************************/
109
/* CHECKS                                                                     */
110
/******************************************************************************/
111

    
112
/******************************************************************************/
113
/* DATA STRUCTURES AND TYPES                                                  */
114
/******************************************************************************/
115

    
116
/**
117
 * @brief   AosShellChannel specific methods.
118
 */
119
#define _aos_shell_channel_methods                                          \
120
  _base_asynchronous_channel_methods
121

    
122
/**
123
 * @brief   AosShellChannel specific data.
124
 */
125
#define _aos_shell_channel_data                                             \
126
  /* pointer to a BaseAsynchronousChannel object */                         \
127
  BaseAsynchronousChannel* asyncchannel;                                    \
128
  /* event listener for the associated BaseAsynchronousChannel */           \
129
  event_listener_t listener;                                                \
130
  /* pointer to the next chennal in a AosShellStream */                     \
131
  struct aos_shellchannel* next;                                            \
132
  /* flags related to the channel */                                        \
133
  uint8_t flags;
134

    
135
/**
136
 * @extends BaseAsynchronousChannelVMT
137
 *
138
 * @brief   AosShellChannel virtual methods table.
139
 */
140
struct AosShellChannelVMT {
141
  _aos_shell_channel_methods
142
};
143

    
144
/**
145
 * @extends BaseAsynchronousChannel
146
 *
147
 * @brief   Shell channel class.
148
 * @details This class implements an asynchronous I/O channel.
149
 */
150
typedef struct aos_shellchannel {
151
  /** @brief Virtual Methods Table. */
152
  const struct AosShellChannelVMT* vmt;
153
  _aos_shell_channel_data
154
} AosShellChannel;
155

    
156
/**
157
 * @brief   AosShellStream methods.
158
 */
159
#define _aos_shellstream_methods                                            \
160
  _base_sequential_stream_methods
161

    
162
/**
163
 * @brief   AosShellStream data.
164
 */
165
#define _aos_shellstream_data                                               \
166
  /* Pointer to the first channel in a list. */                             \
167
  AosShellChannel* channel;
168

    
169
/**
170
 * @extends BaseSequentialStream
171
 *
172
 * @brief   AosShellStream virtual methods table.
173
 */
174
struct AosShellStreamVMT {
175
  _aos_shellstream_methods
176
};
177

    
178
/**
179
 * @extends BaseSequentialStream
180
 *
181
 * @brief   Shell Stream class.
182
 * @details This class implements an base sequential stream.
183
 * @todo    So far only output but no input is supported.
184
 */
185
typedef struct aos_shellstream {
186
  const struct AosShellStreamVMT* vmt;
187
  _aos_shellstream_data
188
} AosShellStream;
189

    
190
/**
191
 * @brief   Shell command calback type.
192
 */
193
typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]);
194

    
195
/**
196
 * @brief   Shell command structure.
197
 */
198
typedef struct aos_shellcommand {
199
  /**
200
   * @brief   Command name.
201
   */
202
  const char* name;
203

    
204
  /**
205
   * @brief   Callback function.
206
   */
207
  aos_shellcmdcb_t callback;
208

    
209
  /**
210
   * @brief   Pointer to next command in a singly linked list.
211
   */
212
  struct aos_shellcommand* next;
213

    
214
} aos_shellcommand_t;
215

    
216
/**
217
 * @brief   Execution status of a shell command.
218
 */
219
typedef struct aos_shellexecstatus {
220
  /**
221
   * @brief   Pointer to the command that was executed.
222
   */
223
  aos_shellcommand_t* command;
224

    
225
  /**
226
   * @brief   Return value of the executed command.
227
   */
228
  int retval;
229
} aos_shellexecstatus_t;
230

    
231
/**
232
 * @brief   Enumerator to encode shell actions.
233
 */
234
typedef enum aos_shellaction {
235
  AOS_SHELL_ACTION_NONE,
236
  AOS_SHELL_ACTION_READCHAR,
237
  AOS_SHELL_ACTION_AUTOFILL,
238
  AOS_SHELL_ACTION_SUGGEST,
239
  AOS_SHELL_ACTION_INSERTTOGGLE,
240
  AOS_SHELL_ACTION_DELETEFORWARD,
241
  AOS_SHELL_ACTION_DELETEBACKWARD,
242
  AOS_SHELL_ACTION_RECALLLAST,
243
  AOS_SHELL_ACTION_CLEAR,
244
  AOS_SHELL_ACTION_CURSOR2START,
245
  AOS_SHELL_ACTION_CURSOR2END,
246
  AOS_SHELL_ACTION_CURSORLEFT,
247
  AOS_SHELL_ACTION_CURSORRIGHT,
248
  AOS_SHELL_ACTION_EXECUTE,
249
  AOS_SHELL_ACTION_ESCSTART,
250
} aos_shellaction_t;
251

    
252
/**
253
 * @brief   Shell structure.
254
 */
255
typedef struct aos_shell {
256
  /**
257
   * @brief   Pointer to the thread object.
258
   */
259
  thread_t* thread;
260

    
261
  /**
262
   * @brief   Event source.
263
   */
264
  event_source_t eventSource;
265

    
266
  /**
267
   * @brief   Struct for OS related events
268
   */
269
  struct {
270
    /**
271
     * @brief   Pointer to the OS' event source.
272
     */
273
    event_source_t* eventSource;
274

    
275
    /**
276
     * @brief   Listener for OS related events.
277
     */
278
    event_listener_t eventListener;
279
  } os;
280

    
281
  /**
282
     * @brief   Pointer to the first I/O channel.
283
     */
284
  AosShellStream stream;
285

    
286
  /**
287
   * @brief   String to printed as prompt.
288
   */
289
  const char* prompt;
290

    
291
  /**
292
   * @brief   Pointer to the first element of the singly linked list of commands.
293
   * @details Commands are ordered alphabetically in the list.
294
   */
295
  aos_shellcommand_t* commands;
296

    
297
  /**
298
   * @brief   Execution status of the most recent command.
299
   */
300
  aos_shellexecstatus_t execstatus;
301

    
302
  /**
303
   * @brief   Input buffer.
304
   */
305
  char* line;
306

    
307
  /**
308
   * @brief   Size of the input buffer.
309
   */
310
  size_t linesize;
311

    
312
  /**
313
   * @brief   Structure containing data for internal input parsing.
314
   */
315
  struct {
316
    /**
317
     * @brief   The last action executed by the shell.
318
     */
319
    aos_shellaction_t lastaction;
320

    
321
    /**
322
     * @brief   Number of character in the current escape sequence.
323
     */
324
    uint8_t escp;
325

    
326
    /**
327
     * @brief   Buffer to store an escape sequence.
328
     */
329
    char escseq[5];
330

    
331
    /**
332
     * @brief   Current curso position.
333
     */
334
    size_t cursorpos;
335

    
336
    /**
337
     * @brief   Current line width.
338
     */
339
    size_t lineend;
340

    
341
    /**
342
     * @brief   Flag whether there was input since the prompt was printed the last time.
343
     */
344
    bool noinput;
345
  } inputdata;
346

    
347
  /**
348
   * @brief   Argument buffer.
349
   */
350
  char** arglist;
351

    
352
  /**
353
   * @brief   Size of the argument buffer.
354
   */
355
  size_t arglistsize;
356

    
357
  /**
358
   * @brief   Configuration flags.
359
   */
360
  uint8_t config;
361
} aos_shell_t;
362

    
363
/******************************************************************************/
364
/* MACROS                                                                     */
365
/******************************************************************************/
366

    
367
/******************************************************************************/
368
/* EXTERN DECLARATIONS                                                        */
369
/******************************************************************************/
370

    
371
#ifdef __cplusplus
372
extern "C" {
373
#endif
374
  void aosShellInit(aos_shell_t* shell, event_source_t* oseventsource, const char* prompt, char* line, size_t linesize, char** arglist, size_t arglistsize);
375
  void aosShellStreamInit(AosShellStream* stream);
376
  void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
377
  aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd);
378
  aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
379
  unsigned int aosShellCountCommands(aos_shell_t* shell);
380
  void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
381
  aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel);
382
  void aosShellChannelInputEnable(AosShellChannel* channel);
383
  void aosShellChannelInputDisable( AosShellChannel* channel);
384
  void aosShellChannelOutputEnable(AosShellChannel* channel);
385
  void aosShellChannelOutputDisable(AosShellChannel* channel);
386
  THD_FUNCTION(aosShellThread, shell);
387
#ifdef __cplusplus
388
}
389
#endif
390

    
391
/******************************************************************************/
392
/* INLINE FUNCTIONS                                                           */
393
/******************************************************************************/
394

    
395
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) */
396

    
397
#endif /* AMIROOS_SHELL_H */
398

    
399
/** @} */