Skip to content

Commit

Permalink
Do not allow telemetryConfirm before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Jul 13, 2024
1 parent bdc77dd commit ded21a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/lib/StubbornSender/stubborn_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void StubbornSender::ResetState()
bytesLastPayload = 0;
currentOffset = 0;
currentPackage = 1;
waitUntilTelemetryConfirm = true;
telemetryConfirmExpectedValue = true;
waitCount = 0;
// 80 corresponds to UpdateTelemetryRate(ANY, 2, 1), which is what the TX uses in boost mode
maxWaitCount = 80;
Expand All @@ -45,7 +45,7 @@ void StubbornSender::SetDataToTransmit(uint8_t* dataToTransmit, uint8_t lengthTo
currentOffset = 0;
currentPackage = 1;
waitCount = 0;
senderState = (senderState == SENDER_IDLE) ? SENDING : RESYNC_THEN_SEND;
senderState = (senderState == SENDER_IDLE) ? SEND_PENDING : RESYNC_THEN_SEND;
}

/**
Expand All @@ -63,6 +63,10 @@ uint8_t StubbornSender::GetCurrentPayload(uint8_t *outData, uint8_t maxLen)
case RESYNC_THEN_SEND:
packageIndex = maxPackageIndex;
break;
case SEND_PENDING:
// This package can now be acked
senderState = SENDING;
// fallthrough
case SENDING:
{
bytesLastPayload = std::min((uint8_t)(length - currentOffset), maxLen);
Expand Down Expand Up @@ -90,12 +94,12 @@ void StubbornSender::ConfirmCurrentPayload(bool telemetryConfirmValue)
switch (senderState)
{
case SENDING:
if (telemetryConfirmValue != waitUntilTelemetryConfirm)
if (telemetryConfirmValue != telemetryConfirmExpectedValue)
{
waitCount++;
if (waitCount > maxWaitCount)
{
waitUntilTelemetryConfirm = !telemetryConfirmValue;
telemetryConfirmExpectedValue = !telemetryConfirmValue;
nextSenderState = RESYNC;
}
break;
Expand All @@ -114,30 +118,33 @@ void StubbornSender::ConfirmCurrentPayload(bool telemetryConfirmValue)
}

currentPackage++;
waitUntilTelemetryConfirm = !waitUntilTelemetryConfirm;
telemetryConfirmExpectedValue = !telemetryConfirmExpectedValue;
waitCount = 0;
break;

case RESYNC:
case RESYNC_THEN_SEND:
case WAIT_UNTIL_NEXT_CONFIRM:
if (telemetryConfirmValue == waitUntilTelemetryConfirm)
if (telemetryConfirmValue == telemetryConfirmExpectedValue)
{
nextSenderState = (senderState == RESYNC_THEN_SEND) ? SENDING : SENDER_IDLE;
waitUntilTelemetryConfirm = !telemetryConfirmValue;
telemetryConfirmExpectedValue = !telemetryConfirmValue;
}
// switch to resync if tx does not confirm value fast enough
else if (senderState == WAIT_UNTIL_NEXT_CONFIRM)
{
waitCount++;
if (waitCount > maxWaitCount)
{
waitUntilTelemetryConfirm = !telemetryConfirmValue;
telemetryConfirmExpectedValue = !telemetryConfirmValue;
nextSenderState = RESYNC;
}
}
break;

case SEND_PENDING:
// TelemetryConfirm acks are not accepted before sending
// fallthrough
case SENDER_IDLE:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/StubbornSender/stubborn_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

typedef enum {
SENDER_IDLE = 0,
SEND_PENDING,
SENDING,
WAIT_UNTIL_NEXT_CONFIRM,
RESYNC,
Expand All @@ -31,7 +32,7 @@ class StubbornSender
uint8_t currentOffset;
uint8_t bytesLastPayload;
uint8_t currentPackage;
bool waitUntilTelemetryConfirm;
bool telemetryConfirmExpectedValue;
uint16_t waitCount;
uint16_t maxWaitCount;
uint8_t maxPackageIndex;
Expand Down

0 comments on commit ded21a3

Please sign in to comment.