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:
Sat Mar 01 17:01:46 2014 +0000
Revision:
47:fa96ddc36f04
Parent:
43:361a61395588
Child:
85:8af3f3101ba3
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 10:62107616fc6c 1 /* Copyright C2013 Doug Anson, MIT License
ansond 10:62107616fc6c 2 *
ansond 10:62107616fc6c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 10:62107616fc6c 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 10:62107616fc6c 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 10:62107616fc6c 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 10:62107616fc6c 7 * furnished to do so, subject to the following conditions:
ansond 10:62107616fc6c 8 *
ansond 10:62107616fc6c 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 10:62107616fc6c 10 * substantial portions of the Software.
ansond 10:62107616fc6c 11 *
ansond 10:62107616fc6c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 10:62107616fc6c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 10:62107616fc6c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 10:62107616fc6c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 10:62107616fc6c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 10:62107616fc6c 17 */
ansond 10:62107616fc6c 18
ansond 10:62107616fc6c 19 #include "MBEDToIOCResourceMap.h"
ansond 10:62107616fc6c 20
ansond 10:62107616fc6c 21 // default constructor
ansond 20:f2dbbd852e08 22 MBEDToIOCResourceMap::MBEDToIOCResourceMap(ErrorHandler *error_handler) {
ansond 20:f2dbbd852e08 23 this->m_error_handler = error_handler;
ansond 20:f2dbbd852e08 24 for(int i=0;i<NUM_MAPPINGS;++i) this->m_map[i] = NULL;
ansond 47:fa96ddc36f04 25 this->m_count = 0;
ansond 10:62107616fc6c 26 this->createMap();
ansond 10:62107616fc6c 27 }
ansond 10:62107616fc6c 28
ansond 10:62107616fc6c 29 // destructor
ansond 10:62107616fc6c 30 MBEDToIOCResourceMap::~MBEDToIOCResourceMap() {
ansond 47:fa96ddc36f04 31 for(int i=0;i<this->m_count;++i)
ansond 20:f2dbbd852e08 32 if (this->m_map[i] != NULL) delete this->m_map[i];
ansond 10:62107616fc6c 33 }
ansond 20:f2dbbd852e08 34
ansond 20:f2dbbd852e08 35 // get our error handler
ansond 20:f2dbbd852e08 36 ErrorHandler *MBEDToIOCResourceMap::logger() { return this->m_error_handler; }
ansond 25:2a001b4f7024 37
ansond 25:2a001b4f7024 38 // get the IOC equivalent resource name from the Endpoint resource name
ansond 25:2a001b4f7024 39 char *MBEDToIOCResourceMap::endpointNameToIOCName(char *mbed_name) {
ansond 25:2a001b4f7024 40 bool found = false;
ansond 25:2a001b4f7024 41 char *ioc_name = NULL;
ansond 25:2a001b4f7024 42
ansond 25:2a001b4f7024 43 // lookup the IOC name and map it to an MBED (NSP) name
ansond 47:fa96ddc36f04 44 for(int i=0;i<this->m_count && !found && this->m_map[i] != NULL && mbed_name != NULL;++i) {
ansond 25:2a001b4f7024 45 if (strcmp(this->m_map[i]->mbedName(),mbed_name) == 0) {
ansond 25:2a001b4f7024 46 found = true;
ansond 25:2a001b4f7024 47 ioc_name = this->m_map[i]->iocName();
ansond 25:2a001b4f7024 48 }
ansond 25:2a001b4f7024 49 }
ansond 25:2a001b4f7024 50 return ioc_name;
ansond 25:2a001b4f7024 51 }
ansond 10:62107616fc6c 52
ansond 25:2a001b4f7024 53 // get the Endpoint resource name from the IOC resource name given
ansond 25:2a001b4f7024 54 char *MBEDToIOCResourceMap::iocNameToEndpointName(char *ioc_name) {
ansond 10:62107616fc6c 55 bool found = false;
ansond 10:62107616fc6c 56 char *mbed_name = NULL;
ansond 10:62107616fc6c 57
ansond 21:cfdaee0a2b50 58 // convert if we have a nickname
ansond 21:cfdaee0a2b50 59 char *actual_ioc_name = this->convertFromNickName(ioc_name);
ansond 21:cfdaee0a2b50 60
ansond 21:cfdaee0a2b50 61 // lookup the IOC name and map it to an MBED (NSP) name
ansond 47:fa96ddc36f04 62 for(int i=0;i<this->m_count && !found && this->m_map[i] != NULL && ioc_name != NULL;++i) {
ansond 21:cfdaee0a2b50 63 if (strcmp(this->m_map[i]->iocName(),actual_ioc_name) == 0) {
ansond 10:62107616fc6c 64 found = true;
ansond 11:f1c9299a3ca1 65 mbed_name = this->m_map[i]->mbedName();
ansond 10:62107616fc6c 66 }
ansond 10:62107616fc6c 67 }
ansond 10:62107616fc6c 68 return mbed_name;
ansond 10:62107616fc6c 69 }
ansond 10:62107616fc6c 70
ansond 21:cfdaee0a2b50 71 // convert from an IOC nickname to the actual IOC property name
ansond 21:cfdaee0a2b50 72 char *MBEDToIOCResourceMap::convertFromNickName(char *ioc_name) {
ansond 21:cfdaee0a2b50 73 char *actual_name = ioc_name;
ansond 21:cfdaee0a2b50 74 bool found = false;
ansond 21:cfdaee0a2b50 75
ansond 47:fa96ddc36f04 76 for(int i=0;i<this->m_count && !found && this->m_map[i] != NULL && ioc_name != NULL;++i) {
ansond 47:fa96ddc36f04 77 if (this->m_map[i]->iocNickName() != NULL && strcmp(this->m_map[i]->iocNickName(),ioc_name) == 0) {
ansond 21:cfdaee0a2b50 78 found = true;
ansond 21:cfdaee0a2b50 79 actual_name = this->m_map[i]->iocName();
ansond 21:cfdaee0a2b50 80 }
ansond 21:cfdaee0a2b50 81 }
ansond 21:cfdaee0a2b50 82
ansond 21:cfdaee0a2b50 83 return actual_name;
ansond 21:cfdaee0a2b50 84 }
ansond 21:cfdaee0a2b50 85
ansond 10:62107616fc6c 86 // create our map - this MUST match the Resource Mapping found in the gateway mapping database
ansond 10:62107616fc6c 87 void MBEDToIOCResourceMap::createMap() {
ansond 47:fa96ddc36f04 88 this->m_map[this->m_count++] = new MapEntry("Property_1","/dev/addldata");
ansond 47:fa96ddc36f04 89 this->m_map[this->m_count++] = new MapEntry("Property_2","/dev/location");
ansond 47:fa96ddc36f04 90 this->m_map[this->m_count++] = new MapEntry("Property_3","/dev/bat");
ansond 47:fa96ddc36f04 91 this->m_map[this->m_count++] = new MapEntry("Property_4","/sen/I");
ansond 47:fa96ddc36f04 92 this->m_map[this->m_count++] = new MapEntry("Property_5","/nw/ipaddr");
ansond 47:fa96ddc36f04 93 this->m_map[this->m_count++] = new MapEntry("Property_6","/lt/0/dim","Dimming");
ansond 47:fa96ddc36f04 94 this->m_map[this->m_count++] = new MapEntry("Property_7","/nw/eripaddr");
ansond 47:fa96ddc36f04 95 this->m_map[this->m_count++] = new MapEntry("Property_8","/lt/0/on","EndpointStatus");
ansond 47:fa96ddc36f04 96 this->m_map[this->m_count++] = new MapEntry("Property_9","/dev/mdl");
ansond 47:fa96ddc36f04 97 this->m_map[this->m_count++] = new MapEntry("Property_10","/gps/int");
ansond 47:fa96ddc36f04 98 this->m_map[this->m_count++] = new MapEntry("Property_11","/gps/fix");
ansond 47:fa96ddc36f04 99 this->m_map[this->m_count++] = new MapEntry("Property_12","/nw/pipaddr");
ansond 47:fa96ddc36f04 100 this->m_map[this->m_count++] = new MapEntry("Property_13","/nw/prssi");
ansond 47:fa96ddc36f04 101 this->m_map[this->m_count++] = new MapEntry("Property_14","/sen/temp");
ansond 47:fa96ddc36f04 102 this->m_map[this->m_count++] = new MapEntry("Property_15","/sen/V");
ansond 47:fa96ddc36f04 103 this->m_map[this->m_count++] = new MapEntry("LOCATION","/gps/loc");
ansond 20:f2dbbd852e08 104
ansond 20:f2dbbd852e08 105 // DEBUG
ansond 47:fa96ddc36f04 106 this->logger()->log("Installed %d Resource Mappings.",this->m_count);
ansond 10:62107616fc6c 107 }