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 22:07:45 2014 +0000
Revision:
21:d1ce325d1d32
Parent:
19:956b694f6542
Child:
22:f1002e5993c5
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 13:25448d92c205 32 // JSON serialization support
ansond 13:25448d92c205 33 #include "MbedJSONValue.h"
ansond 18:214698dbcdfa 34 #include <string>
ansond 13:25448d92c205 35
ansond 7:f570eb3f38cd 36 // shutdown endpoint reference
ansond 7:f570eb3f38cd 37 extern void closedown(int code);
ansond 0:ae2a45502448 38
ansond 0:ae2a45502448 39 // default constructor
ansond 0:ae2a45502448 40 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
ansond 10:748fc7052e61 41 this->m_ioc_id = -1;
ansond 0:ae2a45502448 42 bool success = true;
ansond 0:ae2a45502448 43 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 0:ae2a45502448 44 this->m_error_handler = error_handler;
ansond 15:e44d75d95b38 45 this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 0:ae2a45502448 46 this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:ae2a45502448 47 if (success) this->logger()->turnLEDBlue();
ansond 0:ae2a45502448 48 if (success) success = this->initializeEthernet(ethernet);
ansond 0:ae2a45502448 49 if (success) this->logger()->turnLEDYellow();
ansond 8:45f9a920e82c 50 if (success) success = this->initializeLights();
ansond 0:ae2a45502448 51 if (success) success = this->initializeTransports();
ansond 0:ae2a45502448 52 if (success) this->logger()->turnLEDGreen();
ansond 7:f570eb3f38cd 53 if (!success) closedown(2);
ansond 0:ae2a45502448 54 }
ansond 0:ae2a45502448 55
ansond 0:ae2a45502448 56 // default destructor
ansond 0:ae2a45502448 57 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:ae2a45502448 58 bool success = true;
ansond 0:ae2a45502448 59 if (success) this->logger()->turnLEDYellow();
ansond 0:ae2a45502448 60 if (success) success = this->closeLights();
ansond 0:ae2a45502448 61 if (success) success = this->closeTransports();
ansond 0:ae2a45502448 62 if (success) success = this->closeEthernet();
ansond 0:ae2a45502448 63 if (success) this->logger()->turnLEDBlue();
ansond 15:e44d75d95b38 64 if (this->m_map != NULL) delete this->m_map;
ansond 0:ae2a45502448 65 }
ansond 0:ae2a45502448 66
ansond 10:748fc7052e61 67 // set the IOC ID
ansond 10:748fc7052e61 68 void MBEDEndpoint::setIOCID(int ioc_id) { this->m_ioc_id = ioc_id; }
ansond 10:748fc7052e61 69
ansond 10:748fc7052e61 70 // get the IOC ID
ansond 10:748fc7052e61 71 int MBEDEndpoint::getIOCID() { return this->m_ioc_id; }
ansond 10:748fc7052e61 72
ansond 15:e44d75d95b38 73 // get the IOC <--> MBED resource map
ansond 15:e44d75d95b38 74 MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
ansond 15:e44d75d95b38 75
ansond 0:ae2a45502448 76 // initialize the Lights
ansond 0:ae2a45502448 77 bool MBEDEndpoint::initializeLights() {
ansond 0:ae2a45502448 78 this->logger()->log("Initializing Lights...");
ansond 0:ae2a45502448 79 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:ae2a45502448 80 this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this);
ansond 0:ae2a45502448 81 this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 82 this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 83 }
ansond 0:ae2a45502448 84 return true;
ansond 0:ae2a45502448 85 }
ansond 0:ae2a45502448 86
ansond 0:ae2a45502448 87 // does the input name match any of our light resources?
ansond 0:ae2a45502448 88 int MBEDEndpoint::indexOfLight(char *name) {
ansond 0:ae2a45502448 89 bool found = false;
ansond 0:ae2a45502448 90 int index = -1;
ansond 0:ae2a45502448 91
ansond 0:ae2a45502448 92 for(int i=0;i<NUM_LIGHTS && !found;++i) {
ansond 0:ae2a45502448 93 if (strcmp(this->m_lights[i]->getName(),name) == 0) {
ansond 0:ae2a45502448 94 found = true;
ansond 0:ae2a45502448 95 index = i;
ansond 0:ae2a45502448 96 }
ansond 0:ae2a45502448 97 }
ansond 0:ae2a45502448 98
ansond 0:ae2a45502448 99 return index;
ansond 0:ae2a45502448 100 }
ansond 0:ae2a45502448 101
ansond 0:ae2a45502448 102 // get a specific resources
ansond 0:ae2a45502448 103 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 0:ae2a45502448 104 if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
ansond 0:ae2a45502448 105 return NULL;
ansond 0:ae2a45502448 106 }
ansond 0:ae2a45502448 107
ansond 0:ae2a45502448 108 // initialize our ResourceFactory
ansond 2:90a84a216c58 109 ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger()); }
ansond 2:90a84a216c58 110
ansond 2:90a84a216c58 111 // get our endpoint name (first light name)
ansond 6:34c07e145caa 112 char *MBEDEndpoint::getEndpointName() { return this->m_lights[0]->getName(); }
ansond 0:ae2a45502448 113
ansond 0:ae2a45502448 114 // initialize a specific transport
ansond 0:ae2a45502448 115 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:ae2a45502448 116 bool success = false;
ansond 0:ae2a45502448 117 if (this->m_transports[index] == NULL) {
ansond 0:ae2a45502448 118 this->logger()->log("Initializing %s Transport...", key);
ansond 0:ae2a45502448 119 this->m_transports[index] = transport;
ansond 0:ae2a45502448 120 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:ae2a45502448 121 }
ansond 0:ae2a45502448 122 else {
ansond 0:ae2a45502448 123 this->logger()->log("%s already connected (OK)...", key);
ansond 0:ae2a45502448 124 success = true;
ansond 0:ae2a45502448 125 }
ansond 0:ae2a45502448 126 return success;
ansond 0:ae2a45502448 127 }
ansond 0:ae2a45502448 128
ansond 0:ae2a45502448 129 // initialize our transports
ansond 0:ae2a45502448 130 bool MBEDEndpoint::initializeTransports() {
ansond 0:ae2a45502448 131 bool success = true;
ansond 0:ae2a45502448 132
ansond 0:ae2a45502448 133 if (success == true) {
ansond 0:ae2a45502448 134 // MQTT Initialization
ansond 15:e44d75d95b38 135 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this,this->getMap()));
ansond 0:ae2a45502448 136 }
ansond 0:ae2a45502448 137
ansond 0:ae2a45502448 138 if (success == true) {
ansond 0:ae2a45502448 139 // HTTP Initialization
ansond 7:f570eb3f38cd 140 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this));
ansond 0:ae2a45502448 141 }
ansond 0:ae2a45502448 142 return success;
ansond 0:ae2a45502448 143 }
ansond 0:ae2a45502448 144
ansond 0:ae2a45502448 145 // initialize our Ethernet
ansond 0:ae2a45502448 146 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:ae2a45502448 147 bool success = false;
ansond 0:ae2a45502448 148 this->m_ethernet = ethernet;
ansond 0:ae2a45502448 149 if (this->m_ethernet != NULL) {
ansond 0:ae2a45502448 150 this->logger()->log("Initializing Ethernet...");
ansond 0:ae2a45502448 151
ansond 0:ae2a45502448 152 // connect up ethernet
ansond 0:ae2a45502448 153 this->m_ethernet->init();
ansond 0:ae2a45502448 154 this->m_ethernet->connect();
ansond 0:ae2a45502448 155
ansond 0:ae2a45502448 156 // display our IP address
ansond 0:ae2a45502448 157 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:ae2a45502448 158 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:ae2a45502448 159 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:ae2a45502448 160 success = true;
ansond 0:ae2a45502448 161 }
ansond 0:ae2a45502448 162 else {
ansond 0:ae2a45502448 163 this->logger()->log("Ethernet Not Connected...");
ansond 0:ae2a45502448 164 success = false;
ansond 0:ae2a45502448 165 }
ansond 0:ae2a45502448 166 }
ansond 0:ae2a45502448 167 else {
ansond 0:ae2a45502448 168 this->logger()->log("No Ethernet instance found");
ansond 0:ae2a45502448 169 success = false;
ansond 0:ae2a45502448 170 }
ansond 0:ae2a45502448 171 return success;
ansond 0:ae2a45502448 172 }
ansond 0:ae2a45502448 173
ansond 13:25448d92c205 174 // load up all endpoints into the IOC
ansond 13:25448d92c205 175 bool MBEDEndpoint::loadEndpoints() {
ansond 13:25448d92c205 176 bool success = true;
ansond 15:e44d75d95b38 177 this->logger()->log("Loading All Endpoints to IOC...");
ansond 13:25448d92c205 178 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]);
ansond 13:25448d92c205 179 return success;
ansond 13:25448d92c205 180 }
ansond 13:25448d92c205 181
ansond 0:ae2a45502448 182 // load up our endpoint to the IOC
ansond 15:e44d75d95b38 183 bool MBEDEndpoint::loadEndpoint(Light *light) {
ansond 11:59ee476fda24 184 bool success = false;
ansond 11:59ee476fda24 185 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 186 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 187
ansond 11:59ee476fda24 188 // initialize
ansond 17:84ab108ed670 189 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 190 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 15:e44d75d95b38 191
ansond 15:e44d75d95b38 192 // DEBUG
ansond 15:e44d75d95b38 193 this->logger()->log("Loading Light: %s...",light->getName());
ansond 12:952dce085876 194
ansond 11:59ee476fda24 195 // build the payload
ansond 14:0a6497a380a4 196 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 11:59ee476fda24 197
ansond 15:e44d75d95b38 198 // DEBUG
ansond 15:e44d75d95b38 199 this->logger()->log("Sending Light: %s to the IOC...",light->getName());
ansond 19:956b694f6542 200
ansond 11:59ee476fda24 201 // issue the request
ansond 12:952dce085876 202 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 11:59ee476fda24 203
ansond 15:e44d75d95b38 204 // DEBUG
ansond 19:956b694f6542 205 if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
ansond 15:e44d75d95b38 206
ansond 11:59ee476fda24 207 // update the IOC ID if found
ansond 11:59ee476fda24 208 if (success) this->saveIOCID(result);
ansond 11:59ee476fda24 209
ansond 15:e44d75d95b38 210 // DEBUG
ansond 19:956b694f6542 211 if (success) this->logger()->log("Light: %s IOC ID=%d...",light->getName(),this->getIOCID());
ansond 19:956b694f6542 212
ansond 19:956b694f6542 213 // DEBUG
ansond 19:956b694f6542 214 if (!success) this->logger()->log("Loading Light: %s FAILED",light->getName());
ansond 15:e44d75d95b38 215
ansond 11:59ee476fda24 216 // return our status
ansond 11:59ee476fda24 217 return success;
ansond 11:59ee476fda24 218 }
ansond 0:ae2a45502448 219
ansond 13:25448d92c205 220 // update all endpoints to the IOC
ansond 13:25448d92c205 221 bool MBEDEndpoint::updateEndpoints() {
ansond 13:25448d92c205 222 bool success = true;
ansond 13:25448d92c205 223 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i);
ansond 13:25448d92c205 224 return success;
ansond 13:25448d92c205 225 }
ansond 13:25448d92c205 226
ansond 13:25448d92c205 227 // update all endpoints to the IOC
ansond 13:25448d92c205 228 bool MBEDEndpoint::updateEndpoints(int index) {
ansond 13:25448d92c205 229 if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]);
ansond 13:25448d92c205 230 return false;
ansond 13:25448d92c205 231 }
ansond 13:25448d92c205 232
ansond 0:ae2a45502448 233 // update our endpoint with the IOC
ansond 13:25448d92c205 234 bool MBEDEndpoint::updateEndpoint(Light *light) {
ansond 11:59ee476fda24 235 bool success = false;
ansond 11:59ee476fda24 236 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 237 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 238
ansond 11:59ee476fda24 239 // initialize
ansond 17:84ab108ed670 240 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 241 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 12:952dce085876 242
ansond 11:59ee476fda24 243 // build the payload
ansond 14:0a6497a380a4 244 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 11:59ee476fda24 245
ansond 11:59ee476fda24 246 // issue the request
ansond 12:952dce085876 247 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 11:59ee476fda24 248
ansond 11:59ee476fda24 249 // return our status
ansond 11:59ee476fda24 250 return success;
ansond 11:59ee476fda24 251 }
ansond 11:59ee476fda24 252
ansond 11:59ee476fda24 253 // build out the Payload
ansond 13:25448d92c205 254 char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) {
ansond 11:59ee476fda24 255 // construct the payload for Load/Updates
ansond 13:25448d92c205 256 ResourceFactory *factory = light->getResourceFactory();
ansond 21:d1ce325d1d32 257
ansond 19:956b694f6542 258 // accumulator string
ansond 19:956b694f6542 259 std::string str_json("{");
ansond 13:25448d92c205 260
ansond 13:25448d92c205 261 // loop through the resources and build a JSON representation for the payload
ansond 13:25448d92c205 262 for(int i=0;i<factory->numResources();++i) {
ansond 13:25448d92c205 263 // get the ith resource
ansond 13:25448d92c205 264 Resource *resource = factory->getResource(i);
ansond 13:25448d92c205 265
ansond 13:25448d92c205 266 // add to the JSON payload
ansond 17:84ab108ed670 267 char *name = this->getMap()->endpointNameToIOCName(resource->getName());
ansond 18:214698dbcdfa 268 char *value = resource->getValue();
ansond 17:84ab108ed670 269
ansond 21:d1ce325d1d32 270 // Handle LOCATION a special way
ansond 19:956b694f6542 271 if (strcmp(name,"LOCATION") != 0) {
ansond 21:d1ce325d1d32 272 str_json += "\"" + std::string(name) + "\":\"" + std::string(value) + "\",";
ansond 21:d1ce325d1d32 273 }
ansond 21:d1ce325d1d32 274 else {
ansond 21:d1ce325d1d32 275 // IOC expects "Point(X,Y)" for LOCATION
ansond 21:d1ce325d1d32 276 str_json += "\"" + std::string(name) + "\":\"Point(" + std::string(value) + ")\",";
ansond 21:d1ce325d1d32 277 }
ansond 13:25448d92c205 278 }
ansond 13:25448d92c205 279
ansond 17:84ab108ed670 280 // Special Case: STARTDATETIME
ansond 21:d1ce325d1d32 281 str_json += "\"STARTDATETIME\":\"2014-02-27T22:03:06.973Z\",";
ansond 17:84ab108ed670 282
ansond 17:84ab108ed670 283 // Special Case: ENDDATETIME
ansond 21:d1ce325d1d32 284 str_json += "\"ENDDATETIME\":\"2015-02-27T03:35:55.940Z\",";
ansond 21:d1ce325d1d32 285
ansond 21:d1ce325d1d32 286 // Special Case: NAME
ansond 21:d1ce325d1d32 287 str_json += "\"NAME\":\"" + std::string(light->getName()) + "\",";
ansond 19:956b694f6542 288
ansond 21:d1ce325d1d32 289 // Special Case: TIMEZONEOFFSET
ansond 21:d1ce325d1d32 290 str_json += "\"TIMEZONEOFFSET\":\"+2:00\"";
ansond 17:84ab108ed670 291
ansond 21:d1ce325d1d32 292 // close
ansond 21:d1ce325d1d32 293 str_json += "}";
ansond 21:d1ce325d1d32 294
ansond 13:25448d92c205 295 // copy over to the buffer
ansond 18:214698dbcdfa 296 strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
ansond 15:e44d75d95b38 297
ansond 13:25448d92c205 298 // DEBUG
ansond 18:214698dbcdfa 299 this->logger()->log("Loading Payload: %s",data);
ansond 11:59ee476fda24 300
ansond 11:59ee476fda24 301 // return the payload
ansond 12:952dce085876 302 return data;
ansond 11:59ee476fda24 303 }
ansond 11:59ee476fda24 304
ansond 11:59ee476fda24 305 // save the IOC ID
ansond 13:25448d92c205 306 void MBEDEndpoint::saveIOCID(char *json) {
ansond 13:25448d92c205 307 // JSON factory
ansond 13:25448d92c205 308 MbedJSONValue parser;
ansond 11:59ee476fda24 309
ansond 19:956b694f6542 310 this->logger()->log("Load Result: %s",json);
ansond 19:956b694f6542 311
ansond 13:25448d92c205 312 // parse the result
ansond 13:25448d92c205 313 parse(parser,json);
ansond 13:25448d92c205 314
ansond 13:25448d92c205 315 // look for the ID element specifically
ansond 13:25448d92c205 316 int ioc_id = parser[IOC_REPONSE_ID_KEY].get<int>();
ansond 11:59ee476fda24 317
ansond 11:59ee476fda24 318 // save the IOC ID
ansond 11:59ee476fda24 319 if (ioc_id > 0) this->setIOCID(ioc_id);
ansond 11:59ee476fda24 320 }
ansond 0:ae2a45502448 321
ansond 0:ae2a45502448 322 // close down the Lights
ansond 0:ae2a45502448 323 bool MBEDEndpoint::closeLights() {
ansond 0:ae2a45502448 324 bool success = true;
ansond 0:ae2a45502448 325 this->logger()->log("Closing down Lights...");
ansond 15:e44d75d95b38 326 for(int i=0;i<NUM_LIGHTS;++i)
ansond 15:e44d75d95b38 327 if (this->m_lights[i] != NULL) delete this->m_lights[i];
ansond 0:ae2a45502448 328 return success;
ansond 0:ae2a45502448 329 }
ansond 0:ae2a45502448 330
ansond 0:ae2a45502448 331 // close a given transport
ansond 0:ae2a45502448 332 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:ae2a45502448 333 this->logger()->log("Closing down %s Transport...", key);
ansond 0:ae2a45502448 334 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:ae2a45502448 335 return true;
ansond 0:ae2a45502448 336 }
ansond 0:ae2a45502448 337
ansond 0:ae2a45502448 338 // close down our transports
ansond 0:ae2a45502448 339 bool MBEDEndpoint::closeTransports() {
ansond 0:ae2a45502448 340 bool success = true;
ansond 0:ae2a45502448 341
ansond 0:ae2a45502448 342 if (success) {
ansond 0:ae2a45502448 343 // close MQTT
ansond 0:ae2a45502448 344 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:ae2a45502448 345 }
ansond 0:ae2a45502448 346
ansond 0:ae2a45502448 347 if (success) {
ansond 0:ae2a45502448 348 // close HTTP
ansond 0:ae2a45502448 349 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:ae2a45502448 350 }
ansond 0:ae2a45502448 351
ansond 0:ae2a45502448 352 return success;
ansond 0:ae2a45502448 353 }
ansond 0:ae2a45502448 354
ansond 0:ae2a45502448 355 // close down our Ethernet
ansond 0:ae2a45502448 356 bool MBEDEndpoint::closeEthernet() {
ansond 0:ae2a45502448 357 this->logger()->log("Closing down Ethernet...");
ansond 0:ae2a45502448 358 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:ae2a45502448 359 return true;
ansond 0:ae2a45502448 360 }
ansond 0:ae2a45502448 361
ansond 13:25448d92c205 362 // min function
ansond 13:25448d92c205 363 int MBEDEndpoint::min(int value1,int value2) {
ansond 13:25448d92c205 364 if (value1 < value2) return value1;
ansond 13:25448d92c205 365 return value2;
ansond 13:25448d92c205 366 }
ansond 13:25448d92c205 367
ansond 0:ae2a45502448 368 // get our error handler
ansond 0:ae2a45502448 369 ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
ansond 0:ae2a45502448 370
ansond 0:ae2a45502448 371 // main running loop
ansond 0:ae2a45502448 372 void MBEDEndpoint::run() {
ansond 0:ae2a45502448 373 this->logger()->log("Endpoint Main Loop");
ansond 0:ae2a45502448 374 while(true) {
ansond 0:ae2a45502448 375 // sleep a bit
ansond 0:ae2a45502448 376 //this->logger()->log("Sleeping for a bit...");
ansond 0:ae2a45502448 377 wait_ms(MAIN_LOOP_SLEEP);
ansond 0:ae2a45502448 378
ansond 0:ae2a45502448 379 // check for events
ansond 0:ae2a45502448 380 //this->logger()->log("Processing Events...");
ansond 0:ae2a45502448 381 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:ae2a45502448 382
ansond 0:ae2a45502448 383 // check for exit
ansond 0:ae2a45502448 384 //this->logger()->log("Checking for exit...");
ansond 0:ae2a45502448 385 this->logger()->checkForExit();
ansond 0:ae2a45502448 386 }
ansond 0:ae2a45502448 387 }
ansond 0:ae2a45502448 388