Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.937 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  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 cc33217b Thomas Schöpping
/**
39 e545e620 Thomas Schöpping
 * @brief   Shell event flag that is emitted when the thread starts.
40
 */
41 ab5cad1b Thomas Schöpping
#define AOS_SHELL_EVTFLAG_START                 ((eventflags_t)(1 << 0))
42 e545e620 Thomas Schöpping
43
/**
44
 * @brief   Shell event flag that is emitted when a command is executed.
45
 */
46 ab5cad1b Thomas Schöpping
#define AOS_SHELL_EVTFLAG_EXECUTE               ((eventflags_t)(1 << 1))
47 e545e620 Thomas Schöpping
48
/**
49
 * @brief   Shell event flag that is emitted when a command execution finished.
50
 */
51 ab5cad1b Thomas Schöpping
#define AOS_SHELL_EVTFLAG_DONE                  ((eventflags_t)(1 << 2))
52 e545e620 Thomas Schöpping
53
/**
54
 * @brief   Shell event flag that is emitted when the shread stops.
55
 */
56 ab5cad1b Thomas Schöpping
#define AOS_SHELL_EVTFLAG_EXIT                  ((eventflags_t)(1 << 3))
57 e545e620 Thomas Schöpping
58
/**
59
 * @brief   Shell event flag that is emitted when an I/O error occurred.
60
 */
61 ab5cad1b Thomas Schöpping
#define AOS_SHELL_EVTFLAG_IOERROR               ((eventflags_t)(1 << 4))
62 e545e620 Thomas Schöpping
63
/**
64
 * @brief   Shell input configuration for replacing content by user input.
65
 */
66 ab5cad1b Thomas Schöpping
#define AOS_SHELL_CONFIG_INPUT_OVERWRITE        (1 << 0)
67 e545e620 Thomas Schöpping
68
/**
69
 * @brief   Shell prompt configuration print a minimalistic prompt.
70
 */
71 ab5cad1b Thomas Schöpping
#define AOS_SHELL_CONFIG_PROMPT_MINIMAL         (1 << 1)
72 e545e620 Thomas Schöpping
73
/**
74
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
75
 */
76 ab5cad1b Thomas Schöpping
#define AOS_SHELL_CONFIG_PROMPT_UPTIME          (1 << 2)
77 e545e620 Thomas Schöpping
78
/**
79 8399aeae Thomas Schöpping
 * @brief   Shell prompt configuration to additionally print the date and time with the prompt.
80
 */
81 ab5cad1b Thomas Schöpping
#define AOS_SHELL_CONFIG_PROMPT_DATETIME        (2 << 2)
82 8399aeae Thomas Schöpping
83
/**
84 e545e620 Thomas Schöpping
 * @brief   Shell prompt configuration to additionally print the system uptime with the prompt.
85
 */
86 ab5cad1b Thomas Schöpping
#define AOS_SHELL_CONFIG_MATCH_CASE             (1 << 4)
87 ba516b61 Thomas Schöpping
88
/**
89
 * @brief   Shell I/O channel flag whether the channel is attached to a list.
90
 */
91 ab5cad1b Thomas Schöpping
#define AOS_SHELLCHANNEL_ATTACHED               (1 << 0)
92 ba516b61 Thomas Schöpping
93
/**
94
 * @brief   Shell I/O channel flag whether the channel is enabled as input.
95
 */
96 ab5cad1b Thomas Schöpping
#define AOS_SHELLCHANNEL_INPUT_ENABLED          (1 << 1)
97 ba516b61 Thomas Schöpping
98
/**
99
 * @brief   Shell I/O channel flag whether the channel is enabled as output.
100
 */
101 ab5cad1b Thomas Schöpping
#define AOS_SHELLCHANNEL_OUTPUT_ENABLED         (1 << 2)
102 ba516b61 Thomas Schöpping
103 f3ac1c96 Thomas Schöpping
/******************************************************************************/
104
/* SETTINGS                                                                   */
105
/******************************************************************************/
106
107
/******************************************************************************/
108
/* CHECKS                                                                     */
109
/******************************************************************************/
110
111
/******************************************************************************/
112
/* DATA STRUCTURES AND TYPES                                                  */
113
/******************************************************************************/
114 ba516b61 Thomas Schöpping
115
/**
116
 * @brief   AosShellChannel specific methods.
117
 */
118
#define _aos_shell_channel_methods                                          \
119
  _base_asynchronous_channel_methods
120
121
/**
122
 * @brief   AosShellChannel specific data.
123
 */
124
#define _aos_shell_channel_data                                             \
125 dd8738ea Thomas Schöpping
  /* pointer to a BaseAsynchronousChannel object */                         \
