Expose read-only RX power-saving diagnostics through framed CLI

This commit is contained in:
mikecarper committed 2026-10-17 15:56:43 -07:00
1 parent cdfd1ad691
commit ff548d2481
5 files changed
+292 -71

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include "helpers/radiolib/RXPowerSaving.h"
struct Radio {
bool supported = true;
bool fallback = false;
unsigned fallback_queries = 0;
bool supportsRxPowerSaving() const { return supported; }
bool isRxPowerSavingContinuousFallback() { ++fallback_queries; return fallback; }
// No mutating radio APIs exist in this fixture: a write cannot compile.
} radio_driver;
struct Prefs {
uint8_t rx_powersaving_enabled = 1;
uint8_t rx_ps_level = 8;
uint8_t rx_ps_preamble = 16;
uint32_t rx_ps_rx_us = 11111;
uint32_t rx_ps_sleep_us = 22222;
uint8_t sf = 7;
float bw = 62.5;
};
struct Output {
std::string value;
void printf(const char* format, ...) {
char result[512];
va_list args;
va_start(args, format);
const int count = vsnprintf(result, sizeof(result), format, args);
va_end(args);
assert(count >= 0 && static_cast<size_t>(count) < sizeof(result));
value += result;
}
};
struct MyMesh {
Prefs _prefs;
bool temporary = false;
uint8_t _temp_radio_sf = 5;
float _temp_radio_bw = 500;
Output output;
bool isTempRadioActive() const { return temporary; }
bool handleRxPowerSavingGetCommand(const char*, char*, size_t) const;
bool handleLocalControlCommand(const char*, char*, size_t);
Output& terminalOutput() { return output; }
void ascii(const char* command) {
char local_reply[160];
#include "production_ascii.inc"
assert(false && "Fixture expects one recognized ASCII RXPS getter");
}
};
#include "production_getters.inc"
static std::string expected(const MyMesh& mesh, bool config) {
if (!radio_driver.supported) return "Error: RX power saving is unsupported on this radio";
const Prefs& prefs = mesh._prefs;
uint32_t rx = prefs.rx_ps_rx_us, sleep = prefs.rx_ps_sleep_us;
uint8_t level = 0, preamble = 0;
if (!config && prefs.rx_ps_level != 0) {
uint8_t sf = prefs.sf;
float bw = prefs.bw;
#if COMPANION_FEATURE_TEMP_RADIO
if (mesh.temporary) { sf = mesh._temp_radio_sf; bw = mesh._temp_radio_bw; }
#endif
recalcRxPowerSavingFromLevel(prefs.rx_ps_level, sf, bw, prefs.rx_ps_preamble,
&rx, &sleep, &level, &preamble);
}
char result[256];
snprintf(result, sizeof(result), "> %s,level=%u,preamble=%u,rx=%lu,sleep=%lu%s",
prefs.rx_powersaving_enabled ? "on" : "off", unsigned(prefs.rx_ps_level),
unsigned(prefs.rx_ps_preamble), static_cast<unsigned long>(rx),
static_cast<unsigned long>(sleep),
!config && radio_driver.fallback ? ",mode=continuous-fast" : "");
std::string out = result;
if (!config && level && level != prefs.rx_ps_level) out += ",effective-level=" + std::to_string(level);
if (!config && preamble && preamble != prefs.rx_ps_preamble) out += ",effective-preamble=" + std::to_string(preamble);
return out;
}
static void check(MyMesh& mesh, bool config, bool all_capacities) {
const char* command = config ? "get radio.rxps.config" : "get radio.rxps";
const std::string wanted = expected(mesh, config);
const Prefs saved = mesh._prefs;
const unsigned before = radio_driver.fallback_queries;
for (size_t capacity = all_capacities ? 1 : 160; capacity <= 180; ++capacity) {
std::vector<unsigned char> guarded(capacity + 16, 0xA7);
char* reply = reinterpret_cast<char*>(guarded.data() + 8);
assert(mesh.handleLocalControlCommand(command, reply, capacity));
const size_t count = wanted.size() < capacity - 1 ? wanted.size() : capacity - 1;
assert(strlen(reply) == count && !memcmp(reply, wanted.data(), count));
for (size_t i = 0; i < 8; ++i) assert(guarded[i] == 0xA7);
for (size_t i = 8 + capacity; i < guarded.size(); ++i) assert(guarded[i] == 0xA7);
assert(memcmp(&mesh._prefs, &saved, sizeof(saved)) == 0);
}
if (config || !radio_driver.supported) assert(radio_driver.fallback_queries == before);
mesh.output.value.clear();
mesh.ascii(command);
const std::string ascii = radio_driver.supported
? " " + std::string(command + 4) + " " + wanted.substr(2) + "\r\n"
: " ERROR: RX power saving is unsupported on this radio\r\n";
assert(mesh.output.value == ascii);
assert(memcmp(&mesh._prefs, &saved, sizeof(saved)) == 0);
}
int main() {
MyMesh mesh;
for (uint8_t enabled = 0; enabled <= 1; ++enabled) {
for (uint8_t level = 0; level <= 10; ++level) {
for (uint8_t preamble : {uint8_t(0), uint8_t(16), uint8_t(32)}) {
for (bool temporary : {false, true}) {
mesh._prefs.rx_powersaving_enabled = enabled;
mesh._prefs.rx_ps_level = level;
mesh._prefs.rx_ps_preamble = preamble;
mesh.temporary = temporary;
radio_driver.fallback = temporary;
check(mesh, false, level == 8);
check(mesh, true, level == 8);
}
}
}
}
// Config must return actual stored timings, never a recalculated pair. The
// effective getter follows TempRadio only when compiled and currently active.
mesh._prefs = Prefs();
mesh.temporary = false;
const std::string normal = expected(mesh, false);
mesh.temporary = true;
const std::string temporary = expected(mesh, false);
#if COMPANION_FEATURE_TEMP_RADIO
assert(normal != temporary);
#else
assert(normal == temporary);
#endif
check(mesh, false, true);
check(mesh, true, true);
radio_driver.supported = false;
check(mesh, false, true);
check(mesh, true, true);
radio_driver.supported = true;
char reply[160];
memset(reply, 'X', sizeof(reply));
assert(!mesh.handleRxPowerSavingGetCommand(nullptr, reply, sizeof(reply)));
assert(!mesh.handleRxPowerSavingGetCommand("get radio.rxps", nullptr, sizeof(reply)));
assert(!mesh.handleRxPowerSavingGetCommand("get radio.rxps", reply, 0));
for (const char* command : {"", "get radio.rxps ", "get radio.rxps.extra", "get radio.rxps.configx",
"get radio.rxps.config extra", "set radio.rxps on", "GET radio.rxps"}) {
assert(!mesh.handleLocalControlCommand(command, reply, sizeof(reply)));
for (char byte : reply) assert(byte == 'X');
}
assert(mesh.handleLocalControlCommand(" get radio.rxps.config", reply, sizeof(reply)));
assert(std::string(reply) == expected(mesh, true));
puts("RXPS getter contracts passed");
}
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
"""Exercise the production shared RXPS getters and ASCII adapter under sanitizers."""
from pathlib import Path
import os
import subprocess
import tempfile
import unittest
from test_replay_reset_integration import extract_braced
ROOT = Path(__file__).resolve().parents[1]
class CompanionRxpsGetterTests(unittest.TestCase):
def test_real_getters_are_bounded_readonly_and_match_active_primary(self):
source = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text(encoding="ascii")
getter = extract_braced(source, "bool MyMesh::handleRxPowerSavingGetCommand(")
local = extract_braced(source, "bool MyMesh::handleLocalControlCommand(")
signature = " if (handleRxPowerSavingGetCommand(command, reply, reply_size)) return true;"
self.assertIn(signature, local)
# Keep the real local entry validation/space handling and registration;
# unrelated setters/transport helpers are not part of this fixture.
entry = local[:local.index(" if (mesh::powerdiag::handleCommand")]
entry += signature + "\n return false;\n}\n"
terminal = extract_braced(source, "void MyMesh::handleTerminalCommand(")
ascii_adapter = extract_braced(terminal, "if (handleRxPowerSavingGetCommand(command, local_reply, sizeof(local_reply)))")
with tempfile.TemporaryDirectory(prefix="meshcore-companion-rxps-get-") as directory:
work = Path(directory)
(work / "production_getters.inc").write_text(getter + "\n" + entry, encoding="ascii")
(work / "production_ascii.inc").write_text(ascii_adapter, encoding="ascii")
for platform in ("NRF52_PLATFORM", "ESP32_PLATFORM", "STM32_PLATFORM"):
for temporary in (0, 1):
with self.subTest(platform=platform, temporary=temporary):
binary = work / (platform + str(temporary))
built = subprocess.run([
os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", "-Werror",
"-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-pie", "-no-pie",
"-D" + platform + "=1", "-DCOMPANION_FEATURE_TEMP_RADIO=" + str(temporary),
"-I", str(ROOT / "src"), "-isystem", str(ROOT / "test/mocks"), "-I", str(work),
str(ROOT / "test/fixtures/companion_rxps_getters.cpp"), "-o", str(binary),
], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
ran = subprocess.run([str(binary)], capture_output=True, text=True, timeout=20)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
self.assertIn("RXPS getter contracts passed", ran.stdout)
def test_framed_admin_and_ascii_share_the_same_getter(self):
source = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text(encoding="ascii")
local = extract_braced(source, "bool MyMesh::handleLocalControlCommand(")
self.assertIn("handleRxPowerSavingGetCommand(command, reply, reply_size)", local)
shared = extract_braced(source, "bool MyMesh::handleCommand(")
self.assertIn("handleLocalControlCommand(command, reply, reply_capacity)", shared)
framed = source[source.index("} else if (mesh::companion::isRunCliFrame("):]
self.assertIn("handled = handleCommand(text, 0, reply_buf);", framed)
admin = extract_braced(source, "void MyMesh::execAdminCommand(")
self.assertIn("if (handleCommand(cmd, 0, reply)) return;", admin)
self.assertNotIn("get radio.rxps", admin)
terminal = extract_braced(source, "void MyMesh::handleTerminalCommand(")
self.assertLess(terminal.index("handleRxPowerSavingGetCommand("), terminal.index("handleLocalControlCommand("))
self.assertNotIn("recalcRxPowerSavingFromLevel", terminal)
helper = extract_braced(source, "bool MyMesh::handleRxPowerSavingGetCommand(")
self.assertNotIn("savePrefs", helper)
self.assertNotIn("setRxPowerSaving", helper)
self.assertNotIn("ota_", helper)
self.assertNotIn("_prefs.rx_ps_rx_us =", helper)
if __name__ == "__main__":
unittest.main()