diff --git a/bridge/main.cpp b/bridge/main.cpp index 4a06df2..94e62e3 100644 --- a/bridge/main.cpp +++ b/bridge/main.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -54,6 +55,14 @@ void loop(); extern uint32_t g_sim_millis; int g_stuck_irq_ms = 0; bool g_lenient_receive = false; +// How long the application is busy after it takes a packet from its radio: a +// real MCU spends that verifying, decrypting and deciding whether to forward, +// and a packet that lands meanwhile waits in the chip. Zero, the default, is an +// application that is never late, which no hardware is. +#ifndef SIM_RX_BUSY_MS +#define SIM_RX_BUSY_MS 0 +#endif +int g_rx_busy_ms = SIM_RX_BUSY_MS; // The receiver-noise stream the chip model draws from. Distinct per node so // two nodes do not read the same "random" bytes at the same instants - which // is what seeds each node's RNG, its CSMA backoff and its advert jitter. Left @@ -81,9 +90,11 @@ constexpr uint8_t kConsoleOut = 0x07; // bytes the node printed constexpr uint8_t kChannelBusy = 0x08; // is another station on the air here? constexpr uint8_t kRadioStats = 0x09; // node -> host: what the chip has seen constexpr uint8_t kFrameFailed = 0x0A; // host -> node: a frame locked onto and lost +constexpr uint8_t kFrameStarted = 0x0B; // host -> node: a frame whose carrier has begun constexpr uint8_t kCapFrameFailed = 0x01; // stats byte 47: this bridge takes kFrameFailed constexpr uint8_t kCapNoLock = 0x02; // and its chip reads failure 3 as never locked onto constexpr uint8_t kCapRxFaults = 0x04; // and failures 4-6: no CRC, the next header before/during the read +constexpr uint8_t kCapFrameStarted = 0x08; // and kFrameStarted sock_t gFd = BAD_SOCK; std::deque gConsoleIn; @@ -147,6 +158,34 @@ void drainTx() { writeMsg(gFd, kFrame, chip.pendingTx.data(), chip.pendingTx.size()); } +uint32_t gBusyUntil = 0; + +// Frames whose carrier starts at a millisecond this node has not reached yet: +// the engine says once per tick, and a tick can be ten of these. +std::deque>> gStarts; + +void startFrames() { + while (!gStarts.empty() && gStarts.front().first <= g_sim_millis) { + auto& f = gStarts.front().second; + sim_hal.chip().frameStarted(f.data(), f.size()); + gStarts.pop_front(); + } +} + +// One millisecond of the node. The radio's interrupt is serviced every time, as +// an ISR runs whatever the application is doing; the application runs only once +// it has finished with the last packet it took. +void stepNode() { + sim_hal.beginTick(g_sim_millis); + startFrames(); + sim_hal.servicePendingIrq(); + if (g_sim_millis < gBusyUntil) return; + const uint32_t reads = sim_hal.bufferReads(); + loop(); + drainTx(); + if (g_rx_busy_ms > 0 && sim_hal.bufferReads() != reads) gBusyUntil = g_sim_millis + g_rx_busy_ms; +} + sock_t connectTo(const std::string& addr) { #ifdef _WIN32 WSADATA wsa; @@ -207,6 +246,7 @@ int main(int argc, char** argv) { // The chip's inbox grace, back on: for reproducing a run made before this // build received only what its receiver was listening for. else if (!strcmp(argv[i], "--lenient-receive")) g_lenient_receive = true; + else if (!strcmp(argv[i], "--rx-busy-ms")) g_rx_busy_ms = atoi(next()); else if (!strcmp(argv[i], "--print-airtime")) printAirtimeFor = atoi(next()); } @@ -219,7 +259,7 @@ int main(int argc, char** argv) { } if (bridge.empty()) { fprintf(stderr, - "usage: %s --bridge host:port [--seed N] [--sf N] [--bw-khz F] [--cr N]\n" + "usage: %s --bridge host:port [--seed N] [--sf N] [--bw-khz F] [--cr N] [--rx-busy-ms N]\n" "A MeshCore node with its radio and console on a socket.\n", argv[0]); return 2; @@ -269,6 +309,17 @@ int main(int argc, char** argv) { if (n >= 1) sim_hal.chip().deliverFrameFailed(payload.data() + 1, n - 1, payload[0]); break; + case kFrameStarted: + // [start ms:4][frame], from an engine that read kCapFrameStarted. The + // frame lands in the chip's buffer as it arrives from that millisecond, + // so a late read can find the next one. + if (n > 4) { + uint32_t at = ((uint32_t)payload[0] << 24) | ((uint32_t)payload[1] << 16) | + ((uint32_t)payload[2] << 8) | payload[3]; + gStarts.emplace_back(at, std::vector(payload.begin() + 4, payload.end())); + } + break; + case kTxDone: sim_hal.chip().transmitFinished(); break; @@ -333,16 +384,10 @@ int main(int argc, char** argv) { if (g_sim_millis % 1000 == 0) { rtc_clock.advance(1); } - sim_hal.beginTick(g_sim_millis); - sim_hal.servicePendingIrq(); - loop(); - drainTx(); + stepNode(); } } else { - sim_hal.beginTick(g_sim_millis); - sim_hal.servicePendingIrq(); - loop(); - drainTx(); + stepNode(); } flushConsole(); { @@ -404,8 +449,8 @@ int main(int argc, char** argv) { // not know, and the engine cannot tell the two apart any other way. // A chip older than ABI 1.7 reads failure 3 as a CRC error, so the // engine sends it only where this bit says the chip knows better. - // Failures 4 to 6 need ABI 1.8, which the vendored chip is. - sb[47] = kCapFrameFailed | kCapNoLock | kCapRxFaults; + // Failures 4 to 6 need ABI 1.8 and kFrameStarted 1.9, which the vendored chip is. + sb[47] = kCapFrameFailed | kCapNoLock | kCapRxFaults | kCapFrameStarted; writeMsg(gFd, kRadioStats, sb, sizeof(sb)); } if (!writeMsg(gFd, kAck, payload.data(), 4)) goto done; diff --git a/build.sh b/build.sh index 4a79654..27f78b3 100755 --- a/build.sh +++ b/build.sh @@ -133,6 +133,11 @@ cxxflags=("${STD:--std=c++17}" -O2 -w -fpermissive -DMESHCORE_HOST_VARIANT=1 -DN if [ -n "${STUCK_IRQ_MS:-}" ]; then cxxflags+=("-DVIRTUAL_SX1262_STUCK_IRQ_MS=$STUCK_IRQ_MS") fi +# A build whose application is busy this long after taking each packet, as a +# real MCU is. Same flag as --rx-busy-ms, for an engine that cannot pass one. +if [ -n "${RX_BUSY_MS:-}" ]; then + cxxflags+=("-DSIM_RX_BUSY_MS=$RX_BUSY_MS") +fi # Flags this particular role needs, if any. See roles.d/README.md. if [ -f "$root/roles.d/$role.flags" ]; then diff --git a/variants/host/SimHal.h b/variants/host/SimHal.h index 027efdc..99c2b90 100644 --- a/variants/host/SimHal.h +++ b/variants/host/SimHal.h @@ -69,6 +69,7 @@ class SimHal : public RadioLibHal { // A command costs time even though BUSY never rises, so the firmware's own // timing arithmetic sees a plausible driver rather than an instant one. advanceUs(30 + len * 2); + if (len && out[0] == kReadBuffer) bufferReads_++; chip_.spiTransfer(out, len, in); // A command that clears the flags drops the line, so the next one is an edge. if (!chip_.irqAsserted()) dio1High_ = false; @@ -78,6 +79,9 @@ class SimHal : public RadioLibHal { VirtualSX1262& chip() { return chip_; } + // ReadBuffer commands so far: how the bridge sees the firmware take a packet. + uint32_t bufferReads() const { return bufferReads_; } + // Called by the bridge at the start of each tick. The node's local clock is // reconciled to network time here: if the firmware spent longer inside a tick // than the tick was worth, the excess carries rather than being forgiven. @@ -108,6 +112,7 @@ class SimHal : public RadioLibHal { static constexpr uint32_t kPinDio1 = 2; static constexpr uint32_t kPinReset = 3; static constexpr uint32_t kPinBusy = 4; + static constexpr uint8_t kReadBuffer = 0x1E; private: void advanceUs(uint64_t us) { @@ -120,6 +125,7 @@ class SimHal : public RadioLibHal { bool inIsr_ = false; bool dio1High_ = false; uint64_t localUs_ = 0; + uint32_t bufferReads_ = 0; }; extern SimHal sim_hal; diff --git a/vendor/virtual-sx1262 b/vendor/virtual-sx1262 index 6ecf441..705d1a1 160000 --- a/vendor/virtual-sx1262 +++ b/vendor/virtual-sx1262 @@ -1 +1 @@ -Subproject commit 6ecf44177f0fbd1bb04361d8a281c6fb230dbb1e +Subproject commit 705d1a11f40402937c7b8f77f8c04d3f01e8a9f9