From 7390e2908611822372c15de7f0a2c7b302b0cca6 Mon Sep 17 00:00:00 2001 From: mikecarper Date: Wed, 28 Oct 2026 02:41:30 -0700 Subject: [PATCH] Keep USB mode reporting out of large command dispatchers --- src/helpers/UsbMode.h | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/helpers/UsbMode.h b/src/helpers/UsbMode.h index 2a5c2fe31..a81db1eb5 100644 --- a/src/helpers/UsbMode.h +++ b/src/helpers/UsbMode.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -12,21 +11,25 @@ namespace mesh { // Include after Arduino.h. The caller supplies decltype(Serial), after any // board-specific alias is visible, so a custom primary UART cannot inherit a // native-USB claim merely because the unused USB peripheral has those flags. -template +template inline const char* usbModeName() { #if defined(ESP32) || defined(ESP32_PLATFORM) - if (std::is_same::value) return "uart"; + if (std::is_same::value) + return WithPrefix ? "usb.mode uart" : "uart"; #if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT #if defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 1 - if (std::is_same::value) return "hardware-cdc-jtag"; + if (std::is_same::value) + return WithPrefix ? "usb.mode hardware-cdc-jtag" : "hardware-cdc-jtag"; #elif defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE == 0 - if (std::is_same::value) return "tinyusb"; + if (std::is_same::value) + return WithPrefix ? "usb.mode tinyusb" : "tinyusb"; #endif #endif #elif defined(NRF52_PLATFORM) && defined(USE_TINYUSB) - if (std::is_same::value) return "tinyusb"; + if (std::is_same::value) + return WithPrefix ? "usb.mode tinyusb" : "tinyusb"; #endif - return "unknown"; + return WithPrefix ? "usb.mode unknown" : "unknown"; } inline bool isUsbModeCommand(const char* command) { @@ -36,10 +39,17 @@ inline bool isUsbModeCommand(const char* command) { // Read-only, bounded and independent of diagnostic/logging build flags. // No setter: changing a USB backend requires installing another application. template -inline bool handleUsbModeCommand(const char* command, char* reply, +#if defined(__GNUC__) +__attribute__((noinline)) +#endif +bool handleUsbModeCommand(const char* command, char* reply, size_t capacity, const SerialType&) { if (!isUsbModeCommand(command) || reply == nullptr || capacity == 0) return false; - snprintf(reply, capacity, "usb.mode %s", usbModeName()); + const char* response = usbModeName(); + size_t length = strlen(response); + if (length >= capacity) length = capacity - 1; + memcpy(reply, response, length); + reply[length] = 0; return true; }