[BugFix] settings.c CRC calculation skipped F_CUSTOM_SETTING

F_CUSTOM_SETTING write to the cfg file they do not however
have a value in the settings struct to check against for the CRC

at best you get the is_changed value but as Chris_S pointed out this
works the first time BUT unless you are changing from DEFAULT to
'is_changed' or back there is nothing different in the CRC and thus
further changes to custom settings do not get saved unless other
settings change the CRC.

Instead we will ask each of the custom settings to write their output
and run the CRC on those values as well and allowing us to check for changes

Change-Id: Ib3e7b621d676be5eb5ddc9eea00c4805292773fb
This commit is contained in:
William Wilgus 2025-02-15 12:35:07 -05:00
parent 5471f58fb1
commit 7912afa32f

View file

@ -154,6 +154,30 @@ const char* setting_get_cfgvals(const struct settings_list *setting)
return NULL; return NULL;
} }
/* calculates and stores crc of settings, returns true if settings have changed */
static bool settings_crc_changed(void)
{
char value[MAX_PATH];
uint32_t custom_crc = 0xFFFFFFFF;
for(int i=0; i<nb_settings; i++)
{
const struct settings_list *setting = &settings[i];
if (!(setting->flags & F_CUSTOM_SETTING))
continue;
cfg_to_string(setting, value, sizeof(value));
custom_crc = crc_32(value, strlen(value), custom_crc);
}
uint32_t crc = crc_32(&global_settings, sizeof(global_settings), custom_crc);
if (crc != user_settings_crc)
{
user_settings_crc = crc;
return true;
}
return false;
}
/** Reading from a config file **/ /** Reading from a config file **/
/* /*
* load settings from disk * load settings from disk
@ -174,8 +198,7 @@ void settings_load(void)
settings_load_config(FIXEDSETTINGSFILE, false); settings_load_config(FIXEDSETTINGSFILE, false);
/* set initial CRC value - settings_save checks, if changed writes to disk */ /* set initial CRC value - settings_save checks, if changed writes to disk */
user_settings_crc = crc_32(&global_settings, settings_crc_changed();
sizeof(global_settings), 0xFFFFFFFF);
} }
bool cfg_string_to_int(const struct settings_list *setting, int* out, const char* str) bool cfg_string_to_int(const struct settings_list *setting, int* out, const char* str)
@ -615,13 +638,11 @@ static void flush_global_status_callback(void)
static void flush_config_block_callback(void) static void flush_config_block_callback(void)
{ {
uint32_t crc = crc_32(&global_settings, sizeof(global_settings), 0xFFFFFFFF); if (settings_crc_changed())
if (user_settings_crc != crc)
{ {
DEBUGF("Writing changed user_settings to disk\n"); DEBUGF("Writing changed user_settings to disk\n");
logf("Writing changed user_settings to disk"); logf("Writing changed user_settings to disk");
user_settings_crc = crc; /* update immediately in case we yield */
if (!settings_write_config(CONFIGFILE_TEMP, SETTINGS_SAVE_CHANGED)) if (!settings_write_config(CONFIGFILE_TEMP, SETTINGS_SAVE_CHANGED))
{ {
user_settings_crc = 0; user_settings_crc = 0;