Harden USB logging and persist watchdog events in management reports
This commit is contained in:
1 parent
bd55369aa9
commit
3add4695cc
98 files changed
+7207
-372
No files matched your search
@@ -8,6 +8,7 @@ using esp_event_handler_t = void (*)(void*, esp_event_base_t, int32_t, void*);
|
||||
constexpr int ARDUINO_USB_CDC_ANY_EVENT = -1;
|
||||
constexpr int ARDUINO_USB_CDC_DISCONNECTED_EVENT = 1;
|
||||
constexpr int ARDUINO_USB_CDC_LINE_STATE_EVENT = 2;
|
||||
constexpr int ARDUINO_USB_CDC_TX_EVENT = 3;
|
||||
struct arduino_usb_cdc_event_data_t {
|
||||
struct { bool dtr; bool rts; } line_state{};
|
||||
};
|
||||
@@ -35,6 +36,7 @@ class MockSerial : public Stream {
|
||||
assert(false && "mode0 must never call USBCDC::flush");
|
||||
}
|
||||
void setDebugOutput(bool enabled) { debug_enabled = enabled; }
|
||||
void setTxTimeoutMs(unsigned timeout) { tx_timeout_ms = timeout; }
|
||||
void onEvent(int event, esp_event_handler_t handler) {
|
||||
assert(event == ARDUINO_USB_CDC_ANY_EVENT);
|
||||
callback = handler;
|
||||
@@ -44,6 +46,7 @@ class MockSerial : public Stream {
|
||||
int rx_count = 0;
|
||||
int registrations = 0;
|
||||
bool debug_enabled = true;
|
||||
unsigned tx_timeout_ms = 99;
|
||||
esp_event_handler_t callback = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,3 +7,6 @@ uint32_t tud_cdc_n_write_available(uint8_t instance);
|
||||
uint32_t tud_cdc_n_write(uint8_t instance, const void* data, uint32_t size);
|
||||
uint32_t tud_cdc_n_write_flush(uint8_t instance);
|
||||
bool tud_cdc_n_write_clear(uint8_t instance);
|
||||
bool tud_mounted();
|
||||
bool tud_connect();
|
||||
bool tud_disconnect();
|
||||
@@ -3,10 +3,15 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "helpers/UsbLogging.h"
|
||||
#include "helpers/UsbLoggingClientActivity.h"
|
||||
#include "MeshCore.h"
|
||||
#include "helpers/SerialPacketLog.h"
|
||||
|
||||
MockSerial Serial;
|
||||
static bool connected = true;
|
||||
static bool mounted = true;
|
||||
static unsigned disconnect_calls = 0;
|
||||
static unsigned connect_calls = 0;
|
||||
static bool auto_drain = false;
|
||||
static std::string fifo;
|
||||
static std::string host;
|
||||
@@ -15,6 +20,10 @@ static unsigned flush_calls = 0;
|
||||
static unsigned clear_calls = 0;
|
||||
static void (*during_write)() = nullptr;
|
||||
|
||||
bool tud_mounted() { return mounted; }
|
||||
bool tud_disconnect() { ++disconnect_calls; mounted = false; connected = false; return true; }
|
||||
bool tud_connect() { ++connect_calls; return true; }
|
||||
|
||||
bool tud_cdc_n_connected(uint8_t instance) {
|
||||
assert(!mock_isr);
|
||||
assert(instance == 0);
|
||||
@@ -82,6 +91,50 @@ static void fresh_session() {
|
||||
auto_drain = false;
|
||||
}
|
||||
|
||||
static void check_logging_watchdog_transport_observations_and_recovery() {
|
||||
fresh_session();
|
||||
mesh::setUsbLoggingEnabled(true);
|
||||
const auto initial = mesh::observeUsbLoggingTransport();
|
||||
assert(initial.supported && initial.host_connected && initial.reader_connected);
|
||||
assert(!initial.pending);
|
||||
assert(!mesh::isUsbLoggingClientActive()); // An open CDC is not an app poll.
|
||||
mesh::noteUsbLoggingStatsCommand("stats-core");
|
||||
assert(mesh::isUsbLoggingClientActive());
|
||||
fresh_session(); // Even a close/reopen between service calls revokes it.
|
||||
assert(!mesh::isUsbLoggingClientActive());
|
||||
mesh::noteUsbLoggingStatsCommand("stats-radio");
|
||||
mesh::setUsbLoggingEnabled(false);
|
||||
assert(!mesh::isUsbLoggingClientActive());
|
||||
mesh::setUsbLoggingEnabled(true);
|
||||
mesh::probeUsbLoggingTransport();
|
||||
const auto queued = mesh::observeUsbLoggingTransport();
|
||||
assert(queued.pending && queued.tx_progress == initial.tx_progress);
|
||||
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Attempted);
|
||||
// Purge and probe merely change queues; neither is host acknowledgement.
|
||||
assert(mesh::observeUsbLoggingTransport().tx_progress == initial.tx_progress);
|
||||
host += fifo; fifo.clear();
|
||||
Serial.callback(nullptr, nullptr, ARDUINO_USB_CDC_TX_EVENT, nullptr);
|
||||
const auto completed = mesh::observeUsbLoggingTransport();
|
||||
assert(completed.tx_progress == initial.tx_progress + 1);
|
||||
assert(!completed.pending);
|
||||
const auto detached_at = millis();
|
||||
assert(mesh::recoverUsbLoggingTransport(2) == mesh::UsbLoggingRecoveryResult::Attempted);
|
||||
assert(disconnect_calls == 1 && connect_calls == 0);
|
||||
assert(millis() == detached_at); // No blocking wait or descriptor rebuild.
|
||||
g_mock_millis += 49;
|
||||
mesh::serviceUsbLoggingPort();
|
||||
assert(connect_calls == 0);
|
||||
++g_mock_millis;
|
||||
mesh::serviceUsbLoggingPort();
|
||||
assert(connect_calls == 1 && !mesh::observeUsbLoggingTransport().host_connected);
|
||||
mock_isr = true;
|
||||
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Deferred);
|
||||
mesh::probeUsbLoggingTransport();
|
||||
mock_isr = false;
|
||||
mounted = connected = true;
|
||||
fresh_session();
|
||||
}
|
||||
|
||||
static void check_native_short_writes_and_mota() {
|
||||
const unsigned before = write_calls;
|
||||
assert(put(mesh::usbCompanionPort(), std::string(200, 'B')) == 64);
|
||||
@@ -150,6 +203,7 @@ static void check_disconnect_cleanup_keeps_new_host_input() {
|
||||
}
|
||||
|
||||
static void check_debug_formatter_and_reentrancy() {
|
||||
mesh::setUsbDebugEnabled(true);
|
||||
auto_drain = true;
|
||||
const std::string long_text(1000, 'D');
|
||||
assert(mesh::nrf52DebugPrintf("%s\n", long_text.c_str()) == 255);
|
||||
@@ -165,6 +219,39 @@ static void check_debug_formatter_and_reentrancy() {
|
||||
drain_all();
|
||||
assert(host == "outer\n");
|
||||
fresh_session();
|
||||
mesh::setUsbDebugEnabled(false);
|
||||
}
|
||||
|
||||
static void check_independent_debug_gate() {
|
||||
assert(!mesh::isUsbDebugEnabled());
|
||||
assert(mesh::isUsbLoggingEnabled());
|
||||
const auto before = write_calls;
|
||||
MESH_DEBUG_PRINTLN("default-off debug");
|
||||
assert(mesh::nrf52DebugPrintf("also quiet\n") == 0);
|
||||
assert(write_calls == before && !mesh::hasPendingUsbTerminalOutput());
|
||||
mesh::SerialLogLine<> raw;
|
||||
raw.printf("12:34:56 - 3/10/2026 U RAW: 0102");
|
||||
assert(raw.flush(mesh::usbLoggingPort()));
|
||||
drain_all();
|
||||
assert(host == "12:34:56 - 3/10/2026 U RAW: 0102\r\n");
|
||||
fresh_session();
|
||||
|
||||
mesh::setUsbDebugEnabled(true);
|
||||
assert(mesh::isUsbDebugEnabled() && mesh::isUsbDebugLoggingEnabled());
|
||||
MESH_DEBUG_PRINTLN("explicit debug");
|
||||
drain_all();
|
||||
assert(host == "DEBUG: explicit debug\n");
|
||||
fresh_session();
|
||||
mesh::setUsbLoggingEnabled(false);
|
||||
assert(mesh::isUsbDebugEnabled() && !mesh::isUsbDebugLoggingEnabled());
|
||||
const auto gated = write_calls;
|
||||
MESH_DEBUG_PRINTLN("master is off");
|
||||
assert(write_calls == gated && !mesh::hasPendingUsbTerminalOutput());
|
||||
mesh::setUsbLoggingEnabled(true);
|
||||
assert(mesh::isUsbDebugLoggingEnabled());
|
||||
mesh::setUsbDebugEnabled(false);
|
||||
assert(mesh::isUsbLoggingEnabled() && !mesh::isUsbDebugLoggingEnabled());
|
||||
assert(!Serial.debug_enabled); // native raw framework output stays forbidden
|
||||
}
|
||||
|
||||
static void check_cached_logging_gate_and_isr() {
|
||||
@@ -215,6 +302,8 @@ int main() {
|
||||
assert(!Serial.debug_enabled);
|
||||
mesh::serviceUsbLoggingPort();
|
||||
assert(&mesh::usbConsolePort() == &mesh::usbTerminalPort());
|
||||
check_logging_watchdog_transport_observations_and_recovery();
|
||||
check_independent_debug_gate();
|
||||
check_native_short_writes_and_mota();
|
||||
check_ordered_text_and_functional_reserve();
|
||||
check_stalled_host_and_visible_overflow();
|
||||
@@ -227,6 +316,9 @@ int main() {
|
||||
assert(&mesh::usbConsolePort() == &Serial);
|
||||
assert(mesh::canAcceptUsbConsoleCommand());
|
||||
assert(mesh::usbTerminalDroppedBytes() == 0);
|
||||
assert(!mesh::observeUsbLoggingTransport().supported);
|
||||
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Unsupported);
|
||||
mesh::probeUsbLoggingTransport();
|
||||
#endif
|
||||
std::cout << "ESP32 TinyUSB transport checks passed\n";
|
||||
}
|
||||
Reference in new issue
Block a user