Validate beacon power without stranding saved settings

This commit is contained in:
mikecarper committed 2026-10-16 00:39:07 -07:00
1 parent adbeb2d007
commit d5cc73c43d
15 files changed
+662 -7

No files matched your search

+57
View File
@@ -57,6 +57,62 @@ void put32(uint8_t* bytes, uint32_t value) {
}
void fixCrc(std::vector<uint8_t>& bytes) { put32(bytes.data() + bytes.size() - 4, crc(bytes.data(), bytes.size() - 4)); }
void structuralStorage() {
Fixture mc; mc.mc(1, "Old");
Fixture mt; mt.mt();
for (unsigned fault = 0; fault < 15; ++fault) {
Fixture f;
f.fs.files = fault >= 12 ? mt.fs.files : mc.fs.files;
auto& bytes = f.fs.files.at("/beacons");
uint8_t* record = bytes.data() + 8;
switch (fault) {
case 0: record[10] = 255; break; // Invalid SF.
case 1: record[11] = 4; break; // Invalid coding rate.
case 2: {
const float bw = 123;
std::memcpy(record + 4, &bw, 4); break;
}
case 3: record[8] = 7; record[9] = 0; break;
case 4: { // Valid tuple, but its long preamble overflows safe airtime.
const float bw = 7.8f;
std::memcpy(record + 4, &bw, 4);
record[10] = 12; record[8] = 248; record[9] = 255; break;
}
case 5: put32(record, 0x7fc00000U); break; // NaN frequency.
case 6: record[17] = 255; break; // Malformed UTF-8.
case 7: record[17] = '\n'; break;
case 8: record[21] = 'X'; break; // Hidden bytes after the terminator.
case 9: record[16] = 0; break; // Disabled records must be all zero.
case 10: record[12] = uint8_t(mesh::RadioProfileProtocol::Ethernet); break;
case 11: record[15] = 8; break;
case 12: record[14] = 255; break; // Invalid MT preset.
case 13: record[8] = 32; record[9] = 0; break; // MT preamble is fixed.
case 14: bytes[6] = 0; break; // MT requires durable ID initialization.
}
fixCrc(bytes);
const auto original = f.fs.files;
f.manager.begin(&f.fs, &f.mesh, &f.clock, f.name);
f.cmd("get beacon", false);
f.cmd("set beacon.interval 12", false);
f.cmd("delete beacon all", false);
f.tick(48 * H);
assert(f.mesh.attempts == 0 && f.fs.files == original);
}
// Neither a future schema nor a read error may fall back to an older image.
for (bool unreadable : {false, true}) {
Fixture f;
f.fs.files = mc.fs.files;
f.fs.files["/beacons.bak"] = f.fs.files.at("/beacons");
if (unreadable) f.fs.fail_read_open = true;
else f.fs.files.at("/beacons")[4] = 2;
const auto original = f.fs.files;
f.manager.begin(&f.fs, &f.mesh, &f.clock, f.name);
f.cmd("get beacon", false);
f.tick(48 * H);
assert(f.mesh.attempts == 0 && f.fs.files == original);
}
}
void commands() {
Fixture f;
assert(!f.mesh.wake_enabled && f.fs.files.empty());
@@ -407,6 +463,7 @@ int main(int argc, char** argv) {
else if (!std::strcmp(argv[1], "schedule")) schedule();
else if (!std::strcmp(argv[1], "rolling")) rolling();
else if (!std::strcmp(argv[1], "storage")) storage();
else if (!std::strcmp(argv[1], "structural_storage")) structuralStorage();
else if (!std::strcmp(argv[1], "ids")) ids();
else if (!std::strcmp(argv[1], "mutations")) mutations();
else if (!std::strcmp(argv[1], "stale_backup")) staleBackup();
+90
View File
@@ -0,0 +1,90 @@
#pragma once
#include <RadioProfiles.h>
#include <helpers/radiolib/RadioProfileChip.h>
#include <helpers/radiolib/RadioPowerLimits.h>
#include <algorithm>
#include <cassert>
#include <cstdint>
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_ERR_INVALID_OUTPUT_POWER -1
#define RADIOLIB_MIN(a, b) std::min(int(a), int(b))
#define RADIOLIB_MAX(a, b) std::max(int(a), int(b))
#define RADIOLIB_CHECK_RANGE(value, low, high, error) \
if ((value) < (low) || (value) > (high)) return (error)
extern unsigned power_hardware_calls;
inline void dacWrite(uint8_t, uint8_t) { ++power_hardware_calls; }
namespace mesh {
struct MainBoard {};
struct Radio {
@RADIO_CAPABILITY@
};
}
struct PhysicalLayer {
bool highFreq = false;
float frequency = 909.5f;
virtual int16_t checkOutputPower(int8_t, int8_t*) = 0;
int16_t setOutputPower(int8_t, bool) { ++power_hardware_calls; return 0; }
};
struct SX1262 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
};
struct SX1276 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
int16_t checkOutputPower(int8_t, int8_t*, bool);
};
struct SX1272 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
int16_t checkOutputPower(int8_t, int8_t*, bool);
};
struct LR1110 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
int16_t checkOutputPower(int8_t, int8_t*, bool);
};
struct LR1121 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
int16_t checkOutputPower(int8_t, int8_t*, bool);
};
struct LR2021 : PhysicalLayer {
int16_t checkOutputPower(int8_t, int8_t*) override;
};
struct Module {
bool hp = true, lp = true;
bool findRfSwitchMode(int mode) const { return mode == 1 ? hp : lp; }
};
struct STM32WLx : PhysicalLayer {
static constexpr int MODE_TX_HP = 1, MODE_TX_LP = 2;
Module module;
const Module* getMod() const { return &module; }
int16_t checkOutputPower(int8_t, int8_t*) override;
};
@DRIVER_CHECKS@
class RadioLibWrapper : public mesh::Radio {
protected:
PhysicalLayer* _radio;
virtual bool checkBeaconOutputPower(float, int8_t) const;
public:
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard&) : _radio(&radio) {}
virtual void loop() { ++power_hardware_calls; }
virtual int16_t applyCachedTxPower(int8_t) { ++power_hardware_calls; return 0; }
bool setTxPower(int8_t power) { return applyCachedTxPower(power) == 0; }
bool validateBeaconPower(float, int8_t) const override;
bool validateProfile(const mesh::RadioProfileParams&) const;
bool supportsMeshtext() const { return true; }
bool supportsEthernetTunnel() const { return true; }
};
using CustomSX1276 = SX1276;
using CustomSX1262 = SX1262;
struct CustomSX1276Wrapper : RadioLibWrapper { using RadioLibWrapper::RadioLibWrapper; };
struct CustomSX1262Wrapper : RadioLibWrapper { using RadioLibWrapper::RadioLibWrapper; };
struct IndicatorRadioHal {
unsigned services = 0;
void serviceInterrupt() { ++services; ++power_hardware_calls; }
};
@INDICATOR_WRAPPER@
@DAC_LEVEL@
#define DAC_PA_DEFAULT_DRIVE_DBM 2
#define DAC_PA_TABLE_MAX INT8_MAX
@DAC_WRAPPER@
+251
View File
@@ -0,0 +1,251 @@
#include <Mesh.h>
#include <helpers/BeaconManager.h>
#include <helpers/BeaconFrames.h>
#include <helpers/UTF8Helpers.h>
#include <cstdio>
#include <limits>
unsigned power_hardware_calls = 0;
uint64_t beacon_test_time = 0;
@PRODUCTION_METHODS@
static bool boardAllows(int power, float frequency) {
// Explicit board caps are independent of the chip and calibrated PA range.
(void)power;
(void)frequency;
#ifdef MIN_LORA_TX_POWER
if (power < MIN_LORA_TX_POWER) return false;
#endif
#ifdef MAX_LORA_TX_POWER
if (power > MAX_LORA_TX_POWER) return false;
#endif
return true;
}
static bool driverAllows(int power, float frequency) {
(void)power;
#if defined(TEST_STM32)
(void)frequency;
#ifdef TEST_NO_PA
return false;
#elif defined(TEST_LP_ONLY)
return power >= -17 && power <= 14;
#elif defined(TEST_HP_ONLY)
return power >= -9 && power <= 22;
#else
return power >= -17 && power <= 22;
#endif
#elif MESH_RADIO_PROFILE_CHIP == 1276
(void)frequency;
return (power >= -4 && power <= 17) || power == 20;
#elif MESH_RADIO_PROFILE_CHIP == 1272
(void)frequency;
return power >= -1 && power <= 20;
#elif MESH_RADIO_PROFILE_CHIP == 1110
(void)frequency;
return power >= -17 && power <= 22;
#elif MESH_RADIO_PROFILE_CHIP == 1121
if (frequency > 1000) return power >= -18 && power <= 13;
#ifdef LR1121_FORCE_HP_PA
return power >= -9 && power <= 22;
#else
return power >= -17 && power <= 22;
#endif
#elif MESH_RADIO_PROFILE_CHIP == 2021
return frequency >= 1900 ? power >= -19 && power <= 12 : power >= -9 && power <= 22;
#else
(void)frequency;
return power >= -9 && power <= 22;
#endif
}
static void capability(RadioLibWrapper& wrapper, PhysicalLayer& chip) {
mesh::Radio unsupported;
assert(!unsupported.validateBeaconPower(909.5f, 17));
assert(!wrapper.validateBeaconPower(std::numeric_limits<float>::quiet_NaN(), 10));
for (bool normal_high_band : {false, true}) {
chip.highFreq = normal_high_band;
chip.frequency = normal_high_band ? 2440.0f : 909.5f;
for (float requested : {433.875f, 909.5f, 2440.0f}) {
for (int power = -128; power <= 127; ++power) {
const bool expected = boardAllows(power, requested) && driverAllows(power, requested);
assert(wrapper.validateBeaconPower(requested, int8_t(power)) == expected);
}
assert(chip.frequency == (normal_high_band ? 2440.0f : 909.5f));
assert(chip.highFreq == normal_high_band && power_hardware_calls == 0);
}
}
}
#if !defined(TEST_NO_PA)
static void image(const char* path, std::vector<uint8_t>& bytes, bool write) {
FILE* file = std::fopen(path, write ? "wb" : "rb");
assert(file);
if (write) assert(std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size());
else {
bytes.clear();
for (int value; (value = std::fgetc(file)) != EOF;) bytes.push_back(uint8_t(value));
assert(!std::ferror(file));
}
assert(std::fclose(file) == 0);
}
static void upgrade(RadioLibWrapper& wrapper, bool legacy) {
MemoryFS fs;
if (!legacy) {
image("upgrade.beacons", fs.files["/beacons"], false);
image("upgrade.ids", fs.files["/beacon_ids"], false);
}
mesh::Mesh mesh;
mesh._radio = &wrapper;
mesh::RTCClock rtc;
mesh::BeaconManager manager;
manager.begin(&fs, &mesh, &rtc, "Upgrade test");
char reply[256];
const auto command = [&](const char* text) {
assert(manager.handle(text, reply, sizeof(reply)));
assert(std::strncmp(reply, "Err", 3));
};
const auto tick = [&](unsigned hours) {
g_mock_millis += hours * mesh::BeaconManager::HourMillis;
manager.loop(g_mock_millis);
};
if (legacy) {
// These images are produced by the real manager and old Mesh validator,
// not hand-crafted bytes that the old firmware might never have saved.
command("set beacon 1 meshcore 909.5,125,7,5,100 Old MC");
command("set beacon 2 meshcore 909.5,125,7,5,22 Sibling");
command("set beacon 3 meshtext US LongFast 100 Old MT");
tick(6);
assert(mesh.sends.size() == 1 && mesh.sends[0].id != 0);
image("upgrade.beacons", fs.files.at("/beacons"), true);
image("upgrade.ids", fs.files.at("/beacon_ids"), true);
return;
}
const auto original = fs.files;
assert(original.at("/beacons")[6] == 1); // Durable IDs were initialized.
command("get beacon");
command("get beacon 1"); assert(std::strstr(reply, "100") && std::strstr(reply, "Old MC"));
command("get beacon 3"); assert(std::strstr(reply, "100") && std::strstr(reply, "Old MT"));
tick(2); // Unsupported MC consumes no queue attempt or storage write.
assert(mesh.attempts == 0 && mesh.sends.empty() && fs.files == original);
tick(2); // A supported sibling still transmits.
assert(mesh.attempts == 1 && mesh.sends.size() == 1 && mesh.sends[0].slot.power == 22);
assert(fs.files == original);
tick(2); // Unsupported MT must be rejected before durable nextId().
assert(mesh.attempts == 1 && mesh.sends.size() == 1 && fs.files == original);
command("set beacon.interval 12");
command("set beacon 2 meshcore 909.5,125,7,5,20 Updated sibling");
const auto& before = original.at("/beacons");
const auto& after = fs.files.at("/beacons");
for (unsigned index : {0U, 2U})
assert(std::equal(before.begin() + 8 + index * 98, before.begin() + 8 + (index + 1) * 98,
after.begin() + 8 + index * 98));
assert(fs.files.at("/beacon_ids") == original.at("/beacon_ids"));
command("get beacon 3"); assert(std::strstr(reply, "100"));
command("set beacon 1 meshcore 909.5,125,7,5,20 Repaired");
command("delete beacon 3");
assert(fs.files.at("/beacon_ids") == original.at("/beacon_ids"));
manager.begin(&fs, &mesh, &rtc, "Upgrade test");
command("get beacon 1"); assert(std::strstr(reply, "Repaired"));
command("get beacon.interval"); assert(std::strstr(reply, "12"));
tick(1);
assert(mesh.sends.size() == 2 && mesh.sends.back().slot.power == 20);
tick(1);
assert(mesh.sends.size() == 3 && std::strstr(mesh.sends.back().slot.text, "Updated sibling"));
assert(fs.files.at("/beacon_ids") == original.at("/beacon_ids"));
assert(power_hardware_calls == 0);
}
static void manager(RadioLibWrapper& wrapper) {
MemoryFS fs;
mesh::Mesh mesh;
mesh._radio = &wrapper;
mesh::RTCClock rtc;
mesh::BeaconManager manager;
manager.begin(&fs, &mesh, &rtc, "Power test");
char reply[256], command[128];
const float frequency = MESH_RADIO_PROFILE_CHIP == 1268 ? 433.875f : 909.5f;
for (int bad : {-128, 100, 127}) {
std::snprintf(command, sizeof(command), "set beacon 1 meshcore %g,125,7,5,%d Hello", double(frequency), bad);
assert(manager.handle(command, reply, sizeof(reply)));
assert(!std::strncmp(reply, "Err", 3));
assert(fs.files.empty()); // Rejection happens before persistence.
}
int accepted = 22;
while (!driverAllows(accepted, frequency) || !boardAllows(accepted, frequency)) --accepted;
std::snprintf(command, sizeof(command), "set beacon 1 meshcore %g,125,7,5,%d Hello", double(frequency), accepted);
assert(manager.handle(command, reply, sizeof(reply)) && !std::strncmp(reply, "OK", 2));
assert(fs.files.count("/beacons") && power_hardware_calls == 0);
manager.begin(&fs, &mesh, &rtc, "Power test");
g_mock_millis += 2 * mesh::BeaconManager::HourMillis;
manager.loop(g_mock_millis);
assert(mesh.sends.size() == 1 && mesh.sends[0].slot.power == accepted);
assert(power_hardware_calls == 0);
}
#endif
#if MESH_RADIO_PROFILE_CHIP == 1276
static void externalAmplifier() {
mesh::MainBoard board;
CustomSX1276 chip;
const DacPaLevel levels[] = {{10, 30}, {17, 50}, {30, 130}, {33, 225}};
DacPaSX1276Wrapper amplifier(chip, board, 4, levels, 4, 30);
for (int power = -128; power <= 127; ++power)
assert(amplifier.validateBeaconPower(909.5f, int8_t(power))
== (boardAllows(power, 909.5f) && power >= 10 && power <= 30));
DacPaSX1276Wrapper empty(chip, board, 4, nullptr, 0);
assert(!empty.validateBeaconPower(909.5f, 17));
DacPaSX1276Wrapper impossible(chip, board, 4, levels, 4, 9);
assert(!impossible.validateBeaconPower(909.5f, 17));
assert(power_hardware_calls == 0);
}
#endif
int main(int argc, char** argv) {
mesh::MainBoard board;
#if defined(TEST_STM32)
STM32WLx chip;
#ifdef TEST_LP_ONLY
chip.module.hp = false;
#elif defined(TEST_HP_ONLY)
chip.module.lp = false;
#elif defined(TEST_NO_PA)
chip.module.hp = chip.module.lp = false;
#endif
#elif MESH_RADIO_PROFILE_CHIP == 1276
SX1276 chip;
#elif MESH_RADIO_PROFILE_CHIP == 1272
SX1272 chip;
#elif MESH_RADIO_PROFILE_CHIP == 1110
LR1110 chip;
#elif MESH_RADIO_PROFILE_CHIP == 1121
LR1121 chip;
#elif MESH_RADIO_PROFILE_CHIP == 2021
LR2021 chip;
#else
SX1262 chip;
#endif
RadioLibWrapper wrapper(chip, board);
#if !defined(TEST_NO_PA)
if (argc == 2 && !std::strcmp(argv[1], "manager")) { manager(wrapper); return 0; }
if (argc == 2 && !std::strcmp(argv[1], "legacy_write")) { upgrade(wrapper, true); return 0; }
if (argc == 2 && !std::strcmp(argv[1], "upgrade")) { upgrade(wrapper, false); return 0; }
#else
(void)argc; (void)argv;
#endif
capability(wrapper, chip);
#if !defined(TEST_NO_PA)
manager(wrapper);
#endif
#if MESH_RADIO_PROFILE_CHIP == 1276
externalAmplifier();
#endif
#if MESH_RADIO_PROFILE_CHIP == 1262 && !defined(TEST_STM32)
IndicatorRadioHal hal;
IndicatorSX1262Wrapper indicator(chip, board, hal);
assert(indicator.validateBeaconPower(909.5f, 10) == boardAllows(10, 909.5f));
assert(!indicator.validateBeaconPower(909.5f, 100));
assert(hal.services == 0 && power_hardware_calls == 0);
#endif
puts("PASS beacon power capability and persisted CLI validation");
}