Skip to content

Commit 78a58be

Browse files
committed
Fix #417: handle TX busy failures more gracefully
1 parent 56fd7e0 commit 78a58be

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/lmic/lmic_compliance.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ fsmDispatch(
556556
}
557557

558558
case LMIC_COMPLIANCE_FSMSTATE_TESTMODE: {
559+
if (LMIC.opmode & OP_TXDATA) {
560+
// stay here until we can do something
561+
break;
562+
}
559563
if (eventflags_TestAndClear(LMIC_COMPLIANCE_EVENT_DEACTIVATE)) {
560564
newState = LMIC_COMPLIANCE_FSMSTATE_INACTIVE;
561565
} else if (eventflags_TestAndClear(LMIC_COMPLIANCE_EVENT_JOIN_CMD)) {
@@ -652,10 +656,12 @@ static void lmicEventCb(
652656
);
653657
}
654658

655-
// if it's a EV_JOINED, we should tell the FSM.
659+
// if it's a EV_JOINED, or a TXCMOMPLETE, we should tell the FSM.
656660
if (ev == EV_JOINED) {
657661
LMIC_Compliance.eventflags |= LMIC_COMPLIANCE_EVENT_JOINED;
658662
fsmEvalDeferred();
663+
} else if (ev == EV_TXCOMPLETE) {
664+
fsmEvalDeferred();
659665
}
660666
}
661667

@@ -679,22 +685,34 @@ static void acSendUplink(void) {
679685
LMIC_Compliance.eventflags &= ~LMIC_COMPLIANCE_EVENT_UPLINK_COMPLETE;
680686

681687
// don't try to send if busy; might be sending echo message.
682-
if ((LMIC.opmode & OP_TXRXPEND) == 0 &&
688+
lmic_tx_error_t const eSend =
683689
LMIC_sendWithCallback(
684690
LORAWAN_PORT_COMPLIANCE,
685691
payload, sizeof(payload),
686692
/* confirmed? */
687693
!! (LMIC_Compliance.fsmFlags & LMIC_COMPLIANCE_FSM_CONFIRM),
688-
sendUplinkCompleteCb, NULL) == 0) {
694+
sendUplinkCompleteCb, NULL
695+
);
696+
697+
if (eSend == LMIC_ERROR_SUCCESS) {
689698
// queued successfully
690699
LMIC_COMPLIANCE_PRINTF(
691-
"lmic_compliance.acSendUplink: queued uplink message(%u, %p)\n",
700+
"lmic_compliance.%s: queued uplink message(%u, %p)\n",
701+
__func__,
692702
(unsigned) downlink & 0xFFFF,
693703
LMIC.client.txMessageCb
694704
);
695705
} else {
696706
// failed to queue; just skip this cycle.
697-
sendUplinkCompleteCb(NULL, false);
707+
LMIC_COMPLIANCE_PRINTF(
708+
"lmic_compliance.%s: error(%d) sending uplink message(%u), %u bytes\n",
709+
__func__,
710+
eSend,
711+
(unsigned) downlink & 0xFFFF,
712+
LMIC.client.txMessageCb
713+
);
714+
LMIC_Compliance.eventflags |= LMIC_COMPLIANCE_EVENT_UPLINK_COMPLETE;
715+
fsmEval();
698716
}
699717
}
700718

@@ -706,16 +724,18 @@ static void sendUplinkCompleteCb(void *pUserData, int fSuccess) {
706724

707725
static void acSendUplinkBuffer(void) {
708726
// send uplink data.
709-
if (LMIC_sendWithCallback(
727+
lmic_tx_error_t const eSend =
728+
LMIC_sendWithCallback(
710729
LORAWAN_PORT_COMPLIANCE,
711730
LMIC_Compliance.uplinkMessage, LMIC_Compliance.uplinkSize,
712731
/* confirmed? */ (LMIC_Compliance.fsmFlags & LMIC_COMPLIANCE_FSM_CONFIRM) != 0,
713732
sendUplinkCompleteCb,
714-
NULL) == 0
715-
) {
733+
NULL);
734+
735+
if (eSend == LMIC_ERROR_SUCCESS) {
716736
LMIC_COMPLIANCE_PRINTF("%s: queued %u bytes\n", __func__, LMIC_Compliance.uplinkSize);
717737
} else {
718-
LMIC_COMPLIANCE_PRINTF("%s: uplink %u bytes failed\n", __func__, LMIC_Compliance.uplinkSize);
738+
LMIC_COMPLIANCE_PRINTF("%s: uplink %u bytes failed (error %d)\n", __func__, LMIC_Compliance.uplinkSize, eSend);
719739
LMIC_Compliance.eventflags |= LMIC_COMPLIANCE_EVENT_UPLINK_COMPLETE;
720740
fsmEval();
721741
}

0 commit comments

Comments
 (0)