MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_ublox_cellular

Dependencies:   C027_Support C12832 StatusReporter LM75B MQTT-ansond c027_radios endpoint_core endpoint_mqtt mbed-rtos mbed

Committer:
ansond
Date:
Mon Mar 17 03:44:56 2014 +0000
Revision:
133:e69d03f4eb5c
Parent:
132:563a1ee99efc
Child:
136:ea8f3900bc13
updates

Who changed what in which revision?

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