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:
Sat Sep 13 02:56:16 2014 +0000
Revision:
53:74816856d11b
Parent:
52:8b95544920af
Child:
56:789a1a8c5ebe
enabled auto register

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 51:15c81f725ba2 32 // Salesforce Status Reporting (if enabled)
ansond 51:15c81f725ba2 33 #if SF_STATUS_REPORTING
ansond 51:15c81f725ba2 34 #include "StatusReporter.h"
ansond 51:15c81f725ba2 35 #endif
ansond 51:15c81f725ba2 36
ansond 0:a3fc1c6ef150 37 // string support
ansond 0:a3fc1c6ef150 38 #include <stdlib.h>
ansond 0:a3fc1c6ef150 39 #include <string.h>
ansond 0:a3fc1c6ef150 40
ansond 0:a3fc1c6ef150 41 // shutdown endpoint reference
ansond 0:a3fc1c6ef150 42 extern void closedown(int code);
ansond 0:a3fc1c6ef150 43
ansond 0:a3fc1c6ef150 44 // default constructor
ansond 51:15c81f725ba2 45 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,void *transport,void *status_reporter,void *extra) : BaseClass(error_handler,NULL) {
ansond 0:a3fc1c6ef150 46 bool success = true;
ansond 0:a3fc1c6ef150 47 this->m_instance_id = 0;
ansond 0:a3fc1c6ef150 48 this->m_preferences = NULL;
ansond 51:15c81f725ba2 49 this->m_status_reporter = status_reporter;
ansond 0:a3fc1c6ef150 50 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:a3fc1c6ef150 51 memset(this->m_gw_address,0,PREFERENCE_VALUE_LEN+1);
ansond 0:a3fc1c6ef150 52 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 11:9ae0fe4517c1 53 this->logger()->setEndpoint((void *)this);
ansond 27:bb4253d9f1d0 54 this->setEndpoint(new IOCEndpoint(error_handler,(void *)this));
ansond 0:a3fc1c6ef150 55 if (success) this->initPreferences();
ansond 0:a3fc1c6ef150 56 if (success) this->initEndpointName();
ansond 0:a3fc1c6ef150 57 if (success) this->logger()->turnLEDBlue();
ansond 30:00ff5fe192f0 58 #ifdef MAC_ADDRESS
ansond 0:a3fc1c6ef150 59 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 0:a3fc1c6ef150 60 if (success)this->logger()->log("%s (MAC: %s)",ENDPOINT_VERSION_ANNOUNCE,fmt_mac);
ansond 0:a3fc1c6ef150 61 #else
ansond 0:a3fc1c6ef150 62 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:a3fc1c6ef150 63 #endif
ansond 0:a3fc1c6ef150 64 if (success) this->initGWAddress();
ansond 47:b775f5453b5d 65 //if (success) this->logger()->log("IOC GW IP: %s",this->m_gw_address);
ansond 0:a3fc1c6ef150 66 if (PL_ENABLE && success) this->logger()->log("Philips Light ID: %d Philips Gateway IP: %s",PL_LIGHT_ID,PL_GW_ADDRESS);
ansond 18:8ab7aa1dbb41 67 #ifdef CELLULAR_NETWORK
ansond 18:8ab7aa1dbb41 68 this->m_cellular_modem = NULL;
ansond 18:8ab7aa1dbb41 69 this->m_gps = NULL;
ansond 29:ac6390032cec 70 this->logger()->log("initializing Cellular Modem...");
ansond 44:2740794b059a 71 if (success) success = this->initializeCellularModem(transport);
ansond 39:fec38d630383 72 //this->logger()->log("initializing GPS Receiver...");
ansond 39:fec38d630383 73 //if (success) success = this->initializeGPSReceiver(this,extra);
ansond 18:8ab7aa1dbb41 74 #else
ansond 23:34086bc15b02 75 if (success) success = this->initializeEthernet((EthernetInterface *)transport);
ansond 14:717372430717 76 #endif
ansond 0:a3fc1c6ef150 77 if (success) this->logger()->turnLEDYellow();
ansond 29:ac6390032cec 78 this->logger()->log("initializing resource map...");
ansond 0:a3fc1c6ef150 79 if (success)this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 29:ac6390032cec 80 this->logger()->log("initializing transports...");
ansond 0:a3fc1c6ef150 81 if (success) success = this->initializeTransports();
ansond 29:ac6390032cec 82 this->logger()->log("initializing personalities...");
ansond 27:bb4253d9f1d0 83 if (success) success = this->initializePersonalities();
ansond 29:ac6390032cec 84 this->logger()->log("initialization complete for endpoint...");
ansond 0:a3fc1c6ef150 85 if (success) this->logger()->turnLEDOrange();
ansond 0:a3fc1c6ef150 86 this->logger()->lcdStatusOnly(true);
ansond 0:a3fc1c6ef150 87 if (!success) closedown(2);
ansond 53:74816856d11b 88 if (success && AUTO_REGISTER_WITH_IOC) {
ansond 53:74816856d11b 89 // load up our endpoints
ansond 53:74816856d11b 90 this->loadPersonalities();
ansond 53:74816856d11b 91 this->updatePersonalities();
ansond 53:74816856d11b 92 }
ansond 0:a3fc1c6ef150 93 }
ansond 0:a3fc1c6ef150 94
ansond 0:a3fc1c6ef150 95 // default destructor
ansond 0:a3fc1c6ef150 96 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:a3fc1c6ef150 97 bool success = true;
ansond 0:a3fc1c6ef150 98 if (success) this->logger()->turnLEDYellow();
ansond 7:8a4a61202b36 99 if (success) success = this->closePersonalities();
ansond 0:a3fc1c6ef150 100 if (success) success = this->closeTransports();
ansond 18:8ab7aa1dbb41 101 #ifdef CELLULAR_NETWORK
ansond 18:8ab7aa1dbb41 102 if (success) success = this->closeCellularModem();
ansond 18:8ab7aa1dbb41 103 if (success) success = this->closeGPSReceiver();
ansond 19:194cdc5e5a1e 104 if (this->m_cellular_modem != NULL) delete this->m_cellular_modem;
ansond 18:8ab7aa1dbb41 105 if (this->m_gps != NULL) delete this->m_gps;
ansond 24:e706feda4970 106 if (this->m_preferences != NULL) delete this->m_preferences;
ansond 18:8ab7aa1dbb41 107 #else
ansond 0:a3fc1c6ef150 108 if (success) success = this->closeEthernet();
ansond 14:717372430717 109 #endif
ansond 0:a3fc1c6ef150 110 if (success) this->logger()->turnLEDBlue();
ansond 0:a3fc1c6ef150 111 if (this->m_map != NULL) delete this->m_map;
ansond 13:66f6cf705184 112 if (this->getEndpoint() != NULL) delete (IOCEndpoint *)this->getEndpoint();
ansond 0:a3fc1c6ef150 113 }
ansond 0:a3fc1c6ef150 114
ansond 0:a3fc1c6ef150 115 // get the IOC <--> MBED resource map
ansond 0:a3fc1c6ef150 116 MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
ansond 0:a3fc1c6ef150 117
ansond 0:a3fc1c6ef150 118 // initialize our preferences
ansond 27:bb4253d9f1d0 119 void MBEDEndpoint::initPreferences() {
ansond 27:bb4253d9f1d0 120 if (this->m_preferences == NULL)
ansond 27:bb4253d9f1d0 121 this->m_preferences = new Preferences(this->logger());
ansond 27:bb4253d9f1d0 122 if (this->m_preferences != NULL)
ansond 27:bb4253d9f1d0 123 this->m_preferences->fixCoordsForIOC();
ansond 27:bb4253d9f1d0 124 }
ansond 0:a3fc1c6ef150 125
ansond 0:a3fc1c6ef150 126 // get our preferences
ansond 0:a3fc1c6ef150 127 Preferences *MBEDEndpoint::preferences() { return this->m_preferences; }
ansond 0:a3fc1c6ef150 128
ansond 0:a3fc1c6ef150 129 // initialize the GW address from the configuration
ansond 0:a3fc1c6ef150 130 void MBEDEndpoint::initGWAddress() {
ansond 0:a3fc1c6ef150 131 memset(this->m_gw_address,0,PREFERENCE_VALUE_LEN+1);
ansond 29:ac6390032cec 132 strcpy(this->m_gw_address,GW_IPADDRESS);
ansond 46:32a7545dde45 133 if (this->m_preferences != NULL) {
ansond 29:ac6390032cec 134 this->m_preferences->getPreference("gw_address",this->m_gw_address,PREFERENCE_VALUE_LEN,GW_IPADDRESS);
ansond 46:32a7545dde45 135 this->m_preferences->getPreference("gw_port",this->m_gw_port,PREFERENCE_VALUE_LEN,GW_PORT);
ansond 46:32a7545dde45 136 this->logger()->log("GW: %s:%s",this->m_gw_address,this->m_gw_port);
ansond 46:32a7545dde45 137 }
ansond 0:a3fc1c6ef150 138 }
ansond 0:a3fc1c6ef150 139
ansond 0:a3fc1c6ef150 140 // get our GW address
ansond 0:a3fc1c6ef150 141 char *MBEDEndpoint::getGWAddress() { return this->m_gw_address; }
ansond 0:a3fc1c6ef150 142
ansond 46:32a7545dde45 143 // get our GW Port
ansond 46:32a7545dde45 144 char *MBEDEndpoint::getGWPort() { return this->m_gw_port; }
ansond 46:32a7545dde45 145
ansond 0:a3fc1c6ef150 146 // get our LCD status
ansond 0:a3fc1c6ef150 147 char *MBEDEndpoint::getLCDStatus() {
ansond 0:a3fc1c6ef150 148 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:a3fc1c6ef150 149
ansond 0:a3fc1c6ef150 150 // look at Light#0 to determine the IOC linkage ID...
ansond 7:8a4a61202b36 151 char *ioc = this->m_personalities[0]->getResourceFactory()->getResourceValue(EXTERNAL_LINKAGE_RESOURCE);
ansond 0:a3fc1c6ef150 152
ansond 0:a3fc1c6ef150 153 // color our LED depending on whether we have IOC linkage or not...
ansond 7:8a4a61202b36 154 if (ioc == NULL || strcmp(ioc,EXTERNAL_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
ansond 0:a3fc1c6ef150 155 else this->logger()->turnLEDGreen();
ansond 0:a3fc1c6ef150 156
ansond 0:a3fc1c6ef150 157 sprintf(this->m_lcd_status,"Node: %s\nGW IP: %s\nIOC Link: %s",this->getEndpointName(),this->getGWAddress(),ioc);
ansond 0:a3fc1c6ef150 158 return this->m_lcd_status;
ansond 0:a3fc1c6ef150 159 }
ansond 0:a3fc1c6ef150 160
ansond 5:1ba6e68bf50e 161 // initialize our personality
ansond 7:8a4a61202b36 162 bool MBEDEndpoint::initializePersonalities() {
ansond 5:1ba6e68bf50e 163 #ifdef LIGHT_PERSONALITY
ansond 5:1ba6e68bf50e 164 return this->initializeLights();
ansond 17:36a4ab911aaa 165 #endif
ansond 17:36a4ab911aaa 166 #ifdef COPCAR_PERSONALITY
ansond 17:36a4ab911aaa 167 return this->initializeLights();
ansond 5:1ba6e68bf50e 168 #endif
ansond 5:1ba6e68bf50e 169 }
ansond 5:1ba6e68bf50e 170
ansond 0:a3fc1c6ef150 171 // initialize the Lights
ansond 0:a3fc1c6ef150 172 bool MBEDEndpoint::initializeLights() {
ansond 29:ac6390032cec 173 int index = PERSONALITY_NAME_INDEX;
ansond 29:ac6390032cec 174 if (this->m_preferences != NULL) index = this->m_preferences->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 0:a3fc1c6ef150 175 this->logger()->log("Initializing Lights...");
ansond 7:8a4a61202b36 176 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) {
ansond 7:8a4a61202b36 177 this->m_personalities[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
ansond 10:d96c57c19611 178 ((Light *)this->m_personalities[i])->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 10:d96c57c19611 179 ((Light *)this->m_personalities[i])->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 0:a3fc1c6ef150 180 }
ansond 0:a3fc1c6ef150 181 return true;
ansond 0:a3fc1c6ef150 182 }
ansond 0:a3fc1c6ef150 183
ansond 7:8a4a61202b36 184 // does the input name match any of our personality instances?
ansond 7:8a4a61202b36 185 int MBEDEndpoint::indexOfPersonality(char *name) {
ansond 0:a3fc1c6ef150 186 bool found = false;
ansond 0:a3fc1c6ef150 187 int index = -1;
ansond 0:a3fc1c6ef150 188
ansond 7:8a4a61202b36 189 for(int i=0;i<NUM_PERSONALITY_INSTANCES && !found;++i) {
ansond 7:8a4a61202b36 190 if (strcmp(this->m_personalities[i]->getName(),name) == 0) {
ansond 0:a3fc1c6ef150 191 found = true;
ansond 0:a3fc1c6ef150 192 index = i;
ansond 0:a3fc1c6ef150 193 }
ansond 0:a3fc1c6ef150 194 }
ansond 0:a3fc1c6ef150 195
ansond 0:a3fc1c6ef150 196 return index;
ansond 0:a3fc1c6ef150 197 }
ansond 0:a3fc1c6ef150 198
ansond 0:a3fc1c6ef150 199 // get a specific resources
ansond 0:a3fc1c6ef150 200 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 7:8a4a61202b36 201 if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->m_personalities[index]->getResourceFactory();
ansond 0:a3fc1c6ef150 202 return NULL;
ansond 0:a3fc1c6ef150 203 }
ansond 0:a3fc1c6ef150 204
ansond 5:1ba6e68bf50e 205 // initialize the ResourceFactory to fit our personality
ansond 5:1ba6e68bf50e 206 ResourceFactory *MBEDEndpoint::initResourceFactory() {
ansond 17:36a4ab911aaa 207 #ifdef LIGHT_PERSONALITY
ansond 17:36a4ab911aaa 208 return this->initLightResourceFactory();
ansond 17:36a4ab911aaa 209 #endif
ansond 17:36a4ab911aaa 210 #ifdef COPCAR_PERSONALITY
ansond 17:36a4ab911aaa 211 return this->initLightResourceFactory();
ansond 17:36a4ab911aaa 212 #endif
ansond 5:1ba6e68bf50e 213 }
ansond 5:1ba6e68bf50e 214
ansond 5:1ba6e68bf50e 215 // initialize a Light Resource Factory
ansond 5:1ba6e68bf50e 216 ResourceFactory *MBEDEndpoint::initLightResourceFactory() { return new EmulatedLightResourceFactory(this->logger(),(void *)this); }
ansond 0:a3fc1c6ef150 217
ansond 0:a3fc1c6ef150 218 // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
ansond 0:a3fc1c6ef150 219 void MBEDEndpoint::initEndpointName() {
ansond 29:ac6390032cec 220 this->m_instance_id = PERSONALITY_NAME_INDEX;
ansond 7:8a4a61202b36 221 memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1);
ansond 29:ac6390032cec 222 if (this->m_preferences != NULL)
ansond 29:ac6390032cec 223 this->m_instance_id = this->m_preferences->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 7:8a4a61202b36 224 sprintf(this->m_endpoint_name,PERSONALITY_NAME,this->m_instance_id);
ansond 0:a3fc1c6ef150 225 }
ansond 0:a3fc1c6ef150 226
ansond 0:a3fc1c6ef150 227 // get our endpoint name
ansond 0:a3fc1c6ef150 228 char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; }
ansond 0:a3fc1c6ef150 229
ansond 0:a3fc1c6ef150 230 // get our instance id
ansond 0:a3fc1c6ef150 231 int MBEDEndpoint::getInstanceID() { return this->m_instance_id; }
ansond 0:a3fc1c6ef150 232
ansond 0:a3fc1c6ef150 233 // initialize a specific transport
ansond 0:a3fc1c6ef150 234 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:a3fc1c6ef150 235 bool success = false;
ansond 0:a3fc1c6ef150 236 if (this->m_transports[index] == NULL) {
ansond 0:a3fc1c6ef150 237 this->logger()->log("Initializing %s Transport...", key);
ansond 0:a3fc1c6ef150 238 this->m_transports[index] = transport;
ansond 0:a3fc1c6ef150 239 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:a3fc1c6ef150 240 if (success) this->logger()->log("Transport %s initialized and connected",key);
ansond 0:a3fc1c6ef150 241 }
ansond 0:a3fc1c6ef150 242 else {
ansond 0:a3fc1c6ef150 243 this->logger()->log("%s already connected (OK)...", key);
ansond 0:a3fc1c6ef150 244 success = true;
ansond 0:a3fc1c6ef150 245 }
ansond 0:a3fc1c6ef150 246 return success;
ansond 0:a3fc1c6ef150 247 }
ansond 0:a3fc1c6ef150 248
ansond 0:a3fc1c6ef150 249 // initialize our transports
ansond 0:a3fc1c6ef150 250 bool MBEDEndpoint::initializeTransports() {
ansond 0:a3fc1c6ef150 251 bool success = true;
ansond 0:a3fc1c6ef150 252
ansond 0:a3fc1c6ef150 253 if (success == true) {
ansond 0:a3fc1c6ef150 254 // MQTT Initialization
ansond 13:66f6cf705184 255 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->logger(),this,this->getMap()));
ansond 0:a3fc1c6ef150 256 }
ansond 0:a3fc1c6ef150 257
ansond 0:a3fc1c6ef150 258 if (success == true) {
ansond 0:a3fc1c6ef150 259 // HTTP Initialization
ansond 13:66f6cf705184 260 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new IOCHTTPTransport(this->logger(),this));
ansond 0:a3fc1c6ef150 261 }
ansond 0:a3fc1c6ef150 262 return success;
ansond 0:a3fc1c6ef150 263 }
ansond 0:a3fc1c6ef150 264
ansond 18:8ab7aa1dbb41 265 #ifdef CELLULAR_NETWORK
ansond 44:2740794b059a 266 bool MBEDEndpoint::initializeCellularModem(void *modem) {
ansond 18:8ab7aa1dbb41 267 bool success = false;
ansond 18:8ab7aa1dbb41 268
ansond 18:8ab7aa1dbb41 269 // initialize
ansond 39:fec38d630383 270 if (this->m_cellular_modem == NULL) this->m_cellular_modem = new MBEDUbloxCellRadio(this->logger(),(void *)this,modem);
ansond 18:8ab7aa1dbb41 271 if (this->m_cellular_modem != NULL) success = this->m_cellular_modem->connect();
ansond 18:8ab7aa1dbb41 272
ansond 18:8ab7aa1dbb41 273 // return our status
ansond 18:8ab7aa1dbb41 274 return success;
ansond 18:8ab7aa1dbb41 275 }
ansond 18:8ab7aa1dbb41 276
ansond 39:fec38d630383 277 bool MBEDEndpoint::initializeGPSReceiver(void *gps) {
ansond 18:8ab7aa1dbb41 278 bool success = false;
ansond 18:8ab7aa1dbb41 279
ansond 18:8ab7aa1dbb41 280 // initialize
ansond 39:fec38d630383 281 if (this->m_gps == NULL) this->m_gps = new MBEDUbloxGPS(this->logger(),(void *)this,gps);
ansond 18:8ab7aa1dbb41 282 if (this->m_gps != NULL) success = this->m_gps->connect();
ansond 18:8ab7aa1dbb41 283
ansond 18:8ab7aa1dbb41 284 // return our status
ansond 18:8ab7aa1dbb41 285 return success;
ansond 18:8ab7aa1dbb41 286 }
ansond 18:8ab7aa1dbb41 287 #endif
ansond 18:8ab7aa1dbb41 288
ansond 14:717372430717 289 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 290 // initialize our Ethernet
ansond 0:a3fc1c6ef150 291 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:a3fc1c6ef150 292 bool success = false;
ansond 0:a3fc1c6ef150 293 this->m_ethernet = ethernet;
ansond 0:a3fc1c6ef150 294 if (this->m_ethernet != NULL) {
ansond 0:a3fc1c6ef150 295 this->logger()->log("Initializing Ethernet...");
ansond 0:a3fc1c6ef150 296
ansond 0:a3fc1c6ef150 297 // connect up ethernet
ansond 0:a3fc1c6ef150 298 this->m_ethernet->init();
ansond 41:3f494366fc76 299 //this->m_ethernet->init("192.168.1.220","255.255.255.0","192.168.1.1");
ansond 0:a3fc1c6ef150 300 this->m_ethernet->connect();
ansond 0:a3fc1c6ef150 301
ansond 0:a3fc1c6ef150 302 // display our IP address
ansond 0:a3fc1c6ef150 303 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:a3fc1c6ef150 304 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:a3fc1c6ef150 305 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:a3fc1c6ef150 306 success = true;
ansond 0:a3fc1c6ef150 307 }
ansond 0:a3fc1c6ef150 308 else {
ansond 0:a3fc1c6ef150 309 this->logger()->log("Ethernet Not Connected...");
ansond 0:a3fc1c6ef150 310 success = false;
ansond 0:a3fc1c6ef150 311 }
ansond 0:a3fc1c6ef150 312 }
ansond 0:a3fc1c6ef150 313 else {
ansond 0:a3fc1c6ef150 314 this->logger()->log("No Ethernet instance found");
ansond 0:a3fc1c6ef150 315 success = false;
ansond 0:a3fc1c6ef150 316 }
ansond 0:a3fc1c6ef150 317 return success;
ansond 0:a3fc1c6ef150 318 }
ansond 14:717372430717 319 #endif
ansond 0:a3fc1c6ef150 320
ansond 7:8a4a61202b36 321 // load up all personality instances into the IOC
ansond 7:8a4a61202b36 322 bool MBEDEndpoint::loadPersonalities() {
ansond 0:a3fc1c6ef150 323 bool success = true;
ansond 7:8a4a61202b36 324 this->logger()->log("Loading All Personalities into the IOC...");
ansond 7:8a4a61202b36 325 for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->loadPersonality(this->m_personalities[i]);
ansond 0:a3fc1c6ef150 326 return success;
ansond 0:a3fc1c6ef150 327 }
ansond 0:a3fc1c6ef150 328
ansond 7:8a4a61202b36 329 // load up our personality to the IOC
ansond 7:8a4a61202b36 330 bool MBEDEndpoint::loadPersonality(Personality *instance) {
ansond 0:a3fc1c6ef150 331 bool success = false;
ansond 0:a3fc1c6ef150 332 char result[IOC_RESULT_LEN+1];
ansond 0:a3fc1c6ef150 333 char payload[IOC_PAYLOAD_LEN+1];
ansond 0:a3fc1c6ef150 334
ansond 0:a3fc1c6ef150 335 // initialize
ansond 0:a3fc1c6ef150 336 memset(result,0,IOC_RESULT_LEN+1);
ansond 0:a3fc1c6ef150 337 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 0:a3fc1c6ef150 338
ansond 0:a3fc1c6ef150 339 // DEBUG
ansond 7:8a4a61202b36 340 this->logger()->log("Building Payload for Personality(%s): %s...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 341
ansond 7:8a4a61202b36 342 // build the personality payload
ansond 38:bf10a01eb123 343 char *data = ((IOCEndpoint *)this->getEndpoint())->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
ansond 0:a3fc1c6ef150 344
ansond 0:a3fc1c6ef150 345 // issue the request
ansond 0:a3fc1c6ef150 346 if (data != NULL && strlen(data) > 0) {
ansond 0:a3fc1c6ef150 347 // DEBUG
ansond 7:8a4a61202b36 348 this->logger()->log("Sending Personality(%s): %s to the IOC...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 349
ansond 0:a3fc1c6ef150 350 // load
ansond 0:a3fc1c6ef150 351 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 0:a3fc1c6ef150 352 }
ansond 0:a3fc1c6ef150 353
ansond 0:a3fc1c6ef150 354 // DEBUG
ansond 0:a3fc1c6ef150 355 //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
ansond 0:a3fc1c6ef150 356
ansond 7:8a4a61202b36 357 // update the External ID if found
ansond 11:9ae0fe4517c1 358 if (success) ((IOCEndpoint *)this->getEndpoint())->saveExternalID(instance,result);
ansond 0:a3fc1c6ef150 359
ansond 0:a3fc1c6ef150 360 // DEBUG
ansond 7:8a4a61202b36 361 if (success) this->logger()->log("Personality(%s): %s IOC ID=%d sent successfully",instance->getType(),instance->getName(),instance->getExternalID());
ansond 0:a3fc1c6ef150 362
ansond 0:a3fc1c6ef150 363 // DEBUG
ansond 0:a3fc1c6ef150 364 if (!success) {
ansond 7:8a4a61202b36 365 if (instance != NULL) this->logger()->log("Personality(%s): %s send FAILED",instance->getType(),instance->getName());
ansond 7:8a4a61202b36 366 else this->logger()->log("Personality send FAILED: NULL Personality instance");
ansond 0:a3fc1c6ef150 367 }
ansond 0:a3fc1c6ef150 368
ansond 0:a3fc1c6ef150 369 // return our status
ansond 0:a3fc1c6ef150 370 return success;
ansond 0:a3fc1c6ef150 371 }
ansond 0:a3fc1c6ef150 372
ansond 7:8a4a61202b36 373 // update all personality instances into the IOC
ansond 7:8a4a61202b36 374 bool MBEDEndpoint::updatePersonalities() {
ansond 0:a3fc1c6ef150 375 bool success = true;
ansond 7:8a4a61202b36 376 for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->updatePersonality(i);
ansond 0:a3fc1c6ef150 377 return success;
ansond 0:a3fc1c6ef150 378 }
ansond 0:a3fc1c6ef150 379
ansond 7:8a4a61202b36 380 // update all personality instances to the IOC
ansond 7:8a4a61202b36 381 bool MBEDEndpoint::updatePersonality(int index) {
ansond 7:8a4a61202b36 382 if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->updatePersonality(this->m_personalities[index]);
ansond 0:a3fc1c6ef150 383 return false;
ansond 0:a3fc1c6ef150 384 }
ansond 0:a3fc1c6ef150 385
ansond 7:8a4a61202b36 386 // update our ith personality instance with the IOC
ansond 7:8a4a61202b36 387 bool MBEDEndpoint::updatePersonality(Personality *instance) {
ansond 0:a3fc1c6ef150 388 bool success = false;
ansond 0:a3fc1c6ef150 389 char result[IOC_RESULT_LEN+1];
ansond 0:a3fc1c6ef150 390 char payload[IOC_PAYLOAD_LEN+1];
ansond 0:a3fc1c6ef150 391
ansond 0:a3fc1c6ef150 392 // initialize
ansond 0:a3fc1c6ef150 393 memset(result,0,IOC_RESULT_LEN+1);
ansond 0:a3fc1c6ef150 394 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 0:a3fc1c6ef150 395
ansond 0:a3fc1c6ef150 396 // build the payload
ansond 38:bf10a01eb123 397 char *data = ((IOCEndpoint *)this->getEndpoint())->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
ansond 7:8a4a61202b36 398
ansond 0:a3fc1c6ef150 399 // issue the request
ansond 0:a3fc1c6ef150 400 if (data != NULL && strlen(data) > 0) {
ansond 0:a3fc1c6ef150 401 // DEBUG
ansond 7:8a4a61202b36 402 this->logger()->log("Updating Personality(%s): %s at the IOC...",instance->getType(),instance->getName());
ansond 0:a3fc1c6ef150 403
ansond 0:a3fc1c6ef150 404 // update
ansond 7:8a4a61202b36 405 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(instance->getExternalID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 0:a3fc1c6ef150 406 }
ansond 0:a3fc1c6ef150 407
ansond 0:a3fc1c6ef150 408 // DEBUG
ansond 7:8a4a61202b36 409 if (success) this->logger()->log("Update of Personality instance to IOC successful");
ansond 7:8a4a61202b36 410 else this->logger()->log("Update of Personality instance to IOC FAILED");
ansond 0:a3fc1c6ef150 411
ansond 0:a3fc1c6ef150 412 // return our status
ansond 0:a3fc1c6ef150 413 return success;
ansond 0:a3fc1c6ef150 414 }
ansond 0:a3fc1c6ef150 415
ansond 7:8a4a61202b36 416 // close down our personalities
ansond 7:8a4a61202b36 417 bool MBEDEndpoint::closePersonalities() {
ansond 0:a3fc1c6ef150 418 bool success = true;
ansond 7:8a4a61202b36 419 this->logger()->log("Closing down Personalities...");
ansond 7:8a4a61202b36 420 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i)
ansond 7:8a4a61202b36 421 if (this->m_personalities[i] != NULL) delete this->m_personalities[i];
ansond 0:a3fc1c6ef150 422 return success;
ansond 0:a3fc1c6ef150 423 }
ansond 0:a3fc1c6ef150 424
ansond 0:a3fc1c6ef150 425 // close a given transport
ansond 0:a3fc1c6ef150 426 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:a3fc1c6ef150 427 this->logger()->log("Closing down %s Transport...", key);
ansond 0:a3fc1c6ef150 428 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:a3fc1c6ef150 429 return true;
ansond 0:a3fc1c6ef150 430 }
ansond 0:a3fc1c6ef150 431
ansond 0:a3fc1c6ef150 432 // close down our transports
ansond 0:a3fc1c6ef150 433 bool MBEDEndpoint::closeTransports() {
ansond 0:a3fc1c6ef150 434 bool success = true;
ansond 0:a3fc1c6ef150 435
ansond 0:a3fc1c6ef150 436 if (success) {
ansond 0:a3fc1c6ef150 437 // close MQTT
ansond 0:a3fc1c6ef150 438 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:a3fc1c6ef150 439 }
ansond 0:a3fc1c6ef150 440
ansond 0:a3fc1c6ef150 441 if (success) {
ansond 0:a3fc1c6ef150 442 // close HTTP
ansond 0:a3fc1c6ef150 443 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:a3fc1c6ef150 444 }
ansond 0:a3fc1c6ef150 445
ansond 0:a3fc1c6ef150 446 return success;
ansond 0:a3fc1c6ef150 447 }
ansond 0:a3fc1c6ef150 448
ansond 18:8ab7aa1dbb41 449 #ifdef CELLULAR_NETWORK
ansond 18:8ab7aa1dbb41 450 bool MBEDEndpoint::closeCellularModem() {
ansond 18:8ab7aa1dbb41 451 bool success = true;
ansond 18:8ab7aa1dbb41 452 if (this->m_cellular_modem != NULL) success = this->m_cellular_modem->disconnect();
ansond 18:8ab7aa1dbb41 453 return success;
ansond 18:8ab7aa1dbb41 454 }
ansond 18:8ab7aa1dbb41 455
ansond 18:8ab7aa1dbb41 456 bool MBEDEndpoint::closeGPSReceiver() {
ansond 18:8ab7aa1dbb41 457 bool success = true;
ansond 18:8ab7aa1dbb41 458 if (this->m_gps != NULL) success = this->m_gps->disconnect();
ansond 18:8ab7aa1dbb41 459 return success;
ansond 18:8ab7aa1dbb41 460 }
ansond 18:8ab7aa1dbb41 461 #endif
ansond 18:8ab7aa1dbb41 462
ansond 14:717372430717 463 #ifndef CELLULAR_NETWORK
ansond 0:a3fc1c6ef150 464 // close down our Ethernet
ansond 0:a3fc1c6ef150 465 bool MBEDEndpoint::closeEthernet() {
ansond 0:a3fc1c6ef150 466 this->logger()->log("Closing down Ethernet...");
ansond 0:a3fc1c6ef150 467 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:a3fc1c6ef150 468 return true;
ansond 0:a3fc1c6ef150 469 }
ansond 14:717372430717 470 #endif
ansond 11:9ae0fe4517c1 471
ansond 0:a3fc1c6ef150 472 // main running loop
ansond 0:a3fc1c6ef150 473 void MBEDEndpoint::run() {
ansond 0:a3fc1c6ef150 474 this->logger()->log("Endpoint Main Loop");
ansond 0:a3fc1c6ef150 475 while(true) {
ansond 0:a3fc1c6ef150 476 // sleep a bit
ansond 0:a3fc1c6ef150 477 //this->logger()->log("Sleeping for a bit...");
ansond 52:8b95544920af 478 Thread::wait(MAIN_LOOP_SLEEP);
ansond 0:a3fc1c6ef150 479
ansond 0:a3fc1c6ef150 480 // check for events
ansond 0:a3fc1c6ef150 481 //this->logger()->log("Processing Events...");
ansond 0:a3fc1c6ef150 482 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:a3fc1c6ef150 483
ansond 0:a3fc1c6ef150 484 // check for exit
ansond 0:a3fc1c6ef150 485 //this->logger()->log("Checking for exit...");
ansond 0:a3fc1c6ef150 486 this->logger()->checkForExit();
ansond 51:15c81f725ba2 487
ansond 51:15c81f725ba2 488 // do any extra event loop activities
ansond 51:15c81f725ba2 489 this->extraEventLoopWork();
ansond 0:a3fc1c6ef150 490 }
ansond 51:15c81f725ba2 491 }
ansond 51:15c81f725ba2 492
ansond 51:15c81f725ba2 493 // do any extra work from within the event loop
ansond 51:15c81f725ba2 494 void MBEDEndpoint::extraEventLoopWork() {
ansond 51:15c81f725ba2 495 // check and send status reporting if enabled
ansond 51:15c81f725ba2 496 #if SF_STATUS_REPORTING
ansond 51:15c81f725ba2 497 if (this->m_status_reporter != NULL) {
ansond 51:15c81f725ba2 498 StatusReporter *status_reporter = (StatusReporter *)this->m_status_reporter;
ansond 51:15c81f725ba2 499 status_reporter->checkAndReportOnStatus();
ansond 51:15c81f725ba2 500 }
ansond 51:15c81f725ba2 501 #endif
ansond 51:15c81f725ba2 502 }
ansond 0:a3fc1c6ef150 503