Merge RX duty-cycle power saving into keymindCascade
This commit is contained in:
commit
0ede4e2066
18 files changed
+865
-52
No files matched your search
+250
-4
@@ -313,6 +313,80 @@ static const char* retryPresetName(uint8_t preset) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool isValidRxPowerSavingPeriod(uint32_t us) {
|
||||
return us >= RX_POWERSAVING_MIN_PERIOD_US && us <= RX_POWERSAVING_MAX_PERIOD_US;
|
||||
}
|
||||
|
||||
// MeshCore preamble convention used for the RX powersaving timing calculation.
|
||||
// Must stay in sync with RadioLibWrapper::preambleLengthForSF() (the value the
|
||||
// radio actually transmits); kept local here because CommonCLI is radio-agnostic.
|
||||
static uint16_t rxPowerSavingPreambleForSF(uint8_t sf) {
|
||||
return sf <= 8 ? 32 : 16;
|
||||
}
|
||||
|
||||
static bool isNumeric(const char* sp) {
|
||||
if (!sp || !*sp) return false;
|
||||
while (*sp) {
|
||||
if (*sp < '0' || *sp > '9') return false;
|
||||
sp++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t ceilPositiveFloat(float value) {
|
||||
uint32_t rounded = (uint32_t)value;
|
||||
return value > (float)rounded ? rounded + 1 : rounded;
|
||||
}
|
||||
|
||||
static bool calcRxPowerSavingLevel(uint32_t level, uint8_t sf, float bw, uint32_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us) {
|
||||
if (level < 1 || level > 10 || sf < 5 || sf > 12 || bw <= 0.0f || (preamble != 16 && preamble != 32)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const float symbol_us = (1000.0f * (float)(1UL << sf)) / bw;
|
||||
const float amount = (float)(level - 1) / 9.0f;
|
||||
const float rx_start_symbols = preamble == 16 ? 12.0f : 16.0f;
|
||||
const float sleep_start_symbols = preamble == 16 ? 2.0f : 15.0f;
|
||||
const float rx_edge_symbols = 8.0f;
|
||||
const float sleep_edge_symbols = (float)preamble + 4.25f - 8.0f;
|
||||
|
||||
const float rx_symbols = rx_start_symbols + amount * (rx_edge_symbols - rx_start_symbols);
|
||||
const float sleep_symbols = sleep_start_symbols + amount * (sleep_edge_symbols - sleep_start_symbols);
|
||||
|
||||
*rx_us = ceilPositiveFloat(rx_symbols * symbol_us);
|
||||
*sleep_us = (uint32_t)(sleep_symbols * symbol_us);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ensureRxPowerSavingDefaults(NodePrefs* prefs) {
|
||||
if (!isValidRxPowerSavingPeriod(prefs->rx_ps_rx_us)) {
|
||||
prefs->rx_ps_rx_us = RX_POWERSAVING_DEFAULT_RX_US;
|
||||
}
|
||||
if (!isValidRxPowerSavingPeriod(prefs->rx_ps_sleep_us)) {
|
||||
prefs->rx_ps_sleep_us = RX_POWERSAVING_DEFAULT_SLEEP_US;
|
||||
}
|
||||
}
|
||||
|
||||
// Recomputes rx_ps_rx_us/rx_ps_sleep_us from the stored level and the current
|
||||
// radio SF/BW. No-op (returns false) for manual timings (rx_ps_level == 0).
|
||||
// Lets level-based RX powersaving auto-retune when SF/BW change.
|
||||
static bool recalcRxPowerSavingFromLevel(NodePrefs* prefs) {
|
||||
if (prefs->rx_ps_level < 1 || prefs->rx_ps_level > 10) return false; // manual: nothing to recompute
|
||||
uint32_t preamble = prefs->rx_ps_preamble ? prefs->rx_ps_preamble
|
||||
: rxPowerSavingPreambleForSF(prefs->sf);
|
||||
uint32_t rx_us, sleep_us;
|
||||
if (!calcRxPowerSavingLevel(prefs->rx_ps_level, prefs->sf, prefs->bw, preamble, &rx_us, &sleep_us)) {
|
||||
return false;
|
||||
}
|
||||
if (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us)) {
|
||||
return false;
|
||||
}
|
||||
prefs->rx_ps_rx_us = rx_us;
|
||||
prefs->rx_ps_sleep_us = sleep_us;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void markDirectRetryPrefsValid(NodePrefs* prefs) {
|
||||
prefs->direct_retry_prefs_magic[0] = DIRECT_RETRY_PREFS_MAGIC_0;
|
||||
prefs->direct_retry_prefs_magic[1] = DIRECT_RETRY_PREFS_MAGIC_1;
|
||||
@@ -763,6 +837,11 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
// (upstream defaults: FEM RX gain on, CAD off) — overwritten below if present.
|
||||
_prefs->radio_fem_rxgain = 1;
|
||||
_prefs->cad_enabled = 0;
|
||||
_prefs->rx_powersaving_enabled = 0;
|
||||
_prefs->rx_ps_rx_us = RX_POWERSAVING_DEFAULT_RX_US;
|
||||
_prefs->rx_ps_sleep_us = RX_POWERSAVING_DEFAULT_SLEEP_US;
|
||||
_prefs->rx_ps_level = 0;
|
||||
_prefs->rx_ps_preamble = 0;
|
||||
#if defined(ENABLE_OTA)
|
||||
// OTA settings were appended after Keymind's retry/flood tail. Initialize them
|
||||
// before reading so older and legacy preference files remain conservative.
|
||||
@@ -987,6 +1066,36 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
}
|
||||
// next: 811
|
||||
#endif
|
||||
// RXPS is stored at a fixed offset after the reserved OTA tail. This keeps
|
||||
// both old OTA and non-OTA /com_prefs files unambiguous.
|
||||
bool has_rxps_tail = file.available() >= 11;
|
||||
#if !defined(ENABLE_OTA)
|
||||
const size_t ota_tail_size = 136;
|
||||
const size_t padded_rxps_tail_size = sizeof(_prefs->rx_powersaving_enabled)
|
||||
+ sizeof(_prefs->rx_ps_rx_us) + sizeof(_prefs->rx_ps_sleep_us)
|
||||
+ sizeof(_prefs->rx_ps_level) + sizeof(_prefs->rx_ps_preamble);
|
||||
if (file.available() >= (int)(ota_tail_size + padded_rxps_tail_size)) {
|
||||
size_t remaining = ota_tail_size;
|
||||
while (remaining > 0) {
|
||||
size_t n = remaining > sizeof(pad) ? sizeof(pad) : remaining;
|
||||
file.read(pad, n);
|
||||
remaining -= n;
|
||||
}
|
||||
has_rxps_tail = true;
|
||||
} else {
|
||||
has_rxps_tail = false;
|
||||
}
|
||||
#endif
|
||||
const size_t rxps_tail_size = sizeof(_prefs->rx_powersaving_enabled)
|
||||
+ sizeof(_prefs->rx_ps_rx_us) + sizeof(_prefs->rx_ps_sleep_us)
|
||||
+ sizeof(_prefs->rx_ps_level) + sizeof(_prefs->rx_ps_preamble);
|
||||
if (has_rxps_tail && file.available() >= (int)rxps_tail_size) {
|
||||
file.read((uint8_t *)&_prefs->rx_powersaving_enabled, sizeof(_prefs->rx_powersaving_enabled));
|
||||
file.read((uint8_t *)&_prefs->rx_ps_rx_us, sizeof(_prefs->rx_ps_rx_us));
|
||||
file.read((uint8_t *)&_prefs->rx_ps_sleep_us, sizeof(_prefs->rx_ps_sleep_us));
|
||||
file.read((uint8_t *)&_prefs->rx_ps_level, sizeof(_prefs->rx_ps_level));
|
||||
file.read((uint8_t *)&_prefs->rx_ps_preamble, sizeof(_prefs->rx_ps_preamble));
|
||||
}
|
||||
}
|
||||
|
||||
// sanitise bad pref values
|
||||
@@ -1071,6 +1180,13 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
if (_prefs->ota_max_hops > 8) _prefs->ota_max_hops = 3; // 0=direct only; cap absurd reach
|
||||
if (_prefs->ota_signer_count > 4) _prefs->ota_signer_count = 0; // corrupt count -> drop keys
|
||||
#endif
|
||||
_prefs->rx_powersaving_enabled = constrain(_prefs->rx_powersaving_enabled, 0, 1);
|
||||
_prefs->rx_ps_level = constrain(_prefs->rx_ps_level, 0, 10);
|
||||
if (_prefs->rx_ps_preamble != 16 && _prefs->rx_ps_preamble != 32) {
|
||||
_prefs->rx_ps_preamble = 0; // 0 = auto (derive from SF)
|
||||
}
|
||||
ensureRxPowerSavingDefaults(_prefs);
|
||||
recalcRxPowerSavingFromLevel(_prefs); // retune level-based timings to the loaded SF/BW
|
||||
|
||||
file.close();
|
||||
}
|
||||
@@ -1132,9 +1248,8 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->discovery_mod_timestamp, sizeof(_prefs->discovery_mod_timestamp)); // 162
|
||||
file.write((uint8_t *)&_prefs->adc_multiplier, sizeof(_prefs->adc_multiplier)); // 166
|
||||
file.write((uint8_t *)_prefs->owner_info, sizeof(_prefs->owner_info)); // 170
|
||||
// MQTT/observer settings are stored in /mqtt_prefs, not here. No zero-gap is
|
||||
// written anymore — /com_prefs holds the upstream fields plus the keymind
|
||||
// retry tail below (loadPrefsInt reads the same sequence; keep in sync).
|
||||
// MQTT/observer settings are stored in /mqtt_prefs, not here. /com_prefs
|
||||
// holds the upstream fields plus the keymind retry tail below.
|
||||
file.write((uint8_t *)&_prefs->rx_boosted_gain, sizeof(_prefs->rx_boosted_gain)); // 290
|
||||
file.write((uint8_t *)&_prefs->flood_max_unscoped, sizeof(_prefs->flood_max_unscoped)); // 291
|
||||
file.write((uint8_t *)&_prefs->flood_max_advert, sizeof(_prefs->flood_max_advert)); // 292
|
||||
@@ -1178,7 +1293,28 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->ota_advert_interval, sizeof(_prefs->ota_advert_interval)); // 808
|
||||
file.write((uint8_t *)&_prefs->ota_max_hops, sizeof(_prefs->ota_max_hops)); // 810
|
||||
// next: 811
|
||||
#else
|
||||
// Reserve the OTA tail so RXPS has the same offset in every build. Write
|
||||
// OTA's normal defaults so a later OTA-enabled firmware reads sane values.
|
||||
file.write(pad, 3); // autofetch, autoinstall, signer_count
|
||||
for (size_t remaining = 128; remaining > 0; ) {
|
||||
size_t n = remaining > sizeof(pad) ? sizeof(pad) : remaining;
|
||||
file.write(pad, n);
|
||||
remaining -= n;
|
||||
}
|
||||
const uint16_t ota_checkpoint_blocks = 4;
|
||||
const uint16_t ota_advert_interval = 0;
|
||||
const uint8_t ota_max_hops = 3;
|
||||
file.write((uint8_t *)&ota_checkpoint_blocks, sizeof(ota_checkpoint_blocks));
|
||||
file.write((uint8_t *)&ota_advert_interval, sizeof(ota_advert_interval));
|
||||
file.write((uint8_t *)&ota_max_hops, sizeof(ota_max_hops));
|
||||
#endif
|
||||
file.write((uint8_t *)&_prefs->rx_powersaving_enabled, sizeof(_prefs->rx_powersaving_enabled)); // 811
|
||||
file.write((uint8_t *)&_prefs->rx_ps_rx_us, sizeof(_prefs->rx_ps_rx_us)); // 812
|
||||
file.write((uint8_t *)&_prefs->rx_ps_sleep_us, sizeof(_prefs->rx_ps_sleep_us)); // 816
|
||||
file.write((uint8_t *)&_prefs->rx_ps_level, sizeof(_prefs->rx_ps_level)); // 820
|
||||
file.write((uint8_t *)&_prefs->rx_ps_preamble, sizeof(_prefs->rx_ps_preamble)); // 821
|
||||
// next: 822
|
||||
|
||||
file.close();
|
||||
}
|
||||
@@ -1992,6 +2128,105 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
} else {
|
||||
strcpy(reply, "Error: state must be on or off");
|
||||
}
|
||||
} else if (memcmp(config, "radio.rxps ", 11) == 0) {
|
||||
const char* value = &config[11];
|
||||
uint8_t enable = _prefs->rx_powersaving_enabled;
|
||||
uint32_t rx_us = _prefs->rx_ps_rx_us;
|
||||
uint32_t sleep_us = _prefs->rx_ps_sleep_us;
|
||||
uint32_t level = 0;
|
||||
uint32_t preamble = rxPowerSavingPreambleForSF(_prefs->sf);
|
||||
bool level_requested = false;
|
||||
bool preamble_overridden = false;
|
||||
|
||||
ensureRxPowerSavingDefaults(_prefs);
|
||||
rx_us = _prefs->rx_ps_rx_us;
|
||||
sleep_us = _prefs->rx_ps_sleep_us;
|
||||
|
||||
if (strcmp(value, "off") == 0) {
|
||||
enable = 0;
|
||||
} else if (strcmp(value, "on") == 0 || strcmp(value, "conservative") == 0) {
|
||||
enable = 1;
|
||||
level = RX_POWERSAVING_CONSERVATIVE_LEVEL;
|
||||
preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
} else if (strcmp(value, "balanced") == 0) {
|
||||
enable = 1;
|
||||
level = RX_POWERSAVING_BALANCED_LEVEL;
|
||||
preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
} else {
|
||||
StrHelper::strncpy(tmp, value, sizeof(tmp));
|
||||
const char *parts[4];
|
||||
int num = mesh::Utils::parseTextParts(tmp, parts, 4, ' ');
|
||||
if (num == 1 && isNumeric(parts[0])) {
|
||||
level = _atoi(parts[0]);
|
||||
level_requested = true;
|
||||
enable = 1;
|
||||
} else if (num == 2 && strcmp(parts[0], "level") == 0 && isNumeric(parts[1])) {
|
||||
level = _atoi(parts[1]);
|
||||
level_requested = true;
|
||||
enable = 1;
|
||||
} else if (num == 4 && strcmp(parts[0], "level") == 0 && isNumeric(parts[1]) &&
|
||||
strcmp(parts[2], "preamble") == 0 && isNumeric(parts[3])) {
|
||||
level = _atoi(parts[1]);
|
||||
preamble = _atoi(parts[3]);
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
enable = 1;
|
||||
} else if (num == 2 && isNumeric(parts[0]) && isNumeric(parts[1])) {
|
||||
rx_us = _atoi(parts[0]);
|
||||
sleep_us = _atoi(parts[1]);
|
||||
enable = 1;
|
||||
} else {
|
||||
strcpy(reply, "ERROR: use off|on|conservative|balanced|level <1-10>|<rx_us> <sleep_us>");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (level_requested && !calcRxPowerSavingLevel(level, _prefs->sf, _prefs->bw, preamble, &rx_us, &sleep_us)) {
|
||||
strcpy(reply, "ERROR: level range is 1-10; preamble is 16 or 32");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us)) {
|
||||
sprintf(reply, "ERROR: range is %lu-%lu us",
|
||||
(unsigned long)RX_POWERSAVING_MIN_PERIOD_US,
|
||||
(unsigned long)RX_POWERSAVING_MAX_PERIOD_US);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_callbacks->setRxPowerSaving(enable, rx_us, sleep_us)) {
|
||||
strcpy(reply, "ERROR: RX powersaving unsupported");
|
||||
return;
|
||||
}
|
||||
|
||||
_prefs->rx_powersaving_enabled = enable;
|
||||
_prefs->rx_ps_rx_us = rx_us;
|
||||
_prefs->rx_ps_sleep_us = sleep_us;
|
||||
if (level_requested) {
|
||||
// Remember the intent so the timings can auto-retune when SF/BW change.
|
||||
_prefs->rx_ps_level = level;
|
||||
_prefs->rx_ps_preamble = preamble_overridden ? preamble : 0; // 0 = auto (derive from SF)
|
||||
} else if (strcmp(value, "off") != 0) {
|
||||
// manual <rx_us> <sleep_us> timings are fixed, not level-derived
|
||||
// (the named profiles set level_requested and are handled above)
|
||||
_prefs->rx_ps_level = 0;
|
||||
_prefs->rx_ps_preamble = 0;
|
||||
}
|
||||
savePrefs();
|
||||
if (level_requested) {
|
||||
sprintf(reply, "OK - level %lu,%s,%lu,%lu,preamble=%lu",
|
||||
(unsigned long)level,
|
||||
enable ? "on" : "off",
|
||||
(unsigned long)rx_us,
|
||||
(unsigned long)sleep_us,
|
||||
(unsigned long)preamble);
|
||||
} else {
|
||||
sprintf(reply, "OK - %s,%lu,%lu", enable ? "on" : "off",
|
||||
(unsigned long)rx_us, (unsigned long)sleep_us);
|
||||
}
|
||||
} else if (memcmp(config, "radio ", 6) == 0) {
|
||||
strcpy(tmp, &config[6]);
|
||||
const char *parts[4];
|
||||
@@ -2005,8 +2240,11 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
_prefs->cr = cr;
|
||||
_prefs->freq = freq;
|
||||
_prefs->bw = bw;
|
||||
// Retune level-based RX powersaving to the new SF/BW. Persist only; the
|
||||
// radio itself is "reboot to apply", and begin() re-arms the timings then.
|
||||
bool rxps_retuned = recalcRxPowerSavingFromLevel(_prefs);
|
||||
_callbacks->savePrefs();
|
||||
strcpy(reply, "OK - reboot to apply");
|
||||
strcpy(reply, rxps_retuned ? "OK - reboot to apply (rxps retuned)" : "OK - reboot to apply");
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid radio params");
|
||||
}
|
||||
@@ -2573,6 +2811,14 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
_callbacks->formatScheduledRadioParams(true, skipSpacesConst(&config[11]), reply);
|
||||
} else if (memcmp(config, "radioat", 7) == 0 && (config[7] == 0 || config[7] == ' ')) {
|
||||
_callbacks->formatScheduledRadioParams(false, skipSpacesConst(&config[7]), reply);
|
||||
} else if (memcmp(config, "radio.rxps", 10) == 0) {
|
||||
ensureRxPowerSavingDefaults(_prefs);
|
||||
sprintf(reply, "> %s,%lu,%lu", _prefs->rx_powersaving_enabled ? "on" : "off",
|
||||
(unsigned long)_prefs->rx_ps_rx_us, (unsigned long)_prefs->rx_ps_sleep_us);
|
||||
} else if (memcmp(config, "rxps.wd", 7) == 0) {
|
||||
uint32_t wd_soft, wd_hard;
|
||||
_callbacks->getRxPsWatchdogCounts(&wd_soft, &wd_hard);
|
||||
sprintf(reply, "> soft=%lu,hard=%lu", (unsigned long)wd_soft, (unsigned long)wd_hard);
|
||||
} else if (memcmp(config, "radio", 5) == 0) {
|
||||
char freq[16], bw[16];
|
||||
strcpy(freq, StrHelper::ftoa(_prefs->freq));
|
||||
|
||||
Reference in new issue
Block a user