Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Committer:
ansond
Date:
Mon Mar 03 03:32:42 2014 +0000
Revision:
60:76728655fa12
Parent:
59:c58774344049
Child:
125:979332edc1c2
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 6:9a4085eeac52 1 /* Copyright C2013 Doug Anson, MIT License
ansond 6:9a4085eeac52 2 *
ansond 6:9a4085eeac52 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 6:9a4085eeac52 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 6:9a4085eeac52 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 6:9a4085eeac52 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 6:9a4085eeac52 7 * furnished to do so, subject to the following conditions:
ansond 6:9a4085eeac52 8 *
ansond 6:9a4085eeac52 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 6:9a4085eeac52 10 * substantial portions of the Software.
ansond 6:9a4085eeac52 11 *
ansond 6:9a4085eeac52 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 6:9a4085eeac52 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 6:9a4085eeac52 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 6:9a4085eeac52 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 6:9a4085eeac52 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 6:9a4085eeac52 17 */
ansond 6:9a4085eeac52 18
ansond 6:9a4085eeac52 19 #include "Resource.h"
ansond 40:eae89a487d86 20 #include "MBEDio.h"
ansond 6:9a4085eeac52 21
ansond 6:9a4085eeac52 22 // default constructor
ansond 54:d315b7c0a7a3 23 Resource::Resource(ErrorHandler *error_handler,char *ep_name,char *name,char *value,void *cb) {
ansond 6:9a4085eeac52 24 this->m_error_handler = error_handler;
ansond 42:ff05e71c4cb5 25 this->m_io = NULL;
ansond 6:9a4085eeac52 26 memset(this->m_name,0,RESOURCE_NAME_LEN+1);
ansond 6:9a4085eeac52 27 memset(this->m_value,0,RESOURCE_VALUE_LEN+1);
ansond 47:fa96ddc36f04 28 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 6:9a4085eeac52 29 this->setName(name);
ansond 6:9a4085eeac52 30 this->setValue(value);
ansond 9:90fadae5489a 31 this->setCallbackPointer(cb);
ansond 58:1552dd51615b 32 this->plumb(ep_name,this->m_name);
ansond 6:9a4085eeac52 33 }
ansond 6:9a4085eeac52 34
ansond 6:9a4085eeac52 35 // default destructor
ansond 6:9a4085eeac52 36 Resource::~Resource() {
ansond 6:9a4085eeac52 37 }
ansond 6:9a4085eeac52 38
ansond 6:9a4085eeac52 39 // get the error handler
ansond 6:9a4085eeac52 40 ErrorHandler *Resource::logger() { return this->m_error_handler; }
ansond 6:9a4085eeac52 41
ansond 6:9a4085eeac52 42 // get and set the name of the resource
ansond 6:9a4085eeac52 43 char *Resource::getName() { return this->m_name; }
ansond 51:73e837ee5aa6 44 void Resource::setName(char *name) {
ansond 51:73e837ee5aa6 45 if (name != NULL) {
ansond 51:73e837ee5aa6 46 memset(this->m_name,0,RESOURCE_NAME_LEN+1);
ansond 51:73e837ee5aa6 47 strncpy(this->m_name,name,this->min(RESOURCE_NAME_LEN,strlen(name)));
ansond 51:73e837ee5aa6 48 }
ansond 51:73e837ee5aa6 49 }
ansond 6:9a4085eeac52 50
ansond 56:604a75f111fd 51 // get the value of the resource
ansond 47:fa96ddc36f04 52 char *Resource::getValue() { this->extract(this->getName()); return this->getValuePointer(); }
ansond 58:1552dd51615b 53 char *Resource::getValuePointer() {
ansond 60:76728655fa12 54 //this->logger()->log("GET: Name: %s Value: %s",this->m_name,this->m_value);
ansond 58:1552dd51615b 55 return this->m_value;
ansond 58:1552dd51615b 56 }
ansond 56:604a75f111fd 57
ansond 56:604a75f111fd 58 // set the value of the resource
ansond 56:604a75f111fd 59 void Resource::setValue(char *value) { this->setValue(value,strlen(value)); }
ansond 56:604a75f111fd 60 void Resource::setValue(char *value,int length) {
ansond 56:604a75f111fd 61 if (value != NULL && length > 0) {
ansond 56:604a75f111fd 62 memset(this->m_value,0,RESOURCE_VALUE_LEN+1);
ansond 56:604a75f111fd 63 strncpy(this->m_value,value,this->min(RESOURCE_VALUE_LEN,length));
ansond 60:76728655fa12 64 //this->logger()->log("SET: Name: %s Value: %s",this->m_name,this->m_value);
ansond 56:604a75f111fd 65 }
ansond 51:73e837ee5aa6 66 }
ansond 6:9a4085eeac52 67
ansond 6:9a4085eeac52 68 // plumb the resource (base class does nothing other than save the endpoint_name)
ansond 57:da85ca0fbbf9 69 void Resource::plumb(char *ep_name,char *name) {
ansond 60:76728655fa12 70 //this->logger()->log("Plumbing Resource - Endpoint: %s Resource: %s",ep_name,name);
ansond 51:73e837ee5aa6 71 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 57:da85ca0fbbf9 72 if (ep_name != NULL) {
ansond 51:73e837ee5aa6 73 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 57:da85ca0fbbf9 74 strncpy(this->m_endpoint_name,ep_name,this->min(LIGHT_NAME_LEN,strlen(ep_name)));
ansond 51:73e837ee5aa6 75 }
ansond 51:73e837ee5aa6 76 }
ansond 6:9a4085eeac52 77
ansond 40:eae89a487d86 78 // extract the resource value
ansond 40:eae89a487d86 79 void Resource::extract(char *name) {
ansond 40:eae89a487d86 80 if (this->m_io != NULL) {
ansond 60:76728655fa12 81 //this->logger()->log("EXTRACT: Invoking MBEDio UPDATE for Name: %s Value: %s",this->m_name,this->m_value);
ansond 41:e54e5888c49e 82 MBEDio *io = (MBEDio *)this->m_io;
ansond 40:eae89a487d86 83 io->update();
ansond 40:eae89a487d86 84 }
ansond 40:eae89a487d86 85 }
ansond 6:9a4085eeac52 86
ansond 6:9a4085eeac52 87 // get the endpoint anme
ansond 56:604a75f111fd 88 char *Resource::getEndpointName() { return this->m_endpoint_name; }
ansond 6:9a4085eeac52 89
ansond 40:eae89a487d86 90 // set the base I/O
ansond 59:c58774344049 91 void Resource::setIO(void *io) {
ansond 60:76728655fa12 92 //this->logger()->log("SET: MBEDio SET for Name: %s Value: %s",this->m_name,this->m_value);
ansond 59:c58774344049 93 this->m_io = io;
ansond 59:c58774344049 94 }
ansond 40:eae89a487d86 95
ansond 6:9a4085eeac52 96 // set the internal resource linkage
ansond 9:90fadae5489a 97 void Resource::setCallbackPointer(void *cb) { this->m_cb = cb; }
ansond 9:90fadae5489a 98 void *Resource::getCallbackPointer() { return this->m_cb; }
ansond 6:9a4085eeac52 99
ansond 10:62107616fc6c 100 // min function
ansond 10:62107616fc6c 101 int Resource::min(int value1,int value2) {
ansond 6:9a4085eeac52 102 if (value1 < value2) return value1;
ansond 6:9a4085eeac52 103 return value2;
ansond 10:62107616fc6c 104 }
ansond 6:9a4085eeac52 105