Clamp bw for malformed use_preset false with empty bandwidth

This commit is contained in:
Ben Meadors
2026-07-09 10:30:11 -05:00
parent 168f669e61
commit 515fe8b94f
3 changed files with 124 additions and 0 deletions
+10
View File
@@ -190,6 +190,16 @@ static inline uint16_t bwKHzToCode(float bwKHz)
return (uint16_t)(bwKHz + 0.5f);
}
/// Clamp a bandwidth *code* (the on-wire config.lora.bandwidth value) to a usable value.
/// A code of 0 (proto default / unset) returns the default bandwidth's code; any other value
/// is returned unchanged (RadioLib validates the exact discrete bandwidth downstream).
static inline uint16_t clampBandwidthCode(uint16_t bwCode)
{
if (bwCode == 0)
return bwKHzToCode(LORA_BW_DEFAULT_KHZ);
return bwCode;
}
static inline void modemPresetToParams(meshtastic_Config_LoRaConfig_ModemPreset preset, bool wideLora, float &bwKHz, uint8_t &sf,
uint8_t &cr)
{
+11
View File
@@ -916,6 +916,17 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c, bool fromOthers)
validatedLora.spread_factor = LORA_SF_DEFAULT;
}
// A custom (non-preset) config that leaves bandwidth at its proto zero-value otherwise slips
// through validateConfigLora() and persists as 0, while the radio silently falls back to the
// default (config.lora.bandwidth then reads back 0 even though the radio runs at 250kHz).
// Coerce it here like coding_rate/spread_factor so the stored config matches the radio. In
// preset mode bandwidth 0 is expected (the preset supplies it), so leave it untouched.
const uint16_t clampedBandwidth = clampBandwidthCode(validatedLora.bandwidth);
if (!validatedLora.use_preset && validatedLora.bandwidth != clampedBandwidth) {
LOG_WARN("Invalid bandwidth %d, setting to %d", validatedLora.bandwidth, clampedBandwidth);
validatedLora.bandwidth = clampedBandwidth;
}
// If we're setting a new region, check the region is valid and then init the region or discard the change
if (validatedLora.region != myRegion->code) {
// Region has changed so check whether it is valid for e.g. licensing conditions and if the lora config is valid