From 40c166fa7ff5a4364ffca99eac05da8da72e87fb Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 11 Jul 2026 19:21:11 +0200 Subject: [PATCH] feat: dual BLE/USB serial interface + dock-quiet BLE Backport of PR #1779 (BLE companion usable over USB while BLE disconnected), plus: track the active transport (secure-transport gating), and stop BLE advertising while a USB host is connected (docked) so the node stays quiet on 2.4 GHz; resume advertising when undocked. An active BLE link is never force-disconnected. Co-Authored-By: Claude Opus 4.8 --- examples/companion_radio/main.cpp | 11 ++- examples/companion_radio/ui-new/UITask.cpp | 2 +- src/helpers/SerialDualInterface.h | 102 +++++++++++++++++++++ 3 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 src/helpers/SerialDualInterface.h diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index ef9b6bfc..cb7b22b5 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -43,7 +43,8 @@ static uint32_t _atoi(const char* sp) { #endif #elif defined(BLE_PIN_CODE) #include - SerialBLEInterface serial_interface; + #include + SerialDualInterface serial_interface; #elif defined(SERIAL_RX) #include ArduinoSerialInterface serial_interface; @@ -72,8 +73,10 @@ static uint32_t _atoi(const char* sp) { #endif #elif defined(NRF52_PLATFORM) #ifdef BLE_PIN_CODE + // Backport upstream PR #1779: BLE companion dostupný i přes USB #include - SerialBLEInterface serial_interface; + #include + SerialDualInterface serial_interface; #else #include ArduinoSerialInterface serial_interface; @@ -157,7 +160,7 @@ void setup() { ); #ifdef BLE_PIN_CODE - serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin()); + serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin(), Serial); #else serial_interface.begin(Serial); #endif @@ -216,7 +219,7 @@ void setup() { WiFi.begin(WIFI_SSID, WIFI_PWD); serial_interface.begin(TCP_PORT); #elif defined(BLE_PIN_CODE) - serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin()); + serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin(), Serial); #elif defined(SERIAL_RX) companion_serial.setPins(SERIAL_RX, SERIAL_TX); companion_serial.begin(115200); diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 7c842019..e94796a5 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -228,7 +228,7 @@ public: display.setTextSize(1); display.drawTextCentered(display.width() / 2, 43, "< Connected >"); - } else if (the_mesh.getBLEPin() != 0) { // BT pin + } else if (the_mesh.getBLEPin() != 0 && _task->isSerialEnabled()) { // BT pin display.setColor(DisplayDriver::RED); display.setTextSize(2); sprintf(tmp, "Pin:%d", the_mesh.getBLEPin()); diff --git a/src/helpers/SerialDualInterface.h b/src/helpers/SerialDualInterface.h new file mode 100644 index 00000000..6348e09f --- /dev/null +++ b/src/helpers/SerialDualInterface.h @@ -0,0 +1,102 @@ +#pragma once + +// Upstream backport (TeTeHacko): https://github.com/meshcore-dev/MeshCore/pull/1779 +// Makes the BLE companion usable over USB while BLE is disconnected. +// Once the PR is merged upstream, this file just takes the upstream version on rebase. + +#include "ArduinoSerialInterface.h" + +template +class SerialDualInterface : public BaseSerialInterface { + BLEInterface _ble; + ArduinoSerialInterface _usb; + bool _bleWasConnected; + bool _lastFrameFromBle; + +public: + SerialDualInterface() : _bleWasConnected(false), _lastFrameFromBle(false) { } + + void begin(const char* prefix, char* name, uint32_t pin_code, Stream& serial) { + _ble.begin(prefix, name, pin_code); + _usb.begin(serial); + } + + // enable/disable only control BLE, USB is always available + void enable() override { + _ble.enable(); + } + + void disable() override { + _ble.disable(); + } + + bool isEnabled() const override { + return _ble.isEnabled() || _usb.isEnabled(); + } + + bool isConnected() const override { + return _ble.isConnected() || _usb.isConnected(); + } + + // Only frames received over the MITM-paired BLE link are authenticated; + // frames received over USB are unauthenticated (physical presence only). + bool isConnectionSecure() const override { + return _lastFrameFromBle; + } + + bool isWriteBusy() const override { + if (_ble.isConnected()) return _ble.isWriteBusy(); + return _usb.isWriteBusy(); + } + + size_t writeFrame(const uint8_t src[], size_t len) override { + if (_ble.isConnected()) return _ble.writeFrame(src, len); + return _usb.writeFrame(src, len); + } + + size_t checkRecvFrame(uint8_t dest[]) override { + // Always call BLE first, it needs polling for send queue draining + // and advertising watchdog, even when USB is the active transport + size_t n = _ble.checkRecvFrame(dest); + + bool bleNow = _ble.isConnected(); + if (bleNow != _bleWasConnected) { + if (bleNow) { + // BLE just connected, wait for any pending USB write to finish + while (_usb.isWriteBusy()) { } + _usb.disable(); + } else { + // BLE just disconnected, re-enable USB (resets state machine, + // discarding any stale bytes from the previous session) + _usb.enable(); + } + _bleWasConnected = bleNow; + } + + // Dock behaviour: while a USB host is connected (card sitting in the dock), + // stop BLE advertising so the node stays quiet on 2.4 GHz and the app talks + // over USB; resume advertising once undocked. Only advertising is gated -- + // an already-established BLE link is left untouched (never force-disconnect). + if (!bleNow) { + bool docked = _usb.isConnected(); + if (docked && _ble.isEnabled()) { + _ble.disable(); + } else if (!docked && !_ble.isEnabled()) { + _ble.enable(); + } + } + + if (n > 0) { // got a BLE frame + _lastFrameFromBle = true; + return n; + } + + // Only check USB when BLE is not connected + if (!bleNow) { + n = _usb.checkRecvFrame(dest); + if (n > 0) _lastFrameFromBle = false; + return n; + } + return 0; + } +};