mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
MQTTTransport.cpp@0:a3fc1c6ef150, 2014-03-26 (annotated)
- Committer:
- ansond
- Date:
- Wed Mar 26 19:48:35 2014 +0000
- Revision:
- 0:a3fc1c6ef150
- Child:
- 7:8a4a61202b36
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:a3fc1c6ef150 | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 0:a3fc1c6ef150 | 2 | * |
ansond | 0:a3fc1c6ef150 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:a3fc1c6ef150 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:a3fc1c6ef150 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:a3fc1c6ef150 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:a3fc1c6ef150 | 7 | * furnished to do so, subject to the following conditions: |
ansond | 0:a3fc1c6ef150 | 8 | * |
ansond | 0:a3fc1c6ef150 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:a3fc1c6ef150 | 10 | * substantial portions of the Software. |
ansond | 0:a3fc1c6ef150 | 11 | * |
ansond | 0:a3fc1c6ef150 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:a3fc1c6ef150 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:a3fc1c6ef150 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:a3fc1c6ef150 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:a3fc1c6ef150 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:a3fc1c6ef150 | 17 | */ |
ansond | 0:a3fc1c6ef150 | 18 | |
ansond | 0:a3fc1c6ef150 | 19 | #include "mbed.h" |
ansond | 0:a3fc1c6ef150 | 20 | #include "rtos.h" |
ansond | 0:a3fc1c6ef150 | 21 | |
ansond | 0:a3fc1c6ef150 | 22 | #include "MQTTTransport.h" |
ansond | 0:a3fc1c6ef150 | 23 | |
ansond | 0:a3fc1c6ef150 | 24 | // Endpoint Support |
ansond | 0:a3fc1c6ef150 | 25 | #include "MBEDEndpoint.h" |
ansond | 0:a3fc1c6ef150 | 26 | |
ansond | 0:a3fc1c6ef150 | 27 | // EmulatedResourceFactory support |
ansond | 0:a3fc1c6ef150 | 28 | #include "EmulatedResourceFactory.h" |
ansond | 0:a3fc1c6ef150 | 29 | |
ansond | 0:a3fc1c6ef150 | 30 | // Network mutex |
ansond | 0:a3fc1c6ef150 | 31 | extern Mutex *network_mutex; |
ansond | 0:a3fc1c6ef150 | 32 | |
ansond | 0:a3fc1c6ef150 | 33 | // our transmitt instance |
ansond | 0:a3fc1c6ef150 | 34 | MQTTTransport *_mqtt_instance = NULL; |
ansond | 0:a3fc1c6ef150 | 35 | |
ansond | 0:a3fc1c6ef150 | 36 | // MQTT callback to handle received messages |
ansond | 0:a3fc1c6ef150 | 37 | void _mqtt_message_handler(char *topic,char *payload,unsigned int length) { |
ansond | 0:a3fc1c6ef150 | 38 | char buffer[MAX_MQTT_MESSAGE_LENGTH+1]; |
ansond | 0:a3fc1c6ef150 | 39 | char rcv_topic[MQTT_IOC_TOPIC_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 40 | |
ansond | 0:a3fc1c6ef150 | 41 | memset(buffer,0,MAX_MQTT_MESSAGE_LENGTH+1); |
ansond | 0:a3fc1c6ef150 | 42 | memset(rcv_topic,0,MQTT_IOC_TOPIC_LEN+1); |
ansond | 0:a3fc1c6ef150 | 43 | memcpy(buffer,payload,length); |
ansond | 0:a3fc1c6ef150 | 44 | strcpy(rcv_topic,topic); |
ansond | 0:a3fc1c6ef150 | 45 | |
ansond | 0:a3fc1c6ef150 | 46 | if (_mqtt_instance != NULL) { |
ansond | 0:a3fc1c6ef150 | 47 | if (_mqtt_instance->isPongMessage(rcv_topic,buffer,length)) { |
ansond | 0:a3fc1c6ef150 | 48 | _mqtt_instance->processPongMessage(buffer,length); |
ansond | 0:a3fc1c6ef150 | 49 | } |
ansond | 0:a3fc1c6ef150 | 50 | else { |
ansond | 0:a3fc1c6ef150 | 51 | memset(buffer,0,MAX_MQTT_MESSAGE_LENGTH+1); |
ansond | 0:a3fc1c6ef150 | 52 | memset(rcv_topic,0,MQTT_IOC_TOPIC_LEN+1); |
ansond | 0:a3fc1c6ef150 | 53 | memcpy(buffer,payload,length); |
ansond | 0:a3fc1c6ef150 | 54 | strcpy(rcv_topic,topic); |
ansond | 0:a3fc1c6ef150 | 55 | _mqtt_instance->processMessage(_mqtt_instance->getEndpointNameFromTopic(rcv_topic),buffer,length); |
ansond | 0:a3fc1c6ef150 | 56 | } |
ansond | 0:a3fc1c6ef150 | 57 | } |
ansond | 0:a3fc1c6ef150 | 58 | } |
ansond | 0:a3fc1c6ef150 | 59 | |
ansond | 0:a3fc1c6ef150 | 60 | // our MQTT client endpoint |
ansond | 0:a3fc1c6ef150 | 61 | PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler); |
ansond | 0:a3fc1c6ef150 | 62 | |
ansond | 0:a3fc1c6ef150 | 63 | // default constructor |
ansond | 0:a3fc1c6ef150 | 64 | MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint,MBEDToIOCResourceMap *map) : Transport(error_handler,endpoint) { |
ansond | 0:a3fc1c6ef150 | 65 | this->m_mqtt = NULL; |
ansond | 0:a3fc1c6ef150 | 66 | _mqtt_instance = this; |
ansond | 0:a3fc1c6ef150 | 67 | this->m_map = map; |
ansond | 0:a3fc1c6ef150 | 68 | this->m_ping_counter = 1; |
ansond | 0:a3fc1c6ef150 | 69 | this->m_ping_countdown = MQTT_PING_COUNTDOWN; |
ansond | 0:a3fc1c6ef150 | 70 | this->initTopic(); |
ansond | 0:a3fc1c6ef150 | 71 | } |
ansond | 0:a3fc1c6ef150 | 72 | |
ansond | 0:a3fc1c6ef150 | 73 | // default destructor |
ansond | 0:a3fc1c6ef150 | 74 | MQTTTransport::~MQTTTransport() { |
ansond | 0:a3fc1c6ef150 | 75 | this->disconnect(); |
ansond | 0:a3fc1c6ef150 | 76 | } |
ansond | 0:a3fc1c6ef150 | 77 | |
ansond | 0:a3fc1c6ef150 | 78 | // init our topic |
ansond | 0:a3fc1c6ef150 | 79 | void MQTTTransport::initTopic() { |
ansond | 0:a3fc1c6ef150 | 80 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 0:a3fc1c6ef150 | 81 | char *endpoint_name = endpoint->getEndpointName(); |
ansond | 0:a3fc1c6ef150 | 82 | memset(this->m_topic,0,MQTT_IOC_TOPIC_LEN+1); |
ansond | 0:a3fc1c6ef150 | 83 | sprintf(this->m_topic,MQTT_IOC_TOPIC,endpoint_name); |
ansond | 0:a3fc1c6ef150 | 84 | } |
ansond | 0:a3fc1c6ef150 | 85 | |
ansond | 0:a3fc1c6ef150 | 86 | // get our topic |
ansond | 0:a3fc1c6ef150 | 87 | char *MQTTTransport::getTopic() { return this->m_topic; } |
ansond | 0:a3fc1c6ef150 | 88 | |
ansond | 0:a3fc1c6ef150 | 89 | // get the IOC <--> MBED resource map |
ansond | 0:a3fc1c6ef150 | 90 | MBEDToIOCResourceMap *MQTTTransport::getMap() { return this->m_map; } |
ansond | 0:a3fc1c6ef150 | 91 | |
ansond | 0:a3fc1c6ef150 | 92 | // pull the endpoint name from the MQTT topic |
ansond | 0:a3fc1c6ef150 | 93 | char *MQTTTransport::getEndpointNameFromTopic(char *topic) { |
ansond | 0:a3fc1c6ef150 | 94 | if (topic != NULL) { |
ansond | 0:a3fc1c6ef150 | 95 | memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); |
ansond | 0:a3fc1c6ef150 | 96 | char trash[MQTT_IOC_TOPIC_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 97 | char ep[MQTT_IOC_TOPIC_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 98 | memset(trash,0,MQTT_IOC_TOPIC_LEN+1); |
ansond | 0:a3fc1c6ef150 | 99 | memset(ep,0,MQTT_IOC_TOPIC_LEN+1); |
ansond | 0:a3fc1c6ef150 | 100 | bool done = false; |
ansond | 0:a3fc1c6ef150 | 101 | int length = 0; if (topic != NULL) length = strlen(topic); |
ansond | 0:a3fc1c6ef150 | 102 | for(int i=length-1;i>=0 && !done;--i) if (topic[i] == '/') { topic[i] = ' ' ; done = true; } |
ansond | 0:a3fc1c6ef150 | 103 | sscanf(topic,"%s%s",trash,ep); |
ansond | 0:a3fc1c6ef150 | 104 | //this->logger()->log("MQTT: Topic:[%s] trash:[%s] ep:[%s]",topic,trash,ep); |
ansond | 0:a3fc1c6ef150 | 105 | if (strlen(ep) > 0) { |
ansond | 0:a3fc1c6ef150 | 106 | if (strcmp(ep,MQTT_IOC_ALL_ENDPOINT) != 0) { |
ansond | 0:a3fc1c6ef150 | 107 | // just insert the name and let the parser determine if its for us or not... |
ansond | 0:a3fc1c6ef150 | 108 | strncpy(this->m_endpoint_name,ep,strlen(ep)); |
ansond | 0:a3fc1c6ef150 | 109 | } |
ansond | 0:a3fc1c6ef150 | 110 | else { |
ansond | 0:a3fc1c6ef150 | 111 | // this is a broadcast message - so we need to process it |
ansond | 0:a3fc1c6ef150 | 112 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 0:a3fc1c6ef150 | 113 | char *endpoint_name = endpoint->getEndpointName(); |
ansond | 0:a3fc1c6ef150 | 114 | strcpy(this->m_endpoint_name,endpoint_name); |
ansond | 0:a3fc1c6ef150 | 115 | } |
ansond | 0:a3fc1c6ef150 | 116 | } |
ansond | 0:a3fc1c6ef150 | 117 | //this->logger()->log("MQTT Topic (discovered): %s Original: %s",this->m_endpoint_name,topic); |
ansond | 0:a3fc1c6ef150 | 118 | return this->m_endpoint_name; |
ansond | 0:a3fc1c6ef150 | 119 | } |
ansond | 0:a3fc1c6ef150 | 120 | //this->logger()->log("MQTT Topic (discovered): NULL Original: %s",topic); |
ansond | 0:a3fc1c6ef150 | 121 | return NULL; |
ansond | 0:a3fc1c6ef150 | 122 | } |
ansond | 0:a3fc1c6ef150 | 123 | |
ansond | 0:a3fc1c6ef150 | 124 | // process a MQTT Message |
ansond | 0:a3fc1c6ef150 | 125 | void MQTTTransport::processMessage(char *message_name,char *payload, unsigned int payload_length) { |
ansond | 0:a3fc1c6ef150 | 126 | char message_type[MQTT_PAYLOAD_SEGMENT_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 127 | char message_verb[MQTT_PAYLOAD_SEGMENT_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 128 | char message_value[MQTT_PAYLOAD_SEGMENT_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 129 | char message_opt[MQTT_PAYLOAD_SEGMENT_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 130 | |
ansond | 0:a3fc1c6ef150 | 131 | // initialize |
ansond | 0:a3fc1c6ef150 | 132 | memset(message_type,0,MQTT_PAYLOAD_SEGMENT_LEN+1); |
ansond | 0:a3fc1c6ef150 | 133 | memset(message_verb,0,MQTT_PAYLOAD_SEGMENT_LEN+1); |
ansond | 0:a3fc1c6ef150 | 134 | memset(message_value,0,MQTT_PAYLOAD_SEGMENT_LEN+1); |
ansond | 0:a3fc1c6ef150 | 135 | memset(message_opt,0,MQTT_PAYLOAD_SEGMENT_LEN+1); |
ansond | 0:a3fc1c6ef150 | 136 | |
ansond | 0:a3fc1c6ef150 | 137 | // get our endpoint |
ansond | 0:a3fc1c6ef150 | 138 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 0:a3fc1c6ef150 | 139 | char *endpoint_name = endpoint->getEndpointName(); |
ansond | 0:a3fc1c6ef150 | 140 | |
ansond | 0:a3fc1c6ef150 | 141 | // DEBUG |
ansond | 0:a3fc1c6ef150 | 142 | //this->logger()->log("Endpoint:[%s] Target: [%s]",endpoint_name,message_name); |
ansond | 0:a3fc1c6ef150 | 143 | |
ansond | 0:a3fc1c6ef150 | 144 | // only respond if its for our node |
ansond | 0:a3fc1c6ef150 | 145 | if (strcmp(endpoint_name,message_name) == 0) { |
ansond | 0:a3fc1c6ef150 | 146 | // format of the MQTT message: message_type:verb|Parameter_X:value|keyword:optional_data |
ansond | 0:a3fc1c6ef150 | 147 | char buffer[MAX_MQTT_MESSAGE_LENGTH+1]; |
ansond | 0:a3fc1c6ef150 | 148 | memset(buffer,0,MAX_MQTT_MESSAGE_LENGTH+1); |
ansond | 0:a3fc1c6ef150 | 149 | memcpy(buffer,payload,payload_length); |
ansond | 0:a3fc1c6ef150 | 150 | int count = 0; for(int i=0;i<payload_length;++i) if (payload[i] == ':') ++count; |
ansond | 0:a3fc1c6ef150 | 151 | for(int i=0;i<payload_length;++i) { |
ansond | 0:a3fc1c6ef150 | 152 | if (buffer[i] == ':') { |
ansond | 0:a3fc1c6ef150 | 153 | if (i < (payload_length-1)) buffer[i] = ' '; |
ansond | 0:a3fc1c6ef150 | 154 | else buffer[i] = '\0'; |
ansond | 0:a3fc1c6ef150 | 155 | } |
ansond | 0:a3fc1c6ef150 | 156 | } |
ansond | 0:a3fc1c6ef150 | 157 | if (count == 1) sscanf(buffer,"%s%s",message_type,message_verb); |
ansond | 0:a3fc1c6ef150 | 158 | if (count == 2) sscanf(buffer,"%s%s%s",message_type,message_verb,message_value); |
ansond | 0:a3fc1c6ef150 | 159 | if (count == 3) sscanf(buffer,"%s%s%s%s",message_type,message_verb,message_value,message_opt); |
ansond | 0:a3fc1c6ef150 | 160 | |
ansond | 0:a3fc1c6ef150 | 161 | // DEBUG |
ansond | 0:a3fc1c6ef150 | 162 | //this->logger()->log("Raw Payload: %s, length: %d",payload,payload_length); |
ansond | 0:a3fc1c6ef150 | 163 | //this->logger()->log("Buffer: %s, length: %d",buffer,strlen(buffer)); |
ansond | 0:a3fc1c6ef150 | 164 | //this->logger()->log("Parsed Payload: Type: [%s] Name: [%s] Verb: [%s] Value: [%s]",message_type,message_name,message_verb,message_value); |
ansond | 0:a3fc1c6ef150 | 165 | |
ansond | 0:a3fc1c6ef150 | 166 | // load endpoints |
ansond | 0:a3fc1c6ef150 | 167 | if (message_type != NULL && strcmp(message_type,IOC_ENDPOINT_VERB) == 0) { // Endpoint |
ansond | 0:a3fc1c6ef150 | 168 | if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) { // load |
ansond | 0:a3fc1c6ef150 | 169 | if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) { // all |
ansond | 0:a3fc1c6ef150 | 170 | // load up our endpoints |
ansond | 0:a3fc1c6ef150 | 171 | endpoint->loadEndpoints(); |
ansond | 0:a3fc1c6ef150 | 172 | endpoint->updateEndpoints(); |
ansond | 0:a3fc1c6ef150 | 173 | } |
ansond | 0:a3fc1c6ef150 | 174 | else if (message_value != NULL && strcmp(message_value,this->m_endpoint_name) == 0) { |
ansond | 0:a3fc1c6ef150 | 175 | // load up our endpoints (us only) |
ansond | 0:a3fc1c6ef150 | 176 | endpoint->loadEndpoints(); |
ansond | 0:a3fc1c6ef150 | 177 | endpoint->updateEndpoints(); |
ansond | 0:a3fc1c6ef150 | 178 | } |
ansond | 0:a3fc1c6ef150 | 179 | } |
ansond | 0:a3fc1c6ef150 | 180 | |
ansond | 0:a3fc1c6ef150 | 181 | else if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) { // update |
ansond | 0:a3fc1c6ef150 | 182 | if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) { // all |
ansond | 0:a3fc1c6ef150 | 183 | // update our endpoints |
ansond | 0:a3fc1c6ef150 | 184 | endpoint->updateEndpoints(); |
ansond | 0:a3fc1c6ef150 | 185 | } |
ansond | 0:a3fc1c6ef150 | 186 | else { |
ansond | 0:a3fc1c6ef150 | 187 | // update just our endpoint |
ansond | 0:a3fc1c6ef150 | 188 | int index = -1; |
ansond | 0:a3fc1c6ef150 | 189 | if (message_name != NULL) { |
ansond | 0:a3fc1c6ef150 | 190 | index = endpoint->indexOfLight((char *)message_name); |
ansond | 0:a3fc1c6ef150 | 191 | if (index >= 0) { |
ansond | 0:a3fc1c6ef150 | 192 | if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) { |
ansond | 0:a3fc1c6ef150 | 193 | // update our endpoint |
ansond | 0:a3fc1c6ef150 | 194 | endpoint->updateEndpoints(index); |
ansond | 0:a3fc1c6ef150 | 195 | } |
ansond | 0:a3fc1c6ef150 | 196 | } |
ansond | 0:a3fc1c6ef150 | 197 | } |
ansond | 0:a3fc1c6ef150 | 198 | } |
ansond | 0:a3fc1c6ef150 | 199 | } |
ansond | 0:a3fc1c6ef150 | 200 | } |
ansond | 0:a3fc1c6ef150 | 201 | |
ansond | 0:a3fc1c6ef150 | 202 | // change a resource value |
ansond | 0:a3fc1c6ef150 | 203 | if (message_type != NULL && strcmp(message_type,IOC_CHANGE_VERB) == 0) { |
ansond | 0:a3fc1c6ef150 | 204 | if (message_name != NULL) { |
ansond | 0:a3fc1c6ef150 | 205 | // destined for our lights? |
ansond | 0:a3fc1c6ef150 | 206 | int index = endpoint->indexOfLight((char *)message_name); |
ansond | 0:a3fc1c6ef150 | 207 | if (index >= 0) { |
ansond | 0:a3fc1c6ef150 | 208 | if (message_verb != NULL) { |
ansond | 0:a3fc1c6ef150 | 209 | // map the parameter to one of ours |
ansond | 0:a3fc1c6ef150 | 210 | char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb); |
ansond | 0:a3fc1c6ef150 | 211 | if (mapped_resource != NULL) { |
ansond | 0:a3fc1c6ef150 | 212 | if (message_value != NULL) { |
ansond | 0:a3fc1c6ef150 | 213 | EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index); |
ansond | 0:a3fc1c6ef150 | 214 | bool success = factory->setResourceValue(mapped_resource,message_value); |
ansond | 0:a3fc1c6ef150 | 215 | |
ansond | 0:a3fc1c6ef150 | 216 | // end the resource value back over MQTT |
ansond | 0:a3fc1c6ef150 | 217 | this->sendResult(message_name,message_verb,message_value,success); |
ansond | 0:a3fc1c6ef150 | 218 | } |
ansond | 0:a3fc1c6ef150 | 219 | } |
ansond | 0:a3fc1c6ef150 | 220 | } |
ansond | 0:a3fc1c6ef150 | 221 | } |
ansond | 0:a3fc1c6ef150 | 222 | } |
ansond | 0:a3fc1c6ef150 | 223 | } |
ansond | 0:a3fc1c6ef150 | 224 | |
ansond | 0:a3fc1c6ef150 | 225 | // get a resource value |
ansond | 0:a3fc1c6ef150 | 226 | if (message_type != NULL && strcmp(message_type,IOC_REQUEST_VALUE_VERB) == 0) { |
ansond | 0:a3fc1c6ef150 | 227 | if (message_name != NULL) { |
ansond | 0:a3fc1c6ef150 | 228 | // destined for our lights? |
ansond | 0:a3fc1c6ef150 | 229 | int index = endpoint->indexOfLight((char *)message_name); |
ansond | 0:a3fc1c6ef150 | 230 | if (index >= 0) { |
ansond | 0:a3fc1c6ef150 | 231 | if (message_verb != NULL) { |
ansond | 0:a3fc1c6ef150 | 232 | // map the parameter to one of ours |
ansond | 0:a3fc1c6ef150 | 233 | char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb); |
ansond | 0:a3fc1c6ef150 | 234 | if (mapped_resource != NULL) { |
ansond | 0:a3fc1c6ef150 | 235 | EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index); |
ansond | 0:a3fc1c6ef150 | 236 | strcpy(message_value,factory->getResourceValue((char *)mapped_resource)); |
ansond | 0:a3fc1c6ef150 | 237 | bool success = false; if (message_value != NULL) success = true; |
ansond | 0:a3fc1c6ef150 | 238 | |
ansond | 0:a3fc1c6ef150 | 239 | // log resource get |
ansond | 0:a3fc1c6ef150 | 240 | if (success) this->logger()->log("Resource: %s (%s) Value: %s",message_verb,mapped_resource,message_value); |
ansond | 0:a3fc1c6ef150 | 241 | |
ansond | 0:a3fc1c6ef150 | 242 | // end the resource value back over MQTT |
ansond | 0:a3fc1c6ef150 | 243 | this->sendResult(message_name,message_verb,message_value,success); |
ansond | 0:a3fc1c6ef150 | 244 | } |
ansond | 0:a3fc1c6ef150 | 245 | } |
ansond | 0:a3fc1c6ef150 | 246 | } |
ansond | 0:a3fc1c6ef150 | 247 | } |
ansond | 0:a3fc1c6ef150 | 248 | } |
ansond | 0:a3fc1c6ef150 | 249 | } |
ansond | 0:a3fc1c6ef150 | 250 | else { |
ansond | 0:a3fc1c6ef150 | 251 | // message not bound for our node |
ansond | 0:a3fc1c6ef150 | 252 | //this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name); |
ansond | 0:a3fc1c6ef150 | 253 | ; |
ansond | 0:a3fc1c6ef150 | 254 | } |
ansond | 0:a3fc1c6ef150 | 255 | } |
ansond | 0:a3fc1c6ef150 | 256 | |
ansond | 0:a3fc1c6ef150 | 257 | // send result back to MQTT |
ansond | 0:a3fc1c6ef150 | 258 | void MQTTTransport::sendResult(char *endpoint_name,char *resource_name,char *value,bool success) { |
ansond | 0:a3fc1c6ef150 | 259 | if (this->m_connected == true) { |
ansond | 0:a3fc1c6ef150 | 260 | // send the response back to MQTT |
ansond | 0:a3fc1c6ef150 | 261 | this->logger()->log("Sending Response back to MQTT..."); |
ansond | 0:a3fc1c6ef150 | 262 | char message[MAX_MQTT_MESSAGE_LENGTH+1]; |
ansond | 0:a3fc1c6ef150 | 263 | memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1); |
ansond | 0:a3fc1c6ef150 | 264 | char *str_success = IOC_RESPONSE_OK; if (!success) str_success = IOC_RESPONSE_FAILED; |
ansond | 0:a3fc1c6ef150 | 265 | sprintf(message,IOC_RESPONSE_TEMPLATE,IOC_RESPONSE_VERB,endpoint_name,resource_name,value,str_success); |
ansond | 0:a3fc1c6ef150 | 266 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 0:a3fc1c6ef150 | 267 | bool sent = this->m_mqtt->publish(this->getTopic(),message,strlen(message)); |
ansond | 0:a3fc1c6ef150 | 268 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 0:a3fc1c6ef150 | 269 | if (sent) { |
ansond | 0:a3fc1c6ef150 | 270 | this->logger()->log("Result sent successfully"); |
ansond | 0:a3fc1c6ef150 | 271 | this->logger()->blinkTransportTxLED(); |
ansond | 0:a3fc1c6ef150 | 272 | } |
ansond | 0:a3fc1c6ef150 | 273 | else { |
ansond | 0:a3fc1c6ef150 | 274 | this->logger()->log("Result send FAILED"); |
ansond | 0:a3fc1c6ef150 | 275 | } |
ansond | 0:a3fc1c6ef150 | 276 | } |
ansond | 0:a3fc1c6ef150 | 277 | else { |
ansond | 0:a3fc1c6ef150 | 278 | // unable to send the response |
ansond | 0:a3fc1c6ef150 | 279 | this->logger()->log("Unable to send response back to MQTT. Not connected."); |
ansond | 0:a3fc1c6ef150 | 280 | } |
ansond | 0:a3fc1c6ef150 | 281 | } |
ansond | 0:a3fc1c6ef150 | 282 | |
ansond | 0:a3fc1c6ef150 | 283 | char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->getMap()->iocNameToEndpointName(ioc_name); } |
ansond | 0:a3fc1c6ef150 | 284 | |
ansond | 0:a3fc1c6ef150 | 285 | char *MQTTTransport::makeID(char *id_template,char *buffer) { |
ansond | 0:a3fc1c6ef150 | 286 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 0:a3fc1c6ef150 | 287 | int instance_id = rand()%100; |
ansond | 0:a3fc1c6ef150 | 288 | if (endpoint != NULL) instance_id = endpoint->getInstanceID(); |
ansond | 0:a3fc1c6ef150 | 289 | srand(time(0)); |
ansond | 0:a3fc1c6ef150 | 290 | srand(rand()); |
ansond | 0:a3fc1c6ef150 | 291 | sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE,instance_id); |
ansond | 0:a3fc1c6ef150 | 292 | return buffer; |
ansond | 0:a3fc1c6ef150 | 293 | } |
ansond | 0:a3fc1c6ef150 | 294 | |
ansond | 0:a3fc1c6ef150 | 295 | // is this message a PONG message? |
ansond | 0:a3fc1c6ef150 | 296 | bool MQTTTransport::isPongMessage(char *topic,char *payload,int payload_length) { |
ansond | 0:a3fc1c6ef150 | 297 | bool isPong = false; |
ansond | 0:a3fc1c6ef150 | 298 | char verb[MQTT_PING_VERB_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 299 | char end[MQTT_PING_VERB_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 300 | int counter = 0; |
ansond | 0:a3fc1c6ef150 | 301 | |
ansond | 0:a3fc1c6ef150 | 302 | // clean |
ansond | 0:a3fc1c6ef150 | 303 | memset(verb,0,MQTT_PING_VERB_LEN+1); |
ansond | 0:a3fc1c6ef150 | 304 | memset(end,0,MQTT_PING_VERB_LEN+1); |
ansond | 0:a3fc1c6ef150 | 305 | |
ansond | 0:a3fc1c6ef150 | 306 | // make sure this is for us... |
ansond | 0:a3fc1c6ef150 | 307 | char *topic_ep_name = this->getEndpointNameFromTopic(topic); |
ansond | 0:a3fc1c6ef150 | 308 | if (topic_ep_name != NULL && strcmp(topic_ep_name,this->m_endpoint_name) == 0) { |
ansond | 0:a3fc1c6ef150 | 309 | // parse the payload |
ansond | 0:a3fc1c6ef150 | 310 | for(int i=0;payload != NULL && i<payload_length;++i) if (payload[i] == ':') payload[i] = ' '; |
ansond | 0:a3fc1c6ef150 | 311 | sscanf(payload,"%s%d",verb,&counter); |
ansond | 0:a3fc1c6ef150 | 312 | |
ansond | 0:a3fc1c6ef150 | 313 | // check the contents to make sure its for us... |
ansond | 0:a3fc1c6ef150 | 314 | //this->logger()->log("isPongMessage: verb: %s counter %d ping_counter: %d",verb,counter,this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 315 | if (strcmp(verb,"pong") == 0 && counter == this->m_ping_counter) { |
ansond | 0:a3fc1c6ef150 | 316 | // its a PONG message to our PING... |
ansond | 0:a3fc1c6ef150 | 317 | isPong = true; |
ansond | 0:a3fc1c6ef150 | 318 | } |
ansond | 0:a3fc1c6ef150 | 319 | } |
ansond | 0:a3fc1c6ef150 | 320 | |
ansond | 0:a3fc1c6ef150 | 321 | // return isPong status |
ansond | 0:a3fc1c6ef150 | 322 | return isPong; |
ansond | 0:a3fc1c6ef150 | 323 | } |
ansond | 0:a3fc1c6ef150 | 324 | |
ansond | 0:a3fc1c6ef150 | 325 | |
ansond | 0:a3fc1c6ef150 | 326 | // process this PONG message |
ansond | 0:a3fc1c6ef150 | 327 | void MQTTTransport::processPongMessage(char *payload,int payload_length) { |
ansond | 0:a3fc1c6ef150 | 328 | // DEBUG |
ansond | 0:a3fc1c6ef150 | 329 | //this->logger()->log("Received PONG: counter=%d",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 330 | |
ansond | 0:a3fc1c6ef150 | 331 | // simply increment the counter |
ansond | 0:a3fc1c6ef150 | 332 | ++this->m_ping_counter; |
ansond | 0:a3fc1c6ef150 | 333 | |
ansond | 0:a3fc1c6ef150 | 334 | // reset counter if maxed |
ansond | 0:a3fc1c6ef150 | 335 | if (this->m_ping_counter >= MQTT_MAX_COUNTER) this->m_ping_counter = 1; |
ansond | 0:a3fc1c6ef150 | 336 | } |
ansond | 0:a3fc1c6ef150 | 337 | |
ansond | 0:a3fc1c6ef150 | 338 | // send a PING message |
ansond | 0:a3fc1c6ef150 | 339 | bool MQTTTransport::sendPingMessage() { |
ansond | 0:a3fc1c6ef150 | 340 | bool sent = false; |
ansond | 0:a3fc1c6ef150 | 341 | char message[MAX_MQTT_MESSAGE_LENGTH+1]; |
ansond | 0:a3fc1c6ef150 | 342 | |
ansond | 0:a3fc1c6ef150 | 343 | // initialize... |
ansond | 0:a3fc1c6ef150 | 344 | memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1); |
ansond | 0:a3fc1c6ef150 | 345 | |
ansond | 0:a3fc1c6ef150 | 346 | // build message |
ansond | 0:a3fc1c6ef150 | 347 | sprintf(message,"ping:%s:%d:",this->m_endpoint_name,this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 348 | |
ansond | 0:a3fc1c6ef150 | 349 | // send the message over the ping/pong topic |
ansond | 0:a3fc1c6ef150 | 350 | //this->logger()->log("Sending PING: counter=%d",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 351 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 0:a3fc1c6ef150 | 352 | sent = this->m_mqtt->publish(MQTT_PING_SEND_TOPIC,message,strlen(message)); |
ansond | 0:a3fc1c6ef150 | 353 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 0:a3fc1c6ef150 | 354 | if (sent) { |
ansond | 0:a3fc1c6ef150 | 355 | // send succeeded |
ansond | 0:a3fc1c6ef150 | 356 | //this->logger()->log("PING %d sent successfully",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 357 | this->logger()->blinkTransportTxLED(); |
ansond | 0:a3fc1c6ef150 | 358 | |
ansond | 0:a3fc1c6ef150 | 359 | // wait for 1 second |
ansond | 0:a3fc1c6ef150 | 360 | wait_ms(1000); |
ansond | 0:a3fc1c6ef150 | 361 | } |
ansond | 0:a3fc1c6ef150 | 362 | else { |
ansond | 0:a3fc1c6ef150 | 363 | // send failed! - reconnect |
ansond | 0:a3fc1c6ef150 | 364 | this->logger()->log("PING send %d FAILED... (re)connecting...",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 365 | |
ansond | 0:a3fc1c6ef150 | 366 | // attempt reconnect |
ansond | 0:a3fc1c6ef150 | 367 | this->logger()->log("PING send failed - re-connecting MQTT..."); |
ansond | 0:a3fc1c6ef150 | 368 | this->disconnect(); |
ansond | 0:a3fc1c6ef150 | 369 | sent = this->connect(); |
ansond | 0:a3fc1c6ef150 | 370 | if (sent) this->logger()->log("PING %d: MQTT reconnection successful...",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 371 | else this->logger()->log("PING %d: resend failed giving up...",this->m_ping_counter); |
ansond | 0:a3fc1c6ef150 | 372 | } |
ansond | 0:a3fc1c6ef150 | 373 | |
ansond | 0:a3fc1c6ef150 | 374 | // return our status |
ansond | 0:a3fc1c6ef150 | 375 | return sent; |
ansond | 0:a3fc1c6ef150 | 376 | } |
ansond | 0:a3fc1c6ef150 | 377 | |
ansond | 0:a3fc1c6ef150 | 378 | // connect up MQTT |
ansond | 0:a3fc1c6ef150 | 379 | bool MQTTTransport::connect() { |
ansond | 0:a3fc1c6ef150 | 380 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 0:a3fc1c6ef150 | 381 | char mqtt_id[MQTT_ENDPOINT_IDLEN+1]; |
ansond | 0:a3fc1c6ef150 | 382 | memset(mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1)); |
ansond | 0:a3fc1c6ef150 | 383 | if (this->m_connected == false) { |
ansond | 0:a3fc1c6ef150 | 384 | this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT); |
ansond | 0:a3fc1c6ef150 | 385 | this->m_mqtt = &_mqtt; |
ansond | 0:a3fc1c6ef150 | 386 | if (this->m_mqtt != NULL) { |
ansond | 0:a3fc1c6ef150 | 387 | char *id = this->makeID(MQTT_ENDPOINT_ID,mqtt_id); |
ansond | 0:a3fc1c6ef150 | 388 | this->logger()->log("MQTT Connect: ID: %s...",id); |
ansond | 0:a3fc1c6ef150 | 389 | if (this->m_mqtt->connect(id)) { |
ansond | 0:a3fc1c6ef150 | 390 | this->logger()->log("MQTT Subscribe: Topic: %s...",this->getTopic()); |
ansond | 0:a3fc1c6ef150 | 391 | if (this->m_mqtt->subscribe(this->getTopic())) { |
ansond | 0:a3fc1c6ef150 | 392 | if (this->m_mqtt->subscribe(MQTT_IOC_ALL_TOPIC)) { |
ansond | 0:a3fc1c6ef150 | 393 | this->logger()->log("MQTT CONNECTED."); |
ansond | 0:a3fc1c6ef150 | 394 | this->m_connected = true; |
ansond | 0:a3fc1c6ef150 | 395 | } |
ansond | 0:a3fc1c6ef150 | 396 | else { |
ansond | 0:a3fc1c6ef150 | 397 | this->logger()->log("MQTT Subscribe: Topic: %s FAILED",MQTT_IOC_ALL_TOPIC); |
ansond | 0:a3fc1c6ef150 | 398 | this->logger()->turnLEDRed(); |
ansond | 0:a3fc1c6ef150 | 399 | this->m_connected = false; |
ansond | 0:a3fc1c6ef150 | 400 | } |
ansond | 0:a3fc1c6ef150 | 401 | } |
ansond | 0:a3fc1c6ef150 | 402 | else { |
ansond | 0:a3fc1c6ef150 | 403 | this->logger()->log("MQTT Subscribe: Topic: %s FAILED",this->getTopic()); |
ansond | 0:a3fc1c6ef150 | 404 | this->logger()->turnLEDRed(); |
ansond | 0:a3fc1c6ef150 | 405 | this->m_connected = false; |
ansond | 0:a3fc1c6ef150 | 406 | } |
ansond | 0:a3fc1c6ef150 | 407 | } |
ansond | 0:a3fc1c6ef150 | 408 | else { |
ansond | 0:a3fc1c6ef150 | 409 | this->logger()->log("MQTT Connect: ID: %s FAILED",id); |
ansond | 0:a3fc1c6ef150 | 410 | this->logger()->turnLEDRed(); |
ansond | 0:a3fc1c6ef150 | 411 | this->m_connected = false; |
ansond | 0:a3fc1c6ef150 | 412 | } |
ansond | 0:a3fc1c6ef150 | 413 | } |
ansond | 0:a3fc1c6ef150 | 414 | else { |
ansond | 0:a3fc1c6ef150 | 415 | this->logger()->log("MQTT Unable to allocate new instance"); |
ansond | 0:a3fc1c6ef150 | 416 | this->logger()->turnLEDRed(); |
ansond | 0:a3fc1c6ef150 | 417 | this->m_connected = false; |
ansond | 0:a3fc1c6ef150 | 418 | } |
ansond | 0:a3fc1c6ef150 | 419 | } |
ansond | 0:a3fc1c6ef150 | 420 | else { |
ansond | 0:a3fc1c6ef150 | 421 | this->logger()->log("MQTT already connected (OK)"); |
ansond | 0:a3fc1c6ef150 | 422 | } |
ansond | 0:a3fc1c6ef150 | 423 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 0:a3fc1c6ef150 | 424 | return this->m_connected; |
ansond | 0:a3fc1c6ef150 | 425 | } |
ansond | 0:a3fc1c6ef150 | 426 | |
ansond | 0:a3fc1c6ef150 | 427 | // disconnect from MQTT |
ansond | 0:a3fc1c6ef150 | 428 | bool MQTTTransport::disconnect() { |
ansond | 0:a3fc1c6ef150 | 429 | if (this->m_mqtt != NULL) { |
ansond | 0:a3fc1c6ef150 | 430 | this->logger()->log("MQTT Unsubscribing from: %s...",this->getTopic()); |
ansond | 0:a3fc1c6ef150 | 431 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 0:a3fc1c6ef150 | 432 | this->m_mqtt->unsubscribe(this->getTopic()); |
ansond | 0:a3fc1c6ef150 | 433 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 0:a3fc1c6ef150 | 434 | this->logger()->log("MQTT Disconnecting..."); |
ansond | 0:a3fc1c6ef150 | 435 | this->m_mqtt->disconnect(); |
ansond | 0:a3fc1c6ef150 | 436 | } |
ansond | 0:a3fc1c6ef150 | 437 | else { |
ansond | 0:a3fc1c6ef150 | 438 | this->logger()->log("MQTT already disconnected (OK)"); |
ansond | 0:a3fc1c6ef150 | 439 | } |
ansond | 0:a3fc1c6ef150 | 440 | this->m_connected = false; |
ansond | 0:a3fc1c6ef150 | 441 | return true; |
ansond | 0:a3fc1c6ef150 | 442 | } |
ansond | 0:a3fc1c6ef150 | 443 | |
ansond | 0:a3fc1c6ef150 | 444 | // check transport and process stuff |
ansond | 0:a3fc1c6ef150 | 445 | void MQTTTransport::checkAndProcess() { |
ansond | 0:a3fc1c6ef150 | 446 | // process any MQTT messages |
ansond | 0:a3fc1c6ef150 | 447 | if (this->m_mqtt != NULL && this->m_connected == true) { |
ansond | 0:a3fc1c6ef150 | 448 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 0:a3fc1c6ef150 | 449 | bool connected = this->m_mqtt->loop(); |
ansond | 0:a3fc1c6ef150 | 450 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 0:a3fc1c6ef150 | 451 | if (connected) { |
ansond | 0:a3fc1c6ef150 | 452 | this->logger()->blinkTransportRxLED(); |
ansond | 0:a3fc1c6ef150 | 453 | } |
ansond | 0:a3fc1c6ef150 | 454 | else { |
ansond | 0:a3fc1c6ef150 | 455 | this->logger()->log("Attempting reconnection to MQTT in checkAndProcess..."); |
ansond | 0:a3fc1c6ef150 | 456 | this->disconnect(); |
ansond | 0:a3fc1c6ef150 | 457 | this->connect(); |
ansond | 0:a3fc1c6ef150 | 458 | } |
ansond | 0:a3fc1c6ef150 | 459 | } |
ansond | 0:a3fc1c6ef150 | 460 | |
ansond | 0:a3fc1c6ef150 | 461 | // send a PING if time for it |
ansond | 0:a3fc1c6ef150 | 462 | --this->m_ping_countdown; |
ansond | 0:a3fc1c6ef150 | 463 | if (this->m_ping_countdown <= 0) { |
ansond | 0:a3fc1c6ef150 | 464 | //this->logger()->log("MQTT: Sending PING..."); |
ansond | 0:a3fc1c6ef150 | 465 | this->m_ping_countdown = MQTT_PING_COUNTDOWN; |
ansond | 0:a3fc1c6ef150 | 466 | this->sendPingMessage(); |
ansond | 0:a3fc1c6ef150 | 467 | } |
ansond | 0:a3fc1c6ef150 | 468 | } |