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: NSPResourceFactory.cpp
- Revision:
- 0:e1c4378df3fe
- Child:
- 2:a895137e9082
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NSPResourceFactory.cpp Wed Mar 26 19:51:29 2014 +0000 @@ -0,0 +1,227 @@ +/* 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 definition + #include "NSPResourceFactory.h" + + // NSP support + #include "nsdl_support.h" + + // NSP resource support + #include "NSPResource.h" + + // Ethernet Interface + #include "EthernetInterface.h" + extern EthernetInterface ethernet; + + // NSP Support Includes + #include "nsdl_support.h" + + // MBED supports GPS location observation + #include "gps.h" + + // MBED supports RSSI observation + #include "rssi.h" + + // MBED supports Temperature Sensing + #include "temperature.h" + + // MBED supports Battery Emulation + #include "battery.h" + + // MBED supports Voltage Emulation + #include "voltage.h" + + // MBED supports Wattage Emulation + #include "wattage.h" + + // MBED support Current Emulation + #include "current.h" + + // MBED support Dimming Operations + #include "dimmer.h" + + // MBED support Light Switch Operations + #include "switch.h" + + // MBED support AddlData for IOC Integration + #include "addldata.h" + + // MBED supports observable MDL text + #include "mdltext.h" + + // MBED supports observable MFG text + #include "mfgtext.h" + + // MBED supports observable location text + #include "locationtext.h" + + // default constructor + NSPResourceFactory::NSPResourceFactory(ErrorHandler *error_handler,void *endpoint) : ResourceFactory(error_handler,endpoint) { + memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); + this->m_resource_ptr = NULL; + } + + // default destructor + NSPResourceFactory::~NSPResourceFactory() { + } + + // set the endpoint name + void NSPResourceFactory::setEndpointName(char *ep_name) { + memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1); + strncpy(this->m_endpoint_name,ep_name,this->min(strlen(ep_name),LIGHT_NAME_LEN)); + } + + // get the endpoint name + char *NSPResourceFactory::getEndpointName() { return this->m_endpoint_name; } + + // send any observations we have + void NSPResourceFactory::sendObservations() { + for(int i=0;i<this->m_count;++i) { + NSPResource *resource = (NSPResource *)this->m_list[i]; + if (resource != NULL) { + resource->sendObservation(); + } + } + } + + // create resources (base class does nothing) + void NSPResourceFactory::createResources(char *ep_name) { + // record the endpoint name + this->setEndpointName(ep_name); + + // initialize resource structures + this->initResourceStructures(); + + // create all of the resources we expect for this endpoint + this->createResource(ep_name,"/dev/addldata","id:0",(void *)&addldata_cb,(sn_grs_resource_acl_e)(SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED),(void *)&init_addldata,"core#s",(char *)NULL); + this->createResource(ep_name,"/dev/location",ENDPOINT_LOCATION,(void *)&locationtext_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_locationtext,"core#s","ns:location"); + this->createResource("/fw/devtype",NSP_NODE_TYPE,"core#s","ns:devtype"); + this->createResource(ep_name,"/dev/bat","5.0V",(void *)&battery_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_battery,(char *)NULL,"ipso:dev-bat"); + this->createResource(ep_name,"/sen/I","0.1",(void *)¤t_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_current,(char *)NULL,"ucum:A"); + this->createResource(ep_name,"/dev/I","0.1",(void *)¤t_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_current,(char *)NULL,"ucum:A"); + this->createResource("/nw/ipaddr",ethernet.getIPAddress(),"core#s","ns:v4addr"); + this->createResource(ep_name,"/lt/0/dim","25",(void *)&dimmer_cb,(sn_grs_resource_acl_e)(SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED),(void *)&init_dimmer,(char *)NULL,"ipso:lt-dim"); // Action: dim/brighten light + this->createResource("/nw/eripaddr","N/A","core#s","ns:v4addr"); + this->createResource(ep_name,"/lt/0/on","1",(void *)&switch_cb,(sn_grs_resource_acl_e)(SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED),(void *)&init_switch,(char *)NULL,"ipso:lt-on"); // Action: light on/off + this->createResource(ep_name,"/lt/0/ctr","1",(void *)&switch_cb,(sn_grs_resource_acl_e)(SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED),(void *)&init_switch,(char *)NULL,"ns:boolean"); // Action: light on/off + this->createResource(ep_name,"/dev/mdl","Sensinode MBED Light",(void *)&mdltext_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_mdltext,"core#s","ipso:dev-mdl"); + this->createResource(ep_name,"/dev/mfg","NXP MBED",(void *)&mfgtext_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_mfgtext,"core#s","ipso:dev-mfg"); + this->createResource("/gps/int","60","core#s","ns:counter"); + this->createResource("/gps/fix","1","core#s","ns:boolean"); + this->createResource("/nw/pipaddr","N/A","core#s","ns:v4addr"); + this->createResource(ep_name,"/nw/prssi","-75",(void *)&rssi_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_rssi,"core#s","ns:rssi"); + this->createResource(ep_name,"/dev/W","0.1",(void *)&wattage_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_wattage,(char *)NULL,"ucum:W"); + this->createResource(ep_name,"/sen/temp","10.0",(void *)&temp_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_temp,(char *)NULL,"ucum:Cel"); + this->createResource(ep_name,"/dev/t","10.0",(void *)&temp_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_temp,(char *)NULL,"ucum:Cel"); + this->createResource(ep_name,"/sen/V","5.0",(void *)&voltage_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_voltage,(char *)NULL,"ucum:V"); + this->createResource(ep_name,"/dev/V","5.0",(void *)&voltage_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_voltage,(char *)NULL,"ucum:V"); + this->createResource(ep_name,"/gps/loc",ENDPOINT_GPS_COORDS,(void *)&gps_location_cb,(sn_grs_resource_acl_e)SN_GRS_GET_ALLOWED,(void *)&init_gps,"core#s","ns:gpsloc"); + this->createResource("/ns/nspaddr",NSP_ADDRESS,"core#s","ns:v4addr"); + this->createResource(ep_name,"/dev/panic","0",(void *)&switch_cb,(sn_grs_resource_acl_e)(SN_GRS_GET_ALLOWED),(void *)&init_switch,"core#s","ns:panic"); + +#ifdef MAC_ADDRESS + extern char fmt_mac[RESOURCE_VALUE_LEN+1]; + this->createResource("/nw/macaddr",fmt_mac,"core#s","ns:euid64"); +#else + this->createResource("/nw/macaddr","00:00:00:00:00","core#s","ns:euid64"); +#endif + + // finalize the registration + this->finalizeRegistration(); + } + + // create an NSP resource + void NSPResourceFactory::createResource(char *name,char *value) { this->createResource(name,value,(char *)NULL,(char *)NULL); } + void NSPResourceFactory::createResource(char *name,char *value,char *interface,char *resource) { + this->createResource(this->getEndpointName(),name,value,NULL,SN_GRS_GET_ALLOWED,NULL,interface,resource); + } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb) { this->createResource(ep_name,name,value,cb,(char *)NULL,(char *)NULL); } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb,char *interface, char *resource) { + this->createResource(ep_name,name,value,cb,SN_GRS_GET_ALLOWED,NULL,interface,resource); + } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb,sn_grs_resource_acl_e acl) { + this->createResource(ep_name,name,value,cb,acl,(char *)NULL,(char *)NULL); + } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb,sn_grs_resource_acl_e acl,char *interface, char *resource) { + this->createResource(ep_name,name,value,cb,acl,NULL,interface,resource); + } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb,sn_grs_resource_acl_e acl,void *io) { + this->createResource(ep_name,name,value,cb,acl,io,(char *)NULL,(char *)NULL); + } + void NSPResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb,sn_grs_resource_acl_e acl,void *io,char *interface, char *resource) { + // initialize the resource + this->m_list[this->m_count] = new NSPResource(this->logger(),ep_name,name,value,cb,this->m_resource_ptr,acl,interface,resource); + + // call the resource initializer if needed + if (io != NULL && this->m_list[this->m_count] != NULL) { + //this->logger()->log("Calling initializer for %s...",this->m_list[this->m_count]->getName()); + resourceInitializer initializer = (resourceInitializer)io; + (initializer)(this->m_list[this->m_count]); + } + + // increment the counter + if (this->m_list[this->m_count] != NULL) ++this->m_count; + } + + // initialize resource structures if needed + void NSPResourceFactory::initResourceStructures() { + if (this->m_resource_ptr == NULL) { + this->m_resource_ptr = (void *)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); + if (this->m_resource_ptr != NULL) { + memset(this->m_resource_ptr,0,sizeof(sn_nsdl_resource_info_s)); + ((sn_nsdl_resource_info_s *)this->m_resource_ptr)->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); + if (((sn_nsdl_resource_info_s *)this->m_resource_ptr)->resource_parameters_ptr != NULL) { + memset(((sn_nsdl_resource_info_s *)this->m_resource_ptr)->resource_parameters_ptr,0,sizeof(sn_nsdl_resource_parameters_s)); + } + else { + this->logger()->log("NSP: malloc of NSP resource_parameters_ptr FAILED"); + this->freeResourceStructures(); + } + } + else { + this->logger()->log("NSP: malloc of NSP resource_ptr FAILED"); + this->freeResourceStructures(); + } + } + } + + // free resource structures + void NSPResourceFactory::freeResourceStructures() { + if (this->m_resource_ptr != NULL) { + nsdl_free(((sn_nsdl_resource_info_s *)this->m_resource_ptr)->resource_parameters_ptr); + nsdl_free((sn_nsdl_resource_info_s *)this->m_resource_ptr); + } + this->m_resource_ptr = NULL; + } + + // finalize the registrations + void NSPResourceFactory::finalizeRegistration() { + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + // register the endpoint and its resources with NSP + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr,(uint8_t*)_endpoint_name,_ep_type,_lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + this->logger()->log("NSP: Registration of resources for %s FAILED",this->getEndpointName()); + else + this->logger()->log("Installed %d NSP resources",this->m_count); + + // clean up + if (endpoint_ptr != NULL) nsdl_clean_register_endpoint(&endpoint_ptr); + this->freeResourceStructures(); + } \ No newline at end of file