nsp specific components for the NSP version of the impact endpoint

Dependents:   mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet mbed_nsp_endpoint_nxp

Committer:
ansond
Date:
Fri Sep 26 05:55:44 2014 +0000
Revision:
28:b6a7959c8be0
Parent:
24:b62fec3ff73c
updates for new logger

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:e1c4378df3fe 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:e1c4378df3fe 2 *
ansond 0:e1c4378df3fe 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:e1c4378df3fe 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:e1c4378df3fe 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:e1c4378df3fe 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:e1c4378df3fe 7 * furnished to do so, subject to the following conditions:
ansond 0:e1c4378df3fe 8 *
ansond 0:e1c4378df3fe 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:e1c4378df3fe 10 * substantial portions of the Software.
ansond 0:e1c4378df3fe 11 *
ansond 0:e1c4378df3fe 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:e1c4378df3fe 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:e1c4378df3fe 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:e1c4378df3fe 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:e1c4378df3fe 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:e1c4378df3fe 17 */
ansond 0:e1c4378df3fe 18
ansond 0:e1c4378df3fe 19 #include "NSPTransport.h"
ansond 0:e1c4378df3fe 20 #include "MBEDEndpoint.h"
ansond 0:e1c4378df3fe 21
ansond 4:7f7fe167d9c0 22 // Light Personality Support: MBED Light support
ansond 0:e1c4378df3fe 23 #include "MBEDLight.h"
ansond 0:e1c4378df3fe 24
ansond 4:7f7fe167d9c0 25 // Light Personality Support: NSP Resource Factory
ansond 4:7f7fe167d9c0 26 #include "NSPLightResourceFactory.h"
ansond 0:e1c4378df3fe 27
ansond 4:7f7fe167d9c0 28 // Light Personality Support: NSP Actions we can act on
ansond 0:e1c4378df3fe 29 #include "NSPLightDimmerAction.h"
ansond 0:e1c4378df3fe 30 #include "NSPLightSwitchAction.h"
ansond 0:e1c4378df3fe 31
ansond 23:6f6e97a276b9 32 // Salesforce Status Reporting (if enabled)
ansond 23:6f6e97a276b9 33 #if SF_STATUS_REPORTING
ansond 23:6f6e97a276b9 34 #include "StatusReporter.h"
ansond 23:6f6e97a276b9 35 #endif
ansond 23:6f6e97a276b9 36
ansond 0:e1c4378df3fe 37 // shutdown endpoint reference
ansond 0:e1c4378df3fe 38 extern void closedown(int code);
ansond 0:e1c4378df3fe 39
ansond 0:e1c4378df3fe 40 // default constructor
ansond 28:b6a7959c8be0 41 MBEDEndpoint::MBEDEndpoint(Logger *logger,void *transport,void *status_reporter,void *extra) : BaseClass(logger,NULL) {
ansond 0:e1c4378df3fe 42 bool success = true;
ansond 0:e1c4378df3fe 43 this->m_preferences = NULL;
ansond 0:e1c4378df3fe 44 this->m_instance_id = 0;
ansond 23:6f6e97a276b9 45 this->m_status_reporter = status_reporter;
ansond 0:e1c4378df3fe 46 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:e1c4378df3fe 47 memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1);
ansond 22:0f2a0269ce6d 48 memset(this->m_nsp_port_str_def,0,PREFERENCE_VALUE_LEN+1);
ansond 22:0f2a0269ce6d 49 sprintf(this->m_nsp_port_str_def,"%d",NSP_PORT);
ansond 0:e1c4378df3fe 50 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 5:94d000e6fa70 51 this->logger()->setEndpoint((void *)this);
ansond 0:e1c4378df3fe 52 if (success) this->initPreferences();
ansond 0:e1c4378df3fe 53 if (success) this->initEndpointName();
ansond 0:e1c4378df3fe 54 if (success) this->logger()->turnLEDBlue();
ansond 0:e1c4378df3fe 55 #ifdef MAC_ADDRESS
ansond 0:e1c4378df3fe 56 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 0:e1c4378df3fe 57 if (success)this->logger()->log("%s (MAC: %s)",ENDPOINT_VERSION_ANNOUNCE,fmt_mac);
ansond 0:e1c4378df3fe 58 #else
ansond 0:e1c4378df3fe 59 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:e1c4378df3fe 60 #endif
ansond 0:e1c4378df3fe 61 if (success) this->initNSPAddress();
ansond 0:e1c4378df3fe 62 if (PL_ENABLE && success) this->logger()->log("Philips Light ID: %d Philips Gateway IP: %s",PL_LIGHT_ID,PL_GW_ADDRESS);
ansond 11:c4d02616e10f 63 #ifdef CELLULAR_NETWORK
ansond 11:c4d02616e10f 64 this->m_cellular_modem = NULL;
ansond 11:c4d02616e10f 65 this->m_gps = NULL;
ansond 19:3cecccc1a3d4 66 this->logger()->log("initializing Cellular Modem...");
ansond 21:272df0444756 67 if (success) success = this->initializeCellularModem(transport);
ansond 19:3cecccc1a3d4 68 //this->logger()->log("initializing GPS Receiver...");
ansond 19:3cecccc1a3d4 69 //if (success) success = this->initializeGPSReceiver(this,extra);
ansond 11:c4d02616e10f 70 #else
ansond 21:272df0444756 71 if (success) success = this->initializeEthernet((EthernetInterface *)transport);
ansond 6:edf306673e54 72 #endif
ansond 0:e1c4378df3fe 73 if (success) this->logger()->turnLEDYellow();
ansond 16:6936310c9501 74 this->logger()->log("initializing transports...");
ansond 0:e1c4378df3fe 75 if (success) success = this->initializeTransports();
ansond 16:6936310c9501 76 this->logger()->log("initializing personalities...");
ansond 16:6936310c9501 77 if (success) success = this->initializePersonalities();
ansond 16:6936310c9501 78 this->logger()->log("initialization complete for endpoint...");
ansond 0:e1c4378df3fe 79 if (success) this->logger()->turnLEDOrange();
ansond 0:e1c4378df3fe 80 this->logger()->lcdStatusOnly(true);
ansond 0:e1c4378df3fe 81 if (!success) closedown(2);
ansond 24:b62fec3ff73c 82 if (success && AUTO_REGISTER_WITH_IOC) this->autoRegisterWithIOC();
ansond 0:e1c4378df3fe 83 }
ansond 0:e1c4378df3fe 84
ansond 0:e1c4378df3fe 85 // default destructor
ansond 0:e1c4378df3fe 86 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:e1c4378df3fe 87 bool success = true;
ansond 0:e1c4378df3fe 88 if (success) this->logger()->turnLEDYellow();
ansond 4:7f7fe167d9c0 89 if (success) success = this->closePersonalities();
ansond 0:e1c4378df3fe 90 if (success) success = this->closeTransports();
ansond 11:c4d02616e10f 91 #ifdef CELLULAR_NETWORK
ansond 11:c4d02616e10f 92 if (success) success = this->closeCellularModem();
ansond 11:c4d02616e10f 93 if (success) success = this->closeGPSReceiver();
ansond 11:c4d02616e10f 94 if (this->m_cellular_modem != NULL) delete this->m_cellular_modem;
ansond 11:c4d02616e10f 95 if (this->m_gps != NULL) delete this->m_gps;
ansond 11:c4d02616e10f 96 #else
ansond 0:e1c4378df3fe 97 if (success) success = this->closeEthernet();
ansond 6:edf306673e54 98 #endif
ansond 0:e1c4378df3fe 99 if (success) this->logger()->turnLEDBlue();
ansond 0:e1c4378df3fe 100 }
ansond 0:e1c4378df3fe 101
ansond 0:e1c4378df3fe 102 // initialize our preferences
ansond 0:e1c4378df3fe 103 void MBEDEndpoint::initPreferences() { if (this->m_preferences == NULL) this->m_preferences = new Preferences(this->logger()); }
ansond 0:e1c4378df3fe 104
ansond 0:e1c4378df3fe 105 // get our preferences
ansond 0:e1c4378df3fe 106 Preferences *MBEDEndpoint::preferences() { return this->m_preferences; }
ansond 0:e1c4378df3fe 107
ansond 0:e1c4378df3fe 108 // initialize the NSP address from the configuration
ansond 0:e1c4378df3fe 109 void MBEDEndpoint::initNSPAddress() {
ansond 0:e1c4378df3fe 110 memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1);
ansond 22:0f2a0269ce6d 111 memset(this->m_nsp_port_str,0,PREFERENCE_VALUE_LEN+1);
ansond 22:0f2a0269ce6d 112 if (this->m_preferences != NULL) {
ansond 22:0f2a0269ce6d 113 this->preferences()->getPreference("nsp_address",this->m_nsp_address,PREFERENCE_VALUE_LEN,NSP_ADDRESS);
ansond 22:0f2a0269ce6d 114 this->preferences()->getPreference("nsp_port",this->m_nsp_port_str,PREFERENCE_VALUE_LEN,this->m_nsp_port_str_def);
ansond 22:0f2a0269ce6d 115 sscanf(this->m_nsp_port_str,"%d",&(this->m_nsp_port));
ansond 22:0f2a0269ce6d 116 this->logger()->log("NSP: %s:%d",this->getNSPAddress(),this->getNSPPort());
ansond 22:0f2a0269ce6d 117 }
ansond 0:e1c4378df3fe 118 }
ansond 0:e1c4378df3fe 119
ansond 0:e1c4378df3fe 120 // get our NSP address
ansond 0:e1c4378df3fe 121 char *MBEDEndpoint::getNSPAddress() { return this->m_nsp_address; }
ansond 0:e1c4378df3fe 122
ansond 22:0f2a0269ce6d 123 // get our NSP port
ansond 22:0f2a0269ce6d 124 int MBEDEndpoint::getNSPPort() { return this->m_nsp_port; }
ansond 22:0f2a0269ce6d 125
ansond 0:e1c4378df3fe 126 // get our LCD status
ansond 0:e1c4378df3fe 127 char *MBEDEndpoint::getLCDStatus() {
ansond 0:e1c4378df3fe 128 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:e1c4378df3fe 129
ansond 0:e1c4378df3fe 130 // look at Light#0 to determine the IOC linkage ID...
ansond 4:7f7fe167d9c0 131 char *ioc = this->m_personalities[0]->getResourceFactory()->getResourceValue(EXTERNAL_LINKAGE_RESOURCE);
ansond 0:e1c4378df3fe 132
ansond 0:e1c4378df3fe 133 // color our LED depending on whether we have IOC linkage or not...
ansond 4:7f7fe167d9c0 134 if (ioc == NULL || strcmp(ioc,EXTERNAL_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
ansond 0:e1c4378df3fe 135 else this->logger()->turnLEDGreen();
ansond 0:e1c4378df3fe 136
ansond 22:0f2a0269ce6d 137 sprintf(this->m_lcd_status,"Node: %s\nNSP: %s:%d\nIOC Link: %s",this->getEndpointName(),this->getNSPAddress(),this->getNSPPort(),ioc);
ansond 0:e1c4378df3fe 138 return this->m_lcd_status;
ansond 0:e1c4378df3fe 139 }
ansond 4:7f7fe167d9c0 140
ansond 4:7f7fe167d9c0 141 // initialize our Personalities
ansond 4:7f7fe167d9c0 142 bool MBEDEndpoint::initializePersonalities() {
ansond 4:7f7fe167d9c0 143 #ifdef LIGHT_PERSONALITY
ansond 4:7f7fe167d9c0 144 return this->initializeLights();
ansond 7:0207dfb9c36b 145 #endif
ansond 7:0207dfb9c36b 146 #ifdef COPCAR_PERSONALITY
ansond 7:0207dfb9c36b 147 return this->initializeLights();
ansond 4:7f7fe167d9c0 148 #endif
ansond 4:7f7fe167d9c0 149 }
ansond 0:e1c4378df3fe 150
ansond 0:e1c4378df3fe 151 // initialize the Lights
ansond 0:e1c4378df3fe 152 bool MBEDEndpoint::initializeLights() {
ansond 4:7f7fe167d9c0 153 int index = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 8:4060275b2246 154 this->logger()->log("Initializing Personality(%s)...",PERSONALITY_TYPE);
ansond 4:7f7fe167d9c0 155 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) {
ansond 4:7f7fe167d9c0 156 this->m_personalities[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
ansond 4:7f7fe167d9c0 157 ((Light *)this->m_personalities[i])->setDimmerAction(new NSPLightDimmerAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 4:7f7fe167d9c0 158 ((Light *)this->m_personalities[i])->setSwitchAction(new NSPLightSwitchAction(this->logger(),(Light *)this->m_personalities[i]));
ansond 0:e1c4378df3fe 159 }
ansond 0:e1c4378df3fe 160 return true;
ansond 0:e1c4378df3fe 161 }
ansond 0:e1c4378df3fe 162
ansond 0:e1c4378df3fe 163 // send any observations we may have
ansond 0:e1c4378df3fe 164 void MBEDEndpoint::sendObservations() {
ansond 4:7f7fe167d9c0 165 for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) {
ansond 4:7f7fe167d9c0 166 if (this->m_personalities[i] != NULL) {
ansond 4:7f7fe167d9c0 167 NSPResourceFactory *resource_factory = (NSPResourceFactory *)(this->m_personalities[i]->getResourceFactory());
ansond 4:7f7fe167d9c0 168 if (resource_factory != NULL) resource_factory->sendObservations();
ansond 0:e1c4378df3fe 169 }
ansond 0:e1c4378df3fe 170 }
ansond 0:e1c4378df3fe 171 }
ansond 0:e1c4378df3fe 172
ansond 0:e1c4378df3fe 173 // initialize our ResourceFactory
ansond 4:7f7fe167d9c0 174 ResourceFactory *MBEDEndpoint::initResourceFactory() {
ansond 4:7f7fe167d9c0 175 #ifdef LIGHT_PERSONALITY
ansond 4:7f7fe167d9c0 176 return new NSPLightResourceFactory(this->logger(),(void *)this);
ansond 7:0207dfb9c36b 177 #endif
ansond 7:0207dfb9c36b 178 #ifdef COPCAR_PERSONALITY
ansond 7:0207dfb9c36b 179 return new NSPLightResourceFactory(this->logger(),(void *)this);
ansond 4:7f7fe167d9c0 180 #endif
ansond 4:7f7fe167d9c0 181 }
ansond 0:e1c4378df3fe 182
ansond 0:e1c4378df3fe 183 // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
ansond 0:e1c4378df3fe 184 void MBEDEndpoint::initEndpointName() {
ansond 4:7f7fe167d9c0 185 this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
ansond 4:7f7fe167d9c0 186 memset(_endpoint_name,0,PERSONALITY_NAME_LEN+1);
ansond 4:7f7fe167d9c0 187 memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1);
ansond 4:7f7fe167d9c0 188 sprintf(this->m_endpoint_name,PERSONALITY_NAME,this->m_instance_id);
ansond 4:7f7fe167d9c0 189 sprintf(_endpoint_name,PERSONALITY_NAME,this->m_instance_id);
ansond 0:e1c4378df3fe 190 }
ansond 0:e1c4378df3fe 191
ansond 0:e1c4378df3fe 192 // get our endpoint name
ansond 0:e1c4378df3fe 193 char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; }
ansond 0:e1c4378df3fe 194
ansond 0:e1c4378df3fe 195 // get our instance id
ansond 0:e1c4378df3fe 196 int MBEDEndpoint::getInstanceID() { return this->m_instance_id; }
ansond 0:e1c4378df3fe 197
ansond 0:e1c4378df3fe 198 // initialize a specific transport
ansond 0:e1c4378df3fe 199 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:e1c4378df3fe 200 bool success = false;
ansond 0:e1c4378df3fe 201 if (this->m_transports[index] == NULL) {
ansond 0:e1c4378df3fe 202 this->logger()->log("Initializing %s Transport...", key);
ansond 0:e1c4378df3fe 203 this->m_transports[index] = transport;
ansond 0:e1c4378df3fe 204 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:e1c4378df3fe 205 }
ansond 0:e1c4378df3fe 206 else {
ansond 0:e1c4378df3fe 207 this->logger()->log("%s already connected (OK)...", key);
ansond 0:e1c4378df3fe 208 success = true;
ansond 0:e1c4378df3fe 209 }
ansond 0:e1c4378df3fe 210 return success;
ansond 0:e1c4378df3fe 211 }
ansond 0:e1c4378df3fe 212
ansond 0:e1c4378df3fe 213 // initialize our transports
ansond 0:e1c4378df3fe 214 bool MBEDEndpoint::initializeTransports() {
ansond 0:e1c4378df3fe 215 bool success = true;
ansond 0:e1c4378df3fe 216
ansond 0:e1c4378df3fe 217 if (success == true) {
ansond 0:e1c4378df3fe 218 // NSP Initialization
ansond 5:94d000e6fa70 219 success = this->initializeTransport(NSP_TRANSPORT,"NSP",new NSPTransport(this->logger(),this));
ansond 0:e1c4378df3fe 220 }
ansond 0:e1c4378df3fe 221
ansond 0:e1c4378df3fe 222 if (success == true) {
ansond 0:e1c4378df3fe 223 // HTTP Initialization
ansond 5:94d000e6fa70 224 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->logger(),this));
ansond 0:e1c4378df3fe 225 }
ansond 0:e1c4378df3fe 226
ansond 0:e1c4378df3fe 227 return success;
ansond 0:e1c4378df3fe 228 }
ansond 0:e1c4378df3fe 229
ansond 24:b62fec3ff73c 230 // auto register with the IOC
ansond 24:b62fec3ff73c 231 bool MBEDEndpoint::autoRegisterWithIOC() {
ansond 24:b62fec3ff73c 232 bool success = false;
ansond 24:b62fec3ff73c 233 char url[AUTOREGISTER_URL_LENGTH+1];
ansond 24:b62fec3ff73c 234 char result[AUTOREGISTER_RESPONSE_LEN+1];
ansond 24:b62fec3ff73c 235
ansond 24:b62fec3ff73c 236 // we use the HTTP transport to tell the Gateway to register us on our behalf
ansond 24:b62fec3ff73c 237 HTTPTransport *http = (HTTPTransport *)this->m_transports[HTTP_TRANSPORT];
ansond 24:b62fec3ff73c 238 if (http != NULL) {
ansond 24:b62fec3ff73c 239 // DEBUG
ansond 24:b62fec3ff73c 240 this->logger()->log("Initiating registration %s with IOC...",this->getEndpointName());
ansond 24:b62fec3ff73c 241
ansond 24:b62fec3ff73c 242 // initialize buffers
ansond 24:b62fec3ff73c 243 memset(url,0,AUTOREGISTER_URL_LENGTH+1);
ansond 24:b62fec3ff73c 244 memset(result,0,AUTOREGISTER_RESPONSE_LEN+1);
ansond 24:b62fec3ff73c 245
ansond 24:b62fec3ff73c 246 // enable basic auth
ansond 24:b62fec3ff73c 247 http->basicAuth(IOC_USERNAME,IOC_PASSWORD);
ansond 24:b62fec3ff73c 248
ansond 24:b62fec3ff73c 249 // build the URL
ansond 24:b62fec3ff73c 250 sprintf(url,GW_AUTOREGISTER_URL,GW_IPADDRESS,GW_PORT,this->getEndpointName());
ansond 24:b62fec3ff73c 251
ansond 24:b62fec3ff73c 252 // DEBUG
ansond 24:b62fec3ff73c 253 //this->logger()->log("AutoRegister: %s...",url);
ansond 24:b62fec3ff73c 254
ansond 24:b62fec3ff73c 255 // issue the registration request
ansond 24:b62fec3ff73c 256 success = http->httpGet(url,result,AUTOREGISTER_RESPONSE_LEN,GW_AUTOREGISTER_TIMEOUT_MS);
ansond 24:b62fec3ff73c 257
ansond 24:b62fec3ff73c 258 // reset basic auth
ansond 24:b62fec3ff73c 259 http->basicAuth(NULL,NULL);
ansond 24:b62fec3ff73c 260 }
ansond 24:b62fec3ff73c 261
ansond 24:b62fec3ff73c 262 // return our status
ansond 24:b62fec3ff73c 263 return success;
ansond 24:b62fec3ff73c 264 }
ansond 24:b62fec3ff73c 265
ansond 11:c4d02616e10f 266 #ifdef CELLULAR_NETWORK
ansond 21:272df0444756 267 bool MBEDEndpoint::initializeCellularModem(void *modem) {
ansond 11:c4d02616e10f 268 bool success = false;
ansond 11:c4d02616e10f 269
ansond 11:c4d02616e10f 270 // initialize
ansond 19:3cecccc1a3d4 271 if (this->m_cellular_modem == NULL) this->m_cellular_modem = new MBEDUbloxCellRadio(this->logger(),(void *)this,modem);
ansond 11:c4d02616e10f 272 if (this->m_cellular_modem != NULL) success = this->m_cellular_modem->connect();
ansond 11:c4d02616e10f 273
ansond 11:c4d02616e10f 274 // return our status
ansond 11:c4d02616e10f 275 return success;
ansond 11:c4d02616e10f 276 }
ansond 11:c4d02616e10f 277
ansond 19:3cecccc1a3d4 278 bool MBEDEndpoint::initializeGPSReceiver(void *gps) {
ansond 11:c4d02616e10f 279 bool success = false;
ansond 11:c4d02616e10f 280
ansond 11:c4d02616e10f 281 // initialize
ansond 19:3cecccc1a3d4 282 if (this->m_gps == NULL) this->m_gps = new MBEDUbloxGPS(this->logger(),(void *)this,gps);
ansond 11:c4d02616e10f 283 if (this->m_gps != NULL) success = this->m_gps->connect();
ansond 11:c4d02616e10f 284
ansond 11:c4d02616e10f 285 // return our status
ansond 11:c4d02616e10f 286 return success;
ansond 11:c4d02616e10f 287 }
ansond 11:c4d02616e10f 288 #endif
ansond 11:c4d02616e10f 289
ansond 6:edf306673e54 290 #ifndef CELLULAR_NETWORK
ansond 0:e1c4378df3fe 291 // initialize our Ethernet
ansond 0:e1c4378df3fe 292 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:e1c4378df3fe 293 bool success = false;
ansond 0:e1c4378df3fe 294 this->m_ethernet = ethernet;
ansond 0:e1c4378df3fe 295 if (this->m_ethernet != NULL) {
ansond 0:e1c4378df3fe 296 this->logger()->log("Initializing Ethernet...");
ansond 0:e1c4378df3fe 297
ansond 0:e1c4378df3fe 298 // connect up ethernet
ansond 20:def10d2d9273 299 this->m_ethernet->init(); // DHCP
ansond 20:def10d2d9273 300 //this->m_ethernet->init("192.168.1.220","255.255.255.0","192.168.1.1");
ansond 0:e1c4378df3fe 301 this->m_ethernet->connect();
ansond 0:e1c4378df3fe 302
ansond 0:e1c4378df3fe 303 // display our IP address
ansond 0:e1c4378df3fe 304 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:e1c4378df3fe 305 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:e1c4378df3fe 306 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:e1c4378df3fe 307 success = true;
ansond 0:e1c4378df3fe 308 }
ansond 0:e1c4378df3fe 309 else {
ansond 0:e1c4378df3fe 310 this->logger()->log("Ethernet Not Connected...");
ansond 0:e1c4378df3fe 311 success = false;
ansond 0:e1c4378df3fe 312 }
ansond 0:e1c4378df3fe 313 }
ansond 0:e1c4378df3fe 314 else {
ansond 0:e1c4378df3fe 315 this->logger()->log("No Ethernet instance found");
ansond 0:e1c4378df3fe 316 success = false;
ansond 0:e1c4378df3fe 317 }
ansond 0:e1c4378df3fe 318 return success;
ansond 0:e1c4378df3fe 319 }
ansond 6:edf306673e54 320 #endif
ansond 0:e1c4378df3fe 321
ansond 4:7f7fe167d9c0 322 // close down the Personalities
ansond 4:7f7fe167d9c0 323 bool MBEDEndpoint::closePersonalities() {
ansond 0:e1c4378df3fe 324 bool success = true;
ansond 4:7f7fe167d9c0 325 this->logger()->log("Closing down Personalities...");
ansond 0:e1c4378df3fe 326 return success;
ansond 0:e1c4378df3fe 327 }
ansond 0:e1c4378df3fe 328
ansond 0:e1c4378df3fe 329 // close a given transport
ansond 0:e1c4378df3fe 330 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:e1c4378df3fe 331 this->logger()->log("Closing down %s Transport...", key);
ansond 0:e1c4378df3fe 332 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:e1c4378df3fe 333 return true;
ansond 0:e1c4378df3fe 334 }
ansond 0:e1c4378df3fe 335
ansond 0:e1c4378df3fe 336 // close down our transports
ansond 0:e1c4378df3fe 337 bool MBEDEndpoint::closeTransports() {
ansond 0:e1c4378df3fe 338 bool success = true;
ansond 0:e1c4378df3fe 339
ansond 0:e1c4378df3fe 340 if (success) {
ansond 0:e1c4378df3fe 341 // close NSP
ansond 0:e1c4378df3fe 342 success = this->closeTransport(NSP_TRANSPORT,"NSP");
ansond 0:e1c4378df3fe 343 }
ansond 0:e1c4378df3fe 344
ansond 0:e1c4378df3fe 345 return success;
ansond 0:e1c4378df3fe 346 }
ansond 0:e1c4378df3fe 347
ansond 11:c4d02616e10f 348 #ifdef CELLULAR_NETWORK
ansond 11:c4d02616e10f 349 bool MBEDEndpoint::closeCellularModem() {
ansond 11:c4d02616e10f 350 bool success = true;
ansond 11:c4d02616e10f 351 if (this->m_cellular_modem != NULL) success = this->m_cellular_modem->disconnect();
ansond 11:c4d02616e10f 352 return success;
ansond 11:c4d02616e10f 353 }
ansond 11:c4d02616e10f 354
ansond 11:c4d02616e10f 355 bool MBEDEndpoint::closeGPSReceiver() {
ansond 11:c4d02616e10f 356 bool success = true;
ansond 11:c4d02616e10f 357 if (this->m_gps != NULL) success = this->m_gps->disconnect();
ansond 11:c4d02616e10f 358 return success;
ansond 11:c4d02616e10f 359 }
ansond 11:c4d02616e10f 360 #endif
ansond 11:c4d02616e10f 361
ansond 6:edf306673e54 362 #ifndef CELLULAR_NETWORK
ansond 0:e1c4378df3fe 363 // close down our Ethernet
ansond 0:e1c4378df3fe 364 bool MBEDEndpoint::closeEthernet() {
ansond 0:e1c4378df3fe 365 this->logger()->log("Closing down Ethernet...");
ansond 0:e1c4378df3fe 366 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:e1c4378df3fe 367 return true;
ansond 0:e1c4378df3fe 368 }
ansond 6:edf306673e54 369 #endif
ansond 0:e1c4378df3fe 370
ansond 0:e1c4378df3fe 371 // main running loop
ansond 0:e1c4378df3fe 372 void MBEDEndpoint::run() {
ansond 0:e1c4378df3fe 373 this->logger()->log("Endpoint Main Loop");
ansond 0:e1c4378df3fe 374 this->m_transports[NSP_TRANSPORT]->checkAndProcess();
ansond 23:6f6e97a276b9 375 }
ansond 23:6f6e97a276b9 376
ansond 23:6f6e97a276b9 377 // do any extra work from within the event loop
ansond 23:6f6e97a276b9 378 void MBEDEndpoint::extraEventLoopWork() {
ansond 23:6f6e97a276b9 379 // check and send status reporting if enabled
ansond 23:6f6e97a276b9 380 #if SF_STATUS_REPORTING
ansond 23:6f6e97a276b9 381 if (this->m_status_reporter != NULL) {
ansond 23:6f6e97a276b9 382 StatusReporter *status_reporter = (StatusReporter *)this->m_status_reporter;
ansond 23:6f6e97a276b9 383 status_reporter->checkAndReportOnStatus();
ansond 23:6f6e97a276b9 384 }
ansond 23:6f6e97a276b9 385 #endif
ansond 23:6f6e97a276b9 386 }
ansond 0:e1c4378df3fe 387