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:
Wed Feb 26 18:59:08 2014 +0000
Revision:
7:f570eb3f38cd
Parent:
6:34c07e145caa
Child:
8:45f9a920e82c
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 7:f570eb3f38cd 24 // splitstring support
ansond 7:f570eb3f38cd 25 #include "splitstring.h"
ansond 0:ae2a45502448 26
ansond 0:ae2a45502448 27 // our transmitt instance
ansond 0:ae2a45502448 28 MQTTTransport *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 6:34c07e145caa 32 if (instance != NULL) instance->processMessage(instance->getEndpointNameFromTopic(topic),payload,length);
ansond 0:ae2a45502448 33 }
ansond 0:ae2a45502448 34
ansond 0:ae2a45502448 35 // our MQTT client endpoint
ansond 0:ae2a45502448 36 PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler);
ansond 0:ae2a45502448 37
ansond 0:ae2a45502448 38 // default constructor
ansond 7:f570eb3f38cd 39 MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) {
ansond 0:ae2a45502448 40 this->m_mqtt = NULL;
ansond 0:ae2a45502448 41 instance = this;
ansond 7:f570eb3f38cd 42 this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 0:ae2a45502448 43 }
ansond 0:ae2a45502448 44
ansond 0:ae2a45502448 45 // default destructor
ansond 0:ae2a45502448 46 MQTTTransport::~MQTTTransport() {
ansond 0:ae2a45502448 47 this->disconnect();
ansond 0:ae2a45502448 48 if (this->m_mqtt != NULL) delete this->m_mqtt;
ansond 0:ae2a45502448 49 }
ansond 0:ae2a45502448 50
ansond 6:34c07e145caa 51 // pull the endpoint name from the MQTT topic
ansond 6:34c07e145caa 52 char *MQTTTransport::getEndpointNameFromTopic(char *topic) {
ansond 6:34c07e145caa 53 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 6:34c07e145caa 54
ansond 6:34c07e145caa 55 // XXX until we get the splitter working, lets just default this name
ansond 6:34c07e145caa 56 char *tmp = ((MBEDEndpoint *)this->getEndpoint())->getEndpointName();
ansond 6:34c07e145caa 57 strncpy(this->m_endpoint_name,tmp,strlen(tmp));
ansond 6:34c07e145caa 58
ansond 6:34c07e145caa 59 // DEBUG
ansond 6:34c07e145caa 60 if (tmp == NULL) this->logger()->log("ERROR! endpointName is NULL");
ansond 6:34c07e145caa 61 else this->logger()->log("TMP: %s length=%d",tmp,strlen(tmp));
ansond 6:34c07e145caa 62
ansond 6:34c07e145caa 63 return this->m_endpoint_name;
ansond 6:34c07e145caa 64 }
ansond 6:34c07e145caa 65
ansond 0:ae2a45502448 66 // process a MQTT Message
ansond 6:34c07e145caa 67 void MQTTTransport::processMessage(char *message_name,char *payload, unsigned int len) {
ansond 7:f570eb3f38cd 68 char *message_type = "";
ansond 7:f570eb3f38cd 69 char *message_verb = "";
ansond 7:f570eb3f38cd 70 char *message_value = "";
ansond 7:f570eb3f38cd 71 char *message_opt = "";
ansond 7:f570eb3f38cd 72
ansond 7:f570eb3f38cd 73 bool enable = true;
ansond 6:34c07e145caa 74
ansond 6:34c07e145caa 75 // get our endpoint
ansond 0:ae2a45502448 76 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 7:f570eb3f38cd 77 char *endpoint_name = endpoint->getEndpointName();
ansond 6:34c07e145caa 78
ansond 0:ae2a45502448 79 // DEBUG
ansond 7:f570eb3f38cd 80 this->logger()->pause("MQTT Message: %s length=%d endpoint_name=%s",payload,len,message_name);
ansond 7:f570eb3f38cd 81
ansond 3:3db076b9d380 82 // only respond if its for our node
ansond 7:f570eb3f38cd 83 if (enable && strcmp(endpoint_name,message_name) == 0) {
ansond 3:3db076b9d380 84 // DEBUG
ansond 3:3db076b9d380 85 this->logger()->log("Message: %s bound for %s... processing...",payload,endpoint_name);
ansond 7:f570eb3f38cd 86
ansond 7:f570eb3f38cd 87 // format of the MQTT message: message_type:verb|Parameter_X:value|keyword:optional_data
ansond 7:f570eb3f38cd 88 splitstring *tmp = new splitstring(payload);
ansond 7:f570eb3f38cd 89 this->logger()->pause("DOUG1");
ansond 7:f570eb3f38cd 90 vector<string> data = tmp->split(':');
ansond 7:f570eb3f38cd 91 this->logger()->pause("DOUG2");
ansond 7:f570eb3f38cd 92 if (data.size() > 0) message_type = (char *)data[0].c_str();
ansond 7:f570eb3f38cd 93 this->logger()->pause("DOUG3");
ansond 7:f570eb3f38cd 94 if (data.size() > 1) message_verb = (char *)data[1].c_str();
ansond 7:f570eb3f38cd 95 this->logger()->pause("DOUG4");
ansond 7:f570eb3f38cd 96 if (data.size() > 2) message_value = (char *)data[2].c_str();
ansond 7:f570eb3f38cd 97 this->logger()->pause("DOUG5");
ansond 7:f570eb3f38cd 98 if (data.size() > 3) message_opt = (char *)data[3].c_str();
ansond 7:f570eb3f38cd 99 this->logger()->pause("DOUG6");
ansond 7:f570eb3f38cd 100
ansond 7:f570eb3f38cd 101 // DEBUG
ansond 7:f570eb3f38cd 102 this->logger()->log("Type: %s Name: %s Verb: %s Value: %s",message_type,message_name,message_verb,message_value);
ansond 7:f570eb3f38cd 103
ansond 7:f570eb3f38cd 104 return;
ansond 7:f570eb3f38cd 105
ansond 3:3db076b9d380 106 // load endpoints
ansond 3:3db076b9d380 107 if (message_type != NULL && strcmp(message_type,"Endpoint") == 0) {
ansond 3:3db076b9d380 108 if (message_name != NULL && strcmp(message_name,"all") == 0) {
ansond 3:3db076b9d380 109 if (message_verb != NULL && strcmp(message_verb,"load") == 0) {
ansond 3:3db076b9d380 110 // load up our endpoint
ansond 3:3db076b9d380 111 endpoint->loadEndpoint();
ansond 3:3db076b9d380 112 }
ansond 3:3db076b9d380 113 if (message_verb != NULL && strcmp(message_verb,"Update") == 0) {
ansond 0:ae2a45502448 114 // update our endpoint
ansond 0:ae2a45502448 115 endpoint->updateEndpoint();
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 3:3db076b9d380 122 endpoint->indexOfLight((char *)message_name);
ansond 3:3db076b9d380 123 if (index >= 0) {
ansond 3:3db076b9d380 124 if (message_verb != NULL && strcmp(message_verb,"Update") == 0) {
ansond 3:3db076b9d380 125 // update our endpoint
ansond 3:3db076b9d380 126 endpoint->updateEndpoint();
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 3:3db076b9d380 134 if (message_type != NULL && strcmp(message_type,"Change") == 0) {
ansond 3:3db076b9d380 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 3:3db076b9d380 144 ResourceFactory *factory = endpoint->getResources(index);
ansond 3:3db076b9d380 145 factory->setResourceValue(message_name,message_value);
ansond 3:3db076b9d380 146 }
ansond 3:3db076b9d380 147 }
ansond 3:3db076b9d380 148 }
ansond 3:3db076b9d380 149 }
ansond 3:3db076b9d380 150 }
ansond 3:3db076b9d380 151 }
ansond 3:3db076b9d380 152
ansond 3:3db076b9d380 153 // get a resource value
ansond 3:3db076b9d380 154 if (message_type != NULL && strcmp(message_type,"Get") == 0) {
ansond 3:3db076b9d380 155 if (message_name != NULL) {
ansond 3:3db076b9d380 156 // destined for our lights?
ansond 3:3db076b9d380 157 int index = endpoint->indexOfLight((char *)message_name);
ansond 3:3db076b9d380 158 if (index >= 0) {
ansond 3:3db076b9d380 159 if (message_verb != NULL) {
ansond 3:3db076b9d380 160 // map the parameter to one of ours
ansond 3:3db076b9d380 161 char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb);
ansond 3:3db076b9d380 162 if (mapped_resource != NULL) {
ansond 3:3db076b9d380 163 ResourceFactory *factory = endpoint->getResources(index);
ansond 3:3db076b9d380 164 char *resource_value = factory->getResourceValue((char *)message_name);
ansond 3:3db076b9d380 165
ansond 3:3db076b9d380 166 // end the resource value back over MQTT
ansond 3:3db076b9d380 167 this->sendResourceValue(message_name,message_verb,resource_value);
ansond 3:3db076b9d380 168 }
ansond 3:3db076b9d380 169 }
ansond 3:3db076b9d380 170 }
ansond 0:ae2a45502448 171 }
ansond 0:ae2a45502448 172 }
ansond 7:f570eb3f38cd 173
ansond 7:f570eb3f38cd 174 // clean up the array
ansond 7:f570eb3f38cd 175 //if (data != NULL) splitter->clear(data);
ansond 7:f570eb3f38cd 176
ansond 7:f570eb3f38cd 177 // clean up
ansond 7:f570eb3f38cd 178 //if (splitter != NULL) delete splitter;
ansond 0:ae2a45502448 179 }
ansond 3:3db076b9d380 180 else {
ansond 3:3db076b9d380 181 // message not bound for our node
ansond 3:3db076b9d380 182 this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name);
ansond 7:f570eb3f38cd 183 }
ansond 0:ae2a45502448 184 }
ansond 0:ae2a45502448 185
ansond 0:ae2a45502448 186 void MQTTTransport::sendResourceValue(char *endpoint_name,char *parameter_name,char *value) {
ansond 0:ae2a45502448 187 }
ansond 0:ae2a45502448 188
ansond 0:ae2a45502448 189 char *MQTTTransport::mapEndpointResourceToIOCResource(char *ioc_name) {
ansond 0:ae2a45502448 190 return this->m_map->getMBEDMappedName(ioc_name);
ansond 0:ae2a45502448 191 }
ansond 0:ae2a45502448 192
ansond 0:ae2a45502448 193 char *MQTTTransport::makeID(char *id_template,char *buffer) {
ansond 0:ae2a45502448 194 srand(time(0));
ansond 0:ae2a45502448 195 srand(rand());
ansond 0:ae2a45502448 196 sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE);
ansond 0:ae2a45502448 197 return buffer;
ansond 0:ae2a45502448 198 }
ansond 0:ae2a45502448 199
ansond 0:ae2a45502448 200 // connect up MQTT
ansond 0:ae2a45502448 201 bool MQTTTransport::connect() {
ansond 0:ae2a45502448 202 char mqtt_id[MQTT_ENDPOINT_IDLEN+1];
ansond 0:ae2a45502448 203 memset(mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1));
ansond 0:ae2a45502448 204 if (this->m_connected == false) {
ansond 0:ae2a45502448 205 this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT);
ansond 0:ae2a45502448 206 this->m_mqtt = &_mqtt;
ansond 0:ae2a45502448 207 if (this->m_mqtt != NULL) {
ansond 0:ae2a45502448 208 char *id = this->makeID(MQTT_ENDPOINT_ID,mqtt_id);
ansond 0:ae2a45502448 209 this->logger()->log("MQTT Connect: ID: %s...",id);
ansond 0:ae2a45502448 210 if (this->m_mqtt->connect(id)) {
ansond 0:ae2a45502448 211 this->logger()->log("MQTT Subscribe: Topic: %s...",MQTT_IOC_TOPIC);
ansond 0:ae2a45502448 212 if (this->m_mqtt->subscribe(MQTT_IOC_TOPIC)) {
ansond 0:ae2a45502448 213 this->logger()->log("MQTT CONNECTED.");
ansond 0:ae2a45502448 214 this->m_connected = true;
ansond 0:ae2a45502448 215 }
ansond 0:ae2a45502448 216 else {
ansond 0:ae2a45502448 217 this->logger()->log("MQTT Subscribe: Topic: %s FAILED",MQTT_IOC_TOPIC);
ansond 0:ae2a45502448 218 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 219 this->m_connected = false;
ansond 0:ae2a45502448 220 }
ansond 0:ae2a45502448 221 }
ansond 0:ae2a45502448 222 else {
ansond 0:ae2a45502448 223 this->logger()->log("MQTT Connect: ID: %s FAILED",id);
ansond 0:ae2a45502448 224 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 225 this->m_connected = false;
ansond 0:ae2a45502448 226 }
ansond 0:ae2a45502448 227 }
ansond 0:ae2a45502448 228 else {
ansond 0:ae2a45502448 229 this->logger()->log("MQTT Unable to allocate new instance");
ansond 0:ae2a45502448 230 this->logger()->turnLEDRed();
ansond 7:f570eb3f38cd 231 this->m_connected = false;
ansond 0:ae2a45502448 232 }
ansond 0:ae2a45502448 233 }
ansond 0:ae2a45502448 234 else {
ansond 0:ae2a45502448 235 this->logger()->log("MQTT already connected (OK)");
ansond 0:ae2a45502448 236 }
ansond 0:ae2a45502448 237 return this->m_connected;
ansond 0:ae2a45502448 238 }
ansond 0:ae2a45502448 239
ansond 0:ae2a45502448 240 // disconnect from MQTT
ansond 0:ae2a45502448 241 bool MQTTTransport::disconnect() {
ansond 0:ae2a45502448 242 if (this->m_mqtt != NULL) {
ansond 0:ae2a45502448 243 this->logger()->log("MQTT Unsubscribing from: %s...",MQTT_IOC_TOPIC);
ansond 0:ae2a45502448 244 this->m_mqtt->unsubscribe(MQTT_IOC_TOPIC);
ansond 0:ae2a45502448 245 this->logger()->log("MQTT Disconnecting...");
ansond 0:ae2a45502448 246 this->m_mqtt->disconnect();
ansond 0:ae2a45502448 247 }
ansond 0:ae2a45502448 248 else {
ansond 0:ae2a45502448 249 this->logger()->log("MQTT already disconnected (OK)");
ansond 0:ae2a45502448 250 }
ansond 0:ae2a45502448 251 this->m_connected = false;
ansond 0:ae2a45502448 252 return true;
ansond 0:ae2a45502448 253 }
ansond 0:ae2a45502448 254
ansond 0:ae2a45502448 255 // check transport and process stuff
ansond 0:ae2a45502448 256 void MQTTTransport::checkAndProcess() {
ansond 0:ae2a45502448 257 if (this->m_mqtt != NULL && this->m_connected == true) {
ansond 0:ae2a45502448 258 this->m_mqtt->loop();
ansond 0:ae2a45502448 259 this->logger()->blinkMQTTTransportRxLED();
ansond 0:ae2a45502448 260 }
ansond 0:ae2a45502448 261 }