Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
Resource.cpp@192:54b758a8eaaa, 2014-09-26 (annotated)
- Committer:
- ansond
- Date:
- Fri Sep 26 05:16:07 2014 +0000
- Revision:
- 192:54b758a8eaaa
- Parent:
- 135:7f3f963cd159
updates to new logger name
Who changed what in which revision?
User | Revision | Line number | New 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 | 192:54b758a8eaaa | 23 | Resource::Resource(Logger *logger,char *ep_name,char *name,char *value,void *cb) : BaseClass(logger,NULL) { |
ansond | 42:ff05e71c4cb5 | 24 | this->m_io = NULL; |
ansond | 6:9a4085eeac52 | 25 | memset(this->m_name,0,RESOURCE_NAME_LEN+1); |
ansond | 6:9a4085eeac52 | 26 | memset(this->m_value,0,RESOURCE_VALUE_LEN+1); |
ansond | 134:58e7537a8c5f | 27 | memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1); |
ansond | 6:9a4085eeac52 | 28 | this->setName(name); |
ansond | 6:9a4085eeac52 | 29 | this->setValue(value); |
ansond | 9:90fadae5489a | 30 | this->setCallbackPointer(cb); |
ansond | 58:1552dd51615b | 31 | this->plumb(ep_name,this->m_name); |
ansond | 6:9a4085eeac52 | 32 | } |
ansond | 6:9a4085eeac52 | 33 | |
ansond | 6:9a4085eeac52 | 34 | // default destructor |
ansond | 6:9a4085eeac52 | 35 | Resource::~Resource() { |
ansond | 6:9a4085eeac52 | 36 | } |
ansond | 135:7f3f963cd159 | 37 | |
ansond | 6:9a4085eeac52 | 38 | // get and set the name of the resource |
ansond | 6:9a4085eeac52 | 39 | char *Resource::getName() { return this->m_name; } |
ansond | 51:73e837ee5aa6 | 40 | void Resource::setName(char *name) { |
ansond | 51:73e837ee5aa6 | 41 | if (name != NULL) { |
ansond | 51:73e837ee5aa6 | 42 | memset(this->m_name,0,RESOURCE_NAME_LEN+1); |
ansond | 51:73e837ee5aa6 | 43 | strncpy(this->m_name,name,this->min(RESOURCE_NAME_LEN,strlen(name))); |
ansond | 51:73e837ee5aa6 | 44 | } |
ansond | 51:73e837ee5aa6 | 45 | } |
ansond | 6:9a4085eeac52 | 46 | |
ansond | 56:604a75f111fd | 47 | // get the value of the resource |
ansond | 47:fa96ddc36f04 | 48 | char *Resource::getValue() { this->extract(this->getName()); return this->getValuePointer(); } |
ansond | 58:1552dd51615b | 49 | char *Resource::getValuePointer() { |
ansond | 60:76728655fa12 | 50 | //this->logger()->log("GET: Name: %s Value: %s",this->m_name,this->m_value); |
ansond | 58:1552dd51615b | 51 | return this->m_value; |
ansond | 58:1552dd51615b | 52 | } |
ansond | 56:604a75f111fd | 53 | |
ansond | 56:604a75f111fd | 54 | // set the value of the resource |
ansond | 56:604a75f111fd | 55 | void Resource::setValue(char *value) { this->setValue(value,strlen(value)); } |
ansond | 56:604a75f111fd | 56 | void Resource::setValue(char *value,int length) { |
ansond | 56:604a75f111fd | 57 | if (value != NULL && length > 0) { |
ansond | 56:604a75f111fd | 58 | memset(this->m_value,0,RESOURCE_VALUE_LEN+1); |
ansond | 56:604a75f111fd | 59 | strncpy(this->m_value,value,this->min(RESOURCE_VALUE_LEN,length)); |
ansond | 60:76728655fa12 | 60 | //this->logger()->log("SET: Name: %s Value: %s",this->m_name,this->m_value); |
ansond | 56:604a75f111fd | 61 | } |
ansond | 51:73e837ee5aa6 | 62 | } |
ansond | 6:9a4085eeac52 | 63 | |
ansond | 6:9a4085eeac52 | 64 | // plumb the resource (base class does nothing other than save the endpoint_name) |
ansond | 57:da85ca0fbbf9 | 65 | void Resource::plumb(char *ep_name,char *name) { |
ansond | 60:76728655fa12 | 66 | //this->logger()->log("Plumbing Resource - Endpoint: %s Resource: %s",ep_name,name); |
ansond | 134:58e7537a8c5f | 67 | memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1); |
ansond | 57:da85ca0fbbf9 | 68 | if (ep_name != NULL) { |
ansond | 134:58e7537a8c5f | 69 | memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1); |
ansond | 134:58e7537a8c5f | 70 | strncpy(this->m_endpoint_name,ep_name,this->min(PERSONALITY_NAME_LEN,strlen(ep_name))); |
ansond | 51:73e837ee5aa6 | 71 | } |
ansond | 51:73e837ee5aa6 | 72 | } |
ansond | 6:9a4085eeac52 | 73 | |
ansond | 40:eae89a487d86 | 74 | // extract the resource value |
ansond | 40:eae89a487d86 | 75 | void Resource::extract(char *name) { |
ansond | 40:eae89a487d86 | 76 | if (this->m_io != NULL) { |
ansond | 60:76728655fa12 | 77 | //this->logger()->log("EXTRACT: Invoking MBEDio UPDATE for Name: %s Value: %s",this->m_name,this->m_value); |
ansond | 41:e54e5888c49e | 78 | MBEDio *io = (MBEDio *)this->m_io; |
ansond | 40:eae89a487d86 | 79 | io->update(); |
ansond | 40:eae89a487d86 | 80 | } |
ansond | 40:eae89a487d86 | 81 | } |
ansond | 6:9a4085eeac52 | 82 | |
ansond | 6:9a4085eeac52 | 83 | // get the endpoint anme |
ansond | 56:604a75f111fd | 84 | char *Resource::getEndpointName() { return this->m_endpoint_name; } |
ansond | 6:9a4085eeac52 | 85 | |
ansond | 40:eae89a487d86 | 86 | // set the base I/O |
ansond | 59:c58774344049 | 87 | void Resource::setIO(void *io) { |
ansond | 60:76728655fa12 | 88 | //this->logger()->log("SET: MBEDio SET for Name: %s Value: %s",this->m_name,this->m_value); |
ansond | 59:c58774344049 | 89 | this->m_io = io; |
ansond | 59:c58774344049 | 90 | } |
ansond | 40:eae89a487d86 | 91 | |
ansond | 125:979332edc1c2 | 92 | // get the base I/O |
ansond | 125:979332edc1c2 | 93 | void *Resource::getIO() { return this->m_io; } |
ansond | 125:979332edc1c2 | 94 | |
ansond | 6:9a4085eeac52 | 95 | // set the internal resource linkage |
ansond | 9:90fadae5489a | 96 | void Resource::setCallbackPointer(void *cb) { this->m_cb = cb; } |
ansond | 135:7f3f963cd159 | 97 | void *Resource::getCallbackPointer() { return this->m_cb; } |