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:
Thu Mar 27 03:52:29 2014 +0000
Revision:
4:380d79b22468
Parent:
2:e949b5a5f40a
Child:
5:1ba6e68bf50e
updates

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 0:a3fc1c6ef150 22 // MBEDEndpoint support
ansond 0:a3fc1c6ef150 23 #include "MBEDEndpoint.h"
ansond 0:a3fc1c6ef150 24
ansond 0:a3fc1c6ef150 25 // MBED supports Battery Emulation
ansond 0:a3fc1c6ef150 26 #include "MBEDBattery.h"
ansond 0:a3fc1c6ef150 27
ansond 0:a3fc1c6ef150 28 // MBED supports Current Emulation
ansond 0:a3fc1c6ef150 29 #include "MBEDCurrent.h"
ansond 0:a3fc1c6ef150 30
ansond 0:a3fc1c6ef150 31 // MBED supports Temperature Sensing
ansond 0:a3fc1c6ef150 32 #include "MBEDTemperature.h"
ansond 0:a3fc1c6ef150 33
ansond 0:a3fc1c6ef150 34 // MBED supports GPS Emulation
ansond 0:a3fc1c6ef150 35 #include "MBEDgps.h"
ansond 0:a3fc1c6ef150 36
ansond 0:a3fc1c6ef150 37 // MBED supports RSSI Emulation
ansond 0:a3fc1c6ef150 38 #include "MBEDrssi.h"
ansond 0:a3fc1c6ef150 39
ansond 0:a3fc1c6ef150 40 // MBED supports Voltage Emulation
ansond 0:a3fc1c6ef150 41 #include "MBEDVoltage.h"
ansond 0:a3fc1c6ef150 42
ansond 0:a3fc1c6ef150 43 // MBED supports Wattage Emulation
ansond 0:a3fc1c6ef150 44 #include "MBEDWattage.h"
ansond 0:a3fc1c6ef150 45
ansond 0:a3fc1c6ef150 46 // Initializers
ansond 0:a3fc1c6ef150 47 void init_battery(Resource *resource) { if (resource != NULL) new MBEDBattery(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 48 void init_current(Resource *resource) { if (resource != NULL) new MBEDCurrent(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 49 void init_gps(Resource *resource) { if (resource != NULL) new MBEDgps(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 50 void init_rssi(Resource *resource) { if (resource != NULL) new MBEDrssi(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 51 void init_temperature(Resource *resource) { if (resource != NULL) new MBEDTemperature(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 52 void init_voltage(Resource *resource) { if (resource != NULL) new MBEDVoltage(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 53 void init_wattage(Resource *resource) { if (resource != NULL) new MBEDWattage(resource->logger(),resource); }
ansond 0:a3fc1c6ef150 54
ansond 0:a3fc1c6ef150 55 // Ethernet
ansond 0:a3fc1c6ef150 56 #include "EthernetInterface.h"
ansond 0:a3fc1c6ef150 57 extern EthernetInterface ethernet;
ansond 0:a3fc1c6ef150 58
ansond 0:a3fc1c6ef150 59 // NSP supports Light Dimming
ansond 0:a3fc1c6ef150 60 extern void emulated_light_dimming_cb();
ansond 0:a3fc1c6ef150 61
ansond 0:a3fc1c6ef150 62 // NSP supports Light Switch
ansond 0:a3fc1c6ef150 63 extern void emulated_light_switch_cb();
ansond 0:a3fc1c6ef150 64
ansond 0:a3fc1c6ef150 65 // default constructor
ansond 0:a3fc1c6ef150 66 EmulatedResourceFactory::EmulatedResourceFactory(ErrorHandler *error_handler,void *endpoint) : ResourceFactory(error_handler,endpoint) {
ansond 0:a3fc1c6ef150 67 this->initGPSCoords();
ansond 0:a3fc1c6ef150 68 }
ansond 0:a3fc1c6ef150 69
ansond 0:a3fc1c6ef150 70 // default destructor
ansond 0:a3fc1c6ef150 71 EmulatedResourceFactory::~EmulatedResourceFactory() {
ansond 0:a3fc1c6ef150 72 }
ansond 0:a3fc1c6ef150 73
ansond 0:a3fc1c6ef150 74 // initialize our GPS coords
ansond 0:a3fc1c6ef150 75 void EmulatedResourceFactory::initGPSCoords() {
ansond 0:a3fc1c6ef150 76 memset(this->m_gps_coords,0,PREFERENCE_VALUE_LEN+1);
ansond 0:a3fc1c6ef150 77 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->m_endpoint;
ansond 0:a3fc1c6ef150 78 endpoint->preferences()->getPreference("coords",this->m_gps_coords,PREFERENCE_VALUE_LEN,ENDPOINT_GPS_COORDS);
ansond 0:a3fc1c6ef150 79 }
ansond 0:a3fc1c6ef150 80
ansond 0:a3fc1c6ef150 81 // get our GPS coords from preferences
ansond 0:a3fc1c6ef150 82 char *EmulatedResourceFactory::getGPSCoords() { return this->m_gps_coords; }
ansond 0:a3fc1c6ef150 83
ansond 0:a3fc1c6ef150 84 // create resources
ansond 0:a3fc1c6ef150 85 void EmulatedResourceFactory::createResources(char *endpoint_name) {
ansond 0:a3fc1c6ef150 86 // create all of the resources we expect for this endpoint
ansond 0:a3fc1c6ef150 87 this->createResource("/dev/addldata","id:0");
ansond 0:a3fc1c6ef150 88 this->createResource("/dev/location",ENDPOINT_LOCATION);
ansond 0:a3fc1c6ef150 89 this->createResource("/fw/devtype","Light");
ansond 0:a3fc1c6ef150 90 this->createResource("/dev/bat","5.0V",(void *)&init_battery,NULL);
ansond 0:a3fc1c6ef150 91 this->createResource("/sen/I","0.1A",(void *)&init_current,NULL);
ansond 0:a3fc1c6ef150 92 this->createResource("/dev/I","0.1A",(void *)&init_current,NULL);
ansond 0:a3fc1c6ef150 93 this->createResource("/nw/ipaddr",ethernet.getIPAddress());
ansond 0:a3fc1c6ef150 94 this->createResource(endpoint_name,"/lt/0/dim","25",(void *)&emulated_light_dimming_cb); // Action: dim/brighten light
ansond 0:a3fc1c6ef150 95 this->createResource("/nw/eripaddr","N/A");
ansond 0:a3fc1c6ef150 96 this->createResource(endpoint_name,"/lt/0/on","1",(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 4:380d79b22468 97 //this->createResource(endpoint_name,"/lt/0/ctr","1",(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 0:a3fc1c6ef150 98 this->createResource("/dev/mdl","MQTT MBED Light");
ansond 2:e949b5a5f40a 99 this->createResource("/dev/mfg",PLATFORM_STRING);
ansond 0:a3fc1c6ef150 100 this->createResource("/gps/int","60");
ansond 0:a3fc1c6ef150 101 this->createResource("/gps/fix","1");
ansond 0:a3fc1c6ef150 102 this->createResource("/nw/pipaddr","N/A");
ansond 0:a3fc1c6ef150 103 this->createResource("/dev/W","0.1W",(void *)&init_wattage,NULL);
ansond 0:a3fc1c6ef150 104 this->createResource("/nw/prssi","-75",(void *)&init_rssi,NULL);
ansond 0:a3fc1c6ef150 105 this->createResource("/sen/temp","10.0C",(void *)&init_temperature,NULL);
ansond 0:a3fc1c6ef150 106 this->createResource("/dev/t","10.0C",(void *)&init_temperature,NULL);
ansond 0:a3fc1c6ef150 107 this->createResource("/sen/V","5.0V",(void *)&init_voltage,NULL);
ansond 0:a3fc1c6ef150 108 this->createResource("/dev/V","5.0V",(void *)&init_voltage,NULL);
ansond 0:a3fc1c6ef150 109 this->createResource("/gps/loc",this->getGPSCoords(),(void *)&init_gps,NULL);
ansond 0:a3fc1c6ef150 110 this->createResource("/ns/nspaddr","(NA)");
ansond 0:a3fc1c6ef150 111 this->createResource(endpoint_name,"/dev/panic","0",(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 0:a3fc1c6ef150 112
ansond 0:a3fc1c6ef150 113 #ifdef MAC_ADDRESS
ansond 0:a3fc1c6ef150 114 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 0:a3fc1c6ef150 115 this->createResource("/nw/macaddr",fmt_mac);
ansond 0:a3fc1c6ef150 116 #else
ansond 0:a3fc1c6ef150 117 this->createResource("/nw/macaddr","00:00:00:00:00");
ansond 0:a3fc1c6ef150 118 #endif
ansond 0:a3fc1c6ef150 119 }
ansond 0:a3fc1c6ef150 120
ansond 0:a3fc1c6ef150 121 void EmulatedResourceFactory::createResource(char *name,char *value) { ResourceFactory::createResource(name,value); }
ansond 0:a3fc1c6ef150 122 void EmulatedResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb) { ResourceFactory::createResource(ep_name,name,value,cb); }
ansond 0:a3fc1c6ef150 123 void EmulatedResourceFactory::createResource(char *name,char *value,void *io,void *notused) {
ansond 0:a3fc1c6ef150 124 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->m_endpoint;
ansond 0:a3fc1c6ef150 125 char *ep_name = NULL; if (endpoint != NULL) ep_name = endpoint->getEndpointName();
ansond 0:a3fc1c6ef150 126 this->m_list[this->m_count] = new Resource(this->logger(),ep_name,name,value,NULL);
ansond 0:a3fc1c6ef150 127 if (io != NULL && this->m_list[this->m_count] != NULL) {
ansond 0:a3fc1c6ef150 128 resourceInitializer initializer = (resourceInitializer)io;
ansond 0:a3fc1c6ef150 129 (initializer)(this->m_list[this->m_count]);
ansond 0:a3fc1c6ef150 130 }
ansond 0:a3fc1c6ef150 131 if (this->m_list[this->m_count] != NULL) ++this->m_count;
ansond 0:a3fc1c6ef150 132 }
ansond 0:a3fc1c6ef150 133
ansond 0:a3fc1c6ef150 134 // set a resource value (AND trigger the Emulated actions if registered)
ansond 0:a3fc1c6ef150 135 bool EmulatedResourceFactory::setResourceValue(char *name, char *value) {
ansond 0:a3fc1c6ef150 136 // set the resource value
ansond 0:a3fc1c6ef150 137 bool success = ResourceFactory::setResourceValue(name,value);
ansond 0:a3fc1c6ef150 138 if (success) {
ansond 0:a3fc1c6ef150 139 // invoke an action if registered
ansond 0:a3fc1c6ef150 140 EmulatedCallbackPointer cb = (EmulatedCallbackPointer)this->getCallbackPointer(name);
ansond 0:a3fc1c6ef150 141 if (cb != NULL) {
ansond 0:a3fc1c6ef150 142 // invoke the callback
ansond 0:a3fc1c6ef150 143 this->logger()->log("Invoking Action...");
ansond 0:a3fc1c6ef150 144 cb();
ansond 0:a3fc1c6ef150 145 }
ansond 0:a3fc1c6ef150 146 }
ansond 0:a3fc1c6ef150 147 return success;
ansond 0:a3fc1c6ef150 148 }