Review comments taken on board

This commit is contained in:
jpirnay
2026-05-24 16:40:05 +02:00
parent a9c7894120
commit 9bb9739b27
3 changed files with 21 additions and 2 deletions
+5 -1
View File
@@ -507,7 +507,11 @@ bool JsonSettingsIO::loadWifi(WifiCredentialStore& store, const char* json, bool
}
const auto parseQuad = [](const std::string& s, uint8_t out[4]) -> bool {
unsigned int a = 0, b = 0, c = 0, d = 0;
if (sscanf(s.c_str(), "%u.%u.%u.%u", &a, &b, &c, &d) != 4) return false;
int consumed = 0;
// %n stores characters consumed; require it to equal full string length so we
// reject inputs with trailing garbage like "192.168.1.10xyz".
if (sscanf(s.c_str(), "%u.%u.%u.%u%n", &a, &b, &c, &d, &consumed) != 4) return false;
if (consumed < 0 || static_cast<size_t>(consumed) != s.size()) return false;
if (a > 255 || b > 255 || c > 255 || d > 255) return false;
out[0] = a;
out[1] = b;
+9 -1
View File
@@ -95,8 +95,16 @@ bool WifiCredentialStore::updateConnectionCache(const std::string& ssid, const u
const bool sameHint = (channel == cred->channel) && std::memcmp(bssid, cred->bssid, 6) == 0;
const bool sameIp = std::memcmp(ip, cred->ip, 4) == 0 && std::memcmp(gateway, cred->gateway, 4) == 0 &&
std::memcmp(mask, cred->mask, 4) == 0 && std::memcmp(dns, cred->dns, 4) == 0;
// Timestamp updates are not worth a write on their own; only persist when topology changed.
if (sameHint && sameIp) {
// Refresh the TTL only if the cache is older than half the TTL window — otherwise
// a long-running device that reconnects daily would let its 7-day TTL expire even
// though the topology hasn't changed. Half-TTL avoids an SD write on every connect.
constexpr uint32_t REFRESH_THRESHOLD_SECONDS = (7 * 24 * 60 * 60) / 2;
const int64_t elapsed = static_cast<int64_t>(cacheTimestamp) - static_cast<int64_t>(cred->cacheTimestamp);
if (cacheTimestamp != 0 && cred->cacheTimestamp != 0 && elapsed > REFRESH_THRESHOLD_SECONDS) {
cred->cacheTimestamp = cacheTimestamp;
return saveToFile();
}
return true;
}
std::memcpy(cred->bssid, bssid, 6);
@@ -549,6 +549,13 @@ void WifiSelectionActivity::checkConnectionStatus() {
LOG_DBG("WIFI", "Hint attempt did not connect (%s after %lu ms), retrying with full scan",
hintHardFail ? "hard fail" : "timeout", millis() - connectionStartTime);
hintFallbackDone = true;
// Wipe the stale cache before retrying. If the fallback succeeds, the success
// path writes a fresh cache (one extra SD write). If it fails or is interrupted,
// we won't carry a known-bad hint into the next session.
{
RenderLock lock(*this);
WIFI_STORE.clearConnectionCache(selectedSSID);
}
WiFi.disconnect(true, false);
connectionStartTime = millis();
issueWifiBegin(/*useHint=*/false);