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:
1 parent
c58274a2e1
commit
14f5892033
9 files changed
+260
-56
No files matched your search
@@ -274,7 +274,7 @@ Refer to the node's manual for the correct settings to use.
|
||||
- `set tx <dbm>`
|
||||
|
||||
**Parameters:**
|
||||
- `dbm`: Power level in dBm (1-22)
|
||||
- `dbm`: Power level in dBm (-9 to 22 on SX126x; meshcore-tth refuses values out of range or not taken by the radio)
|
||||
|
||||
**Set by build flag:** `LORA_TX_POWER`
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ Radio parameters are common to all roles and handled by `CommonRadioPrefs`.
|
||||
- `set tx <dbm>`
|
||||
|
||||
**Parameters:**
|
||||
- `dbm`: Power level in dBm (1-22)
|
||||
- `dbm`: Power level in dBm (-9 to 22 on SX126x; meshcore-tth refuses values out of range or not taken by the radio)
|
||||
|
||||
**Set by build flag:** `LORA_TX_POWER`
|
||||
|
||||
|
||||
+16
-1
@@ -106,12 +106,27 @@ stall watchdog", "Reboot into the bootloader" and "Why the board last reset".
|
||||
| `dfu [uf2\|serial\|ota]` | *local only*: reboot into the bootloader -- UF2 drive (default), CDC port for `adafruit-nrfutil`, or BLE DFU |
|
||||
| `fw ...` | `-ota` images only: admin-pushed firmware update, see [ota.md](ota.md) |
|
||||
|
||||
## Number arguments of `set` (all, and the companion's serial CLI)
|
||||
|
||||
Upstream parses the value of a `set` with `atoi()`/`atof()`: text became 0,
|
||||
`300` became 44 in a one-byte setting, and the result was saved with `OK`. This
|
||||
fork refuses a value that is not a number or lies outside the range and keeps
|
||||
the old one (`Error, must be ...`). It applies to `set tx` (-9 up to the
|
||||
board's `MAX_LORA_TX_POWER`, else 30, and saved only if the radio takes it:
|
||||
SX1262 -9..22), `set radio` and `tempradio` (each field; the timeout 1-10080
|
||||
min), `set freq`, `set lat`/`set lon`, `set flood.max`, `set
|
||||
flood.max.unscoped`, `set flood.max.advert` (0-64), `set int.thresh` (0-255),
|
||||
`set multi.acks` (0-1), `set path.hash.mode` (0-2), `set agc.reset.interval`
|
||||
(0-1020), `set rxdelay`, `set txdelay`, `set direct.txdelay`, `set
|
||||
adc.multiplier`, and on the companion `set pin`, `set adc.multiplier`, `set
|
||||
tz.offset`. No exponent (`1e3`), no `nan`/`inf`.
|
||||
|
||||
## Diagnostics
|
||||
|
||||
| command | where | |
|
||||
|---|---|---|
|
||||
| `blink [n]` / `blink on\|off` | repeater | flash a spare LED (not the BLE or TX one) `n` times (default 6, max 999), or until `blink off` (gives up after 10 min); finds a board on a bench or a node on a mast |
|
||||
| `txpwr [dBm]` | repeater | LoRa output power until reboot, with RadioLib's status (`set tx` saves and says OK even when the radio refused) |
|
||||
| `txpwr [dBm]` | repeater | LoRa output power until reboot, with RadioLib's status; takes -9..30 |
|
||||
| `i2c` | all | scan the I2C bus |
|
||||
| `get chip` | nRF52 (repeater, room server, sensor) | the silicon (`nRF52840 AAD0`; variant `xxF0` and later = rev3) and `UICR.APPROTECT`: `open` means SWD can read the whole flash, keys included ([locked-bootloader.md](locked-bootloader.md)) |
|
||||
| `dio1` | own build: `PIN_DIAG` | has the SX1262 raise TX_DONE and compares its IRQ register with the level on the DIO1 pin -- which end of a dead IRQ line is broken |
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include <Mesh.h>
|
||||
#include <helpers/CliArgs.h>
|
||||
|
||||
#define CMD_APP_START 1
|
||||
#define CMD_SEND_TXT_MSG 2
|
||||
@@ -2209,7 +2210,12 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
|
||||
#endif
|
||||
|
||||
if (memcmp(command, "set pin ", 8) == 0) {
|
||||
_prefs.ble_pin = atoi(&command[8]);
|
||||
long pin; // atoi(): "abc" was PIN 0, i.e. a random one
|
||||
if (!cli_args::parseInt(&command[8], 0, 999999, &pin)) {
|
||||
strcpy(reply, "Error, PIN must be 0-999999");
|
||||
return true;
|
||||
}
|
||||
_prefs.ble_pin = pin;
|
||||
savePrefs();
|
||||
sprintf(reply, "> pin is now %06d", _prefs.ble_pin);
|
||||
return true;
|
||||
@@ -2302,8 +2308,8 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
|
||||
}
|
||||
|
||||
if (memcmp(command, "set adc.multiplier ", 19) == 0) { // same as CommonCLI
|
||||
float mult = atof(&command[19]);
|
||||
if (mult < 0.0f || mult > 10.0f) {
|
||||
float mult = 0;
|
||||
if (!cli_args::parseFloat(&command[19], 0.0f, 10.0f, &mult)) {
|
||||
strcpy(reply, "Error, must be from 0 to 10");
|
||||
} else if (board.setAdcMultiplier(mult)) {
|
||||
_prefs.adc_multiplier = mult;
|
||||
@@ -2334,8 +2340,8 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
|
||||
return true;
|
||||
}
|
||||
if (memcmp(command, "set tz.offset ", 14) == 0) {
|
||||
float tz = atof(&command[14]);
|
||||
if (tz < -12 || tz > 14) {
|
||||
float tz = 0;
|
||||
if (!cli_args::parseFloat(&command[14], -12.0f, 14.0f, &tz)) {
|
||||
strcpy(reply, "Error, must be from -12 to +14");
|
||||
} else {
|
||||
_prefs.tz_offset = tz;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "MyMesh.h"
|
||||
#include <helpers/NodeMaintenance.h>
|
||||
#include <helpers/CliArgs.h>
|
||||
|
||||
// ===========================================================================
|
||||
// CUSTOM (TeTeHacko): `blink [n]` -- make THIS board flash its LED on demand,
|
||||
@@ -625,7 +626,12 @@ MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock,
|
||||
bool txPwrHandleCommand(const char* command, char* reply) {
|
||||
if (memcmp(command, "txpwr", 5) != 0 || (command[5] != 0 && command[5] != ' ')) return false;
|
||||
if (command[5] == ' ') {
|
||||
int dbm = atoi(&command[6]);
|
||||
long dbm_l; // atoi() into int8_t: "250" went out as -6 dBm, "abc" as 0
|
||||
if (!cli_args::parseInt(&command[6], -9, 30, &dbm_l)) {
|
||||
strcpy(reply, "ERR - txpwr <-9..30 dBm>");
|
||||
return true;
|
||||
}
|
||||
int dbm = dbm_l;
|
||||
radio_driver.setTxPower((int8_t) dbm);
|
||||
int16_t st = radio_driver.getLastTxPowerStatus();
|
||||
sprintf(reply, "%s - txpwr %d dBm, RadioLib status %d%s", st == RADIOLIB_ERR_NONE ? "OK" : "ERR",
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
// CUSTOM (TeTeHacko): strict number arguments for CLI `set` commands.
|
||||
//
|
||||
// atoi()/atof() take any text: "abc" is 0, "300" goes into a uint8_t as 44,
|
||||
// and "nan" is a float that no range check refuses (under -ffast-math, which
|
||||
// the nRF52 builds use, the compiler may even fold that check away). The set
|
||||
// commands then saved the result and answered "OK" -- `set tx 30` on an
|
||||
// SX1262, `set freq abc` (150 MHz after the next boot's clamp), `set flood.max
|
||||
// 300` (44). These helpers accept a whole number (or decimal) in a range and
|
||||
// nothing else: optional surrounding white space and sign, digits, and for the
|
||||
// float a '.' and fraction. No exponent, no hex, no trailing text.
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace cli_args {
|
||||
|
||||
// Returns true and sets *out when `s` is an integer in [lo, hi].
|
||||
inline bool parseInt(const char* s, long lo, long hi, long* out) {
|
||||
if (!s) return false;
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
const char* p = s;
|
||||
if (*p == '+' || *p == '-') p++;
|
||||
if (!isdigit((unsigned char)*p)) return false;
|
||||
char* end;
|
||||
long v = strtol(s, &end, 10);
|
||||
while (isspace((unsigned char)*end)) end++;
|
||||
if (*end != 0) return false;
|
||||
if (v < lo || v > hi) return false;
|
||||
*out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns true and sets *out when `s` is a decimal number in [lo, hi].
|
||||
inline bool parseFloat(const char* s, float lo, float hi, float* out) {
|
||||
if (!s) return false;
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
const char* p = s;
|
||||
if (*p == '+' || *p == '-') p++;
|
||||
bool digits = false;
|
||||
while (isdigit((unsigned char)*p)) { p++; digits = true; }
|
||||
if (*p == '.') {
|
||||
p++;
|
||||
while (isdigit((unsigned char)*p)) { p++; digits = true; }
|
||||
}
|
||||
if (!digits) return false;
|
||||
const char* rest = p;
|
||||
while (isspace((unsigned char)*rest)) rest++;
|
||||
if (*rest != 0) return false; // exponent, "nan", "inf" or trailing text
|
||||
float v = strtof(s, nullptr);
|
||||
if (!(v >= lo && v <= hi)) return false;
|
||||
*out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace cli_args
|
||||
+47
-24
@@ -1,4 +1,5 @@
|
||||
#include <Arduino.h>
|
||||
#include "CliArgs.h"
|
||||
#include "CommonCLI.h"
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "AdvertDataHelpers.h"
|
||||
@@ -354,12 +355,14 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
StrHelper::strncpy(tmp, &command[10], sizeof(tmp));
|
||||
const char *parts[5];
|
||||
int num = mesh::Utils::parseTextParts(tmp, parts, 5);
|
||||
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;
|
||||
int temp_timeout_mins = num > 4 ? atoi(parts[4]) : 0;
|
||||
if (freq >= 150.0f && freq <= 2500.0f && sf >= 5 && sf <= 12 && cr >= 5 && cr <= 8 && bw >= 7.0f && bw <= 500.0f && temp_timeout_mins > 0) {
|
||||
float freq = 0, bw = 0;
|
||||
long sf_l = 0, cr_l = 0, mins_l = 0;
|
||||
if (num == 5 && 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) &&
|
||||
cli_args::parseInt(parts[4], 1, 10080, &mins_l)) {
|
||||
uint8_t sf = sf_l, cr = cr_l;
|
||||
int temp_timeout_mins = mins_l;
|
||||
_callbacks->applyTempRadioParams(freq, bw, sf, cr, temp_timeout_mins);
|
||||
sprintf(reply, "OK - temp params for %d mins", temp_timeout_mins);
|
||||
} else {
|
||||
@@ -733,18 +736,28 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
savePrefs();
|
||||
strcpy(reply, _prefs->disable_fwd ? "OK - repeat is now OFF" : "OK - repeat is now ON");
|
||||
} else if (memcmp(config, "lat ", 4) == 0) {
|
||||
_prefs->node_lat = atof(&config[4]);
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
float v;
|
||||
if (cli_args::parseFloat(&config[4], -90.0f, 90.0f, &v)) {
|
||||
_prefs->node_lat = v;
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be -90..90");
|
||||
}
|
||||
} else if (memcmp(config, "lon ", 4) == 0) {
|
||||
_prefs->node_lon = atof(&config[4]);
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
float v;
|
||||
if (cli_args::parseFloat(&config[4], -180.0f, 180.0f, &v)) {
|
||||
_prefs->node_lon = v;
|
||||
_prefs->getGpsPrefs()->applyGuardReference(*_sensors);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be -180..180");
|
||||
}
|
||||
} else if (memcmp(config, "flood.max.unscoped ", 19) == 0) {
|
||||
uint8_t m = atoi(&config[19]);
|
||||
if (m <= 64) {
|
||||
long m; // was uint8_t = atoi(): "300" became 44 and passed
|
||||
if (cli_args::parseInt(&config[19], 0, 64, &m)) {
|
||||
_prefs->flood_max_unscoped = m;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
@@ -752,8 +765,8 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "Error, max 64");
|
||||
}
|
||||
} else if (memcmp(config, "flood.max.advert ", 17) == 0) {
|
||||
uint8_t m = atoi(&config[17]);
|
||||
if (m <= 64) {
|
||||
long m; // was uint8_t = atoi(): "300" became 44 and passed
|
||||
if (cli_args::parseInt(&config[17], 0, 64, &m)) {
|
||||
_prefs->flood_max_advert = m;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
@@ -761,8 +774,8 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "Error, max 64");
|
||||
}
|
||||
} else if (memcmp(config, "flood.max ", 10) == 0) {
|
||||
uint8_t m = atoi(&config[10]);
|
||||
if (m <= 64) {
|
||||
long m; // was uint8_t = atoi(): "300" became 44 and passed
|
||||
if (cli_args::parseInt(&config[10], 0, 64, &m)) {
|
||||
_prefs->flood_max = m;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
@@ -800,9 +813,14 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
} else if (sender_timestamp == 0 && memcmp(config, "freq ", 5) == 0) {
|
||||
_prefs->freq = atof(&config[5]);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK - reboot to apply");
|
||||
float f; // was atof(): "abc" saved 0, i.e. 150 MHz after the next boot's clamp
|
||||
if (cli_args::parseFloat(&config[5], 150.0f, 2500.0f, &f)) {
|
||||
_prefs->freq = f;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK - reboot to apply");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 150-2500 MHz");
|
||||
}
|
||||
#ifdef WITH_BRIDGE
|
||||
} else if (memcmp(config, "bridge.enabled ", 15) == 0) {
|
||||
_prefs->bridge_enabled = memcmp(&config[15], "on", 2) == 0;
|
||||
@@ -885,7 +903,12 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
strcpy(reply, "on - shows on next advert");
|
||||
}
|
||||
} else if (memcmp(config, "adc.multiplier ", 15) == 0) {
|
||||
_prefs->adc_multiplier = atof(&config[15]);
|
||||
float m;
|
||||
if (!cli_args::parseFloat(&config[15], 0.0f, 10.0f, &m)) {
|
||||
strcpy(reply, "Error, must be 0-10 (0 = board default)");
|
||||
return;
|
||||
}
|
||||
_prefs->adc_multiplier = m;
|
||||
if (_board->setAdcMultiplier(_prefs->adc_multiplier)) {
|
||||
savePrefs();
|
||||
if (_prefs->adc_multiplier == 0.0f) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "helpers/CliArgs.h"
|
||||
|
||||
using cli_args::parseFloat;
|
||||
using cli_args::parseInt;
|
||||
|
||||
TEST(CliArgsInt, AcceptsWholeNumbersInRange) {
|
||||
long v = 0;
|
||||
EXPECT_TRUE(parseInt("22", -9, 22, &v)); EXPECT_EQ(v, 22);
|
||||
EXPECT_TRUE(parseInt("-9", -9, 22, &v)); EXPECT_EQ(v, -9);
|
||||
EXPECT_TRUE(parseInt(" +5 ", -9, 22, &v)); EXPECT_EQ(v, 5);
|
||||
EXPECT_TRUE(parseInt("0", 0, 64, &v)); EXPECT_EQ(v, 0);
|
||||
}
|
||||
|
||||
TEST(CliArgsInt, RefusesOutOfRangeAndGarbage) {
|
||||
long v = 7;
|
||||
EXPECT_FALSE(parseInt("23", -9, 22, &v));
|
||||
EXPECT_FALSE(parseInt("-10", -9, 22, &v));
|
||||
EXPECT_FALSE(parseInt("300", 0, 64, &v)); // would be 44 in a uint8_t
|
||||
EXPECT_FALSE(parseInt("abc", 0, 64, &v)); // atoi() says 0
|
||||
EXPECT_FALSE(parseInt("", 0, 64, &v));
|
||||
EXPECT_FALSE(parseInt("12abc", 0, 64, &v));
|
||||
EXPECT_FALSE(parseInt("1.5", 0, 64, &v));
|
||||
EXPECT_FALSE(parseInt("-", 0, 64, &v));
|
||||
EXPECT_FALSE(parseInt(nullptr, 0, 64, &v));
|
||||
EXPECT_EQ(v, 7); // untouched on failure
|
||||
}
|
||||
|
||||
TEST(CliArgsFloat, AcceptsDecimalsInRange) {
|
||||
float f = 0;
|
||||
EXPECT_TRUE(parseFloat("869.432", 150, 2500, &f)); EXPECT_FLOAT_EQ(f, 869.432f);
|
||||
EXPECT_TRUE(parseFloat("-12.5", -90, 90, &f)); EXPECT_FLOAT_EQ(f, -12.5f);
|
||||
EXPECT_TRUE(parseFloat(".5", 0, 2, &f)); EXPECT_FLOAT_EQ(f, 0.5f);
|
||||
EXPECT_TRUE(parseFloat("2", 0, 2, &f)); EXPECT_FLOAT_EQ(f, 2.0f);
|
||||
EXPECT_TRUE(parseFloat("3. ", 0, 9, &f)); EXPECT_FLOAT_EQ(f, 3.0f);
|
||||
}
|
||||
|
||||
TEST(CliArgsFloat, RefusesNanInfExponentAndGarbage) {
|
||||
float f = 1;
|
||||
EXPECT_FALSE(parseFloat("nan", -1e9f, 1e9f, &f));
|
||||
EXPECT_FALSE(parseFloat("inf", -1e9f, 1e9f, &f));
|
||||
EXPECT_FALSE(parseFloat("1e3", 0, 1e9f, &f));
|
||||
EXPECT_FALSE(parseFloat("abc", 0, 2500, &f));
|
||||
EXPECT_FALSE(parseFloat("", 0, 2500, &f));
|
||||
EXPECT_FALSE(parseFloat(".", 0, 2500, &f));
|
||||
EXPECT_FALSE(parseFloat("869,4", 150, 2500, &f));
|
||||
EXPECT_FALSE(parseFloat("100", 150, 2500, &f));
|
||||
EXPECT_FALSE(parseFloat("91", -90, 90, &f));
|
||||
EXPECT_EQ(f, 1);
|
||||
}
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in new issue
Block a user