From 62fd82a5bf719f593d6072d4b18de795a6c5a9bf Mon Sep 17 00:00:00 2001 From: jpirnay Date: Wed, 1 Apr 2026 11:28:20 +0200 Subject: [PATCH] Remember and use last known mac address for immediate display --- src/JsonSettingsIO.cpp | 2 + src/WifiCredentialStore.cpp | 10 +++++ src/WifiCredentialStore.h | 5 +++ .../network/WifiSelectionActivity.cpp | 44 ++++++++++++++++--- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/JsonSettingsIO.cpp b/src/JsonSettingsIO.cpp index a0458043..400e08c0 100644 --- a/src/JsonSettingsIO.cpp +++ b/src/JsonSettingsIO.cpp @@ -248,6 +248,7 @@ bool JsonSettingsIO::loadKOReader(KOReaderCredentialStore& store, const char* js bool JsonSettingsIO::saveWifi(const WifiCredentialStore& store, const char* path) { JsonDocument doc; doc["lastConnectedSsid"] = store.getLastConnectedSsid(); + doc["lastKnownMacAddress"] = store.getLastKnownMacAddress(); JsonArray arr = doc["credentials"].to(); for (const auto& cred : store.getCredentials()) { @@ -271,6 +272,7 @@ bool JsonSettingsIO::loadWifi(WifiCredentialStore& store, const char* json, bool } store.lastConnectedSsid = doc["lastConnectedSsid"] | std::string(""); + store.lastKnownMacAddress = doc["lastKnownMacAddress"] | std::string(""); store.credentials.clear(); JsonArray arr = doc["credentials"].as(); diff --git a/src/WifiCredentialStore.cpp b/src/WifiCredentialStore.cpp index a95be314..9eb4f14d 100644 --- a/src/WifiCredentialStore.cpp +++ b/src/WifiCredentialStore.cpp @@ -168,9 +168,19 @@ void WifiCredentialStore::clearLastConnectedSsid() { } } +void WifiCredentialStore::setLastKnownMacAddress(const std::string& mac) { + if (lastKnownMacAddress != mac) { + lastKnownMacAddress = mac; + saveToFile(); + } +} + +const std::string& WifiCredentialStore::getLastKnownMacAddress() const { return lastKnownMacAddress; } + void WifiCredentialStore::clearAll() { credentials.clear(); lastConnectedSsid.clear(); + lastKnownMacAddress.clear(); saveToFile(); LOG_DBG("WCS", "Cleared all WiFi credentials"); } diff --git a/src/WifiCredentialStore.h b/src/WifiCredentialStore.h index 46650954..000feae3 100644 --- a/src/WifiCredentialStore.h +++ b/src/WifiCredentialStore.h @@ -24,6 +24,7 @@ class WifiCredentialStore { static WifiCredentialStore instance; std::vector credentials; std::string lastConnectedSsid; + std::string lastKnownMacAddress; static constexpr size_t MAX_NETWORKS = 8; @@ -63,6 +64,10 @@ class WifiCredentialStore { const std::string& getLastConnectedSsid() const; void clearLastConnectedSsid(); + // Last known device MAC (formatted as AA-BB-CC-DD-EE-FF for instant UI display) + void setLastKnownMacAddress(const std::string& mac); + const std::string& getLastKnownMacAddress() const; + // Clear all credentials void clearAll(); }; diff --git a/src/activities/network/WifiSelectionActivity.cpp b/src/activities/network/WifiSelectionActivity.cpp index 85650df9..2b05f9db 100644 --- a/src/activities/network/WifiSelectionActivity.cpp +++ b/src/activities/network/WifiSelectionActivity.cpp @@ -13,6 +13,27 @@ #include "components/UITheme.h" #include "fontIds.h" +namespace { + +bool isLikelyValidMac(const uint8_t mac[6]) { + bool allZero = true; + bool allFF = true; + for (int i = 0; i < 6; i++) { + allZero = allZero && (mac[i] == 0x00); + allFF = allFF && (mac[i] == 0xFF); + } + return !allZero && !allFF; +} + +std::string formatMacLabel(const uint8_t mac[6]) { + char macStr[64]; + snprintf(macStr, sizeof(macStr), "%s %02x-%02x-%02x-%02x-%02x-%02x", tr(STR_MAC_ADDRESS), mac[0], mac[1], mac[2], + mac[3], mac[4], mac[5]); + return std::string(macStr); +} + +} // namespace + void WifiSelectionActivity::onEnter() { Activity::onEnter(); @@ -23,6 +44,13 @@ void WifiSelectionActivity::onEnter() { WIFI_STORE.loadFromFile(); } + // Show persisted MAC immediately so UI doesn't briefly display a bogus value. + if (!WIFI_STORE.getLastKnownMacAddress().empty()) { + cachedMacAddress = std::string(tr(STR_MAC_ADDRESS)) + " " + WIFI_STORE.getLastKnownMacAddress(); + } else { + cachedMacAddress = std::string(tr(STR_MAC_ADDRESS)) + " --"; + } + // Reset state selectedNetworkIndex = 0; networks.clear(); @@ -36,13 +64,19 @@ void WifiSelectionActivity::onEnter() { forgetPromptSelection = 0; autoConnecting = false; - // Cache MAC address for display + // Refresh displayed MAC from live hardware value when valid. uint8_t mac[6]; WiFi.macAddress(mac); - char macStr[64]; - snprintf(macStr, sizeof(macStr), "%s %02x-%02x-%02x-%02x-%02x-%02x", tr(STR_MAC_ADDRESS), mac[0], mac[1], mac[2], - mac[3], mac[4], mac[5]); - cachedMacAddress = std::string(macStr); + if (isLikelyValidMac(mac)) { + cachedMacAddress = formatMacLabel(mac); + char persistedMac[18]; + snprintf(persistedMac, sizeof(persistedMac), "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], + mac[4], mac[5]); + if (WIFI_STORE.getLastKnownMacAddress() != persistedMac) { + RenderLock lock(*this); + WIFI_STORE.setLastKnownMacAddress(persistedMac); + } + } // Trigger first update to show scanning message requestUpdate();