Merge pull request #10755 from mikeller/add_parameter_group_reset_defaults

Added resetting to defaults for individual parameter groups.
This commit is contained in:
Michael Keller 2021-06-28 11:07:17 +12:00 committed by GitHub
commit 33dd967736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 69 additions and 31 deletions

View File

@ -679,9 +679,11 @@ static void backupPgConfig(const pgRegistry_t *pg)
memcpy(pg->copy, pg->address, pg->size); memcpy(pg->copy, pg->address, pg->size);
} }
static void restorePgConfig(const pgRegistry_t *pg) static void restorePgConfig(const pgRegistry_t *pg, uint16_t notToRestoreGroupId)
{ {
if (!notToRestoreGroupId || pgN(pg) != notToRestoreGroupId) {
memcpy(pg->address, pg->copy, pg->size); memcpy(pg->address, pg->copy, pg->size);
}
} }
static void backupConfigs(void) static void backupConfigs(void)
@ -690,7 +692,6 @@ static void backupConfigs(void)
return; return;
} }
// make copies of configs to do differencing
PG_FOREACH(pg) { PG_FOREACH(pg) {
backupPgConfig(pg); backupPgConfig(pg);
} }
@ -698,14 +699,14 @@ static void backupConfigs(void)
configIsInCopy = true; configIsInCopy = true;
} }
static void restoreConfigs(void) static void restoreConfigs(uint16_t notToRestoreGroupId)
{ {
if (!configIsInCopy) { if (!configIsInCopy) {
return; return;
} }
PG_FOREACH(pg) { PG_FOREACH(pg) {
restorePgConfig(pg); restorePgConfig(pg, notToRestoreGroupId);
} }
configIsInCopy = false; configIsInCopy = false;
@ -4325,6 +4326,7 @@ bool hasCustomDefaults(void)
static void cliDefaults(const char *cmdName, char *cmdline) static void cliDefaults(const char *cmdName, char *cmdline)
{ {
bool saveConfigs = true; bool saveConfigs = true;
uint16_t parameterGroupId = 0;
#if defined(USE_CUSTOM_DEFAULTS) #if defined(USE_CUSTOM_DEFAULTS)
bool useCustomDefaults = true; bool useCustomDefaults = true;
#elif defined(USE_CUSTOM_DEFAULTS_ADDRESS) #elif defined(USE_CUSTOM_DEFAULTS_ADDRESS)
@ -4334,14 +4336,31 @@ static void cliDefaults(const char *cmdName, char *cmdline)
} }
#endif #endif
if (isEmpty(cmdline)) { char *saveptr;
} else if (strncasecmp(cmdline, "nosave", 6) == 0) { char* tok = strtok_r(cmdline, " ", &saveptr);
int index = 0;
bool expectParameterGroupId = false;
while (tok != NULL) {
if (expectParameterGroupId) {
parameterGroupId = atoi(tok);
expectParameterGroupId = false;
if (!parameterGroupId) {
cliShowParseError(cmdName);
return;
}
} else if (strcasestr(tok, "group_id")) {
expectParameterGroupId = true;
} else if (strcasestr(tok, "nosave")) {
saveConfigs = false; saveConfigs = false;
#if defined(USE_CUSTOM_DEFAULTS) #if defined(USE_CUSTOM_DEFAULTS)
} else if (strncasecmp(cmdline, "bare", 4) == 0) { } else if (strcasestr(tok, "bare")) {
useCustomDefaults = false; useCustomDefaults = false;
} else if (strncasecmp(cmdline, "show", 4) == 0) { } else if (strcasestr(tok, "show")) {
if (hasCustomDefaults()) { if (index != 0) {
cliShowParseError(cmdName);
} else if (hasCustomDefaults()) {
char *customDefaultsPtr = customDefaultsStart; char *customDefaultsPtr = customDefaultsStart;
while (customDefaultsHasNext(customDefaultsPtr)) { while (customDefaultsHasNext(customDefaultsPtr)) {
if (*customDefaultsPtr != '\n') { if (*customDefaultsPtr != '\n') {
@ -4358,12 +4377,27 @@ static void cliDefaults(const char *cmdName, char *cmdline)
return; return;
#endif #endif
} else { } else {
cliPrintError(cmdName, "INVALID OPTION"); cliShowParseError(cmdName);
return; return;
} }
index++;
tok = strtok_r(NULL, " ", &saveptr);
}
if (expectParameterGroupId) {
cliShowParseError(cmdName);
return;
}
if (parameterGroupId) {
cliPrintLinef("\r\n# resetting group %d to defaults", parameterGroupId);
backupConfigs();
} else {
cliPrintHashLine("resetting to defaults"); cliPrintHashLine("resetting to defaults");
}
resetConfig(); resetConfig();
@ -4381,6 +4415,10 @@ static void cliDefaults(const char *cmdName, char *cmdline)
} }
#endif #endif
if (parameterGroupId) {
restoreConfigs(parameterGroupId);
}
if (saveConfigs && tryPrepareSave(cmdName)) { if (saveConfigs && tryPrepareSave(cmdName)) {
writeUnmodifiedConfigToEEPROM(); writeUnmodifiedConfigToEEPROM();
@ -4442,7 +4480,7 @@ STATIC_UNIT_TESTED void cliGet(const char *cmdName, char *cmdline)
} }
} }
restoreConfigs(); restoreConfigs(0);
pidProfileIndexToUse = CURRENT_PROFILE_INDEX; pidProfileIndexToUse = CURRENT_PROFILE_INDEX;
rateProfileIndexToUse = CURRENT_PROFILE_INDEX; rateProfileIndexToUse = CURRENT_PROFILE_INDEX;
@ -6331,7 +6369,7 @@ static void printConfig(const char *cmdName, char *cmdline, bool doDiff)
#endif #endif
// restore configs from copies // restore configs from copies
restoreConfigs(); restoreConfigs(0);
} }
static void cliDump(const char *cmdName, char *cmdline) static void cliDump(const char *cmdName, char *cmdline)
@ -6436,9 +6474,9 @@ const clicmd_t cmdTable[] = {
CLI_COMMAND_DEF("color", "configure colors", NULL, cliColor), CLI_COMMAND_DEF("color", "configure colors", NULL, cliColor),
#endif #endif
#if defined(USE_CUSTOM_DEFAULTS) #if defined(USE_CUSTOM_DEFAULTS)
CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "[nosave|bare|show]", cliDefaults), CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "{show} {nosave} {bare} {group_id <id>}", cliDefaults),
#else #else
CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "[nosave|show]", cliDefaults), CLI_COMMAND_DEF("defaults", "reset to defaults and reboot", "{nosave}", cliDefaults),
#endif #endif
CLI_COMMAND_DEF("diff", "list configuration changes from default", "[master|profile|rates|hardware|all] {defaults|bare}", cliDiff), CLI_COMMAND_DEF("diff", "list configuration changes from default", "[master|profile|rates|hardware|all] {defaults|bare}", cliDiff),
#ifdef USE_RESOURCE_MGMT #ifdef USE_RESOURCE_MGMT