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:
Thu Mar 20 02:30:06 2014 +0000
Revision:
121:40bb95a10a0e
Parent:
120:edf33bd41e4f
Child:
122:21be9cc9e63d
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->initialize();
ansond 112:1fb53d4729af 28 }
ansond 112:1fb53d4729af 29
ansond 112:1fb53d4729af 30 // destructor
ansond 112:1fb53d4729af 31 Preferences::~Preferences() {
ansond 112:1fb53d4729af 32 }
ansond 112:1fb53d4729af 33
ansond 112:1fb53d4729af 34 // initialize the preferences from the preferences file
ansond 112:1fb53d4729af 35 bool Preferences::initialize() {
ansond 118:c8a80adfe90d 36 char buffer[PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10];
ansond 112:1fb53d4729af 37 bool success = true;
ansond 112:1fb53d4729af 38
ansond 118:c8a80adfe90d 39 // initialize the memory buffers and count
ansond 118:c8a80adfe90d 40 for(int i=0;i<MAX_NUM_PREFERENCES;++i) {
ansond 118:c8a80adfe90d 41 memset(this->m_names[i],0,PREFERENCE_NAME_LEN+1);
ansond 118:c8a80adfe90d 42 memset(this->m_values[i],0,PREFERENCE_VALUE_LEN+1);
ansond 118:c8a80adfe90d 43 }
ansond 118:c8a80adfe90d 44 this->m_num_preferences = 0;
ansond 118:c8a80adfe90d 45
ansond 118:c8a80adfe90d 46 // read and open the config file
ansond 118:c8a80adfe90d 47 FILE *fp = fopen(PREFERENCES_FILE,"r");
ansond 118:c8a80adfe90d 48 if (fp != NULL) {
ansond 118:c8a80adfe90d 49 // read in the first line
ansond 118:c8a80adfe90d 50 memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10);
ansond 118:c8a80adfe90d 51 int n = fscanf(fp,"%s",buffer);
ansond 118:c8a80adfe90d 52
ansond 118:c8a80adfe90d 53 // loop and read each line
ansond 118:c8a80adfe90d 54 while(n != EOF) {
ansond 118:c8a80adfe90d 55 // replace the equals sign with a space
ansond 118:c8a80adfe90d 56 for(int i=0;i<strlen(buffer);++i) if (buffer[i] == '=') buffer[i] = ' ';
ansond 118:c8a80adfe90d 57
ansond 118:c8a80adfe90d 58 // parse
ansond 118:c8a80adfe90d 59 sscanf(buffer,"%s%s",this->m_names[this->m_num_preferences],this->m_values[this->m_num_preferences]);
ansond 118:c8a80adfe90d 60
ansond 118:c8a80adfe90d 61 // DEBUG
ansond 118:c8a80adfe90d 62 this->logger()->log("Installed Preference: %s=%s",this->m_names[this->m_num_preferences],this->m_values[this->m_num_preferences]);
ansond 118:c8a80adfe90d 63
ansond 118:c8a80adfe90d 64 // increment the tally
ansond 118:c8a80adfe90d 65 ++this->m_num_preferences;
ansond 118:c8a80adfe90d 66
ansond 118:c8a80adfe90d 67 // reset the buffer and read in another line
ansond 118:c8a80adfe90d 68 memset(buffer,0,PREFERENCE_NAME_LEN + PREFERENCE_NAME_LEN + 10);
ansond 118:c8a80adfe90d 69 n = fscanf(fp,"%s",buffer);
ansond 112:1fb53d4729af 70 }
ansond 118:c8a80adfe90d 71
ansond 118:c8a80adfe90d 72 // close
ansond 118:c8a80adfe90d 73 fclose(fp);
ansond 118:c8a80adfe90d 74
ansond 118:c8a80adfe90d 75 // summary
ansond 118:c8a80adfe90d 76 this->logger()->log("Installed %d preferences",this->m_num_preferences);
ansond 112:1fb53d4729af 77 }
ansond 112:1fb53d4729af 78 else {
ansond 118:c8a80adfe90d 79 // unable to open the preferences file - not loaded
ansond 118:c8a80adfe90d 80 this->logger()->log("Unable to open preferences file %s... Preferences not loaded.",PREFERENCES_FILE);
ansond 112:1fb53d4729af 81 success = false;
ansond 112:1fb53d4729af 82 }
ansond 112:1fb53d4729af 83
ansond 112:1fb53d4729af 84 // return our status
ansond 112:1fb53d4729af 85 return success;
ansond 112:1fb53d4729af 86 }
ansond 112:1fb53d4729af 87
ansond 120:edf33bd41e4f 88 // HACK: fix up coords because IOC's POINT() macro does not like commas
ansond 120:edf33bd41e4f 89 void Preferences::fixCoordsForIOC() {
ansond 121:40bb95a10a0e 90 int index = this->indexOfPreference("coords");
ansond 120:edf33bd41e4f 91 if (index >= 0) {
ansond 120:edf33bd41e4f 92 // remove the comma...
ansond 120:edf33bd41e4f 93 for(int i=0;i<strlen(this->m_values[index]);++i) if (this->m_values[index][i] == ',') this->m_values[index][i] = ' ';
ansond 120:edf33bd41e4f 94 }
ansond 120:edf33bd41e4f 95 }
ansond 120:edf33bd41e4f 96
ansond 112:1fb53d4729af 97 // integer preference with defaults
ansond 112:1fb53d4729af 98 int Preferences::getIntPreference(char *name,int def_value) {\
ansond 112:1fb53d4729af 99 int int_value = def_value;
ansond 112:1fb53d4729af 100 char buffer[PREFERENCE_VALUE_LEN+1];
ansond 112:1fb53d4729af 101 memset(buffer,0,PREFERENCE_VALUE_LEN+1);
ansond 112:1fb53d4729af 102 char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
ansond 116:428281cb5066 103 if (value != NULL && strlen(value) > 0) sscanf(buffer,"%d",&int_value);
ansond 112:1fb53d4729af 104 return int_value;
ansond 112:1fb53d4729af 105 }
ansond 112:1fb53d4729af 106
ansond 112:1fb53d4729af 107 // boolean preference with defaults
ansond 112:1fb53d4729af 108 bool Preferences::getBooleanPreference(char *name,bool def_value) {
ansond 112:1fb53d4729af 109 bool bool_value = def_value;
ansond 112:1fb53d4729af 110 char buffer[PREFERENCE_VALUE_LEN+1];
ansond 112:1fb53d4729af 111 memset(buffer,0,PREFERENCE_VALUE_LEN+1);
ansond 112:1fb53d4729af 112 char *value = this->getPreference(name,buffer,PREFERENCE_VALUE_LEN,NULL);
ansond 112:1fb53d4729af 113 if (value != NULL && strcmp(value,"true") == 0) bool_value = true;
ansond 112:1fb53d4729af 114 if (value != NULL && strcmp(value,"false") == 0) bool_value = false;
ansond 112:1fb53d4729af 115 return bool_value;
ansond 112:1fb53d4729af 116 }
ansond 112:1fb53d4729af 117
ansond 112:1fb53d4729af 118 // string preference with defaults
ansond 112:1fb53d4729af 119 char *Preferences::getPreference(char *name,char *buffer,int buffer_length,char *def_value) {
ansond 118:c8a80adfe90d 120 char *value = NULL;
ansond 118:c8a80adfe90d 121
ansond 118:c8a80adfe90d 122 // clean the buffer
ansond 118:c8a80adfe90d 123 memset(buffer,0,buffer_length);
ansond 118:c8a80adfe90d 124
ansond 118:c8a80adfe90d 125 // get our index
ansond 118:c8a80adfe90d 126 int index = this->indexOfPreference(name);
ansond 118:c8a80adfe90d 127 if (index >= 0) {
ansond 118:c8a80adfe90d 128 // fill with our value
ansond 118:c8a80adfe90d 129 strncpy(buffer,this->m_values[index],this->min(buffer_length,strlen(this->m_values[index])));
ansond 118:c8a80adfe90d 130 value = buffer;
ansond 118:c8a80adfe90d 131 }
ansond 118:c8a80adfe90d 132 else {
ansond 118:c8a80adfe90d 133 // fill with the default
ansond 118:c8a80adfe90d 134 if (def_value != NULL) strncpy(buffer,def_value,this->min(buffer_length,strlen(def_value)));
ansond 118:c8a80adfe90d 135 if (def_value != NULL) value = buffer;
ansond 114:bd38ad417d6a 136 }
ansond 118:c8a80adfe90d 137
ansond 118:c8a80adfe90d 138 // return the value
ansond 118:c8a80adfe90d 139 return value;
ansond 118:c8a80adfe90d 140 }
ansond 118:c8a80adfe90d 141
ansond 118:c8a80adfe90d 142 // get the index of the named preference
ansond 118:c8a80adfe90d 143 int Preferences::indexOfPreference(char *name) {
ansond 118:c8a80adfe90d 144 bool found = false;
ansond 118:c8a80adfe90d 145 int index = -1;
ansond 118:c8a80adfe90d 146
ansond 118:c8a80adfe90d 147 // parameter check
ansond 118:c8a80adfe90d 148 if (name != NULL && strlen(name) > 0) {
ansond 118:c8a80adfe90d 149
ansond 118:c8a80adfe90d 150 // loop until we find the name.. then stop and record its index
ansond 118:c8a80adfe90d 151 for(int i=0;i<this->m_num_preferences && !found;++i) {
ansond 118:c8a80adfe90d 152 if (strcmp(name,this->m_names[i]) == 0) {
ansond 118:c8a80adfe90d 153 found = true;
ansond 118:c8a80adfe90d 154 index = i;
ansond 118:c8a80adfe90d 155 }
ansond 118:c8a80adfe90d 156 }
ansond 118:c8a80adfe90d 157 }
ansond 118:c8a80adfe90d 158
ansond 118:c8a80adfe90d 159 // return the index value
ansond 118:c8a80adfe90d 160 return index;
ansond 112:1fb53d4729af 161 }
ansond 112:1fb53d4729af 162
ansond 112:1fb53d4729af 163 // get our error handler
ansond 112:1fb53d4729af 164 ErrorHandler *Preferences::logger() { return this->m_error_handler; }
ansond 112:1fb53d4729af 165
ansond 112:1fb53d4729af 166 // get our preference count
ansond 118:c8a80adfe90d 167 int Preferences::numPreferences() { return this->m_num_preferences; }
ansond 112:1fb53d4729af 168
ansond 112:1fb53d4729af 169 // min function
ansond 112:1fb53d4729af 170 int Preferences::min(int value1,int value2) {
ansond 112:1fb53d4729af 171 if (value1 < value2) return value1;
ansond 112:1fb53d4729af 172 return value2;
ansond 112:1fb53d4729af 173 }