Merge branch 'chore-display-last-known-mac' of https://github.com/jpirnay/crosspoint-reader into mybuild
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <ObfuscationUtils.h>
|
#include <ObfuscationUtils.h>
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -272,7 +273,33 @@ bool JsonSettingsIO::loadWifi(WifiCredentialStore& store, const char* json, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
store.lastConnectedSsid = doc["lastConnectedSsid"] | std::string("");
|
store.lastConnectedSsid = doc["lastConnectedSsid"] | std::string("");
|
||||||
|
|
||||||
|
const auto isValidDashedMac = [](const std::string& value) -> bool {
|
||||||
|
if (value.empty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (value.size() != 17) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < value.size(); i++) {
|
||||||
|
if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14) {
|
||||||
|
if (value[i] != '-') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (!std::isxdigit(static_cast<unsigned char>(value[i]))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
store.lastKnownMacAddress = doc["lastKnownMacAddress"] | std::string("");
|
store.lastKnownMacAddress = doc["lastKnownMacAddress"] | std::string("");
|
||||||
|
if (!isValidDashedMac(store.lastKnownMacAddress)) {
|
||||||
|
store.lastKnownMacAddress.clear();
|
||||||
|
if (needsResave) {
|
||||||
|
*needsResave = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
store.credentials.clear();
|
store.credentials.clear();
|
||||||
JsonArray arr = doc["credentials"].as<JsonArray>();
|
JsonArray arr = doc["credentials"].as<JsonArray>();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <I18n.h>
|
#include <I18n.h>
|
||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <esp_mac.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
@@ -15,15 +16,7 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
bool isLikelyValidMac(const uint8_t mac[6]) {
|
void readDeviceBaseMac(uint8_t mac[6]) { esp_efuse_mac_get_default(mac); }
|
||||||
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]) {
|
std::string formatMacLabel(const uint8_t mac[6]) {
|
||||||
char macStr[64];
|
char macStr[64];
|
||||||
@@ -32,6 +25,19 @@ std::string formatMacLabel(const uint8_t mac[6]) {
|
|||||||
return std::string(macStr);
|
return std::string(macStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string formatMacDashed(const uint8_t mac[6]) {
|
||||||
|
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]);
|
||||||
|
return std::string(persistedMac);
|
||||||
|
}
|
||||||
|
|
||||||
|
String formatMacCompact(const uint8_t mac[6]) {
|
||||||
|
char compactMac[13];
|
||||||
|
snprintf(compactMac, sizeof(compactMac), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
return String(compactMac);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void WifiSelectionActivity::onEnter() {
|
void WifiSelectionActivity::onEnter() {
|
||||||
@@ -44,12 +50,10 @@ void WifiSelectionActivity::onEnter() {
|
|||||||
WIFI_STORE.loadFromFile();
|
WIFI_STORE.loadFromFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show persisted MAC immediately so UI doesn't briefly display a bogus value.
|
// Use base MAC from eFuse (stable per-device, independent of WiFi init timing).
|
||||||
if (!WIFI_STORE.getLastKnownMacAddress().empty()) {
|
uint8_t mac[6];
|
||||||
cachedMacAddress = std::string(tr(STR_MAC_ADDRESS)) + " " + WIFI_STORE.getLastKnownMacAddress();
|
readDeviceBaseMac(mac);
|
||||||
} else {
|
cachedMacAddress = formatMacLabel(mac);
|
||||||
cachedMacAddress = std::string(tr(STR_MAC_ADDRESS)) + " --";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset state
|
// Reset state
|
||||||
selectedNetworkIndex = 0;
|
selectedNetworkIndex = 0;
|
||||||
@@ -64,18 +68,10 @@ void WifiSelectionActivity::onEnter() {
|
|||||||
forgetPromptSelection = 0;
|
forgetPromptSelection = 0;
|
||||||
autoConnecting = false;
|
autoConnecting = false;
|
||||||
|
|
||||||
// Refresh displayed MAC from live hardware value when valid.
|
const std::string persistedMac = formatMacDashed(mac);
|
||||||
uint8_t mac[6];
|
if (WIFI_STORE.getLastKnownMacAddress() != persistedMac) {
|
||||||
WiFi.macAddress(mac);
|
RenderLock lock(*this);
|
||||||
if (isLikelyValidMac(mac)) {
|
WIFI_STORE.setLastKnownMacAddress(persistedMac);
|
||||||
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
|
// Trigger first update to show scanning message
|
||||||
@@ -256,10 +252,10 @@ void WifiSelectionActivity::attemptConnection() {
|
|||||||
WiFi.disconnect(true, true); // Abort any in-progress SDK auto-connect and clear NVS-saved SSID
|
WiFi.disconnect(true, true); // Abort any in-progress SDK auto-connect and clear NVS-saved SSID
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
// Set hostname so routers show "CrossPoint-Reader-AABBCCDDEEFF" instead of "esp32-XXXXXXXXXXXX"
|
// Use stable base MAC so hostname suffix is deterministic across WiFi states.
|
||||||
String mac = WiFi.macAddress();
|
uint8_t baseMac[6];
|
||||||
mac.replace(":", "");
|
readDeviceBaseMac(baseMac);
|
||||||
String hostname = "CrossPoint-Reader-" + mac;
|
String hostname = "CrossPoint-Reader-" + formatMacCompact(baseMac);
|
||||||
WiFi.setHostname(hostname.c_str());
|
WiFi.setHostname(hostname.c_str());
|
||||||
|
|
||||||
if (selectedRequiresPassword && !enteredPassword.empty()) {
|
if (selectedRequiresPassword && !enteredPassword.empty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user