Skip to content

Commit 78c7e68

Browse files
committed
fix(commands): reject batches that change sensor mapping and settings
The mapping and the settings are stored in separate NVS records, so a power loss between the two writes of a mixed batch left a new mapping next to old settings, despite the atomic batch contract. No client sends such a batch: the web portal and the local UI send mapping-only batches and MQTT sends one command per message. execute() now rejects a mixed batch with INVALID, which makes the compensating mapping restore unnecessary. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012JY8RZscs5W8tLQMuZB95K
1 parent 4a901d2 commit 78c7e68

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

‎src/ControllerCommandHandler.cpp‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ CommandStatus ControllerCommandHandler::execute(
121121
return CommandStatus::INVALID;
122122
}
123123

124-
// Persist everything before touching the runtime: the mapping first, then
125-
// the settings. A failed settings write restores the previous mapping so a
126-
// batch is either stored completely or not at all.
127-
std::uint8_t previousSolar[8]{};
128-
std::uint8_t previousPool[8]{};
124+
// The sensor mapping and the settings live in separate NVS records. A batch
125+
// that changes both could be left half stored by a power loss between the
126+
// two writes, so it is rejected; no client sends such a batch.
127+
if (plan.mappingChanged && plan.settingsChanged) {
128+
LOG_WARN("Controller command: sensor mapping and settings must be sent in separate batches\n");
129+
return CommandStatus::INVALID;
130+
}
131+
132+
// Persist before touching the runtime, so a failed write changes nothing.
129133
if (plan.mappingChanged) {
130-
if (!ConfigManager::loadSensorMappingCached(previousSolar, previousPool)) {
131-
LOG_ERROR("Controller command: cannot read current sensor mapping\n");
132-
return CommandStatus::PERSIST_FAILED;
133-
}
134134
if (!ConfigManager::saveSensorMapping(plan.solarAddress, plan.poolAddress)) {
135135
LOG_ERROR("Controller command: failed to persist sensor mapping\n");
136136
return CommandStatus::PERSIST_FAILED;
@@ -150,9 +150,6 @@ CommandStatus ControllerCommandHandler::execute(
150150
if (!ConfigManager::save()) {
151151
LOG_ERROR("Controller command: could not restore previous settings in NVS\n");
152152
}
153-
if (plan.mappingChanged && !ConfigManager::saveSensorMapping(previousSolar, previousPool)) {
154-
LOG_ERROR("Controller command: could not restore previous sensor mapping\n");
155-
}
156153
return CommandStatus::PERSIST_FAILED;
157154
}
158155
}

‎src/ControllerCommandHandler.hpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class ControllerCommandHandler final {
6767
/**
6868
* @brief Apply a batch of commands atomically on the owning application task.
6969
*
70-
* FACTORY_RESET must be sent alone. `result` (optional) receives the
70+
* FACTORY_RESET must be sent alone, and a batch may change either the
71+
* sensor mapping or persisted settings, not both: they are stored in two
72+
* NVS records that a power loss could leave out of step. `result`
73+
* (optional) receives the
7174
* resolved mode and the last toggled pump state.
7275
*/
7376
CommandStatus execute(const ControllerCommand *commands, std::size_t count, ControllerCommandResult *result = nullptr);

‎test/native/tests/test_controller_command_handler.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ static int test_sensor_mapping_persist_failure_leaves_runtime_unchanged() {
513513
return 0;
514514
}
515515

516-
static int test_batch_settings_failure_restores_sensor_mapping() {
517-
test_begin("ControllerCommandHandler", "settings write failure in a mapping batch restores the old mapping");
516+
static int test_mixed_mapping_and_settings_batch_is_rejected() {
517+
test_begin("ControllerCommandHandler", "a batch changing mapping and settings is rejected unchanged");
518518
HandlerFixture fixture;
519519
const std::uint8_t oldSolar[8] = {0x28, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
520520
const std::uint8_t oldPool[8] = {0x28, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
@@ -530,7 +530,7 @@ static int test_batch_settings_failure_restores_sensor_mapping() {
530530
batch[1].type = ControllerCommandType::SET_BUTTON_1_MIN;
531531
batch[1].integerValue = 1000;
532532

533-
ASSERT_EQ(executeWithFailingSave(fixture.handler, batch, 2), CommandStatus::PERSIST_FAILED);
533+
ASSERT_EQ(fixture.handler.execute(batch, 2), CommandStatus::INVALID);
534534
std::uint8_t savedSolar[8] = {0};
535535
std::uint8_t savedPool[8] = {0};
536536
ASSERT_TRUE(PoolController::ConfigManager::loadSensorMapping(savedSolar, savedPool));
@@ -563,7 +563,7 @@ int run_controller_command_handler_tests() {
563563
test_factory_reset_owns_runtime_reset_mutation, test_status_separates_invalid_rejected_and_persist_failures,
564564
test_batch_is_all_or_nothing, test_persist_failure_rolls_back_every_setting_kind, test_enter_manual_toggle_is_atomic,
565565
test_time_loss_bounds_can_move_together, test_factory_reset_keeps_relay_state_when_save_fails,
566-
test_sensor_mapping_persist_failure_leaves_runtime_unchanged, test_batch_settings_failure_restores_sensor_mapping,
566+
test_sensor_mapping_persist_failure_leaves_runtime_unchanged, test_mixed_mapping_and_settings_batch_is_rejected,
567567
test_factory_reset_failure_keeps_runtime_configuration};
568568
for (auto test : tests) {
569569
if (test() == 0) {

0 commit comments

Comments
 (0)