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>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-27 17:33:42 +01:00
1 parent 739fdcceca
commit f3eae0582c
9 files changed
+260 -56

No files matched your search

+66 -23
View File
@@ -1,4 +1,5 @@
#include "CommonRadioPrefs.h"
#include "CliArgs.h"
#include "TxtDataHelpers.h"
#include "Utils.h"
#include "target.h"
@@ -30,6 +31,19 @@ RxPowerSavingControl* rxpsControl() {
}
#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
@@ -62,12 +76,12 @@ bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
}
bool CommonRadioPrefs::setByKey(const char* key, const char* value) {
if (strcmp(key, "fem_rxgain") == 0) {
setFEMRxGain(atoi(value));
setFEMRxGain(atoi(value) ? 1 : 0); // boolean, as the prefs load clamps it
markDirty();
return true;
}
if (strcmp(key, "fem_txgain") == 0) {
setFEMTxGain(atoi(value));
setFEMTxGain(atoi(value) ? 1 : 0); // boolean, as the prefs load clamps it
markDirty();
return true;
}
@@ -111,11 +125,12 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
strcpy(tmp, &command[10]);
const char *parts[4];
int num = mesh::Utils::parseTextParts(tmp, parts, 4);
float freq = num > 0 ? strtof(parts[0], nullptr) : 0.0f;
float bw = num > 1 ? strtof(parts[1], nullptr) : 0.0f;
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
if (freq >= 150.0f && freq <= 2500.0f && sf >= 5 && sf <= 12 && cr >= 5 && cr <= 8 && bw >= 7.0f && bw <= 500.0f) {
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);
@@ -186,8 +201,13 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set int.thresh ", 15) == 0) {
setIntThresh(atoi(&command[15]));
strcpy(reply, "OK");
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;
}
@@ -221,8 +241,23 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set tx ", 7) == 0) {
setTxPower(atoi(&command[7]));
radio_driver.setTxPower(getTxPower());
// 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;
}
@@ -232,8 +267,8 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set rxdelay ", 12) == 0) {
float db = atof(&command[12]);
if (db >= 0 && db <= 20.0f) {
float db;
if (cli_args::parseFloat(&command[12], 0, 20.0f, &db)) {
setRxDelay(db);
strcpy(reply, "OK");
} else {
@@ -247,8 +282,11 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set agc.reset.interval ", 23) == 0) {
int secs = atoi(&command[23]);
secs = constrain(secs, 0, 255 * 4);
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;
@@ -260,8 +298,8 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
}
if (memcmp(command, "set path.hash.mode ", 19) == 0) {
const char* config = command + 19;
uint8_t mode = atoi(config);
if (mode < 3) {
long mode;
if (cli_args::parseInt(config, 0, 2, &mode)) {
setHashMode(mode);
strcpy(reply, "OK");
} else {
@@ -275,8 +313,13 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set multi.acks ", 15) == 0) {
setMultiAcks(atoi(&command[15]));
strcpy(reply, "OK");
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;
}
@@ -285,8 +328,8 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set txdelay ", 12) == 0) {
float f = atof(&command[12]);
if (f >= 0 && f <= 2.0f) {
float f;
if (cli_args::parseFloat(&command[12], 0, 2.0f, &f)) {
setFloodTxDelay(f);
strcpy(reply, "OK");
} else {
@@ -300,8 +343,8 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}
if (memcmp(command, "set direct.txdelay ", 19) == 0) {
float f = atof(&command[19]);
if (f >= 0 && f <= 2.0f) {
float f;
if (cli_args::parseFloat(&command[19], 0, 2.0f, &f)) {
setDirectTxDelay(f);
strcpy(reply, "OK");
} else {