Skip to content

Commit 130c11b

Browse files
committed
fix(config): persist mode key checked and keep legacy mapping keys
The legacy opmode key that boot reads after the settings record is now written and checked before the command is reported as applied, with rollback on failure. Sensor mapping saves keep the legacy solar_adr and pool_adr keys in step with the combined record so a firmware rollback still finds the mapping. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012JY8RZscs5W8tLQMuZB95K
1 parent 29df77f commit 130c11b

4 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎src/ConfigManager.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,11 @@ bool ConfigManager::saveSensorMapping(const uint8_t solarAddr[8], const uint8_t
358358
memcpy(record + 8, poolAddr, 8);
359359
const bool written = prefs.putBytes(kSensorMappingKey, record, sizeof(record)) == sizeof(record);
360360
if (written) {
361-
// The record takes precedence; dropping the legacy keys only tidies up.
362-
prefs.remove("solar_adr");
363-
prefs.remove("pool_adr");
361+
// The record takes precedence. The legacy per-role keys stay in step so a
362+
// rollback to firmware that only reads them still sees the current pair.
363+
// A failed legacy write is harmless here because the record wins.
364+
prefs.putBytes("solar_adr", solarAddr, 8);
365+
prefs.putBytes("pool_adr", poolAddr, 8);
364366
}
365367
prefs.end();
366368
if (!written) {

‎src/ControllerCommandHandler.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ bool persistTimer(const TimerSetting &timer) {
6868
return ok;
6969
}
7070

71+
/** @brief Writes the legacy mode key that boot reads back after the settings record. */
72+
bool persistMode(const String &mode) {
73+
return StateManager::saveString("opmode", mode);
74+
}
75+
7176
bool isValidTime(const ControllerCommand &command) {
7277
return command.hour <= 23 && command.minute <= 59;
7378
}
@@ -157,6 +162,17 @@ CommandStatus ControllerCommandHandler::execute(
157162
}
158163
return CommandStatus::PERSIST_FAILED;
159164
}
165+
const String previousMode = dependencies_.operationMode.getMode();
166+
if (plan.modeChanged && !persistMode(toString(plan.mode))) {
167+
LOG_ERROR("Controller command: failed to persist operation mode\n");
168+
if (!persistMode(previousMode)) {
169+
LOG_ERROR("Controller command: could not restore the previous mode in NVS\n");
170+
}
171+
if (plan.timerChanged && !persistTimer(previousTimer)) {
172+
LOG_ERROR("Controller command: could not restore the previous timer in NVS\n");
173+
}
174+
return CommandStatus::PERSIST_FAILED;
175+
}
160176
if (plan.settingsChanged) {
161177
const Settings previousSettings = liveSettings;
162178
const NtpSettings previousNtp = liveNtp;
@@ -174,6 +190,9 @@ CommandStatus ControllerCommandHandler::execute(
174190
if (plan.timerChanged && !persistTimer(previousTimer)) {
175191
LOG_ERROR("Controller command: could not restore the previous timer in NVS\n");
176192
}
193+
if (plan.modeChanged && !persistMode(previousMode)) {
194+
LOG_ERROR("Controller command: could not restore the previous mode in NVS\n");
195+
}
177196
return CommandStatus::PERSIST_FAILED;
178197
}
179198
}

‎test/native/config_persistence/test_config_persistence.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void test_mapping_is_written_as_one_record() {
124124
}
125125

126126
void test_legacy_mapping_keys_are_migrated() {
127-
section("legacy per-role keys are read and replaced by the record");
127+
section("legacy per-role keys are read and kept in step with the record");
128128
resetStore();
129129
{
130130
Preferences prefs;
@@ -143,8 +143,12 @@ void test_legacy_mapping_keys_are_migrated() {
143143
EXPECT(ConfigManager::saveSensorMapping(kSolarB, kPoolB));
144144
Preferences prefs;
145145
prefs.begin("ds18b20", true);
146-
EXPECT(!prefs.isKey("solar_adr"));
147-
EXPECT(!prefs.isKey("pool_adr"));
146+
uint8_t legacy[8];
147+
// Legacy keys mirror the record so a firmware rollback keeps the mapping.
148+
EXPECT(prefs.getBytes("solar_adr", legacy, 8) == 8);
149+
EXPECT(memcmp(legacy, kSolarB, 8) == 0);
150+
EXPECT(prefs.getBytes("pool_adr", legacy, 8) == 8);
151+
EXPECT(memcmp(legacy, kPoolB, 8) == 0);
148152
prefs.end();
149153
EXPECT(ConfigManager::loadSensorMapping(solar, pool));
150154
EXPECT(memcmp(solar, kSolarB, 8) == 0);

‎test/native/tests/test_controller_command_handler.cpp‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,25 @@ static int test_timer_persist_failure_is_reported_and_keeps_runtime() {
506506
return 0;
507507
}
508508

509+
static int test_mode_persist_failure_is_reported_and_keeps_runtime() {
510+
test_begin("ControllerCommandHandler", "a failed mode write reports PERSIST_FAILED and keeps the previous mode");
511+
HandlerFixture fixture;
512+
const String before = fixture.operation.getMode();
513+
514+
ControllerCommand command{};
515+
command.type = ControllerCommandType::SET_MODE;
516+
command.mode = before == "manual" ? OperationMode::AUTO : OperationMode::MANUAL;
517+
Preferences::s_failingPutKeys.insert("opmode");
518+
const CommandStatus failed = fixture.handler.execute(command);
519+
Preferences::s_failingPutKeys.clear();
520+
ASSERT_EQ(failed, CommandStatus::PERSIST_FAILED);
521+
ASSERT_TRUE(fixture.operation.getMode() == before);
522+
523+
ASSERT_EQ(fixture.handler.execute(command), CommandStatus::APPLIED);
524+
ASSERT_TRUE(fixture.operation.getMode() != before);
525+
return 0;
526+
}
527+
509528
static int test_factory_reset_keeps_relay_state_when_save_fails() {
510529
test_begin("ControllerCommandHandler", "factory reset clears relay/mode NVS only after the defaults were saved");
511530
HandlerFixture fixture;
@@ -608,9 +627,9 @@ int run_controller_command_handler_tests() {
608627
test_factory_reset_owns_runtime_reset_mutation, test_status_separates_invalid_rejected_and_persist_failures,
609628
test_batch_is_all_or_nothing, test_persist_failure_rolls_back_every_setting_kind, test_enter_manual_toggle_is_atomic,
610629
test_time_loss_bounds_can_move_together, test_time_loss_runtime_follows_validated_pair_in_any_order,
611-
test_timer_persist_failure_is_reported_and_keeps_runtime, test_factory_reset_keeps_relay_state_when_save_fails,
612-
test_sensor_mapping_persist_failure_leaves_runtime_unchanged, test_mixed_mapping_and_settings_batch_is_rejected,
613-
test_factory_reset_failure_keeps_runtime_configuration};
630+
test_timer_persist_failure_is_reported_and_keeps_runtime, test_mode_persist_failure_is_reported_and_keeps_runtime,
631+
test_factory_reset_keeps_relay_state_when_save_fails, test_sensor_mapping_persist_failure_leaves_runtime_unchanged,
632+
test_mixed_mapping_and_settings_batch_is_rejected, test_factory_reset_failure_keeps_runtime_configuration};
614633
for (auto test : tests) {
615634
if (test() == 0) {
616635
++passed;

0 commit comments

Comments
 (0)