MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_ublox_ethernet

Dependencies:   C027 C12832 EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed

Committer:
ansond
Date:
Thu Feb 27 04:05:41 2014 +0000
Revision:
13:25448d92c205
Parent:
9:ff877db53cfd
Child:
14:0a6497a380a4
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 8:45f9a920e82c 26
ansond 7:f570eb3f38cd 27 // splitstring support
ansond 7:f570eb3f38cd 28 #include "splitstring.h"
ansond 0:ae2a45502448 29
ansond 0:ae2a45502448 30 // our transmitt instance
ansond 0:ae2a45502448 31 MQTTTransport *instance = NULL;
ansond 0:ae2a45502448 32
ansond 0:ae2a45502448 33 // MQTT callback to handle received messages
ansond 0:ae2a45502448 34 void _mqtt_message_handler(char *topic,char *payload,unsigned int length) {
ansond 6:34c07e145caa 35 if (instance != NULL) instance->processMessage(instance->getEndpointNameFromTopic(topic),payload,length);
ansond 0:ae2a45502448 36 }
ansond 0:ae2a45502448 37
ansond 0:ae2a45502448 38 // our MQTT client endpoint
ansond 0:ae2a45502448 39 PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler);
ansond 0:ae2a45502448 40
ansond 0:ae2a45502448 41 // default constructor
ansond 7:f570eb3f38cd 42 MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) {
ansond 0:ae2a45502448 43 this->m_mqtt = NULL;
ansond 0:ae2a45502448 44 instance = this;
ansond 7:f570eb3f38cd 45 this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 8:45f9a920e82c 46 this->initTopic();
ansond 0:ae2a45502448 47 }
ansond 0:ae2a45502448 48
ansond 0:ae2a45502448 49 // default destructor
ansond 0:ae2a45502448 50 MQTTTransport::~MQTTTransport() {
ansond 0:ae2a45502448 51 this->disconnect();
ansond 0:ae2a45502448 52 if (this->m_mqtt != NULL) delete this->m_mqtt;
ansond 0:ae2a45502448 53 }
ansond 0:ae2a45502448 54
ansond 8:45f9a920e82c 55 // init our topic
ansond 8:45f9a920e82c 56 void MQTTTransport::initTopic() {
ansond 8:45f9a920e82c 57 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 8:45f9a920e82c 58 char *endpoint_name = endpoint->getEndpointName();
ansond 8:45f9a920e82c 59 memset(this->m_topic,0,MQTT_IOC_TOPIC_LEN+1);
ansond 8:45f9a920e82c 60 sprintf(this->m_topic,MQTT_IOC_TOPIC,endpoint_name);
ansond 8:45f9a920e82c 61 }
ansond 8:45f9a920e82c 62
ansond 8:45f9a920e82c 63 // get our topic
ansond 8:45f9a920e82c 64 char *MQTTTransport::getTopic() { return this->m_topic; }
ansond 8:45f9a920e82c 65
ansond 6:34c07e145caa 66 // pull the endpoint name from the MQTT topic
ansond 6:34c07e145caa 67 char *MQTTTransport::getEndpointNameFromTopic(char *topic) {
ansond 6:34c07e145caa 68 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 8:45f9a920e82c 69 splitstring *tmp = new splitstring(topic);
ansond 8:45f9a920e82c 70 vector<string> data = tmp->split('/');
ansond 8:45f9a920e82c 71 if (data.size() > 0) {
ansond 8:45f9a920e82c 72 char *ep = (char *)data[data.size()-1].c_str();
ansond 8:45f9a920e82c 73 strncpy(this->m_endpoint_name,ep,strlen(ep));
ansond 8:45f9a920e82c 74 }
ansond 8:45f9a920e82c 75 if (tmp != NULL) delete tmp;
ansond 6:34c07e145caa 76 return this->m_endpoint_name;
ansond 6:34c07e145caa 77 }
ansond 6:34c07e145caa 78
ansond 0:ae2a45502448 79 // process a MQTT Message
ansond 6:34c07e145caa 80 void MQTTTransport::processMessage(char *message_name,char *payload, unsigned int len) {
ansond 7:f570eb3f38cd 81 char *message_type = "";
ansond 7:f570eb3f38cd 82 char *message_verb = "";
ansond 7:f570eb3f38cd 83 char *message_value = "";
ansond 7:f570eb3f38cd 84 char *message_opt = "";
ansond 7:f570eb3f38cd 85
ansond 6:34c07e145caa 86 // get our endpoint
ansond 0:ae2a45502448 87 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 7:f570eb3f38cd 88 char *endpoint_name = endpoint->getEndpointName();
ansond 6:34c07e145caa 89
ansond 0:ae2a45502448 90 // DEBUG
ansond 8:45f9a920e82c 91 //this->logger()->log("Endpoint:[%s] Target: [%s]",endpoint_name,message_name);
ansond 7:f570eb3f38cd 92
ansond 3:3db076b9d380 93 // only respond if its for our node
ansond 8:45f9a920e82c 94 if (strcmp(endpoint_name,message_name) == 0) {
ansond 7:f570eb3f38cd 95 // format of the MQTT message: message_type:verb|Parameter_X:value|keyword:optional_data
ansond 7:f570eb3f38cd 96 splitstring *tmp = new splitstring(payload);
ansond 7:f570eb3f38cd 97 vector<string> data = tmp->split(':');
ansond 7:f570eb3f38cd 98 if (data.size() > 0) message_type = (char *)data[0].c_str();
ansond 7:f570eb3f38cd 99 if (data.size() > 1) message_verb = (char *)data[1].c_str();
ansond 7:f570eb3f38cd 100 if (data.size() > 2) message_value = (char *)data[2].c_str();
ansond 7:f570eb3f38cd 101 if (data.size() > 3) message_opt = (char *)data[3].c_str();
ansond 7:f570eb3f38cd 102
ansond 7:f570eb3f38cd 103 // DEBUG
ansond 8:45f9a920e82c 104 //this->logger()->log("Type: %s Name: %s Verb: %s Value: %s",message_type,message_name,message_verb,message_value);
ansond 8:45f9a920e82c 105
ansond 3:3db076b9d380 106 // load endpoints
ansond 13:25448d92c205 107 if (message_type != NULL && strcmp(message_type,IOC_ENDPOINT_VERB) == 0) {
ansond 13:25448d92c205 108 if (message_name != NULL && strcmp(message_name,IOC_ENDPOINT_ALL_VERB) == 0) {
ansond 13:25448d92c205 109 if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) {
ansond 13:25448d92c205 110 // load up our endpoints
ansond 13:25448d92c205 111 endpoint->loadEndpoints();
ansond 3:3db076b9d380 112 }
ansond 13:25448d92c205 113 if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
ansond 13:25448d92c205 114 // update our endpoints
ansond 13:25448d92c205 115 endpoint->updateEndpoints();
ansond 0:ae2a45502448 116 }
ansond 0:ae2a45502448 117 }
ansond 3:3db076b9d380 118 else {
ansond 3:3db076b9d380 119 // destined for our lights?
ansond 3:3db076b9d380 120 int index = -1;
ansond 3:3db076b9d380 121 if (message_name != NULL) {
ansond 9:ff877db53cfd 122 index = endpoint->indexOfLight((char *)message_name);
ansond 3:3db076b9d380 123 if (index >= 0) {
ansond 13:25448d92c205 124 if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
ansond 3:3db076b9d380 125 // update our endpoint
ansond 13:25448d92c205 126 endpoint->updateEndpoints(index);
ansond 3:3db076b9d380 127 }
ansond 3:3db076b9d380 128 }
ansond 3:3db076b9d380 129 }
ansond 3:3db076b9d380 130 }
ansond 0:ae2a45502448 131 }
ansond 3:3db076b9d380 132
ansond 3:3db076b9d380 133 // change a resource value
ansond 13:25448d92c205 134 if (message_type != NULL && strcmp(message_type,IOC_CHANGE_VERB) == 0) {
ansond 8:45f9a920e82c 135 if (message_name != NULL) {
ansond 3:3db076b9d380 136 // destined for our lights?
ansond 3:3db076b9d380 137 int index = endpoint->indexOfLight((char *)message_name);
ansond 3:3db076b9d380 138 if (index >= 0) {
ansond 3:3db076b9d380 139 if (message_verb != NULL) {
ansond 3:3db076b9d380 140 // map the parameter to one of ours
ansond 3:3db076b9d380 141 char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb);
ansond 3:3db076b9d380 142 if (mapped_resource != NULL) {
ansond 3:3db076b9d380 143 if (message_value != NULL) {
ansond 8:45f9a920e82c 144 EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index);
ansond 8:45f9a920e82c 145 bool success = factory->setResourceValue(mapped_resource,message_value);
ansond 8:45f9a920e82c 146
ansond 8:45f9a920e82c 147 // end the resource value back over MQTT
ansond 8:45f9a920e82c 148 this->sendResult(message_name,message_verb,message_value,success);
ansond 3:3db076b9d380 149 }
ansond 3:3db076b9d380 150 }
ansond 3:3db076b9d380 151 }
ansond 3:3db076b9d380 152 }
ansond 3:3db076b9d380 153 }
ansond 3:3db076b9d380 154 }
ansond 3:3db076b9d380 155
ansond 3:3db076b9d380 156 // get a resource value
ansond 13:25448d92c205 157 if (message_type != NULL && strcmp(message_type,IOC_REQUEST_VALUE_VERB) == 0) {
ansond 3:3db076b9d380 158 if (message_name != NULL) {
ansond 3:3db076b9d380 159 // destined for our lights?
ansond 3:3db076b9d380 160 int index = endpoint->indexOfLight((char *)message_name);
ansond 3:3db076b9d380 161 if (index >= 0) {
ansond 3:3db076b9d380 162 if (message_verb != NULL) {
ansond 3:3db076b9d380 163 // map the parameter to one of ours
ansond 13:25448d92c205 164 char *mapped_resource = this->mapIOCResourceToEndpointResource((char *)message_verb);
ansond 3:3db076b9d380 165 if (mapped_resource != NULL) {
ansond 8:45f9a920e82c 166 EmulatedResourceFactory *factory = (EmulatedResourceFactory *)endpoint->getResources(index);
ansond 8:45f9a920e82c 167 message_value = factory->getResourceValue((char *)mapped_resource);
ansond 8:45f9a920e82c 168 bool success = false; if (message_value != NULL) success = true;
ansond 8:45f9a920e82c 169
ansond 8:45f9a920e82c 170 // log resource get
ansond 8:45f9a920e82c 171 if (success) this->logger()->log("Resource: %s (%s) Value: %s",message_verb,mapped_resource,message_value);
ansond 3:3db076b9d380 172
ansond 3:3db076b9d380 173 // end the resource value back over MQTT
ansond 8:45f9a920e82c 174 this->sendResult(message_name,message_verb,message_value,success);
ansond 3:3db076b9d380 175 }
ansond 3:3db076b9d380 176 }
ansond 3:3db076b9d380 177 }
ansond 0:ae2a45502448 178 }
ansond 0:ae2a45502448 179 }
ansond 7:f570eb3f38cd 180
ansond 7:f570eb3f38cd 181 // clean up
ansond 8:45f9a920e82c 182 if (tmp != NULL) delete tmp;
ansond 0:ae2a45502448 183 }
ansond 3:3db076b9d380 184 else {
ansond 3:3db076b9d380 185 // message not bound for our node
ansond 3:3db076b9d380 186 this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name);
ansond 7:f570eb3f38cd 187 }
ansond 0:ae2a45502448 188 }
ansond 0:ae2a45502448 189
ansond 8:45f9a920e82c 190 // send result back to MQTT
ansond 8:45f9a920e82c 191 void MQTTTransport::sendResult(char *endpoint_name,char *resource_name,char *value,bool success) {
ansond 8:45f9a920e82c 192 if (this->m_connected == true) {
ansond 8:45f9a920e82c 193 // send the response back to MQTT
ansond 8:45f9a920e82c 194 this->logger()->log("Sending Response back to MQTT...");
ansond 8:45f9a920e82c 195 char message[MAX_MQTT_MESSAGE_LENGTH+1];
ansond 8:45f9a920e82c 196 memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1);
ansond 13:25448d92c205 197 char *str_success = IOC_RESPONSE_OK; if (!success) str_success = IOC_RESPONSE_FAILED;
ansond 13:25448d92c205 198 sprintf(message,IOC_RESPONSE_TEMPLATE,IOC_RESPONSE_VERB,endpoint_name,resource_name,value,str_success);
ansond 8:45f9a920e82c 199 bool sent = this->m_mqtt->publish(this->getTopic(),message,strlen(message));
ansond 8:45f9a920e82c 200 if (sent) {
ansond 8:45f9a920e82c 201 this->logger()->log("Result sent successfully");
ansond 8:45f9a920e82c 202 this->logger()->blinkMQTTTransportTxLED();
ansond 8:45f9a920e82c 203 }
ansond 8:45f9a920e82c 204 else {
ansond 8:45f9a920e82c 205 this->logger()->log("Result send FAILED");
ansond 8:45f9a920e82c 206 }
ansond 8:45f9a920e82c 207 }
ansond 8:45f9a920e82c 208 else {
ansond 8:45f9a920e82c 209 // unable to send the response
ansond 8:45f9a920e82c 210 this->logger()->log("Unable to send response back to MQTT. Not connected.");
ansond 8:45f9a920e82c 211 }
ansond 0:ae2a45502448 212 }
ansond 0:ae2a45502448 213
ansond 13:25448d92c205 214 char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->m_map->iocNameToEndpointName(ioc_name); }
ansond 0:ae2a45502448 215
ansond 0:ae2a45502448 216 char *MQTTTransport::makeID(char *id_template,char *buffer) {
ansond 0:ae2a45502448 217 srand(time(0));
ansond 0:ae2a45502448 218 srand(rand());
ansond 0:ae2a45502448 219 sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE);
ansond 0:ae2a45502448 220 return buffer;
ansond 0:ae2a45502448 221 }
ansond 0:ae2a45502448 222
ansond 0:ae2a45502448 223 // connect up MQTT
ansond 0:ae2a45502448 224 bool MQTTTransport::connect() {
ansond 0:ae2a45502448 225 char mqtt_id[MQTT_ENDPOINT_IDLEN+1];
ansond 0:ae2a45502448 226 memset(mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1));
ansond 0:ae2a45502448 227 if (this->m_connected == false) {
ansond 0:ae2a45502448 228 this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT);
ansond 0:ae2a45502448 229 this->m_mqtt = &_mqtt;
ansond 0:ae2a45502448 230 if (this->m_mqtt != NULL) {
ansond 0:ae2a45502448 231 char *id = this->makeID(MQTT_ENDPOINT_ID,mqtt_id);
ansond 0:ae2a45502448 232 this->logger()->log("MQTT Connect: ID: %s...",id);
ansond 0:ae2a45502448 233 if (this->m_mqtt->connect(id)) {
ansond 8:45f9a920e82c 234 this->logger()->log("MQTT Subscribe: Topic: %s...",this->getTopic());
ansond 8:45f9a920e82c 235 if (this->m_mqtt->subscribe(this->getTopic())) {
ansond 0:ae2a45502448 236 this->logger()->log("MQTT CONNECTED.");
ansond 0:ae2a45502448 237 this->m_connected = true;
ansond 0:ae2a45502448 238 }
ansond 0:ae2a45502448 239 else {
ansond 8:45f9a920e82c 240 this->logger()->log("MQTT Subscribe: Topic: %s FAILED",this->getTopic());
ansond 0:ae2a45502448 241 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 242 this->m_connected = false;
ansond 0:ae2a45502448 243 }
ansond 0:ae2a45502448 244 }
ansond 0:ae2a45502448 245 else {
ansond 0:ae2a45502448 246 this->logger()->log("MQTT Connect: ID: %s FAILED",id);
ansond 0:ae2a45502448 247 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 248 this->m_connected = false;
ansond 0:ae2a45502448 249 }
ansond 0:ae2a45502448 250 }
ansond 0:ae2a45502448 251 else {
ansond 0:ae2a45502448 252 this->logger()->log("MQTT Unable to allocate new instance");
ansond 0:ae2a45502448 253 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 254 this->m_connected = false;
ansond 0:ae2a45502448 255 }
ansond 0:ae2a45502448 256 }
ansond 0:ae2a45502448 257 else {
ansond 0:ae2a45502448 258 this->logger()->log("MQTT already connected (OK)");
ansond 0:ae2a45502448 259 }
ansond 0:ae2a45502448 260 return this->m_connected;
ansond 0:ae2a45502448 261 }
ansond 0:ae2a45502448 262
ansond 0:ae2a45502448 263 // disconnect from MQTT
ansond 0:ae2a45502448 264 bool MQTTTransport::disconnect() {
ansond 0:ae2a45502448 265 if (this->m_mqtt != NULL) {
ansond 8:45f9a920e82c 266 this->logger()->log("MQTT Unsubscribing from: %s...",this->getTopic());
ansond 8:45f9a920e82c 267 this->m_mqtt->unsubscribe(this->getTopic());
ansond 0:ae2a45502448 268 this->logger()->log("MQTT Disconnecting...");
ansond 0:ae2a45502448 269 this->m_mqtt->disconnect();
ansond 0:ae2a45502448 270 }
ansond 0:ae2a45502448 271 else {
ansond 0:ae2a45502448 272 this->logger()->log("MQTT already disconnected (OK)");
ansond 0:ae2a45502448 273 }
ansond 0:ae2a45502448 274 this->m_connected = false;
ansond 0:ae2a45502448 275 return true;
ansond 0:ae2a45502448 276 }
ansond 0:ae2a45502448 277
ansond 0:ae2a45502448 278 // check transport and process stuff
ansond 0:ae2a45502448 279 void MQTTTransport::checkAndProcess() {
ansond 0:ae2a45502448 280 if (this->m_mqtt != NULL && this->m_connected == true) {
ansond 0:ae2a45502448 281 this->m_mqtt->loop();
ansond 0:ae2a45502448 282 this->logger()->blinkMQTTTransportRxLED();
ansond 0:ae2a45502448 283 }
ansond 0:ae2a45502448 284 }