Add runtime RS232 bridge diagnostics and wired link probes
This commit is contained in:
1 parent
e7b863494c
commit
281f1a5199
22 files changed
+1468
-24
No files matched your search
+293
@@ -0,0 +1,293 @@
|
||||
// Share hardware/pool/old-parser fixtures with the independent heartbeat suite.
|
||||
// All diagnostics below exercise the compiled production RS232Bridge.cpp.
|
||||
#define main heartbeat_fixture_main
|
||||
#include "rs232_heartbeat/main.cpp"
|
||||
#undef main
|
||||
|
||||
namespace diag = mesh::bridge::rs232diag;
|
||||
|
||||
std::vector<uint8_t> probe(uint32_t nonce) {
|
||||
std::vector<uint8_t> bytes(diag::FrameSize);
|
||||
bytes[0] = 0xC0; bytes[1] = 0x3E;
|
||||
assert(diag::encodeProbe(bytes.data() + 4, bytes.size() - 4, nonce));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void probe_codec_test() {
|
||||
assert(diag::FrameSize == 52 && sizeof(diag::Stats) <= 128);
|
||||
for (uint32_t nonce : {0U, 1U, 0xA1B2C3D4U, UINT32_MAX}) {
|
||||
const auto frame = probe(nonce);
|
||||
assert(frame[2] == 0 && frame[3] == 0);
|
||||
assert(memcmp(frame.data() + 4, "DG1T", 4) == 0);
|
||||
for (size_t i = 4; i < frame.size(); ++i) assert(frame[i] < 0x80);
|
||||
uint32_t parsed = 0x12345678;
|
||||
assert(diag::decodeProbe(frame.data() + 4, 48, parsed) && parsed == nonce);
|
||||
hb::Control heartbeat;
|
||||
assert(!hb::decode(frame.data() + 4, heartbeat));
|
||||
for (size_t i = 4; i < frame.size(); ++i) for (unsigned bit = 0; bit < 8; ++bit) {
|
||||
auto bad = frame; bad[i] ^= 1U << bit;
|
||||
parsed = 0x12345678;
|
||||
assert(!diag::decodeProbe(bad.data() + 4, 48, parsed));
|
||||
assert(parsed == 0x12345678);
|
||||
}
|
||||
for (size_t size = 0; size < 80; ++size) if (size != 48) {
|
||||
parsed = 0x12345678;
|
||||
assert(!diag::decodeProbe(frame.data() + 4, size, parsed));
|
||||
assert(parsed == 0x12345678);
|
||||
}
|
||||
}
|
||||
std::array<uint8_t, 60> untouched; untouched.fill(0x55);
|
||||
assert(!diag::encodeProbe(untouched.data(), 47, 42));
|
||||
for (auto value : untouched) assert(value == 0x55);
|
||||
uint32_t nonce = 42;
|
||||
assert(!diag::encodeProbe(nullptr, 48, 1));
|
||||
assert(!diag::decodeProbe(nullptr, 48, nonce) && nonce == 42);
|
||||
auto reserved = probe(42); reserved[16] = '1'; rechecksum(reserved);
|
||||
assert(!diag::decodeProbe(reserved.data() + 4, 48, nonce));
|
||||
}
|
||||
|
||||
void probe_isolation_test() {
|
||||
now_ms = 100; Peer sender(A_SESSION), receiver(B_SESSION);
|
||||
sender.driver.setHeartbeatEnabled(false);
|
||||
receiver.driver.setHeartbeatEnabled(false);
|
||||
sender.uart.delivered = [&](const uint8_t* data, size_t size) { receiver.uart.feed(data, size); };
|
||||
receiver.manager.starved = true;
|
||||
const auto last_peer = receiver.driver._power.peerAge(now_ms);
|
||||
assert(sender.driver.sendTest(0xA1B2C3D4));
|
||||
assert(sender.uart.tx.empty()); // Queueing is not serial or peer acceptance.
|
||||
sender.driver.loop(); receiver.driver.loop();
|
||||
assert(sender.uart.tx == probe(0xA1B2C3D4));
|
||||
assert(sender.driver._stats.probe.sent == 1);
|
||||
assert(receiver.driver._stats.probe.received == 1);
|
||||
assert(receiver.driver._stats.probe.last_rx_nonce == 0xA1B2C3D4);
|
||||
assert(sender.manager.allocations == 0 && receiver.manager.allocations == 0);
|
||||
assert(sender.manager.received.empty() && receiver.manager.received.empty());
|
||||
assert(sender.driver.seenCount() == 0 && receiver.driver.seenCount() == 0);
|
||||
assert(receiver.uart.tx.empty() && !receiver.driver._have_ack && !receiver.driver._legacy_rx);
|
||||
assert(receiver.driver._pong_target == 0 && !receiver.driver._power.hasPeer());
|
||||
assert(receiver.driver._power.peerAge(now_ms) == last_peer);
|
||||
receiver.driver.setHeartbeatEnabled(true);
|
||||
assert(receiver.driver.servicePowerPolicy(true, 1, now_ms + 1, true) == Action::Suspend);
|
||||
receiver.driver.setHeartbeatEnabled(false);
|
||||
for (unsigned i = 0; i < 5; ++i) { sender.driver.loop(); receiver.driver.loop(); }
|
||||
assert(sender.uart.tx.size() == diag::FrameSize && receiver.uart.tx.empty());
|
||||
auto bad = probe(42); bad[49] ^= 1;
|
||||
receiver.uart.feed(bad); receiver.driver.loop();
|
||||
assert(receiver.driver._stats.probe.bad == 1 && receiver.manager.allocations == 0);
|
||||
assert(receiver.driver._stats.probe.last_rx_nonce == 0xA1B2C3D4);
|
||||
}
|
||||
|
||||
void probe_legacy_test() {
|
||||
for (unsigned parser = 0; parser < 2; ++parser) for (size_t prefix = 0; prefix <= diag::FrameSize; ++prefix) {
|
||||
NodePrefs prefs; HardwareSerial uart; uart.begin(115200);
|
||||
mesh::PacketManager manager;
|
||||
StockBridge old(&prefs, uart, &manager);
|
||||
UpstreamStockBridge upstream(&prefs, uart, &manager);
|
||||
BridgeBase* bridge = parser ? static_cast<BridgeBase*>(&upstream) : &old;
|
||||
const auto frame = probe(0xA1B2C3D4);
|
||||
uart.feed(frame.data(), prefix); bridge->loop();
|
||||
assert(manager.allocations == 0 && manager.received.empty());
|
||||
const auto data = data_frame({5, 6, 7, 8});
|
||||
uart.feed(data); bridge->loop();
|
||||
// A legacy parser cannot resynchronize a truncated header (1..3 bytes),
|
||||
// just as for any interrupted data frame. A whole probe or body prefix is
|
||||
// ignored and does not hide the following ordinary frame.
|
||||
if (prefix == 0 || prefix >= 6) {
|
||||
assert(manager.received == std::vector<std::vector<uint8_t>>({{5, 6, 7, 8}}));
|
||||
assert(manager.allocations == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void probe_backpressure_test() {
|
||||
// All possible partial writes retain their exact prefix. A timeout cancels
|
||||
// only an unsent frame; a partial header keeps its unsent suffix owned until
|
||||
// UART recovery. This protects both legacy parsers from a later mesh header.
|
||||
for (size_t prefix = 0; prefix < diag::FrameSize; ++prefix) {
|
||||
now_ms = 100; Peer sender(A_SESSION), receiver(B_SESSION);
|
||||
sender.driver.setHeartbeatEnabled(false); receiver.driver.setHeartbeatEnabled(false);
|
||||
sender.uart.write_limit = prefix;
|
||||
assert(sender.driver.sendTest(0x12345678));
|
||||
sender.driver.drainTransmit(now_ms);
|
||||
assert(sender.uart.tx.size() == prefix);
|
||||
const auto accepted = sender.uart.tx;
|
||||
sender.uart.write_limit = 0;
|
||||
sender.driver.loop();
|
||||
assert(sender.uart.tx == accepted && sender.driver._stats.probe.sent == 0);
|
||||
now_ms += sender.driver.frameAssemblyMs();
|
||||
sender.driver.loop();
|
||||
assert(sender.driver._stats.probe.failed == 1);
|
||||
assert(sender.driver._tx_size == (prefix ? diag::FrameSize : 0));
|
||||
assert(sender.driver._stats.error == diag::Error::TestTimeout);
|
||||
if (prefix) assert(sender.driver._tx_wire_pending); // Prefix wire delay is not erased.
|
||||
for (unsigned n = 0; n < 3; ++n) sender.driver.loop();
|
||||
assert(sender.uart.tx == accepted && sender.driver._stats.probe.failed == 1);
|
||||
sender.uart.write_limit = SIZE_MAX;
|
||||
now_ms += 100;
|
||||
if (prefix) {
|
||||
assert(!sender.driver.sendTest(0xFEDCBA98));
|
||||
sender.driver.loop();
|
||||
assert(sender.uart.tx == probe(0x12345678));
|
||||
assert(sender.driver._stats.probe.sent == 1);
|
||||
now_ms += 100;
|
||||
}
|
||||
assert(sender.driver.sendTest(0xFEDCBA98)); sender.driver.loop();
|
||||
receiver.uart.feed(sender.uart.tx); receiver.driver.loop();
|
||||
assert(receiver.driver._stats.probe.received == (prefix ? 2U : 1U));
|
||||
assert(receiver.driver._stats.probe.last_rx_nonce == 0xFEDCBA98);
|
||||
for (unsigned parser = 0; parser < 2; ++parser) {
|
||||
NodePrefs prefs; HardwareSerial uart; uart.begin(115200);
|
||||
mesh::PacketManager manager;
|
||||
StockBridge old(&prefs, uart, &manager);
|
||||
UpstreamStockBridge upstream(&prefs, uart, &manager);
|
||||
BridgeBase* bridge = parser ? static_cast<BridgeBase*>(&upstream) : &old;
|
||||
uart.feed(sender.uart.tx); bridge->loop();
|
||||
assert(manager.allocations == 0);
|
||||
uart.feed(data_frame({5, 6, 7, 8})); bridge->loop();
|
||||
assert(manager.received == std::vector<std::vector<uint8_t>>({{5, 6, 7, 8}}));
|
||||
}
|
||||
assert(receiver.manager.allocations == 0 && !receiver.driver._power.hasPeer());
|
||||
}
|
||||
now_ms = 100; Peer completed(A_SESSION); completed.driver.setHeartbeatEnabled(false);
|
||||
completed.uart.write_limit = 1;
|
||||
assert(completed.driver.sendTest(0));
|
||||
for (unsigned i = 0; i < 26; ++i) completed.driver.loop();
|
||||
assert(completed.uart.tx == probe(0) && completed.driver._stats.probe.sent == 1);
|
||||
assert(completed.driver._stats.probe.failed == 0);
|
||||
}
|
||||
|
||||
void probe_state_test() {
|
||||
now_ms = 100; Peer p(A_SESSION);
|
||||
const auto before = p.driver._power.peerAge(now_ms);
|
||||
assert(!p.driver.sendTest(1)); // Initial heartbeat must go first.
|
||||
assert(p.driver._stats.error == diag::Error::TestBusy && p.uart.tx.empty());
|
||||
p.driver.setHeartbeatEnabled(false);
|
||||
assert(p.driver.sendTest(2));
|
||||
assert(!p.driver.sendTest(3));
|
||||
assert(p.driver._stats.probe.last_tx_nonce == 2 && p.driver._power.peerAge(now_ms) == before);
|
||||
p.driver.end();
|
||||
assert(p.driver._stats.probe.failed == 3); // 2 rejected + 1 canceled.
|
||||
assert(!p.driver.sendTest(4));
|
||||
assert(p.driver._stats.error == diag::Error::TestStopped && !p.uart.installed);
|
||||
p.driver.begin();
|
||||
assert(p.driver._stats.probe.queued == 1 && p.driver._stats.probe.failed == 4);
|
||||
p.driver.setHeartbeatEnabled(true);
|
||||
p.enter_standby(now_ms + 1);
|
||||
assert(!p.driver.sendTest(5) && !p.uart.installed);
|
||||
assert(p.driver._power.suspended());
|
||||
}
|
||||
|
||||
void diagnostic_counters_test() {
|
||||
now_ms = 100; Peer p(A_SESSION); p.driver.setHeartbeatEnabled(false);
|
||||
p.uart.feed({0x01, 0xC0, 0x11, 0xC0, 0x3E, 0xFF, 0xFF}); p.driver.loop();
|
||||
assert(p.driver._stats.rx.bytes == 7 && p.driver._stats.rx.noise == 2);
|
||||
assert(p.driver._stats.rx.length_bad == 1);
|
||||
auto bad = data_frame({4, 5, 6}); bad.back() ^= 1;
|
||||
p.uart.feed(bad); p.driver.loop();
|
||||
assert(p.driver._stats.rx.checksum_bad == 1 && p.driver._stats.rx.frames == 0);
|
||||
p.uart.feed({0xC0, 0x3E, 0, 10, 1}); p.driver.loop();
|
||||
now_ms += p.driver.frameAssemblyMs(); p.driver.loop();
|
||||
assert(p.driver._stats.rx.timeout == 1);
|
||||
p.manager.starved = true;
|
||||
p.uart.feed(data_frame({8, 9, 10})); p.driver.loop();
|
||||
for (unsigned n = 0; n < 10; ++n) p.driver.loop();
|
||||
assert(p.driver._stats.rx.frames == 1 && p.driver._stats.rx.pool_wait == 1);
|
||||
p.manager.starved = false; p.driver.loop();
|
||||
assert(p.driver._stats.rx.delivered == 1 && !p.driver._rx_packet_ready);
|
||||
mesh::Packet packet; packet.payload = {12, 13, 14};
|
||||
p.driver.sendPacket(&packet); p.driver.sendPacket(&packet);
|
||||
assert(p.driver._stats.tx.requested == 2 && p.driver._stats.tx.queued == 1);
|
||||
assert(p.driver._stats.tx.sent == 1 && p.driver._stats.tx.duplicate == 1);
|
||||
p.uart.feed(data_frame(packet.payload)); p.driver.loop();
|
||||
assert(p.driver._stats.rx.duplicate == 1 && p.driver._stats.rx.delivered == 1);
|
||||
packet.allowed = false; p.driver.sendPacket(&packet);
|
||||
assert(p.driver._stats.tx.filtered == 1);
|
||||
const auto old_rx = p.driver._stats.rx.bytes;
|
||||
p.driver.end(); p.driver.begin();
|
||||
assert(p.driver._stats.rx.bytes == old_rx && p.driver._stats.tx.sent == 1);
|
||||
p.driver._outstanding_challenge = 1234; p.driver._last_ack = 12;
|
||||
p.driver._pong_target = B_SESSION;
|
||||
p.uart.feed({0xC0}); p.driver.loop();
|
||||
const auto parser_pos = p.driver._rx_buffer_pos;
|
||||
p.driver.resetStats();
|
||||
assert(p.driver._stats.rx.bytes == 0 && p.driver._stats.tx.sent == 0);
|
||||
assert(p.driver._outstanding_challenge == 1234 && p.driver._last_ack == 12);
|
||||
assert(p.driver._rx_buffer_pos == parser_pos);
|
||||
}
|
||||
|
||||
void diagnostic_bounds_test() {
|
||||
now_ms = 100; Peer p(A_SESSION); p.driver.setHeartbeatEnabled(false);
|
||||
for (uint32_t start : {0U, UINT32_MAX - 1, UINT32_MAX}) {
|
||||
uint32_t counter = start;
|
||||
diag::increment(counter);
|
||||
assert(counter == (start == UINT32_MAX ? UINT32_MAX : start + 1));
|
||||
diag::add(counter, SIZE_MAX); assert(counter == UINT32_MAX);
|
||||
}
|
||||
p.driver._stats.rx = {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX,
|
||||
UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX};
|
||||
p.driver._stats.tx = {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX,
|
||||
UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX};
|
||||
p.driver._stats.probe = {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX,
|
||||
UINT32_MAX, UINT32_MAX, UINT32_MAX};
|
||||
for (const char* page : {"", "rx", "tx", "drops", "test", "invalid"}) {
|
||||
std::array<char, 200> output; output.fill('!');
|
||||
p.driver.formatStats(page, output.data(), 157);
|
||||
assert(strlen(output.data()) < 157 && output[157] == '!');
|
||||
if (strcmp(page, "drops") == 0) assert(strstr(output.data(), "big=4294967295"));
|
||||
p.driver.formatStats(page, output.data(), 1); assert(output[0] == 0);
|
||||
p.driver.formatStats(page, nullptr, 0);
|
||||
}
|
||||
p.prefs.bridge_baud = UINT32_MAX; p.driver._rx_pin = INT16_MIN; p.driver._tx_pin = INT16_MAX;
|
||||
p.driver._stats.error = diag::Error::TestStopped;
|
||||
char status[158]; memset(status, '!', sizeof(status));
|
||||
p.driver.formatStatus(status, 157);
|
||||
assert(strlen(status) < 157 && status[157] == '!');
|
||||
assert(strstr(status, "last=test.stopped"));
|
||||
p.driver.formatStatus(status, 1); assert(status[0] == 0);
|
||||
p.driver.formatStatus(nullptr, 0);
|
||||
}
|
||||
|
||||
void diagnostic_debug_test() {
|
||||
now_ms = 1000; Peer p(A_SESSION); p.driver.setHeartbeatEnabled(false);
|
||||
mesh::logging_enabled = mesh::debug_enabled = false;
|
||||
p.driver.loop(); assert(mesh::debug_stream.writes == 0);
|
||||
mesh::debug_enabled = true; p.driver.loop(); assert(mesh::debug_stream.writes == 0);
|
||||
mesh::debug_enabled = false; mesh::logging_enabled = true;
|
||||
p.driver.loop(); assert(mesh::debug_stream.writes == 0);
|
||||
mesh::debug_enabled = true;
|
||||
p.driver.loop(); assert(mesh::debug_stream.writes == 1);
|
||||
const std::string text(mesh::debug_stream.bytes.begin(), mesh::debug_stream.bytes.end());
|
||||
assert(text.find("RS232: ready baud=115200 pins=5/6") != std::string::npos);
|
||||
assert(p.uart.tx.empty()); // Debug output never enters the bridge wire.
|
||||
now_ms += 10000; p.driver.loop(); assert(mesh::debug_stream.writes == 1);
|
||||
for (unsigned i = 0; i < 20; ++i) {
|
||||
p.uart.feed({1}); p.driver.loop();
|
||||
}
|
||||
assert(mesh::debug_stream.writes == 2); // At most one dirty summary/second.
|
||||
mesh::debug_stream.capacity = 0; now_ms += 1000;
|
||||
p.uart.feed({1}); p.driver.loop();
|
||||
const auto total = mesh::debug_stream.bytes.size();
|
||||
now_ms += 1000; p.uart.feed({1}); p.driver.loop();
|
||||
assert(mesh::debug_stream.bytes.size() == total);
|
||||
mesh::debug_stream.capacity = 4096;
|
||||
mesh::debug_enabled = false; now_ms += 1000; p.uart.feed({1}); p.driver.loop();
|
||||
assert(mesh::debug_stream.bytes.size() == total);
|
||||
mesh::debug_enabled = true; p.driver.loop();
|
||||
assert(mesh::debug_stream.bytes.size() > total && p.uart.tx.empty());
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
assert(argc == 2);
|
||||
const std::string name = argv[1];
|
||||
if (name == "codec") probe_codec_test();
|
||||
else if (name == "isolation") probe_isolation_test();
|
||||
else if (name == "legacy") probe_legacy_test();
|
||||
else if (name == "backpressure") probe_backpressure_test();
|
||||
else if (name == "state") probe_state_test();
|
||||
else if (name == "counters") diagnostic_counters_test();
|
||||
else if (name == "bounds") diagnostic_bounds_test();
|
||||
else if (name == "debug") diagnostic_debug_test();
|
||||
else assert(false);
|
||||
printf("%s passed\n", name.c_str());
|
||||
return 0;
|
||||
}
|
||||
+42
-1
@@ -2,13 +2,47 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#if defined(ARDUINO)
|
||||
#include <Stream.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
#define MAX_TRANS_UNIT 255
|
||||
#define PAYLOAD_TYPE_OTA 0x0c
|
||||
#define BRIDGE_DEBUG_PRINTLN(...) ((void)0)
|
||||
struct NodePrefs { uint32_t bridge_baud = 115200; };
|
||||
struct NodePrefs { uint32_t bridge_baud = 115200; uint8_t bridge_pkt_src = 1; };
|
||||
|
||||
namespace mesh {
|
||||
#if defined(ARDUINO)
|
||||
inline bool logging_enabled = false, debug_enabled = false;
|
||||
class DebugStream : public Stream {
|
||||
public:
|
||||
int capacity = 4096;
|
||||
unsigned writes = 0;
|
||||
std::vector<uint8_t> bytes;
|
||||
int available() override { return 0; }
|
||||
int read() override { return -1; }
|
||||
int availableForWrite() override { return capacity; }
|
||||
size_t write(const uint8_t* data, size_t size) override {
|
||||
++writes;
|
||||
if (size > static_cast<size_t>(capacity)) return 0;
|
||||
bytes.insert(bytes.end(), data, data + size); return size;
|
||||
}
|
||||
};
|
||||
inline DebugStream debug_stream;
|
||||
inline bool isUsbDebugLoggingEnabled() { return logging_enabled && debug_enabled; }
|
||||
inline Stream& usbLoggingPort() { return debug_stream; }
|
||||
inline size_t nrf52DebugPrintf(const char* format, ...) {
|
||||
if (!isUsbDebugLoggingEnabled()) return 0;
|
||||
char record[256];
|
||||
va_list args; va_start(args, format);
|
||||
int size = vsnprintf(record, sizeof(record), format, args); va_end(args);
|
||||
if (size <= 0) return 0;
|
||||
if (size >= static_cast<int>(sizeof(record))) size = sizeof(record) - 1;
|
||||
return debug_stream.write(reinterpret_cast<const uint8_t*>(record), size);
|
||||
}
|
||||
#endif
|
||||
struct Packet {
|
||||
std::vector<uint8_t> payload{1, 2, 3};
|
||||
bool allowed = true;
|
||||
@@ -56,7 +90,10 @@ public:
|
||||
virtual void sendPacket(mesh::Packet*) = 0;
|
||||
virtual void onPacketReceived(mesh::Packet*) = 0;
|
||||
bool isRunning() const { return _initialized; }
|
||||
size_t seenCount() const { return _seen_packets.seen.size(); }
|
||||
protected:
|
||||
enum class IngressResult : uint8_t { Stopped, Filtered, Duplicate, Queued };
|
||||
virtual void onIngressResult(IngressResult) {}
|
||||
bool _initialized = false;
|
||||
NodePrefs* _prefs;
|
||||
mesh::PacketManager* _mgr;
|
||||
@@ -74,6 +111,10 @@ protected:
|
||||
return fletcher16(bytes, size) == checksum;
|
||||
}
|
||||
void handleReceivedPacket(mesh::Packet* packet) {
|
||||
if (!allowsPacket(packet)) {
|
||||
onIngressResult(IngressResult::Filtered); _mgr->free(packet); return;
|
||||
}
|
||||
onIngressResult(IngressResult::Queued);
|
||||
_mgr->received.push_back(packet->payload); _mgr->free(packet);
|
||||
}
|
||||
};
|
||||
+1
@@ -690,4 +690,5 @@ int main(int argc, char** argv) {
|
||||
else if (name == "counter-cycle") counter_cycle_test();
|
||||
else assert(false);
|
||||
printf("%s passed\n", name.c_str());
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compile and execute the production USB formatter with bridge-only flags."""
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class BridgeDebugFormatterTests(unittest.TestCase):
|
||||
def test_rs232_only_native_usb_has_bounded_runtime_gated_output(self):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler)
|
||||
header = (ROOT / "src/MeshCore.h").read_text(encoding="ascii")
|
||||
start = header.index("#if defined(ARDUINO) && \\\n (defined(NRF52_PLATFORM)")
|
||||
formatter = header[start:header.index("#if MESH_DEBUG && ARDUINO", start)]
|
||||
harness = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
struct Stream {
|
||||
unsigned calls = 0;
|
||||
size_t capacity = 256;
|
||||
std::string output;
|
||||
size_t write(const uint8_t* bytes, size_t length) {
|
||||
++calls;
|
||||
if (length > capacity) return 0;
|
||||
output.append(reinterpret_cast<const char*>(bytes), length);
|
||||
capacity -= length;
|
||||
return length;
|
||||
}
|
||||
} port;
|
||||
namespace mesh {
|
||||
bool master = false, verbose = false;
|
||||
bool isUsbDebugLoggingEnabled() { return master && verbose; }
|
||||
Stream& usbLoggingPort() { return port; }
|
||||
}
|
||||
@FORMATTER@
|
||||
int main() {
|
||||
using mesh::nrf52DebugPrintf;
|
||||
assert(nrf52DebugPrintf("quiet") == 0 && port.calls == 0);
|
||||
mesh::verbose = true;
|
||||
assert(nrf52DebugPrintf("quiet") == 0 && port.calls == 0);
|
||||
mesh::master = true;
|
||||
mesh::verbose = false;
|
||||
assert(nrf52DebugPrintf("quiet") == 0 && port.calls == 0);
|
||||
mesh::verbose = true;
|
||||
assert(nrf52DebugPrintf("RS232: ready\n") == 13);
|
||||
assert(port.output == "RS232: ready\n");
|
||||
port.output.clear(); port.capacity = 256;
|
||||
const std::string large(1024, 'x');
|
||||
assert(nrf52DebugPrintf("%s\n", large.c_str()) == 255);
|
||||
assert(port.output.size() == 255);
|
||||
assert(port.output.substr(251) == "...\n");
|
||||
port.output.clear(); port.capacity = 0;
|
||||
assert(nrf52DebugPrintf("blocked\n") == 0 && port.output.empty());
|
||||
assert(nrf52DebugPrintf(nullptr) == 0);
|
||||
}
|
||||
'''.replace("@FORMATTER@", formatter)
|
||||
with tempfile.TemporaryDirectory(prefix="bridge-debug-formatter-") as temporary:
|
||||
work = Path(temporary)
|
||||
(work / "helpers").mkdir()
|
||||
(work / "Arduino.h").write_text("#pragma once\n", encoding="ascii")
|
||||
(work / "helpers/NonBlockingWriteStream.h").write_text("#pragma once\n", encoding="ascii")
|
||||
source = work / "main.cpp"
|
||||
source.write_text(harness, encoding="ascii")
|
||||
for name, flags in (
|
||||
("nrf52", ["-DNRF52_PLATFORM=1"]),
|
||||
("esp32", ["-DMESH_ESP32_USB_CONSOLE_COOPERATIVE=1"]),
|
||||
):
|
||||
with self.subTest(platform=name):
|
||||
binary = work / name
|
||||
built = subprocess.run([
|
||||
compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-fno-sanitize-recover=all",
|
||||
"-fno-pie", "-no-pie", "-DARDUINO=1", "-DWITH_RS232_BRIDGE=1",
|
||||
"-DMESH_DEBUG=0", "-DBRIDGE_DEBUG=0", "-DPOWERSAVING_DEBUG=0",
|
||||
*flags, "-I" + str(work), str(source), "-o", str(binary),
|
||||
], capture_output=True, text=True, timeout=30)
|
||||
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
|
||||
checked = subprocess.run([str(binary)], capture_output=True,
|
||||
text=True, timeout=10)
|
||||
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -18,6 +18,7 @@ MOCKS = r'''
|
||||
#include <cstdarg>
|
||||
#include <string>
|
||||
#include <helpers/bridges/RS232Heartbeat.h>
|
||||
#include <helpers/bridges/RS232BridgeDiagnostics.h>
|
||||
#include <helpers/bridges/ESPNowBridgeDiagnostics.h>
|
||||
#define MAX_TRANS_UNIT 255
|
||||
#define PAYLOAD_TYPE_OTA 0x0c
|
||||
@@ -138,7 +139,7 @@ def code():
|
||||
"int esp_now_deinit() { assert(espnow_active); if(failure==18)return -1; espnow_active=false; return ESP_OK; }")
|
||||
base = (ROOT / "src/helpers/bridges/BridgeBase.cpp").read_text()
|
||||
base_header = (ROOT / "src/helpers/bridges/BridgeBase.h").read_text()
|
||||
ingress_hook = "#if defined(WITH_ESPNOW_BRIDGE)\n"
|
||||
ingress_hook = "#if defined(WITH_ESPNOW_BRIDGE) || defined(WITH_RS232_BRIDGE)\n"
|
||||
ingress_hook += method(base_header, "enum class IngressResult") + ";\n"
|
||||
ingress_hook += method(base_header, "virtual void onIngressResult(") + "\n#endif\n"
|
||||
base_methods = "\n".join(method(base, signature) for signature in (
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Sanitized production UART diagnostics, probes and legacy parser tests."""
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURES = ROOT / "test/fixtures/rs232_heartbeat"
|
||||
|
||||
|
||||
class RS232BridgeDiagnosticTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
if not compiler:
|
||||
raise AssertionError("a host C++ compiler is required")
|
||||
cls.temporary = tempfile.TemporaryDirectory(prefix="rs232-diagnostics-")
|
||||
cls.binaries = []
|
||||
for platform, cooperative in (("ESP32", 0), ("ESP32", 1),
|
||||
("NRF52_PLATFORM", 0),
|
||||
("RP2040_PLATFORM", 0),
|
||||
("STM32_PLATFORM", 0)):
|
||||
binary = Path(cls.temporary.name) / f"diagnostics-{platform}-{cooperative}"
|
||||
built = subprocess.run([
|
||||
compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-fno-sanitize-recover=all",
|
||||
"-fno-pie", "-no-pie", "-DARDUINO=1", f"-D{platform}=1",
|
||||
"-DWITH_RS232_BRIDGE=1",
|
||||
f"-DMESH_ESP32_USB_CONSOLE_COOPERATIVE={cooperative}",
|
||||
"-I" + str(FIXTURES), "-I" + str(ROOT / "src"),
|
||||
str(ROOT / "test/fixtures/rs232_bridge_diagnostics.cpp"),
|
||||
str(ROOT / "src/helpers/bridges/RS232Bridge.cpp"),
|
||||
"-o", str(binary),
|
||||
], capture_output=True, text=True, timeout=60)
|
||||
if built.returncode:
|
||||
cls.temporary.cleanup()
|
||||
raise AssertionError(built.stdout + built.stderr)
|
||||
cls.binaries.append(binary)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.temporary.cleanup()
|
||||
|
||||
def run_case(self, name):
|
||||
for binary in self.binaries:
|
||||
with self.subTest(console=binary.name):
|
||||
checked = subprocess.run([str(binary), name], capture_output=True,
|
||||
text=True, timeout=60)
|
||||
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
|
||||
self.assertIn(name + " passed", checked.stdout)
|
||||
|
||||
def test_probe_codec_strict_integrity_version_and_bounds(self):
|
||||
self.run_case("codec")
|
||||
|
||||
def test_probe_never_allocates_echoes_forwards_or_refreshes_peer_power(self):
|
||||
self.run_case("isolation")
|
||||
|
||||
def test_both_real_legacy_parsers_ignore_reserved_frames(self):
|
||||
self.run_case("legacy")
|
||||
|
||||
def test_all_partial_tx_prefixes_are_bounded_and_never_retransmitted(self):
|
||||
self.run_case("backpressure")
|
||||
|
||||
def test_stopped_busy_and_heartbeat_owned_transports_reject_probe(self):
|
||||
self.run_case("state")
|
||||
|
||||
def test_data_errors_pool_wait_and_lifetime_reset_are_observed(self):
|
||||
self.run_case("counters")
|
||||
|
||||
def test_saturating_counters_and_all_cli_page_bounds(self):
|
||||
self.run_case("bounds")
|
||||
|
||||
def test_usb_debug_both_gates_change_rate_limit_and_console_backpressure(self):
|
||||
self.run_case("debug")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,244 @@
|
||||
"""Compile actual RS232 diagnostic dispatch/forwarders with native fakes."""
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class Rs232DiagnosticCliTests(unittest.TestCase):
|
||||
def compile_run(self, source, flags=()):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler)
|
||||
with tempfile.TemporaryDirectory(prefix="rs232-cli-") as directory:
|
||||
source_path = Path(directory) / "main.cpp"
|
||||
binary = Path(directory) / "check"
|
||||
source_path.write_text(source, encoding="ascii")
|
||||
subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-fno-omit-frame-pointer",
|
||||
"-I", str(ROOT / "src"), *flags, str(source_path),
|
||||
"-o", str(binary)], check=True, capture_output=True, text=True)
|
||||
subprocess.run([str(binary)], check=True, capture_output=True, text=True)
|
||||
|
||||
def test_actual_dispatch_strict_local_actions_and_bounded_replies(self):
|
||||
source = (ROOT / "src/helpers/CommonCLI.cpp").read_text(encoding="ascii")
|
||||
header = (ROOT / "src/helpers/CommonCLI.h").read_text(encoding="ascii")
|
||||
body = extract_braced(source, "bool CommonCLI::handleRs232DiagnosticCommand(")
|
||||
defaults = "\n".join(extract_braced(header, signature) for signature in (
|
||||
"virtual void formatRs232Transport(", "virtual void formatRs232Stats(",
|
||||
"virtual bool sendRs232Test(", "virtual bool resetRs232Stats("))
|
||||
self.assertIn("if (handleRs232DiagnosticCommand(sender_timestamp, command, reply)) return;", source)
|
||||
self.compile_run(r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
static uint32_t now_ms = 0x1234ABCD;
|
||||
uint32_t millis() { return now_ms; }
|
||||
struct Callbacks {
|
||||
virtual ~Callbacks() = default;
|
||||
''' + defaults + r'''
|
||||
};
|
||||
struct Fake : Callbacks {
|
||||
int statuses = 0, stats = 0, sends = 0, resets = 0;
|
||||
uint32_t nonce = 0;
|
||||
size_t capacity = 0;
|
||||
std::string page;
|
||||
bool send_result = true, reset_result = true, long_reply = false;
|
||||
void fill(char* reply, size_t cap, const char* value) {
|
||||
capacity = cap;
|
||||
const std::string text = long_reply ? std::string(300, 'x') : value;
|
||||
snprintf(reply, cap, "%s", text.c_str());
|
||||
}
|
||||
void formatRs232Transport(char* r, size_t cap) override {
|
||||
++statuses; fill(r, cap, "status");
|
||||
}
|
||||
void formatRs232Stats(const char* p, char* r, size_t cap) override {
|
||||
++stats; page = p; fill(r, cap, "stats");
|
||||
}
|
||||
bool sendRs232Test(uint32_t n) override {
|
||||
++sends; nonce = n; return send_result;
|
||||
}
|
||||
bool resetRs232Stats() override { ++resets; return reset_result; }
|
||||
int count() const { return statuses + stats + sends + resets; }
|
||||
};
|
||||
struct CommonCLI {
|
||||
Callbacks* _callbacks;
|
||||
bool handleRs232DiagnosticCommand(uint32_t, const char*, char*);
|
||||
};
|
||||
''' + body + r'''
|
||||
int main() {
|
||||
Fake callbacks;
|
||||
CommonCLI cli{&callbacks};
|
||||
char reply[160];
|
||||
auto command = [&](const char* text, uint32_t sender = 0) {
|
||||
memset(reply, 'Z', sizeof(reply));
|
||||
const bool handled = cli.handleRs232DiagnosticCommand(sender, text, reply);
|
||||
assert(reply[157] == 'Z' && reply[158] == 'Z' && reply[159] == 'Z');
|
||||
if (handled) assert(memchr(reply, 0, 157) != nullptr);
|
||||
return handled;
|
||||
};
|
||||
assert(command("get rs232.transport", 77));
|
||||
assert(callbacks.statuses == 1 && callbacks.capacity == 157);
|
||||
for (const char* page : {"", "rx", "tx", "drops", "test"}) {
|
||||
const std::string text = std::string("get rs232.stats") + (*page ? " " : "") + page;
|
||||
assert(command(text.c_str(), 77));
|
||||
assert(callbacks.page == page && callbacks.capacity == 157);
|
||||
}
|
||||
callbacks.long_reply = true;
|
||||
assert(command("get rs232.transport"));
|
||||
assert(strlen(reply) == 156);
|
||||
assert(command("get rs232.stats rx"));
|
||||
assert(strlen(reply) == 156);
|
||||
callbacks.long_reply = false;
|
||||
for (const char* text : {
|
||||
"get rs232.transport extra", "get rs232.transportx", "get rs232.transport ",
|
||||
"get rs232.statsrx", "get rs232.stats x", "get rs232.stats rx junk",
|
||||
"get rs232.stats ", "get rs232.stats\trx", "get rs232.stats rx",
|
||||
"rs232", "rs232 testx", "rs232 test ", "rs232 test 1234567",
|
||||
"rs232 test 123456789", "rs232 test 0x123456", "rs232 test +1234567",
|
||||
"rs232 test 1234567Z", "rs232 test 12345678 ", "rs232 test 12345678 x",
|
||||
"rs232 resetstatsx", "rs232 resetstats ", "rs232 resetstats now"}) {
|
||||
const int before = callbacks.count();
|
||||
assert(command(text));
|
||||
assert(strncmp(reply, "Error:", 6) == 0 && callbacks.count() == before);
|
||||
}
|
||||
for (const char* text : {"rs232test", "rs232x test", "get rs232.enabled", "get rs232.status", "get repeat"}) {
|
||||
const int before = callbacks.count();
|
||||
assert(!command(text));
|
||||
assert(callbacks.count() == before);
|
||||
}
|
||||
for (uint32_t remote : {1U, UINT32_MAX}) {
|
||||
for (const char* text : {"rs232 test", "rs232 test 12345678", "rs232 resetstats"}) {
|
||||
const int before = callbacks.count();
|
||||
assert(command(text, remote));
|
||||
assert(strstr(reply, "local connection") && callbacks.count() == before);
|
||||
}
|
||||
}
|
||||
assert(command("rs232 test"));
|
||||
assert(callbacks.sends == 1 && callbacks.nonce == now_ms);
|
||||
assert(strcmp(reply, "OK - test queued id=1234ABCD; confirm on peer") == 0);
|
||||
assert(command("rs232 test 89abcDEF"));
|
||||
assert(callbacks.sends == 2 && callbacks.nonce == 0x89ABCDEF);
|
||||
assert(command("rs232 test 00000000"));
|
||||
assert(callbacks.sends == 3 && callbacks.nonce == 0);
|
||||
callbacks.send_result = false;
|
||||
assert(command("rs232 test DEADBEEF"));
|
||||
assert(callbacks.sends == 4 && strstr(reply, "not queued"));
|
||||
assert(command("rs232 resetstats"));
|
||||
assert(callbacks.resets == 1 && strcmp(reply, "OK - RS232 statistics reset") == 0);
|
||||
callbacks.reset_result = false;
|
||||
assert(command("rs232 resetstats"));
|
||||
assert(callbacks.resets == 2 && strstr(reply, "unavailable"));
|
||||
Callbacks unsupported;
|
||||
cli._callbacks = &unsupported;
|
||||
assert(command("get rs232.transport") && strstr(reply, "unavailable"));
|
||||
assert(command("get rs232.stats") && strstr(reply, "unavailable"));
|
||||
assert(command("rs232 test") && strstr(reply, "not queued"));
|
||||
assert(command("rs232 resetstats") && strstr(reply, "unavailable"));
|
||||
}
|
||||
''')
|
||||
|
||||
def test_actual_repeater_forwarders_select_uart_and_handle_absent_transport(self):
|
||||
repeater = (ROOT / "examples/simple_repeater/MyMesh.h").read_text(encoding="ascii")
|
||||
signatures = ("void formatRs232Transport(", "void formatRs232Stats(",
|
||||
"bool sendRs232Test(", "bool resetRs232Stats(")
|
||||
forwarders = "\n".join(extract_braced(repeater, signature) for signature in signatures)
|
||||
# Room servers own MQTT/ESP-NOW only and retain the explicit unsupported
|
||||
# defaults rather than accidentally routing UART diagnostics to MQTT.
|
||||
room = (ROOT / "examples/simple_room_server/MyMesh.h").read_text(encoding="ascii")
|
||||
for signature in signatures:
|
||||
self.assertNotIn(signature, room)
|
||||
source = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct Base {
|
||||
virtual void formatRs232Transport(char*, size_t) {}
|
||||
virtual void formatRs232Stats(const char*, char*, size_t) {}
|
||||
virtual bool sendRs232Test(uint32_t) { return false; }
|
||||
virtual bool resetRs232Stats() { return false; }
|
||||
};
|
||||
struct RS232Bridge {
|
||||
int calls = 0; uint32_t nonce = 0;
|
||||
void formatStatus(char* r, size_t cap) { ++calls; snprintf(r, cap, "status"); }
|
||||
void formatStats(const char* page, char* r, size_t cap) {
|
||||
++calls; assert(strcmp(page, "rx") == 0); snprintf(r, cap, "rx");
|
||||
}
|
||||
bool sendTest(uint32_t n) { ++calls; nonce = n; return true; }
|
||||
void resetStats() { ++calls; }
|
||||
};
|
||||
struct Role : Base {
|
||||
RS232Bridge uart, mqtt_bridge, espnow_bridge;
|
||||
RS232Bridge* bridge = &uart;
|
||||
''' + forwarders + r'''
|
||||
};
|
||||
int main() {
|
||||
Role role; char reply[160];
|
||||
memset(reply, 'Z', sizeof(reply));
|
||||
role.formatRs232Transport(reply, 157);
|
||||
role.formatRs232Stats("rx", reply, 157);
|
||||
assert(role.sendRs232Test(0xABCD));
|
||||
assert(role.resetRs232Stats());
|
||||
assert(role.uart.calls == 4 && role.uart.nonce == 0xABCD);
|
||||
assert(role.mqtt_bridge.calls == 0 && role.espnow_bridge.calls == 0);
|
||||
role.bridge = nullptr;
|
||||
role.formatRs232Transport(reply, 157);
|
||||
assert(strstr(reply, "unavailable"));
|
||||
role.formatRs232Stats("rx", reply, 157);
|
||||
assert(strstr(reply, "unavailable"));
|
||||
assert(!role.sendRs232Test(0xABCD) && !role.resetRs232Stats());
|
||||
assert(role.uart.calls == 4);
|
||||
assert(reply[157] == 'Z' && reply[158] == 'Z' && reply[159] == 'Z');
|
||||
}
|
||||
'''
|
||||
for macros in ((), ("WITH_ESPNOW_BRIDGE",), ("WITH_MQTT_BRIDGE",),
|
||||
("WITH_MQTT_BRIDGE", "WITH_ESPNOW_BRIDGE")):
|
||||
with self.subTest(macros=macros):
|
||||
self.compile_run(source, ["-DWITH_RS232_BRIDGE=1", *["-D" + m for m in macros]])
|
||||
|
||||
def test_diagnostic_virtuals_are_absent_without_rs232(self):
|
||||
header = (ROOT / "src/helpers/CommonCLI.h").read_text(encoding="ascii")
|
||||
start = header.index("#if defined(WITH_RS232_BRIDGE)\n virtual void formatRs232Transport(")
|
||||
end = header.index("\n#endif", start) + len("\n#endif")
|
||||
block = header[start:end]
|
||||
source = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
struct Callbacks {
|
||||
''' + block + r'''
|
||||
};
|
||||
template <typename T, typename = void> struct HasDiagnostics : std::false_type {};
|
||||
template <typename T> struct HasDiagnostics<T, std::void_t<
|
||||
decltype(std::declval<T>().formatRs232Transport(nullptr, 0)),
|
||||
decltype(std::declval<T>().formatRs232Stats(nullptr, nullptr, 0)),
|
||||
decltype(std::declval<T>().sendRs232Test(0)),
|
||||
decltype(std::declval<T>().resetRs232Stats())>> : std::true_type {};
|
||||
int main() {
|
||||
#if defined(WITH_RS232_BRIDGE)
|
||||
static_assert(HasDiagnostics<Callbacks>::value);
|
||||
Callbacks callbacks; char reply[157];
|
||||
callbacks.formatRs232Transport(reply, sizeof(reply));
|
||||
assert(reply[0] == 'E');
|
||||
assert(!callbacks.sendRs232Test(0) && !callbacks.resetRs232Stats());
|
||||
#else
|
||||
static_assert(!HasDiagnostics<Callbacks>::value);
|
||||
static_assert(sizeof(Callbacks) == 1);
|
||||
#endif
|
||||
}
|
||||
'''
|
||||
self.compile_run(source)
|
||||
self.compile_run(source, ["-DWITH_RS232_BRIDGE=1"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -13,6 +13,8 @@ ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <helpers/bridges/RS232BridgeDiagnostics.h>
|
||||
namespace rsdiag = mesh::bridge::rs232diag;
|
||||
#define ESP32 1
|
||||
#define BRIDGE_DEBUG_PRINTLN(...) ((void)0)
|
||||
struct NodePrefs { uint32_t bridge_baud = 57600; };
|
||||
@@ -38,7 +40,10 @@ struct RS232Bridge : BridgeBase {
|
||||
Stream* _serial;
|
||||
int16_t _rx_pin, _tx_pin;
|
||||
uint16_t _rx_buffer_pos = 37;
|
||||
bool _rx_packet_ready = true;
|
||||
bool _rx_packet_ready = true, _rx_pool_blocked = false;
|
||||
bool _tx_is_probe = false, _tx_is_data = false, _debug_dirty = false;
|
||||
bool _probe_timed_out = false;
|
||||
rsdiag::Stats _stats;
|
||||
uint16_t _tx_size = 19, _tx_pos = 3;
|
||||
bool _tx_wire_pending = true, _tx_is_ping = true;
|
||||
uint32_t _tx_ping_challenge = 4, _outstanding_challenge = 5;
|
||||
@@ -127,7 +132,8 @@ class RS232DriverLifecycleTests(unittest.TestCase):
|
||||
source.write_text(code, encoding="ascii")
|
||||
built = subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-pie", "-no-pie",
|
||||
str(source), "-o", str(binary)], capture_output=True, text=True, timeout=60)
|
||||
"-I" + str(ROOT / "src"), str(source), "-o", str(binary)],
|
||||
capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
|
||||
checked = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
|
||||
|
||||
@@ -40,6 +40,8 @@ HARNESS = r'''
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <helpers/bridges/RS232Heartbeat.h>
|
||||
#include <helpers/bridges/RS232BridgeDiagnostics.h>
|
||||
namespace rsdiag = mesh::bridge::rs232diag;
|
||||
#include <helpers/bridges/ESPNowBridgeDiagnostics.h>
|
||||
#include <helpers/bridges/ESPNowBridgeFormat.h>
|
||||
#define BRIDGE_DEBUG_PRINTLN(...) ((void)0)
|
||||
@@ -108,7 +110,11 @@ struct RS232Bridge : BridgeBase {
|
||||
uint16_t _tx_size=0,_tx_pos=0;
|
||||
uint32_t _tx_wire_until=0,_tx_ping_challenge=0;
|
||||
uint32_t _outstanding_challenge=0,_outstanding_since=0;
|
||||
bool _tx_wire_pending=false,_tx_is_ping=false;
|
||||
bool _tx_wire_pending=false,_tx_is_ping=false,_tx_is_probe=false,_tx_is_data=false,_probe_timed_out=false;
|
||||
uint32_t _probe_deadline=0;
|
||||
bool _debug_dirty=false;
|
||||
rsdiag::Stats _stats;
|
||||
void expireProbeTransmit(uint32_t);
|
||||
void drainTransmit(uint32_t,bool blocking=false);
|
||||
void sendPacket(mesh::Packet*) override;
|
||||
};
|
||||
@@ -210,7 +216,7 @@ int main() {
|
||||
# Importers use this shared harness directly, so expand its production hook
|
||||
# here rather than leaving fixture setup to one test's compilation path.
|
||||
_base_header = (ROOT / 'src/helpers/bridges/BridgeBase.h').read_text(encoding='ascii')
|
||||
_ingress_hook = '#if defined(WITH_ESPNOW_BRIDGE)\n'
|
||||
_ingress_hook = '#if defined(WITH_ESPNOW_BRIDGE) || defined(WITH_RS232_BRIDGE)\n'
|
||||
_ingress_hook += method(_base_header, 'enum class IngressResult') + ';\n'
|
||||
_ingress_hook += method(_base_header, 'virtual void onIngressResult(') + '\n#endif\n'
|
||||
HARNESS = HARNESS.replace('@INGRESS_HOOK@', _ingress_hook)
|
||||
@@ -393,7 +399,7 @@ int main() {
|
||||
methods = []
|
||||
for name, signatures in {
|
||||
'BridgeBase': ['void BridgeBase::handleReceivedPacket('],
|
||||
'RS232Bridge': ['void RS232Bridge::drainTransmit(', 'void RS232Bridge::sendPacket('],
|
||||
'RS232Bridge': ['void RS232Bridge::expireProbeTransmit(', 'void RS232Bridge::drainTransmit(', 'void RS232Bridge::sendPacket('],
|
||||
'ESPNowBridge': ['void ESPNowBridge::sendPacket('],
|
||||
'MQTTBridge': ['void MQTTBridge::sendPacket(', 'void MQTTBridge::onPacketReceived('],
|
||||
}.items():
|
||||
|
||||
Reference in new issue
Block a user