-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseCommand.cpp
127 lines (115 loc) · 4.04 KB
/
ParseCommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* ParseCommand - A Wiring/Arduino library to tokenize and parse commands
* received over a serial port.
*
* Copyright (C) 2012 Stefan Rado
* Copyright (C) 2011 Steven Cogswell <steven.cogswell@gmail.com>
* http://husks.wordpress.com
*
* Version 20120522
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ParseCommand.h"
/**
* Constructor makes sure some things are set.
*/
ParseCommand::ParseCommand()
: commandList(NULL),
commandCount(0),
defaultHandler(NULL),
term('\n'), // default terminator for commands, newline character
last(NULL)
{
strcpy(delim, " "); // strtok_r needs a null-terminated string
clearBuffer();
}
/**
* Adds a "command" and a handler function to the list of available commands.
* This is used for matching a found token in the buffer, and gives the pointer
* to the handler function to deal with it.
*/
void ParseCommand::addCommand(const char *command, void (*function)()) {
#ifdef PARSECOMMAND_DEBUG
Serial.print("Adding command (");
Serial.print(commandCount);
Serial.print("): ");
Serial.println(command);
#endif
commandList = (ParseCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(ParseCommandCallback));
strncpy(commandList[commandCount].command, command, PARSECOMMAND_MAXCOMMANDLENGTH);
commandList[commandCount].function = function;
commandCount++;
}
/**
* This sets up a handler to be called in the event that the receveived command string
* isn't in the list of commands.
*/
void ParseCommand::setDefaultHandler(void (*function)(const char *)) {
defaultHandler = function;
}
/**
* This checks the Serial stream for characters, and assembles them into a buffer.
* When the terminator character (default '\n') is seen, it starts parsing the
* buffer for a prefix command, and calls handlers setup by addCommand() member
*/
void ParseCommand::readCommand(const char * sbuffer) {
snprintf(buffer,sizeof(buffer),"%s",sbuffer);
#ifdef PARSECOMMAND_DEBUG
Serial.print("Received: ");
Serial.println(buffer);
#endif
char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
if (command != NULL) {
boolean matched = false;
for (int i = 0; i < commandCount; i++) {
#ifdef PARSECOMMAND_DEBUG
Serial.print("Comparing [");
Serial.print(command);
Serial.print("] to [");
Serial.print(commandList[i].command);
Serial.println("]");
#endif
// Compare the found command against the list of known commands for a match
if (strncmp(command, commandList[i].command, PARSECOMMAND_MAXCOMMANDLENGTH) == 0) {
#ifdef PARSECOMMAND_DEBUG
Serial.print("Matched Command: ");
Serial.println(command);
#endif
// Execute the stored handler function for the command
(*commandList[i].function)();
matched = true;
break;
}
}
if (!matched && (defaultHandler != NULL)) {
(*defaultHandler)(command);
}
}
clearBuffer();
}
/*
* Clear the input buffer.
*/
void ParseCommand::clearBuffer() {
buffer[0] = '\0';
bufPos = 0;
}
/**
* Retrieve the next token ("word" or "argument") from the command buffer.
* Returns NULL if no more tokens exist.
*/
char *ParseCommand::next() {
return strtok_r(NULL, delim, &last);
}