Skip to content

Commit 668c6ab

Browse files
author
Kimmo Vaisanen
committed
Lora: Fix cancel_sending
This commit fixes some bugs from cancel_sending() method: - System crashed if method was called before initialization. Now LORAWAN_STATUS_NOT_INITIALIZED will be returned. - Method returned LORAWAN_STATUS_BUSY error when no send request was pending. LORAWAN_STATUS_OK should be returned in this case. - LORAWAN_STATUS_BUSY is now returned if backoff timer is just about to be dispatched (time_left returns 0).
1 parent 8292aff commit 668c6ab

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
@@ -845,6 +845,7 @@ lorawan_status_t LoRaMac::handle_retransmission()
845845
void LoRaMac::on_backoff_timer_expiry(void)
846846
{
847847
Lock lock(*this);
848+
_lora_time.stop(_params.timers.backoff_timer);
848849
lorawan_status_t status = schedule_tx();
849850
MBED_ASSERT(status == LORAWAN_STATUS_OK);
850851
(void) status;
@@ -1018,17 +1019,24 @@ int LoRaMac::get_backoff_timer_event_id(void)
10181019
lorawan_status_t LoRaMac::clear_tx_pipe(void)
10191020
{
10201021
// check if the event is not already queued
1021-
if (_ev_queue->time_left(get_backoff_timer_event_id()) > 0) {
1022+
const int id = get_backoff_timer_event_id();
1023+
if (id == 0) {
1024+
// No queued send request
1025+
return LORAWAN_STATUS_OK;
1026+
}
1027+
1028+
if (_ev_queue->time_left(id) > 0) {
10221029
_lora_time.stop(_params.timers.backoff_timer);
10231030
_lora_time.stop(_params.timers.ack_timeout_timer);
10241031
memset(_params.tx_buffer, 0, sizeof _params.tx_buffer);
10251032
_params.tx_buffer_len = 0;
10261033
reset_ongoing_tx(true);
10271034
tr_debug("Sending Cancelled");
10281035
return LORAWAN_STATUS_OK;
1036+
} else {
1037+
// Event is already being dispatched so it cannot be cancelled
1038+
return LORAWAN_STATUS_BUSY;
10291039
}
1030-
1031-
return LORAWAN_STATUS_BUSY;
10321040
}
10331041

10341042
lorawan_status_t LoRaMac::schedule_tx()

0 commit comments

Comments
 (0)