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:
Wed Mar 19 04:12:53 2014 +0000
Revision:
116:428281cb5066
Parent:
114:bd38ad417d6a
Child:
117:34f0d83dcabb
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 112:1fb53d4729af 1 /* Copyright C2013 Doug Anson, MIT License
ansond 112:1fb53d4729af 2 *
ansond 112:1fb53d4729af 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 112:1fb53d4729af 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 112:1fb53d4729af 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 112:1fb53d4729af 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 112:1fb53d4729af 7 * furnished to do so, subject to the following conditions:
ansond 112:1fb53d4729af 8 *
ansond 112:1fb53d4729af 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 112:1fb53d4729af 10 * substantial portions of the Software.
ansond 112:1fb53d4729af 11 *
ansond 112:1fb53d4729af 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 112:1fb53d4729af 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 112:1fb53d4729af 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 112:1fb53d4729af 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 112:1fb53d4729af 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 112:1fb53d4729af 17 */
ansond 112:1fb53d4729af 18
ansond 112:1fb53d4729af 19 #include "mbed.h"
ansond 112:1fb53d4729af 20 #include "Preferences.h"
ansond 112:1fb53d4729af 21
ansond 116:428281cb5066 22 LocalFileSystem local("local");
ansond 116:428281cb5066 23
ansond 112:1fb53d4729af 24 // default constructor
ansond 112:1fb53d4729af 25 Preferences::Preferences(ErrorHandler *error_handler) {
ansond 112:1fb53d4729af 26 this->m_error_handler = error_handler;
ansond 112:1fb53d4729af 27 this->m_config_file = new ConfigFile();
ansond 112:1fb53d4729af 28 this->initialize();
ansond 112:1fb53d4729af 29 }
ansond 112:1fb53d4729af 30
ansond 112:1fb53d4729af 31 // destructor
ansond 112:1fb53d4729af 32 Preferences::~Preferences() {
ansond 112:1fb53d4729af 33 if (this->m_config_file != NULL) delete this->m_config_file;
ansond 112:1fb53d4729af 34 }
ansond 112:1fb53d4729af 35
ansond 112:1fb53d4729af 36 // initialize the preferences from the preferences file
ansond 112:1fb53d4729af 37 bool Preferences::initialize() {
ansond 112:1fb53d4729af 38 bool success = true;
ansond 112:1fb53d4729af 39
ansond 112:1fb53d4729af 40 // read from the config file
ansond 112:1fb53d4729af 41 if (this->m_config_file != NULL) {
ansond 112:1fb53d4729af 42 if (!this->m_config_file->read(PREFERENCES_FILE)) {
ansond 112:1fb53d4729af 43 this->logger()->log("Unable to initialize preferences: %s",PREFERENCES_FILE);
ansond 112:1fb53d4729af 44 success = false;
ansond 112:1fb53d4729af 45 }
ansond 112:1fb53d4729af 46 else {
ansond 112:1fb53d4729af 47 // success!
ansond 112:1fb53d4729af 48 this->logger()->log("Preference File: %s read successfully",PREFERENCES_FILE);
ansond 112:1fb53d4729af 49 success = true;
ansond 112:1fb53d4729af 50 }
ansond 112:1fb53d4729af 51 }
ansond 112:1fb53d4729af 52 else {
ansond 112:1fb53d4729af 53 // no config file object
ansond 112:1fb53d4729af 54 this->logger()->log("Unable to initialize preferences: %s. No instance.",PREFERENCES_FILE);
ansond 112:1fb53d4729af 55 success = false;
ansond 112:1fb53d4729af 56 }
ansond 112:1fb53d4729af 57
ansond 112:1fb53d4729af 58 // return our status
ansond 112:1fb53d4729af 59 return success;
ansond 112:1fb53d4729af 60 }
ansond 112:1fb53d4729af 61
ansond 112:1fb53d4729af 62 // integer preference with defaults
ansond 112:1fb53d4729af 63 int Preferences::getIntPreference(char *name,int def_value) {\
ansond 112:1fb53d4729af 64 int int_value = def_value;
ansond 112:1fb53d4729af 65 char buffer[PREFERENCE_VALUE_LEN+1];
ansond 112:1fb53d4729af 66 memset(buffer,0,PREFERENCE_VALUE_LEN+1);
ansond 112:1fb53d4729af 67 char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
ansond 116:428281cb5066 68 if (value != NULL && strlen(value) > 0) sscanf(buffer,"%d",&int_value);
ansond 112:1fb53d4729af 69 return int_value;
ansond 112:1fb53d4729af 70 }
ansond 112:1fb53d4729af 71
ansond 112:1fb53d4729af 72 // boolean preference with defaults
ansond 112:1fb53d4729af 73 bool Preferences::getBooleanPreference(char *name,bool def_value) {
ansond 112:1fb53d4729af 74 bool bool_value = def_value;
ansond 112:1fb53d4729af 75 char buffer[PREFERENCE_VALUE_LEN+1];
ansond 112:1fb53d4729af 76 memset(buffer,0,PREFERENCE_VALUE_LEN+1);
ansond 112:1fb53d4729af 77 char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
ansond 112:1fb53d4729af 78 if (value != NULL && strcmp(value,"true") == 0) bool_value = true;
ansond 112:1fb53d4729af 79 if (value != NULL && strcmp(value,"false") == 0) bool_value = false;
ansond 112:1fb53d4729af 80 return bool_value;
ansond 112:1fb53d4729af 81 }
ansond 112:1fb53d4729af 82
ansond 112:1fb53d4729af 83 // string preference with defaults
ansond 112:1fb53d4729af 84 char *Preferences::getPreference(char *name,char *buffer,int buffer_length,char *def_value) {
ansond 114:bd38ad417d6a 85 if (this->m_config_file != NULL) {
ansond 114:bd38ad417d6a 86 memset(buffer,0,buffer_length);
ansond 116:428281cb5066 87 this->m_config_file->getValue(name,&buffer[0],buffer_length);
ansond 114:bd38ad417d6a 88 if (strlen(buffer) == 0 && def_value != NULL) strncpy(buffer,def_value,this->min(buffer_length,strlen(def_value)));
ansond 114:bd38ad417d6a 89 }
ansond 114:bd38ad417d6a 90 return buffer;
ansond 112:1fb53d4729af 91 }
ansond 112:1fb53d4729af 92
ansond 112:1fb53d4729af 93 // get our error handler
ansond 112:1fb53d4729af 94 ErrorHandler *Preferences::logger() { return this->m_error_handler; }
ansond 112:1fb53d4729af 95
ansond 112:1fb53d4729af 96 // get our preference count
ansond 112:1fb53d4729af 97 int Preferences::numPreferences() { return this->m_config_file->getCount(); }
ansond 112:1fb53d4729af 98
ansond 112:1fb53d4729af 99 // min function
ansond 112:1fb53d4729af 100 int Preferences::min(int value1,int value2) {
ansond 112:1fb53d4729af 101 if (value1 < value2) return value1;
ansond 112:1fb53d4729af 102 return value2;
ansond 112:1fb53d4729af 103 }