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); }
CommandDispatcher.h@3:8784fbd35d29, 2014-09-03 (annotated)
- Committer:
- rominos2
- Date:
- Wed Sep 03 10:53:38 2014 +0000
- Revision:
- 3:8784fbd35d29
- Parent:
- 2:695d21706b72
- Child:
- 4:ca5d575c03c8
Add License headers.; Remove some deprecated #define.; Optimize memory managment of Commands.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rominos2 | 3:8784fbd35d29 | 1 | /* |
rominos2 | 3:8784fbd35d29 | 2 | Copyright (c) 2014 Romain Berrada |
rominos2 | 3:8784fbd35d29 | 3 | |
rominos2 | 3:8784fbd35d29 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
rominos2 | 3:8784fbd35d29 | 5 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
rominos2 | 3:8784fbd35d29 | 6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, |
rominos2 | 3:8784fbd35d29 | 7 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
rominos2 | 3:8784fbd35d29 | 8 | furnished to do so, subject to the following conditions: |
rominos2 | 1:855efbf6d7ae | 9 | |
rominos2 | 3:8784fbd35d29 | 10 | The above copyright notice and this permission notice shall be included in all copies or |
rominos2 | 3:8784fbd35d29 | 11 | substantial portions of the Software. |
rominos2 | 3:8784fbd35d29 | 12 | |
rominos2 | 3:8784fbd35d29 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
rominos2 | 3:8784fbd35d29 | 14 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
rominos2 | 3:8784fbd35d29 | 15 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
rominos2 | 3:8784fbd35d29 | 16 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
rominos2 | 3:8784fbd35d29 | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
rominos2 | 3:8784fbd35d29 | 18 | */ |
rominos2 | 3:8784fbd35d29 | 19 | |
rominos2 | 3:8784fbd35d29 | 20 | #include "mbed.h" |
rominos2 | 1:855efbf6d7ae | 21 | |
rominos2 | 1:855efbf6d7ae | 22 | /** A light Command Dispatcher Library \n |
rominos2 | 1:855efbf6d7ae | 23 | You can link commands to functions. \n |
rominos2 | 1:855efbf6d7ae | 24 | On each execution of a command (char array), it will parse the array and send all the parameters to the functions. \n |
rominos2 | 1:855efbf6d7ae | 25 | Here is an quick example class that to show the purpose of this library. |
rominos2 | 1:855efbf6d7ae | 26 | @code |
rominos2 | 1:855efbf6d7ae | 27 | #include "mbed.h" |
rominos2 | 1:855efbf6d7ae | 28 | #include "CommandDispatcher.h" |
rominos2 | 1:855efbf6d7ae | 29 | |
rominos2 | 1:855efbf6d7ae | 30 | Serial pc(USBTX, USBRX); |
rominos2 | 1:855efbf6d7ae | 31 | |
rominos2 | 1:855efbf6d7ae | 32 | void echoCommand(unsigned int argc, char* argv[], char* result); |
rominos2 | 1:855efbf6d7ae | 33 | |
rominos2 | 1:855efbf6d7ae | 34 | int main() { |
rominos2 | 1:855efbf6d7ae | 35 | CommandDispatcher disp = CommandDispatcher(); |
rominos2 | 1:855efbf6d7ae | 36 | char buffer[50]; |
rominos2 | 1:855efbf6d7ae | 37 | char result[50]; |
rominos2 | 1:855efbf6d7ae | 38 | int i=0; |
rominos2 | 1:855efbf6d7ae | 39 | |
rominos2 | 1:855efbf6d7ae | 40 | disp.addCommand("echo", echoCommand); |
rominos2 | 1:855efbf6d7ae | 41 | pc.printf("Example Command Dispatcher\n\n"); |
rominos2 | 1:855efbf6d7ae | 42 | |
rominos2 | 1:855efbf6d7ae | 43 | while(true) { |
rominos2 | 1:855efbf6d7ae | 44 | buffer[i++] = pc.getc(); |
rominos2 | 1:855efbf6d7ae | 45 | if (buffer[i-1]=='\n') { |
rominos2 | 1:855efbf6d7ae | 46 | buffer[i-1]='\0'; |
rominos2 | 1:855efbf6d7ae | 47 | i=0; |
rominos2 | 1:855efbf6d7ae | 48 | |
rominos2 | 1:855efbf6d7ae | 49 | if (disp.executeCommand(buffer, result)) { |
rominos2 | 1:855efbf6d7ae | 50 | pc.printf("%s\n", result); |
rominos2 | 1:855efbf6d7ae | 51 | } else { |
rominos2 | 1:855efbf6d7ae | 52 | pc.printf("Command not found.\n"); |
rominos2 | 1:855efbf6d7ae | 53 | } |
rominos2 | 1:855efbf6d7ae | 54 | } |
rominos2 | 1:855efbf6d7ae | 55 | } |
rominos2 | 1:855efbf6d7ae | 56 | } |
rominos2 | 1:855efbf6d7ae | 57 | |
rominos2 | 1:855efbf6d7ae | 58 | void echoCommand(unsigned int argc, char* argv[], char* result) { |
rominos2 | 1:855efbf6d7ae | 59 | int i; |
rominos2 | 1:855efbf6d7ae | 60 | sprintf(result, ""); |
rominos2 | 1:855efbf6d7ae | 61 | for (i=1; i<argc; i++) { |
rominos2 | 1:855efbf6d7ae | 62 | sprintf(result, "%s %s", result, argv[i]); |
rominos2 | 1:855efbf6d7ae | 63 | } |
rominos2 | 1:855efbf6d7ae | 64 | sprintf(result, "%s\n", result); |
rominos2 | 1:855efbf6d7ae | 65 | } |
rominos2 | 1:855efbf6d7ae | 66 | @endcode |
rominos2 | 1:855efbf6d7ae | 67 | */ |
rominos2 | 1:855efbf6d7ae | 68 | class CommandDispatcher { |
rominos2 | 1:855efbf6d7ae | 69 | private: |
rominos2 | 1:855efbf6d7ae | 70 | class Command { |
rominos2 | 1:855efbf6d7ae | 71 | private: |
rominos2 | 3:8784fbd35d29 | 72 | char* _name; |
rominos2 | 1:855efbf6d7ae | 73 | void(*_function)(unsigned int argc, char* argv[], char* result); |
rominos2 | 1:855efbf6d7ae | 74 | Command* _next; |
rominos2 | 1:855efbf6d7ae | 75 | |
rominos2 | 1:855efbf6d7ae | 76 | friend class CommandDispatcher; |
rominos2 | 1:855efbf6d7ae | 77 | }; |
rominos2 | 1:855efbf6d7ae | 78 | |
rominos2 | 1:855efbf6d7ae | 79 | CommandDispatcher::Command* _first_command; |
rominos2 | 1:855efbf6d7ae | 80 | |
rominos2 | 1:855efbf6d7ae | 81 | int parse_buffer(char* buffer, char*** argv); |
rominos2 | 1:855efbf6d7ae | 82 | |
rominos2 | 1:855efbf6d7ae | 83 | public: |
rominos2 | 1:855efbf6d7ae | 84 | /** Constructor of the Command Dispatcher |
rominos2 | 1:855efbf6d7ae | 85 | */ |
rominos2 | 1:855efbf6d7ae | 86 | CommandDispatcher(); |
rominos2 | 1:855efbf6d7ae | 87 | |
rominos2 | 1:855efbf6d7ae | 88 | /** Add a command to the Dispatcher. |
rominos2 | 1:855efbf6d7ae | 89 | @param commandName the name of the command. Not case sensitive. |
rominos2 | 1:855efbf6d7ae | 90 | @param commandFunction the pointer to the function called when function is executed. \n |
rominos2 | 1:855efbf6d7ae | 91 | The 2 first parameters are liked a C code main (number and value of arguments). \n |
rominos2 | 1:855efbf6d7ae | 92 | The last (result) is got on command execution and transport a potential result to the executor. \n |
rominos2 | 1:855efbf6d7ae | 93 | */ |
rominos2 | 1:855efbf6d7ae | 94 | void addCommand(char* commandName, void(*commandFunction)(unsigned int argc, char* argv[], char* result)); |
rominos2 | 1:855efbf6d7ae | 95 | /** Clean all the commands (free memory allocated). |
rominos2 | 1:855efbf6d7ae | 96 | */ |
rominos2 | 1:855efbf6d7ae | 97 | void cleanCommands(); |
rominos2 | 1:855efbf6d7ae | 98 | |
rominos2 | 1:855efbf6d7ae | 99 | /** Execute a command. It will check each registered command with its name. |
rominos2 | 1:855efbf6d7ae | 100 | @param command the command with the arguments in a single char array. |
rominos2 | 1:855efbf6d7ae | 101 | @param result a char array alocated by the user and sent to the function for feedback use. |
rominos2 | 2:695d21706b72 | 102 | @return true if a registered command was found, else false. |
rominos2 | 1:855efbf6d7ae | 103 | */ |
rominos2 | 1:855efbf6d7ae | 104 | bool executeCommand(char* command, char* result); |
rominos2 | 1:855efbf6d7ae | 105 | |
rominos2 | 1:855efbf6d7ae | 106 | /** A Utility function to transform a char array to lower case (useful for argument checks). \n |
rominos2 | 1:855efbf6d7ae | 107 | WARNING : This function modifies the word. |
rominos2 | 1:855efbf6d7ae | 108 | @param word the word to modify. |
rominos2 | 1:855efbf6d7ae | 109 | */ |
rominos2 | 1:855efbf6d7ae | 110 | static void toLowerCase(char* word); |
rominos2 | 1:855efbf6d7ae | 111 | }; |