Remember and use last known mac address for immediate display

This commit is contained in:
jpirnay
2026-04-01 11:28:20 +02:00
parent aa085425af
commit 62fd82a5bf
4 changed files with 56 additions and 5 deletions
+2
View File
@@ -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<JsonArray>();
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<JsonArray>();
+10
View File
@@ -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");
}
+5
View File
@@ -24,6 +24,7 @@ class WifiCredentialStore {
static WifiCredentialStore instance;
std::vector<WifiCredential> 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();
};
@@ -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();