mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Committer:
ansond
Date:
Fri Sep 26 05:16:26 2014 +0000
Revision:
56:789a1a8c5ebe
Parent:
50:38cd1b5f1cde
updates for new logger name

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:a3fc1c6ef150 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:a3fc1c6ef150 2 *
ansond 0:a3fc1c6ef150 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:a3fc1c6ef150 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:a3fc1c6ef150 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:a3fc1c6ef150 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:a3fc1c6ef150 7 * furnished to do so, subject to the following conditions:
ansond 0:a3fc1c6ef150 8 *
ansond 0:a3fc1c6ef150 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:a3fc1c6ef150 10 * substantial portions of the Software.
ansond 0:a3fc1c6ef150 11 *
ansond 0:a3fc1c6ef150 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:a3fc1c6ef150 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:a3fc1c6ef150 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:a3fc1c6ef150 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:a3fc1c6ef150 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:a3fc1c6ef150 17 */
ansond 0:a3fc1c6ef150 18
ansond 0:a3fc1c6ef150 19 // class definition
ansond 0:a3fc1c6ef150 20 #include "EmulatedResourceFactory.h"
ansond 0:a3fc1c6ef150 21
ansond 5:1ba6e68bf50e 22 // MBEDEndpoint instance support
ansond 0:a3fc1c6ef150 23 #include "MBEDEndpoint.h"
ansond 0:a3fc1c6ef150 24
ansond 0:a3fc1c6ef150 25 // default constructor
ansond 56:789a1a8c5ebe 26 EmulatedResourceFactory::EmulatedResourceFactory(Logger *logger,void *endpoint) : ResourceFactory(logger,endpoint) {
ansond 0:a3fc1c6ef150 27 }
ansond 0:a3fc1c6ef150 28
ansond 0:a3fc1c6ef150 29 // default destructor
ansond 0:a3fc1c6ef150 30 EmulatedResourceFactory::~EmulatedResourceFactory() {
ansond 0:a3fc1c6ef150 31 }
ansond 0:a3fc1c6ef150 32
ansond 5:1ba6e68bf50e 33 // resource creators for the EmulatedResourceFactory...
ansond 0:a3fc1c6ef150 34 void EmulatedResourceFactory::createResource(char *name,char *value) { ResourceFactory::createResource(name,value); }
ansond 0:a3fc1c6ef150 35 void EmulatedResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb) { ResourceFactory::createResource(ep_name,name,value,cb); }
ansond 0:a3fc1c6ef150 36 void EmulatedResourceFactory::createResource(char *name,char *value,void *io,void *notused) {
ansond 11:9ae0fe4517c1 37 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 0:a3fc1c6ef150 38 char *ep_name = NULL; if (endpoint != NULL) ep_name = endpoint->getEndpointName();
ansond 0:a3fc1c6ef150 39 this->m_list[this->m_count] = new Resource(this->logger(),ep_name,name,value,NULL);
ansond 0:a3fc1c6ef150 40 if (io != NULL && this->m_list[this->m_count] != NULL) {
ansond 0:a3fc1c6ef150 41 resourceInitializer initializer = (resourceInitializer)io;
ansond 0:a3fc1c6ef150 42 (initializer)(this->m_list[this->m_count]);
ansond 0:a3fc1c6ef150 43 }
ansond 0:a3fc1c6ef150 44 if (this->m_list[this->m_count] != NULL) ++this->m_count;
ansond 0:a3fc1c6ef150 45 }
ansond 0:a3fc1c6ef150 46
ansond 0:a3fc1c6ef150 47 // set a resource value (AND trigger the Emulated actions if registered)
ansond 0:a3fc1c6ef150 48 bool EmulatedResourceFactory::setResourceValue(char *name, char *value) {
ansond 0:a3fc1c6ef150 49 // set the resource value
ansond 0:a3fc1c6ef150 50 bool success = ResourceFactory::setResourceValue(name,value);
ansond 0:a3fc1c6ef150 51 if (success) {
ansond 0:a3fc1c6ef150 52 // invoke an action if registered
ansond 0:a3fc1c6ef150 53 EmulatedCallbackPointer cb = (EmulatedCallbackPointer)this->getCallbackPointer(name);
ansond 0:a3fc1c6ef150 54 if (cb != NULL) {
ansond 0:a3fc1c6ef150 55 // invoke the callback
ansond 0:a3fc1c6ef150 56 this->logger()->log("Invoking Action...");
ansond 0:a3fc1c6ef150 57 cb();
ansond 0:a3fc1c6ef150 58 }
ansond 50:38cd1b5f1cde 59
ansond 50:38cd1b5f1cde 60 // additionally update the IOC with a REST call to update its record of us (assuming 1 personality/endpoint!!)
ansond 50:38cd1b5f1cde 61 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 50:38cd1b5f1cde 62 endpoint->updatePersonality(0);
ansond 0:a3fc1c6ef150 63 }
ansond 0:a3fc1c6ef150 64 return success;
ansond 0:a3fc1c6ef150 65 }