AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
CommandDispatcher.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_ROUTINE_COMMAND_DISPATCHER_H
26 #define ACE_ROUTINE_COMMAND_DISPATCHER_H
27 
28 #include <Arduino.h> // Print
29 #include <AceRoutine.h>
30 #include "StreamReader.h"
31 
32 namespace ace_routine {
33 namespace cli {
34 
36 typedef void (*CommandHandler)(Print& printer, int argc, const char** argv);
37 
45  const CommandHandler command;
46  const char* name;
47  const char* helpString;
48 };
49 
55  const CommandHandler command;
56  const __FlashStringHelper* name;
57  const __FlashStringHelper* helpString;
58 };
59 
78  public:
91  StreamReader& streamReader,
92  Print& printer,
93  uint8_t numCommands,
94  const char** argv,
95  uint8_t argvSize):
96  mStreamReader(streamReader),
97  mPrinter(printer),
98  mNumCommands(numCommands),
99  mArgv(argv),
100  mArgvSize(argvSize) {}
101 
106  static uint8_t tokenize(char* line, const char** argv, uint8_t argvSize);
107 
108  protected:
109  // Disable copy-constructor and assignment operator
110  CommandDispatcher(const CommandDispatcher&) = delete;
111  CommandDispatcher& operator=(const CommandDispatcher&) = delete;
112 
113  static const uint8_t STATUS_SUCCESS = 0;
114  static const uint8_t STATUS_BUFFER_OVERFLOW = 1;
115  static const uint8_t STATUS_FLUSH_TO_EOL = 2;
116  static const char DELIMS[];
117 
119  void printLineError(const char* line, uint8_t statusCode);
120 
122  void helpCommandHandler(Print& printer, int argc, const char** argv);
123 
125  virtual void helpGeneric(Print& printer) = 0;
126 
128  virtual bool helpSpecific(Print& printer, const char* cmd) = 0;
129 
131  void runCommand(char* line);
132 
134  virtual void findAndRunCommand(
135  const char* cmd, int argc, const char** argv) = 0;
136 
137  virtual int run() override;
138 
139  StreamReader& mStreamReader;
140  Print& mPrinter;
141  const uint8_t mNumCommands;
142  const char** const mArgv;
143  const uint8_t mArgvSize;
144 };
145 
148  public:
162  StreamReader& streamReader,
163  Print& printer,
164  const DispatchRecordC* dispatchTable,
165  uint8_t numCommands,
166  const char** argv,
167  uint8_t argvSize):
168  CommandDispatcher(streamReader, printer, numCommands, argv, argvSize),
169  mDispatchTable(dispatchTable) {}
170 
178  static const DispatchRecordC* findCommand(
179  const DispatchRecordC* dispatchTable,
180  uint8_t numCommands, const char* cmd);
181 
182  private:
184  virtual void helpGeneric(Print& printer) override;
185 
187  virtual bool helpSpecific(Print& printer, const char* cmd) override;
188 
190  virtual void findAndRunCommand(
191  const char* cmd, int argc, const char** argv) override;
192 
193  const DispatchRecordC* const mDispatchTable;
194 };
195 
201  public:
215  StreamReader& streamReader,
216  Print& printer,
217  const DispatchRecordF* dispatchTable,
218  uint8_t numCommands,
219  const char** argv,
220  uint8_t argvSize):
221  CommandDispatcher(streamReader, printer, numCommands, argv, argvSize),
222  mDispatchTable(dispatchTable) {}
223 
227  static const DispatchRecordF* findCommand(
228  const DispatchRecordF* dispatchTable,
229  uint8_t numCommands, const char* cmd);
230 
231  private:
233  virtual void helpGeneric(Print& printer) override;
234 
236  virtual bool helpSpecific(Print& printer, const char* cmd) override;
237 
239  virtual void findAndRunCommand(
240  const char* cmd, int argc, const char** argv) override;
241 
242  const DispatchRecordF* const mDispatchTable;
243 };
244 
245 }
246 }
247 
248 #endif
CommandDispatcherF(StreamReader &streamReader, Print &printer, const DispatchRecordF *dispatchTable, uint8_t numCommands, const char **argv, uint8_t argvSize)
Constructor.
A CommandDispatcher that takes DispatchRecordF records using FlashStrings.
Reads tokens (lines, words, integers, characters, etc) from the Stream device.
Definition: StreamReader.h:39
A CommandDispatcher that takes DispatchRecordC records using C-strings.
Base-class of a coroutine that reads lines from the Serial port, tokenizes the line on whitespace bou...
A record of the command name and its handler.
CommandDispatcherC(StreamReader &streamReader, Print &printer, const DispatchRecordC *dispatchTable, uint8_t numCommands, const char **argv, uint8_t argvSize)
Constructor.
CommandDispatcher(StreamReader &streamReader, Print &printer, uint8_t numCommands, const char **argv, uint8_t argvSize)
Constructor.
Base class of all coroutines.
Definition: Coroutine.h:217
Same as DispatchRecordC but uses FlashStrings instead of (const char*) to save static RAM on AVR boar...