From c1eaa6f98ba0f4f27bd76320f95c1d9fb78df947 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 3 Nov 2026 01:37:38 +0100 Subject: [PATCH] simple_repeater: `ble pin <6 digits>` -- own BLE PIN, saved; public builds refuse `ble on` without it Co-Authored-By: Claude Opus 5.5 --- README.md | 14 ++--- examples/simple_repeater/main.cpp | 88 +++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0f179405..d5e9cad8 100644 --- a/README.md +++ b/README.md @@ -101,12 +101,14 @@ Observer variants (`*_repeater-observer-*`) of the nRF52 repeaters (XIAO, SenseCAP Solar, RAK4631, T1000-E, Heltec T114) add the `nodes` / `rxlog` / `pktlog` rings and a text console over BLE, for reading the repeater over USB or BLE ([meshcore-repeater-observer](https://git.rfa.cz/tth-pub/meshcore-repeater-observer)). -The BLE console is **off** after flashing (`RPT_BLE_DEFAULT_OFF`) and should -stay off on a public build: it has no login of its own, only the BLE PIN, and -that PIN is fixed at build time to `123456` -- the console cannot change it. -Anyone in BLE range who knows the default would get the full admin console. -Use the observer over USB, or build the env yourself with your own -`-D BLE_PIN_CODE=` before `ble on`. +The BLE console is **off** after flashing (`RPT_BLE_DEFAULT_OFF`). It has no +login of its own, only the BLE PIN, and the build's PIN is the public +placeholder `123456`. So first set your own, in the USB console: +`ble pin <6 digits>` (saved, survives a reboot and a reflash; it also drops +every existing bond, so paired devices pair again). Only then `ble on` -- +these builds refuse it while the PIN is still the placeholder. `ble` shows +whether the PIN is `own` or `build default`, never the PIN itself. Releases +up to `tth-654d53f3` do not have `ble pin`: keep BLE off there and use USB. ### Flashing a release diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 8c891194..a1eb12bc 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -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 #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_*). {