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