AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
CommandDispatcher.cpp
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 #include "../Flash.h"
26 #include "CommandDispatcher.h"
27 
28 namespace ace_routine {
29 namespace cli {
30 
31 const char CommandDispatcher::DELIMS[] = " \t\n";
32 
33 void CommandDispatcher::printLineError(const char* line, uint8_t statusCode) {
34  if (statusCode == STATUS_BUFFER_OVERFLOW) {
35  mPrinter.print(F("BufferOverflow: "));
36  mPrinter.println(line);
37  } else if (statusCode == STATUS_FLUSH_TO_EOL) {
38  mPrinter.print(F("FlushToEOL: "));
39  mPrinter.println(line);
40  } else {
41  mPrinter.print(F("UnknownError: "));
42  mPrinter.print(statusCode);
43  mPrinter.print(F(": "));
44  mPrinter.println(line);
45  }
46 }
47 
50  Print& printer, int argc, const char** argv) {
51  if (argc == 2) {
52  const char* cmd = argv[1];
53  if (strcmp(cmd, "help") == 0) {
54  printer.println(F("Usage: help [command]"));
55  return;
56  }
57 
58  bool found = helpSpecific(printer, cmd);
59  if (found) return;
60  printer.print(F("Unknown command: "));
61  printer.println(cmd);
62  } else {
63  printer.println(F("Usage: help [command]"));
64  printer.print(F("Commands: help "));
65  helpGeneric(printer);
66  }
67 }
68 
71  // Tokenize the line.
72  int argc = tokenize(line, mArgv, mArgvSize);
73  if (argc == 0) return;
74  const char* cmd = mArgv[0];
75 
76  // Handle the built-in 'help' command.
77  if (strcmp(cmd, "help") == 0) {
78  helpCommandHandler(mPrinter, argc, mArgv);
79  return;
80  }
81 
82  findAndRunCommand(cmd, argc, mArgv);
83 }
84 
85 uint8_t CommandDispatcher::tokenize(char* line, const char** argv,
86  uint8_t argvSize) {
87  char* token = strtok(line, DELIMS);
88  int argc = 0;
89  while (token != nullptr && argc < argvSize) {
90  argv[argc] = token;
91  argc++;
92  token = strtok(nullptr, DELIMS);
93  }
94  return argc;
95 }
96 
98  bool isError;
99  char* line;
100  COROUTINE_LOOP() {
101  COROUTINE_AWAIT(mStreamReader.getLine(&isError, &line));
102 
103  if (isError) {
104  printLineError(line, STATUS_BUFFER_OVERFLOW);
105  while (isError) {
106  COROUTINE_AWAIT(mStreamReader.getLine(&isError, &line));
107  printLineError(line, STATUS_FLUSH_TO_EOL);
108  }
109  continue;
110  }
111 
112  runCommand(line);
113  }
114 }
115 
116 //---------------------------------------------------------------------------
117 
118 void CommandDispatcherC::helpGeneric(Print& printer) {
119  for (uint8_t i = 0; i < mNumCommands; i++) {
120  const DispatchRecordC* record = &mDispatchTable[i];
121  printer.print(record->name);
122  printer.print(' ');
123  }
124  printer.println();
125 }
126 
127 bool CommandDispatcherC::helpSpecific(Print& printer, const char* cmd) {
128  const DispatchRecordC* record =
129  findCommand(mDispatchTable, mNumCommands, cmd);
130  if (record != nullptr) {
131  printer.print(F("Usage: "));
132  printer.print(cmd);
133  printer.print(' ');
134  printer.println(record->helpString);
135  return true;
136  }
137  return false;
138 }
139 
140 void CommandDispatcherC::findAndRunCommand(
141  const char* cmd, int argc, const char** argv) {
142  const DispatchRecordC* record =
143  findCommand(mDispatchTable, mNumCommands, cmd);
144  if (record != nullptr) {
145  record->command(mPrinter, argc, argv);
146  return;
147  }
148 
149  mPrinter.print(F("Unknown command: "));
150  mPrinter.println(cmd);
151 }
152 
154  const DispatchRecordC* dispatchTable,
155  uint8_t numCommands, const char* cmd) {
156  for (uint8_t i = 0; i < numCommands; i++) {
157  const DispatchRecordC* record = &dispatchTable[i];
158  if (strcmp(cmd, record->name) == 0) {
159  return record;
160  }
161  }
162  return nullptr;
163 }
164 
165 //---------------------------------------------------------------------------
166 
167 void CommandDispatcherF::helpGeneric(Print& printer) {
168  for (uint8_t i = 0; i < mNumCommands; i++) {
169  const DispatchRecordF* record = &mDispatchTable[i];
170  printer.print(record->name);
171  printer.print(' ');
172  }
173  printer.println();
174 }
175 
176 bool CommandDispatcherF::helpSpecific(Print& printer, const char* cmd) {
177  const DispatchRecordF* record =
178  findCommand(mDispatchTable, mNumCommands, cmd);
179  if (record != nullptr) {
180  printer.print(F("Usage: "));
181  printer.print(cmd);
182  printer.print(' ');
183  printer.println(record->helpString);
184  return true;
185  }
186  return false;
187 }
188 
189 void CommandDispatcherF::findAndRunCommand(
190  const char* cmd, int argc, const char** argv) {
191  const DispatchRecordF* record =
192  findCommand(mDispatchTable, mNumCommands, cmd);
193  if (record != nullptr) {
194  record->command(mPrinter, argc, argv);
195  return;
196  }
197 
198  mPrinter.print(F("Unknown command: "));
199  mPrinter.println(cmd);
200 }
201 
203  const DispatchRecordF* dispatchTableF,
204  uint8_t numCommands, const char* cmd) {
205  for (uint8_t i = 0; i < numCommands; i++) {
206  const DispatchRecordF* record = &dispatchTableF[i];
207  if (strcmp_P(cmd, (const char*) record->name) == 0) {
208  return record;
209  }
210  }
211  return nullptr;
212 }
213 
214 }
215 }
#define COROUTINE_AWAIT(condition)
Yield until condition is true, then execution continues.
Definition: Coroutine.h:164
void runCommand(char *line)
Tokenize the given line and run the command handler.
void helpCommandHandler(Print &printer, int argc, const char **argv)
Handle the &#39;help&#39; command.
bool getLine(bool *isError, char **line)
Get a line.
static const DispatchRecordF * findCommand(const DispatchRecordF *dispatchTable, uint8_t numCommands, const char *cmd)
Same as findCommand() except use DispatchTableF instead of DispatchTable.
#define COROUTINE_LOOP()
Mark the beginning of a coroutine loop.
Definition: Coroutine.h:132
static uint8_t tokenize(char *line, const char **argv, uint8_t argvSize)
Tokenize the line, and fill argv with each token until argvSize is reached.
static const DispatchRecordC * findCommand(const DispatchRecordC *dispatchTable, uint8_t numCommands, const char *cmd)
Find the CommandHandler of the given command name.
virtual void findAndRunCommand(const char *cmd, int argc, const char **argv)=0
Find and run the given command.
virtual int run() override
The body of the coroutine.
A record of the command name and its handler.
virtual bool helpSpecific(Print &printer, const char *cmd)=0
Print helpString of specific cmd.
Same as DispatchRecordC but uses FlashStrings instead of (const char*) to save static RAM on AVR boar...
virtual void helpGeneric(Print &printer)=0
Print generic help.
void printLineError(const char *line, uint8_t statusCode)
Print the error caused by the given line.