Sam Grove / CommHandler

Dependents:   Waldo_Embed_V2

Committer:
sam_grove
Date:
Mon Apr 08 22:27:42 2013 +0000
Revision:
0:cd84204f23d8
Child:
3:42d524ce6b19
Created helper class from a function pointer and linked list. Neatly parse ascii communication between devices.

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 0:cd84204f23d8 43 _list.append( (MsgObj *)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