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