mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
Revision 6:2db2c7e75ad9, committed 2014-03-27
- Comitter:
- ansond
- Date:
- Thu Mar 27 18:14:05 2014 +0000
- Parent:
- 5:1ba6e68bf50e
- Child:
- 7:8a4a61202b36
- Commit message:
- updates
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IOCEndpoint.cpp Thu Mar 27 18:14:05 2014 +0000 @@ -0,0 +1,146 @@ +/* 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. + */ + + // class support + #include "IOCEndpoint.h" + + // MBED instance support + #include "MBEDEndpoint.h" + + // default constructor + IOCEndpoint::IOCEndpoint(ErrorHandler *error_handler,void *endpoint) { + this->m_error_handler = error_handler; + this->m_endpoint = endpoint; + } + + // default destructor + IOCEndpoint::~IOCEndpoint() { + } + + // build out the IOC Payload + char *IOCEndpoint::buildPayload(char *data,int data_length,Light *light) { + char tmp[TEMP_BUFFER_LEN+1]; + + // MBED Endpoint + MBEDEndpoint *endpoint = (MBEDEndpoint *)this->m_endpoint; + + // construct the payload for Load/Updates + ResourceFactory *factory = light->getResourceFactory(); + + // start the buffer + strcat(data,"{"); + + // loop through the resources and build a JSON representation for the payload + for(int i=0;i<factory->numResources();++i) { + // get the ith resource + Resource *resource = factory->getResource(i); + if (resource != NULL) { + // add to the JSON payload + char *name = endpoint->getMap()->endpointNameToIOCName(resource->getName()); + char *value = resource->getValue(); + + // make sure that we have a positive IOC resource match for the NSP resource + if (name != NULL && strlen(name) > 0) { + // Handle LOCATION a special way + if (strcmp(name,"LOCATION") != 0) { + // standard name,value for IOC + sprintf(tmp, "\"%s\":\"%s\",",name,value); + } + else { + // IOC expects "Point(X,Y)" for LOCATION + sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value); + } + strcat(data,tmp); + + // Handle /dev/addldata + char *dev_addldata = endpoint->getMap()->endpointNameToIOCName("/dev/addldata"); + if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) { + char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID()); + sprintf(tmp,"\"%s\":\"id:%s\",",name,buf); + strcat(data,tmp); + } + } + } + } + + // Special Case: STARTDATETIME + strcat(data,ENDPOINT_STARTTIME); + + // Special Case: ENDDATETIME + strcat(data,ENDPOINT_STOPTIME); + + // Special Case: NAME + sprintf(tmp,"\"NAME\":\"%s\",",light->getName()); + strcat(data,tmp); + + // Special Case: TIMEZONEOFFSET + strcat(data,ENDPOINT_TIMEZONE); + + // close + strcat(data,"}"); + + // DEBUG + //this->logger()->log("Loading Payload: %s",data); + + // return the payload + return data; + } + + // save the IOC ID for a Light node + void IOCEndpoint::saveLightID(Light *light,char *json) { + if (json != NULL) { + //this->logger()->log("RESULT: %s",json); + + // look for "id": + char *check = "\"id\":"; + char *pos1 = strstr(json,check); + if (pos1 != NULL) { + char *pos2 = strstr(pos1,","); + if (pos1 != NULL && pos2 != NULL && pos2 > pos1) { + pos1 += strlen(check); + int length = pos2 - pos1; + char str_ioc_id[IOC_IOC_ID_LEN+1]; + memset(str_ioc_id,0,IOC_IOC_ID_LEN+1); + strncpy(str_ioc_id,pos1,length); + + // DEBUG + //this->logger()->log("IOC ID found: %s",str_ioc_id); + + // parse into int + int ioc_id = 0; + sscanf(str_ioc_id,"%d",&ioc_id); + + // save the IOC ID + if (ioc_id > 0) light->setIOCID(ioc_id); + } + else { + // cannot find the ID tag in the result JSON + this->logger()->log("Cannot find the IOC ID in the JSON result"); + this->logger()->log("JSON: %s",json); + } + } + else { + // cannot find the ID tag in the result JSON + this->logger()->log("Cannot find the IOC ID in the JSON result"); + this->logger()->log("JSON: %s",json); + } + } + } + + // get our error handler + ErrorHandler *IOCEndpoint::logger() { return this->m_error_handler; } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IOCEndpoint.h Thu Mar 27 18:14:05 2014 +0000 @@ -0,0 +1,44 @@ +/* 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. + */ + +#ifndef _IOC_ENDPOINT_H_ +#define _IOC_ENDPOINT_H_ + +// Error handler support +#include "ErrorHandler.h" + +// Light personality support +#include "Light.h" + +class IOCEndpoint { + private: + ErrorHandler *m_error_handler; + void *m_endpoint; + + public: + IOCEndpoint(ErrorHandler *error_handler,void *endpoint); + ~IOCEndpoint(); + + char *buildPayload(char *data,int data_length,Light *light); + void saveLightID(Light *light,char *data); + + private: + ErrorHandler *logger(); +}; + +#endif // _IOC_ENDPOINT_H_ \ No newline at end of file
--- a/MBEDEndpoint.cpp Thu Mar 27 17:48:38 2014 +0000 +++ b/MBEDEndpoint.cpp Thu Mar 27 18:14:05 2014 +0000 @@ -45,6 +45,7 @@ memset(this->m_gw_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_ioc_endpoint = new IOCEndpoint(error_handler,(void *)this); this->m_error_handler->setEndpoint((void *)this); if (success) this->initPreferences(); if (success) this->initEndpointName(); @@ -62,7 +63,7 @@ if (success) this->logger()->turnLEDYellow(); if (success)this->m_map = new MBEDToIOCResourceMap(error_handler); if (success) success = this->initializeTransports(); - if (success) success = this->initializePersonality(); + if (success) success = this->initializeEndpointPersonality(); if (success) this->logger()->turnLEDOrange(); this->logger()->lcdStatusOnly(true); if (!success) closedown(2); @@ -72,7 +73,7 @@ MBEDEndpoint::~MBEDEndpoint() { bool success = true; if (success) this->logger()->turnLEDYellow(); - if (success) success = this->closePersonality(); + if (success) success = this->closeEndpointPersonality(); if (success) success = this->closeTransports(); if (success) success = this->closeEthernet(); if (success) this->logger()->turnLEDBlue(); @@ -263,7 +264,7 @@ this->logger()->log("Building Payload for Light: %s...",light->getName()); // build the payload - char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); + char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light); // issue the request if (data != NULL && strlen(data) > 0) { @@ -278,7 +279,7 @@ //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName()); // update the IOC ID if found - if (success) this->saveIOCID(light,result); + if (success) this->iocEndpoint()->saveLightID(light,result); // DEBUG if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID()); @@ -317,7 +318,7 @@ memset(payload,0,IOC_PAYLOAD_LEN+1); // build the payload - char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); + char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light); // issue the request if (data != NULL && strlen(data) > 0) { @@ -336,113 +337,6 @@ return success; } - // build out the Payload - char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) { - char tmp[TEMP_BUFFER_LEN+1]; - - // construct the payload for Load/Updates - ResourceFactory *factory = light->getResourceFactory(); - - // start the buffer - strcat(data,"{"); - - // loop through the resources and build a JSON representation for the payload - for(int i=0;i<factory->numResources();++i) { - // get the ith resource - Resource *resource = factory->getResource(i); - if (resource != NULL) { - // add to the JSON payload - char *name = this->getMap()->endpointNameToIOCName(resource->getName()); - char *value = resource->getValue(); - - // make sure that we have a positive IOC resource match for the NSP resource - if (name != NULL && strlen(name) > 0) { - // Handle LOCATION a special way - if (strcmp(name,"LOCATION") != 0) { - // standard name,value for IOC - sprintf(tmp, "\"%s\":\"%s\",",name,value); - } - else { - // IOC expects "Point(X,Y)" for LOCATION - sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value); - } - strcat(data,tmp); - - // Handle /dev/addldata - char *dev_addldata = this->getMap()->endpointNameToIOCName("/dev/addldata"); - if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) { - char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID()); - sprintf(tmp,"\"%s\":\"id:%s\",",name,buf); - strcat(data,tmp); - } - } - } - } - - // Special Case: STARTDATETIME - strcat(data,ENDPOINT_STARTTIME); - - // Special Case: ENDDATETIME - strcat(data,ENDPOINT_STOPTIME); - - // Special Case: NAME - sprintf(tmp,"\"NAME\":\"%s\",",light->getName()); - strcat(data,tmp); - - // Special Case: TIMEZONEOFFSET - strcat(data,ENDPOINT_TIMEZONE); - - // close - strcat(data,"}"); - - // DEBUG - //this->logger()->log("Loading Payload: %s",data); - - // return the payload - return data; - } - - // save the IOC ID - void MBEDEndpoint::saveIOCID(Light *light,char *json) { - if (json != NULL) { - //this->logger()->log("RESULT: %s",json); - - // look for "id": - char *check = "\"id\":"; - char *pos1 = strstr(json,check); - if (pos1 != NULL) { - char *pos2 = strstr(pos1,","); - if (pos1 != NULL && pos2 != NULL && pos2 > pos1) { - pos1 += strlen(check); - int length = pos2 - pos1; - char str_ioc_id[IOC_IOC_ID_LEN+1]; - memset(str_ioc_id,0,IOC_IOC_ID_LEN+1); - strncpy(str_ioc_id,pos1,length); - - // DEBUG - //this->logger()->log("IOC ID found: %s",str_ioc_id); - - // parse into int - int ioc_id = 0; - sscanf(str_ioc_id,"%d",&ioc_id); - - // save the IOC ID - if (ioc_id > 0) light->setIOCID(ioc_id); - } - else { - // cannot find the ID tag in the result JSON - this->logger()->log("Cannot find the IOC ID in the JSON result"); - this->logger()->log("JSON: %s",json); - } - } - else { - // cannot find the ID tag in the result JSON - this->logger()->log("Cannot find the IOC ID in the JSON result"); - this->logger()->log("JSON: %s",json); - } - } - } - // close down our endpoint personality bool MBEDEndpoint::closeEndpointPersonality() { #ifdef LIGHT_PERSONALITY @@ -501,6 +395,9 @@ // get our error handler ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } + // get our IOC endpoint + IOCEndpoint *MBEDEndpoint::iocEndpoint() { return this->m_ioc_endpoint; } + // main running loop void MBEDEndpoint::run() { this->logger()->log("Endpoint Main Loop");
--- a/MBEDEndpoint.h Thu Mar 27 17:48:38 2014 +0000 +++ b/MBEDEndpoint.h Thu Mar 27 18:14:05 2014 +0000 @@ -32,6 +32,9 @@ #include "MQTTTransport.h" #include "IOCHTTPTransport.h" +// IOC Endpoint support +#include "IOCEndpoint.h" + // Light Support #include "Light.h" @@ -45,6 +48,7 @@ private: EthernetInterface *m_ethernet; // ethernet interface ErrorHandler *m_error_handler; // our error handler + IOCEndpoint *m_ioc_endpoint; // IOC integration Transport *m_transports[NUM_TRANSPORTS]; // our transport Light *m_lights[NUM_LIGHTS]; // our lights (at least 1) char m_endpoint_name[LIGHT_NAME_LEN+1]; // our endpoint name (light[0]) @@ -93,14 +97,13 @@ bool initializeEthernet(EthernetInterface *ethernet); bool loadEndpoint(Light *light); bool updateEndpoint(Light *light); - char *buildIOCPayload(char *data,int data_length,Light *light); - void saveIOCID(Light *light,char *data); bool closeLights(); bool closeTransport(int index,char *key); bool closeTransports(); bool closeEthernet(); ErrorHandler *logger(); int min(int value1, int value2); + IOCEndpoint *iocEndpoint(); }; #endif // _MBED_ENDPOINT_H_