MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_nxp

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

Committer:
ansond
Date:
Thu Feb 27 00:23:30 2014 +0000
Revision:
12:952dce085876
Parent:
11:59ee476fda24
Child:
13:25448d92c205
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 #include "MBEDEndpoint.h"
ansond 0:ae2a45502448 21
ansond 0:ae2a45502448 22 // MBED Light support
ansond 0:ae2a45502448 23 #include "MBEDLight.h"
ansond 0:ae2a45502448 24
ansond 2:90a84a216c58 25 // Emulated Resource Factory
ansond 2:90a84a216c58 26 #include "EmulatedResourceFactory.h"
ansond 2:90a84a216c58 27
ansond 0:ae2a45502448 28 // Emulated Actions we can act on
ansond 0:ae2a45502448 29 #include "EmulatedLightDimmerAction.h"
ansond 0:ae2a45502448 30 #include "EmulatedLightSwitchAction.h"
ansond 7:f570eb3f38cd 31
ansond 7:f570eb3f38cd 32 // shutdown endpoint reference
ansond 7:f570eb3f38cd 33 extern void closedown(int code);
ansond 0:ae2a45502448 34
ansond 0:ae2a45502448 35 // default constructor
ansond 0:ae2a45502448 36 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
ansond 10:748fc7052e61 37 this->m_ioc_id = -1;
ansond 0:ae2a45502448 38 bool success = true;
ansond 0:ae2a45502448 39 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 0:ae2a45502448 40 this->m_error_handler = error_handler;
ansond 0:ae2a45502448 41 this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:ae2a45502448 42 if (success) this->logger()->turnLEDBlue();
ansond 0:ae2a45502448 43 if (success) success = this->initializeEthernet(ethernet);
ansond 0:ae2a45502448 44 if (success) this->logger()->turnLEDYellow();
ansond 8:45f9a920e82c 45 if (success) success = this->initializeLights();
ansond 0:ae2a45502448 46 if (success) success = this->initializeTransports();
ansond 0:ae2a45502448 47 if (success) this->logger()->turnLEDGreen();
ansond 7:f570eb3f38cd 48 if (!success) closedown(2);
ansond 0:ae2a45502448 49 }
ansond 0:ae2a45502448 50
ansond 0:ae2a45502448 51 // default destructor
ansond 0:ae2a45502448 52 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:ae2a45502448 53 bool success = true;
ansond 0:ae2a45502448 54 if (success) this->logger()->turnLEDYellow();
ansond 0:ae2a45502448 55 if (success) success = this->closeLights();
ansond 0:ae2a45502448 56 if (success) success = this->closeTransports();
ansond 0:ae2a45502448 57 if (success) success = this->closeEthernet();
ansond 0:ae2a45502448 58 if (success) this->logger()->turnLEDBlue();
ansond 0:ae2a45502448 59 }
ansond 0:ae2a45502448 60
ansond 10:748fc7052e61 61 // set the IOC ID
ansond 10:748fc7052e61 62 void MBEDEndpoint::setIOCID(int ioc_id) { this->m_ioc_id = ioc_id; }
ansond 10:748fc7052e61 63
ansond 10:748fc7052e61 64 // get the IOC ID
ansond 10:748fc7052e61 65 int MBEDEndpoint::getIOCID() { return this->m_ioc_id; }
ansond 10:748fc7052e61 66
ansond 0:ae2a45502448 67 // initialize the Lights
ansond 0:ae2a45502448 68 bool MBEDEndpoint::initializeLights() {
ansond 0:ae2a45502448 69 this->logger()->log("Initializing Lights...");
ansond 0:ae2a45502448 70 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:ae2a45502448 71 this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this);
ansond 0:ae2a45502448 72 this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 73 this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 74 }
ansond 0:ae2a45502448 75 return true;
ansond 0:ae2a45502448 76 }
ansond 0:ae2a45502448 77
ansond 0:ae2a45502448 78 // does the input name match any of our light resources?
ansond 0:ae2a45502448 79 int MBEDEndpoint::indexOfLight(char *name) {
ansond 0:ae2a45502448 80 bool found = false;
ansond 0:ae2a45502448 81 int index = -1;
ansond 0:ae2a45502448 82
ansond 0:ae2a45502448 83 for(int i=0;i<NUM_LIGHTS && !found;++i) {
ansond 0:ae2a45502448 84 if (strcmp(this->m_lights[i]->getName(),name) == 0) {
ansond 0:ae2a45502448 85 found = true;
ansond 0:ae2a45502448 86 index = i;
ansond 0:ae2a45502448 87 }
ansond 0:ae2a45502448 88 }
ansond 0:ae2a45502448 89
ansond 0:ae2a45502448 90 return index;
ansond 0:ae2a45502448 91 }
ansond 0:ae2a45502448 92
ansond 0:ae2a45502448 93 // get a specific resources
ansond 0:ae2a45502448 94 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 0:ae2a45502448 95 if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
ansond 0:ae2a45502448 96 return NULL;
ansond 0:ae2a45502448 97 }
ansond 0:ae2a45502448 98
ansond 0:ae2a45502448 99 // initialize our ResourceFactory
ansond 2:90a84a216c58 100 ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger()); }
ansond 2:90a84a216c58 101
ansond 2:90a84a216c58 102 // get our endpoint name (first light name)
ansond 6:34c07e145caa 103 char *MBEDEndpoint::getEndpointName() { return this->m_lights[0]->getName(); }
ansond 0:ae2a45502448 104
ansond 0:ae2a45502448 105 // initialize a specific transport
ansond 0:ae2a45502448 106 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:ae2a45502448 107 bool success = false;
ansond 0:ae2a45502448 108 if (this->m_transports[index] == NULL) {
ansond 0:ae2a45502448 109 this->logger()->log("Initializing %s Transport...", key);
ansond 0:ae2a45502448 110 this->m_transports[index] = transport;
ansond 0:ae2a45502448 111 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:ae2a45502448 112 }
ansond 0:ae2a45502448 113 else {
ansond 0:ae2a45502448 114 this->logger()->log("%s already connected (OK)...", key);
ansond 0:ae2a45502448 115 success = true;
ansond 0:ae2a45502448 116 }
ansond 0:ae2a45502448 117 return success;
ansond 0:ae2a45502448 118 }
ansond 0:ae2a45502448 119
ansond 0:ae2a45502448 120 // initialize our transports
ansond 0:ae2a45502448 121 bool MBEDEndpoint::initializeTransports() {
ansond 0:ae2a45502448 122 bool success = true;
ansond 0:ae2a45502448 123
ansond 0:ae2a45502448 124 if (success == true) {
ansond 0:ae2a45502448 125 // MQTT Initialization
ansond 7:f570eb3f38cd 126 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this));
ansond 0:ae2a45502448 127 }
ansond 0:ae2a45502448 128
ansond 0:ae2a45502448 129 if (success == true) {
ansond 0:ae2a45502448 130 // HTTP Initialization
ansond 7:f570eb3f38cd 131 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this));
ansond 0:ae2a45502448 132 }
ansond 0:ae2a45502448 133 return success;
ansond 0:ae2a45502448 134 }
ansond 0:ae2a45502448 135
ansond 0:ae2a45502448 136 // initialize our Ethernet
ansond 0:ae2a45502448 137 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:ae2a45502448 138 bool success = false;
ansond 0:ae2a45502448 139 this->m_ethernet = ethernet;
ansond 0:ae2a45502448 140 if (this->m_ethernet != NULL) {
ansond 0:ae2a45502448 141 this->logger()->log("Initializing Ethernet...");
ansond 0:ae2a45502448 142
ansond 0:ae2a45502448 143 // connect up ethernet
ansond 0:ae2a45502448 144 this->m_ethernet->init();
ansond 0:ae2a45502448 145 this->m_ethernet->connect();
ansond 0:ae2a45502448 146
ansond 0:ae2a45502448 147 // display our IP address
ansond 0:ae2a45502448 148 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:ae2a45502448 149 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:ae2a45502448 150 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:ae2a45502448 151 success = true;
ansond 0:ae2a45502448 152 }
ansond 0:ae2a45502448 153 else {
ansond 0:ae2a45502448 154 this->logger()->log("Ethernet Not Connected...");
ansond 0:ae2a45502448 155 success = false;
ansond 0:ae2a45502448 156 }
ansond 0:ae2a45502448 157 }
ansond 0:ae2a45502448 158 else {
ansond 0:ae2a45502448 159 this->logger()->log("No Ethernet instance found");
ansond 0:ae2a45502448 160 success = false;
ansond 0:ae2a45502448 161 }
ansond 0:ae2a45502448 162 return success;
ansond 0:ae2a45502448 163 }
ansond 0:ae2a45502448 164
ansond 0:ae2a45502448 165 // load up our endpoint to the IOC
ansond 11:59ee476fda24 166 bool MBEDEndpoint::loadEndpoint() {
ansond 11:59ee476fda24 167 bool success = false;
ansond 11:59ee476fda24 168 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 169 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 170
ansond 11:59ee476fda24 171 // initialize
ansond 11:59ee476fda24 172 memset(result,0,IOC_RESULT_LEN+1);
ansond 12:952dce085876 173 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 12:952dce085876 174
ansond 11:59ee476fda24 175 // build the payload
ansond 11:59ee476fda24 176 char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN);
ansond 11:59ee476fda24 177
ansond 11:59ee476fda24 178 // issue the request
ansond 12:952dce085876 179 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 11:59ee476fda24 180
ansond 11:59ee476fda24 181 // update the IOC ID if found
ansond 11:59ee476fda24 182 if (success) this->saveIOCID(result);
ansond 11:59ee476fda24 183
ansond 11:59ee476fda24 184 // return our status
ansond 11:59ee476fda24 185 return success;
ansond 11:59ee476fda24 186 }
ansond 0:ae2a45502448 187
ansond 0:ae2a45502448 188 // update our endpoint with the IOC
ansond 11:59ee476fda24 189 bool MBEDEndpoint::updateEndpoint() {
ansond 11:59ee476fda24 190 bool success = false;
ansond 11:59ee476fda24 191 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 192 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 193
ansond 11:59ee476fda24 194 // initialize
ansond 11:59ee476fda24 195 memset(result,0,IOC_RESULT_LEN+1);
ansond 12:952dce085876 196 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 12:952dce085876 197
ansond 11:59ee476fda24 198 // build the payload
ansond 11:59ee476fda24 199 char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN);
ansond 11:59ee476fda24 200
ansond 11:59ee476fda24 201 // issue the request
ansond 12:952dce085876 202 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 11:59ee476fda24 203
ansond 11:59ee476fda24 204 // return our status
ansond 11:59ee476fda24 205 return success;
ansond 11:59ee476fda24 206 }
ansond 11:59ee476fda24 207
ansond 11:59ee476fda24 208 // build out the Payload
ansond 11:59ee476fda24 209 char *MBEDEndpoint::buildIOCPayload(char *data,int data_length) {
ansond 11:59ee476fda24 210 // construct the payload for Load/Updates
ansond 11:59ee476fda24 211
ansond 11:59ee476fda24 212 // return the payload
ansond 12:952dce085876 213 return data;
ansond 11:59ee476fda24 214 }
ansond 11:59ee476fda24 215
ansond 11:59ee476fda24 216 // save the IOC ID
ansond 11:59ee476fda24 217 void MBEDEndpoint::saveIOCID(char *data) {
ansond 11:59ee476fda24 218 int ioc_id = -1;
ansond 11:59ee476fda24 219
ansond 11:59ee476fda24 220 // parse the IOC ID from the data result
ansond 11:59ee476fda24 221
ansond 11:59ee476fda24 222 // save the IOC ID
ansond 11:59ee476fda24 223 if (ioc_id > 0) this->setIOCID(ioc_id);
ansond 11:59ee476fda24 224 }
ansond 0:ae2a45502448 225
ansond 0:ae2a45502448 226 // close down the Lights
ansond 0:ae2a45502448 227 bool MBEDEndpoint::closeLights() {
ansond 0:ae2a45502448 228 bool success = true;
ansond 0:ae2a45502448 229 this->logger()->log("Closing down Lights...");
ansond 0:ae2a45502448 230 return success;
ansond 0:ae2a45502448 231 }
ansond 0:ae2a45502448 232
ansond 0:ae2a45502448 233 // close a given transport
ansond 0:ae2a45502448 234 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:ae2a45502448 235 this->logger()->log("Closing down %s Transport...", key);
ansond 0:ae2a45502448 236 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:ae2a45502448 237 return true;
ansond 0:ae2a45502448 238 }
ansond 0:ae2a45502448 239
ansond 0:ae2a45502448 240 // close down our transports
ansond 0:ae2a45502448 241 bool MBEDEndpoint::closeTransports() {
ansond 0:ae2a45502448 242 bool success = true;
ansond 0:ae2a45502448 243
ansond 0:ae2a45502448 244 if (success) {
ansond 0:ae2a45502448 245 // close MQTT
ansond 0:ae2a45502448 246 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:ae2a45502448 247 }
ansond 0:ae2a45502448 248
ansond 0:ae2a45502448 249 if (success) {
ansond 0:ae2a45502448 250 // close HTTP
ansond 0:ae2a45502448 251 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:ae2a45502448 252 }
ansond 0:ae2a45502448 253
ansond 0:ae2a45502448 254 return success;
ansond 0:ae2a45502448 255 }
ansond 0:ae2a45502448 256
ansond 0:ae2a45502448 257 // close down our Ethernet
ansond 0:ae2a45502448 258 bool MBEDEndpoint::closeEthernet() {
ansond 0:ae2a45502448 259 this->logger()->log("Closing down Ethernet...");
ansond 0:ae2a45502448 260 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:ae2a45502448 261 return true;
ansond 0:ae2a45502448 262 }
ansond 0:ae2a45502448 263
ansond 0:ae2a45502448 264 // get our error handler
ansond 0:ae2a45502448 265 ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
ansond 0:ae2a45502448 266
ansond 0:ae2a45502448 267 // main running loop
ansond 0:ae2a45502448 268 void MBEDEndpoint::run() {
ansond 0:ae2a45502448 269 this->logger()->log("Endpoint Main Loop");
ansond 0:ae2a45502448 270 while(true) {
ansond 0:ae2a45502448 271 // sleep a bit
ansond 0:ae2a45502448 272 //this->logger()->log("Sleeping for a bit...");
ansond 0:ae2a45502448 273 wait_ms(MAIN_LOOP_SLEEP);
ansond 0:ae2a45502448 274
ansond 0:ae2a45502448 275 // check for events
ansond 0:ae2a45502448 276 //this->logger()->log("Processing Events...");
ansond 0:ae2a45502448 277 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:ae2a45502448 278
ansond 0:ae2a45502448 279 // check for exit
ansond 0:ae2a45502448 280 //this->logger()->log("Checking for exit...");
ansond 0:ae2a45502448 281 this->logger()->checkForExit();
ansond 0:ae2a45502448 282 }
ansond 0:ae2a45502448 283 }
ansond 0:ae2a45502448 284