Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / inc / aos_shell.h @ 8399aeae

History | View | Annotate | Download (9.346 KB)

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