Utility to manage ASCII communications. Register a header and event and then pass messages into the class and events are generated on a match

Dependents:   Waldo_Embed_V2

Example

#include "mbed.h"
#include "CommHandler.h"
  
DigitalOut myled(LED1);
CommHandler msgs;
  
char *one(char* msg)
{
    // you can parse msg here
    LOG("\n");
    return msg;
}
  
class Wrap
{
public:
    Wrap(){}
    char *two(char *msg)
    {
        // you can parse msg here
        LOG("\n");
        return msg;
    }
}obj;
  
char *three(char* msg)
{
    // you can parse msg here
    LOG("\n");
    return msg;
}
  
int main()
{
    char *tmp = 0;
  
    msgs.attachMsg("One", &one);
    msgs.attachMsg("Two", &obj, &Wrap::two);
    msgs.attachMsg("Three", &three);
      
    tmp = msgs.messageLookup(0);
    printf("0:%s\n", tmp);
    tmp = msgs.messageLookup(1);
    printf("1:%s\n", tmp);
    tmp = msgs.messageLookup(2);
    printf("2:%s\n", tmp);
    tmp = msgs.messageLookup(3);
    printf("3:%s\n", tmp);
    tmp = msgs.messageLookup(4);
    printf("4:%s\n", tmp);
         
    tmp = msgs.serviceMessage("Two-00-66-99-20133");
    printf("1: Found: %s\n", tmp);
    tmp = msgs.serviceMessage("One-99-60-1-339788354");
    printf("2: Found: %s\n", tmp);
    tmp = msgs.serviceMessage("Three-xx-xx-XX-XXXXXXX");
    printf("3: Found: %s\n", tmp);
      
    error("End of Test\n");
}
Committer:
sam_grove
Date:
Wed May 22 19:37:22 2013 +0000
Revision:
9:1a76d4848462
Parent:
4:5d54100399b1
Removed an un-necessary cast. Important to note that this currently should be used in global scope. Objects that are added to the underlying linked list do not have a mechanism to be deleted. Therefore a memory leak would occur if used in local scope

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:cd84204f23d8 1 /**
sam_grove 0:cd84204f23d8 2 * @file CommHandler.h
sam_grove 0:cd84204f23d8 3 * @brief Core Utility - Manage ASCII communication between devices
sam_grove 0:cd84204f23d8 4 * @author sam grove
sam_grove 0:cd84204f23d8 5 * @version 1.0
sam_grove 0:cd84204f23d8 6 * @see
sam_grove 0:cd84204f23d8 7 *
sam_grove 0:cd84204f23d8 8 * Copyright (c) 2013
sam_grove 0:cd84204f23d8 9 *
sam_grove 0:cd84204f23d8 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:cd84204f23d8 11 * you may not use this file except in compliance with the License.
sam_grove 0:cd84204f23d8 12 * You may obtain a copy of the License at
sam_grove 0:cd84204f23d8 13 *
sam_grove 0:cd84204f23d8 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:cd84204f23d8 15 *
sam_grove 0:cd84204f23d8 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:cd84204f23d8 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:cd84204f23d8 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:cd84204f23d8 19 * See the License for the specific language governing permissions and
sam_grove 0:cd84204f23d8 20 * limitations under the License.
sam_grove 0:cd84204f23d8 21 */
sam_grove 0:cd84204f23d8 22
sam_grove 0:cd84204f23d8 23 #ifndef COMMHANDLER_H
sam_grove 0:cd84204f23d8 24 #define COMMHANDLER_H
sam_grove 0:cd84204f23d8 25
sam_grove 0:cd84204f23d8 26 #include "FP.h"
sam_grove 0:cd84204f23d8 27 #include "LinkedList.h"
sam_grove 4:5d54100399b1 28 #include "LogUtil.h"
sam_grove 0:cd84204f23d8 29
sam_grove 0:cd84204f23d8 30 /** Example using the CommHandler class
sam_grove 0:cd84204f23d8 31 * @code
sam_grove 0:cd84204f23d8 32 * #include "mbed.h"
sam_grove 0:cd84204f23d8 33 * #include "CommHandler.h"
sam_grove 0:cd84204f23d8 34 *
sam_grove 0:cd84204f23d8 35 * DigitalOut myled(LED1);
sam_grove 0:cd84204f23d8 36 * CommHandler msgs;
sam_grove 0:cd84204f23d8 37 *
sam_grove 0:cd84204f23d8 38 * char *one(char* msg)
sam_grove 0:cd84204f23d8 39 * {
sam_grove 0:cd84204f23d8 40 * // you can parse msg here
sam_grove 0:cd84204f23d8 41 * LOG("\n");
sam_grove 0:cd84204f23d8 42 * return msg;
sam_grove 0:cd84204f23d8 43 * }
sam_grove 0:cd84204f23d8 44 *
sam_grove 0:cd84204f23d8 45 * class Wrap
sam_grove 0:cd84204f23d8 46 * {
sam_grove 0:cd84204f23d8 47 * public:
sam_grove 0:cd84204f23d8 48 * Wrap(){}
sam_grove 0:cd84204f23d8 49 * char *two(char *msg)
sam_grove 0:cd84204f23d8 50 * {
sam_grove 0:cd84204f23d8 51 * // you can parse msg here
sam_grove 0:cd84204f23d8 52 * LOG("\n");
sam_grove 0:cd84204f23d8 53 * return msg;
sam_grove 0:cd84204f23d8 54 * }
sam_grove 0:cd84204f23d8 55 * }obj;
sam_grove 0:cd84204f23d8 56 *
sam_grove 0:cd84204f23d8 57 *
sam_grove 0:cd84204f23d8 58 * char *three(char* msg)
sam_grove 0:cd84204f23d8 59 * {
sam_grove 0:cd84204f23d8 60 * // you can parse msg here
sam_grove 0:cd84204f23d8 61 * LOG("\n");
sam_grove 0:cd84204f23d8 62 * return msg;
sam_grove 0:cd84204f23d8 63 * }
sam_grove 0:cd84204f23d8 64 *
sam_grove 0:cd84204f23d8 65 * int main()
sam_grove 0:cd84204f23d8 66 * {
sam_grove 0:cd84204f23d8 67 * char *tmp = 0;
sam_grove 0:cd84204f23d8 68 *
sam_grove 0:cd84204f23d8 69 * msgs.attachMsg("One", &one);
sam_grove 0:cd84204f23d8 70 * msgs.attachMsg("Two", &obj, &Wrap::two);
sam_grove 0:cd84204f23d8 71 * msgs.attachMsg("Three", &three);
sam_grove 0:cd84204f23d8 72 *
sam_grove 0:cd84204f23d8 73 * tmp = msgs.messageLookup(0);
sam_grove 9:1a76d4848462 74 * printf("0:%s\n", tmp);
sam_grove 0:cd84204f23d8 75 * tmp = msgs.messageLookup(1);
sam_grove 9:1a76d4848462 76 * printf("1:%s\n", tmp);
sam_grove 9:1a76d4848462 77 * tmp = msgs.messageLookup(2);
sam_grove 0:cd84204f23d8 78 * printf("2:%s\n", tmp);
sam_grove 9:1a76d4848462 79 * tmp = msgs.messageLookup(3);
sam_grove 0:cd84204f23d8 80 * printf("3:%s\n", tmp);
sam_grove 9:1a76d4848462 81 * tmp = msgs.messageLookup(4);
sam_grove 0:cd84204f23d8 82 * printf("4:%s\n", tmp);
sam_grove 0:cd84204f23d8 83 *
sam_grove 0:cd84204f23d8 84 * tmp = msgs.serviceMessage("Two-00-66-99-20133");
sam_grove 0:cd84204f23d8 85 * printf("1: Found: %s\n", tmp);
sam_grove 0:cd84204f23d8 86 * tmp = msgs.serviceMessage("One-99-60-1-339788354");
sam_grove 0:cd84204f23d8 87 * printf("2: Found: %s\n", tmp);
sam_grove 0:cd84204f23d8 88 * tmp = msgs.serviceMessage("Three-xx-xx-XX-XXXXXXX");
sam_grove 0:cd84204f23d8 89 * printf("3: Found: %s\n", tmp);
sam_grove 0:cd84204f23d8 90 *
sam_grove 0:cd84204f23d8 91 * error("End of Test\n");
sam_grove 0:cd84204f23d8 92 * }
sam_grove 0:cd84204f23d8 93 * @endcode
sam_grove 0:cd84204f23d8 94 */
sam_grove 0:cd84204f23d8 95
sam_grove 0:cd84204f23d8 96 /**
sam_grove 0:cd84204f23d8 97 * @class CommHandler
sam_grove 0:cd84204f23d8 98 * @brief API abstraction for managing device to device communication
sam_grove 0:cd84204f23d8 99 */
sam_grove 0:cd84204f23d8 100 class CommHandler
sam_grove 0:cd84204f23d8 101 {
sam_grove 0:cd84204f23d8 102 private:
sam_grove 0:cd84204f23d8 103 LinkedList <node>_list;
sam_grove 0:cd84204f23d8 104
sam_grove 0:cd84204f23d8 105 public:
sam_grove 0:cd84204f23d8 106
sam_grove 0:cd84204f23d8 107 /**
sam_grove 0:cd84204f23d8 108 * @struct MsgObject
sam_grove 0:cd84204f23d8 109 * @brief An object to store in the linked list
sam_grove 0:cd84204f23d8 110 */
sam_grove 0:cd84204f23d8 111 struct MsgObj
sam_grove 0:cd84204f23d8 112 {
sam_grove 0:cd84204f23d8 113 char *string; /*!< The header we are going to match */
sam_grove 0:cd84204f23d8 114 FP<char *, char *>handler; /*!< The function to call when a match is found */
sam_grove 0:cd84204f23d8 115 };
sam_grove 0:cd84204f23d8 116
sam_grove 0:cd84204f23d8 117 /** Create the CommHandler object
sam_grove 0:cd84204f23d8 118 */
sam_grove 0:cd84204f23d8 119 CommHandler();
sam_grove 0:cd84204f23d8 120
sam_grove 0:cd84204f23d8 121 /** Attach a member function as the match handler
sam_grove 0:cd84204f23d8 122 * @param string - The string that we're trying to match
sam_grove 0:cd84204f23d8 123 * @param item - Address of a initialized object
sam_grove 0:cd84204f23d8 124 * @param member - Address of the initialized object's member function
sam_grove 0:cd84204f23d8 125 */
sam_grove 0:cd84204f23d8 126 template<class T>
sam_grove 0:cd84204f23d8 127 void attachMsg( char *string, T *item, char*(T::*method)(char *) )
sam_grove 0:cd84204f23d8 128 {
sam_grove 0:cd84204f23d8 129 MsgObj *new_node = new MsgObj [1];
sam_grove 0:cd84204f23d8 130 // make sure the new object was allocated
sam_grove 0:cd84204f23d8 131 if (NULL == new_node)
sam_grove 0:cd84204f23d8 132 {
sam_grove 0:cd84204f23d8 133 ERROR("Memory Allocation Failed\n");
sam_grove 0:cd84204f23d8 134 }
sam_grove 0:cd84204f23d8 135 // store the user parameters
sam_grove 0:cd84204f23d8 136 new_node->string = string;
sam_grove 0:cd84204f23d8 137 new_node->handler.attach( item, method );
sam_grove 0:cd84204f23d8 138 // and insert them into the list
sam_grove 0:cd84204f23d8 139 _list.append(new_node);
sam_grove 0:cd84204f23d8 140 return;
sam_grove 0:cd84204f23d8 141 }
sam_grove 0:cd84204f23d8 142
sam_grove 0:cd84204f23d8 143 /** Attach a global function as the match handler
sam_grove 0:cd84204f23d8 144 * @param string - The string that we're trying to match
sam_grove 0:cd84204f23d8 145 * @param function - Address of the global function
sam_grove 0:cd84204f23d8 146 */
sam_grove 0:cd84204f23d8 147 void attachMsg( char *string, char *(*function)(char*) );
sam_grove 0:cd84204f23d8 148
sam_grove 0:cd84204f23d8 149 /** called in a loop somewhere to processes messages
sam_grove 0:cd84204f23d8 150 * @param buffer - A buffer containing ascii data
sam_grove 0:cd84204f23d8 151 * @return Data from the handler message when a match is found and 0 otherwise
sam_grove 0:cd84204f23d8 152 */
sam_grove 0:cd84204f23d8 153 char *serviceMessage( char* buffer );
sam_grove 0:cd84204f23d8 154
sam_grove 0:cd84204f23d8 155
sam_grove 0:cd84204f23d8 156 /** Determine what a message in location X is looking to match
sam_grove 0:cd84204f23d8 157 * @param loc - The location of the member in the list
sam_grove 0:cd84204f23d8 158 * @return The message that is attached to the list
sam_grove 0:cd84204f23d8 159 */
sam_grove 0:cd84204f23d8 160 char *messageLookup( uint32_t const loc );
sam_grove 0:cd84204f23d8 161
sam_grove 0:cd84204f23d8 162 };
sam_grove 0:cd84204f23d8 163
sam_grove 0:cd84204f23d8 164 #endif
sam_grove 0:cd84204f23d8 165