repeater/ble: blepwr se ukládá a přežije reboot
Výkon BLE spojení (výchozí 0 dBm) se dal měnit za běhu, ale každý reboot ho vrátil na nulu. mast-three přitom drží most z bridge-host přes zavřené okno jen na +8 dBm. `blepwr <dBm>` teď platnou hodnotu uloží do InternalFS (/ble_pwr, výchozí = žádný soubor) a boot ji načte. Neplatná hodnota (mimo −40,−20,−16,−12,−8,−4,0,2..8) skončí ERR a neuloží se. Automatický návrat oslabení po 5 minutách a `hold` jsou pryč: s uloženou hodnotou by se rozcházely s tím, co platí po rebootu. Špatně nastavený výkon se opraví přes mesh admin CLI, které na BLE nezávisí. Ověřeno na x1: ERR + neuloženo, 6 → reboot → 6, −4 → reboot → −4, 0 → reboot → 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d58e76009
commit
29ef420486
1 file changed
+40
-28
@@ -413,18 +413,10 @@ bool identHandleCommand(const char* command, char* reply) {
|
|||||||
#ifndef RPT_BLE_CONN_TX_POWER
|
#ifndef RPT_BLE_CONN_TX_POWER
|
||||||
#define RPT_BLE_CONN_TX_POWER 0 // dBm; = SoftDevice default
|
#define RPT_BLE_CONN_TX_POWER 0 // dBm; = SoftDevice default
|
||||||
#endif
|
#endif
|
||||||
// Auto-revert, because a weakened link locks you out of the console you would
|
// Measured on the bench: at -20 dBm the node's replies stop arriving and at
|
||||||
// need to undo it. Measured on the bench: at -20 dBm the node's replies stop
|
// -40 dBm a connection cannot be established at all. A too-low value set by
|
||||||
// arriving, and at -40 dBm a connection cannot be established at all -- and
|
// mistake is fixed over the mesh admin CLI (`blepwr` works there too).
|
||||||
// since the value is re-applied on every 'secured', reconnecting does not
|
|
||||||
// help. On the mast the only way back would be `reboot` over the mesh admin
|
|
||||||
// CLI. So any weakening reverts on its own unless refreshed; `blepwr <p> hold`
|
|
||||||
// opts out. Not persisted, so a reboot always comes up at the default anyway.
|
|
||||||
#ifndef RPT_BLE_PWR_REVERT_MS
|
|
||||||
#define RPT_BLE_PWR_REVERT_MS 300000 // 5 min
|
|
||||||
#endif
|
|
||||||
static int8_t ble_conn_tx_power = RPT_BLE_CONN_TX_POWER;
|
static int8_t ble_conn_tx_power = RPT_BLE_CONN_TX_POWER;
|
||||||
static unsigned long ble_pwr_revert_at = 0; // 0 = no revert armed
|
|
||||||
|
|
||||||
// CUSTOM (TeTeHacko): last connection parameters actually in force, latched
|
// CUSTOM (TeTeHacko): last connection parameters actually in force, latched
|
||||||
// from loop() so they can be read over USB after the central has gone --
|
// from loop() so they can be read over USB after the central has gone --
|
||||||
@@ -482,6 +474,33 @@ bool identHandleCommand(const char* command, char* reply) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CUSTOM (TeTeHacko): `blepwr` survives a reboot (marker file like `ble off`).
|
||||||
|
// 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) {
|
||||||
|
return p == -40 || p == -20 || p == -16 || p == -12 || p == -8 || p == -4 || p == 0
|
||||||
|
|| (p >= 2 && p <= 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void bleConnTxPowerPersist(int8_t p) {
|
||||||
|
InternalFS.remove(BLE_PWR_FILE); // FILE_O_WRITE appends; start clean
|
||||||
|
if (p == RPT_BLE_CONN_TX_POWER) return; // default needs no file
|
||||||
|
auto f = InternalFS.open(BLE_PWR_FILE, Adafruit_LittleFS_Namespace::FILE_O_WRITE);
|
||||||
|
if (f) { f.write((uint8_t)p); f.close(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
static int8_t bleConnTxPowerLoad() {
|
||||||
|
int8_t p = RPT_BLE_CONN_TX_POWER;
|
||||||
|
auto f = InternalFS.open(BLE_PWR_FILE, Adafruit_LittleFS_Namespace::FILE_O_READ);
|
||||||
|
if (f) {
|
||||||
|
uint8_t b;
|
||||||
|
if (f.read(&b, 1) == 1 && bleConnTxPowerValid((int8_t)b)) p = (int8_t)b;
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
static void bleApply(bool on) {
|
static void bleApply(bool on) {
|
||||||
if (on) {
|
if (on) {
|
||||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||||
@@ -517,31 +536,30 @@ bool identHandleCommand(const char* command, char* reply) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// CUSTOM (TeTeHacko): `blepwr [dBm]` -- report or set the CONNECTION tx
|
// CUSTOM (TeTeHacko): `blepwr [dBm]` -- report or set the CONNECTION tx
|
||||||
// power (see RPT_BLE_CONN_TX_POWER). Applied to every live connection AND
|
// power (see RPT_BLE_CONN_TX_POWER). Applied to every live connection,
|
||||||
// remembered for the next 'secured', so a reconnect keeps the setting.
|
// remembered for the next 'secured' and saved, so a reconnect AND a reboot
|
||||||
|
// keep the setting.
|
||||||
// The nRF52840 only accepts -40,-20,-16,-12,-8,-4,0,2..8; setTxPower()
|
// 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.
|
// rejects anything else, hence the applied/failed count in the reply.
|
||||||
if (memcmp(command, "blepwr", 6) == 0 && (command[6] == 0 || command[6] == ' ')) {
|
if (memcmp(command, "blepwr", 6) == 0 && (command[6] == 0 || command[6] == ' ')) {
|
||||||
if (command[6] == ' ') {
|
if (command[6] == ' ') {
|
||||||
int8_t p = (int8_t) atoi(&command[7]);
|
int8_t p = (int8_t) atoi(&command[7]);
|
||||||
bool hold = strstr(&command[7], "hold") != NULL;
|
|
||||||
int live = 0;
|
int live = 0;
|
||||||
int ok = bleApplyConnTxPower(p, &live);
|
int ok = bleApplyConnTxPower(p, &live);
|
||||||
// only latch a value the radio actually accepted, otherwise the next
|
// only latch a value the radio actually accepted, otherwise the next
|
||||||
// reconnect would silently re-apply a bogus one
|
// reconnect would silently re-apply a bogus one
|
||||||
bool applied = (live == 0 || ok > 0);
|
bool applied = (live == 0 || ok > 0);
|
||||||
|
// with no live connection nothing has vetted the value yet
|
||||||
|
if (live == 0 && !bleConnTxPowerValid(p)) applied = false;
|
||||||
if (applied) {
|
if (applied) {
|
||||||
ble_conn_tx_power = p;
|
ble_conn_tx_power = p;
|
||||||
ble_pwr_revert_at = (p < RPT_BLE_CONN_TX_POWER && !hold)
|
bleConnTxPowerPersist(p);
|
||||||
? millis() + RPT_BLE_PWR_REVERT_MS : 0;
|
|
||||||
}
|
}
|
||||||
sprintf(reply, "%s - BLE conn tx %d dBm (%d/%d conn)%s",
|
sprintf(reply, "%s - BLE conn tx %d dBm (%d/%d conn)%s",
|
||||||
applied ? "OK" : "ERR", (int) p, ok, live,
|
applied ? "OK" : "ERR", (int) p, ok, live, applied ? ", saved" : "");
|
||||||
ble_pwr_revert_at ? ", auto-revert armed" : "");
|
|
||||||
} else {
|
} else {
|
||||||
sprintf(reply, "BLE conn tx: %d dBm, adv tx: %d dBm%s",
|
sprintf(reply, "BLE conn tx: %d dBm, adv tx: %d dBm",
|
||||||
(int) ble_conn_tx_power, (int) Bluefruit.getTxPower(),
|
(int) ble_conn_tx_power, (int) Bluefruit.getTxPower());
|
||||||
ble_pwr_revert_at ? " (auto-revert armed)" : "");
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1014,6 +1032,7 @@ void setup() {
|
|||||||
Bluefruit.Advertising.setFastTimeout(RPT_BLE_ADV_FAST_SECS);
|
Bluefruit.Advertising.setFastTimeout(RPT_BLE_ADV_FAST_SECS);
|
||||||
// persistent BLE state: with the marker present keep BLE dark (see `ble on|off`)
|
// persistent BLE state: with the marker present keep BLE dark (see `ble on|off`)
|
||||||
ble_enabled = !InternalFS.exists(BLE_OFF_MARKER);
|
ble_enabled = !InternalFS.exists(BLE_OFF_MARKER);
|
||||||
|
ble_conn_tx_power = bleConnTxPowerLoad(); // `blepwr` saved value, if any
|
||||||
if (ble_enabled) {
|
if (ble_enabled) {
|
||||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||||
Bluefruit.Advertising.start(0);
|
Bluefruit.Advertising.start(0);
|
||||||
@@ -1060,13 +1079,6 @@ void loop() {
|
|||||||
ble_toggle_at = 0;
|
ble_toggle_at = 0;
|
||||||
bleApply(ble_toggle_target);
|
bleApply(ble_toggle_target);
|
||||||
}
|
}
|
||||||
// CUSTOM (TeTeHacko): give back the BLE tx power a `blepwr` took away, so a
|
|
||||||
// weakened link cannot outlive the console you would need to fix it.
|
|
||||||
if (ble_pwr_revert_at && (long)(millis() - ble_pwr_revert_at) >= 0) {
|
|
||||||
ble_pwr_revert_at = 0;
|
|
||||||
ble_conn_tx_power = RPT_BLE_CONN_TX_POWER;
|
|
||||||
bleApplyConnTxPower(ble_conn_tx_power, NULL);
|
|
||||||
}
|
|
||||||
// CUSTOM (TeTeHacko): latch the live connection parameters once a second, so
|
// CUSTOM (TeTeHacko): latch the live connection parameters once a second, so
|
||||||
// `bleconn` can report them after the central is gone (see ble_seen_*).
|
// `bleconn` can report them after the central is gone (see ble_seen_*).
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user