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