Files
meshcore-tth/src/helpers/CommonRadioPrefs.cpp
T
tthandClaude Opus 5.5 f3eae0582c Repeater, room server, sensor, companion: set commands refuse out-of-range or non-numeric values instead of saving them
Changelog: Repeater, room server, sensor, companion: `set tx`, `set radio`, `set freq` and other number settings now refuse a value that is out of range or not a number, instead of saving it and answering OK.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 17:33:42 +01:00

358 lines
11 KiB
C++

#include "CommonRadioPrefs.h"
#include "CliArgs.h"
#include "TxtDataHelpers.h"
#include "Utils.h"
#include "target.h"
#include <ctype.h>
#ifdef WITH_RX_POWERSAVING
#include "radiolib/RXPowerSavingCLI.h"
#endif
namespace {
// strtof() and atof() also read "nan" and "inf". A NaN cannot be refused by a
// range check: no ordered comparison is true for it, and under -ffast-math,
// which nRF52 builds use, the compiler may assume it never occurs and fold any
// test on the parsed value away. So the text is checked before it is parsed:
// after optional white space and sign, a number starts with a digit or a '.'.
bool looksLikeANumber(const char* s) {
while (isspace((unsigned char)*s)) s++;
if (*s == '+' || *s == '-') s++;
return isdigit((unsigned char)*s) || *s == '.';
}
#ifdef WITH_RX_POWERSAVING
RxPowerSavingControl* rxpsControl() {
#ifdef WRAPPER_CLASS
return &radio_driver;
#else
return nullptr;
#endif
}
#endif
// CUSTOM (TeTeHacko): the radio's verdict on the last setTxPower(), where the
// driver keeps one (RadioLibWrapper); other drivers (ESP-NOW) count as accepted.
template <class R>
auto lastTxPowerStatus(R& r, int) -> decltype(r.getLastTxPowerStatus()) { return r.getLastTxPowerStatus(); }
template <class R>
int16_t lastTxPowerStatus(R&, long) { return 0; }
#ifdef MAX_LORA_TX_POWER
constexpr long TX_POWER_MAX = MAX_LORA_TX_POWER;
#else
constexpr long TX_POWER_MAX = 30; // the same ceiling as the prefs load clamp
#endif
} // namespace
#ifdef WITH_RX_POWERSAVING
bool CommonRadioPrefs::applyRxPowerSaving(uint8_t sf, float bw) {
_active_sf = sf;
_active_bw = bw;
RxPowerSavingConfig* config = getRxPowerSavingConfig();
if (!config) return false;
// temporary radio parameters must not overwrite the saved automatic periods
RxPowerSavingConfig active = *config;
return applyRxPowerSavingConfig(&active, sf, bw, rxpsControl());
}
bool CommonRadioPrefs::isRxPowerSavingCalibrationActive() const {
const auto* control = rxpsControl();
return control && control->isRxPowerSavingCalibrationActive();
}
#endif
bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
if (strcmp(key, "fem_rxgain") == 0) {
snprintf(value, max_len, "%d", (uint32_t)getFEMRxGain());
return true;
}
if (strcmp(key, "fem_txgain") == 0) {
snprintf(value, max_len, "%d", (uint32_t)getFEMTxGain());
return true;
}
return false;
}
bool CommonRadioPrefs::setByKey(const char* key, const char* value) {
if (strcmp(key, "fem_rxgain") == 0) {
setFEMRxGain(atoi(value) ? 1 : 0); // boolean, as the prefs load clamps it
markDirty();
return true;
}
if (strcmp(key, "fem_txgain") == 0) {
setFEMTxGain(atoi(value) ? 1 : 0); // boolean, as the prefs load clamps it
markDirty();
return true;
}
return false;
}
bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
#ifdef WITH_RX_POWERSAVING
if (strncmp(command, "get radio.rxps", 14) == 0 && (command[14] == 0 || command[14] == ' ')) {
const char* suffix = command + 14;
while (*suffix == ' ') ++suffix;
if (*suffix) return false;
auto* config = getRxPowerSavingConfig();
if (!config) { strcpy(reply, "Error: unsupported"); return true; }
const uint8_t sf = _active_sf ? _active_sf : getSpreadFactor();
const float bw = _active_sf ? _active_bw : getBandwidth();
RxPowerSavingConfig active = *config;
normalizeRxPowerSavingConfig(&active, sf, bw,
rxPowerSavingCaptureCost(rxpsControl()), rxPowerSavingTransition(rxpsControl()));
RXPowerSavingCLI::get(&active, rxpsControl(), sf, bw, reply, 160);
return true;
}
if (strncmp(command, "set radio.rxps ", 15) == 0) {
auto* config = getRxPowerSavingConfig();
if (!config) { strcpy(reply, "Error: unsupported"); return true; }
if (RXPowerSavingCLI::set(command + 15,
_active_sf ? _active_sf : getSpreadFactor(), _active_sf ? _active_bw : getBandwidth(),
config, rxpsControl(), reply, 160)) markDirty();
return true;
}
#endif
if (strcmp(command, "get radio") == 0) {
char freq[16], bw[16];
strcpy(freq, StrHelper::ftoa(getFreq()));
strcpy(bw, StrHelper::ftoa3(getBandwidth()));
sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)getSpreadFactor(), (uint32_t)getCodingRate());
return true;
}
if (memcmp(command, "set radio ", 10) == 0) {
char tmp[132];
strcpy(tmp, &command[10]);
const char *parts[4];
int num = mesh::Utils::parseTextParts(tmp, parts, 4);
float freq = 0, bw = 0;
long sf_l = 0, cr_l = 0;
if (num == 4 && cli_args::parseFloat(parts[0], 150.0f, 2500.0f, &freq) &&
cli_args::parseFloat(parts[1], 7.0f, 500.0f, &bw) &&
cli_args::parseInt(parts[2], 5, 12, &sf_l) && cli_args::parseInt(parts[3], 5, 8, &cr_l)) {
uint8_t sf = sf_l, cr = cr_l;
setSpreadFactor(sf);
setCodingRate(cr);
setFreq(freq);
setBandwidth(bw);
#ifdef WITH_RX_POWERSAVING
// the RXPS timing is in symbols: a new SF/BW can make it invalid
auto* rxps = getRxPowerSavingConfig();
const bool timing_ok = !rxps || !rxps->enabled || normalizeRxPowerSavingConfig(
rxps, sf, bw, rxPowerSavingCaptureCost(rxpsControl()), rxPowerSavingTransition(rxpsControl()));
if (!timing_ok && rxpsControl()) {
rxpsControl()->setRxPowerSaving(false, rxps->rx_us, rxps->sleep_us);
rxpsControl()->setRxPowerSavingConfigError(true);
}
strcpy(reply, timing_ok ? "OK - reboot to apply" : "OK - reboot to apply; RXPS continuous: invalid timing");
#else
strcpy(reply, "OK - reboot to apply");
#endif
} else {
strcpy(reply, "Error, invalid radio params");
}
return true;
}
if (strcmp(command, "get freq") == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(getFreq()));
return true;
}
if (strcmp(command, "get af") == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(getAirtimeFactor()));
return true;
}
if (memcmp(command, "set af ", 7) == 0) {
char* end;
float af = strtof(&command[7], &end);
if (end == &command[7] || !looksLikeANumber(&command[7]) || af < 0 || af > 9) {
strcpy(reply, "ERROR: af must be 0-9");
} else {
setAirtimeFactor(af);
strcpy(reply, "OK");
}
return true;
}
if (strcmp(command, "get dutycycle") == 0) {
float dc = 100.0f / (getAirtimeFactor() + 1.0f);
int dc_int = (int)dc;
int dc_frac = (int)((dc - dc_int) * 10.0f + 0.5f);
sprintf(reply, "> %d.%d%%", dc_int, dc_frac);
return true;
}
if (memcmp(command, "set dutycycle ", 14) == 0) {
float dc = atof(&command[14]);
if (!looksLikeANumber(&command[14]) || dc < 1 || dc > 100) {
strcpy(reply, "ERROR: dutycycle must be 1-100");
} else {
setAirtimeFactor((100.0f / dc) - 1.0f);
float actual = 100.0f / (getAirtimeFactor() + 1.0f);
int a_int = (int)actual;
int a_frac = (int)((actual - a_int) * 10.0f + 0.5f);
sprintf(reply, "OK - %d.%d%%", a_int, a_frac);
}
return true;
}
if (strcmp(command, "get int.thresh") == 0) {
sprintf(reply, "> %d", (uint32_t) getIntThresh());
return true;
}
if (memcmp(command, "set int.thresh ", 15) == 0) {
long t;
if (cli_args::parseInt(&command[15], 0, 255, &t)) {
setIntThresh(t);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0-255");
}
return true;
}
if (strcmp(command, "get cad") == 0) {
sprintf(reply, "> %s", isCadEnabled() ? "on" : "off");
return true;
}
if (memcmp(command, "set cad ", 8) == 0) {
setCadEnabled(memcmp(&command[8], "on", 2) == 0);
strcpy(reply, "OK");
return true;
}
if (strcmp(command, "get radio.rxgain") == 0) {
sprintf(reply, "> %s", getRxGain() != 0 ? "on" : "off");
return true;
}
if (memcmp(command, "set radio.rxgain ", 17) == 0) {
bool enabled = memcmp(&command[17], "on", 2) == 0;
setRxGain(enabled);
if (radio_driver.setRxBoostedGainMode(enabled)) {
strcpy(reply, "OK");
} else {
strcpy(reply, "Error: unsupported");
}
return true;
}
if (memcmp(command, "get tx", 6) == 0 && (command[6] == 0 || command[6] == ' ')) {
sprintf(reply, "> %d", (int32_t) getTxPower());
return true;
}
if (memcmp(command, "set tx ", 7) == 0) {
// CUSTOM (TeTeHacko): was atoi() straight into the prefs and "OK" -- out of
// range or not a number got saved, and the radio could refuse it silently
// (SX1262 takes -9..22 and keeps its old power). Only a power the radio
// took is saved now.
long dbm;
if (!cli_args::parseInt(&command[7], -9, TX_POWER_MAX, &dbm)) {
sprintf(reply, "Error, must be -9..%d", (int)TX_POWER_MAX);
return true;
}
int8_t old_dbm = getTxPower();
radio_driver.setTxPower((int8_t)dbm);
if (lastTxPowerStatus(radio_driver, 0) != 0) {
radio_driver.setTxPower(old_dbm);
sprintf(reply, "Error, radio refuses %d dBm (kept %d)", (int)dbm, (int)old_dbm);
return true;
}
setTxPower((int8_t)dbm);
strcpy(reply, "OK");
return true;
}
if (strcmp(command, "get rxdelay") == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(getRxDelay()));
return true;
}
if (memcmp(command, "set rxdelay ", 12) == 0) {
float db;
if (cli_args::parseFloat(&command[12], 0, 20.0f, &db)) {
setRxDelay(db);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0-20");
}
return true;
}
if (strcmp(command, "get agc.reset.interval") == 0) {
sprintf(reply, "> %d", (uint32_t) getAgcResetInt());
return true;
}
if (memcmp(command, "set agc.reset.interval ", 23) == 0) {
long secs;
if (!cli_args::parseInt(&command[23], 0, 255 * 4, &secs)) {
strcpy(reply, "Error, must be 0-1020");
return true;
}
setAgcResetInt((uint16_t) secs);
sprintf(reply, "OK - interval rounded to %d", (uint32_t) getAgcResetInt());
return true;
}
if (strcmp(command, "get path.hash.mode") == 0) {
sprintf(reply, "> %d", (uint32_t)getHashMode());
return true;
}
if (memcmp(command, "set path.hash.mode ", 19) == 0) {
const char* config = command + 19;
long mode;
if (cli_args::parseInt(config, 0, 2, &mode)) {
setHashMode(mode);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0,1, or 2");
}
return true;
}
if (strcmp(command, "get multi.acks") == 0) {
sprintf(reply, "> %d", (uint32_t) getMultiAcks());
return true;
}
if (memcmp(command, "set multi.acks ", 15) == 0) {
long n;
if (cli_args::parseInt(&command[15], 0, 1, &n)) {
setMultiAcks(n);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0 or 1");
}
return true;
}
if (strcmp(command, "get txdelay") == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(getFloodTxDelay()));
return true;
}
if (memcmp(command, "set txdelay ", 12) == 0) {
float f;
if (cli_args::parseFloat(&command[12], 0, 2.0f, &f)) {
setFloodTxDelay(f);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0-2");
}
return true;
}
if (strcmp(command, "get direct.txdelay") == 0) {
sprintf(reply, "> %s", StrHelper::ftoa(getDirectTxDelay()));
return true;
}
if (memcmp(command, "set direct.txdelay ", 19) == 0) {
float f;
if (cli_args::parseFloat(&command[19], 0, 2.0f, &f)) {
setDirectTxDelay(f);
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be 0-2");
}
return true;
}
return false; // not handled
}