Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.7 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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) || defined(__DOXYGEN__)
33

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

    
38

    
39

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

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

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

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

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

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

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

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

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

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

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

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

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

    
105
/******************************************************************************/
106
/* SETTINGS                                                                   */
107
/******************************************************************************/
108

    
109
/******************************************************************************/
110
/* CHECKS                                                                     */
111
/******************************************************************************/
112

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

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

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

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

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

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

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

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

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

    
191
/**
192
 * @brief   Shell command calback type.
193
 *
194
 * @param[in] stream  Stream to print to.
195
 * @param[in] argc    Number of arguments.
196
 * @param[in] argv    List of arguments.
197
 */
198
typedef int (*aos_shellcmdcb_t)(BaseSequentialStream* stream, int argc, char* argv[]);
199

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

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

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

    
219
} aos_shellcommand_t;
220

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

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

    
236
/**
237
 * @brief   Shell structure.
238
 */
239
typedef struct aos_shell {
240
  /**
241
   * @brief   Pointer to the thread object.
242
   */
243
  thread_t* thread;
244

    
245
  /**
246
   * @brief   Event source.
247
   */
248
  event_source_t eventSource;
249

    
250
  /**
251
   * @brief   Listener for OS related events.
252
   */
253
  event_listener_t osEventListener;
254

    
255
  /**
256
     * @brief   Pointer to the first I/O channel.
257
     */
258
  AosShellStream stream;
259

    
260
  /**
261
   * @brief   String to printed as prompt.
262
   */
263
  const char* prompt;
264

    
265
  /**
266
   * @brief   Pointer to the first element of the singly linked list of commands.
267
   * @details Commands are ordered alphabetically in the list.
268
   */
269
  aos_shellcommand_t* commands;
270

    
271
  /**
272
   * @brief   Execution status of the most recent command.
273
   */
274
  aos_shellexecstatus_t execstatus;
275

    
276
  /**
277
   * @brief   Structure containing all input data.
278
   */
279
  struct {
280
    /**
281
     * @brief   Input buffer.
282
     * @details This buffer is interpreted as two dimensional array.
283
     *          It contains @p nentries elements of @p linewidth size each.
284
     */
285
    char* buffer;
286

    
287
    /**
288
     * @brief   Number of entries in the buffer.
289
     * @note    Must be >0.
290
     */
291
    size_t nentries;
292

    
293
    /**
294
     * @brief   Width of each input line.
295
     * @brief   Must be >0.
296
     */
297
    size_t linewidth;
298

    
299
    /**
300
     * @brief   Size of the argument buffer.
301
     * @note    Must be >0.
302
     */
303
    size_t nargs;
304
  } input;
305

    
306
  /**
307
   * @brief   Configuration flags.
308
   */
309
  uint8_t config;
310
} aos_shell_t;
311

    
312
/******************************************************************************/
313
/* MACROS                                                                     */
314
/******************************************************************************/
315

    
316
/**
317
 * @brief   Initializes a shell command object.
318
 *
319
 * @param[in] var       Name of the object variable to be initialized.
320
 * @param[in] name      Shell command name.
321
 * @param[in] callback  Pointer to the callback function.
322
 */
323
#define AOS_SHELL_COMMAND(var, name, callback) aos_shellcommand_t var = {     \
324
  /* name     */ name,                                                        \
325
  /* callback */ callback,                                                    \
326
  /* next     */ NULL,                                                        \
327
}
328

    
329
/******************************************************************************/
330
/* EXTERN DECLARATIONS                                                        */
331
/******************************************************************************/
332

    
333
#if defined(__cplusplus)
334
extern "C" {
335
#endif /* defined(__cplusplus) */
336
  void aosShellInit(aos_shell_t* shell, const char* prompt, char inbuf[], size_t entries, size_t linewidth, size_t numargs);
337
  void aosShellStreamInit(AosShellStream* stream);
338
  void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel);
339
  aos_status_t aosShellAddCommand(aos_shell_t* shell, aos_shellcommand_t* cmd);
340
  aos_status_t aosShellRemoveCommand(aos_shell_t* shell, char* cmd, aos_shellcommand_t** removed);
341
  unsigned int aosShellCountCommands(aos_shell_t* shell);
342
  void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel);
343
  aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel);
344
  void aosShellChannelInputEnable(AosShellChannel* channel);
345
  void aosShellChannelInputDisable( AosShellChannel* channel);
346
  void aosShellChannelOutputEnable(AosShellChannel* channel);
347
  void aosShellChannelOutputDisable(AosShellChannel* channel);
348
  THD_FUNCTION(aosShellThread, shell);
349
#if defined(__cplusplus)
350
}
351
#endif /* defined(__cplusplus) */
352

    
353
/******************************************************************************/
354
/* INLINE FUNCTIONS                                                           */
355
/******************************************************************************/
356

    
357
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
358

    
359
#endif /* AMIROOS_SHELL_H */
360

    
361
/** @} */