From 4a2d84c7a875d4362ddf1bf95c4b91fa831ae9f4 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 6 Oct 2026 23:54:44 +0200 Subject: [PATCH] =?UTF-8?q?blepwr:=20explicitn=C3=AD=20hodnota=20se=20ukl?= =?UTF-8?q?=C3=A1d=C3=A1=20v=C5=BEdy,=20`blepwr=20default`=20ji=20zapomene?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- examples/simple_repeater/main.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index caa70b59..7747073b 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -407,7 +407,7 @@ bool identHandleCommand(const char* command, char* reply) { // the link drops outright and Bluefruit re-advertises (at +8 dBm, 3 channels) // -- also radio time, and the same reconnect churn the mast shows as 0x3E. // - // Runtime and not a build flag: reflashing the XIAO costs a physical + // Runtime as well as a build flag: reflashing the XIAO costs a physical // double-tap + UF2 drop (BLE DFU cannot reach its bootloader, same MAC), and // a dose-response sweep needs many points. #ifndef RPT_BLE_CONN_TX_POWER @@ -478,15 +478,21 @@ bool identHandleCommand(const char* command, char* reply) { // mast-three's bridge reaches it through a closed window and the link only holds // at +8 dBm, which every reboot used to drop back to the 0 dBm default. #define BLE_PWR_FILE "/ble_pwr" - static bool bleConnTxPowerValid(int p) { + static constexpr bool bleConnTxPowerValid(int p) { return p == -40 || p == -20 || p == -16 || p == -12 || p == -8 || p == -4 || p == 0 || (p >= 2 && p <= 8); } + static_assert(bleConnTxPowerValid(RPT_BLE_CONN_TX_POWER), + "RPT_BLE_CONN_TX_POWER: -40,-20,-16,-12,-8,-4,0,2..8"); + // Pins an EXPLICIT value, even one equal to the build default: a file that + // only held deviations from the default would silently follow a reflash + // with a different RPT_BLE_CONN_TX_POWER (`blepwr 8` on mast-three, then an + // image without its -D, would come up at 0 dBm). `blepwr default` unpins. // true when the value will come back after a reboot static bool bleConnTxPowerPersist(int8_t p) { - InternalFS.remove(BLE_PWR_FILE); // FILE_O_WRITE appends; start clean - if (p == RPT_BLE_CONN_TX_POWER) return true; // default needs no file + // FILE_O_WRITE appends; a stale file would shadow the new byte + if (!InternalFS.remove(BLE_PWR_FILE) && InternalFS.exists(BLE_PWR_FILE)) return false; auto f = InternalFS.open(BLE_PWR_FILE, Adafruit_LittleFS_Namespace::FILE_O_WRITE); if (!f) return false; bool ok = f.write((uint8_t)p) == 1; @@ -546,6 +552,16 @@ bool identHandleCommand(const char* command, char* reply) { // The nRF52840 only accepts -40,-20,-16,-12,-8,-4,0,2..8; setTxPower() // rejects anything else, hence the applied/failed count in the reply. if (memcmp(command, "blepwr", 6) == 0 && (command[6] == 0 || command[6] == ' ')) { + if (strcmp(&command[6], " default") == 0) { + // back to the build default: drop the saved value, apply it now + InternalFS.remove(BLE_PWR_FILE); + ble_conn_tx_power = RPT_BLE_CONN_TX_POWER; + int live = 0; + int ok = bleApplyConnTxPower(ble_conn_tx_power, &live); + sprintf(reply, "OK - BLE conn tx %d dBm = build default (%d/%d conn), not saved", + (int) ble_conn_tx_power, ok, live); + return true; + } if (command[6] == ' ') { // strict parse: now that the value is saved, a typo must not become // permanent (atoi turned "216" into -40 and "abc" into 0)