Statistics
| Branch: | Tag: | Revision:

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

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