Skip to content

Commit 03ad9d6

Browse files
authored
Merge pull request #7620 from kivaisan/fix_cancel_sending
Lora: Fix cancel_sending
2 parents 2ec9915 + 668c6ab commit 03ad9d6

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

features/lorawan/LoRaWANStack.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,16 @@ lorawan_status_t LoRaWANStack::enable_adaptive_datarate(bool adr_enabled)
277277

278278
lorawan_status_t LoRaWANStack::stop_sending(void)
279279
{
280+
if (_device_current_state == DEVICE_STATE_NOT_INITIALIZED) {
281+
return LORAWAN_STATUS_NOT_INITIALIZED;
282+
}
283+
280284
if (_loramac.clear_tx_pipe() == LORAWAN_STATUS_OK) {
281-
if (_device_current_state == DEVICE_STATE_SENDING) {
282-
_ctrl_flags &= ~TX_DONE_FLAG;
283-
_ctrl_flags &= ~TX_ONGOING_FLAG;
284-
_loramac.set_tx_ongoing(false);
285-
_device_current_state = DEVICE_STATE_IDLE;
286-
return LORAWAN_STATUS_OK;
287-
}
285+
_ctrl_flags &= ~TX_DONE_FLAG;
286+
_ctrl_flags &= ~TX_ONGOING_FLAG;
287+
_loramac.set_tx_ongoing(false);
288+
_device_current_state = DEVICE_STATE_IDLE;
289+
return LORAWAN_STATUS_OK;
288290
}
289291

290292
return LORAWAN_STATUS_BUSY;

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ lorawan_status_t LoRaMac::handle_retransmission()
839839
void LoRaMac::on_backoff_timer_expiry(void)
840840
{
841841
Lock lock(*this);
842+
_lora_time.stop(_params.timers.backoff_timer);
842843
lorawan_status_t status = schedule_tx();
843844
MBED_ASSERT(status == LORAWAN_STATUS_OK);
844845
(void) status;
@@ -1012,17 +1013,24 @@ int LoRaMac::get_backoff_timer_event_id(void)
10121013
lorawan_status_t LoRaMac::clear_tx_pipe(void)
10131014
{
10141015
// check if the event is not already queued
1015-
if (_ev_queue->time_left(get_backoff_timer_event_id()) > 0) {
1016+
const int id = get_backoff_timer_event_id();
1017+
if (id == 0) {
1018+
// No queued send request
1019+
return LORAWAN_STATUS_OK;
1020+
}
1021+
1022+
if (_ev_queue->time_left(id) > 0) {
10161023
_lora_time.stop(_params.timers.backoff_timer);
10171024
_lora_time.stop(_params.timers.ack_timeout_timer);
10181025
memset(_params.tx_buffer, 0, sizeof _params.tx_buffer);
10191026
_params.tx_buffer_len = 0;
10201027
reset_ongoing_tx(true);
10211028
tr_debug("Sending Cancelled");
10221029
return LORAWAN_STATUS_OK;
1030+
} else {
1031+
// Event is already being dispatched so it cannot be cancelled
1032+
return LORAWAN_STATUS_BUSY;
10231033
}
1024-
1025-
return LORAWAN_STATUS_BUSY;
10261034
}
10271035

10281036
lorawan_status_t LoRaMac::schedule_tx()

0 commit comments

Comments
 (0)