A light Command Dispatcher Library with commands linked to your functions.
You can register your commands and the functions linked.
On each execution of a command (char array), it will parse the array and send all the parameters to the functions.
Here is a quick example :
#include "mbed.h" #include "CommandDispatcher.h" Serial pc(USBTX, USBRX); void echoCommand(unsigned int argc, char* argv[], char* result); int main() { CommandDispatcher disp = CommandDispatcher(); char buffer[50]; char result[50]; int i=0; // register a command disp.addCommand("echo", echoCommand); pc.printf("Example Command Dispatcher\n\n"); while(true) { // get a complete line from serial buffer[i++] = pc.getc(); if (buffer[i-1]=='\n') { buffer[i-1]='\0'; i=0; // send it to the dispatcher and print result if (disp.executeCommand(buffer, result)) { pc.printf("%s\n", result); } else { pc.printf("Command not found.\n"); } } } } // the actual function called void echoCommand(unsigned int argc, char* argv[], char* result) { int i; sprintf(result, ""); for (i=1; i<argc; i++) { sprintf(result, "%s %s", result, argv[i]); } sprintf(result, "%s\n", result); }
CommandServer.cpp@0:1bfd6f4c0dbb, 2014-09-02 (annotated)
- Committer:
- rominos2
- Date:
- Tue Sep 02 22:07:18 2014 +0000
- Revision:
- 0:1bfd6f4c0dbb
- Child:
- 1:855efbf6d7ae
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rominos2 | 0:1bfd6f4c0dbb | 1 | #include "mbed.h" |
rominos2 | 0:1bfd6f4c0dbb | 2 | #include "CommandServer.h" |
rominos2 | 0:1bfd6f4c0dbb | 3 | |
rominos2 | 0:1bfd6f4c0dbb | 4 | extern Serial pc; |
rominos2 | 0:1bfd6f4c0dbb | 5 | |
rominos2 | 0:1bfd6f4c0dbb | 6 | CommandServer::CommandServer() : _first_command(NULL) { |
rominos2 | 0:1bfd6f4c0dbb | 7 | } |
rominos2 | 0:1bfd6f4c0dbb | 8 | |
rominos2 | 0:1bfd6f4c0dbb | 9 | void CommandServer::addCommand(char* commandName, void(*command_function)(int argc, char* argv[], char* result)) { |
rominos2 | 0:1bfd6f4c0dbb | 10 | CommandServer::Command* com = new CommandServer::Command(); |
rominos2 | 0:1bfd6f4c0dbb | 11 | strcpy(com->_name, commandName); |
rominos2 | 0:1bfd6f4c0dbb | 12 | com->_function = command_function; |
rominos2 | 0:1bfd6f4c0dbb | 13 | com->_next = _first_command; |
rominos2 | 0:1bfd6f4c0dbb | 14 | _first_command = com; |
rominos2 | 0:1bfd6f4c0dbb | 15 | } |
rominos2 | 0:1bfd6f4c0dbb | 16 | |
rominos2 | 0:1bfd6f4c0dbb | 17 | void CommandServer::cleanCommands() { |
rominos2 | 0:1bfd6f4c0dbb | 18 | CommandServer::Command* com = _first_command; |
rominos2 | 0:1bfd6f4c0dbb | 19 | while (com!=NULL) { |
rominos2 | 0:1bfd6f4c0dbb | 20 | _first_command = com->_next; |
rominos2 | 0:1bfd6f4c0dbb | 21 | delete com; |
rominos2 | 0:1bfd6f4c0dbb | 22 | } |
rominos2 | 0:1bfd6f4c0dbb | 23 | } |
rominos2 | 0:1bfd6f4c0dbb | 24 | |
rominos2 | 0:1bfd6f4c0dbb | 25 | bool CommandServer::executeCommand(char* command, char* result) { |
rominos2 | 0:1bfd6f4c0dbb | 26 | CommandServer::Command* command_ptr; |
rominos2 | 0:1bfd6f4c0dbb | 27 | bool function_return = false; |
rominos2 | 0:1bfd6f4c0dbb | 28 | int argc; |
rominos2 | 0:1bfd6f4c0dbb | 29 | char** argv; |
rominos2 | 0:1bfd6f4c0dbb | 30 | char* buffer; |
rominos2 | 0:1bfd6f4c0dbb | 31 | |
rominos2 | 0:1bfd6f4c0dbb | 32 | buffer = new char[strlen(command)+1]; // + 1 for the \0 |
rominos2 | 0:1bfd6f4c0dbb | 33 | strcpy(buffer, command); |
rominos2 | 0:1bfd6f4c0dbb | 34 | |
rominos2 | 0:1bfd6f4c0dbb | 35 | argc = parse_buffer(buffer, &argv); |
rominos2 | 0:1bfd6f4c0dbb | 36 | toLowerCase(argv[0]); |
rominos2 | 0:1bfd6f4c0dbb | 37 | |
rominos2 | 0:1bfd6f4c0dbb | 38 | for (command_ptr=_first_command; command_ptr!=NULL; command_ptr = command_ptr->_next) { |
rominos2 | 0:1bfd6f4c0dbb | 39 | if (strcmp(argv[0], command_ptr->_name)==0) { |
rominos2 | 0:1bfd6f4c0dbb | 40 | command_ptr->_function(argc, argv, result); |
rominos2 | 0:1bfd6f4c0dbb | 41 | function_return = true; |
rominos2 | 0:1bfd6f4c0dbb | 42 | break; |
rominos2 | 0:1bfd6f4c0dbb | 43 | } |
rominos2 | 0:1bfd6f4c0dbb | 44 | } |
rominos2 | 0:1bfd6f4c0dbb | 45 | |
rominos2 | 0:1bfd6f4c0dbb | 46 | delete[] argv; |
rominos2 | 0:1bfd6f4c0dbb | 47 | delete[] buffer; |
rominos2 | 0:1bfd6f4c0dbb | 48 | return function_return; |
rominos2 | 0:1bfd6f4c0dbb | 49 | } |
rominos2 | 0:1bfd6f4c0dbb | 50 | |
rominos2 | 0:1bfd6f4c0dbb | 51 | int CommandServer::parse_buffer(char* buffer, char*** argv) { |
rominos2 | 0:1bfd6f4c0dbb | 52 | int i; |
rominos2 | 0:1bfd6f4c0dbb | 53 | int word_count; |
rominos2 | 0:1bfd6f4c0dbb | 54 | int letter_found; |
rominos2 | 0:1bfd6f4c0dbb | 55 | int buffer_size = strlen(buffer); |
rominos2 | 0:1bfd6f4c0dbb | 56 | int argc; |
rominos2 | 0:1bfd6f4c0dbb | 57 | char** argv_tab; |
rominos2 | 0:1bfd6f4c0dbb | 58 | |
rominos2 | 0:1bfd6f4c0dbb | 59 | // 1st cycle, counting the number of words |
rominos2 | 0:1bfd6f4c0dbb | 60 | letter_found = 0; |
rominos2 | 0:1bfd6f4c0dbb | 61 | word_count = 0; |
rominos2 | 0:1bfd6f4c0dbb | 62 | for (i=0; i<buffer_size; i++) { |
rominos2 | 0:1bfd6f4c0dbb | 63 | if (buffer[i]==' ' && letter_found) { |
rominos2 | 0:1bfd6f4c0dbb | 64 | word_count++; |
rominos2 | 0:1bfd6f4c0dbb | 65 | letter_found=0; |
rominos2 | 0:1bfd6f4c0dbb | 66 | } else letter_found=1; |
rominos2 | 0:1bfd6f4c0dbb | 67 | } |
rominos2 | 0:1bfd6f4c0dbb | 68 | if (letter_found!=0) word_count++; // to get last word |
rominos2 | 0:1bfd6f4c0dbb | 69 | |
rominos2 | 0:1bfd6f4c0dbb | 70 | // create the array of words |
rominos2 | 0:1bfd6f4c0dbb | 71 | argv_tab = new char*[word_count]; |
rominos2 | 0:1bfd6f4c0dbb | 72 | argc = word_count; |
rominos2 | 0:1bfd6f4c0dbb | 73 | |
rominos2 | 0:1bfd6f4c0dbb | 74 | // 2nd cycle, putting words into the array |
rominos2 | 0:1bfd6f4c0dbb | 75 | letter_found = 0; |
rominos2 | 0:1bfd6f4c0dbb | 76 | word_count = 0; |
rominos2 | 0:1bfd6f4c0dbb | 77 | for (i=0; i<buffer_size; i++) { |
rominos2 | 0:1bfd6f4c0dbb | 78 | if (buffer[i]==' ') { |
rominos2 | 0:1bfd6f4c0dbb | 79 | buffer[i] = '\0'; |
rominos2 | 0:1bfd6f4c0dbb | 80 | letter_found = 0; |
rominos2 | 0:1bfd6f4c0dbb | 81 | } else if (!letter_found) { |
rominos2 | 0:1bfd6f4c0dbb | 82 | argv_tab[word_count++]=&buffer[i]; |
rominos2 | 0:1bfd6f4c0dbb | 83 | letter_found=1; |
rominos2 | 0:1bfd6f4c0dbb | 84 | } |
rominos2 | 0:1bfd6f4c0dbb | 85 | } |
rominos2 | 0:1bfd6f4c0dbb | 86 | |
rominos2 | 0:1bfd6f4c0dbb | 87 | *argv = argv_tab; |
rominos2 | 0:1bfd6f4c0dbb | 88 | return argc; |
rominos2 | 0:1bfd6f4c0dbb | 89 | } |
rominos2 | 0:1bfd6f4c0dbb | 90 | |
rominos2 | 0:1bfd6f4c0dbb | 91 | void CommandServer::toLowerCase(char* word) { |
rominos2 | 0:1bfd6f4c0dbb | 92 | int i; |
rominos2 | 0:1bfd6f4c0dbb | 93 | for (i=0; i<strlen(word); i++) { |
rominos2 | 0:1bfd6f4c0dbb | 94 | if (word[i]>=65 && word[i]<=90) { // if between A-Z |
rominos2 | 0:1bfd6f4c0dbb | 95 | word[i] += 32; |
rominos2 | 0:1bfd6f4c0dbb | 96 | } |
rominos2 | 0:1bfd6f4c0dbb | 97 | } |
rominos2 | 0:1bfd6f4c0dbb | 98 | } |