sforkowany z mirror/meshtastic-firmware
Implement listen-before-talk mechanism
- Function setRandomDelay() calls either startTransmitTimer() or startTransmitTimerSNR() - After coming back from Rx/Tx-ing, call setRandomDelay() - If channel is currently busy, call setRandomDelay()raytac-diy
rodzic
6d01f9aa89
commit
c60d4c1ecc
|
@ -118,20 +118,10 @@ ErrorCode RadioLibInterface::send(MeshPacket *p)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want all sending/receiving to be done by our daemon thread, We use a delay here because this packet might have been sent
|
// set random transmit delay to let others reconfigure their radio,
|
||||||
// in response to a packet we just received. So we want to make sure the other side has had a chance to reconfigure its radio
|
// to avoid collisions and implement timing-based flooding
|
||||||
|
// DEBUG_MSG("Set random delay before transmitting.\n");
|
||||||
/* We assume if rx_snr = 0 and rx_rssi = 0, the packet was not generated locally.
|
setRandomDelay();
|
||||||
* This assumption is valid because of the offset generated by the radio to account for the noise
|
|
||||||
* floor.
|
|
||||||
*/
|
|
||||||
if (p->rx_snr == 0 && p->rx_rssi == 0) {
|
|
||||||
startTransmitTimer(true);
|
|
||||||
} else {
|
|
||||||
// If there is a SNR, start a timer scaled based on that SNR.
|
|
||||||
DEBUG_MSG("rx_snr found. hop_limit:%d rx_snr:%f\n", p->hop_limit, p->rx_snr);
|
|
||||||
startTransmitTimerSNR(p->rx_snr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
#else
|
#else
|
||||||
|
@ -164,8 +154,8 @@ bool RadioLibInterface::cancelSending(NodeNum from, PacketId id)
|
||||||
/** radio helper thread callback.
|
/** radio helper thread callback.
|
||||||
|
|
||||||
We never immediately transmit after any operation (either rx or tx). Instead we should start receiving and
|
We never immediately transmit after any operation (either rx or tx). Instead we should start receiving and
|
||||||
wait a random delay of 50 to 200 ms to make sure we are not stomping on someone else. The 50ms delay at the beginning ensures all
|
wait a random delay of 100ms to 100ms+shortPacketMsec to make sure we are not stomping on someone else. The 100ms delay at the beginning ensures all
|
||||||
possible listeners have had time to finish processing the previous packet and now have their radio in RX state. The up to 200ms
|
possible listeners have had time to finish processing the previous packet and now have their radio in RX state. The up to 100ms+shortPacketMsec
|
||||||
random delay gives a chance for all possible senders to have high odds of detecting that someone else started transmitting first
|
random delay gives a chance for all possible senders to have high odds of detecting that someone else started transmitting first
|
||||||
and then they will wait until that packet finishes.
|
and then they will wait until that packet finishes.
|
||||||
|
|
||||||
|
@ -192,20 +182,26 @@ void RadioLibInterface::onNotify(uint32_t notification)
|
||||||
case TRANSMIT_DELAY_COMPLETED:
|
case TRANSMIT_DELAY_COMPLETED:
|
||||||
// DEBUG_MSG("delay done\n");
|
// DEBUG_MSG("delay done\n");
|
||||||
|
|
||||||
// If we are not currently in receive mode, then restart the timer and try again later (this can happen if the main thread
|
// If we are not currently in receive mode, then restart the random delay (this can happen if the main thread
|
||||||
// has placed the unit into standby) FIXME, how will this work if the chipset is in sleep mode?
|
// has placed the unit into standby) FIXME, how will this work if the chipset is in sleep mode?
|
||||||
if (!txQueue.empty()) {
|
if (!txQueue.empty()) {
|
||||||
if (!canSendImmediately()) {
|
if (!canSendImmediately()) {
|
||||||
startTransmitTimer(); // try again in a little while
|
// DEBUG_MSG("Currently Rx/Tx-ing: set random delay\n");
|
||||||
|
setRandomDelay(); // currently Rx/Tx-ing: reset random delay
|
||||||
} else {
|
} else {
|
||||||
// Send any outgoing packets we have ready
|
if (isChannelActive()) { // check if there is currently a LoRa packet on the channel
|
||||||
MeshPacket *txp = txQueue.dequeue();
|
// DEBUG_MSG("Channel is active: set random delay\n");
|
||||||
assert(txp);
|
setRandomDelay(); // reset random delay
|
||||||
startSend(txp);
|
} else {
|
||||||
|
// Send any outgoing packets we have ready
|
||||||
|
MeshPacket *txp = txQueue.dequeue();
|
||||||
|
assert(txp);
|
||||||
|
startSend(txp);
|
||||||
|
|
||||||
// Packet has been sent, count it toward our TX airtime utilization.
|
// Packet has been sent, count it toward our TX airtime utilization.
|
||||||
uint32_t xmitMsec = getPacketTime(txp);
|
uint32_t xmitMsec = getPacketTime(txp);
|
||||||
airTime->logAirtime(TX_LOG, xmitMsec);
|
airTime->logAirtime(TX_LOG, xmitMsec);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// DEBUG_MSG("done with txqueue\n");
|
// DEBUG_MSG("done with txqueue\n");
|
||||||
|
@ -216,6 +212,26 @@ void RadioLibInterface::onNotify(uint32_t notification)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RadioLibInterface::setRandomDelay()
|
||||||
|
{
|
||||||
|
MeshPacket *p = txQueue.getFront();
|
||||||
|
// We want all sending/receiving to be done by our daemon thread.
|
||||||
|
// We use a delay here because this packet might have been sent in response to a packet we just received.
|
||||||
|
// So we want to make sure the other side has had a chance to reconfigure its radio.
|
||||||
|
|
||||||
|
/* We assume if rx_snr = 0 and rx_rssi = 0, the packet was generated locally.
|
||||||
|
* This assumption is valid because of the offset generated by the radio to account for the noise
|
||||||
|
* floor.
|
||||||
|
*/
|
||||||
|
if (p->rx_snr == 0 && p->rx_rssi == 0) {
|
||||||
|
startTransmitTimer(true);
|
||||||
|
} else {
|
||||||
|
// If there is a SNR, start a timer scaled based on that SNR.
|
||||||
|
DEBUG_MSG("rx_snr found. hop_limit:%d rx_snr:%f\n", p->hop_limit, p->rx_snr);
|
||||||
|
startTransmitTimerSNR(p->rx_snr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RadioLibInterface::startTransmitTimer(bool withDelay)
|
void RadioLibInterface::startTransmitTimer(bool withDelay)
|
||||||
{
|
{
|
||||||
// If we have work to do and the timer wasn't already scheduled, schedule it now
|
// If we have work to do and the timer wasn't already scheduled, schedule it now
|
||||||
|
|
|
@ -132,6 +132,9 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
|
||||||
*/
|
*/
|
||||||
virtual void startReceive() = 0;
|
virtual void startReceive() = 0;
|
||||||
|
|
||||||
|
/** can we detect a LoRa preamble on the current channel? */
|
||||||
|
virtual bool isChannelActive() = 0;
|
||||||
|
|
||||||
/** are we actively receiving a packet (only called during receiving state)
|
/** are we actively receiving a packet (only called during receiving state)
|
||||||
* This method is only public to facilitate debugging. Do not call.
|
* This method is only public to facilitate debugging. Do not call.
|
||||||
*/
|
*/
|
||||||
|
@ -142,17 +145,13 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** if we have something waiting to send, start a short random timer so we can come check for collision before actually doing
|
/** if we have something waiting to send, start a short random timer so we can come check for collision before actually doing
|
||||||
* the transmit
|
* the transmit */
|
||||||
*
|
void setRandomDelay();
|
||||||
* If the timer was already running, we just wait for that one to occur.
|
|
||||||
* */
|
/** random timer with certain min. and max. settings */
|
||||||
void startTransmitTimer(bool withDelay = true);
|
void startTransmitTimer(bool withDelay = true);
|
||||||
|
|
||||||
/** if we have something waiting to send, start a short scaled timer based on SNR so we can come check for collision before actually doing
|
/** timer scaled to SNR of to be flooded packet */
|
||||||
* the transmit
|
|
||||||
*
|
|
||||||
* If the timer was already running, we just wait for that one to occur.
|
|
||||||
* */
|
|
||||||
void startTransmitTimerSNR(float snr);
|
void startTransmitTimerSNR(float snr);
|
||||||
|
|
||||||
void handleTransmitInterrupt();
|
void handleTransmitInterrupt();
|
||||||
|
|
Ładowanie…
Reference in New Issue