From 668c6ab6facf9b5cf75737e5636db3d6b495b3a0 Mon Sep 17 00:00:00 2001 From: Kimmo Vaisanen Date: Fri, 27 Jul 2018 13:26:43 +0300 Subject: [PATCH] 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). --- features/lorawan/LoRaWANStack.cpp | 16 +++++++++------- features/lorawan/lorastack/mac/LoRaMac.cpp | 14 +++++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/features/lorawan/LoRaWANStack.cpp b/features/lorawan/LoRaWANStack.cpp index ece5aed9f8..3cdb543dd1 100644 --- a/features/lorawan/LoRaWANStack.cpp +++ b/features/lorawan/LoRaWANStack.cpp @@ -277,14 +277,16 @@ lorawan_status_t LoRaWANStack::enable_adaptive_datarate(bool adr_enabled) lorawan_status_t LoRaWANStack::stop_sending(void) { + if (_device_current_state == DEVICE_STATE_NOT_INITIALIZED) { + return LORAWAN_STATUS_NOT_INITIALIZED; + } + if (_loramac.clear_tx_pipe() == LORAWAN_STATUS_OK) { - if (_device_current_state == DEVICE_STATE_SENDING) { - _ctrl_flags &= ~TX_DONE_FLAG; - _ctrl_flags &= ~TX_ONGOING_FLAG; - _loramac.set_tx_ongoing(false); - _device_current_state = DEVICE_STATE_IDLE; - return LORAWAN_STATUS_OK; - } + _ctrl_flags &= ~TX_DONE_FLAG; + _ctrl_flags &= ~TX_ONGOING_FLAG; + _loramac.set_tx_ongoing(false); + _device_current_state = DEVICE_STATE_IDLE; + return LORAWAN_STATUS_OK; } return LORAWAN_STATUS_BUSY; diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index 6d7ed946bd..acdf792b5a 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -845,6 +845,7 @@ lorawan_status_t LoRaMac::handle_retransmission() void LoRaMac::on_backoff_timer_expiry(void) { Lock lock(*this); + _lora_time.stop(_params.timers.backoff_timer); lorawan_status_t status = schedule_tx(); MBED_ASSERT(status == LORAWAN_STATUS_OK); (void) status; @@ -1018,7 +1019,13 @@ int LoRaMac::get_backoff_timer_event_id(void) lorawan_status_t LoRaMac::clear_tx_pipe(void) { // check if the event is not already queued - if (_ev_queue->time_left(get_backoff_timer_event_id()) > 0) { + const int id = get_backoff_timer_event_id(); + if (id == 0) { + // No queued send request + return LORAWAN_STATUS_OK; + } + + if (_ev_queue->time_left(id) > 0) { _lora_time.stop(_params.timers.backoff_timer); _lora_time.stop(_params.timers.ack_timeout_timer); memset(_params.tx_buffer, 0, sizeof _params.tx_buffer); @@ -1026,9 +1033,10 @@ lorawan_status_t LoRaMac::clear_tx_pipe(void) reset_ongoing_tx(true); tr_debug("Sending Cancelled"); return LORAWAN_STATUS_OK; + } else { + // Event is already being dispatched so it cannot be cancelled + return LORAWAN_STATUS_BUSY; } - - return LORAWAN_STATUS_BUSY; } lorawan_status_t LoRaMac::schedule_tx()