simple_repeater: ble pin <6 digits> -- own BLE PIN, saved; public builds refuse ble on without it
pub-release / release (push) Successful in 23m33s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-03 01:37:38 +01:00
1 parent 1cfa726cdd
commit c1eaa6f98b
2 files changed
+91 -11

No files matched your search

+83 -5
View File
@@ -462,8 +462,8 @@ bool identHandleCommand(const char* command, char* reply) {
//
// RPT_BLE_DEFAULT_OFF (public builds): the console starts OFF and `ble on`
// is what gets saved (marker /ble_on). Those builds carry the placeholder PIN
// 123456, and a console that is on after a fresh flash would let anyone in
// range pair with it before the owner changes the PIN.
// 123456, and the console has no login of its own -- the PIN is all that
// guards it. So there `ble on` is refused until `ble pin` has set another.
#include <InternalFileSystem.h>
#define BLE_OFF_MARKER "/ble_off"
#define BLE_ON_MARKER "/ble_on"
@@ -537,6 +537,44 @@ bool identHandleCommand(const char* command, char* reply) {
return p;
}
// CUSTOM (TeTeHacko): `ble pin <6 digits>` -- the BLE PIN, saved in
// InternalFS so it survives a reboot and a reflash (erase drops it). Without
// it the PIN is the build's BLE_PIN_CODE, which in a public build everybody
// knows. A new PIN clears the bonds: a central that paired with the old one
// would otherwise keep its access without ever knowing the new PIN.
#define BLE_PIN_FILE "/ble_pin"
static char ble_pin[7] = _MC_STR(BLE_PIN_CODE); // the SoftDevice keeps the pointer
static bool ble_pin_custom = false;
static unsigned long ble_pin_reset_at = 0; // deferred so the reply can go out first
static_assert(sizeof(_MC_STR(BLE_PIN_CODE)) == 7, "BLE_PIN_CODE must be 6 digits");
static bool blePinValid(const char* p) {
if (strlen(p) != 6) return false;
for (int i = 0; i < 6; i++) if (p[i] < '0' || p[i] > '9') return false;
return true;
}
static void blePinLoad() {
auto f = InternalFS.open(BLE_PIN_FILE, Adafruit_LittleFS_Namespace::FILE_O_READ);
if (!f) return;
char b[7] = {0};
if (f.read((uint8_t*)b, 6) == 6 && blePinValid(b)) {
memcpy(ble_pin, b, 7);
ble_pin_custom = strcmp(ble_pin, _MC_STR(BLE_PIN_CODE)) != 0;
}
f.close();
}
static bool blePinSave(const char* p) {
// FILE_O_WRITE appends; a stale file would shadow the new digits
if (!InternalFS.remove(BLE_PIN_FILE) && InternalFS.exists(BLE_PIN_FILE)) return false;
auto f = InternalFS.open(BLE_PIN_FILE, Adafruit_LittleFS_Namespace::FILE_O_WRITE);
if (!f) return false;
bool ok = f.write((const uint8_t*)p, 6) == 6;
f.close();
return ok;
}
static void bleApply(bool on) {
if (on) {
Bluefruit.Advertising.restartOnDisconnect(true);
@@ -554,6 +592,12 @@ bool identHandleCommand(const char* command, char* reply) {
// called from MyMesh::handleCommand (serial, BLE console and mesh admin CLI)
bool bleConsoleHandleCommand(const char* command, char* reply) {
if (strcmp(command, "ble on") == 0) {
#ifdef RPT_BLE_DEFAULT_OFF
if (!ble_pin_custom) {
strcpy(reply, "ERR - set your own PIN first: ble pin <6 digits>");
return true;
}
#endif
ble_enabled = true; blePersist(true);
ble_toggle_target = true; ble_toggle_at = millis() + 1500;
strcpy(reply, "OK - BLE on (persistent)");
@@ -566,9 +610,32 @@ bool identHandleCommand(const char* command, char* reply) {
return true;
}
if (strcmp(command, "ble") == 0) {
sprintf(reply, "BLE: %s%s", ble_enabled ? "on" : "off",
sprintf(reply, "BLE: %s%s, PIN %s", ble_enabled ? "on" : "off",
Bluefruit.connected() ? " (connected)"
: (Bluefruit.Advertising.isRunning() ? " (advertising)" : ""));
: (Bluefruit.Advertising.isRunning() ? " (advertising)" : ""),
ble_pin_custom ? "own" : "build default");
return true;
}
// the PIN itself is never printed: the reply may travel over the mesh
if (strcmp(command, "ble pin") == 0) {
sprintf(reply, "BLE PIN: %s", ble_pin_custom ? "own (saved)" : "build default");
return true;
}
if (memcmp(command, "ble pin ", 8) == 0) {
const char* p = &command[8];
if (!blePinValid(p)) {
strcpy(reply, "ERR - ble pin <6 digits>");
return true;
}
if (!blePinSave(p)) {
strcpy(reply, "ERR - BLE PIN not saved");
return true;
}
memcpy(ble_pin, p, 7);
ble_pin_custom = strcmp(ble_pin, _MC_STR(BLE_PIN_CODE)) != 0;
Bluefruit.Security.setPIN(ble_pin);
ble_pin_reset_at = millis() + 1500;
strcpy(reply, "OK - BLE PIN saved, bonds cleared: pair again");
return true;
}
// CUSTOM (TeTeHacko): `blepwr [dBm]` -- report or set the CONNECTION tx
@@ -989,7 +1056,8 @@ void setup() {
const char* nm = the_mesh.getNodePrefs()->node_name;
Bluefruit.setName((nm && nm[0]) ? nm : BLE_DEVICE_NAME);
}
Bluefruit.Security.setPIN(_MC_STR(BLE_PIN_CODE));
blePinLoad(); // `ble pin` saved value, else the build's BLE_PIN_CODE
Bluefruit.Security.setPIN(ble_pin);
// widen the BLE connection params so the busy LoRa loop can't trip the
// (default 2 s) supervision timeout -> no more reconnect churn. PPCP is
// advertised AND requested explicitly on 'secured' (bleOnSecured).
@@ -1059,6 +1127,16 @@ void loop() {
ble_toggle_at = 0;
bleApply(ble_toggle_target);
}
// CUSTOM (TeTeHacko): new BLE PIN -- drop every link and every bond made
// under the old one (see `ble pin`)
if (ble_pin_reset_at && (long)(millis() - ble_pin_reset_at) >= 0) {
ble_pin_reset_at = 0;
for (uint16_t h = 0; h < BLE_MAX_CONNECTION; h++) {
BLEConnection* c = Bluefruit.Connection(h);
if (c && c->connected()) c->disconnect();
}
Bluefruit.Periph.clearBonds();
}
// CUSTOM (TeTeHacko): latch the live connection parameters once a second, so
// `bleconn` can report them after the central is gone (see ble_seen_*).
{