Handle WiFi config save failures safely
This commit is contained in:
@@ -567,10 +567,21 @@ static void handle_wifi_post() {
|
|||||||
cfg.ntp_server_2 = server.arg("ntp2");
|
cfg.ntp_server_2 = server.arg("ntp2");
|
||||||
}
|
}
|
||||||
cfg.valid = true;
|
cfg.valid = true;
|
||||||
|
if (!wifi_save_config(cfg)) {
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
Serial.println("wifi_cfg: save failed, reboot cancelled");
|
||||||
|
}
|
||||||
|
String html = html_header("WiFi/MQTT Config");
|
||||||
|
html += "<p style='color:#b00020;'>Save failed. Configuration was not persisted and reboot was cancelled.</p>";
|
||||||
|
html += "<p><a href='/wifi'>Back to config</a></p>";
|
||||||
|
html += html_footer();
|
||||||
|
server.send(500, "text/html", html);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
g_config = cfg;
|
g_config = cfg;
|
||||||
g_web_user = cfg.web_user;
|
g_web_user = cfg.web_user;
|
||||||
g_web_pass = cfg.web_pass;
|
g_web_pass = cfg.web_pass;
|
||||||
wifi_save_config(cfg);
|
|
||||||
server.send(200, "text/html", "<html><body>Saved. Rebooting...</body></html>");
|
server.send(200, "text/html", "<html><body>Saved. Rebooting...</body></html>");
|
||||||
delay(1000);
|
delay(1000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
|||||||
@@ -5,6 +5,59 @@
|
|||||||
|
|
||||||
static Preferences prefs;
|
static Preferences prefs;
|
||||||
|
|
||||||
|
static bool wifi_log_save_failure(const char *key, const char *reason) {
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
Serial.printf("wifi_cfg: save failed key=%s reason=%s\n", key, reason);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool wifi_write_string_pref(const char *key, const String &value) {
|
||||||
|
size_t written = prefs.putString(key, value);
|
||||||
|
if (written != value.length()) {
|
||||||
|
return wifi_log_save_failure(key, "write_short");
|
||||||
|
}
|
||||||
|
if (!prefs.isKey(key)) {
|
||||||
|
return wifi_log_save_failure(key, "missing_key");
|
||||||
|
}
|
||||||
|
String readback = prefs.getString(key, "");
|
||||||
|
if (readback != value) {
|
||||||
|
return wifi_log_save_failure(key, "verify_mismatch");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool wifi_write_bool_pref(const char *key, bool value) {
|
||||||
|
size_t written = prefs.putBool(key, value);
|
||||||
|
if (written != sizeof(uint8_t)) {
|
||||||
|
return wifi_log_save_failure(key, "write_short");
|
||||||
|
}
|
||||||
|
if (!prefs.isKey(key)) {
|
||||||
|
return wifi_log_save_failure(key, "missing_key");
|
||||||
|
}
|
||||||
|
bool readback = prefs.getBool(key, !value);
|
||||||
|
if (readback != value) {
|
||||||
|
return wifi_log_save_failure(key, "verify_mismatch");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool wifi_write_ushort_pref(const char *key, uint16_t value) {
|
||||||
|
size_t written = prefs.putUShort(key, value);
|
||||||
|
if (written != sizeof(uint16_t)) {
|
||||||
|
return wifi_log_save_failure(key, "write_short");
|
||||||
|
}
|
||||||
|
if (!prefs.isKey(key)) {
|
||||||
|
return wifi_log_save_failure(key, "missing_key");
|
||||||
|
}
|
||||||
|
uint16_t fallback = value == static_cast<uint16_t>(0xFFFF) ? 0 : static_cast<uint16_t>(0xFFFF);
|
||||||
|
uint16_t readback = prefs.getUShort(key, fallback);
|
||||||
|
if (readback != value) {
|
||||||
|
return wifi_log_save_failure(key, "verify_mismatch");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void wifi_manager_init() {
|
void wifi_manager_init() {
|
||||||
prefs.begin("dd3cfg", false);
|
prefs.begin("dd3cfg", false);
|
||||||
}
|
}
|
||||||
@@ -28,17 +81,39 @@ bool wifi_load_config(WifiMqttConfig &config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool wifi_save_config(const WifiMqttConfig &config) {
|
bool wifi_save_config(const WifiMqttConfig &config) {
|
||||||
prefs.putBool("valid", true);
|
if (!wifi_write_bool_pref("valid", true)) {
|
||||||
prefs.putString("ssid", config.ssid);
|
return false;
|
||||||
prefs.putString("pass", config.password);
|
}
|
||||||
prefs.putString("mqhost", config.mqtt_host);
|
if (!wifi_write_string_pref("ssid", config.ssid)) {
|
||||||
prefs.putUShort("mqport", config.mqtt_port);
|
return false;
|
||||||
prefs.putString("mquser", config.mqtt_user);
|
}
|
||||||
prefs.putString("mqpass", config.mqtt_pass);
|
if (!wifi_write_string_pref("pass", config.password)) {
|
||||||
prefs.putString("ntp1", config.ntp_server_1);
|
return false;
|
||||||
prefs.putString("ntp2", config.ntp_server_2);
|
}
|
||||||
prefs.putString("webuser", config.web_user);
|
if (!wifi_write_string_pref("mqhost", config.mqtt_host)) {
|
||||||
prefs.putString("webpass", config.web_pass);
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_ushort_pref("mqport", config.mqtt_port)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("mquser", config.mqtt_user)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("mqpass", config.mqtt_pass)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("ntp1", config.ntp_server_1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("ntp2", config.ntp_server_2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("webuser", config.web_user)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!wifi_write_string_pref("webpass", config.web_pass)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user