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