From 94a3b7e568781533510721670af24c162804a5ca Mon Sep 17 00:00:00 2001 From: mikecarper Date: Thu, 22 Oct 2026 18:13:14 -0700 Subject: [PATCH] Omit optional battery controls from Mini Repeater --- build_legacy.sh | 4 +- docs/cli_commands.md | 4 ++ src/helpers/BatteryForwarding.h | 18 ++++-- src/helpers/BatteryForwardingConfig.h | 9 +++ src/helpers/CommonCLI.cpp | 10 +++ src/helpers/CommonCLI.h | 11 ++++ src/helpers/CommonCLI_Battery.cpp | 16 ++++- test/test_battery_forwarding.py | 80 ++++++++++++++++++++++++ test/test_stm32_mini_repeater_profile.py | 27 +++++++- variants/wio-e5-mini/platformio.ini | 1 + 10 files changed, 170 insertions(+), 10 deletions(-) create mode 100644 src/helpers/BatteryForwardingConfig.h diff --git a/build_legacy.sh b/build_legacy.sh index 4bab183fd..be5b64f67 100755 --- a/build_legacy.sh +++ b/build_legacy.sh @@ -3511,7 +3511,7 @@ declare_build_capability_contract() { declare_full_logging_application_contract "$env_name" record_build_capability "profile.${BUILD_PROFILE_FOR_TARGET}" if [ "$env_name" = wio-e5-mini_repeater ]; then - # This exact fixed-capacity recipe intentionally omits these three + # This exact fixed-capacity recipe intentionally omits these four # optional services. Keep the packaged contract explicit about its cuts. record_build_reduction \ "radio.carrier_tools omitted to preserve the Mini Repeater app boundary" @@ -3519,6 +3519,8 @@ declare_build_capability_contract() { "management.reporting and data.tx omitted to preserve the Mini Repeater app boundary" record_build_reduction \ "logging.file omitted; live USB/MQTT packet logging retained" + record_build_reduction \ + "battery.forwarding omitted; battery alerts and readings retained" fi pio_env_name=$(get_pio_build_env "$env_name") if is_qspi_hardware_qualification_target "$env_name"; then diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 69bd4d8a7..b1f120221 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -2610,6 +2610,10 @@ Repeater, Room Server, and Sensor firmware can automatically pause forwarding below a configured battery percentage. Both controls default to `off`, including when upgrading. They are independent of battery alerts. +`wio-e5-mini_repeater` omits these controls and the adjustable battery voltage +range below to fit its application partition. Battery alerts and basic battery +readings remain available. + **Usage:** - `get repeat.battery` diff --git a/src/helpers/BatteryForwarding.h b/src/helpers/BatteryForwarding.h index b5aacf00f..1c5aadc31 100644 --- a/src/helpers/BatteryForwarding.h +++ b/src/helpers/BatteryForwarding.h @@ -1,5 +1,6 @@ #pragma once +#include "BatteryForwardingConfig.h" #include #include #include @@ -7,6 +8,15 @@ namespace mesh { namespace batteryforward { +// Percentage reporting also serves the existing low-battery alerts when the +// optional forwarding controls are omitted from a constrained image. +inline uint8_t percent(uint16_t mv, uint16_t empty, uint16_t full) { + if (full <= empty || mv >= full) return 100; + if (mv <= empty) return 0; + return uint8_t(uint32_t(mv - empty) * 100U / (full - empty)); +} + +#if MESH_ENABLE_BATTERY_FORWARDING static constexpr uint8_t Repeat = 1; static constexpr uint8_t Flood = 2; static constexpr uint32_t SampleIntervalMs = 30000; @@ -66,12 +76,6 @@ inline bool decode(const uint8_t* in, size_t size, Settings& s) { return true; } -inline uint8_t percent(uint16_t mv, uint16_t empty, uint16_t full) { - if (full <= empty || mv >= full) return 100; - if (mv <= empty) return 0; - return uint8_t(uint32_t(mv - empty) * 100U / (full - empty)); -} - struct State { uint32_t sampledAt = 0; uint16_t millivolts = 0; @@ -185,4 +189,6 @@ bool handleCommand(Settings& s, const State& state, uint16_t empty, uint16_t ful return true; } +#endif // MESH_ENABLE_BATTERY_FORWARDING + }} // namespace mesh::batteryforward diff --git a/src/helpers/BatteryForwardingConfig.h b/src/helpers/BatteryForwardingConfig.h new file mode 100644 index 000000000..d0d406c40 --- /dev/null +++ b/src/helpers/BatteryForwardingConfig.h @@ -0,0 +1,9 @@ +#pragma once + +#ifndef MESH_ENABLE_BATTERY_FORWARDING + #define MESH_ENABLE_BATTERY_FORWARDING 1 +#endif + +#if MESH_ENABLE_BATTERY_FORWARDING != 0 && MESH_ENABLE_BATTERY_FORWARDING != 1 + #error "MESH_ENABLE_BATTERY_FORWARDING must be 0 or 1" +#endif diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 6e8f845b1..2ef9d84a3 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -186,7 +186,9 @@ static bool isGpioConfig(const char* config) { #endif void CommonCLI::loop() { +#if MESH_ENABLE_BATTERY_FORWARDING loopBatteryForwarding(); +#endif #if MESH_ENABLE_MORSE_RX if (_morse_reply.pending()) { const ClientInfo* actor = _acl->getClient(_morse_reply.actor(), PUB_KEY_SIZE); @@ -801,7 +803,9 @@ static void formatSnrDbX4Short(char* dest, size_t dest_len, int16_t snr_x4) { } void CommonCLI::loadPrefs(FILESYSTEM* fs) { +#if MESH_ENABLE_BATTERY_FORWARDING loadBatteryForwarding(fs); +#endif mesh::hilStartupTrace("prefs_profiles_begin"); _radio_profiles.begin(fs, _callbacks->getProfileRadio(), _rtc, true); #if MESH_ENABLE_CARRIER_TOOLS || MESH_ENABLE_MORSE_RX @@ -3260,7 +3264,9 @@ bool CommonCLI::handleUDPBridgeCommand(uint32_t sender_timestamp, void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* reply) { PrefsSaveReplyGuard save_reply(_prefs_save_failures, reply); mesh::cli::normalizeCommandVerb(command); +#if MESH_ENABLE_BATTERY_FORWARDING if (handleBatteryForwardingCommand(command, reply)) return; +#endif #if defined(WITH_UDP_BRIDGE) if (handleUDPBridgeCommand(sender_timestamp, command, reply)) return; #endif @@ -4224,7 +4230,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep } #if defined(NRF52_POWER_MANAGEMENT) if (mesh::power::handleVoltagePolicyCommand(command, reply, 160)) { +#if MESH_ENABLE_BATTERY_FORWARDING if (strncmp(reply, "OK", 2) == 0) loopBatteryForwarding(true); +#endif return; } #endif @@ -5716,7 +5724,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep } else if (_board->setAdcMultiplier(multiplier)) { _prefs->adc_multiplier = multiplier; savePrefs(); +#if MESH_ENABLE_BATTERY_FORWARDING loopBatteryForwarding(true); +#endif if (_prefs->adc_multiplier == 0.0f) { strcpy(reply, "OK - using default board multiplier"); } else { diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 47c932003..095b491d7 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -872,6 +872,7 @@ class CommonCLI { NodePrefs* _prefs; CommonCLICallbacks* _callbacks; mesh::MainBoard* _board; +#if MESH_ENABLE_BATTERY_FORWARDING FILESYSTEM* _battery_forward_fs = nullptr; mesh::batteryforward::Settings _battery_forward_settings; mutable mesh::batteryforward::State _battery_forward_state; @@ -879,6 +880,11 @@ class CommonCLI { bool saveBatteryForwarding(const mesh::batteryforward::Settings& settings); void loopBatteryForwarding(bool force = false) const; bool handleBatteryForwardingCommand(const char* command, char* reply); +#else + void loadBatteryForwarding(FILESYSTEM*) {} + void loopBatteryForwarding(bool = false) const {} + bool handleBatteryForwardingCommand(const char*, char*) { return false; } +#endif uint16_t batteryEmptyMv() const; uint16_t batteryFullMv() const; #if defined(WITH_ESPNOW_BRIDGE) \ @@ -1019,8 +1025,13 @@ public: FILESYSTEM* _fs, PrefsSaveRouting::Scope scope = PrefsSaveRouting::Scope::Both); void loop(); +#if MESH_ENABLE_BATTERY_FORWARDING bool batteryAllowsForward(bool flood) const; bool batteryAllowsTransmit(const mesh::Packet* packet) const; +#else + bool batteryAllowsForward(bool) const { return true; } + bool batteryAllowsTransmit(const mesh::Packet*) const { return true; } +#endif uint8_t batteryPercent(uint16_t mv) const; bool hasActiveUserGpioTimer() const; void handleCommand(uint32_t sender_timestamp, char* command, char* reply); diff --git a/src/helpers/CommonCLI_Battery.cpp b/src/helpers/CommonCLI_Battery.cpp index 5821acbff..1bbfcc3bc 100644 --- a/src/helpers/CommonCLI_Battery.cpp +++ b/src/helpers/CommonCLI_Battery.cpp @@ -1,9 +1,12 @@ #include "CommonCLI.h" +#if MESH_ENABLE_BATTERY_FORWARDING #include "CheckedSettingsFile.h" +#endif #include #if defined(NRF52_PLATFORM) && defined(NRF52_POWER_MANAGEMENT) #include "NRF52VoltagePolicy.h" #endif +#if MESH_ENABLE_BATTERY_FORWARDING #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #include "AtomicFileWriter.h" #else @@ -56,6 +59,7 @@ bool CommonCLI::saveBatteryForwarding(const mesh::batteryforward::Settings& sett verified, sizeof(verified)) && memcmp(bytes, verified, sizeof(bytes)) == 0; #endif } +#endif // MESH_ENABLE_BATTERY_FORWARDING uint16_t CommonCLI::batteryEmptyMv() const { #if defined(NRF52_PLATFORM) && defined(NRF52_POWER_MANAGEMENT) @@ -66,7 +70,10 @@ uint16_t CommonCLI::batteryEmptyMv() const { #else const uint16_t compiled = 3000; #endif - return _battery_forward_settings.emptyMv ? _battery_forward_settings.emptyMv : compiled; +#if MESH_ENABLE_BATTERY_FORWARDING + if (_battery_forward_settings.emptyMv) return _battery_forward_settings.emptyMv; +#endif + return compiled; #endif } @@ -79,7 +86,10 @@ uint16_t CommonCLI::batteryFullMv() const { #else const uint16_t compiled = 4200; #endif - return _battery_forward_settings.fullMv ? _battery_forward_settings.fullMv : compiled; +#if MESH_ENABLE_BATTERY_FORWARDING + if (_battery_forward_settings.fullMv) return _battery_forward_settings.fullMv; +#endif + return compiled; #endif } @@ -87,6 +97,7 @@ uint8_t CommonCLI::batteryPercent(uint16_t mv) const { return mesh::batteryforward::percent(mv, batteryEmptyMv(), batteryFullMv()); } +#if MESH_ENABLE_BATTERY_FORWARDING bool CommonCLI::batteryAllowsTransmit(const mesh::Packet* packet) const { // Explicit relay ownership survives path stripping and retry clones. Own // replies and OTA session traffic are not inferred from radio routing flags. @@ -130,3 +141,4 @@ bool CommonCLI::handleBatteryForwardingCommand(const char* command, char* reply) if (saved) loopBatteryForwarding(true); return handled; } +#endif // MESH_ENABLE_BATTERY_FORWARDING diff --git a/test/test_battery_forwarding.py b/test/test_battery_forwarding.py index 6c6de2559..8fb9ed3e1 100644 --- a/test/test_battery_forwarding.py +++ b/test/test_battery_forwarding.py @@ -1,6 +1,7 @@ """Run the production battery forwarding policy, CLI and record under sanitizers.""" from pathlib import Path import os +import re import subprocess import tempfile import unittest @@ -128,6 +129,85 @@ struct CommonCLI { }; '''.replace("EXTRA_MEMBERS", extra_members) + methods + def disabled_runtime_prefix(self): + """Compile the real disabled declarations and entire battery implementation. + + No board or filesystem implementation is provided: a retained sampler, + persistence path, or policy member makes this fixture fail to compile. + """ + header = (ROOT / "src/helpers/CommonCLI.h").read_text(encoding="ascii") + start = header.index("#if MESH_ENABLE_BATTERY_FORWARDING\n FILESYSTEM* _battery_forward_fs") + end = header.index("#if defined(WITH_ESPNOW_BRIDGE)", start) + private_api = header[start:end] + start = header.index("#if MESH_ENABLE_BATTERY_FORWARDING\n bool batteryAllowsForward") + end = header.index(" bool hasActiveUserGpioTimer()", start) + public_api = header[start:end] + implementation = (ROOT / "src/helpers/CommonCLI_Battery.cpp").read_text(encoding="ascii") + implementation = re.sub(r"^#include[^\n]*\n", "", implementation, flags=re.MULTILINE) + return r''' +#include +#include +#include +#include +namespace mesh { struct Packet; } +struct FILESYSTEM; +class CommonCLI { +public: +''' + private_api + public_api + r''' +}; +static_assert(sizeof(CommonCLI) == 1, "disabled forwarding must not retain state"); +''' + implementation + + def test_disabled_actual_common_cli_has_no_storage_adc_or_forwarding_commands(self): + prefix = self.disabled_runtime_prefix() + for extra_flags, empty, full in (((), 3000, 4200), + (("-DBATT_MIN_MILLIVOLTS=6000", "-DBATT_MAX_MILLIVOLTS=8400"), + 6000, 8400)): + with self.subTest(endpoints=(empty, full)): + self.exercise(r''' + static_assert(MESH_ENABLE_BATTERY_FORWARDING == 0, "explicit opt-out"); + CommonCLI cli; + cli.loadBatteryForwarding(reinterpret_cast(1)); + cli.loopBatteryForwarding(); + cli.loopBatteryForwarding(true); + assert(cli.batteryAllowsForward(false) && cli.batteryAllowsForward(true)); + assert(cli.batteryAllowsTransmit(nullptr)); + assert(cli.batteryAllowsTransmit(reinterpret_cast(1))); + char reply[] = "untouched"; + for (const char* command : {"get repeat.battery", "set repeat.battery on", + "get flood.battery", "set flood.battery 20", + "get battery.forward", "set battery.empty 3200", + "get battery.full"}) { + assert(!cli.handleBatteryForwardingCommand(command, reply)); + assert(strcmp(reply, "untouched") == 0); + } + assert(cli.batteryEmptyMv() == EMPTY && cli.batteryFullMv() == FULL); + assert(cli.batteryPercent(EMPTY - 1) == 0); + assert(cli.batteryPercent(EMPTY) == 0); + assert(cli.batteryPercent((EMPTY + FULL) / 2) == 50); + assert(cli.batteryPercent(FULL) == 100); + assert(cli.batteryPercent(FULL + 1) == 100); +'''.replace("EMPTY", str(empty)).replace("FULL", str(full)), prefix=prefix, + flags=("-DSTM32_PLATFORM", "-DMESH_ENABLE_BATTERY_FORWARDING=0", + *extra_flags)) + + def test_stm32_explicit_opt_in_keeps_real_commands_and_admission(self): + self.exercise(r''' + static_assert(MESH_ENABLE_BATTERY_FORWARDING == 1, "explicit opt-in"); + Board board; + board.mv = 3108; + CommonCLI cli(board); + char reply[160] = {}; + assert(cli.handleBatteryForwardingCommand("set repeat.battery 15", reply)); + assert(strcmp(reply, "OK - saved") == 0 && board.reads == 1 && cli.saveCalls == 1); + assert(!cli.batteryAllowsForward(false) && !cli.batteryAllowsForward(true)); + assert(cli.handleBatteryForwardingCommand("get battery.forward", reply)); + assert(strcmp(reply, "> battery=9% mv=3108 repeat=blocked flood=blocked") == 0); + assert(cli.handleBatteryForwardingCommand("set repeat.battery off", reply)); + assert(cli.batteryAllowsForward(false) && cli.batteryAllowsForward(true)); +''', prefix=self.runtime_prefix(), + flags=("-DSTM32_PLATFORM", "-DMESH_ENABLE_BATTERY_FORWARDING=1")) + def test_actual_set_command_voltage_policy_branch_refreshes_profile_endpoints_and_calibration(self): source = (ROOT / "src/helpers/CommonCLI.cpp").read_text(encoding="ascii") setter = extract_braced(source, "void CommonCLI::handleSetCmd(") diff --git a/test/test_stm32_mini_repeater_profile.py b/test/test_stm32_mini_repeater_profile.py index 292247c4c..a691d7c56 100644 --- a/test/test_stm32_mini_repeater_profile.py +++ b/test/test_stm32_mini_repeater_profile.py @@ -2,9 +2,11 @@ """Guard the constrained Mini Repeater recipe without bypassing size/storage gates.""" import configparser +import os import re import runpy import shlex +import shutil import subprocess import textwrap from pathlib import Path @@ -25,6 +27,7 @@ OPTIONAL_CUTS = ( "MESH_ENABLE_CARRIER_TOOLS", "MESH_ENABLE_MANAGEMENT_REPORTING", "MESH_ENABLE_PACKET_FILE_LOGGING", + "MESH_ENABLE_BATTERY_FORWARDING", ) @@ -65,7 +68,7 @@ class MiniRepeaterProfileTest(unittest.TestCase): self.assertNotIn("MAX_CLIENTS", self.flags) self.assertNotIn("--no-check", self.repeater.get("extra_scripts", "")) - def test_three_opt_outs_apply_only_to_exact_mini_repeater(self): + def test_four_opt_outs_apply_only_to_exact_mini_repeater(self): for macro in OPTIONAL_CUTS: self.assertRegex(self.flags, r"-D\s+" + macro + r"=0\b") for path in [ROOT / "platformio.ini", *sorted((ROOT / "variants").rglob("platformio.ini"))]: @@ -149,6 +152,7 @@ printf '%s\n' "${BUILD_REDUCTIONS[@]}" "radio.carrier_tools omitted to preserve the Mini Repeater app boundary", "management.reporting and data.tx omitted to preserve the Mini Repeater app boundary", "logging.file omitted; live USB/MQTT packet logging retained", + "battery.forwarding omitted; battery alerts and readings retained", ] for target in (TARGET, "wio-e5-mini_companion_radio_usb", "wio-e5-mini_sensor", "wio-e5_repeater", "RAK_3x72_repeater", TARGET + "_custom"): @@ -161,6 +165,27 @@ printf '%s\n' "${BUILD_REDUCTIONS[@]}" self.assertEqual(result.stdout.splitlines(), ["caller.record", *(omissions if target == TARGET else ())]) + def test_battery_forwarding_defaults_on_and_rejects_invalid_feature_values(self): + compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++") + self.assertIsNotNone(compiler, "host C++ compiler required for the battery feature gate") + source = "#include \nMESH_ENABLE_BATTERY_FORWARDING\n" + for flags, expected in (([], "1"), (["-DMESH_ENABLE_BATTERY_FORWARDING=0"], "0"), + (["-DMESH_ENABLE_BATTERY_FORWARDING=1"], "1")): + with self.subTest(flags=flags): + result = subprocess.run([compiler, "-E", "-P", "-x", "c++", "-", + "-I", str(ROOT / "src"), *flags], + input=source, capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), expected) + for value in ("-1", "2", "255"): + with self.subTest(value=value): + result = subprocess.run([compiler, "-E", "-P", "-x", "c++", "-", + "-I", str(ROOT / "src"), + "-DMESH_ENABLE_BATTERY_FORWARDING=" + value], + input=source, capture_output=True, text=True, timeout=10) + self.assertNotEqual(result.returncode, 0) + self.assertIn("MESH_ENABLE_BATTERY_FORWARDING", result.stderr) + def test_ci_links_exact_cable_only_profile_with_release_logging_and_gates(self): workflow = (ROOT / ".github/workflows/run-unit-tests.yml").read_text() job = re.search(r"^ stm32-mini-repeater-smoke:\n(.*?)(?=^ [\w-]+:|\Z)", diff --git a/variants/wio-e5-mini/platformio.ini b/variants/wio-e5-mini/platformio.ini index a302c8e01..747958315 100644 --- a/variants/wio-e5-mini/platformio.ini +++ b/variants/wio-e5-mini/platformio.ini @@ -62,6 +62,7 @@ build_flags = ${lora_e5_mini.build_flags} -D MESH_ENABLE_CARRIER_TOOLS=0 -D MESH_ENABLE_MANAGEMENT_REPORTING=0 -D MESH_ENABLE_PACKET_FILE_LOGGING=0 + -D MESH_ENABLE_BATTERY_FORWARDING=0 ; retain battery alerts and the existing 16 KiB filesystem -D MESH_ENABLE_RECENT_REPEATERS=1 -D LORA_TX_POWER=22 -D ADVERT_NAME='"wio-e5-mini Repeater"'