A light Command Dispatcher Library with commands linked to your functions.

Dependents:   Rocket

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);
}
Committer:
rominos2
Date:
Wed Sep 03 19:33:41 2014 +0000
Revision:
4:ca5d575c03c8
Parent:
3:8784fbd35d29
Add #ifndef on headers

Who changed what in which revision?

UserRevisionLine numberNew 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 4:ca5d575c03c8 20 #ifndef INCLUDE_COMMANDDISPATCHER_H
rominos2 4:ca5d575c03c8 21 #define INCLUDE_COMMANDDISPATCHER_H
rominos2 4:ca5d575c03c8 22
rominos2 3:8784fbd35d29 23 #include "mbed.h"
rominos2 1:855efbf6d7ae 24
rominos2 1:855efbf6d7ae 25 /** A light Command Dispatcher Library \n
rominos2 1:855efbf6d7ae 26 You can link commands to functions. \n
rominos2 1:855efbf6d7ae 27 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 28 Here is an quick example class that to show the purpose of this library.
rominos2 1:855efbf6d7ae 29 @code
rominos2 1:855efbf6d7ae 30 #include "mbed.h"
rominos2 1:855efbf6d7ae 31 #include "CommandDispatcher.h"
rominos2 1:855efbf6d7ae 32
rominos2 1:855efbf6d7ae 33 Serial pc(USBTX, USBRX);
rominos2 1:855efbf6d7ae 34
rominos2 1:855efbf6d7ae 35 void echoCommand(unsigned int argc, char* argv[], char* result);
rominos2 1:855efbf6d7ae 36
rominos2 1:855efbf6d7ae 37 int main() {
rominos2 1:855efbf6d7ae 38 CommandDispatcher disp = CommandDispatcher();
rominos2 1:855efbf6d7ae 39 char buffer[50];
rominos2 1:855efbf6d7ae 40 char result[50];
rominos2 1:855efbf6d7ae 41 int i=0;
rominos2 1:855efbf6d7ae 42
rominos2 1:855efbf6d7ae 43 disp.addCommand("echo", echoCommand);
rominos2 1:855efbf6d7ae 44 pc.printf("Example Command Dispatcher\n\n");
rominos2 1:855efbf6d7ae 45
rominos2 1:855efbf6d7ae 46 while(true) {
rominos2 1:855efbf6d7ae 47 buffer[i++] = pc.getc();
rominos2 1:855efbf6d7ae 48 if (buffer[i-1]=='\n') {
rominos2 1:855efbf6d7ae 49 buffer[i-1]='\0';
rominos2 1:855efbf6d7ae 50 i=0;
rominos2 1:855efbf6d7ae 51
rominos2 1:855efbf6d7ae 52 if (disp.executeCommand(buffer, result)) {
rominos2 1:855efbf6d7ae 53 pc.printf("%s\n", result);
rominos2 1:855efbf6d7ae 54 } else {
rominos2 1:855efbf6d7ae 55 pc.printf("Command not found.\n");
rominos2 1:855efbf6d7ae 56 }
rominos2 1:855efbf6d7ae 57 }
rominos2 1:855efbf6d7ae 58 }
rominos2 1:855efbf6d7ae 59 }
rominos2 1:855efbf6d7ae 60
rominos2 1:855efbf6d7ae 61 void echoCommand(unsigned int argc, char* argv[], char* result) {
rominos2 1:855efbf6d7ae 62 int i;
rominos2 1:855efbf6d7ae 63 sprintf(result, "");
rominos2 1:855efbf6d7ae 64 for (i=1; i<argc; i++) {
rominos2 1:855efbf6d7ae 65 sprintf(result, "%s %s", result, argv[i]);
rominos2 1:855efbf6d7ae 66 }
rominos2 1:855efbf6d7ae 67 sprintf(result, "%s\n", result);
rominos2 1:855efbf6d7ae 68 }
rominos2 1:855efbf6d7ae 69 @endcode
rominos2 1:855efbf6d7ae 70 */
rominos2 1:855efbf6d7ae 71 class CommandDispatcher {
rominos2 1:855efbf6d7ae 72 private:
rominos2 1:855efbf6d7ae 73 class Command {
rominos2 1:855efbf6d7ae 74 private:
rominos2 3:8784fbd35d29 75 char* _name;
rominos2 1:855efbf6d7ae 76 void(*_function)(unsigned int argc, char* argv[], char* result);
rominos2 1:855efbf6d7ae 77 Command* _next;
rominos2 1:855efbf6d7ae 78
rominos2 1:855efbf6d7ae 79 friend class CommandDispatcher;
rominos2 1:855efbf6d7ae 80 };
rominos2 1:855efbf6d7ae 81
rominos2 1:855efbf6d7ae 82 CommandDispatcher::Command* _first_command;
rominos2 1:855efbf6d7ae 83
rominos2 1:855efbf6d7ae 84 int parse_buffer(char* buffer, char*** argv);
rominos2 1:855efbf6d7ae 85
rominos2 1:855efbf6d7ae 86 public:
rominos2 1:855efbf6d7ae 87 /** Constructor of the Command Dispatcher
rominos2 1:855efbf6d7ae 88 */
rominos2 1:855efbf6d7ae 89 CommandDispatcher();
rominos2 1:855efbf6d7ae 90
rominos2 1:855efbf6d7ae 91 /** Add a command to the Dispatcher.
rominos2 1:855efbf6d7ae 92 @param commandName the name of the command. Not case sensitive.
rominos2 1:855efbf6d7ae 93 @param commandFunction the pointer to the function called when function is executed. \n
rominos2 1:855efbf6d7ae 94 The 2 first parameters are liked a C code main (number and value of arguments). \n
rominos2 1:855efbf6d7ae 95 The last (result) is got on command execution and transport a potential result to the executor. \n
rominos2 1:855efbf6d7ae 96 */
rominos2 1:855efbf6d7ae 97 void addCommand(char* commandName, void(*commandFunction)(unsigned int argc, char* argv[], char* result));
rominos2 1:855efbf6d7ae 98 /** Clean all the commands (free memory allocated).
rominos2 1:855efbf6d7ae 99 */
rominos2 1:855efbf6d7ae 100 void cleanCommands();
rominos2 1:855efbf6d7ae 101
rominos2 1:855efbf6d7ae 102 /** Execute a command. It will check each registered command with its name.
rominos2 1:855efbf6d7ae 103 @param command the command with the arguments in a single char array.
rominos2 1:855efbf6d7ae 104 @param result a char array alocated by the user and sent to the function for feedback use.
rominos2 2:695d21706b72 105 @return true if a registered command was found, else false.
rominos2 1:855efbf6d7ae 106 */
rominos2 1:855efbf6d7ae 107 bool executeCommand(char* command, char* result);
rominos2 1:855efbf6d7ae 108
rominos2 1:855efbf6d7ae 109 /** A Utility function to transform a char array to lower case (useful for argument checks). \n
rominos2 1:855efbf6d7ae 110 WARNING : This function modifies the word.
rominos2 1:855efbf6d7ae 111 @param word the word to modify.
rominos2 1:855efbf6d7ae 112 */
rominos2 1:855efbf6d7ae 113 static void toLowerCase(char* word);
rominos2 4:ca5d575c03c8 114 };
rominos2 4:ca5d575c03c8 115
rominos2 4:ca5d575c03c8 116 #endif