blepwr: explicitní hodnota se ukládá vždy, blepwr default ji zapomene

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-06 23:54:44 +02:00
1 parent 81ccbc2051
commit d934161a77
1 file changed
+20 -4
+20 -4
View File
@@ -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)