Protect ESP32 HWCDC console sessions

This commit is contained in:
mikecarper committed 2026-09-04 17:18:54 -07:00
1 parent 36435b450f
commit b724d19d6f
18 files changed
+196 -104

No files matched your search

+1 -30
View File
@@ -1978,37 +1978,8 @@ void halt() {
#endif
void setup() {
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT \
&& defined(ENABLE_USB_INTERFACE)
// Configure HWCDC before begin() allocates its mutex and enables the USB ISR.
// The bundled setTxBufferSize() deletes/recreates its ring without taking the
// TX mutex or masking that ISR, so even an otherwise-quiet post-begin resize
// can race the interrupt handler.
// A failed resize deletes HWCDC's existing ring. Try progressively smaller
// queues while setup still has no producers, and tell reset cleanup the exact
// capacity it must observe before reopening a host session.
static const size_t usb_tx_sizes[] = {
MESH_ESP32_USB_TX_BUFFER_SIZE, 2048, 1024, 512, 256};
size_t usb_tx_capacity = 0;
for (size_t candidate : usb_tx_sizes) {
usb_tx_capacity = Serial.setTxBufferSize(candidate);
if (usb_tx_capacity == candidate) break;
}
Serial.setTxTimeoutMs(5);
#endif
mesh::prepareUsbLoggingPort();
Serial.begin(115200);
#if defined(ESP32) && defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 \
&& defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT \
&& defined(ENABLE_USB_INTERFACE)
// If every pre-begin allocation failed, begin() may have recovered by
// creating its built-in 256-byte ring. Discover that ring without replacing
// it after the ISR is live; a zero result leaves reset cleanup quarantined.
if (usb_tx_capacity == 0) {
usb_tx_capacity = Serial.availableForWrite();
}
mesh::setUsbCompanionTxBufferCapacity(usb_tx_capacity);
#endif
#if MESH_PACKET_LOGGING
mesh::serialLogBegin();
#endif
+6 -6
View File
@@ -2402,7 +2402,7 @@ void MyMesh::printRecentRepeatersSerial() {
return;
}
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (hasPendingSerialOutput()) {
mesh::usbConsolePort().printf("Err - USB output busy\r\n");
return;
@@ -4845,7 +4845,7 @@ void MyMesh::updateFloodAdvertTimer() {
}
void MyMesh::dumpLogFile() {
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (hasPendingSerialOutput()) {
mesh::usbConsolePort().printf("Err - USB output busy\r\n");
return;
@@ -4869,7 +4869,7 @@ void MyMesh::dumpLogFile() {
#endif
}
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
bool MyMesh::hasPendingSerialOutput() const {
return serial_log_active || serial_log_eof_pending || serial_recent_next >= 0;
}
@@ -11536,7 +11536,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
// Compatibility path for manually defined legacy portable builds. Current
// release builds use FULL and do not enter this branch.
_cli.handleCommand(sender_timestamp, command, reply);
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
#endif
@@ -11942,7 +11942,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
#endif
} else{
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
#endif
@@ -11956,7 +11956,7 @@ void MyMesh::loop() {
// Check radio FIRST to ensure we don't miss incoming packets
// MQTT processing runs in a separate FreeRTOS task on Core 0, so we don't call bridge.loop() here
mesh::Mesh::loop();
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
servicePendingSerialOutput();
#endif
_cli.loop();
+2 -2
View File
@@ -288,7 +288,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
};
FILESYSTEM* _fs;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
File serial_log_dump;
size_t serial_log_remaining = 0;
size_t serial_log_pending_size = 0;
@@ -982,7 +982,7 @@ public:
}
void dumpLogFile() override;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
// Large local-only replies advance between radio service passes.
bool hasPendingSerialOutput() const;
void servicePendingSerialOutput();
+5 -4
View File
@@ -71,8 +71,9 @@ static unsigned long userBtnDownAt = 0;
#endif
void setup() {
mesh::prepareUsbLoggingPort();
Serial.begin(115200);
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::beginUsbLoggingPort();
#endif
#if MESH_PACKET_LOGGING
@@ -116,7 +117,7 @@ void setup() {
radioinit_attempts = 0;
const uint32_t retry_started = millis();
while (millis() - retry_started < 60000UL) {
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
#endif
@@ -224,7 +225,7 @@ void setup() {
static void __attribute__((noinline)) serviceCommandInterfaces() {
bool usb_ready = true;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
if (mesh::takeUsbTerminalSessionReset()) {
@@ -351,7 +352,7 @@ void loop() {
if (display_ready) ui_task.loop();
#endif
rtc_clock.tick();
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbTerminalPort();
#endif
+4 -4
View File
@@ -1577,7 +1577,7 @@ void MyMesh::updateFloodAdvertTimer() {
}
void MyMesh::dumpLogFile() {
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (hasPendingSerialOutput()) {
mesh::usbConsolePort().printf("Err - USB output busy\r\n");
return;
@@ -1601,7 +1601,7 @@ void MyMesh::dumpLogFile() {
#endif
}
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
bool MyMesh::hasPendingSerialOutput() const {
return serial_log_active || serial_log_eof_pending;
}
@@ -2415,7 +2415,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
// handled by the role-independent mesh clock synchronizer
} else {
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
#endif
@@ -2433,7 +2433,7 @@ void MyMesh::loop() {
// Check radio FIRST to ensure we don't miss incoming packets
// MQTT processing can take time, so we prioritize radio reception
mesh::Mesh::loop();
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
servicePendingSerialOutput();
#endif
_cli.loop();
+2 -2
View File
@@ -158,7 +158,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
#endif
{
FILESYSTEM* _fs;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
File serial_log_dump;
size_t serial_log_remaining = 0;
size_t serial_log_pending_size = 0;
@@ -463,7 +463,7 @@ public:
}
void dumpLogFile() override;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
// Large local-only replies advance between radio service passes.
bool hasPendingSerialOutput() const;
void servicePendingSerialOutput();
+4 -3
View File
@@ -43,8 +43,9 @@ static char ethernet_command[MAX_POST_TEXT_LEN+1];
unsigned long POWERSAVING_FIRSTSLEEP_SECS = 120; // The first sleep (if enabled) from boot
void setup() {
mesh::prepareUsbLoggingPort();
Serial.begin(115200);
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::beginUsbLoggingPort();
#endif
#if MESH_PACKET_LOGGING
@@ -157,7 +158,7 @@ void loop() {
board.feedWatchdog(the_mesh.getNodePrefs()->system_watchdog_enabled != 0);
#endif
bool usb_ready = true;
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
if (mesh::takeUsbTerminalSessionReset()) {
@@ -219,7 +220,7 @@ void loop() {
if (display_ready) ui_task.loop();
#endif
rtc_clock.tick();
#if MESH_ESP32_TINYUSB_NONBLOCKING
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbTerminalPort();
#endif
#ifdef TBEAM_1W
+6 -4
View File
@@ -4,6 +4,7 @@
#include <helpers/ClientPathPersistence.h>
#include <helpers/LazyPersistence.h>
#include <helpers/radiolib/RXPowerSaving.h>
#include <helpers/UsbLogging.h>
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
uint8_t shift = failure_count < 5 ? failure_count : 5;
@@ -623,14 +624,15 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
}
}
} else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) {
Serial.println("ACL:");
Stream& console = mesh::usbConsolePort();
console.println("ACL:");
for (int i = 0; i < acl.getNumClients(); i++) {
auto c = acl.getClientByIdx(i);
if (c->permissions == 0) continue; // skip deleted entries
Serial.printf("%02X ", c->permissions);
mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE);
Serial.printf("\n");
console.printf("%02X ", c->permissions);
mesh::Utils::printHex(console, c->id.pub_key, PUB_KEY_SIZE);
console.print('\n');
}
reply[0] = 0;
} else if (memcmp(command, "io ", 2) == 0) { // io {value}: write, io: read
+26 -6
View File
@@ -1,5 +1,6 @@
#include "SensorMesh.h"
#include <helpers/IdentityGeneration.h>
#include <helpers/UsbLogging.h>
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
@@ -59,7 +60,11 @@ static char command[160];
static const unsigned long POWERSAVING_FIRST_SLEEP_SECS = 120;
void setup() {
mesh::prepareUsbLoggingPort();
Serial.begin(115200);
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::beginUsbLoggingPort();
#endif
delay(1000);
board.begin();
@@ -124,8 +129,10 @@ void setup() {
return;
}
Serial.print("Sensor ID: ");
mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
Stream& console = mesh::usbConsolePort();
console.print("Sensor ID: ");
mesh::Utils::printHex(console, the_mesh.self_id.pub_key, PUB_KEY_SIZE);
console.println();
command[0] = 0;
@@ -149,14 +156,23 @@ void loop() {
#if defined(NRF52_PLATFORM)
board.feedWatchdog(the_mesh.getNodePrefs()->system_watchdog_enabled != 0);
#endif
bool usb_ready = true;
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
if (mesh::takeUsbTerminalSessionReset()) command[0] = 0;
usb_ready = mesh::tryCompleteUsbTerminalSessionReset()
&& mesh::canAcceptUsbConsoleCommand();
#endif
Stream& console = mesh::usbConsolePort();
int len = strlen(command);
while (Serial.available() && len < sizeof(command)-1) {
char c = Serial.read();
while (usb_ready && console.available() && len < sizeof(command)-1) {
char c = console.read();
if (c != '\n') {
command[len++] = c;
command[len] = 0;
}
Serial.print(c);
console.print(c);
}
if (len == sizeof(command)-1) { // command buffer full
command[sizeof(command)-1] = '\r';
@@ -168,7 +184,8 @@ void loop() {
reply[0] = 0;
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
if (reply[0]) {
Serial.print(" -> "); Serial.println(reply);
console.print(" -> ");
console.println(reply);
}
command[0] = 0; // reset command buffer
@@ -180,6 +197,9 @@ void loop() {
ui_task.loop();
#endif
rtc_clock.tick();
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbTerminalPort();
#endif
#ifdef HAS_EXTERNAL_WATCHDOG
external_watchdog.loop();
#endif