126
  BaseAsynchronousChannel* asyncchannel;                                    \
127 ba516b61 Thomas Schöpping
  /* event listener for the associated BaseAsynchronousChannel */           \
128
  event_listener_t listener;                                                \
129
  /* pointer to the next chennal in a AosShellStream */                     \
130 f3ac1c96 Thomas Schöpping
  struct aos_shellchannel* next;                                            \
131 ba516b61 Thomas Schöpping
  /* flags related to the channel */                                        \
132
  uint8_t flags;
133
134
/**
135
 * @extends BaseAsynchronousChannelVMT
136
 *
137
 * @brief   AosShellChannel virtual methods table.
138
 */
139
struct AosShellChannelVMT {
140
  _aos_shell_channel_methods
141
};
142
143
/**
144
 * @extends BaseAsynchronousChannel
145
 *
146
 * @brief   Shell channel class.
147
 * @details This class implements an asynchronous I/O channel.
148
 */
149 f3ac1c96 Thomas Schöpping
typedef struct aos_shellchannel {
150 ba516b61 Thomas Schöpping
  /** @brief Virtual Methods Table. */
151
  const struct AosShellChannelVMT* vmt;
152
  _aos_shell_channel_data
153 f3ac1c96 Thomas Schöpping
} AosShellChannel;
154 ba516b61 Thomas Schöpping
155
/**
156
 * @brief   AosShellStream methods.
157
 */
158
#define _aos_shellstream_methods                                            \
159
  _base_sequential_stream_methods
160
161
/**
162
 * @brief   AosShellStream data.
163
 */
164
#define _aos_shellstream_data                                               \
165
  /* Pointer to the first channel in a list. */                             \
166
  AosShellChannel* channel;
167
168
/**
169
 * @extends BaseSequentialStream
170
 *
171
 * @brief   AosShellStream virtual methods table.
172
 */
173
struct AosShellStreamVMT {
174
  _aos_shellstream_methods
175
};
176
177
/**
178
 * @extends BaseSequentialStream
179
 *
180
 * @brief   Shell Stream class.
181
 * @details This class implements an base sequential stream.
182
 * @todo    So far only output but no input is supported.
183
 */
184 f3ac1c96 Thomas Schöpping
typedef struct aos_shellstream {
185 ba516b61 Thomas Schöpping
  const struct AosShellStreamVMT* vmt;
186
  _aos_shellstream_data
187 f3ac1c96 Thomas Schöpping
} AosShellStream;
188 e545e620 Thomas Schöpping
189
/**
190
 * @brief   Shell command calback type.
191 4c72a54c Thomas Schöpping
 *
192
 * @param[in] stream  Stream to print to.
193
 * @param[in] argc    Number of arguments.
194
 * @param[in] argv    List of arguments.
195 e545e620 Thomas Schöpping
 */
196
typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]);
197
198
/**
199
 * @brief   Shell command structure.
200
 */
201
typedef struct aos_shellcommand {
202
  /**
203
   * @brief   Command name.
204
   */
205
  const char* name;
206
207
  /**
208
   * @brief   Callback function.
209
   */
210
  aos_shellcmdcb_t callback;
211
212
  /**
213
   * @brief   Pointer to next command in a singly linked list.
214
   */
215
  struct aos_shellcommand* next;
216 ba516b61 Thomas Schöpping
217 e545e620 Thomas Schöpping
} aos_shellcommand_t;
218
219
/**
220
 * @brief   Execution status of a shell command.
221
 */
222
typedef struct aos_shellexecstatus {
223 ba516b61 Thomas Schöpping
  /**
224
   * @brief   Pointer to the command that was executed.
225
   */
226
  aos_shellcommand_t* command;
227
228
  /**
229
   * @brief   Return value of the executed command.
230
   */
231
  int retval;
232 e545e620 Thomas Schöpping
} aos_shellexecstatus_t;
233
234
/**
235
 * @brief   Shell structure.
236
 */
