Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions bridge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <cstdlib>
#include <cstring>
#include <deque>
#include <utility>
#include <string>
#include <vector>

Expand All @@ -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
Expand Down Expand Up @@ -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<uint8_t> gConsoleIn;
Expand Down Expand Up @@ -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<std::pair<uint32_t, std::vector<uint8_t>>> 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;
Expand Down Expand Up @@ -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());
}

Expand All @@ -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;
Expand Down Expand Up @@ -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<uint8_t>(payload.begin() + 4, payload.end()));
}
break;

case kTxDone:
sim_hal.chip().transmitFinished();
break;
Expand Down Expand Up @@ -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();
{
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions variants/host/SimHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Loading