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
Diff: MBEDEndpoint.cpp
- Revision:
- 0:e1c4378df3fe
- Child:
- 4:7f7fe167d9c0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MBEDEndpoint.cpp Wed Mar 26 19:51:29 2014 +0000 @@ -0,0 +1,248 @@ +/* Copyright C2013 Doug Anson, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files the "Software", to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + #include "NSPTransport.h" + #include "MBEDEndpoint.h" + + // MBED Light support + #include "MBEDLight.h" + + // NSP Resource Factory + #include "NSPResourceFactory.h" + + // NSP Actions we can act on + #include "NSPLightDimmerAction.h" + #include "NSPLightSwitchAction.h" + + // shutdown endpoint reference + extern void closedown(int code); + + // default constructor + MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) { + bool success = true; + this->m_preferences = NULL; + this->m_instance_id = 0; + memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1); + memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1); + for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL; + this->m_error_handler = error_handler; + this->m_error_handler->setEndpoint((void *)this); + if (success) this->initPreferences(); + if (success) this->initEndpointName(); + if (success) this->logger()->turnLEDBlue(); +#ifdef MAC_ADDRESS + extern char fmt_mac[RESOURCE_VALUE_LEN+1]; + if (success)this->logger()->log("%s (MAC: %s)",ENDPOINT_VERSION_ANNOUNCE,fmt_mac); +#else + if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE); +#endif + if (success) this->initNSPAddress(); + if (PL_ENABLE && success) this->logger()->log("Philips Light ID: %d Philips Gateway IP: %s",PL_LIGHT_ID,PL_GW_ADDRESS); + if (success) success = this->initializeEthernet(ethernet); + if (success) this->logger()->turnLEDYellow(); + if (success) success = this->initializeTransports(); + if (success) success = this->initializeLights(); + if (success) this->logger()->turnLEDOrange(); + this->logger()->lcdStatusOnly(true); + if (!success) closedown(2); + } + + // default destructor + MBEDEndpoint::~MBEDEndpoint() { + bool success = true; + if (success) this->logger()->turnLEDYellow(); + if (success) success = this->closeLights(); + if (success) success = this->closeTransports(); + if (success) success = this->closeEthernet(); + if (success) this->logger()->turnLEDBlue(); + } + + // initialize our preferences + void MBEDEndpoint::initPreferences() { if (this->m_preferences == NULL) this->m_preferences = new Preferences(this->logger()); } + + // get our preferences + Preferences *MBEDEndpoint::preferences() { return this->m_preferences; } + + // initialize the NSP address from the configuration + void MBEDEndpoint::initNSPAddress() { + memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1); + this->preferences()->getPreference("nsp_address",this->m_nsp_address,PREFERENCE_VALUE_LEN,NSP_ADDRESS); + this->logger()->log("NSP IP: %s PORT: %d",this->getNSPAddress(),NSP_PORT); + } + + // get our NSP address + char *MBEDEndpoint::getNSPAddress() { return this->m_nsp_address; } + + // get our LCD status + char *MBEDEndpoint::getLCDStatus() { + memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1); + + // look at Light#0 to determine the IOC linkage ID... + char *ioc = this->m_lights[0]->getResourceFactory()->getResourceValue(IOC_LINKAGE_RESOURCE); + + // color our LED depending on whether we have IOC linkage or not... + if (ioc == NULL || strcmp(ioc,IOC_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange(); + else this->logger()->turnLEDGreen(); + + sprintf(this->m_lcd_status,"Node: %s\nNSP IP: %s\nIOC Link: %s",this->getEndpointName(),this->getNSPAddress(),ioc); + return this->m_lcd_status; + } + + // initialize the Lights + bool MBEDEndpoint::initializeLights() { + int index = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX); + this->logger()->log("Initializing Lights..."); + for(int i=0;i<NUM_LIGHTS;++i) { + this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this); + this->m_lights[i]->setDimmerAction(new NSPLightDimmerAction(this->logger(),this->m_lights[i])); + this->m_lights[i]->setSwitchAction(new NSPLightSwitchAction(this->logger(),this->m_lights[i])); + } + return true; + } + + // send any observations we may have + void MBEDEndpoint::sendObservations() { + for(int i=0;i<NUM_LIGHTS;++i) { + if (this->m_lights[i] != NULL) { + NSPResourceFactory *resource_factory = (NSPResourceFactory *)(this->m_lights[i]->getResourceFactory()); + resource_factory->sendObservations(); + } + } + } + + // initialize our ResourceFactory + ResourceFactory *MBEDEndpoint::initResourceFactory() { return new NSPResourceFactory(this->logger(),(void *)this); } + + // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...) + void MBEDEndpoint::initEndpointName() { + this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX); + memset(_endpoint_name,0,LIGHT_NAME_LEN+1); + memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); + sprintf(this->m_endpoint_name,LIGHT_NAME,this->m_instance_id); + sprintf(_endpoint_name,LIGHT_NAME,this->m_instance_id); + } + + // get our endpoint name + char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; } + + // get our instance id + int MBEDEndpoint::getInstanceID() { return this->m_instance_id; } + + // initialize a specific transport + bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) { + bool success = false; + if (this->m_transports[index] == NULL) { + this->logger()->log("Initializing %s Transport...", key); + this->m_transports[index] = transport; + if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect(); + } + else { + this->logger()->log("%s already connected (OK)...", key); + success = true; + } + return success; + } + + // initialize our transports + bool MBEDEndpoint::initializeTransports() { + bool success = true; + + if (success == true) { + // NSP Initialization + success = this->initializeTransport(NSP_TRANSPORT,"NSP",new NSPTransport(this->m_error_handler,this)); + } + + if (success == true) { + // HTTP Initialization + success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this)); + } + + return success; + } + + // initialize our Ethernet + bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) { + bool success = false; + this->m_ethernet = ethernet; + if (this->m_ethernet != NULL) { + this->logger()->log("Initializing Ethernet..."); + + // connect up ethernet + this->m_ethernet->init(); + this->m_ethernet->connect(); + + // display our IP address + char *ipaddr = this->m_ethernet->getIPAddress(); + if (ipaddr != NULL && strlen(ipaddr) > 0) { + this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress()); + success = true; + } + else { + this->logger()->log("Ethernet Not Connected..."); + success = false; + } + } + else { + this->logger()->log("No Ethernet instance found"); + success = false; + } + return success; + } + + // close down the Lights + bool MBEDEndpoint::closeLights() { + bool success = true; + this->logger()->log("Closing down Lights..."); + return success; + } + + // close a given transport + bool MBEDEndpoint::closeTransport(int index,char *key) { + this->logger()->log("Closing down %s Transport...", key); + if (this->m_transports[index] != NULL) delete this->m_transports[index]; + return true; + } + + // close down our transports + bool MBEDEndpoint::closeTransports() { + bool success = true; + + if (success) { + // close NSP + success = this->closeTransport(NSP_TRANSPORT,"NSP"); + } + + return success; + } + + // close down our Ethernet + bool MBEDEndpoint::closeEthernet() { + this->logger()->log("Closing down Ethernet..."); + if (this->m_ethernet != NULL) this->m_ethernet->disconnect(); + return true; + } + + // get our error handler + ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } + + // main running loop + void MBEDEndpoint::run() { + this->logger()->log("Endpoint Main Loop"); + this->m_transports[NSP_TRANSPORT]->checkAndProcess(); +} + \ No newline at end of file