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