Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027 C12832 EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed
Diff: MQTTTransport.cpp
- Revision:
- 0:ae2a45502448
- Child:
- 2:90a84a216c58
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTTransport.cpp Tue Feb 25 16:56:31 2014 +0000
@@ -0,0 +1,201 @@
+/* Copyright C2013 Doug Anson, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files the "Software", to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+ #include "MQTTTransport.h"
+
+ #include "MBEDEndpoint.h"
+
+ // String splitting support
+ #include "splitstring.h"
+
+ // our transmitt instance
+ MQTTTransport *instance = NULL;
+
+ // MQTT callback to handle received messages
+ void _mqtt_message_handler(char *topic,char *payload,unsigned int length) {
+ if (instance != NULL) instance->processMessage((char *)payload,length);
+ }
+
+ // our MQTT client endpoint
+ PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler);
+
+ // default constructor
+ MQTTTransport::MQTTTransport(ErrorHandler *error_handler) : Transport(error_handler) {
+ this->m_mqtt = NULL;
+ instance = this;
+ this->m_map = new MBEDToIOCResourceMap();
+ }
+
+ // default destructor
+ MQTTTransport::~MQTTTransport() {
+ this->disconnect();
+ if (this->m_mqtt != NULL) delete this->m_mqtt;
+ }
+
+ // process a MQTT Message
+ void MQTTTransport::processMessage(char *payload, unsigned int len) {
+ string message_type;
+ string message_name;
+ string message_verb;
+ string message_value;
+ string message_opt;
+ MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
+
+ // DEBUG
+ this->logger()->log("MQTT Message: %s length=%d",payload,len);
+
+ // split the string by the delimiter
+ splitstring str_payload(payload);
+ vector<string> data = str_payload.split(':');
+
+ // format of the MQTT message: message_type:name:verb|Parameter_X:value|keyword:optional_data
+ if (data.size() > 0) message_type = data[0];
+ if (data.size() > 1) message_name = data[1];
+ if (data.size() > 2) message_verb = data[2];
+ if (data.size() > 3) message_value = data[3];
+ if (data.size() > 4) message_opt = data[4];
+
+ // load endpoints
+ if (strcmp(message_type.c_str(),"endpoint") == 0) {
+ if (strcmp(message_name.c_str(),"all") == 0) {
+ if (strcmp(message_verb.c_str(),"load") == 0) {
+ // load up our endpoint
+ endpoint->loadEndpoint();
+ }
+ if (strcmp(message_verb.c_str(),"update") == 0) {
+ // update our endpoint
+ endpoint->updateEndpoint();
+ }
+ }
+ else {
+ // destined for our lights?
+ int index = endpoint->indexOfLight((char *)message_name.c_str());
+ if (index >= 0) {
+ if (strcmp(message_verb.c_str(),"update") == 0) {
+ // update our endpoint
+ endpoint->updateEndpoint();
+ }
+ }
+ }
+ }
+
+ // change a resource value
+ if (strcmp(message_type.c_str(),"change") == 0) {
+ // destined for our lights?
+ int index = endpoint->indexOfLight((char *)message_name.c_str());
+ if (index >= 0) {
+ // map the parameter to one of ours
+ char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb.c_str());
+ if (mapped_resource != NULL) {
+ ResourceFactory *factory = endpoint->getResources(index);
+ factory->setResourceValue((char *)message_name.c_str(),(char *)message_value.c_str());
+ }
+ }
+ }
+
+ // get a resource value
+ if (strcmp(message_type.c_str(),"get") == 0) {
+ // destined for our lights?
+ int index = endpoint->indexOfLight((char *)message_name.c_str());
+ if (index >= 0) {
+ // map the parameter to one of ours
+ char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb.c_str());
+ if (mapped_resource != NULL) {
+ ResourceFactory *factory = endpoint->getResources(index);
+ char *resource_value = factory->getResourceValue((char *)message_name.c_str());
+
+ // end the resource value back over MQTT
+ this->sendResourceValue((char *)message_name.c_str(),(char *)message_verb.c_str(),resource_value);
+ }
+ }
+ }
+ }
+
+ void MQTTTransport::sendResourceValue(char *endpoint_name,char *parameter_name,char *value) {
+ }
+
+ char *MQTTTransport::mapEndpointResourceToIOCResource(char *ioc_name) {
+ return this->m_map->getMBEDMappedName(ioc_name);
+ }
+
+ char *MQTTTransport::makeID(char *id_template,char *buffer) {
+ srand(time(0));
+ srand(rand());
+ sprintf(buffer,id_template,rand()%MQTT_MAXID_VALUE);
+ return buffer;
+ }
+
+ // connect up MQTT
+ bool MQTTTransport::connect() {
+ char mqtt_id[MQTT_ENDPOINT_IDLEN+1];
+ memset(mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1));
+ if (this->m_connected == false) {
+ this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT);
+ this->m_mqtt = &_mqtt;
+ if (this->m_mqtt != NULL) {
+ char *id = this->makeID(MQTT_ENDPOINT_ID,mqtt_id);
+ this->logger()->log("MQTT Connect: ID: %s...",id);
+ if (this->m_mqtt->connect(id)) {
+ this->logger()->log("MQTT Subscribe: Topic: %s...",MQTT_IOC_TOPIC);
+ if (this->m_mqtt->subscribe(MQTT_IOC_TOPIC)) {
+ this->logger()->log("MQTT CONNECTED.");
+ this->m_connected = true;
+ }
+ else {
+ this->logger()->log("MQTT Subscribe: Topic: %s FAILED",MQTT_IOC_TOPIC);
+ this->logger()->turnLEDRed();
+ }
+ }
+ else {
+ this->logger()->log("MQTT Connect: ID: %s FAILED",id);
+ this->logger()->turnLEDRed();
+ }
+ }
+ else {
+ this->logger()->log("MQTT Unable to allocate new instance");
+ this->logger()->turnLEDRed();
+ }
+ }
+ else {
+ this->logger()->log("MQTT already connected (OK)");
+ }
+ return this->m_connected;
+ }
+
+ // disconnect from MQTT
+ bool MQTTTransport::disconnect() {
+ if (this->m_mqtt != NULL) {
+ this->logger()->log("MQTT Unsubscribing from: %s...",MQTT_IOC_TOPIC);
+ this->m_mqtt->unsubscribe(MQTT_IOC_TOPIC);
+ this->logger()->log("MQTT Disconnecting...");
+ this->m_mqtt->disconnect();
+ }
+ else {
+ this->logger()->log("MQTT already disconnected (OK)");
+ }
+ this->m_connected = false;
+ return true;
+ }
+
+ // check transport and process stuff
+ void MQTTTransport::checkAndProcess() {
+ if (this->m_mqtt != NULL && this->m_connected == true) {
+ this->m_mqtt->loop();
+ this->logger()->blinkMQTTTransportRxLED();
+ }
+ }
\ No newline at end of file