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:
Fri Sep 26 05:16:07 2014 +0000
Revision:
192:54b758a8eaaa
Parent:
184:b73d6c976063
updates to new logger name

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