Add safe ESP32 WiFi compatibility controls and unified status
This commit is contained in:
1 parent
d3bbd5a4d0
commit
9363ad4f95
22 files changed
+1555
-38
No files matched your search
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
namespace wifi {
|
||||
|
||||
// Collected by the loop-task owner. Formatting never reads NVS, touches a
|
||||
// wireless driver, allocates memory, or retains pointers after the call.
|
||||
struct CompanionWiFiStatusSnapshot {
|
||||
const char* state;
|
||||
const char* ssid;
|
||||
const char* ip;
|
||||
bool has_rssi;
|
||||
int rssi;
|
||||
const char* protocol_saved;
|
||||
const char* protocol_boot;
|
||||
const char* phy;
|
||||
const char* mode_saved;
|
||||
const char* mode_boot;
|
||||
const char* powersave;
|
||||
bool coexist;
|
||||
bool ota_listening;
|
||||
bool ota_attached;
|
||||
uint16_t ota_port;
|
||||
};
|
||||
|
||||
namespace status_format_detail {
|
||||
|
||||
inline const char* knownText(const char* value) {
|
||||
return value && value[0] ? value : "n/a";
|
||||
}
|
||||
|
||||
class BoundedReply {
|
||||
public:
|
||||
BoundedReply(char* output, size_t capacity)
|
||||
: output_(output), capacity_(capacity), used_(0), complete_(true) {
|
||||
output_[0] = 0;
|
||||
}
|
||||
|
||||
void append(const char* text) {
|
||||
for (const char* cursor = text; *cursor; ++cursor) {
|
||||
if (used_ < capacity_ - 1) output_[used_++] = *cursor;
|
||||
else complete_ = false;
|
||||
}
|
||||
output_[used_] = 0;
|
||||
}
|
||||
|
||||
void appendNumber(int number) {
|
||||
char digits[16]; // Enough for every signed 32-bit int, including INT_MIN.
|
||||
snprintf(digits, sizeof(digits), "%d", number);
|
||||
append(digits);
|
||||
}
|
||||
|
||||
bool complete() const { return complete_; }
|
||||
|
||||
private:
|
||||
char* output_;
|
||||
size_t capacity_;
|
||||
size_t used_;
|
||||
bool complete_;
|
||||
};
|
||||
|
||||
} // namespace status_format_detail
|
||||
|
||||
// Protocol is always saved/boot/live; mode repeats the boot choice only when
|
||||
// it differs from the saved choice. All applicable fields are emitted in a
|
||||
// fixed order, rather than dropping diagnostics to fit a particular buffer.
|
||||
// Returns false on an invalid destination or truncation. Any nonempty valid
|
||||
// destination is NUL-terminated, including a one-byte destination.
|
||||
inline bool formatCompanionWiFiStatusSnapshot(
|
||||
char* output, size_t capacity, const CompanionWiFiStatusSnapshot& snapshot) {
|
||||
if (!output || !capacity) return false;
|
||||
using status_format_detail::knownText;
|
||||
status_format_detail::BoundedReply reply(output, capacity);
|
||||
reply.append("> ");
|
||||
reply.append(knownText(snapshot.state));
|
||||
reply.append(" ssid=");
|
||||
reply.append(knownText(snapshot.ssid));
|
||||
if (snapshot.ip && snapshot.ip[0]) {
|
||||
reply.append(" ip=");
|
||||
reply.append(snapshot.ip);
|
||||
}
|
||||
if (snapshot.has_rssi) {
|
||||
reply.append(" rssi=");
|
||||
reply.appendNumber(snapshot.rssi);
|
||||
}
|
||||
reply.append("\nprotocol=");
|
||||
reply.append(knownText(snapshot.protocol_saved));
|
||||
reply.append("/");
|
||||
reply.append(knownText(snapshot.protocol_boot));
|
||||
reply.append("/");
|
||||
reply.append(knownText(snapshot.phy));
|
||||
reply.append(" mode=");
|
||||
const char* saved_mode = knownText(snapshot.mode_saved);
|
||||
const char* boot_mode = knownText(snapshot.mode_boot);
|
||||
reply.append(saved_mode);
|
||||
if (strcmp(saved_mode, boot_mode) != 0) {
|
||||
reply.append("/");
|
||||
reply.append(boot_mode);
|
||||
}
|
||||
reply.append(" ps=");
|
||||
reply.append(knownText(snapshot.powersave));
|
||||
reply.append(" coex=");
|
||||
reply.append(snapshot.coexist ? "yes" : "no");
|
||||
reply.append(" ota=");
|
||||
if (snapshot.ota_attached || snapshot.ota_listening) {
|
||||
reply.appendNumber(snapshot.ota_port);
|
||||
reply.append(snapshot.ota_attached ? ":client" : ":listen");
|
||||
} else {
|
||||
reply.append("off");
|
||||
}
|
||||
return reply.complete();
|
||||
}
|
||||
|
||||
} // namespace wifi
|
||||
} // namespace mesh
|
||||
@@ -3630,7 +3630,8 @@ bool MQTTBridge::handleWiFiConnection(unsigned long now) {
|
||||
#if defined(COMPANION_EXCLUSIVE_WIFI_BLE)
|
||||
false,
|
||||
#elif defined(BLE_PIN_CODE) && defined(WIFI_SSID)
|
||||
true,
|
||||
mesh::wifi::activeCompanionMode()
|
||||
!= mesh::wifi::CompanionMode::WiFiOnly,
|
||||
#else
|
||||
false,
|
||||
#endif
|
||||
|
||||
@@ -176,7 +176,8 @@ static bool bluetoothWiFiCoexistenceRequired() {
|
||||
// Any running WebConfig instance therefore has no active Bluetooth stack.
|
||||
return false;
|
||||
#elif defined(BLE_PIN_CODE) && defined(WIFI_SSID)
|
||||
return true;
|
||||
return mesh::wifi::activeCompanionMode()
|
||||
!= mesh::wifi::CompanionMode::WiFiOnly;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#if defined(ESP32)
|
||||
#include <Preferences.h>
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
namespace wifi {
|
||||
|
||||
enum class StationProtocol : uint8_t { Bgn = 0, Bg = 1 };
|
||||
enum class CompanionMode : uint8_t { Normal = 0, WiFiOnly = 1 };
|
||||
enum class CompatibilitySaveResult : uint8_t {
|
||||
Saved, InvalidValue, RadioConflict, StorageError
|
||||
};
|
||||
using StationProtocolSaveResult = CompatibilitySaveResult;
|
||||
using CompanionModeSaveResult = CompatibilitySaveResult;
|
||||
|
||||
namespace compatibility_detail {
|
||||
template <typename Value> struct BootPreference {
|
||||
bool loaded = false;
|
||||
bool load_ok = false;
|
||||
Value active = static_cast<Value>(0);
|
||||
Value configured = static_cast<Value>(0);
|
||||
};
|
||||
|
||||
inline BootPreference<StationProtocol>& protocolPreference() {
|
||||
static BootPreference<StationProtocol> state;
|
||||
return state;
|
||||
}
|
||||
|
||||
inline BootPreference<CompanionMode>& modePreference() {
|
||||
static BootPreference<CompanionMode> state;
|
||||
return state;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
template <typename Value>
|
||||
inline bool loadForBoot(const char* key, BootPreference<Value>& state,
|
||||
bool reject_nondefault = false) {
|
||||
if (state.loaded) return state.load_ok;
|
||||
Preferences prefs;
|
||||
const bool opened = prefs.begin("mesh-wifi", false);
|
||||
uint8_t stored = 0;
|
||||
if (opened) {
|
||||
stored = prefs.getUChar(key, 0);
|
||||
prefs.end();
|
||||
}
|
||||
if (stored > 1 || reject_nondefault) stored = 0;
|
||||
state.active = state.configured = static_cast<Value>(stored);
|
||||
// Freeze even an unavailable/corrupt store to the safe default this boot.
|
||||
state.loaded = true;
|
||||
state.load_ok = opened;
|
||||
return opened;
|
||||
}
|
||||
|
||||
template <typename Value>
|
||||
inline CompatibilitySaveResult save(const char* key, Value value,
|
||||
BootPreference<Value>& state) {
|
||||
if (static_cast<uint8_t>(value) > 1) {
|
||||
return CompatibilitySaveResult::InvalidValue;
|
||||
}
|
||||
if (!state.loaded) return CompatibilitySaveResult::StorageError;
|
||||
Preferences prefs;
|
||||
if (!prefs.begin("mesh-wifi", false)) {
|
||||
return CompatibilitySaveResult::StorageError;
|
||||
}
|
||||
const uint8_t stored = static_cast<uint8_t>(value);
|
||||
const bool saved = prefs.putUChar(key, stored) == sizeof(uint8_t)
|
||||
&& prefs.getUChar(key, 255) == stored;
|
||||
if (!saved) {
|
||||
// A failed verification must not knowingly leave the requested mode saved.
|
||||
prefs.putUChar(key, static_cast<uint8_t>(state.configured));
|
||||
}
|
||||
prefs.end();
|
||||
if (!saved) return CompatibilitySaveResult::StorageError;
|
||||
state.configured = value;
|
||||
return CompatibilitySaveResult::Saved;
|
||||
}
|
||||
#endif
|
||||
} // namespace compatibility_detail
|
||||
|
||||
// Reads are deliberately NVS-free: boot explicitly loads these before either
|
||||
// radio starts; saving only changes the configuration for the next reboot.
|
||||
inline StationProtocol activeStationProtocol() {
|
||||
return compatibility_detail::protocolPreference().active;
|
||||
}
|
||||
inline StationProtocol configuredStationProtocol() {
|
||||
return compatibility_detail::protocolPreference().configured;
|
||||
}
|
||||
inline bool stationProtocolRestartPending() {
|
||||
return configuredStationProtocol() != activeStationProtocol();
|
||||
}
|
||||
inline const char* stationProtocolName(StationProtocol value) {
|
||||
return value == StationProtocol::Bg ? "bg" : "bgn";
|
||||
}
|
||||
inline bool parseStationProtocol(const char* value, StationProtocol* out) {
|
||||
if (value == nullptr || out == nullptr) return false;
|
||||
if (strcmp(value, "bgn") == 0) *out = StationProtocol::Bgn;
|
||||
else if (strcmp(value, "bg") == 0) *out = StationProtocol::Bg;
|
||||
else return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline CompanionMode activeCompanionMode() {
|
||||
return compatibility_detail::modePreference().active;
|
||||
}
|
||||
inline CompanionMode configuredCompanionMode() {
|
||||
return compatibility_detail::modePreference().configured;
|
||||
}
|
||||
inline bool companionModeRestartPending() {
|
||||
return configuredCompanionMode() != activeCompanionMode();
|
||||
}
|
||||
inline const char* companionModeName(CompanionMode value) {
|
||||
return value == CompanionMode::WiFiOnly ? "wifi-only" : "normal";
|
||||
}
|
||||
inline bool parseCompanionMode(const char* value, CompanionMode* out) {
|
||||
if (value == nullptr || out == nullptr) return false;
|
||||
if (strcmp(value, "normal") == 0) *out = CompanionMode::Normal;
|
||||
else if (strcmp(value, "wifi-only") == 0) *out = CompanionMode::WiFiOnly;
|
||||
else return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
inline bool loadCompanionModeForBoot() {
|
||||
return compatibility_detail::loadForBoot(
|
||||
"wifi_only", compatibility_detail::modePreference());
|
||||
}
|
||||
inline CompatibilitySaveResult saveCompanionMode(CompanionMode value) {
|
||||
return compatibility_detail::save(
|
||||
"wifi_only", value, compatibility_detail::modePreference());
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace wifi
|
||||
} // namespace mesh
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <helpers/WiFiChannelPolicy.h>
|
||||
#include <helpers/esp32/WiFiCompatibility.h>
|
||||
#include <atomic>
|
||||
|
||||
#if defined(MESH_PRIMARY_ESPNOW) && MESH_PRIMARY_ESPNOW
|
||||
@@ -47,6 +48,11 @@ static constexpr uint8_t kProtocolMask = WIFI_PROTOCOL_11B
|
||||
static constexpr uint8_t kAccessPointProtocolMask = WIFI_PROTOCOL_11B
|
||||
| WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N;
|
||||
|
||||
inline uint8_t conventionalStationProtocolMask() {
|
||||
return activeStationProtocol() == StationProtocol::Bg
|
||||
? WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G : kAccessPointProtocolMask;
|
||||
}
|
||||
|
||||
static constexpr uint8_t kLongRangeRadioOwner = 1;
|
||||
static constexpr uint8_t kLongRangeBridgeOwner = 2;
|
||||
inline std::atomic<uint8_t>& longRangeOwners() {
|
||||
@@ -61,6 +67,25 @@ inline bool accessPointCompatibleWithLongRange() {
|
||||
return longRangeOwners().load() == 0;
|
||||
}
|
||||
|
||||
#if defined(ESP32)
|
||||
inline bool loadStationProtocolForBoot() {
|
||||
return compatibility_detail::loadForBoot(
|
||||
"sta_protocol", compatibility_detail::protocolPreference(),
|
||||
kPrimaryEspNowRadio || !accessPointCompatibleWithLongRange());
|
||||
}
|
||||
inline CompatibilitySaveResult saveStationProtocol(StationProtocol value) {
|
||||
if (static_cast<uint8_t>(value) > 1) {
|
||||
return CompatibilitySaveResult::InvalidValue;
|
||||
}
|
||||
if (value == StationProtocol::Bg
|
||||
&& (kPrimaryEspNowRadio || !accessPointCompatibleWithLongRange())) {
|
||||
return CompatibilitySaveResult::RadioConflict;
|
||||
}
|
||||
return compatibility_detail::save(
|
||||
"sta_protocol", value, compatibility_detail::protocolPreference());
|
||||
}
|
||||
#endif
|
||||
|
||||
// A bridge can start/stop independently of infrastructure WiFi. Its channel
|
||||
// constraint exists only while it runs; primary ESP-NOW keeps its boot policy.
|
||||
inline std::atomic<uint8_t>& bridgeEspNowChannel() {
|
||||
@@ -133,6 +158,9 @@ inline bool saveConfiguredEspNowChannel(uint8_t) {
|
||||
// Check the SDK rather than Arduino's facade: an ESP-NOW bridge can own a
|
||||
// driver which Arduino has not initialized. An absent driver is safe to start.
|
||||
inline esp_err_t checkLongRangeRadioStart() {
|
||||
if (activeStationProtocol() == StationProtocol::Bg) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
const esp_err_t result = esp_wifi_get_mode(&mode);
|
||||
if (result == ESP_ERR_WIFI_NOT_INIT) return ESP_OK;
|
||||
@@ -154,11 +182,19 @@ inline esp_err_t applyAccessPointProtocolMask() {
|
||||
}
|
||||
|
||||
inline esp_err_t applyStationProtocolMask(uint8_t protocols, bool require_lr = false) {
|
||||
if (activeStationProtocol() == StationProtocol::Bg) {
|
||||
if (require_lr || kPrimaryEspNowRadio
|
||||
|| !accessPointCompatibleWithLongRange()) return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
esp_err_t result = esp_wifi_get_mode(&mode);
|
||||
if (result != ESP_OK) return result;
|
||||
if ((mode & WIFI_MODE_AP) && require_lr) return ESP_ERR_INVALID_STATE;
|
||||
if (mode & WIFI_MODE_AP) protocols = kAccessPointProtocolMask;
|
||||
// Setup AP beacons stay B/G/N; only the station uses compatibility BG.
|
||||
if (activeStationProtocol() == StationProtocol::Bg) {
|
||||
protocols = conventionalStationProtocolMask();
|
||||
}
|
||||
|
||||
// persistent(false) only changes Arduino's next initialization preference.
|
||||
// Select RAM on this already initialized SDK driver before any LR write.
|
||||
@@ -177,7 +213,7 @@ inline esp_err_t applyStationProtocolMask(uint8_t protocols, bool require_lr = f
|
||||
// An established LR owner already prevents AP startup. Do not silently
|
||||
// downgrade that owner when a later infrastructure-only mode read fails.
|
||||
if (accessPointCompatibleWithLongRange()) {
|
||||
esp_wifi_set_protocol(WIFI_IF_STA, kAccessPointProtocolMask);
|
||||
esp_wifi_set_protocol(WIFI_IF_STA, conventionalStationProtocolMask());
|
||||
applyAccessPointProtocolMask();
|
||||
}
|
||||
return result;
|
||||
@@ -185,7 +221,7 @@ inline esp_err_t applyStationProtocolMask(uint8_t protocols, bool require_lr = f
|
||||
if (mode & WIFI_MODE_AP) {
|
||||
result = applyAccessPointProtocolMask();
|
||||
if (result != ESP_OK) {
|
||||
esp_wifi_set_protocol(WIFI_IF_STA, kAccessPointProtocolMask);
|
||||
esp_wifi_set_protocol(WIFI_IF_STA, conventionalStationProtocolMask());
|
||||
return result;
|
||||
}
|
||||
uint8_t actual = 0;
|
||||
|
||||
Reference in new issue
Block a user