Utility to manage ASCII communications. Register a header and event and then pass messages into the class and events are generated on a match
Information
Dependencies not included with library:
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"); }
CommHandler.cpp@3:42d524ce6b19, 2013-04-10 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Apr 10 04:46:29 2013 +0000
- Revision:
- 3:42d524ce6b19
- Parent:
- 0:cd84204f23d8
- Child:
- 4:5d54100399b1
Added include for LogUtil.h to CommHandler.cpp. Link broken when encapsulating the libs
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:42d524ce6b19 | 24 | #include "LogUtil.h" |
sam_grove | 0:cd84204f23d8 | 25 | #include <string.h> |
sam_grove | 0:cd84204f23d8 | 26 | |
sam_grove | 0:cd84204f23d8 | 27 | CommHandler::CommHandler( void ) |
sam_grove | 0:cd84204f23d8 | 28 | { |
sam_grove | 0:cd84204f23d8 | 29 | return; |
sam_grove | 0:cd84204f23d8 | 30 | } |
sam_grove | 0:cd84204f23d8 | 31 | |
sam_grove | 0:cd84204f23d8 | 32 | void CommHandler::attachMsg( char *string, char *(*function)(char *) ) |
sam_grove | 0:cd84204f23d8 | 33 | { |
sam_grove | 0:cd84204f23d8 | 34 | MsgObj *new_node = new MsgObj [1]; |
sam_grove | 0:cd84204f23d8 | 35 | // make sure the new object was allocated |
sam_grove | 0:cd84204f23d8 | 36 | if (0 == new_node) |
sam_grove | 0:cd84204f23d8 | 37 | { |
sam_grove | 0:cd84204f23d8 | 38 | ERROR("Memory Allocation Failed\n"); |
sam_grove | 0:cd84204f23d8 | 39 | } |
sam_grove | 0:cd84204f23d8 | 40 | // store the user parameters |
sam_grove | 0:cd84204f23d8 | 41 | new_node->string = string; |
sam_grove | 0:cd84204f23d8 | 42 | new_node->handler.attach( function ); |
sam_grove | 0:cd84204f23d8 | 43 | // and insert them into the list |
sam_grove | 0:cd84204f23d8 | 44 | _list.append( (MsgObj *)new_node); |
sam_grove | 0:cd84204f23d8 | 45 | |
sam_grove | 0:cd84204f23d8 | 46 | return; |
sam_grove | 0:cd84204f23d8 | 47 | } |
sam_grove | 0:cd84204f23d8 | 48 | |
sam_grove | 0:cd84204f23d8 | 49 | char *CommHandler::serviceMessage( char* buffer ) |
sam_grove | 0:cd84204f23d8 | 50 | { |
sam_grove | 0:cd84204f23d8 | 51 | uint32_t cnt = _list.length(); |
sam_grove | 0:cd84204f23d8 | 52 | node *item; |
sam_grove | 0:cd84204f23d8 | 53 | MsgObj *tmp_data; |
sam_grove | 0:cd84204f23d8 | 54 | |
sam_grove | 0:cd84204f23d8 | 55 | for(uint32_t i=1; i<=cnt; ++i) |
sam_grove | 0:cd84204f23d8 | 56 | { |
sam_grove | 0:cd84204f23d8 | 57 | item = _list.pop(i); |
sam_grove | 0:cd84204f23d8 | 58 | tmp_data = (MsgObj *)item->data; |
sam_grove | 0:cd84204f23d8 | 59 | // Test the input against the stored record |
sam_grove | 0:cd84204f23d8 | 60 | if( 0 == memcmp( buffer, tmp_data->string, strlen(tmp_data->string) ) ) |
sam_grove | 0:cd84204f23d8 | 61 | { |
sam_grove | 0:cd84204f23d8 | 62 | return tmp_data->handler( buffer ); |
sam_grove | 0:cd84204f23d8 | 63 | } |
sam_grove | 0:cd84204f23d8 | 64 | } |
sam_grove | 0:cd84204f23d8 | 65 | |
sam_grove | 0:cd84204f23d8 | 66 | return 0; |
sam_grove | 0:cd84204f23d8 | 67 | } |
sam_grove | 0:cd84204f23d8 | 68 | |
sam_grove | 0:cd84204f23d8 | 69 | char *CommHandler::messageLookup( uint32_t const loc ) |
sam_grove | 0:cd84204f23d8 | 70 | { |
sam_grove | 0:cd84204f23d8 | 71 | node *tmp_item = _list.pop( loc ); |
sam_grove | 0:cd84204f23d8 | 72 | |
sam_grove | 0:cd84204f23d8 | 73 | if(0 == tmp_item) |
sam_grove | 0:cd84204f23d8 | 74 | { |
sam_grove | 0:cd84204f23d8 | 75 | return 0; |
sam_grove | 0:cd84204f23d8 | 76 | } |
sam_grove | 0:cd84204f23d8 | 77 | |
sam_grove | 0:cd84204f23d8 | 78 | MsgObj *tmp_data = (MsgObj *)tmp_item->data; |
sam_grove | 0:cd84204f23d8 | 79 | |
sam_grove | 0:cd84204f23d8 | 80 | return tmp_data->string; |
sam_grove | 0:cd84204f23d8 | 81 | } |
sam_grove | 0:cd84204f23d8 | 82 |