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.cpp
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 #include "CommHandler.h"
sam_grove 0:cd84204f23d8 24 #include <string.h>
sam_grove 0:cd84204f23d8 25
sam_grove 0:cd84204f23d8 26 CommHandler::CommHandler( void )
sam_grove 0:cd84204f23d8 27 {
sam_grove 0:cd84204f23d8 28 return;
sam_grove 0:cd84204f23d8 29 }
sam_grove 0:cd84204f23d8 30
sam_grove 0:cd84204f23d8 31 void CommHandler::attachMsg( char *string, char *(*function)(char *) )
sam_grove 0:cd84204f23d8 32 {
sam_grove 0:cd84204f23d8 33 MsgObj *new_node = new MsgObj [1];
sam_grove 0:cd84204f23d8 34 // make sure the new object was allocated
sam_grove 0:cd84204f23d8 35 if (0 == new_node)
sam_grove 0:cd84204f23d8 36 {
sam_grove 0:cd84204f23d8 37 ERROR("Memory Allocation Failed\n");
sam_grove 0:cd84204f23d8 38 }
sam_grove 0:cd84204f23d8 39 // store the user parameters
sam_grove 0:cd84204f23d8 40 new_node->string = string;
sam_grove 0:cd84204f23d8 41 new_node->handler.attach( function );
sam_grove 0:cd84204f23d8 42 // and insert them into the list
sam_grove 9:1a76d4848462 43 _list.append(new_node);
sam_grove 0:cd84204f23d8 44
sam_grove 0:cd84204f23d8 45 return;
sam_grove 0:cd84204f23d8 46 }
sam_grove 0:cd84204f23d8 47
sam_grove 0:cd84204f23d8 48 char *CommHandler::serviceMessage( char* buffer )
sam_grove 0:cd84204f23d8 49 {
sam_grove 0:cd84204f23d8 50 uint32_t cnt = _list.length();
sam_grove 0:cd84204f23d8 51 node *item;
sam_grove 0:cd84204f23d8 52 MsgObj *tmp_data;
sam_grove 0:cd84204f23d8 53
sam_grove 0:cd84204f23d8 54 for(uint32_t i=1; i<=cnt; ++i)
sam_grove 0:cd84204f23d8 55 {
sam_grove 0:cd84204f23d8 56 item = _list.pop(i);
sam_grove 0:cd84204f23d8 57 tmp_data = (MsgObj *)item->data;
sam_grove 0:cd84204f23d8 58 // Test the input against the stored record
sam_grove 0:cd84204f23d8 59 if( 0 == memcmp( buffer, tmp_data->string, strlen(tmp_data->string) ) )
sam_grove 0:cd84204f23d8 60 {
sam_grove 0:cd84204f23d8 61 return tmp_data->handler( buffer );
sam_grove 0:cd84204f23d8 62 }
sam_grove 0:cd84204f23d8 63 }
sam_grove 0:cd84204f23d8 64
sam_grove 0:cd84204f23d8 65 return 0;
sam_grove 0:cd84204f23d8 66 }
sam_grove 0:cd84204f23d8 67
sam_grove 0:cd84204f23d8 68 char *CommHandler::messageLookup( uint32_t const loc )
sam_grove 0:cd84204f23d8 69 {
sam_grove 0:cd84204f23d8 70 node *tmp_item = _list.pop( loc );
sam_grove 0:cd84204f23d8 71
sam_grove 0:cd84204f23d8 72 if(0 == tmp_item)
sam_grove 0:cd84204f23d8 73 {
sam_grove 0:cd84204f23d8 74 return 0;
sam_grove 0:cd84204f23d8 75 }
sam_grove 0:cd84204f23d8 76
sam_grove 0:cd84204f23d8 77 MsgObj *tmp_data = (MsgObj *)tmp_item->data;
sam_grove 0:cd84204f23d8 78
sam_grove 0:cd84204f23d8 79 return tmp_data->string;
sam_grove 0:cd84204f23d8 80 }
sam_grove 0:cd84204f23d8 81