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.h@2:2a5b66b60883, 2013-04-10 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Apr 10 04:39:57 2013 +0000
- Revision:
- 2:2a5b66b60883
- Parent:
- 0:cd84204f23d8
- Child:
- 4:5d54100399b1
re-organize to pull-in dependencies when importing with the online compiler
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.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 | 0:cd84204f23d8 | 28 | |
sam_grove | 0:cd84204f23d8 | 29 | /** Example using the CommHandler class |
sam_grove | 0:cd84204f23d8 | 30 | * @code |
sam_grove | 0:cd84204f23d8 | 31 | * #include "mbed.h" |
sam_grove | 0:cd84204f23d8 | 32 | * #include "CommHandler.h" |
sam_grove | 0:cd84204f23d8 | 33 | * |
sam_grove | 0:cd84204f23d8 | 34 | * DigitalOut myled(LED1); |
sam_grove | 0:cd84204f23d8 | 35 | * CommHandler msgs; |
sam_grove | 0:cd84204f23d8 | 36 | * |
sam_grove | 0:cd84204f23d8 | 37 | * char *one(char* msg) |
sam_grove | 0:cd84204f23d8 | 38 | * { |
sam_grove | 0:cd84204f23d8 | 39 | * // you can parse msg here |
sam_grove | 0:cd84204f23d8 | 40 | * LOG("\n"); |
sam_grove | 0:cd84204f23d8 | 41 | * return msg; |
sam_grove | 0:cd84204f23d8 | 42 | * } |
sam_grove | 0:cd84204f23d8 | 43 | * |
sam_grove | 0:cd84204f23d8 | 44 | * class Wrap |
sam_grove | 0:cd84204f23d8 | 45 | * { |
sam_grove | 0:cd84204f23d8 | 46 | * public: |
sam_grove | 0:cd84204f23d8 | 47 | * Wrap(){} |
sam_grove | 0:cd84204f23d8 | 48 | * char *two(char *msg) |
sam_grove | 0:cd84204f23d8 | 49 | * { |
sam_grove | 0:cd84204f23d8 | 50 | * // you can parse msg here |
sam_grove | 0:cd84204f23d8 | 51 | * LOG("\n"); |
sam_grove | 0:cd84204f23d8 | 52 | * return msg; |
sam_grove | 0:cd84204f23d8 | 53 | * } |
sam_grove | 0:cd84204f23d8 | 54 | * }obj; |
sam_grove | 0:cd84204f23d8 | 55 | * |
sam_grove | 0:cd84204f23d8 | 56 | * |
sam_grove | 0:cd84204f23d8 | 57 | * char *three(char* msg) |
sam_grove | 0:cd84204f23d8 | 58 | * { |
sam_grove | 0:cd84204f23d8 | 59 | * // you can parse msg here |
sam_grove | 0:cd84204f23d8 | 60 | * LOG("\n"); |
sam_grove | 0:cd84204f23d8 | 61 | * return msg; |
sam_grove | 0:cd84204f23d8 | 62 | * } |
sam_grove | 0:cd84204f23d8 | 63 | * |
sam_grove | 0:cd84204f23d8 | 64 | * int main() |
sam_grove | 0:cd84204f23d8 | 65 | * { |
sam_grove | 0:cd84204f23d8 | 66 | * char *tmp = 0; |
sam_grove | 0:cd84204f23d8 | 67 | * |
sam_grove | 0:cd84204f23d8 | 68 | * msgs.attachMsg("One", &one); |
sam_grove | 0:cd84204f23d8 | 69 | * msgs.attachMsg("Two", &obj, &Wrap::two); |
sam_grove | 0:cd84204f23d8 | 70 | * msgs.attachMsg("Three", &three); |
sam_grove | 0:cd84204f23d8 | 71 | * |
sam_grove | 0:cd84204f23d8 | 72 | * tmp = msgs.messageLookup(0); |
sam_grove | 0:cd84204f23d8 | 73 | * printf("1:%s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 74 | * tmp = msgs.messageLookup(1); |
sam_grove | 0:cd84204f23d8 | 75 | * printf("2:%s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 76 | * tmp = msgs.messageLookup(2); |
sam_grove | 0:cd84204f23d8 | 77 | * printf("3:%s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 78 | * tmp = msgs.messageLookup(3); |
sam_grove | 0:cd84204f23d8 | 79 | * printf("4:%s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 80 | * tmp = msgs.messageLookup(4); |
sam_grove | 0:cd84204f23d8 | 81 | * printf("5:%s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 82 | * |
sam_grove | 0:cd84204f23d8 | 83 | * tmp = msgs.serviceMessage("Two-00-66-99-20133"); |
sam_grove | 0:cd84204f23d8 | 84 | * printf("1: Found: %s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 85 | * tmp = msgs.serviceMessage("One-99-60-1-339788354"); |
sam_grove | 0:cd84204f23d8 | 86 | * printf("2: Found: %s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 87 | * tmp = msgs.serviceMessage("Three-xx-xx-XX-XXXXXXX"); |
sam_grove | 0:cd84204f23d8 | 88 | * printf("3: Found: %s\n", tmp); |
sam_grove | 0:cd84204f23d8 | 89 | * |
sam_grove | 0:cd84204f23d8 | 90 | * error("End of Test\n"); |
sam_grove | 0:cd84204f23d8 | 91 | * } |
sam_grove | 0:cd84204f23d8 | 92 | * @endcode |
sam_grove | 0:cd84204f23d8 | 93 | */ |
sam_grove | 0:cd84204f23d8 | 94 | |
sam_grove | 0:cd84204f23d8 | 95 | /** |
sam_grove | 0:cd84204f23d8 | 96 | * @class CommHandler |
sam_grove | 0:cd84204f23d8 | 97 | * @brief API abstraction for managing device to device communication |
sam_grove | 0:cd84204f23d8 | 98 | */ |
sam_grove | 0:cd84204f23d8 | 99 | class CommHandler |
sam_grove | 0:cd84204f23d8 | 100 | { |
sam_grove | 0:cd84204f23d8 | 101 | private: |
sam_grove | 0:cd84204f23d8 | 102 | LinkedList <node>_list; |
sam_grove | 0:cd84204f23d8 | 103 | |
sam_grove | 0:cd84204f23d8 | 104 | public: |
sam_grove | 0:cd84204f23d8 | 105 | |
sam_grove | 0:cd84204f23d8 | 106 | /** |
sam_grove | 0:cd84204f23d8 | 107 | * @struct MsgObject |
sam_grove | 0:cd84204f23d8 | 108 | * @brief An object to store in the linked list |
sam_grove | 0:cd84204f23d8 | 109 | */ |
sam_grove | 0:cd84204f23d8 | 110 | struct MsgObj |
sam_grove | 0:cd84204f23d8 | 111 | { |
sam_grove | 0:cd84204f23d8 | 112 | char *string; /*!< The header we are going to match */ |
sam_grove | 0:cd84204f23d8 | 113 | FP<char *, char *>handler; /*!< The function to call when a match is found */ |
sam_grove | 0:cd84204f23d8 | 114 | }; |
sam_grove | 0:cd84204f23d8 | 115 | |
sam_grove | 0:cd84204f23d8 | 116 | /** Create the CommHandler object |
sam_grove | 0:cd84204f23d8 | 117 | */ |
sam_grove | 0:cd84204f23d8 | 118 | CommHandler(); |
sam_grove | 0:cd84204f23d8 | 119 | |
sam_grove | 0:cd84204f23d8 | 120 | /** Attach a member function as the match handler |
sam_grove | 0:cd84204f23d8 | 121 | * @param string - The string that we're trying to match |
sam_grove | 0:cd84204f23d8 | 122 | * @param item - Address of a initialized object |
sam_grove | 0:cd84204f23d8 | 123 | * @param member - Address of the initialized object's member function |
sam_grove | 0:cd84204f23d8 | 124 | */ |
sam_grove | 0:cd84204f23d8 | 125 | template<class T> |
sam_grove | 0:cd84204f23d8 | 126 | void attachMsg( char *string, T *item, char*(T::*method)(char *) ) |
sam_grove | 0:cd84204f23d8 | 127 | { |
sam_grove | 0:cd84204f23d8 | 128 | MsgObj *new_node = new MsgObj [1]; |
sam_grove | 0:cd84204f23d8 | 129 | // make sure the new object was allocated |
sam_grove | 0:cd84204f23d8 | 130 | if (NULL == new_node) |
sam_grove | 0:cd84204f23d8 | 131 | { |
sam_grove | 0:cd84204f23d8 | 132 | ERROR("Memory Allocation Failed\n"); |
sam_grove | 0:cd84204f23d8 | 133 | } |
sam_grove | 0:cd84204f23d8 | 134 | // store the user parameters |
sam_grove | 0:cd84204f23d8 | 135 | new_node->string = string; |
sam_grove | 0:cd84204f23d8 | 136 | new_node->handler.attach( item, method ); |
sam_grove | 0:cd84204f23d8 | 137 | // and insert them into the list |
sam_grove | 0:cd84204f23d8 | 138 | _list.append(new_node); |
sam_grove | 0:cd84204f23d8 | 139 | return; |
sam_grove | 0:cd84204f23d8 | 140 | } |
sam_grove | 0:cd84204f23d8 | 141 | |
sam_grove | 0:cd84204f23d8 | 142 | /** Attach a global function as the match handler |
sam_grove | 0:cd84204f23d8 | 143 | * @param string - The string that we're trying to match |
sam_grove | 0:cd84204f23d8 | 144 | * @param function - Address of the global function |
sam_grove | 0:cd84204f23d8 | 145 | */ |
sam_grove | 0:cd84204f23d8 | 146 | void attachMsg( char *string, char *(*function)(char*) ); |
sam_grove | 0:cd84204f23d8 | 147 | |
sam_grove | 0:cd84204f23d8 | 148 | /** called in a loop somewhere to processes messages |
sam_grove | 0:cd84204f23d8 | 149 | * @param buffer - A buffer containing ascii data |
sam_grove | 0:cd84204f23d8 | 150 | * @return Data from the handler message when a match is found and 0 otherwise |
sam_grove | 0:cd84204f23d8 | 151 | */ |
sam_grove | 0:cd84204f23d8 | 152 | char *serviceMessage( char* buffer ); |
sam_grove | 0:cd84204f23d8 | 153 | |
sam_grove | 0:cd84204f23d8 | 154 | |
sam_grove | 0:cd84204f23d8 | 155 | /** Determine what a message in location X is looking to match |
sam_grove | 0:cd84204f23d8 | 156 | * @param loc - The location of the member in the list |
sam_grove | 0:cd84204f23d8 | 157 | * @return The message that is attached to the list |
sam_grove | 0:cd84204f23d8 | 158 | */ |
sam_grove | 0:cd84204f23d8 | 159 | char *messageLookup( uint32_t const loc ); |
sam_grove | 0:cd84204f23d8 | 160 | |
sam_grove | 0:cd84204f23d8 | 161 | }; |
sam_grove | 0:cd84204f23d8 | 162 | |
sam_grove | 0:cd84204f23d8 | 163 | #endif |
sam_grove | 0:cd84204f23d8 | 164 |