Report the real USB client connection state in companion builds

ArduinoSerialInterface::isConnected() always returned true, so any
companion build with a USB interface believes a client is attached from
boot onwards. Two user visible effects:

- new message notifications never fire, because MyMesh only notifies
  the UI (display, buzzer) while no client is connected
- on builds that also expose BLE the home screen shows '< Connected >'
  instead of the BLE pairing PIN, so a freshly flashed device cannot be
  paired at all

Add an optional connection check callback and wire it up per platform:
native USB-CDC (TinyUSB on nRF52/RP2040/ESP32 with USB_MODE=0) exposes
real DTR through (bool)Serial. The ESP32 USB-Serial-JTAG peripheral has
no DTR concept - it reports 'connected' as soon as the host enumerated
the device - so fall back to frame activity there. Plain UARTs keep the
previous assume-connected behaviour.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
rootandClaude Fable 5 committed 2026-08-14 20:13:36 +02:00
1 parent d929643524
commit ab492ba187
3 files changed
+43 -5

No files matched your search

+19 -4
View File
@@ -4,23 +4,38 @@
#include <Arduino.h>
class ArduinoSerialInterface : public BaseSerialInterface {
public:
// reports whether a client currently has this stream open (see setConnectedCheck)
typedef bool (*ConnectedCheck)();
private:
bool _isEnabled;
uint8_t _state;
uint16_t _frame_len;
uint16_t rx_len;
uint32_t _last_frame_ms;
Stream* _serial;
ConnectedCheck _conn_check;
uint8_t rx_buf[MAX_FRAME_SIZE];
public:
ArduinoSerialInterface() { _isEnabled = false; _state = 0; }
ArduinoSerialInterface() { _isEnabled = false; _state = 0; _last_frame_ms = 0; _conn_check = NULL; }
void begin(Stream& serial) {
_serial = &serial;
void begin(Stream& serial) {
_serial = &serial;
#ifdef RAK_4631
pinMode(WB_IO2, OUTPUT);
#endif
#endif
}
// Optional: let the target report the real link state, e.g. USB-CDC DTR.
// Without it isConnected() assumes true, as a plain UART has no way of knowing.
void setConnectedCheck(ConnectedCheck fn) { _conn_check = fn; }
// millis() of the last completely received frame, 0 if none since boot.
// Useful as an activity-based connection check where no DTR state exists.
uint32_t getLastFrameMillis() const { return _last_frame_ms; }
// BaseSerialInterface methods
void enable() override;
void disable() override;