Skip to content

Commit

Permalink
Make emulating collisions optional
Browse files Browse the repository at this point in the history
  • Loading branch information
GUVWAF committed Dec 2, 2024
1 parent cbbb167 commit a506010
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/mesh/MeshService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void MeshService::handleToRadio(meshtastic_MeshPacket &p)
#if defined(ARCH_PORTDUINO)
if (SimRadio::instance && p.decoded.portnum == meshtastic_PortNum_SIMULATOR_APP) {
// Simulates device received a packet via the LoRa chip
SimRadio::instance->unPackAndReceive(p);
SimRadio::instance->unpackAndReceive(p);
return;
}
#endif
Expand Down
26 changes: 19 additions & 7 deletions src/platform/portduino/SimRadio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void SimRadio::startSend(meshtastic_MeshPacket *txp)
}

// Simulates device received a packet via the LoRa chip
void SimRadio::unPackAndReceive(meshtastic_MeshPacket &p)
void SimRadio::unpackAndReceive(meshtastic_MeshPacket &p)
{
// Simulator packet (=Compressed packet) is encapsulated in a MeshPacket, so need to unwrap first
meshtastic_Compressed scratch;
Expand All @@ -242,24 +242,36 @@ void SimRadio::unPackAndReceive(meshtastic_MeshPacket &p)

void SimRadio::startReceive(meshtastic_MeshPacket *p)
{
#ifdef USERPREFS_SIMRADIO_EMULATE_COLLISIONS
if (isActivelyReceiving()) {
LOG_WARN("Collision detected, dropping current and previous packet!");
rxBad++;
airTime->logAirtime(RX_ALL_LOG, getPacketTime(receivingPacket));
packetPool.release(receivingPacket);
receivingPacket = nullptr;
return;
} else if (sendingPacket && (interval - tillRun(millis()) > preambleTimeMsec)) {
// If not yet transmitting for longer than preamble, do as if not collided (channel should actually be detected as
// active)
LOG_WARN("Collision detected during transmission!");
return;
} else if (sendingPacket) {
uint32_t airtimeLeft = tillRun(millis());
if (airtimeLeft <= 0) {
LOG_WARN("Transmitting packet was already done");
handleTransmitInterrupt(); // Finish sending first
} else if ((interval - airtimeLeft) > preambleTimeMsec) {
// Only if transmitting for longer than preamble there is a collision
// (channel should actually be detected as active otherwise)
LOG_WARN("Collision detected during transmission!");
return;
}
}

isReceiving = true;
receivingPacket = packetPool.allocCopy(*p);
uint32_t airtimeMsec = getPacketTime(p);
notifyLater(airtimeMsec, ISR_RX, false); // Model the time it is busy receiving
#else
isReceiving = true;
receivingPacket = packetPool.allocCopy(*p);
handleReceiveInterrupt(); // Simulate receiving the packet immediately
startTransmitTimer();
#endif
}

meshtastic_QueueStatus SimRadio::getQueueStatus()
Expand Down
2 changes: 1 addition & 1 deletion src/platform/portduino/SimRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SimRadio : public RadioInterface, protected concurrency::NotifiedWorkerThr
meshtastic_QueueStatus getQueueStatus() override;

// Convert Compressed_msg to normal msg and receive it
void unPackAndReceive(meshtastic_MeshPacket &p);
void unpackAndReceive(meshtastic_MeshPacket &p);

/**
* Debugging counts
Expand Down

0 comments on commit a506010

Please sign in to comment.