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:
Wed Mar 26 19:51:29 2014 +0000
Revision:
0:e1c4378df3fe
Child:
4:7f7fe167d9c0
updates

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 0:e1c4378df3fe 22 // MBED Light support
ansond 0:e1c4378df3fe 23 #include "MBEDLight.h"
ansond 0:e1c4378df3fe 24
ansond 0:e1c4378df3fe 25 // NSP Resource Factory
ansond 0:e1c4378df3fe 26 #include "NSPResourceFactory.h"
ansond 0:e1c4378df3fe 27
ansond 0:e1c4378df3fe 28 // 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 0:e1c4378df3fe 32 // shutdown endpoint reference
ansond 0:e1c4378df3fe 33 extern void closedown(int code);
ansond 0:e1c4378df3fe 34
ansond 0:e1c4378df3fe 35 // default constructor
ansond 0:e1c4378df3fe 36 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
ansond 0:e1c4378df3fe 37 bool success = true;
ansond 0:e1c4378df3fe 38 this->m_preferences = NULL;
ansond 0:e1c4378df3fe 39 this->m_instance_id = 0;
ansond 0:e1c4378df3fe 40 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:e1c4378df3fe 41 memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1);
ansond 0:e1c4378df3fe 42 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 0:e1c4378df3fe 43 this->m_error_handler = error_handler;
ansond 0:e1c4378df3fe 44 this->m_error_handler->setEndpoint((void *)this);
ansond 0:e1c4378df3fe 45 if (success) this->initPreferences();
ansond 0:e1c4378df3fe 46 if (success) this->initEndpointName();
ansond 0:e1c4378df3fe 47 if (success) this->logger()->turnLEDBlue();
ansond 0:e1c4378df3fe 48 #ifdef MAC_ADDRESS
ansond 0:e1c4378df3fe 49 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 0:e1c4378df3fe 50 if (success)this->logger()->log("%s (MAC: %s)",ENDPOINT_VERSION_ANNOUNCE,fmt_mac);
ansond 0:e1c4378df3fe 51 #else
ansond 0:e1c4378df3fe 52 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:e1c4378df3fe 53 #endif
ansond 0:e1c4378df3fe 54 if (success) this->initNSPAddress();
ansond 0:e1c4378df3fe 55 if (PL_ENABLE && success) this->logger()->log("Philips Light ID: %d Philips Gateway IP: %s",PL_LIGHT_ID,PL_GW_ADDRESS);
ansond 0:e1c4378df3fe 56 if (success) success = this->initializeEthernet(ethernet);
ansond 0:e1c4378df3fe 57 if (success) this->logger()->turnLEDYellow();
ansond 0:e1c4378df3fe 58 if (success) success = this->initializeTransports();
ansond 0:e1c4378df3fe 59 if (success) success = this->initializeLights();
ansond 0:e1c4378df3fe 60 if (success) this->logger()->turnLEDOrange();
ansond 0:e1c4378df3fe 61 this->logger()->lcdStatusOnly(true);
ansond 0:e1c4378df3fe 62 if (!success) closedown(2);
ansond 0:e1c4378df3fe 63 }
ansond 0:e1c4378df3fe 64
ansond 0:e1c4378df3fe 65 // default destructor
ansond 0:e1c4378df3fe 66 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:e1c4378df3fe 67 bool success = true;
ansond 0:e1c4378df3fe 68 if (success) this->logger()->turnLEDYellow();
ansond 0:e1c4378df3fe 69 if (success) success = this->closeLights();
ansond 0:e1c4378df3fe 70 if (success) success = this->closeTransports();
ansond 0:e1c4378df3fe 71 if (success) success = this->closeEthernet();
ansond 0:e1c4378df3fe 72 if (success) this->logger()->turnLEDBlue();
ansond 0:e1c4378df3fe 73 }
ansond 0:e1c4378df3fe 74
ansond 0:e1c4378df3fe 75 // initialize our preferences
ansond 0:e1c4378df3fe 76 void MBEDEndpoint::initPreferences() { if (this->m_preferences == NULL) this->m_preferences = new Preferences(this->logger()); }
ansond 0:e1c4378df3fe 77
ansond 0:e1c4378df3fe 78 // get our preferences
ansond 0:e1c4378df3fe 79 Preferences *MBEDEndpoint::preferences() { return this->m_preferences; }
ansond 0:e1c4378df3fe 80
ansond 0:e1c4378df3fe 81 // initialize the NSP address from the configuration
ansond 0:e1c4378df3fe 82 void MBEDEndpoint::initNSPAddress() {
ansond 0:e1c4378df3fe 83 memset(this->m_nsp_address,0,PREFERENCE_VALUE_LEN+1);
ansond 0:e1c4378df3fe 84 this->preferences()->getPreference("nsp_address",this->m_nsp_address,PREFERENCE_VALUE_LEN,NSP_ADDRESS);
ansond 0:e1c4378df3fe 85 this->logger()->log("NSP IP: %s PORT: %d",this->getNSPAddress(),NSP_PORT);
ansond 0:e1c4378df3fe 86 }
ansond 0:e1c4378df3fe 87
ansond 0:e1c4378df3fe 88 // get our NSP address
ansond 0:e1c4378df3fe 89 char *MBEDEndpoint::getNSPAddress() { return this->m_nsp_address; }
ansond 0:e1c4378df3fe 90
ansond 0:e1c4378df3fe 91 // get our LCD status
ansond 0:e1c4378df3fe 92 char *MBEDEndpoint::getLCDStatus() {
ansond 0:e1c4378df3fe 93 memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
ansond 0:e1c4378df3fe 94
ansond 0:e1c4378df3fe 95 // look at Light#0 to determine the IOC linkage ID...
ansond 0:e1c4378df3fe 96 char *ioc = this->m_lights[0]->getResourceFactory()->getResourceValue(IOC_LINKAGE_RESOURCE);
ansond 0:e1c4378df3fe 97
ansond 0:e1c4378df3fe 98 // color our LED depending on whether we have IOC linkage or not...
ansond 0:e1c4378df3fe 99 if (ioc == NULL || strcmp(ioc,IOC_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
ansond 0:e1c4378df3fe 100 else this->logger()->turnLEDGreen();
ansond 0:e1c4378df3fe 101
ansond 0:e1c4378df3fe 102 sprintf(this->m_lcd_status,"Node: %s\nNSP IP: %s\nIOC Link: %s",this->getEndpointName(),this->getNSPAddress(),ioc);
ansond 0:e1c4378df3fe 103 return this->m_lcd_status;
ansond 0:e1c4378df3fe 104 }
ansond 0:e1c4378df3fe 105
ansond 0:e1c4378df3fe 106 // initialize the Lights
ansond 0:e1c4378df3fe 107 bool MBEDEndpoint::initializeLights() {
ansond 0:e1c4378df3fe 108 int index = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX);
ansond 0:e1c4378df3fe 109 this->logger()->log("Initializing Lights...");
ansond 0:e1c4378df3fe 110 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:e1c4378df3fe 111 this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
ansond 0:e1c4378df3fe 112 this->m_lights[i]->setDimmerAction(new NSPLightDimmerAction(this->logger(),this->m_lights[i]));
ansond 0:e1c4378df3fe 113 this->m_lights[i]->setSwitchAction(new NSPLightSwitchAction(this->logger(),this->m_lights[i]));
ansond 0:e1c4378df3fe 114 }
ansond 0:e1c4378df3fe 115 return true;
ansond 0:e1c4378df3fe 116 }
ansond 0:e1c4378df3fe 117
ansond 0:e1c4378df3fe 118 // send any observations we may have
ansond 0:e1c4378df3fe 119 void MBEDEndpoint::sendObservations() {
ansond 0:e1c4378df3fe 120 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:e1c4378df3fe 121 if (this->m_lights[i] != NULL) {
ansond 0:e1c4378df3fe 122 NSPResourceFactory *resource_factory = (NSPResourceFactory *)(this->m_lights[i]->getResourceFactory());
ansond 0:e1c4378df3fe 123 resource_factory->sendObservations();
ansond 0:e1c4378df3fe 124 }
ansond 0:e1c4378df3fe 125 }
ansond 0:e1c4378df3fe 126 }
ansond 0:e1c4378df3fe 127
ansond 0:e1c4378df3fe 128 // initialize our ResourceFactory
ansond 0:e1c4378df3fe 129 ResourceFactory *MBEDEndpoint::initResourceFactory() { return new NSPResourceFactory(this->logger(),(void *)this); }
ansond 0:e1c4378df3fe 130
ansond 0:e1c4378df3fe 131 // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
ansond 0:e1c4378df3fe 132 void MBEDEndpoint::initEndpointName() {
ansond 0:e1c4378df3fe 133 this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX);
ansond 0:e1c4378df3fe 134 memset(_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 0:e1c4378df3fe 135 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 0:e1c4378df3fe 136 sprintf(this->m_endpoint_name,LIGHT_NAME,this->m_instance_id);
ansond 0:e1c4378df3fe 137 sprintf(_endpoint_name,LIGHT_NAME,this->m_instance_id);
ansond 0:e1c4378df3fe 138 }
ansond 0:e1c4378df3fe 139
ansond 0:e1c4378df3fe 140 // get our endpoint name
ansond 0:e1c4378df3fe 141 char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; }
ansond 0:e1c4378df3fe 142
ansond 0:e1c4378df3fe 143 // get our instance id
ansond 0:e1c4378df3fe 144 int MBEDEndpoint::getInstanceID() { return this->m_instance_id; }
ansond 0:e1c4378df3fe 145
ansond 0:e1c4378df3fe 146 // initialize a specific transport
ansond 0:e1c4378df3fe 147 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:e1c4378df3fe 148 bool success = false;
ansond 0:e1c4378df3fe 149 if (this->m_transports[index] == NULL) {
ansond 0:e1c4378df3fe 150 this->logger()->log("Initializing %s Transport...", key);
ansond 0:e1c4378df3fe 151 this->m_transports[index] = transport;
ansond 0:e1c4378df3fe 152 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:e1c4378df3fe 153 }
ansond 0:e1c4378df3fe 154 else {
ansond 0:e1c4378df3fe 155 this->logger()->log("%s already connected (OK)...", key);
ansond 0:e1c4378df3fe 156 success = true;
ansond 0:e1c4378df3fe 157 }
ansond 0:e1c4378df3fe 158 return success;
ansond 0:e1c4378df3fe 159 }
ansond 0:e1c4378df3fe 160
ansond 0:e1c4378df3fe 161 // initialize our transports
ansond 0:e1c4378df3fe 162 bool MBEDEndpoint::initializeTransports() {
ansond 0:e1c4378df3fe 163 bool success = true;
ansond 0:e1c4378df3fe 164
ansond 0:e1c4378df3fe 165 if (success == true) {
ansond 0:e1c4378df3fe 166 // NSP Initialization
ansond 0:e1c4378df3fe 167 success = this->initializeTransport(NSP_TRANSPORT,"NSP",new NSPTransport(this->m_error_handler,this));
ansond 0:e1c4378df3fe 168 }
ansond 0:e1c4378df3fe 169
ansond 0:e1c4378df3fe 170 if (success == true) {
ansond 0:e1c4378df3fe 171 // HTTP Initialization
ansond 0:e1c4378df3fe 172 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this));
ansond 0:e1c4378df3fe 173 }
ansond 0:e1c4378df3fe 174
ansond 0:e1c4378df3fe 175 return success;
ansond 0:e1c4378df3fe 176 }
ansond 0:e1c4378df3fe 177
ansond 0:e1c4378df3fe 178 // initialize our Ethernet
ansond 0:e1c4378df3fe 179 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:e1c4378df3fe 180 bool success = false;
ansond 0:e1c4378df3fe 181 this->m_ethernet = ethernet;
ansond 0:e1c4378df3fe 182 if (this->m_ethernet != NULL) {
ansond 0:e1c4378df3fe 183 this->logger()->log("Initializing Ethernet...");
ansond 0:e1c4378df3fe 184
ansond 0:e1c4378df3fe 185 // connect up ethernet
ansond 0:e1c4378df3fe 186 this->m_ethernet->init();
ansond 0:e1c4378df3fe 187 this->m_ethernet->connect();
ansond 0:e1c4378df3fe 188
ansond 0:e1c4378df3fe 189 // display our IP address
ansond 0:e1c4378df3fe 190 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:e1c4378df3fe 191 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:e1c4378df3fe 192 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:e1c4378df3fe 193 success = true;
ansond 0:e1c4378df3fe 194 }
ansond 0:e1c4378df3fe 195 else {
ansond 0:e1c4378df3fe 196 this->logger()->log("Ethernet Not Connected...");
ansond 0:e1c4378df3fe 197 success = false;
ansond 0:e1c4378df3fe 198 }
ansond 0:e1c4378df3fe 199 }
ansond 0:e1c4378df3fe 200 else {
ansond 0:e1c4378df3fe 201 this->logger()->log("No Ethernet instance found");
ansond 0:e1c4378df3fe 202 success = false;
ansond 0:e1c4378df3fe 203 }
ansond 0:e1c4378df3fe 204 return success;
ansond 0:e1c4378df3fe 205 }
ansond 0:e1c4378df3fe 206
ansond 0:e1c4378df3fe 207 // close down the Lights
ansond 0:e1c4378df3fe 208 bool MBEDEndpoint::closeLights() {
ansond 0:e1c4378df3fe 209 bool success = true;
ansond 0:e1c4378df3fe 210 this->logger()->log("Closing down Lights...");
ansond 0:e1c4378df3fe 211 return success;
ansond 0:e1c4378df3fe 212 }
ansond 0:e1c4378df3fe 213
ansond 0:e1c4378df3fe 214 // close a given transport
ansond 0:e1c4378df3fe 215 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:e1c4378df3fe 216 this->logger()->log("Closing down %s Transport...", key);
ansond 0:e1c4378df3fe 217 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:e1c4378df3fe 218 return true;
ansond 0:e1c4378df3fe 219 }
ansond 0:e1c4378df3fe 220
ansond 0:e1c4378df3fe 221 // close down our transports
ansond 0:e1c4378df3fe 222 bool MBEDEndpoint::closeTransports() {
ansond 0:e1c4378df3fe 223 bool success = true;
ansond 0:e1c4378df3fe 224
ansond 0:e1c4378df3fe 225 if (success) {
ansond 0:e1c4378df3fe 226 // close NSP
ansond 0:e1c4378df3fe 227 success = this->closeTransport(NSP_TRANSPORT,"NSP");
ansond 0:e1c4378df3fe 228 }
ansond 0:e1c4378df3fe 229
ansond 0:e1c4378df3fe 230 return success;
ansond 0:e1c4378df3fe 231 }
ansond 0:e1c4378df3fe 232
ansond 0:e1c4378df3fe 233 // close down our Ethernet
ansond 0:e1c4378df3fe 234 bool MBEDEndpoint::closeEthernet() {
ansond 0:e1c4378df3fe 235 this->logger()->log("Closing down Ethernet...");
ansond 0:e1c4378df3fe 236 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:e1c4378df3fe 237 return true;
ansond 0:e1c4378df3fe 238 }
ansond 0:e1c4378df3fe 239
ansond 0:e1c4378df3fe 240 // get our error handler
ansond 0:e1c4378df3fe 241 ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
ansond 0:e1c4378df3fe 242
ansond 0:e1c4378df3fe 243 // main running loop
ansond 0:e1c4378df3fe 244 void MBEDEndpoint::run() {
ansond 0:e1c4378df3fe 245 this->logger()->log("Endpoint Main Loop");
ansond 0:e1c4378df3fe 246 this->m_transports[NSP_TRANSPORT]->checkAndProcess();
ansond 0:e1c4378df3fe 247 }
ansond 0:e1c4378df3fe 248