Review comments taken on board
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user