repeater/ble: blepwr se ukládá a přežije reboot
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c073b0db0d
commit
04db619dc7
1 file changed
+40
-28
@@ -413,18 +413,10 @@ bool identHandleCommand(const char* command, char* reply) {
|
||||
#ifndef RPT_BLE_CONN_TX_POWER
|
||||
#define RPT_BLE_CONN_TX_POWER 0 // dBm; = SoftDevice default
|
||||
#endif
|
||||
// Auto-revert, because a weakened link locks you out of the console you would
|
||||
// need to undo it. Measured on the bench: at -20 dBm the node's replies stop
|
||||
// arriving, and at -40 dBm a connection cannot be established at all -- and
|
||||
// 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
|
||||
// Measured on the bench: at -20 dBm the node's replies stop arriving and at
|
||||
// -40 dBm a connection cannot be established at all. A too-low value set by
|
||||
// mistake is fixed over the mesh admin CLI (`blepwr` works there too).
|
||||
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
|
||||
// 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) {
|
||||
if (on) {
|
||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||
@@ -517,31 +536,30 @@ bool identHandleCommand(const char* command, char* reply) {
|
||||
return true;
|
||||
}
|
||||
// CUSTOM (TeTeHacko): `blepwr [dBm]` -- report or set the CONNECTION tx
|
||||
// power (see RPT_BLE_CONN_TX_POWER). Applied to every live connection AND
|
||||
// remembered for the next 'secured', so a reconnect keeps the setting.
|
||||
// power (see RPT_BLE_CONN_TX_POWER). Applied to every live connection,
|
||||
// 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()
|
||||
// rejects anything else, hence the applied/failed count in the reply.
|
||||
if (memcmp(command, "blepwr", 6) == 0 && (command[6] == 0 || command[6] == ' ')) {
|
||||
if (command[6] == ' ') {
|
||||
int8_t p = (int8_t) atoi(&command[7]);
|
||||
bool hold = strstr(&command[7], "hold") != NULL;
|
||||
int live = 0;
|
||||
int ok = bleApplyConnTxPower(p, &live);
|
||||
// only latch a value the radio actually accepted, otherwise the next
|
||||
// reconnect would silently re-apply a bogus one
|
||||
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) {
|
||||
ble_conn_tx_power = p;
|
||||
ble_pwr_revert_at = (p < RPT_BLE_CONN_TX_POWER && !hold)
|
||||
? millis() + RPT_BLE_PWR_REVERT_MS : 0;
|
||||
bleConnTxPowerPersist(p);
|
||||
}
|
||||
sprintf(reply, "%s - BLE conn tx %d dBm (%d/%d conn)%s",
|
||||
applied ? "OK" : "ERR", (int) p, ok, live,
|
||||
ble_pwr_revert_at ? ", auto-revert armed" : "");
|
||||
applied ? "OK" : "ERR", (int) p, ok, live, applied ? ", saved" : "");
|
||||
} else {
|
||||
sprintf(reply, "BLE conn tx: %d dBm, adv tx: %d dBm%s",
|
||||
(int) ble_conn_tx_power, (int) Bluefruit.getTxPower(),
|
||||
ble_pwr_revert_at ? " (auto-revert armed)" : "");
|
||||
sprintf(reply, "BLE conn tx: %d dBm, adv tx: %d dBm",
|
||||
(int) ble_conn_tx_power, (int) Bluefruit.getTxPower());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1014,6 +1032,7 @@ void setup() {
|
||||
Bluefruit.Advertising.setFastTimeout(RPT_BLE_ADV_FAST_SECS);
|
||||
// persistent BLE state: with the marker present keep BLE dark (see `ble on|off`)
|
||||
ble_enabled = !InternalFS.exists(BLE_OFF_MARKER);
|
||||
ble_conn_tx_power = bleConnTxPowerLoad(); // `blepwr` saved value, if any
|
||||
if (ble_enabled) {
|
||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||
Bluefruit.Advertising.start(0);
|
||||
@@ -1060,13 +1079,6 @@ void loop() {
|
||||
ble_toggle_at = 0;
|
||||
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
|
||||
// `bleconn` can report them after the central is gone (see ble_seen_*).
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user