mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Committer:
ansond
Date:
Mon Mar 31 18:47:19 2014 +0000
Revision:
14:717372430717
Parent:
13:66f6cf705184
Child:
15:363a3299e41a
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:a3fc1c6ef150 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:a3fc1c6ef150 2 *
ansond 0:a3fc1c6ef150 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:a3fc1c6ef150 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:a3fc1c6ef150 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:a3fc1c6ef150 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:a3fc1c6ef150 7 * furnished to do so, subject to the following conditions:
ansond 0:a3fc1c6ef150 8 *
ansond 0:a3fc1c6ef150 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:a3fc1c6ef150 10 * substantial portions of the Software.
ansond 0:a3fc1c6ef150 11 *
ansond 0:a3fc1c6ef150 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:a3fc1c6ef150 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:a3fc1c6ef150 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:a3fc1c6ef150 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:a3fc1c6ef150 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:a3fc1c6ef150 17 */
ansond 0:a3fc1c6ef150 18
ansond 0:a3fc1c6ef150 19 #include "MQTTTransport.h"
ansond 0:a3fc1c6ef150 20 #include "MBEDEndpoint.h"
ansond 0:a3fc1c6ef150 21
ansond 0:a3fc1c6ef150 22 // MBED Light support
ansond 0:a3fc1c6ef150 23 #include "MBEDLight.h"
ansond 0:a3fc1c6ef150 24
ansond 5:1ba6e68bf50e 25 // Light Personality: Emulated Light Resource Factory
ansond 5:1ba6e68bf50e 26 #include "EmulatedLightResourceFactory.h"
ansond 0:a3fc1c6ef150 27
ansond 5:1ba6e68bf50e 28 // Light Personality: Emulated Actions we can act on with the Light personalikty
ansond 0:a3fc1c6ef150 29 #include "EmulatedLightDimmerAction.h"
ansond 0:a3fc1c6ef150 30 #include "EmulatedLightSwitchAction.h"
ansond 0:a3fc1c6ef150 31
ansond 0:a3fc1c6ef150 32 // string support
ansond 0:a3fc1c6ef150 33 #include <stdlib.h>
ansond 0:a3fc1c6ef150 34 #include <string.h>
ansond 0:a3fc1c6ef150 35
ansond 0:a3fc1c6ef150 36 // shutdown endpoint reference
ansond 0:a3fc1c6ef150 37 extern void closedown(int code);
ansond 0:a3fc1c6ef150 38
ansond 0:a3fc1c6ef150 39 // default constructor
ansond 14:717372430717 40 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,void *ethernet) : BaseClass(error_handler,NULL) {
ansond 0:a3fc1c6ef150 41 bool success = true;
ansond 0:a3fc1c6ef150 42 this->m_instance_id = 0;
ansond 0:a3fc1c6ef150 43 this->m_preferences = NULL;
ansond 0:a3fc1c6ef150 44 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:a3fc1c6ef150 45 memset(this->m_gw_address,0,PREFERENCE_VALUE_LEN+1);
ansond 0:a3fc1c6ef150 46 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 11:9ae0fe4517c1 47 this->logger()->setEndpoint((void *)this);
ansond 13:66f6cf705184 48 this->setEndpoint(new IOCEndpoint(this->logger(),(void *)this));
ansond 0:a3fc1c6ef150 49 if (success) this->initPreferences();
ansond 0:a3fc1c6ef150 50 if (success) this->initEndpointName();
ansond 0:a3fc1c6ef150 51 if (success) this->logger()->turnLEDBlue();
ansond 0:a3fc1c6ef150 52 #ifdef MAC_ADDRESS
ansond 0:a3fc1c6ef150 53 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 0:a3fc1c6ef150 54 if (success)this->logger()->log("%s (MAC: %s)",ENDPOINT_VERSION_ANNOUNCE,fmt_mac);
ansond 0:a3fc1c6ef150 55 #else
ansond 0:a3fc1c6ef150 56 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:a3fc1c6ef150 57 #endif
ansond 0:a3fc1c6ef150 58 if (success) this->initGWAddress();
ansond 0:a3fc1c6ef150 59 if (success) this->logger()->log("IOC GW IP: %s",GW_IPADDRESS);
ansond 0:a3fc1c6ef150 60 if (PL_ENABLE && success) this->logger()->log("Philips Light ID: %d Philips Gateway IP: %s",PL_LIGHT_ID,PL_GW_ADDRESS);
ansond 14:717372430717 61 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 62 if (success) success = this->initializeEthernet(ethernet);
ansond 14:717372430717 63 #endif
ansond 0:a3fc1c6ef150 64 if (success) this->logger()->turnLEDYellow();
ansond 0:a3fc1c6ef150 65 if (success)this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 0:a3fc1c6ef150 66 if (success) success = this->initializeTransports();
ansond 7:8a4a61202b36 67 if (success) success = this->initializePersonalities();
ansond 0:a3fc1c6ef150 68 if (success) this->logger()->turnLEDOrange();
ansond 0:a3fc1c6ef150 69 this->logger()->lcdStatusOnly(true);
ansond 0:a3fc1c6ef150 70 if (!success) closedown(2);
ansond 0:a3fc1c6ef150 71 }
ansond 0:a3fc1c6ef150 72
ansond 0:a3fc1c6ef150 73 // default destructor
ansond 0:a3fc1c6ef150 74 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:a3fc1c6ef150 75 bool success = true;
ansond 0:a3fc1c6ef150 76 if (success) this->logger()->turnLEDYellow();
ansond 7:8a4a61202b36 77 if (success) success = this->closePersonalities();
ansond 0:a3fc1c6ef150 78 if (success) success = this->closeTransports();
ansond 14:717372430717 79 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 80 if (success) success = this->closeEthernet();
ansond 14:717372430717 81 #endif
ansond 0:a3fc1c6ef150 82 if (success) this->logger()->turnLEDBlue();
ansond 0:a3fc1c6ef150 83 if (this->m_map != NULL) delete this->m_map;
ansond 13:66f6cf705184 84 if (this->getEndpoint() != NULL) delete (IOCEndpoint *)this->getEndpoint();
ansond 0:a3fc1c6ef150 85 }
ansond 0:a3fc1c6ef150 86
ansond 0:a3fc1c6ef150 87 // get the IOC <--> MBED resource map
ansond 0:a3fc1c6ef150 88 MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
ansond 0:a3fc1c6ef150 89
ansond 0:a3fc1c6ef150 90 // initialize our preferences
ansond 0:a3fc1c6ef150 91 void MBEDEndpoint::initPreferences() { if (this->m_preferences == NULL) this->m_preferences = new Preferences(this->logger()); if (this->m_preferences != NULL) this->m_preferences->fixCoordsForIOC(); }
ansond 0:a3fc1c6ef150 92
ansond 0:a3fc1c6ef150 93 // get our preferences
ansond 0:a3fc1c6ef150 94 Preferences *MBEDEndpoint::preferences() { return this->m_preferences; }
ansond 0:a3fc1c6ef150 95
ansond 0:a3fc1c6ef150 96 // initialize the GW address from the configuration
ansond 0:a3fc1c6ef150 97 void MBEDEndpoint::initGWAddress() {
ansond 0:a3fc1c6ef150 98 memset(this->m_gw_address,0,PREFERENCE_VALUE_LEN+1);
ansond 0:a3fc1c6ef150 99 this->preferences()->getPreference("gw_address",this->m_gw_address,PREFERENCE_VALUE_LEN,GW_IPADDRESS);
ansond 0:a3fc1c6ef150 100 this->logger()->log("GW IP: %s",this->getGWAddress());
ansond 0:a3fc1c6ef150 101 }
ansond 0:a3fc1c6ef150 102
ansond 0:a3fc1c6ef150 103 // get our GW address
ansond 0:a3fc1c6ef150 104 char *MBEDEndpoint::getGWAddress() { return this->m_gw_address; }
ansond 0:a3fc1c6ef150 105
ansond 0:a3fc1c6ef150 106 // get our LCD status
ansond 0:a3fc1c6ef150 107 char *MBEDEndpoint::getLCDStatus() {
ansond 0:a3fc1c6ef150 108 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:a3fc1c6ef150 109
ansond 0:a3fc1c6ef150 110 // look at Light#0 to determine the IOC linkage ID...
ansond 7:8a4a61202b36 111 char *ioc = this->m_personalities[0]->getResourceFactory()->getResourceValue(EXTERNAL_LINKAGE_RESOURCE);
ansond 0:a3fc1c6ef150 112
ansond 0:a3fc1c6ef150 113 // color our LED depending on whether we have IOC linkage or not...
ansond 7:8a4a61202b36 114 if (ioc == NULL || strcmp(ioc,EXTERNAL_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
ansond 0:a3fc1c6ef150 115 else this->logger()->turnLEDGreen();
ansond 0:a3fc1c6ef150 116
ansond 0:a3fc1c6ef150 117 sprintf(this->m_lcd_status,"Node: %s\nGW IP: %s\nIOC Link: %s",this->getEndpointName(),this->getGWAddress(),ioc);
ansond 0:a3fc1c6ef150 118 return this->m_lcd_status;
ansond 0:a3fc1c6ef150 119 }
ansond 0:a3fc1c6ef150 120
ansond 5:1ba6e68bf50e 121 // initialize our personality
ansond 7:8a4a61202b36 122 bool MBEDEndpoint::initializePersonalities() {
ansond 5:1ba6e68bf50e 123 #ifdef LIGHT_PERSONALITY
ansond 5:1ba6e68bf50e 124 return this->initializeLights();
ansond 5:1ba6e68bf50e 125 #else
ansond 5:1ba6e68bf50e 126 return NULL;
ansond 5:1ba6e68bf50e 127 #endif
ansond 5:1ba6e68bf50e 128 }
ansond 5:1ba6e68bf50e 129
ansond 0:a3fc1c6ef150 130 // initialize the Lights
ansond 0:a3fc1c6ef150 131 bool MBEDEndpoint::initializeLights() {
ansond 7:8a4a61202b36 132 int index = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 0:a3fc1c6ef150 133 this->logger()->log("Initializing Lights...");
ansond 7:8a4a61202b36 134 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) {
ansond 7:8a4a61202b36 135 this->m_personalities[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
ansond 10:d96c57c19611 136 ((Light *)this->m_personalities[i])->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 10:d96c57c19611 137 ((Light *)this->m_personalities[i])->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 0:a3fc1c6ef150 138 }
ansond 0:a3fc1c6ef150 139 return true;
ansond 0:a3fc1c6ef150 140 }
ansond 0:a3fc1c6ef150 141
ansond 7:8a4a61202b36 142 // does the input name match any of our personality instances?
ansond 7:8a4a61202b36 143 int MBEDEndpoint::indexOfPersonality(char *name) {
ansond 0:a3fc1c6ef150 144 bool found = false;
ansond 0:a3fc1c6ef150 145 int index = -1;
ansond 0:a3fc1c6ef150 146
ansond 7:8a4a61202b36 147 for(int i=0;i<NUM_PERSONALITY_INSTANCES && !found;++i) {
ansond 7:8a4a61202b36 148 if (strcmp(this->m_personalities[i]->getName(),name) == 0) {
ansond 0:a3fc1c6ef150 149 found = true;
ansond 0:a3fc1c6ef150 150 index = i;
ansond 0:a3fc1c6ef150 151 }
ansond 0:a3fc1c6ef150 152 }
ansond 0:a3fc1c6ef150 153
ansond 0:a3fc1c6ef150 154 return index;
ansond 0:a3fc1c6ef150 155 }
ansond 0:a3fc1c6ef150 156
ansond 0:a3fc1c6ef150 157 // get a specific resources
ansond 0:a3fc1c6ef150 158 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 7:8a4a61202b36 159 if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->m_personalities[index]->getResourceFactory();
ansond 0:a3fc1c6ef150 160 return NULL;
ansond 0:a3fc1c6ef150 161 }
ansond 0:a3fc1c6ef150 162
ansond 5:1ba6e68bf50e 163 // initialize the ResourceFactory to fit our personality
ansond 5:1ba6e68bf50e 164 ResourceFactory *MBEDEndpoint::initResourceFactory() {
ansond 5:1ba6e68bf50e 165 #ifdef LIGHT_PERSONALITY
ansond 5:1ba6e68bf50e 166 return this->initLightResourceFactory();
ansond 5:1ba6e68bf50e 167 #else
ansond 5:1ba6e68bf50e 168 return NULL;
ansond 5:1ba6e68bf50e 169 #endif
ansond 5:1ba6e68bf50e 170 }
ansond 5:1ba6e68bf50e 171
ansond 5:1ba6e68bf50e 172 // initialize a Light Resource Factory
ansond 5:1ba6e68bf50e 173 ResourceFactory *MBEDEndpoint::initLightResourceFactory() { return new EmulatedLightResourceFactory(this->logger(),(void *)this); }
ansond 0:a3fc1c6ef150 174
ansond 0:a3fc1c6ef150 175 // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
ansond 0:a3fc1c6ef150 176 void MBEDEndpoint::initEndpointName() {
ansond 7:8a4a61202b36 177 this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 7:8a4a61202b36 178 memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1);
ansond 7:8a4a61202b36 179 sprintf(this->m_endpoint_name,PERSONALITY_NAME,this->m_instance_id);
ansond 0:a3fc1c6ef150 180 }
ansond 0:a3fc1c6ef150 181
ansond 0:a3fc1c6ef150 182 // get our endpoint name
ansond 0:a3fc1c6ef150 183 char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; }
ansond 0:a3fc1c6ef150 184
ansond 0:a3fc1c6ef150 185 // get our instance id
ansond 0:a3fc1c6ef150 186 int MBEDEndpoint::getInstanceID() { return this->m_instance_id; }
ansond 0:a3fc1c6ef150 187
ansond 0:a3fc1c6ef150 188 // initialize a specific transport
ansond 0:a3fc1c6ef150 189 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:a3fc1c6ef150 190 bool success = false;
ansond 0:a3fc1c6ef150 191 if (this->m_transports[index] == NULL) {
ansond 0:a3fc1c6ef150 192 this->logger()->log("Initializing %s Transport...", key);
ansond 0:a3fc1c6ef150 193 this->m_transports[index] = transport;
ansond 0:a3fc1c6ef150 194 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:a3fc1c6ef150 195 if (success) this->logger()->log("Transport %s initialized and connected",key);
ansond 0:a3fc1c6ef150 196 }
ansond 0:a3fc1c6ef150 197 else {
ansond 0:a3fc1c6ef150 198 this->logger()->log("%s already connected (OK)...", key);
ansond 0:a3fc1c6ef150 199 success = true;
ansond 0:a3fc1c6ef150 200 }
ansond 0:a3fc1c6ef150 201 return success;
ansond 0:a3fc1c6ef150 202 }
ansond 0:a3fc1c6ef150 203
ansond 0:a3fc1c6ef150 204 // initialize our transports
ansond 0:a3fc1c6ef150 205 bool MBEDEndpoint::initializeTransports() {
ansond 0:a3fc1c6ef150 206 bool success = true;
ansond 0:a3fc1c6ef150 207
ansond 0:a3fc1c6ef150 208 if (success == true) {
ansond 0:a3fc1c6ef150 209 // MQTT Initialization
ansond 13:66f6cf705184 210 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->logger(),this,this->getMap()));
ansond 0:a3fc1c6ef150 211 }
ansond 0:a3fc1c6ef150 212
ansond 0:a3fc1c6ef150 213 if (success == true) {
ansond 0:a3fc1c6ef150 214 // HTTP Initialization
ansond 13:66f6cf705184 215 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new IOCHTTPTransport(this->logger(),this));
ansond 0:a3fc1c6ef150 216 }
ansond 0:a3fc1c6ef150 217 return success;
ansond 0:a3fc1c6ef150 218 }
ansond 0:a3fc1c6ef150 219
ansond 14:717372430717 220 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 221 // initialize our Ethernet
ansond 0:a3fc1c6ef150 222 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:a3fc1c6ef150 223 bool success = false;
ansond 0:a3fc1c6ef150 224 this->m_ethernet = ethernet;
ansond 0:a3fc1c6ef150 225 if (this->m_ethernet != NULL) {
ansond 0:a3fc1c6ef150 226 this->logger()->log("Initializing Ethernet...");
ansond 0:a3fc1c6ef150 227
ansond 0:a3fc1c6ef150 228 // connect up ethernet
ansond 0:a3fc1c6ef150 229 this->m_ethernet->init();
ansond 0:a3fc1c6ef150 230 this->m_ethernet->connect();
ansond 0:a3fc1c6ef150 231
ansond 0:a3fc1c6ef150 232 // display our IP address
ansond 0:a3fc1c6ef150 233 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:a3fc1c6ef150 234 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:a3fc1c6ef150 235 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:a3fc1c6ef150 236 success = true;
ansond 0:a3fc1c6ef150 237 }
ansond 0:a3fc1c6ef150 238 else {
ansond 0:a3fc1c6ef150 239 this->logger()->log("Ethernet Not Connected...");
ansond 0:a3fc1c6ef150 240 success = false;
ansond 0:a3fc1c6ef150 241 }
ansond 0:a3fc1c6ef150 242 }
ansond 0:a3fc1c6ef150 243 else {
ansond 0:a3fc1c6ef150 244 this->logger()->log("No Ethernet instance found");
ansond 0:a3fc1c6ef150 245 success = false;
ansond 0:a3fc1c6ef150 246 }
ansond 0:a3fc1c6ef150 247 return success;
ansond 0:a3fc1c6ef150 248 }
ansond 14:717372430717 249 #endif
ansond 0:a3fc1c6ef150 250
ansond 7:8a4a61202b36 251 // load up all personality instances into the IOC
ansond 7:8a4a61202b36 252 bool MBEDEndpoint::loadPersonalities() {
ansond 0:a3fc1c6ef150 253 bool success = true;
ansond 7:8a4a61202b36 254 this->logger()->log("Loading All Personalities into the IOC...");
ansond 7:8a4a61202b36 255 for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->loadPersonality(this->m_personalities[i]);
ansond 0:a3fc1c6ef150 256 return success;
ansond 0:a3fc1c6ef150 257 }
ansond 0:a3fc1c6ef150 258
ansond 7:8a4a61202b36 259 // load up our personality to the IOC
ansond 7:8a4a61202b36 260 bool MBEDEndpoint::loadPersonality(Personality *instance) {
ansond 0:a3fc1c6ef150 261 bool success = false;
ansond 0:a3fc1c6ef150 262 char result[IOC_RESULT_LEN+1];
ansond 0:a3fc1c6ef150 263 char payload[IOC_PAYLOAD_LEN+1];
ansond 0:a3fc1c6ef150 264
ansond 0:a3fc1c6ef150 265 // initialize
ansond 0:a3fc1c6ef150 266 memset(result,0,IOC_RESULT_LEN+1);
ansond 0:a3fc1c6ef150 267 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 0:a3fc1c6ef150 268
ansond 0:a3fc1c6ef150 269 // DEBUG
ansond 7:8a4a61202b36 270 this->logger()->log("Building Payload for Personality(%s): %s...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 271
ansond 7:8a4a61202b36 272 // build the personality payload
ansond 7:8a4a61202b36 273 char *data = NULL;
ansond 7:8a4a61202b36 274 #ifdef LIGHT_PERSONALITY
ansond 11:9ae0fe4517c1 275 data = ((IOCEndpoint *)this->getEndpoint())->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
ansond 7:8a4a61202b36 276 #endif
ansond 0:a3fc1c6ef150 277
ansond 0:a3fc1c6ef150 278 // issue the request
ansond 0:a3fc1c6ef150 279 if (data != NULL && strlen(data) > 0) {
ansond 0:a3fc1c6ef150 280 // DEBUG
ansond 7:8a4a61202b36 281 this->logger()->log("Sending Personality(%s): %s to the IOC...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 282
ansond 0:a3fc1c6ef150 283 // load
ansond 0:a3fc1c6ef150 284 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 0:a3fc1c6ef150 285 }
ansond 0:a3fc1c6ef150 286
ansond 0:a3fc1c6ef150 287 // DEBUG
ansond 0:a3fc1c6ef150 288 //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
ansond 0:a3fc1c6ef150 289
ansond 7:8a4a61202b36 290 // update the External ID if found
ansond 11:9ae0fe4517c1 291 if (success) ((IOCEndpoint *)this->getEndpoint())->saveExternalID(instance,result);
ansond 0:a3fc1c6ef150 292
ansond 0:a3fc1c6ef150 293 // DEBUG
ansond 7:8a4a61202b36 294 if (success) this->logger()->log("Personality(%s): %s IOC ID=%d sent successfully",instance->getType(),instance->getName(),instance->getExternalID());
ansond 0:a3fc1c6ef150 295
ansond 0:a3fc1c6ef150 296 // DEBUG
ansond 0:a3fc1c6ef150 297 if (!success) {
ansond 7:8a4a61202b36 298 if (instance != NULL) this->logger()->log("Personality(%s): %s send FAILED",instance->getType(),instance->getName());
ansond 7:8a4a61202b36 299 else this->logger()->log("Personality send FAILED: NULL Personality instance");
ansond 0:a3fc1c6ef150 300 }
ansond 0:a3fc1c6ef150 301
ansond 0:a3fc1c6ef150 302 // return our status
ansond 0:a3fc1c6ef150 303 return success;
ansond 0:a3fc1c6ef150 304 }
ansond 0:a3fc1c6ef150 305
ansond 7:8a4a61202b36 306 // update all personality instances into the IOC
ansond 7:8a4a61202b36 307 bool MBEDEndpoint::updatePersonalities() {
ansond 0:a3fc1c6ef150 308 bool success = true;
ansond 7:8a4a61202b36 309 for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->updatePersonality(i);
ansond 0:a3fc1c6ef150 310 return success;
ansond 0:a3fc1c6ef150 311 }
ansond 0:a3fc1c6ef150 312
ansond 7:8a4a61202b36 313 // update all personality instances to the IOC
ansond 7:8a4a61202b36 314 bool MBEDEndpoint::updatePersonality(int index) {
ansond 7:8a4a61202b36 315 if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->updatePersonality(this->m_personalities[index]);
ansond 0:a3fc1c6ef150 316 return false;
ansond 0:a3fc1c6ef150 317 }
ansond 0:a3fc1c6ef150 318
ansond 7:8a4a61202b36 319 // update our ith personality instance with the IOC
ansond 7:8a4a61202b36 320 bool MBEDEndpoint::updatePersonality(Personality *instance) {
ansond 0:a3fc1c6ef150 321 bool success = false;
ansond 0:a3fc1c6ef150 322 char result[IOC_RESULT_LEN+1];
ansond 0:a3fc1c6ef150 323 char payload[IOC_PAYLOAD_LEN+1];
ansond 0:a3fc1c6ef150 324
ansond 0:a3fc1c6ef150 325 // initialize
ansond 0:a3fc1c6ef150 326 memset(result,0,IOC_RESULT_LEN+1);
ansond 0:a3fc1c6ef150 327 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 0:a3fc1c6ef150 328
ansond 0:a3fc1c6ef150 329 // build the payload
ansond 7:8a4a61202b36 330 char *data = NULL;
ansond 7:8a4a61202b36 331
ansond 7:8a4a61202b36 332 #ifdef LIGHT_PERSONALITY
ansond 11:9ae0fe4517c1 333 data = ((IOCEndpoint *)this->getEndpoint())->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
ansond 7:8a4a61202b36 334 #endif
ansond 7:8a4a61202b36 335
ansond 0:a3fc1c6ef150 336 // issue the request
ansond 0:a3fc1c6ef150 337 if (data != NULL && strlen(data) > 0) {
ansond 0:a3fc1c6ef150 338 // DEBUG
ansond 7:8a4a61202b36 339 this->logger()->log("Updating Personality(%s): %s at the IOC...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 340
ansond 0:a3fc1c6ef150 341 // update
ansond 7:8a4a61202b36 342 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(instance->getExternalID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 0:a3fc1c6ef150 343 }
ansond 0:a3fc1c6ef150 344
ansond 0:a3fc1c6ef150 345 // DEBUG
ansond 7:8a4a61202b36 346 if (success) this->logger()->log("Update of Personality instance to IOC successful");
ansond 7:8a4a61202b36 347 else this->logger()->log("Update of Personality instance to IOC FAILED");
ansond 0:a3fc1c6ef150 348
ansond 0:a3fc1c6ef150 349 // return our status
ansond 0:a3fc1c6ef150 350 return success;
ansond 0:a3fc1c6ef150 351 }
ansond 0:a3fc1c6ef150 352
ansond 7:8a4a61202b36 353 // close down our personalities
ansond 7:8a4a61202b36 354 bool MBEDEndpoint::closePersonalities() {
ansond 0:a3fc1c6ef150 355 bool success = true;
ansond 7:8a4a61202b36 356 this->logger()->log("Closing down Personalities...");
ansond 7:8a4a61202b36 357 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i)
ansond 7:8a4a61202b36 358 if (this->m_personalities[i] != NULL) delete this->m_personalities[i];
ansond 0:a3fc1c6ef150 359 return success;
ansond 0:a3fc1c6ef150 360 }
ansond 0:a3fc1c6ef150 361
ansond 0:a3fc1c6ef150 362 // close a given transport
ansond 0:a3fc1c6ef150 363 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:a3fc1c6ef150 364 this->logger()->log("Closing down %s Transport...", key);
ansond 0:a3fc1c6ef150 365 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:a3fc1c6ef150 366 return true;
ansond 0:a3fc1c6ef150 367 }
ansond 0:a3fc1c6ef150 368
ansond 0:a3fc1c6ef150 369 // close down our transports
ansond 0:a3fc1c6ef150 370 bool MBEDEndpoint::closeTransports() {
ansond 0:a3fc1c6ef150 371 bool success = true;
ansond 0:a3fc1c6ef150 372
ansond 0:a3fc1c6ef150 373 if (success) {
ansond 0:a3fc1c6ef150 374 // close MQTT
ansond 0:a3fc1c6ef150 375 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:a3fc1c6ef150 376 }
ansond 0:a3fc1c6ef150 377
ansond 0:a3fc1c6ef150 378 if (success) {
ansond 0:a3fc1c6ef150 379 // close HTTP
ansond 0:a3fc1c6ef150 380 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:a3fc1c6ef150 381 }
ansond 0:a3fc1c6ef150 382
ansond 0:a3fc1c6ef150 383 return success;
ansond 0:a3fc1c6ef150 384 }
ansond 0:a3fc1c6ef150 385
ansond 14:717372430717 386 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 387 // close down our Ethernet
ansond 0:a3fc1c6ef150 388 bool MBEDEndpoint::closeEthernet() {
ansond 0:a3fc1c6ef150 389 this->logger()->log("Closing down Ethernet...");
ansond 0:a3fc1c6ef150 390 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:a3fc1c6ef150 391 return true;
ansond 0:a3fc1c6ef150 392 }
ansond 14:717372430717 393 #endif
ansond 11:9ae0fe4517c1 394
ansond 0:a3fc1c6ef150 395 // main running loop
ansond 0:a3fc1c6ef150 396 void MBEDEndpoint::run() {
ansond 0:a3fc1c6ef150 397 this->logger()->log("Endpoint Main Loop");
ansond 0:a3fc1c6ef150 398 while(true) {
ansond 0:a3fc1c6ef150 399 // sleep a bit
ansond 0:a3fc1c6ef150 400 //this->logger()->log("Sleeping for a bit...");
ansond 0:a3fc1c6ef150 401 wait_ms(MAIN_LOOP_SLEEP);
ansond 0:a3fc1c6ef150 402
ansond 0:a3fc1c6ef150 403 // check for events
ansond 0:a3fc1c6ef150 404 //this->logger()->log("Processing Events...");
ansond 0:a3fc1c6ef150 405 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:a3fc1c6ef150 406
ansond 0:a3fc1c6ef150 407 // check for exit
ansond 0:a3fc1c6ef150 408 //this->logger()->log("Checking for exit...");
ansond 0:a3fc1c6ef150 409 this->logger()->checkForExit();
ansond 0:a3fc1c6ef150 410 }
ansond 0:a3fc1c6ef150 411 }
ansond 0:a3fc1c6ef150 412