237
typedef struct aos_shell {
238
  /**
239
   * @brief   Pointer to the thread object.
240
   */
241
  thread_t* thread;
242
243 3da12676 Thomas Schöpping
#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
244
245
  /**
246
   * @brief   Name of the shell and the associated thread.
247
   */
248
  const char* name;
249
250
#endif /* (CH_CFG_USE_REGISTRY == TRUE) */
251
252 e545e620 Thomas Schöpping
  /**
253
   * @brief   Event source.
254
   */
255
  event_source_t eventSource;
256
257
  /**
258 697dba3c Thomas Schöpping
   * @brief   Listener for OS related events.
259 e545e620 Thomas Schöpping
   */
260 697dba3c Thomas Schöpping
  event_listener_t osEventListener;
261 ba516b61 Thomas Schöpping
262
  /**
263
     * @brief   Pointer to the first I/O channel.
264
     */
265
  AosShellStream stream;
266 e545e620 Thomas Schöpping
267
  /**
268
   * @brief   String to printed as prompt.
269
   */
270
  const char* prompt;
271
272
  /**
273
   * @brief   Pointer to the first element of the singly linked list of commands.
274
   * @details Commands are ordered alphabetically in the list.
275
   */
276
  aos_shellcommand_t* commands;
277
278
  /**
279
   * @brief   Execution status of the most recent command.
280
   */
281
  aos_shellexecstatus_t execstatus;
282
283
  /**
284 cc33217b Thomas Schöpping
   * @brief   Structure containing all input data.
285 e545e620 Thomas Schöpping
   */
286 cc33217b Thomas Schöpping
  struct {
287
    /**
288
     * @brief   Input buffer.
289 697dba3c Thomas Schöpping
     * @details This buffer is interpreted as two dimensional array.
290
     *          It contains @p nentries elements of @p linewidth size each.
291 cc33217b Thomas Schöpping
     */
292 697dba3c Thomas Schöpping
    char* buffer;
293 ba516b61 Thomas Schöpping
294
    /**
295 697dba3c Thomas Schöpping
     * @brief   Number of entries in the buffer.
296
     * @note    Must be >0.
297 ba516b61 Thomas Schöpping
     */
298 697dba3c Thomas Schöpping
    size_t nentries;
299 ba516b61 Thomas Schöpping
300
    /**
301 697dba3c Thomas Schöpping
     * @brief   Width of each input line.
302
     * @brief   Must be >0.
303 ba516b61 Thomas Schöpping
     */
304 697dba3c Thomas Schöpping
    size_t linewidth;
305 ba516b61 Thomas Schöpping
306
    /**
307 697dba3c Thomas Schöpping
     * @brief   Size of the argument buffer.
308
     * @note    Must be >0.
309 ba516b61 Thomas Schöpping
     */
310 697dba3c Thomas Schöpping
    size_t nargs;
311
  } input;
312 e545e620 Thomas Schöpping
313
  /**
314
   * @brief   Configuration flags.
315
   */
316
  uint8_t config;
317
} aos_shell_t;
318
319 f3ac1c96 Thomas Schöpping
/******************************************************************************/
320
/* MACROS                                                                     */
321
/******************************************************************************/
322
323 4c72a54c Thomas Schöpping
/**
324
 * @brief   Initializes a shell command object.
325
 *
326
 * @param[in] var       Name of the object variable to be initialized.
327
 * @param[in] name      Shell command name.
328
 * @param[in] callback  Pointer to the callback function.
329
 */
330
#define AOS_SHELL_COMMAND(var, name, callback) aos_shellcommand_t var = {     \
331
  /* name     */ name,                                                        \
332
  /* callback */ callback,                                                    \
333
  /* next     */ NULL,                                                        \
334
}
335
336 f3ac1c96 Thomas Schöpping
/******************************************************************************/
337
/* EXTERN DECLARATIONS                                                        */
338
/******************************************************************************/
339
340 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
341 e545e620 Thomas Schöpping
extern "C" {
342 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
343 3da12676 Thomas Schöpping
  void aosShellInit(aos_shell_t* shell, const char* name, const char* prompt, char inbuf[], size_t entries, size_t linewidth, size_t numargs);
344 ba516b61 Thomas Schöpping
  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 aed3754b Thomas Schöpping
  unsigned int aosShellCountCommands(aos_shell_t* shell);
349 ba516b61 Thomas Schöpping
  void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
350
  aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel);
351
  void aosShellChannelInputEnable(AosShellChannel* channel);
352
  void aosShellChannelInputDisable( AosShellChannel* channel);
353
  void aosShellChannelOutputEnable(AosShellChannel* channel);
354
  void aosShellChannelOutputDisable(AosShellChannel* channel);
355 e545e620 Thomas Schöpping
  THD_FUNCTION(aosShellThread, shell);
356 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
357 e545e620 Thomas Schöpping
}
358 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
359 e545e620 Thomas Schöpping
360 f3ac1c96 Thomas Schöpping
/******************************************************************************/
361
/* INLINE FUNCTIONS                                                           */
362
/******************************************************************************/
363
364 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
365 ba516b61 Thomas Schöpping
366 6ff06bbf Thomas Schöpping
#endif /* AMIROOS_SHELL_H */
367 53710ca3 Marc Rothmann
368
/** @} */