Split system settings to separate config file

This commit is contained in:
2017-12-28 18:36:13 +01:00
parent 0af90eccbf
commit 37f712f054
8 changed files with 101 additions and 48 deletions
+55 -24
View File
@@ -209,33 +209,63 @@ static void savebuf_flush(PayloadBuilder *pb, bool final)
fls_printf("\r\n");
}
/**
* Write system settings to INI (without section)
*/
void settings_build_ini(IniWriter *iw)
// ---------------------------------------------------------------
static void ini_preamble(IniWriter *iw, const char *filename)
{
// File header
iw_hdr_comment(iw, "CONFIG.INI");
iw_hdr_comment(iw, filename);
iw_hdr_comment(iw, "GEX v%s on %s", GEX_VERSION, GEX_PLATFORM);
iw_hdr_comment(iw, "built %s at %s", __DATE__, __TIME__);
iw_cmt_newline(iw);
iw_comment(iw, "Overwrite this file to change settings.");
iw_comment(iw, "Close the LOCK jumper to save them to Flash.");
}
systemsettings_build_ini(iw);
/**
* Write system settings to INI (without section)
*/
void settings_build_units_ini(IniWriter *iw)
{
ini_preamble(iw, "UNITS.INI");
ureg_build_ini(iw);
}
/**
* Write system settings to INI (without section)
*/
void settings_build_system_ini(IniWriter *iw)
{
ini_preamble(iw, "SYSTEM.INI");
systemsettings_build_ini(iw);
}
uint32_t settings_get_units_ini_len(void)
{
// this writer is configured to skip everything, so each written byte will decrement the skip count
IniWriter iw = iw_init(NULL, 0xFFFFFFFF, 1); // count is never used, we use 1 because 0 means we're full
settings_build_units_ini(&iw);
// now we just check how many bytes were skipped
return 0xFFFFFFFF - iw.skip;
}
uint32_t settings_get_system_ini_len(void)
{
// same as above
IniWriter iw = iw_init(NULL, 0xFFFFFFFF, 1);
settings_build_system_ini(&iw);
return 0xFFFFFFFF - iw.skip;
}
// ---------------------------------------------------------------
void settings_load_ini_begin(void)
{
SystemSettings.modified = true;
// load defaults
systemsettings_loadDefaults();
ureg_remove_all_units();
SystemSettings.pristine = true;
}
void settings_load_ini_key(const char *restrict section, const char *restrict key, const char *restrict value)
@@ -244,25 +274,35 @@ void settings_load_ini_key(const char *restrict section, const char *restrict ke
static char namebuf[INI_KEY_MAX];
if (streq(section, "SYSTEM")) {
if (SystemSettings.pristine) {
SystemSettings.pristine = false;
systemsettings_loadDefaults();
}
// system is always at the top
systemsettings_load_ini(key, value);
}
else if (streq(section, "UNITS")) {
if (SystemSettings.pristine) {
SystemSettings.pristine = false;
ureg_remove_all_units();
}
// this will always come before individual units config
// install or tear down units as described by the config
ureg_instantiate_by_ini(key, value);
} else {
}
else {
// not a standard section, may be some unit config
// all unit sections contain the colon character [TYPE:NAME]
// all unit sections contain the colon character [TYPE:NAME@CALLSIGN]
const char *nameptr = strchr(section, ':');
const char *csptr = strchr(section, '@');
dbg("cs = %s", csptr);
if (nameptr && csptr) {
strncpy(namebuf, nameptr+1, csptr - nameptr - 1);
uint8_t cs = (uint8_t) avr_atoi(csptr + 1);
dbg("cs is %d", cs);
dbg("name is %s", namebuf);
ureg_load_unit_ini_key(namebuf, key, value, cs);
} else {
dbg("! Bad config key: [%s] %s = %s", section, key, value);
@@ -276,12 +316,3 @@ void settings_load_ini_end(void)
dbg("Some units failed to init!!");
}
}
uint32_t settings_get_ini_len(void)
{
// this writer is configured to skip everything, so each written byte will decrement the skip count
IniWriter iw = iw_init(NULL, 0xFFFFFFFF, 1);
settings_build_ini(&iw);
// now we just check how many bytes were skipped
return 0xFFFFFFFF - iw.skip;
}
+17 -4
View File
@@ -51,16 +51,29 @@ void settings_load_ini_key(const char *restrict section, const char *restrict ke
void settings_load_ini_end(void);
/**
* Write all settings to a iniwriter
* Write UNITS.INI to a iniwriter
* @param iw - writer handle
*/
void settings_build_ini(IniWriter *iw);
void settings_build_units_ini(IniWriter *iw);
/**
* Get total settings len (caution: this is expensive, works by dummy-printing everything)
* Get UNITS.INI len (expensive, uses dummy read)
*
* @return bytes
*/
uint32_t settings_get_ini_len(void);
uint32_t settings_get_units_ini_len(void);
/**
* Write SYSTEM.INI to iniwriter
* @param iw - writer handle
*/
void settings_build_system_ini(IniWriter *iw);
/**
* Get SYSTEM.INI len (expensive, uses dummy read)
*
* @return bytes
*/
uint32_t settings_get_system_ini_len(void);
#endif //GEX_SETTINGS_H
+1
View File
@@ -17,6 +17,7 @@ struct system_settings {
// Support flags put here for scoping, but not atcually part of the persistent settings
volatile bool editable; //!< True if we booted with the LOCK jumper removed
volatile bool modified; //!< True if user did any change to the settings (checked when the LOCK jumper is replaced)
volatile bool pristine; //!< Marks unknown state before we reach first section marker that determines what file it is
};
extern struct system_settings SystemSettings;
+4
View File
@@ -477,6 +477,8 @@ void ureg_build_ini(IniWriter *iw)
count++;
}
li = li->next;
if (iw->count == 0) return; // avoid printing discarded tail
}
re = re->next;
iw_newline(iw);
@@ -487,6 +489,8 @@ void ureg_build_ini(IniWriter *iw)
while (li != NULL) {
export_unit_do(li, iw);
li = li->next;
if (iw->count == 0) return; // avoid printing discarded tail
}
}