#include "settings.h" #include "cfg_store.h" #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" // The writer mutex spans validation against the current revision, NVS and // publication. The radio never takes it. The spinlock only protects small // in-memory snapshots; no logging, SPI, NVS or blocking calls run underneath it. static SemaphoreHandle_t s_writer; static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED; static settings_snapshot_t s_desired; static bool valid_change(const settings_change_t *change) { if (!change) return false; int32_t v = change->value; switch (change->key) { case SETTINGS_REPEAT_DELAY_MS: return v >= 20 && v <= 1000; case SETTINGS_TX_POWER: return v >= -9 && v <= 2; case SETTINGS_REPEAT_ENABLED: case SETTINGS_VERBOSE: case SETTINGS_FEM_RX_CSD: return v == 0 || v == 1; case SETTINGS_PREAMBLE_BITS: return v == 0 || v == 8 || v == 16 || v == 24 || v == 32; case SETTINGS_DUTY_BUDGET_PPM: return v >= 0 && v <= 100000; case SETTINGS_TEST_LFIELD: return v >= 19 && v <= 200; case SETTINGS_BEACON_PERIOD_MS: return v == 0 || (v >= 250 && v <= 600000); case SETTINGS_WHITELIST: if (change->whitelist_len > 8) return false; for (unsigned i = 0; i < change->whitelist_len; i++) { for (unsigned j = 0; j < i; j++) { if (change->whitelist[i] == change->whitelist[j]) return false; } } return true; default: return false; } } void settings_init(void) { s_writer = xSemaphoreCreateMutex(); configASSERT(s_writer); repeater_cfg_t initial = g_cfg; uint32_t value; bool restored = false; if (cfg_store_load_u32(CFG_KEY_DUTY_PPM, &value)) { if (value <= 100000) { initial.duty_budget_ppm = value; restored = true; } else { printf("{\"event\":\"cfg_restore_invalid\",\"key\":\"duty_budget_ppm\",\"value\":%" PRIu32 "}\n", value); } } if (cfg_store_load_u32(CFG_KEY_DELAY_MS, &value)) { if (value >= 20 && value <= 1000) { initial.repeat_delay_ms = value; restored = true; } else { printf("{\"event\":\"cfg_restore_invalid\",\"key\":\"repeat_delay_ms\",\"value\":%" PRIu32 "}\n", value); } } // No tasks have started: initialize the legacy display snapshot as well. g_cfg = initial; s_desired.values = initial; s_desired.revision = 1; s_desired.applied_revision = 0; if (restored) { printf("{\"event\":\"cfg_restore\",\"duty_budget_ppm\":%" PRIu32 "," "\"repeat_delay_ms\":%" PRIu32 "}\n", initial.duty_budget_ppm, initial.repeat_delay_ms); } fflush(stdout); } void settings_get_snapshot(settings_snapshot_t *out) { if (!out) return; portENTER_CRITICAL(&s_lock); *out = s_desired; portEXIT_CRITICAL(&s_lock); } settings_result_t settings_update(const settings_change_t *change, uint32_t expected_revision, settings_snapshot_t *out) { if (!valid_change(change)) { settings_get_snapshot(out); return SETTINGS_INVALID; } if (!s_writer || xSemaphoreTake(s_writer, pdMS_TO_TICKS(1000)) != pdTRUE) { settings_get_snapshot(out); return SETTINGS_BUSY; } settings_snapshot_t next; settings_get_snapshot(&next); settings_result_t result = SETTINGS_OK; if (expected_revision && expected_revision != next.revision) { result = SETTINGS_CONFLICT; goto done; } // Never wrap into revision 0 (unconditional) or reuse a stale revision. if (next.revision == UINT32_MAX) { result = SETTINGS_BUSY; goto done; } switch (change->key) { case SETTINGS_REPEAT_DELAY_MS: next.values.repeat_delay_ms = (uint32_t)change->value; break; case SETTINGS_TX_POWER: next.values.tx_power_dbm = (int8_t)change->value; break; case SETTINGS_REPEAT_ENABLED: next.values.repeat_enabled = change->value != 0; break; case SETTINGS_VERBOSE: next.values.verbose = change->value != 0; break; case SETTINGS_FEM_RX_CSD: next.values.fem_rx_csd = change->value != 0; break; case SETTINGS_PREAMBLE_BITS: next.values.preamble_bits = (uint8_t)change->value; break; case SETTINGS_DUTY_BUDGET_PPM: next.values.duty_budget_ppm = (uint32_t)change->value; break; case SETTINGS_TEST_LFIELD: next.values.test_lfield = (uint8_t)change->value; break; case SETTINGS_BEACON_PERIOD_MS: next.values.beacon_period_ms = (uint32_t)change->value; break; case SETTINGS_WHITELIST: memset(next.values.whitelist, 0, sizeof(next.values.whitelist)); memcpy(next.values.whitelist, change->whitelist, change->whitelist_len * sizeof(change->whitelist[0])); next.values.whitelist_len = change->whitelist_len; break; default: result = SETTINGS_INVALID; goto done; } const char *store_key = change->key == SETTINGS_REPEAT_DELAY_MS ? CFG_KEY_DELAY_MS : change->key == SETTINGS_DUTY_BUDGET_PPM ? CFG_KEY_DUTY_PPM : NULL; if (store_key && !cfg_store_save_u32(store_key, (uint32_t)change->value)) { result = SETTINGS_STORAGE_ERROR; goto done; } portENTER_CRITICAL(&s_lock); s_desired.values = next.values; s_desired.revision = next.revision + 1; // applied_revision may have advanced while NVS was committing; retain it. portEXIT_CRITICAL(&s_lock); done: settings_get_snapshot(out); xSemaphoreGive(s_writer); return result; } bool settings_radio_pending(void) { portENTER_CRITICAL(&s_lock); bool pending = s_desired.revision != s_desired.applied_revision; portEXIT_CRITICAL(&s_lock); return pending; } bool settings_radio_apply(repeater_cfg_t *active) { portENTER_CRITICAL(&s_lock); bool pending = s_desired.revision != s_desired.applied_revision; if (pending) { *active = s_desired.values; // This acknowledges the coherent copy, not successful hardware/RF operation. s_desired.applied_revision = s_desired.revision; } portEXIT_CRITICAL(&s_lock); return pending; }