mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Committer:
ansond
Date:
Thu Apr 10 03:16:57 2014 +0000
Revision:
32:9a024a6af2fb
Parent:
31:e5950e0677be
Child:
42:297585f8e7bd
updates

Who changed what in which revision?

UserRevisionLine numberNew 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 7:8a4a61202b36 95 memset(this->m_endpoint_name,0,PERSONALITY_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 7:8a4a61202b36 171 endpoint->loadPersonalities();
ansond 7:8a4a61202b36 172 endpoint->updatePersonalities();
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 7:8a4a61202b36 176 endpoint->loadPersonalities();
ansond 7:8a4a61202b36 177 endpoint->updatePersonalities();
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 7:8a4a61202b36 184 endpoint->updatePersonalities();
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 7:8a4a61202b36 190 index = endpoint->indexOfPersonality((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 7:8a4a61202b36 194 endpoint->updatePersonality(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 7:8a4a61202b36 206 int index = endpoint->indexOfPersonality((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 31:e5950e0677be 220
ansond 31:e5950e0677be 221 // for Dimming, we also want to refresh our record (in whole) at the IOC
ansond 31:e5950e0677be 222 if (this->isDimmingResource(message_verb)) {
ansond 31:e5950e0677be 223 // send a fresh update to the IOC - just us...
ansond 31:e5950e0677be 224 int index = endpoint->indexOfPersonality((char *)message_name);
ansond 31:e5950e0677be 225 if (index >= 0) endpoint->updatePersonality(index);
ansond 31:e5950e0677be 226 }
ansond 0:a3fc1c6ef150 227 }
ansond 0:a3fc1c6ef150 228 }
ansond 0:a3fc1c6ef150 229 }
ansond 0:a3fc1c6ef150 230 }
ansond 0:a3fc1c6ef150 231
ansond 0:a3fc1c6ef150 232 // get a resource value
ansond 0:a3fc1c6ef150 233 if (message_type != NULL && strcmp(message_type,IOC_REQUEST_VALUE_VERB) == 0) {
ansond 0:a3fc1c6ef150 234 if (message_name != NULL) {
ansond 0:a3fc1c6ef150 235 // destined for our lights?
ansond 7:8a4a61202b36 236 int index = endpoint->indexOfPersonality((char *)message_name);
ansond 0:a3fc1c6ef150 237 if (index >= 0) {
ansond 0:a3fc1c6ef150 238 if (message_verb != NULL) {
ansond 0:a3fc1c6ef150 239 // map the parameter to one of ours
ansond 0:a3fc1c6ef150 240 char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb);
ansond 0:a3fc1c6ef150 241 if (mapped_resource != NULL) {
ansond 0:a3fc1c6ef150 242 EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index);
ansond 0:a3fc1c6ef150 243 strcpy(message_value,factory->getResourceValue((char *)mapped_resource));
ansond 0:a3fc1c6ef150 244 bool success = false; if (message_value != NULL) success = true;
ansond 0:a3fc1c6ef150 245
ansond 0:a3fc1c6ef150 246 // log resource get
ansond 0:a3fc1c6ef150 247 if (success) this->logger()->log("Resource: %s (%s) Value: %s",message_verb,mapped_resource,message_value);
ansond 0:a3fc1c6ef150 248
ansond 0:a3fc1c6ef150 249 // end the resource value back over MQTT
ansond 0:a3fc1c6ef150 250 this->sendResult(message_name,message_verb,message_value,success);
ansond 0:a3fc1c6ef150 251 }
ansond 0:a3fc1c6ef150 252 }
ansond 0:a3fc1c6ef150 253 }
ansond 0:a3fc1c6ef150 254 }
ansond 0:a3fc1c6ef150 255 }
ansond 0:a3fc1c6ef150 256 }
ansond 0:a3fc1c6ef150 257 else {
ansond 0:a3fc1c6ef150 258 // message not bound for our node
ansond 0:a3fc1c6ef150 259 //this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name);
ansond 0:a3fc1c6ef150 260 ;
ansond 0:a3fc1c6ef150 261 }
ansond 0:a3fc1c6ef150 262 }
ansond 0:a3fc1c6ef150 263
ansond 31:e5950e0677be 264 // is this the dimming resource?
ansond 31:e5950e0677be 265 bool MQTTTransport::isDimmingResource(char *resource) {
ansond 31:e5950e0677be 266 bool isDimming = false;
ansond 31:e5950e0677be 267
ansond 31:e5950e0677be 268 if (resource != NULL && strcmp(resource,"Dimming") == 0) isDimming = true;
ansond 31:e5950e0677be 269
ansond 31:e5950e0677be 270 return isDimming;
ansond 31:e5950e0677be 271 }
ansond 31:e5950e0677be 272
ansond 0:a3fc1c6ef150 273 // send result back to MQTT
ansond 0:a3fc1c6ef150 274 void MQTTTransport::sendResult(char *endpoint_name,char *resource_name,char *value,bool success) {
ansond 0:a3fc1c6ef150 275 if (this->m_connected == true) {
ansond 0:a3fc1c6ef150 276 // send the response back to MQTT
ansond 0:a3fc1c6ef150 277 this->logger()->log("Sending Response back to MQTT...");
ansond 0:a3fc1c6ef150 278 char message[MAX_MQTT_MESSAGE_LENGTH+1];
ansond 0:a3fc1c6ef150 279 memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1);
ansond 0:a3fc1c6ef150 280 char *str_success = IOC_RESPONSE_OK; if (!success) str_success = IOC_RESPONSE_FAILED;
ansond 0:a3fc1c6ef150 281 sprintf(message,IOC_RESPONSE_TEMPLATE,IOC_RESPONSE_VERB,endpoint_name,resource_name,value,str_success);
ansond 0:a3fc1c6ef150 282 if (network_mutex != NULL) network_mutex->lock();
ansond 0:a3fc1c6ef150 283 bool sent = this->m_mqtt->publish(this->getTopic(),message,strlen(message));
ansond 0:a3fc1c6ef150 284 if (network_mutex != NULL) network_mutex->unlock();
ansond 0:a3fc1c6ef150 285 if (sent) {
ansond 0:a3fc1c6ef150 286 this->logger()->log("Result sent successfully");
ansond 0:a3fc1c6ef150 287 this->logger()->blinkTransportTxLED();
ansond 0:a3fc1c6ef150 288 }
ansond 0:a3fc1c6ef150 289 else {
ansond 0:a3fc1c6ef150 290 this->logger()->log("Result send FAILED");
ansond 0:a3fc1c6ef150 291 }
ansond 0:a3fc1c6ef150 292 }
ansond 0:a3fc1c6ef150 293 else {
ansond 0:a3fc1c6ef150 294 // unable to send the response
ansond 0:a3fc1c6ef150 295 this->logger()->log("Unable to send response back to MQTT. Not connected.");
ansond 0:a3fc1c6ef150 296 }
ansond 0:a3fc1c6ef150 297 }
ansond 0:a3fc1c6ef150 298
ansond 0:a3fc1c6ef150 299 char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->getMap()->iocNameToEndpointName(ioc_name); }
ansond 0:a3fc1c6ef150 300
ansond 0:a3fc1c6ef150 301 char *MQTTTransport::makeID(char *id_template,char *buffer) {
ansond 0:a3fc1c6ef150 302 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 0:a3fc1c6ef150 303 int instance_id = rand()%100;
ansond 0:a3fc1c6ef150 304 if (endpoint != NULL) instance_id = endpoint->getInstanceID();
ansond 0:a3fc1c6ef150 305 srand(time(0));
ansond 0:a3fc1c6ef150 306 srand(rand());
ansond 0:a3fc1c6ef150 307 sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE,instance_id);
ansond 0:a3fc1c6ef150 308 return buffer;
ansond 0:a3fc1c6ef150 309 }
ansond 0:a3fc1c6ef150 310
ansond 0:a3fc1c6ef150 311 // is this message a PONG message?
ansond 0:a3fc1c6ef150 312 bool MQTTTransport::isPongMessage(char *topic,char *payload,int payload_length) {
ansond 0:a3fc1c6ef150 313 bool isPong = false;
ansond 0:a3fc1c6ef150 314 char verb[MQTT_PING_VERB_LEN+1];
ansond 0:a3fc1c6ef150 315 char end[MQTT_PING_VERB_LEN+1];
ansond 0:a3fc1c6ef150 316 int counter = 0;
ansond 0:a3fc1c6ef150 317
ansond 0:a3fc1c6ef150 318 // clean
ansond 0:a3fc1c6ef150 319 memset(verb,0,MQTT_PING_VERB_LEN+1);
ansond 0:a3fc1c6ef150 320 memset(end,0,MQTT_PING_VERB_LEN+1);
ansond 0:a3fc1c6ef150 321
ansond 0:a3fc1c6ef150 322 // make sure this is for us...
ansond 0:a3fc1c6ef150 323 char *topic_ep_name = this->getEndpointNameFromTopic(topic);
ansond 0:a3fc1c6ef150 324 if (topic_ep_name != NULL && strcmp(topic_ep_name,this->m_endpoint_name) == 0) {
ansond 0:a3fc1c6ef150 325 // parse the payload
ansond 0:a3fc1c6ef150 326 for(int i=0;payload != NULL && i<payload_length;++i) if (payload[i] == ':') payload[i] = ' ';
ansond 0:a3fc1c6ef150 327 sscanf(payload,"%s%d",verb,&counter);
ansond 0:a3fc1c6ef150 328
ansond 0:a3fc1c6ef150 329 // check the contents to make sure its for us...
ansond 0:a3fc1c6ef150 330 //this->logger()->log("isPongMessage: verb: %s counter %d ping_counter: %d",verb,counter,this->m_ping_counter);
ansond 0:a3fc1c6ef150 331 if (strcmp(verb,"pong") == 0 && counter == this->m_ping_counter) {
ansond 0:a3fc1c6ef150 332 // its a PONG message to our PING...
ansond 0:a3fc1c6ef150 333 isPong = true;
ansond 0:a3fc1c6ef150 334 }
ansond 0:a3fc1c6ef150 335 }
ansond 0:a3fc1c6ef150 336
ansond 0:a3fc1c6ef150 337 // return isPong status
ansond 0:a3fc1c6ef150 338 return isPong;
ansond 0:a3fc1c6ef150 339 }
ansond 0:a3fc1c6ef150 340
ansond 0:a3fc1c6ef150 341
ansond 0:a3fc1c6ef150 342 // process this PONG message
ansond 0:a3fc1c6ef150 343 void MQTTTransport::processPongMessage(char *payload,int payload_length) {
ansond 0:a3fc1c6ef150 344 // DEBUG
ansond 0:a3fc1c6ef150 345 //this->logger()->log("Received PONG: counter=%d",this->m_ping_counter);
ansond 0:a3fc1c6ef150 346
ansond 0:a3fc1c6ef150 347 // simply increment the counter
ansond 0:a3fc1c6ef150 348 ++this->m_ping_counter;
ansond 0:a3fc1c6ef150 349
ansond 0:a3fc1c6ef150 350 // reset counter if maxed
ansond 0:a3fc1c6ef150 351 if (this->m_ping_counter >= MQTT_MAX_COUNTER) this->m_ping_counter = 1;
ansond 0:a3fc1c6ef150 352 }
ansond 0:a3fc1c6ef150 353
ansond 0:a3fc1c6ef150 354 // send a PING message
ansond 0:a3fc1c6ef150 355 bool MQTTTransport::sendPingMessage() {
ansond 0:a3fc1c6ef150 356 bool sent = false;
ansond 0:a3fc1c6ef150 357 char message[MAX_MQTT_MESSAGE_LENGTH+1];
ansond 0:a3fc1c6ef150 358
ansond 0:a3fc1c6ef150 359 // initialize...
ansond 0:a3fc1c6ef150 360 memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1);
ansond 0:a3fc1c6ef150 361
ansond 0:a3fc1c6ef150 362 // build message
ansond 0:a3fc1c6ef150 363 sprintf(message,"ping:%s:%d:",this->m_endpoint_name,this->m_ping_counter);
ansond 0:a3fc1c6ef150 364
ansond 0:a3fc1c6ef150 365 // send the message over the ping/pong topic
ansond 0:a3fc1c6ef150 366 //this->logger()->log("Sending PING: counter=%d",this->m_ping_counter);
ansond 0:a3fc1c6ef150 367 if (network_mutex != NULL) network_mutex->lock();
ansond 0:a3fc1c6ef150 368 sent = this->m_mqtt->publish(MQTT_PING_SEND_TOPIC,message,strlen(message));
ansond 0:a3fc1c6ef150 369 if (network_mutex != NULL) network_mutex->unlock();
ansond 0:a3fc1c6ef150 370 if (sent) {
ansond 0:a3fc1c6ef150 371 // send succeeded
ansond 0:a3fc1c6ef150 372 //this->logger()->log("PING %d sent successfully",this->m_ping_counter);
ansond 0:a3fc1c6ef150 373 this->logger()->blinkTransportTxLED();
ansond 0:a3fc1c6ef150 374
ansond 0:a3fc1c6ef150 375 // wait for 1 second
ansond 0:a3fc1c6ef150 376 wait_ms(1000);
ansond 0:a3fc1c6ef150 377 }
ansond 0:a3fc1c6ef150 378 else {
ansond 20:ee03f10c074e 379 // fail silently now...
ansond 20:ee03f10c074e 380 ;
ansond 20:ee03f10c074e 381
ansond 0:a3fc1c6ef150 382 // send failed! - reconnect
ansond 20:ee03f10c074e 383 //this->logger()->log("PING send %d FAILED... (re)connecting...",this->m_ping_counter);
ansond 0:a3fc1c6ef150 384
ansond 0:a3fc1c6ef150 385 // attempt reconnect
ansond 20:ee03f10c074e 386 //this->logger()->log("PING send failed - re-connecting MQTT...");
ansond 20:ee03f10c074e 387 //this->disconnect();
ansond 20:ee03f10c074e 388 //sent = this->connect();
ansond 20:ee03f10c074e 389 //if (sent) this->logger()->log("PING %d: MQTT reconnection successful...",this->m_ping_counter);
ansond 20:ee03f10c074e 390 //else this->logger()->log("PING %d: resend failed giving up...",this->m_ping_counter);
ansond 0:a3fc1c6ef150 391 }
ansond 0:a3fc1c6ef150 392
ansond 0:a3fc1c6ef150 393 // return our status
ansond 0:a3fc1c6ef150 394 return sent;
ansond 0:a3fc1c6ef150 395 }
ansond 0:a3fc1c6ef150 396
ansond 29:ac6390032cec 397 static char _mqtt_id[MQTT_ENDPOINT_IDLEN+1];
ansond 29:ac6390032cec 398
ansond 0:a3fc1c6ef150 399 // connect up MQTT
ansond 0:a3fc1c6ef150 400 bool MQTTTransport::connect() {
ansond 29:ac6390032cec 401 memset(_mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1));
ansond 29:ac6390032cec 402 //if (network_mutex != NULL) network_mutex->lock();
ansond 0:a3fc1c6ef150 403 if (this->m_connected == false) {
ansond 0:a3fc1c6ef150 404 this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT);
ansond 0:a3fc1c6ef150 405 this->m_mqtt = &_mqtt;
ansond 0:a3fc1c6ef150 406 if (this->m_mqtt != NULL) {
ansond 29:ac6390032cec 407 char *id = this->makeID(MQTT_ENDPOINT_ID,_mqtt_id);
ansond 0:a3fc1c6ef150 408 this->logger()->log("MQTT Connect: ID: %s...",id);
ansond 0:a3fc1c6ef150 409 if (this->m_mqtt->connect(id)) {
ansond 0:a3fc1c6ef150 410 this->logger()->log("MQTT Subscribe: Topic: %s...",this->getTopic());
ansond 0:a3fc1c6ef150 411 if (this->m_mqtt->subscribe(this->getTopic())) {
ansond 0:a3fc1c6ef150 412 if (this->m_mqtt->subscribe(MQTT_IOC_ALL_TOPIC)) {
ansond 0:a3fc1c6ef150 413 this->logger()->log("MQTT CONNECTED.");
ansond 0:a3fc1c6ef150 414 this->m_connected = true;
ansond 0:a3fc1c6ef150 415 }
ansond 0:a3fc1c6ef150 416 else {
ansond 0:a3fc1c6ef150 417 this->logger()->log("MQTT Subscribe: Topic: %s FAILED",MQTT_IOC_ALL_TOPIC);
ansond 0:a3fc1c6ef150 418 this->logger()->turnLEDRed();
ansond 0:a3fc1c6ef150 419 this->m_connected = false;
ansond 0:a3fc1c6ef150 420 }
ansond 0:a3fc1c6ef150 421 }
ansond 0:a3fc1c6ef150 422 else {
ansond 0:a3fc1c6ef150 423 this->logger()->log("MQTT Subscribe: Topic: %s FAILED",this->getTopic());
ansond 0:a3fc1c6ef150 424 this->logger()->turnLEDRed();
ansond 0:a3fc1c6ef150 425 this->m_connected = false;
ansond 0:a3fc1c6ef150 426 }
ansond 0:a3fc1c6ef150 427 }
ansond 0:a3fc1c6ef150 428 else {
ansond 0:a3fc1c6ef150 429 this->logger()->log("MQTT Connect: ID: %s FAILED",id);
ansond 0:a3fc1c6ef150 430 this->logger()->turnLEDRed();
ansond 0:a3fc1c6ef150 431 this->m_connected = false;
ansond 0:a3fc1c6ef150 432 }
ansond 0:a3fc1c6ef150 433 }
ansond 0:a3fc1c6ef150 434 else {
ansond 0:a3fc1c6ef150 435 this->logger()->log("MQTT Unable to allocate new instance");
ansond 0:a3fc1c6ef150 436 this->logger()->turnLEDRed();
ansond 0:a3fc1c6ef150 437 this->m_connected = false;
ansond 0:a3fc1c6ef150 438 }
ansond 0:a3fc1c6ef150 439 }
ansond 0:a3fc1c6ef150 440 else {
ansond 0:a3fc1c6ef150 441 this->logger()->log("MQTT already connected (OK)");
ansond 0:a3fc1c6ef150 442 }
ansond 29:ac6390032cec 443 //if (network_mutex != NULL) network_mutex->unlock();
ansond 0:a3fc1c6ef150 444 return this->m_connected;
ansond 0:a3fc1c6ef150 445 }
ansond 0:a3fc1c6ef150 446
ansond 0:a3fc1c6ef150 447 // disconnect from MQTT
ansond 0:a3fc1c6ef150 448 bool MQTTTransport::disconnect() {
ansond 0:a3fc1c6ef150 449 if (this->m_mqtt != NULL) {
ansond 0:a3fc1c6ef150 450 this->logger()->log("MQTT Unsubscribing from: %s...",this->getTopic());
ansond 0:a3fc1c6ef150 451 if (network_mutex != NULL) network_mutex->lock();
ansond 0:a3fc1c6ef150 452 this->m_mqtt->unsubscribe(this->getTopic());
ansond 0:a3fc1c6ef150 453 if (network_mutex != NULL) network_mutex->unlock();
ansond 0:a3fc1c6ef150 454 this->logger()->log("MQTT Disconnecting...");
ansond 0:a3fc1c6ef150 455 this->m_mqtt->disconnect();
ansond 0:a3fc1c6ef150 456 }
ansond 0:a3fc1c6ef150 457 else {
ansond 0:a3fc1c6ef150 458 this->logger()->log("MQTT already disconnected (OK)");
ansond 0:a3fc1c6ef150 459 }
ansond 0:a3fc1c6ef150 460 this->m_connected = false;
ansond 0:a3fc1c6ef150 461 return true;
ansond 0:a3fc1c6ef150 462 }
ansond 0:a3fc1c6ef150 463
ansond 0:a3fc1c6ef150 464 // check transport and process stuff
ansond 0:a3fc1c6ef150 465 void MQTTTransport::checkAndProcess() {
ansond 0:a3fc1c6ef150 466 // process any MQTT messages
ansond 0:a3fc1c6ef150 467 if (this->m_mqtt != NULL && this->m_connected == true) {
ansond 0:a3fc1c6ef150 468 if (network_mutex != NULL) network_mutex->lock();
ansond 0:a3fc1c6ef150 469 bool connected = this->m_mqtt->loop();
ansond 0:a3fc1c6ef150 470 if (network_mutex != NULL) network_mutex->unlock();
ansond 0:a3fc1c6ef150 471 if (connected) {
ansond 0:a3fc1c6ef150 472 this->logger()->blinkTransportRxLED();
ansond 0:a3fc1c6ef150 473 }
ansond 32:9a024a6af2fb 474 //else {
ansond 32:9a024a6af2fb 475 // this->logger()->log("Attempting reconnection to MQTT in checkAndProcess...");
ansond 32:9a024a6af2fb 476 // this->disconnect();
ansond 32:9a024a6af2fb 477 // Thread::wait(15000);
ansond 32:9a024a6af2fb 478 // this->connect();
ansond 32:9a024a6af2fb 479 //}
ansond 0:a3fc1c6ef150 480 }
ansond 0:a3fc1c6ef150 481
ansond 0:a3fc1c6ef150 482 // send a PING if time for it
ansond 0:a3fc1c6ef150 483 --this->m_ping_countdown;
ansond 0:a3fc1c6ef150 484 if (this->m_ping_countdown <= 0) {
ansond 0:a3fc1c6ef150 485 //this->logger()->log("MQTT: Sending PING...");
ansond 0:a3fc1c6ef150 486 this->m_ping_countdown = MQTT_PING_COUNTDOWN;
ansond 0:a3fc1c6ef150 487 this->sendPingMessage();
ansond 0:a3fc1c6ef150 488 }
ansond 0:a3fc1c6ef150 489 }