Apply busy radio settings and recover service starts automatically
Save valid busy RX, fast-switch and Companion tuple changes once, then apply the latest accepted intent at a safe radio gap. Preserve checked-save rollback, runtime RXEN intent, TempRadio ownership and exact remote reply delivery. Repair failed preamble compensation without hiding uncertain saves. Retry WebConfig, MQTT and NimBLE startup failures automatically. Preserve explicit off choices, OTA ownership and the latest WiFi credentials through cooperative shutdown. Add regression coverage and update command guides. Validation: focused host transaction, rollback, ownership and lifecycle suites; Full RAK4631, Heltec V4 R8 TFT and RC32 packages; representative room, sensor, LR1110, LR2021, SX1276, STM32WL and ESP-NOW builds. Linked RAK crypto and OTA workspace checks pass; its complete OTA image uses 712644/712704 bytes.
This commit is contained in:
1 parent
fb73e3afac
commit
73c5f572eb
89 files changed
+2978
-816
No files matched your search
+154
-39
@@ -174,6 +174,7 @@ void CommonCLI::loop() {
|
||||
#endif
|
||||
if (!mesh::morseTimingActive(_callbacks->getProfileRadio())) {
|
||||
servicePendingTxPower();
|
||||
servicePendingRxSettings();
|
||||
_radio_profiles.loop();
|
||||
loopManagement();
|
||||
#if MESH_ENABLE_TX_BEACONS
|
||||
@@ -247,6 +248,77 @@ void CommonCLI::applyAndSaveTxPower(int8_t power, char* reply) {
|
||||
mesh::cli::formatError(reply, 160, "TX save unconfirmed; reboot may differ");
|
||||
}
|
||||
|
||||
mesh::RadioParamApplyResult CommonCLI::applyRxSetting(uint8_t setting) {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
const mesh::Radio* radio = _callbacks->getProfileRadio();
|
||||
if (mesh::morseTimingActive(radio) || (radio && radio->isCarrierWaveActive())) return Result::BUSY;
|
||||
switch (setting) {
|
||||
case RxGain:
|
||||
return _callbacks->trySetRxBoostedGain(_prefs->rx_boosted_gain != 0);
|
||||
case RxPowerSaving:
|
||||
return _callbacks->trySetRxPowerSaving(_prefs->rx_powersaving_enabled != 0,
|
||||
_prefs->rx_ps_rx_us, _prefs->rx_ps_sleep_us);
|
||||
#if defined(USE_LR2021)
|
||||
case RxSideDetectors: {
|
||||
uint8_t count = 0;
|
||||
if (!mesh::lr2021::storedSideDetectorCount(_prefs->extra_sf, count)) return Result::FAILED;
|
||||
return _callbacks->tryConfigSideDetectors(_prefs->extra_sf, count, _prefs->bw);
|
||||
}
|
||||
#endif
|
||||
case RxRfRx:
|
||||
return _callbacks->trySetRxPowerSavingRfRxDisabled(
|
||||
(_rx_settings_pending & RxRfRxDisabled) != 0);
|
||||
default: return Result::FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
void CommonCLI::servicePendingRxSettings() {
|
||||
if (!(_rx_settings_pending & RxPendingMask) || (_rx_settings_retry_at
|
||||
&& static_cast<int32_t>(millis() - _rx_settings_retry_at) < 0)) return;
|
||||
bool failed = false;
|
||||
for (uint8_t setting = RxGain; setting <= RxRfRx; setting <<= 1) {
|
||||
if (!(_rx_settings_pending & setting)) continue;
|
||||
const auto result = applyRxSetting(setting);
|
||||
if (result == mesh::RadioParamApplyResult::APPLIED) _rx_settings_pending &= ~setting;
|
||||
failed |= result == mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
_rx_settings_retry_at = failed ? millis() + 1000 : 0;
|
||||
if (failed && !_rx_settings_retry_at) _rx_settings_retry_at = 1;
|
||||
}
|
||||
|
||||
bool CommonCLI::saveRxSetting(uint8_t setting, mesh::RadioParamApplyResult result, char* reply) {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
if (result == Result::FAILED) {
|
||||
mesh::cli::formatError(reply, 160, "RX setting rejected");
|
||||
return false;
|
||||
}
|
||||
if (!trySavePrefs()) {
|
||||
mesh::cli::formatError(reply, 160, "RX save unconfirmed; reboot may differ");
|
||||
return false;
|
||||
}
|
||||
_rx_settings_pending &= ~setting;
|
||||
if (result == Result::BUSY) _rx_settings_pending |= setting;
|
||||
_rx_settings_retry_at = 0;
|
||||
strcpy(reply, result == Result::BUSY ? "OK - saved; pending" : "OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
void CommonCLI::restoreRxSetting(uint8_t setting, mesh::RadioParamApplyResult result,
|
||||
uint8_t previous_pending, uint32_t previous_retry_at) {
|
||||
_rx_settings_pending = previous_pending;
|
||||
_rx_settings_retry_at = previous_retry_at;
|
||||
if (result == mesh::RadioParamApplyResult::BUSY) return;
|
||||
// Hardware errors can be partial writes. Repair only from the old accepted
|
||||
// target, including when the rejected request never reached persistence.
|
||||
const auto restored = applyRxSetting(setting);
|
||||
_rx_settings_pending &= ~setting;
|
||||
if (restored != mesh::RadioParamApplyResult::APPLIED) _rx_settings_pending |= setting;
|
||||
_rx_settings_retry_at = restored == mesh::RadioParamApplyResult::FAILED ? millis() + 1000
|
||||
: restored == mesh::RadioParamApplyResult::BUSY ? 0 : previous_retry_at;
|
||||
if (restored == mesh::RadioParamApplyResult::FAILED && !_rx_settings_retry_at)
|
||||
_rx_settings_retry_at = 1;
|
||||
}
|
||||
|
||||
// Old fork firmware persisted the (since removed) NodePrefs MQTT fields to /com_prefs
|
||||
// as a zero-filled gap between owner_info (which ends at offset 290) and a trailing
|
||||
// observer block (rx_boosted_gain, flood_max_*, snmp/watchdog/alert settings).
|
||||
@@ -3577,7 +3649,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
else {
|
||||
// Keep WiFi up, but release MQTT/TLS heap while the browser uploader runs.
|
||||
const bool mqtt_paused = _callbacks->isMqttBridgeRunning();
|
||||
const bool mqtt_paused = _callbacks->isMqttBridgeRunning()
|
||||
|| _callbacks->isMqttBridgeStartPending();
|
||||
_wifi_ota_resume_mqtt = _wifi_ota_resume_mqtt || mqtt_paused;
|
||||
if (mqtt_paused) _callbacks->setMqttBridgeState(false);
|
||||
}
|
||||
@@ -4296,10 +4369,6 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
mesh::cli::formatError(reply, 160, "usage set logging.output off|usb|wifi|both");
|
||||
return;
|
||||
}
|
||||
if (wifi_enabled && _callbacks->isMqttBridgeStopping()) {
|
||||
mesh::cli::formatError(reply, 160, "MQTT is stopping; retry when mqtt.stopping is off");
|
||||
return;
|
||||
}
|
||||
const uint8_t previous_usb = _prefs->usb_logging_enabled;
|
||||
const uint8_t previous_bridge = _prefs->bridge_enabled;
|
||||
_prefs->usb_logging_enabled = usb_enabled ? 1 : 0;
|
||||
@@ -4372,10 +4441,6 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
mesh::cli::formatError(reply, 160, "use set mqtt.enabled on|off");
|
||||
} else {
|
||||
const bool enable = strcmp(value, "on") == 0;
|
||||
if (enable && _callbacks->isMqttBridgeStopping()) {
|
||||
mesh::cli::formatError(reply, 160, "MQTT is stopping; retry when mqtt.stopping is off");
|
||||
return;
|
||||
}
|
||||
const uint8_t previous = _prefs->bridge_enabled;
|
||||
_prefs->bridge_enabled = enable ? 1 : 0;
|
||||
// A cooperative stop cannot be cancelled safely once signalled. Save
|
||||
@@ -4712,13 +4777,23 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "Error, must be on or off");
|
||||
}
|
||||
} else if (memcmp(config, "radio.rxgain ", 13) == 0) {
|
||||
bool enabled = memcmp(&config[13], "on", 2) == 0;
|
||||
if (_callbacks->setRxBoostedGain(enabled)) {
|
||||
_prefs->rx_boosted_gain = enabled;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
const char* value = &config[13];
|
||||
if (strcmp(value, "on") != 0 && strcmp(value, "off") != 0) {
|
||||
mesh::cli::formatError(reply, 160, "state must be on or off");
|
||||
return;
|
||||
}
|
||||
if (!_callbacks->supportsRxBoostedGain()) {
|
||||
mesh::cli::formatError(reply, 160, "unsupported");
|
||||
return;
|
||||
}
|
||||
const uint8_t previous = _prefs->rx_boosted_gain;
|
||||
const uint8_t previous_pending = _rx_settings_pending;
|
||||
const uint32_t previous_retry_at = _rx_settings_retry_at;
|
||||
_prefs->rx_boosted_gain = strcmp(value, "on") == 0;
|
||||
const auto result = applyRxSetting(RxGain);
|
||||
if (!saveRxSetting(RxGain, result, reply)) {
|
||||
_prefs->rx_boosted_gain = previous;
|
||||
restoreRxSetting(RxGain, result, previous_pending, previous_retry_at);
|
||||
}
|
||||
} else if (memcmp(config, "radio.fem.rxgain ", 17) == 0) {
|
||||
const auto* radio = _callbacks->getProfileRadio();
|
||||
@@ -4790,10 +4865,30 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_callbacks->setRxPowerSavingRfRxDisabled(disabled)) {
|
||||
if (!_callbacks->supportsRxPowerSavingRfRxDisable()) {
|
||||
mesh::cli::formatError(reply, 160, "unsupported");
|
||||
return;
|
||||
}
|
||||
// With no outstanding target, snapshot the actual runtime setting. Its
|
||||
// accepted desired value shares the high bit of the pending mask.
|
||||
if (!(_rx_settings_pending & RxRfRx)) {
|
||||
_rx_settings_pending &= ~RxRfRxDisabled;
|
||||
if (_callbacks->isRxPowerSavingRfRxDisabled()) _rx_settings_pending |= RxRfRxDisabled;
|
||||
}
|
||||
const uint8_t previous_pending = _rx_settings_pending;
|
||||
const uint32_t previous_retry_at = _rx_settings_retry_at;
|
||||
_rx_settings_pending &= ~RxRfRxDisabled;
|
||||
if (disabled) _rx_settings_pending |= RxRfRxDisabled;
|
||||
const auto result = applyRxSetting(RxRfRx);
|
||||
if (result == mesh::RadioParamApplyResult::FAILED) {
|
||||
restoreRxSetting(RxRfRx, result, previous_pending, previous_retry_at);
|
||||
mesh::cli::formatError(reply, 160, "RX setting rejected");
|
||||
} else {
|
||||
sprintf(reply, "OK - radio.rxps.rfrx_disabled %s", disabled ? "on" : "off");
|
||||
_rx_settings_pending &= ~RxRfRx;
|
||||
if (result == mesh::RadioParamApplyResult::BUSY) _rx_settings_pending |= RxRfRx;
|
||||
_rx_settings_retry_at = 0;
|
||||
sprintf(reply, "OK - radio.rxps.rfrx_disabled %s%s", disabled ? "on" : "off",
|
||||
result == mesh::RadioParamApplyResult::BUSY ? "; pending" : "");
|
||||
}
|
||||
} else if (memcmp(config, "radio.rxps ", 11) == 0) { // RX PowerSaving
|
||||
const char* value = &config[11];
|
||||
@@ -4806,9 +4901,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
bool preamble_overridden = false;
|
||||
bool manual_timings_requested = false;
|
||||
|
||||
ensureRxPowerSavingDefaults(&_prefs->rx_ps_rx_us, &_prefs->rx_ps_sleep_us);
|
||||
rx_us = _prefs->rx_ps_rx_us;
|
||||
sleep_us = _prefs->rx_ps_sleep_us;
|
||||
ensureRxPowerSavingDefaults(&rx_us, &sleep_us);
|
||||
|
||||
mesh::cli::RxPowerSavingArguments parsed = {};
|
||||
if (!mesh::cli::parseRxPowerSavingArgumentsStrict(value, parsed)) {
|
||||
@@ -4816,14 +4909,10 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
return;
|
||||
}
|
||||
using mesh::cli::RxPowerSavingArgumentMode;
|
||||
#ifdef WRAPPER_CLASS
|
||||
if (!WRAPPER_CLASS::SupportsRxPowerSaving) {
|
||||
if (parsed.mode != RxPowerSavingArgumentMode::Off) {
|
||||
strcpy(reply, "ERROR: RX powersaving unsupported");
|
||||
return;
|
||||
}
|
||||
if (!_callbacks->supportsRxPowerSaving() && parsed.mode != RxPowerSavingArgumentMode::Off) {
|
||||
strcpy(reply, "ERROR: RX powersaving unsupported");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (parsed.mode == RxPowerSavingArgumentMode::Off) {
|
||||
enable = 0;
|
||||
} else if (parsed.mode == RxPowerSavingArgumentMode::Manual) {
|
||||
@@ -4879,11 +4968,11 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_callbacks->setRxPowerSaving(enable, rx_us, sleep_us)) {
|
||||
strcpy(reply, "ERROR: RX powersaving unsupported");
|
||||
return;
|
||||
}
|
||||
|
||||
const RxPowerSavingConfig previous(_prefs->rx_powersaving_enabled,
|
||||
_prefs->rx_ps_rx_us, _prefs->rx_ps_sleep_us,
|
||||
_prefs->rx_ps_level, _prefs->rx_ps_preamble);
|
||||
const uint8_t previous_pending = _rx_settings_pending;
|
||||
const uint32_t previous_retry_at = _rx_settings_retry_at;
|
||||
_prefs->rx_powersaving_enabled = enable;
|
||||
_prefs->rx_ps_rx_us = rx_us;
|
||||
_prefs->rx_ps_sleep_us = sleep_us;
|
||||
@@ -4897,7 +4986,16 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
_prefs->rx_ps_level = 0;
|
||||
_prefs->rx_ps_preamble = 0;
|
||||
}
|
||||
savePrefs();
|
||||
const auto result = applyRxSetting(RxPowerSaving);
|
||||
if (!saveRxSetting(RxPowerSaving, result, reply)) {
|
||||
_prefs->rx_powersaving_enabled = previous.enabled;
|
||||
_prefs->rx_ps_rx_us = previous.rx_us;
|
||||
_prefs->rx_ps_sleep_us = previous.sleep_us;
|
||||
_prefs->rx_ps_level = previous.level;
|
||||
_prefs->rx_ps_preamble = previous.preamble;
|
||||
restoreRxSetting(RxPowerSaving, result, previous_pending, previous_retry_at);
|
||||
return;
|
||||
}
|
||||
if (level_requested) {
|
||||
sprintf(reply, "OK - level %lu,%s,%lu,%lu,preamble=%lu",
|
||||
(unsigned long)level,
|
||||
@@ -4911,6 +5009,10 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
sprintf(reply, "OK - %s,%lu,%lu", enable ? "on" : "off",
|
||||
(unsigned long)rx_us, (unsigned long)sleep_us);
|
||||
}
|
||||
if (result == mesh::RadioParamApplyResult::BUSY) {
|
||||
const size_t used = strlen(reply);
|
||||
if (used < 160) snprintf(reply + used, 160 - used, "; saved; pending");
|
||||
}
|
||||
} else if (strcmp(config, "powersaving on") == 0
|
||||
|| strcmp(config, "powersaving off") == 0) {
|
||||
const bool enabled = strcmp(&config[12], "on") == 0;
|
||||
@@ -5679,21 +5781,34 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
}
|
||||
} else if (strcmp(config, "extra.sf") == 0 || memcmp(config, "extra.sf ", 9) == 0) {
|
||||
#if defined(USE_LR2021)
|
||||
const auto* radio = _callbacks->getProfileRadio();
|
||||
const auto* profiles = radio ? radio->profiles() : nullptr;
|
||||
if (profiles && profiles->enabled()) {
|
||||
mesh::cli::formatError(reply, 160, "extra SFs unavailable with dual radio");
|
||||
return;
|
||||
}
|
||||
uint8_t sideDetSFs[mesh::lr2021::STORED_SIDE_DETECTOR_BYTES] = {};
|
||||
uint8_t num = 0;
|
||||
const char* value = config[8] == '\0' ? "" : &config[9];
|
||||
if (strcmp(value, "none") == 0 || strcmp(value, "off") == 0) value = "";
|
||||
if (!mesh::lr2021::parseSideDetectorSFList(value, sideDetSFs, num)
|
||||
|| !mesh::lr2021::validateSideDetectorSFs(
|
||||
sideDetSFs, num, _prefs->sf, _prefs->bw)) {
|
||||
sideDetSFs, num, _prefs->sf, _prefs->bw)
|
||||
|| (profiles && !mesh::lr2021::validateSideDetectorSFs(
|
||||
sideDetSFs, num, profiles->primary.sf, profiles->primary.bw))) {
|
||||
strcpy(reply, "Invalid extra SF config");
|
||||
} else {
|
||||
if (_callbacks->configSideDetectors(sideDetSFs, num, _prefs->bw)) {
|
||||
memcpy(_prefs->extra_sf, sideDetSFs, sizeof(_prefs->extra_sf));
|
||||
savePrefs();
|
||||
uint8_t previous[sizeof(_prefs->extra_sf)];
|
||||
memcpy(previous, _prefs->extra_sf, sizeof(previous));
|
||||
const uint8_t previous_pending = _rx_settings_pending;
|
||||
const uint32_t previous_retry_at = _rx_settings_retry_at;
|
||||
memcpy(_prefs->extra_sf, sideDetSFs, sizeof(_prefs->extra_sf));
|
||||
const auto result = applyRxSetting(RxSideDetectors);
|
||||
if (!saveRxSetting(RxSideDetectors, result, reply)) {
|
||||
memcpy(_prefs->extra_sf, previous, sizeof(previous));
|
||||
restoreRxSetting(RxSideDetectors, result, previous_pending, previous_retry_at);
|
||||
} else if (result == mesh::RadioParamApplyResult::APPLIED) {
|
||||
strcpy(reply, num == 0 ? "OK - extra SFs cleared" : "OK - extra SFs set");
|
||||
} else {
|
||||
strcpy(reply, "Invalid extra SF config");
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
@@ -668,6 +668,9 @@ public:
|
||||
// Ownership barriers continue to use the synchronous state setter.
|
||||
virtual bool requestMqttBridgeStop() { return setMqttBridgeState(false); }
|
||||
virtual bool isMqttBridgeStopping() { return false; }
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
virtual bool isMqttBridgeStartPending() { return false; }
|
||||
#endif
|
||||
virtual bool setEspNowBridgeState(bool enable) { return setBridgeState(enable); }
|
||||
virtual bool setRs232BridgeState(bool enable) { return setBridgeState(enable); }
|
||||
virtual bool restartMqttBridge() { return restartBridge(); }
|
||||
@@ -829,11 +832,21 @@ public:
|
||||
virtual bool setRxBoostedGain(bool enable) {
|
||||
return false; // CommonCLI reports unsupported if not overridden by wrapper
|
||||
};
|
||||
virtual bool supportsRxBoostedGain() const { return false; }
|
||||
virtual mesh::RadioParamApplyResult trySetRxBoostedGain(bool enable) {
|
||||
return setRxBoostedGain(enable) ? mesh::RadioParamApplyResult::APPLIED
|
||||
: mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
|
||||
#if defined(USE_LR2021)
|
||||
virtual bool configSideDetectors(const uint8_t sideDetSFs[], uint8_t num, float bw) {
|
||||
return false; // Override in wrapper
|
||||
}
|
||||
virtual mesh::RadioParamApplyResult tryConfigSideDetectors(
|
||||
const uint8_t sideDetSFs[], uint8_t num, float bw) {
|
||||
return configSideDetectors(sideDetSFs, num, bw) ? mesh::RadioParamApplyResult::APPLIED
|
||||
: mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
#endif
|
||||
|
||||
virtual void recalibrateNoiseFloor() {
|
||||
@@ -861,12 +874,22 @@ public:
|
||||
virtual bool setRxPowerSaving(bool enable, uint32_t rx_us, uint32_t sleep_us) {
|
||||
return !enable;
|
||||
};
|
||||
virtual bool supportsRxPowerSaving() const { return false; }
|
||||
virtual mesh::RadioParamApplyResult trySetRxPowerSaving(
|
||||
bool enable, uint32_t rx_us, uint32_t sleep_us) {
|
||||
return setRxPowerSaving(enable, rx_us, sleep_us) ? mesh::RadioParamApplyResult::APPLIED
|
||||
: mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
|
||||
virtual bool supportsRxPowerSavingRfRxDisable() const { return false; }
|
||||
virtual bool setRxPowerSavingRfRxDisabled(bool disabled) {
|
||||
(void)disabled;
|
||||
return false;
|
||||
}
|
||||
virtual mesh::RadioParamApplyResult trySetRxPowerSavingRfRxDisabled(bool disabled) {
|
||||
return setRxPowerSavingRfRxDisabled(disabled) ? mesh::RadioParamApplyResult::APPLIED
|
||||
: mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
virtual bool isRxPowerSavingRfRxDisabled() const { return false; }
|
||||
|
||||
virtual void getRxPsWatchdogCounts(uint32_t* soft, uint32_t* hard) {
|
||||
@@ -950,6 +973,12 @@ class CommonCLI {
|
||||
bool _common_save_succeeded = false;
|
||||
bool _tx_power_pending = false;
|
||||
uint32_t _tx_power_retry_at = 0;
|
||||
enum RxSetting : uint8_t {
|
||||
RxGain = 1, RxPowerSaving = 2, RxSideDetectors = 4, RxRfRx = 8,
|
||||
RxPendingMask = 15, RxRfRxDisabled = 128
|
||||
};
|
||||
uint8_t _rx_settings_pending = 0;
|
||||
uint32_t _rx_settings_retry_at = 0;
|
||||
uint32_t _prefs_save_failures = 0;
|
||||
mesh::RadioProfileCLI _radio_profiles;
|
||||
#if MESH_ENABLE_TX_BEACONS
|
||||
@@ -971,6 +1000,10 @@ class CommonCLI {
|
||||
bool trySavePrefs(
|
||||
PrefsSaveRouting::Scope scope = PrefsSaveRouting::Scope::Common);
|
||||
void applyAndSaveTxPower(int8_t power, char* reply);
|
||||
mesh::RadioParamApplyResult applyRxSetting(uint8_t setting);
|
||||
bool saveRxSetting(uint8_t setting, mesh::RadioParamApplyResult result, char* reply);
|
||||
void restoreRxSetting(uint8_t setting, mesh::RadioParamApplyResult result,
|
||||
uint8_t previous_pending, uint32_t previous_retry_at);
|
||||
bool applyAndSaveAdcMultiplier(float multiplier, char* reply, size_t capacity);
|
||||
bool saveObserverPrefs();
|
||||
void loadPrefsInt(FILESYSTEM* _fs, const char* filename);
|
||||
@@ -1063,6 +1096,11 @@ public:
|
||||
bool hasActiveUserGpioTimer() const;
|
||||
bool hasPendingTxPower() const { return _tx_power_pending; }
|
||||
void servicePendingTxPower();
|
||||
bool hasPendingRadioSettings() const {
|
||||
return _tx_power_pending || (_rx_settings_pending & RxPendingMask) != 0
|
||||
|| _radio_profiles.hasPendingTuning();
|
||||
}
|
||||
void servicePendingRxSettings();
|
||||
void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
|
||||
mesh::MainBoard* getBoard() { return _board; }
|
||||
uint8_t buildAdvertData(uint8_t node_type, uint8_t* app_data);
|
||||
|
||||
@@ -202,11 +202,16 @@ void CommonRadioPrefs::formatRxPowerSavingStatus(char* reply, size_t reply_size)
|
||||
RXPowerSavingCLI::get(config, status, sf, bw, reply, reply_size, wire_preamble);
|
||||
}
|
||||
|
||||
bool CommonRadioPrefs::isValidBandwidth(float bandwidth) {
|
||||
return isValidLoRaBandwidth(bandwidth);
|
||||
}
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
// Keep shared radio parsing out of the Companion's larger command dispatcher.
|
||||
__attribute__((noinline))
|
||||
#endif
|
||||
bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
|
||||
bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timestamp, char* reply,
|
||||
bool allow_primary_set) {
|
||||
(void)sender_timestamp;
|
||||
if (command == nullptr || reply == nullptr) return false;
|
||||
|
||||
@@ -222,7 +227,7 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
|
||||
sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)getSpreadFactor(), (uint32_t)getCodingRate());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set radio ")) {
|
||||
if (allow_primary_set && startsWith(command, "set radio ")) {
|
||||
const char* args = command + strlen("set radio ");
|
||||
const size_t args_len = boundedLength(args, RADIO_ARGS_CAPACITY);
|
||||
if (args_len >= RADIO_ARGS_CAPACITY) {
|
||||
|
||||
@@ -66,7 +66,9 @@ public:
|
||||
virtual RxPowerSavingConfig readRxPowerSavingConfig() const { return {}; }
|
||||
void formatRxPowerSavingStatus(char* reply, size_t reply_size) const;
|
||||
|
||||
bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply);
|
||||
static bool isValidBandwidth(float bandwidth);
|
||||
bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply,
|
||||
bool allow_primary_set = true);
|
||||
|
||||
bool setByKey(const char* key, const char* value) override; // for dynamic key/value access
|
||||
bool getByKey(const char* key, char* value, size_t max_len) override;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace mesh { namespace companion {
|
||||
|
||||
// The radio owns RXEN admission and receiver restoration. This command adapter
|
||||
// does not stop RX, change preferences, or bypass an unread packet/other owner.
|
||||
template<typename Driver, typename ClearStats>
|
||||
template<bool AllowRfRxSet = true, typename Driver, typename ClearStats>
|
||||
bool handleRadioDiagnosticsCommand(Driver& driver, bool radio_available,
|
||||
const char* command, char* reply,
|
||||
size_t capacity, ClearStats clear_stats) {
|
||||
@@ -39,6 +39,7 @@ bool handleRadioDiagnosticsCommand(Driver& driver, bool radio_available,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (!AllowRfRxSet) return false;
|
||||
static const char set_key[] = "set radio.rxps.rfrx_disabled";
|
||||
if (strncmp(command, set_key, sizeof(set_key) - 1) != 0
|
||||
|| (command[sizeof(set_key) - 1] != 0
|
||||
|
||||
@@ -369,7 +369,7 @@ __attribute__((noinline)) bool RadioProfileCLI::handleFastSettings(const char* k
|
||||
if (hasReplyMutation() || fast_hold_) {
|
||||
cli::formatError(reply, capacity, "radio settings held; retry after recovery"); return true;
|
||||
}
|
||||
RadioSwitchTuning next = current;
|
||||
RadioSwitchTuning next = fast_pending_ ? fast_saved_ : current;
|
||||
if (spi) {
|
||||
// Single-digit selections remain strict; 9 labels the 8888888 Hz clock.
|
||||
if (!args[1] && strchr("02489", args[0])) next.spi_mhz = args[0] - '0';
|
||||
@@ -389,8 +389,11 @@ __attribute__((noinline)) bool RadioProfileCLI::handleFastSettings(const char* k
|
||||
}
|
||||
const auto result = radio_->trySetSwitchTuning(next, commitFastSettings, this);
|
||||
if (result == RadioParamApplyResult::APPLIED) snprintf(reply, capacity, "OK - %s=%s (saved)", key, args);
|
||||
else cli::formatError(reply, capacity, result == RadioParamApplyResult::BUSY
|
||||
? "radio busy; retry" : "radio tuning could not be applied/saved");
|
||||
else if (result == RadioParamApplyResult::BUSY && saveFastSettings(next)) {
|
||||
fast_pending_ = true;
|
||||
fast_retry_ms_ = 0;
|
||||
snprintf(reply, capacity, "OK - saved; pending");
|
||||
} else cli::formatError(reply, capacity, "radio tuning could not be applied/saved");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -474,13 +477,24 @@ void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool in
|
||||
bool RadioProfileCLI::savePrimaryPreamble(uint16_t symbols) {
|
||||
if (symbols && (symbols < 8 || symbols > RadioProfiles::MaxPreamble)) return false;
|
||||
if (!radio_ || !radio_->profiles()) return symbols == 0;
|
||||
if (symbols == primary_preamble_) return true;
|
||||
if (symbols == primary_preamble_ && !primary_preamble_dirty_) return true;
|
||||
if (hasReplyMutation()) return false;
|
||||
if (!save(saved_, symbols, cross_)) return false;
|
||||
primary_preamble_ = symbols;
|
||||
primary_preamble_dirty_ = false;
|
||||
primary_preamble_retry_ms_ = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RadioProfileCLI::restorePrimaryPreamble(uint16_t symbols) {
|
||||
if (symbols && (symbols < 8 || symbols > RadioProfiles::MaxPreamble)) return false;
|
||||
if (savePrimaryPreamble(symbols)) return true;
|
||||
primary_preamble_ = symbols;
|
||||
primary_preamble_dirty_ = true;
|
||||
primary_preamble_retry_ms_ = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RadioProfileCLI::acceptsPrimary(float freq, float bw, uint8_t sf, uint8_t cr, uint16_t preamble) const {
|
||||
const auto* profiles = radio_ ? radio_->profiles() : nullptr;
|
||||
if (!profiles) return preamble == 0;
|
||||
@@ -761,16 +775,29 @@ bool RadioProfileCLI::applyReplyMutation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void RadioProfileCLI::loop() {
|
||||
const uint32_t now = millis();
|
||||
void RadioProfileCLI::servicePendingTuning() {
|
||||
if (primary_preamble_dirty_ && (!primary_preamble_retry_ms_
|
||||
|| (int32_t)(millis() - primary_preamble_retry_ms_) >= 0)
|
||||
&& !savePrimaryPreamble(primary_preamble_)) {
|
||||
primary_preamble_retry_ms_ = millis() + 1000;
|
||||
if (!primary_preamble_retry_ms_) primary_preamble_retry_ms_ = 1;
|
||||
}
|
||||
#if MESH_RADIO_SWITCH_TUNING
|
||||
if (fast_pending_ && radio_ && (int32_t)(now - fast_retry_ms_) >= 0) {
|
||||
const uint32_t now = millis();
|
||||
if (fast_pending_ && radio_ && (!fast_retry_ms_ || (int32_t)(now - fast_retry_ms_) >= 0)) {
|
||||
// A saved setting may fail RX verification as well as be temporarily busy.
|
||||
// Avoid repeatedly applying and rolling it back at the main-loop rate.
|
||||
fast_pending_ = radio_->trySetSwitchTuning(fast_saved_) != RadioParamApplyResult::APPLIED;
|
||||
fast_retry_ms_ = now + 1000;
|
||||
const auto result = radio_->trySetSwitchTuning(fast_saved_);
|
||||
fast_pending_ = result != RadioParamApplyResult::APPLIED;
|
||||
fast_retry_ms_ = result == RadioParamApplyResult::FAILED ? now + 1000 : 0;
|
||||
if (result == RadioParamApplyResult::FAILED && !fast_retry_ms_) fast_retry_ms_ = 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void RadioProfileCLI::loop() {
|
||||
const uint32_t now = millis();
|
||||
servicePendingTuning();
|
||||
if (!radio_ || !radio_->profiles()) return;
|
||||
const uint32_t elapsed = now - last_ms_;
|
||||
last_ms_ = now;
|
||||
|
||||
@@ -26,6 +26,8 @@ class RadioProfileCLI {
|
||||
RadioProfileConfig temporary_;
|
||||
RadioProfileConfig pending_temporary_;
|
||||
uint16_t primary_preamble_ = 0;
|
||||
bool primary_preamble_dirty_ = false;
|
||||
uint32_t primary_preamble_retry_ms_ = 0;
|
||||
RadioCrossMode cross_ = RadioCrossMode::Off;
|
||||
uint8_t reply_setting_ = 0; // reserved image byte: 0 = role default
|
||||
bool infrastructure_replies_ = false;
|
||||
@@ -118,6 +120,14 @@ class RadioProfileCLI {
|
||||
static void configureBootTuning(FILESYSTEM*, Radio&) {}
|
||||
#endif
|
||||
void begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool infrastructure_replies = false);
|
||||
void servicePendingTuning();
|
||||
bool hasPendingTuning() const {
|
||||
#if MESH_RADIO_SWITCH_TUNING
|
||||
return fast_pending_ || primary_preamble_dirty_;
|
||||
#else
|
||||
return primary_preamble_dirty_;
|
||||
#endif
|
||||
}
|
||||
void loop();
|
||||
bool handle(const char* command, char* reply, size_t capacity = 160, bool remote_origin = false);
|
||||
// Remote infrastructure commands must keep both RX-origin generations alive
|
||||
@@ -146,6 +156,9 @@ class RadioProfileCLI {
|
||||
// its committed value changes saved intent only, never the running radio.
|
||||
void adoptPrimaryPreamble(uint16_t symbols) { primary_preamble_ = symbols; }
|
||||
bool savePrimaryPreamble(uint16_t symbols);
|
||||
// A failed compensating write still restores the old runtime target. Keep
|
||||
// that target dirty until storage confirms it, even for an identical save.
|
||||
bool restorePrimaryPreamble(uint16_t symbols);
|
||||
bool acceptsPrimary(float freq, float bw, uint8_t sf, uint8_t cr, uint16_t preamble) const;
|
||||
RadioParamApplyResult applyPrimary(float freq, float bw, uint8_t sf, uint8_t cr,
|
||||
bool temporary, uint16_t preamble, const uint32_t* timings = nullptr) {
|
||||
|
||||
@@ -188,6 +188,12 @@ static bool mqttStationWiFiMutationAllowed() {
|
||||
|
||||
void MQTTBridge::beginWiFiStation() {
|
||||
if (!mqttStationWiFiMutationAllowed()) return;
|
||||
if (_wifi_credentials_changed) {
|
||||
WiFi.disconnect();
|
||||
_wifi_credentials_changed = false;
|
||||
_last_wifi_reconnect_attempt = millis();
|
||||
_wifi_reconnect_backoff_attempt = 0;
|
||||
}
|
||||
mesh::wifi::beginStation(_wifi_ssid, _wifi_password);
|
||||
}
|
||||
|
||||
@@ -209,6 +215,10 @@ bool MQTTBridge::prepareWiFiCredentials() {
|
||||
} else if (state != mesh::wifi::CredentialState::Ready) {
|
||||
return _wifi_configured = false;
|
||||
}
|
||||
// Only begin(), after the old worker's stop barrier, replaces this snapshot.
|
||||
// Keep a changed tuple armed across allocation failures until Core 0 joins it.
|
||||
if (_wifi_ssid[0] && (strcmp(_wifi_ssid, credentials.ssid)
|
||||
|| strcmp(_wifi_password, credentials.password))) _wifi_credentials_changed = true;
|
||||
memcpy(_wifi_ssid, credentials.ssid, sizeof(_wifi_ssid));
|
||||
memcpy(_wifi_password, credentials.password, sizeof(_wifi_password));
|
||||
_wifi_power_save = credentials.power_save;
|
||||
@@ -1043,8 +1053,12 @@ void MQTTBridge::releaseRuntimeBuffers() {
|
||||
// ---------------------------------------------------------------------------
|
||||
void MQTTBridge::begin() {
|
||||
#if defined(ESP32)
|
||||
if (mesh::wireless::control().blocked(mesh::wireless::WiFi)) return;
|
||||
if (mesh::wireless::control().blocked(mesh::wireless::WiFi)) {
|
||||
_start_requested = false;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
_start_requested = true;
|
||||
MQTT_DEBUG_PRINTLN("Initializing MQTT Bridge...");
|
||||
|
||||
// Idempotent start: a second begin() would re-run allocation and re-create
|
||||
@@ -1053,6 +1067,7 @@ void MQTTBridge::begin() {
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge running or stopping - begin() ignored");
|
||||
return;
|
||||
}
|
||||
_start_retry_at = millis() + 5000;
|
||||
|
||||
cancelNtpRefresh();
|
||||
_ntp_refresh_retry_at = 0;
|
||||
@@ -1131,6 +1146,7 @@ void MQTTBridge::begin() {
|
||||
#endif
|
||||
if (!wifi_configured) {
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge initialization skipped - WiFi credentials not configured");
|
||||
_start_requested = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1341,6 +1357,8 @@ void MQTTBridge::begin() {
|
||||
// requestStop() - signal only; worker retains ownership until acknowledgment
|
||||
// ---------------------------------------------------------------------------
|
||||
void MQTTBridge::requestStop() {
|
||||
_start_requested = false;
|
||||
_start_retry_at = 0;
|
||||
// A repeated request must not clear an acknowledgment or restart its budget.
|
||||
if (!_initialized || _lifecycle.isStopInProgress()) return;
|
||||
|
||||
@@ -1382,6 +1400,8 @@ void MQTTBridge::requestStop() {
|
||||
// end() - synchronous barrier for OTA and other ownership transfers
|
||||
// ---------------------------------------------------------------------------
|
||||
void MQTTBridge::end() {
|
||||
_start_requested = false;
|
||||
_start_retry_at = 0;
|
||||
MQTT_DEBUG_PRINTLN("Stopping MQTT Bridge...");
|
||||
if (!_initialized) return;
|
||||
requestStop(); // Joins an existing request without re-delivering its signal.
|
||||
@@ -1403,6 +1423,12 @@ void MQTTBridge::end() {
|
||||
finishStopped();
|
||||
}
|
||||
|
||||
void MQTTBridge::requestRestart() {
|
||||
requestStop();
|
||||
_start_requested = true;
|
||||
_start_retry_at = millis() + 1000;
|
||||
}
|
||||
|
||||
void MQTTBridge::finishStopped() {
|
||||
if (!_initialized || _lifecycle.isStopInProgress()) return;
|
||||
// Timezone is inline class storage (_timezone_storage) - nothing to delete.
|
||||
@@ -1601,7 +1627,7 @@ bool MQTTBridge::initializeWiFiInTask() {
|
||||
// the MQTT task's first DNS lookup (getaddrinfo fails until WiFi/DNS recovers).
|
||||
// When already connected, the deferred slot setup still fires in mqttTaskLoop()
|
||||
// because _ntp_synced persists across end() (only _slots_setup_done is reset).
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
if (WiFi.status() != WL_CONNECTED || (_manage_wifi && _wifi_credentials_changed)) {
|
||||
if (_manage_wifi) {
|
||||
beginWiFiStation();
|
||||
}
|
||||
@@ -3592,6 +3618,7 @@ bool MQTTBridge::handleWiFiConnection(unsigned long now) {
|
||||
// An AP can start after this worker initialized. Defer channel enforcement,
|
||||
// reconnect/disconnect and radio power changes until that owner releases it.
|
||||
if (!mqttStationWiFiMutationAllowed()) return false;
|
||||
if (_manage_wifi && _wifi_credentials_changed) beginWiFiStation();
|
||||
#endif
|
||||
wl_status_t current_wifi_status = WiFi.status();
|
||||
#ifdef ESP_PLATFORM
|
||||
@@ -3781,8 +3808,6 @@ bool MQTTBridge::isSlotReady(int index, char* reason_buf, size_t reason_size) co
|
||||
// loop() - Core 1 stop reaping on ESP32; cooperative processing elsewhere
|
||||
// ---------------------------------------------------------------------------
|
||||
void MQTTBridge::loop() {
|
||||
if (!_initialized) return;
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
// Poll only: a slow TLS teardown must not stall radio/UART forwarding or
|
||||
// force-delete the worker. Only end(), the synchronous OTA barrier, owns
|
||||
@@ -3791,9 +3816,16 @@ void MQTTBridge::loop() {
|
||||
&& _stop_acked.load(std::memory_order_acquire)) {
|
||||
_lifecycle.onTaskStopped();
|
||||
finishStopped();
|
||||
return; // Let the owner refresh committed credentials before a restart.
|
||||
}
|
||||
if (!_initialized && _start_requested
|
||||
&& int32_t(millis() - _start_retry_at) >= 0) begin();
|
||||
return;
|
||||
#else
|
||||
if (!_initialized) {
|
||||
if (_start_requested && int32_t(millis() - _start_retry_at) >= 0) begin();
|
||||
return;
|
||||
}
|
||||
unsigned long now = millis();
|
||||
const bool wifi_just_connected = handleWiFiConnection(now);
|
||||
if (wifi_just_connected
|
||||
|
||||
@@ -606,6 +606,8 @@ private:
|
||||
};
|
||||
LifecycleOps _lifecycle_ops;
|
||||
MQTTLifecycle::Coordinator _lifecycle;
|
||||
bool _start_requested = false;
|
||||
uint32_t _start_retry_at = 0;
|
||||
|
||||
// Observer config (MQTT/WiFi/timezone/SNMP/alert), persisted to /mqtt_prefs.
|
||||
MQTTPrefs* _obs = nullptr;
|
||||
@@ -617,6 +619,7 @@ private:
|
||||
char _wifi_password[65] = {};
|
||||
uint8_t _wifi_power_save = 0;
|
||||
bool _wifi_configured = false;
|
||||
bool _wifi_credentials_changed = false;
|
||||
bool prepareWiFiCredentials();
|
||||
void beginWiFiStation();
|
||||
#endif
|
||||
@@ -633,6 +636,10 @@ public:
|
||||
// Core 1: request cooperative teardown without waiting or freeing resources.
|
||||
// Call loop() to reap the acknowledgment; end() remains the OTA stop barrier.
|
||||
void requestStop();
|
||||
void requestRestart();
|
||||
bool isStartPending() const {
|
||||
return _start_requested && (!_initialized || isStopping());
|
||||
}
|
||||
bool isStopping() const { return _lifecycle.isStopInProgress(); }
|
||||
bool isRunning() const override { return _initialized; }
|
||||
// Configured and still scheduling recovery, but transports are quiesced.
|
||||
|
||||
@@ -59,6 +59,11 @@ public:
|
||||
*/
|
||||
virtual bool setRxBoostedGainMode(bool) { return false; }
|
||||
virtual bool supportsRxBoostedGainMode() const { return false; }
|
||||
mesh::RadioParamApplyResult trySetRxBoostedGainMode(bool) { return mesh::RadioParamApplyResult::FAILED; }
|
||||
mesh::RadioParamApplyResult trySetRxPowerSaving(bool enabled, uint32_t, uint32_t) {
|
||||
return enabled ? mesh::RadioParamApplyResult::FAILED : mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
mesh::RadioParamApplyResult trySetRxPowerSavingRfRxDisabled(bool) { return mesh::RadioParamApplyResult::FAILED; }
|
||||
virtual bool getRxBoostedGainMode() const { return false; }
|
||||
|
||||
// ESP-NOW has no LoRa RX power-saving watchdog or noise-floor calibration.
|
||||
|
||||
@@ -1584,8 +1584,13 @@ size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
ADVERT_RESTART_DELAY)) {
|
||||
if (pServer->getConnectedCount() == 0) {
|
||||
BLE_DEBUG_PRINTLN("SerialBLEInterface -> re-starting advertising");
|
||||
#if defined(MESH_USE_NIMBLE_ARDUINO)
|
||||
_adv_restart_pending = !pServer->getAdvertising()->start();
|
||||
if (_adv_restart_pending) _adv_restart_started = now;
|
||||
#else
|
||||
pServer->getAdvertising()->start(); // re-Start advertising
|
||||
_adv_restart_pending = false;
|
||||
#endif
|
||||
} else {
|
||||
// A disconnect can take longer than the normal restart delay. Keep the
|
||||
// restart armed instead of losing it while the controller still reports
|
||||
|
||||
@@ -851,6 +851,7 @@ static bool scanSetupNetworks() {
|
||||
}
|
||||
|
||||
bool WebConfigServer::startSetupMode(char reply[]) {
|
||||
_start_retryable = false;
|
||||
if (mesh::wireless::control().blocked(mesh::wireless::WiFi)) {
|
||||
strcpy(reply, "Error: WiFi disabled; use set wifi on or set 2.4ghz on");
|
||||
return false;
|
||||
@@ -944,6 +945,7 @@ bool WebConfigServer::startSetupMode(char reply[]) {
|
||||
if (!promote_lan) stopOwnedWiFiRadio();
|
||||
refreshNetworkPowerHold(millis());
|
||||
strcpy(reply, "Err: failed to start AP");
|
||||
_start_retryable = true;
|
||||
return false;
|
||||
}
|
||||
delay(100); // let the AP netif settle before reading its IP
|
||||
@@ -1028,6 +1030,7 @@ bool WebConfigServer::beginSavedStation(uint32_t now) {
|
||||
}
|
||||
|
||||
bool WebConfigServer::startAutoMode(char reply[]) {
|
||||
_start_retryable = false;
|
||||
if (mesh::wireless::control().blocked(mesh::wireless::WiFi)) {
|
||||
strcpy(reply, "Error: WiFi disabled; use set wifi on or set 2.4ghz on");
|
||||
return false;
|
||||
@@ -1042,6 +1045,7 @@ bool WebConfigServer::startAutoMode(char reply[]) {
|
||||
const bool manages_station = _owns_wifi || !_runtime_wifi_ownership_managed;
|
||||
if (manages_station && !beginSavedStation(millis())) {
|
||||
strcpy(reply, "Err: failed to reset WiFi station protocol");
|
||||
_start_retryable = true;
|
||||
return false;
|
||||
}
|
||||
_mode = MODE_CONNECTING;
|
||||
@@ -1053,6 +1057,56 @@ bool WebConfigServer::startAutoMode(char reply[]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WebConfigServer::requestStart(bool force_ap, char reply[]) {
|
||||
_start_retryable = false;
|
||||
if (mesh::wireless::control().blocked(mesh::wireless::WiFi)) {
|
||||
_start_mode = MODE_OFF;
|
||||
strcpy(reply, "Error: WiFi disabled; use set wifi on or set 2.4ghz on");
|
||||
return false;
|
||||
}
|
||||
if ((force_ap || !_wifi_ssid[0])
|
||||
&& !mesh::wifi::accessPointCompatibleWithLongRange()) {
|
||||
_start_mode = MODE_OFF;
|
||||
strcpy(reply, "Error: stop ESP-NOW before starting a setup AP");
|
||||
return false;
|
||||
}
|
||||
const Mode requested = force_ap ? MODE_SETUP : MODE_CONNECTING;
|
||||
if (!_stopping) {
|
||||
if (force_ap ? startSetupMode(reply) : startAutoMode(reply)) {
|
||||
_start_mode = MODE_OFF;
|
||||
_start_retry_at = 0;
|
||||
_start_retry_delay = 5000;
|
||||
return true;
|
||||
}
|
||||
if (!_start_retryable) {
|
||||
_start_mode = MODE_OFF;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_start_mode = requested;
|
||||
_start_retry_at = WebConfigBatch::scheduleAt(millis(), _start_retry_delay);
|
||||
if (_start_retry_delay < 60000) _start_retry_delay *= 2;
|
||||
if (_start_retry_delay > 60000) _start_retry_delay = 60000;
|
||||
strcpy(reply, "OK - WebConfig start pending; retrying automatically");
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebConfigServer::requestRestart() {
|
||||
if (!isRunning() && !isStartPending()) return;
|
||||
requestStop();
|
||||
_start_mode = MODE_CONNECTING;
|
||||
_start_retry_delay = 5000;
|
||||
_start_retry_at = WebConfigBatch::scheduleAt(millis(), 1000);
|
||||
}
|
||||
|
||||
void WebConfigServer::serviceStartRetry(uint32_t now) {
|
||||
if (!isStartPending() || _stopping
|
||||
|| !WebConfigBatch::deadlineReached(now, _start_retry_at)) return;
|
||||
char reply[160];
|
||||
requestStart(_start_mode == MODE_SETUP, reply);
|
||||
mesh::usbDebugPort().println(reply);
|
||||
}
|
||||
|
||||
bool WebConfigServer::prepareStationOwner() {
|
||||
if (_stopping) return false;
|
||||
updateWiFiOwnership(false);
|
||||
@@ -1121,6 +1175,9 @@ void WebConfigServer::createServer() {
|
||||
}
|
||||
|
||||
void WebConfigServer::requestStop() {
|
||||
_start_mode = MODE_OFF;
|
||||
_start_retry_at = 0;
|
||||
_start_retry_delay = 5000;
|
||||
if (_mode == MODE_OFF && !_stopping) return;
|
||||
detachRoutes();
|
||||
if (_server) _server->end();
|
||||
@@ -1133,6 +1190,8 @@ void WebConfigServer::requestStop() {
|
||||
}
|
||||
|
||||
bool WebConfigServer::stopForOTA(char reply[]) {
|
||||
_start_mode = MODE_OFF;
|
||||
_start_retry_at = 0;
|
||||
if (!isRunning() && !isStopping()) return true;
|
||||
// Keep a STA interface alive while removing our setup AP. Switching the
|
||||
// last interface off and immediately creating the OTA AP races ESP32 netif
|
||||
@@ -1292,6 +1351,7 @@ void WebConfigServer::tick(uint32_t now) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
serviceStartRetry(now);
|
||||
if (_mode == MODE_OFF) return;
|
||||
|
||||
if (!_board_cmds_probed) probeBoardCommands();
|
||||
@@ -1345,6 +1405,10 @@ void WebConfigServer::tick(uint32_t now) {
|
||||
char ignored[160];
|
||||
if (startSetupMode(ignored)) {
|
||||
_retry_saved_wifi_in_setup = retry_saved_wifi;
|
||||
} else if (_start_retryable) {
|
||||
// Keep automatic recovery alive if the AP netif was temporarily busy.
|
||||
_mode = MODE_CONNECTING;
|
||||
_connect_deadline = WebConfigBatch::scheduleAt(millis(), 30000);
|
||||
}
|
||||
mesh::usbDebugPort().println(ignored);
|
||||
}
|
||||
|
||||
@@ -265,6 +265,10 @@ public:
|
||||
bool startSetupMode(char reply[]); // open SoftAP + DNS captive portal
|
||||
bool startLanMode(char reply[]); // bind to existing STA connection
|
||||
bool startAutoMode(char reply[]); // use saved WiFi, or setup AP when absent
|
||||
// Accept a start through asynchronous teardown or a transient SDK failure.
|
||||
bool requestStart(bool force_ap, char reply[]);
|
||||
void requestRestart(); // coalesce saved credential changes
|
||||
bool isStartPending() const { return _start_mode != MODE_OFF; }
|
||||
bool reloadStandaloneWiFi();
|
||||
void requestStop(); // stop listening and detach this session
|
||||
// Release HTTP/DNS for the updater without delayed teardown taking its
|
||||
@@ -332,6 +336,10 @@ private:
|
||||
SemaphoreHandle_t _mux;
|
||||
Mode _mode = MODE_OFF;
|
||||
bool _stopping = false;
|
||||
Mode _start_mode = MODE_OFF;
|
||||
uint32_t _start_retry_at = 0;
|
||||
uint32_t _start_retry_delay = 5000;
|
||||
bool _start_retryable = false;
|
||||
bool _keep_wifi_on_stop = false;
|
||||
bool _was_setup_ap = false;
|
||||
bool _initial_setup = false;
|
||||
@@ -421,6 +429,7 @@ private:
|
||||
void noteNetworkActivity();
|
||||
void refreshNetworkPowerHold(uint32_t now);
|
||||
bool beginSavedStation(uint32_t now);
|
||||
void serviceStartRetry(uint32_t now);
|
||||
void registerRoutes();
|
||||
typedef void (WebConfigServer::*RequestHandler)(AsyncWebServerRequest*);
|
||||
static void dispatchRequest(AsyncWebServerRequest* req, RequestHandler handler);
|
||||
|
||||
@@ -131,19 +131,24 @@ public:
|
||||
}
|
||||
|
||||
bool configSideDetectors(const uint8_t* sideDetSFs, uint8_t num, float bw) override {
|
||||
if (_profiles.enabled()) return false;
|
||||
return tryConfigSideDetectors(sideDetSFs, num, bw) == mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
|
||||
mesh::RadioParamApplyResult tryConfigSideDetectors(const uint8_t* sideDetSFs, uint8_t num, float bw) override {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
if (_profiles.enabled()) return Result::FAILED;
|
||||
const uint8_t primary_sf = getSpreadingFactor();
|
||||
float active_bw = _params_valid ? _cur_bw : ((CustomLR2021 *)_radio)->getBandwidthKhz();
|
||||
if (active_bw <= 0.0f) active_bw = bw;
|
||||
if (!mesh::lr2021::validateSideDetectorSFs(sideDetSFs, num, primary_sf, active_bw)) {
|
||||
return false;
|
||||
return Result::FAILED;
|
||||
}
|
||||
|
||||
LR2021LoRaSideDetector_t tmp[mesh::lr2021::MAX_SIDE_DETECTORS] = {};
|
||||
buildSideDetectorConfig(sideDetSFs, num, active_bw, tmp);
|
||||
|
||||
uint8_t resume_rx = beginReconfigure();
|
||||
if (resume_rx > 1) return false;
|
||||
if (resume_rx > 1) return resume_rx == 2 ? Result::BUSY : Result::FAILED;
|
||||
|
||||
int16_t status = ((CustomLR2021 *)_radio)->setSideDetector(num > 0 ? tmp : nullptr, num);
|
||||
MESH_DEBUG_PRINTLN("setSideDetector() returned %d", status);
|
||||
@@ -165,7 +170,7 @@ public:
|
||||
}
|
||||
|
||||
endReconfigure(resume_rx);
|
||||
return status == RADIOLIB_ERR_NONE;
|
||||
return status == RADIOLIB_ERR_NONE ? Result::APPLIED : Result::FAILED;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
@@ -220,16 +220,22 @@ public:
|
||||
}
|
||||
|
||||
bool setRxPowerSavingRfRxDisabled(bool disabled) override {
|
||||
return trySetRxPowerSavingRfRxDisabled(disabled) == mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
|
||||
mesh::RadioParamApplyResult trySetRxPowerSavingRfRxDisabled(bool disabled) override {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
if (!supportsRxPowerSavingRfRxDisable()) return Result::FAILED;
|
||||
#if MESH_ENABLE_TX_BEACONS
|
||||
if (isBeaconRadioOwned()) return false;
|
||||
if (isBeaconRadioOwned()) return Result::BUSY;
|
||||
#endif
|
||||
if (isCarrierWaveActive()) return false;
|
||||
if (!supportsRxPowerSavingRfRxDisable()) return false;
|
||||
if (isCarrierWaveActive()) return Result::BUSY;
|
||||
if (isRxPowerSavingRfRxDisabled() == disabled) return Result::APPLIED;
|
||||
|
||||
// Do not stop duty-cycle RX or change its policy before admission. A BUSY
|
||||
// sleep window, TX, or unread RX completion still owns the current mode.
|
||||
const uint8_t resume_rx = beginReconfigure();
|
||||
if (resume_rx > 1) return false;
|
||||
if (resume_rx > 1) return resume_rx == 2 ? Result::BUSY : Result::FAILED;
|
||||
|
||||
const bool previous = isRxPowerSavingRfRxDisabled();
|
||||
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(disabled);
|
||||
@@ -238,9 +244,9 @@ public:
|
||||
// startRecv already retained its bounded retry/backoff. That retry must
|
||||
// use the old RXEN policy; never report a failed RX restoration as OK.
|
||||
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(previous);
|
||||
return false;
|
||||
return Result::FAILED;
|
||||
}
|
||||
return true;
|
||||
return Result::APPLIED;
|
||||
}
|
||||
|
||||
bool isRxPowerSavingRfRxDisabled() const override {
|
||||
|
||||
@@ -554,6 +554,7 @@ bool RadioLibWrapper::restoreAfterDeepInit() {
|
||||
if (_beacon_owned) cacheBeaconRestoreParams();
|
||||
#endif
|
||||
_dbm_applied = false;
|
||||
_rx_boosted_gain_applied = false;
|
||||
if (!radioDeepInit()) return false;
|
||||
|
||||
_rx_ps_armed = false;
|
||||
@@ -575,7 +576,8 @@ bool RadioLibWrapper::restoreAfterDeepInit() {
|
||||
restored = _dbm_applied && restored;
|
||||
}
|
||||
if (_rx_boosted_gain_valid) {
|
||||
restored = applyRxBoostedGainMode(_cur_rx_boosted_gain) && restored;
|
||||
_rx_boosted_gain_applied = applyRxBoostedGainMode(_cur_rx_boosted_gain);
|
||||
restored = _rx_boosted_gain_applied && restored;
|
||||
}
|
||||
recalibrateNoiseFloor();
|
||||
if (restored) _profile_repair_required = false;
|
||||
@@ -1426,11 +1428,25 @@ mesh::RadioParamApplyResult RadioLibWrapper::trySetPrimaryParams(const mesh::Rad
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::setRxBoostedGainMode(bool enabled) {
|
||||
uint8_t resume_rx = beginReconfigure();
|
||||
if (resume_rx > 1) return false;
|
||||
return trySetRxBoostedGainMode(enabled) == mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
|
||||
const bool gain_changed = !_rx_boosted_gain_valid || _cur_rx_boosted_gain != enabled;
|
||||
mesh::RadioParamApplyResult RadioLibWrapper::trySetRxBoostedGainMode(bool enabled) {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
if (!supportsRxBoostedGainMode()) return Result::FAILED;
|
||||
#if MESH_ENABLE_TX_BEACONS
|
||||
if (_beacon_owned) return Result::BUSY;
|
||||
#endif
|
||||
if (_cw_active) return Result::BUSY;
|
||||
if (_rx_boosted_gain_valid && _rx_boosted_gain_applied
|
||||
&& _cur_rx_boosted_gain == enabled && !_profile_repair_required) return Result::APPLIED;
|
||||
const uint8_t resume_rx = beginReconfigure();
|
||||
if (resume_rx > 1) return resume_rx == 2 ? Result::BUSY : Result::FAILED;
|
||||
|
||||
const bool gain_changed = !_rx_boosted_gain_valid || !_rx_boosted_gain_applied
|
||||
|| _cur_rx_boosted_gain != enabled;
|
||||
bool success = applyRxBoostedGainMode(enabled);
|
||||
_rx_boosted_gain_applied = success;
|
||||
if (success) {
|
||||
_cur_rx_boosted_gain = enabled;
|
||||
_rx_boosted_gain_valid = true;
|
||||
@@ -1438,7 +1454,7 @@ bool RadioLibWrapper::setRxBoostedGainMode(bool enabled) {
|
||||
}
|
||||
|
||||
endReconfigure(resume_rx);
|
||||
return success;
|
||||
return success ? Result::APPLIED : Result::FAILED;
|
||||
}
|
||||
|
||||
void RadioLibWrapper::idle() {
|
||||
@@ -1565,7 +1581,7 @@ bool RadioLibWrapper::resetAGCIfIdle(bool refresh_floor) {
|
||||
doResetAGC();
|
||||
state = STATE_IDLE; // trigger a startReceive()
|
||||
const bool gain_restored = !_rx_boosted_gain_valid
|
||||
|| applyRxBoostedGainMode(_cur_rx_boosted_gain);
|
||||
|| (_rx_boosted_gain_applied = applyRxBoostedGainMode(_cur_rx_boosted_gain));
|
||||
|
||||
// Adaptive maintenance restores the same RF path. A valid, idle baseline
|
||||
// keeps its existing refresh deadline instead of spending another complete
|
||||
@@ -2097,7 +2113,8 @@ mesh::MorseRxResult RadioLibWrapper::startMorseRx(float frequency_mhz,
|
||||
state = STATE_IDLE;
|
||||
_board->setRadioTestActive(true);
|
||||
if (prepareMorseReceiver(frequency_mhz) != RADIOLIB_ERR_NONE
|
||||
|| (_rx_boosted_gain_valid && !applyRxBoostedGainMode(_cur_rx_boosted_gain))
|
||||
|| (_rx_boosted_gain_valid
|
||||
&& !(_rx_boosted_gain_applied = applyRxBoostedGainMode(_cur_rx_boosted_gain)))
|
||||
|| startMorseReceiver() != RADIOLIB_ERR_NONE) {
|
||||
if (allocated) delete receiver;
|
||||
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
|
||||
@@ -2640,26 +2657,31 @@ bool RadioLibWrapper::getNoiseFloorWakeDelay(uint32_t now, uint32_t& delay_ms) c
|
||||
|
||||
// RX PowerSaving
|
||||
bool RadioLibWrapper::setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) {
|
||||
return trySetRxPowerSaving(enabled, rx_us, sleep_us) == mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
|
||||
mesh::RadioParamApplyResult RadioLibWrapper::trySetRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) {
|
||||
using Result = mesh::RadioParamApplyResult;
|
||||
if (enabled && (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us)
|
||||
|| !supportsRxPowerSaving())) return Result::FAILED;
|
||||
#if MESH_ENABLE_TX_BEACONS
|
||||
if (_beacon_owned) return false;
|
||||
if (_beacon_owned) return Result::BUSY;
|
||||
#endif
|
||||
if (_cw_active) return false;
|
||||
if (enabled && (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us))) {
|
||||
return false;
|
||||
}
|
||||
// A rejected request must not become the policy restored after radio2 ends.
|
||||
if (enabled && !supportsRxPowerSaving()) return false;
|
||||
if (_cw_active) return Result::BUSY;
|
||||
if (_profile_rxps_suspended) {
|
||||
_profile_saved_rxps = enabled;
|
||||
_rx_ps_rx_us = rx_us;
|
||||
_rx_ps_sleep_us = sleep_us;
|
||||
_rx_ps_arm_retry.reset();
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
return true;
|
||||
return Result::APPLIED;
|
||||
}
|
||||
|
||||
if (!_profile_repair_required && _rx_ps_enabled == enabled
|
||||
&& _rx_ps_rx_us == rx_us && _rx_ps_sleep_us == sleep_us) return Result::APPLIED;
|
||||
|
||||
uint8_t resume_rx = beginReconfigure();
|
||||
if (resume_rx > 1) return false;
|
||||
if (resume_rx > 1) return resume_rx == 2 ? Result::BUSY : Result::FAILED;
|
||||
|
||||
_rx_ps_enabled = enabled;
|
||||
_rx_ps_rx_us = rx_us;
|
||||
@@ -2669,7 +2691,7 @@ bool RadioLibWrapper::setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sl
|
||||
_rx_ps_continuous_fallback = enabled
|
||||
&& rxPowerSavingUsesContinuousFallback(rx_us, sleep_us);
|
||||
endReconfigure(resume_rx);
|
||||
return true;
|
||||
return Result::APPLIED;
|
||||
}
|
||||
|
||||
RxPowerSavingStatus RadioLibWrapper::getRxPowerSavingStatus() const {
|
||||
|
||||
@@ -109,6 +109,7 @@ protected:
|
||||
bool _cur_rx_boosted_gain;
|
||||
bool _params_valid, _dbm_valid, _rx_boosted_gain_valid;
|
||||
bool _dbm_applied = false;
|
||||
bool _rx_boosted_gain_applied = false;
|
||||
mesh::RadioProfiles _profiles;
|
||||
uint8_t _active_profile = 0;
|
||||
uint16_t _physical_preamble = 0;
|
||||
@@ -543,6 +544,7 @@ public:
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
bool setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) override;
|
||||
mesh::RadioParamApplyResult trySetRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us);
|
||||
bool supportsRxPowerSaving() const override { return SupportsRxPowerSaving; }
|
||||
RxPowerSavingStatus getRxPowerSavingStatus() const;
|
||||
virtual uint32_t rxPowerSavingTransitionUs() const { return rxPowerSavingDefaultTransitionUs(); }
|
||||
@@ -555,6 +557,9 @@ public:
|
||||
}
|
||||
virtual bool supportsRxPowerSavingRfRxDisable() const { return false; }
|
||||
virtual bool setRxPowerSavingRfRxDisabled(bool) { return false; }
|
||||
virtual mesh::RadioParamApplyResult trySetRxPowerSavingRfRxDisabled(bool) {
|
||||
return mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
virtual bool isRxPowerSavingRfRxDisabled() const { return false; }
|
||||
bool isChannelActive();
|
||||
|
||||
@@ -681,10 +686,14 @@ public:
|
||||
float packetScore(float snr, int packet_len) override { return packetScoreInt(snr, 10, packet_len); } // assume sf=10
|
||||
|
||||
bool setRxBoostedGainMode(bool enabled);
|
||||
mesh::RadioParamApplyResult trySetRxBoostedGainMode(bool enabled);
|
||||
virtual bool supportsRxBoostedGainMode() const { return false; }
|
||||
virtual bool getRxBoostedGainMode() const { return false; }
|
||||
|
||||
virtual bool configSideDetectors(const uint8_t sideDetSFs[], uint8_t num, float bw) { return false; }
|
||||
virtual mesh::RadioParamApplyResult tryConfigSideDetectors(const uint8_t*, uint8_t, float) {
|
||||
return mesh::RadioParamApplyResult::FAILED;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -87,6 +87,11 @@ public:
|
||||
void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
|
||||
bool setRxBoostedGainMode(bool /*enabled*/) { return false; }
|
||||
bool supportsRxBoostedGainMode() const { return false; }
|
||||
mesh::RadioParamApplyResult trySetRxBoostedGainMode(bool) { return mesh::RadioParamApplyResult::FAILED; }
|
||||
mesh::RadioParamApplyResult trySetRxPowerSaving(bool enabled, uint32_t, uint32_t) {
|
||||
return enabled ? mesh::RadioParamApplyResult::FAILED : mesh::RadioParamApplyResult::APPLIED;
|
||||
}
|
||||
mesh::RadioParamApplyResult trySetRxPowerSavingRfRxDisabled(bool) { return mesh::RadioParamApplyResult::FAILED; }
|
||||
bool getRxBoostedGainMode() const { return false; }
|
||||
uint32_t getRxPsWatchdogSoftCount() const { return 0; }
|
||||
uint32_t getRxPsWatchdogHardCount() const { return 0; }
|
||||
|
||||
Reference in new issue
Block a user