CommandProcessing/src/CommandProcessing/Handler.h
Go to the documentation of this file.
1 /*
2  * CommandHandler.h
3  *
4  * Created on: 2 jul. 2015
5  * Author: Herman
6  */
14 #pragma once
15 
16 #include <WVector.h>
19 #include <Data/Buffer/LineBuffer.h>
20 #include "Command.h"
21 
22 namespace CommandProcessing
23 {
24 constexpr size_t MAX_COMMANDSIZE = 64;
25 
27 class Handler
28 {
29 public:
34  {
35  }
36 
37  Handler(ReadWriteStream* stream, bool owned = true) : outputStream(stream), ownedStream(owned)
38  {
39  }
40 
41  Handler(const Handler& rhs) = delete;
42 
44  {
45  if(ownedStream) {
46  delete outputStream;
47  }
48  }
49 
50  // I/O methods
51 
57  void setOutputStream(ReadWriteStream* stream, bool owned = true)
58  {
59  if(ownedStream) {
60  delete outputStream;
61  }
62 
63  outputStream = stream;
64  ownedStream = owned;
65  }
66 
68  {
69  if(outputStream == nullptr) {
70  outputStream = new MemoryDataStream();
71  ownedStream = true;
72  }
73 
74  return *outputStream;
75  }
76 
77  size_t process(char charToWrite);
78 
84  size_t process(const char* buffer, size_t size)
85  {
86  size_t retval = 0;
87  for(size_t i = 0; i < size; i++) {
88  if(process(buffer[i]) != 1) {
89  break;
90  }
91  retval++;
92  }
93  return retval;
94  }
95 
103  String processNow(const char* buffer, size_t size);
104 
105  // Command registration/de-registration methods
106 
113  bool registerCommand(const Command& command);
114 
118  bool unregisterCommand(const Command& command);
119 
130 
135  Command getCommand(const String& name) const;
136 
140  bool isVerbose() const
141  {
142  return verboseMode;
143  }
144 
148  void setVerbose(bool mode)
149  {
150  verboseMode = mode;
151  }
152 
159 
165  void setCommandPrompt(const String& prompt)
166  {
167  this->prompt = prompt;
168  }
169 
176  {
177  return '\n';
178  }
179 
186  {
187  (void)eol;
188  }
189 
195 
201  void setCommandWelcomeMessage(const String& message)
202  {
203  welcomeMessage = message;
204  }
205 
206 private:
207  Vector<CommandDef> registeredCommands;
208  String prompt;
209  bool verboseMode{false};
210  String welcomeMessage;
211 
212  ReadWriteStream* outputStream{nullptr};
213  bool ownedStream = true;
214  LineBuffer<MAX_COMMANDSIZE> commandBuf;
215 
216  void processHelpCommand(String commandLine, ReadWriteStream& outputStream);
217  void processStatusCommand(String commandLine, ReadWriteStream& outputStream);
218  void processEchoCommand(String commandLine, ReadWriteStream& outputStream);
219  void processDebugOnCommand(String commandLine, ReadWriteStream& outputStream);
220  void processDebugOffCommand(String commandLine, ReadWriteStream& outputStream);
221  void processCommandOptions(String commandLine, ReadWriteStream& outputStream);
222 
223  void processCommandLine(const String& cmdString);
224 };
225 
226 } // namespace CommandProcessing
227 
CommandLine commandLine
Command delegate class.
Definition: CommandProcessing/src/CommandProcessing/Command.h:80
Command handler class.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:28
~Handler()
Definition: CommandProcessing/src/CommandProcessing/Handler.h:43
ReadWriteStream & getOutputStream()
Definition: CommandProcessing/src/CommandProcessing/Handler.h:67
void registerSystemCommands()
Register default system commands.
Handler(ReadWriteStream *stream, bool owned=true)
Definition: CommandProcessing/src/CommandProcessing/Handler.h:37
void setCommandWelcomeMessage(const String &message)
Set the welcome message.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:201
void setVerbose(bool mode)
Set the verbose mode.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:148
String processNow(const char *buffer, size_t size)
Process command input and return response text.
void setCommandPrompt(const String &prompt)
Set the command line prompt.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:165
bool isVerbose() const
Get the verbose mode.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:140
bool registerCommand(const Command &command)
Add a new command to the command handler.
Command getCommand(const String &name) const
Find command object.
char getCommandEOL() const
Get the end of line character.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:175
void setCommandEOL(char eol)
Set the end of line character.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:185
String getCommandPrompt() const
Get the command line prompt.
void setOutputStream(ReadWriteStream *stream, bool owned=true)
sets the output stream
Definition: CommandProcessing/src/CommandProcessing/Handler.h:57
Handler()
Instantiate a CommandHandler.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:33
size_t process(const char *buffer, size_t size)
Write chars to stream.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:84
String getCommandWelcomeMessage() const
Get the welcome message.
Handler(const Handler &rhs)=delete
bool unregisterCommand(const Command &command)
Remove a command from the command handler.
size_t process(char charToWrite)
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:27
Base class for read/write stream.
Definition: ReadWriteStream.h:20
The String class.
Definition: WString.h:137
Vector class template.
Definition: WVector.h:32
Definition: CommandProcessing/src/CommandProcessing/Command.h:30
constexpr size_t MAX_COMMANDSIZE
Definition: CommandProcessing/src/CommandProcessing/Handler.h:24
#define SMING_DEPRECATED
Definition: sming_attr.h:36