Skip to content

Commit 6281073

Browse files
Hasnain Virktheotherjimmy
authored andcommitted
[IOTCELL-279] Using Application provided EventQueue
The EventQueue thread in LoRaMac.cpp is disbanded and the LoRaWAN protocol is redesigned to store a pointer for an application provided EventQueue. It means that now the stack runs in the same thread as application. Application provided EventQueue is used to defer ISRs from radio driver and timer callbacks as well as the application events are queued to the same event loop.
1 parent 29353a8 commit 6281073

File tree

9 files changed

+82
-150
lines changed

9 files changed

+82
-150
lines changed

features/lorawan/LoRaWANInterface.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "lorawan/LoRaWANInterface.h"
2323

24+
using namespace events;
25+
2426
inline LoRaWANStack& stk_obj()
2527
{
2628
return LoRaWANStack::get_lorawan_stack();
@@ -40,9 +42,13 @@ LoRaWANInterface::~LoRaWANInterface()
4042
{
4143
}
4244

43-
lora_mac_status_t LoRaWANInterface::initialize()
45+
lora_mac_status_t LoRaWANInterface::initialize(EventQueue *queue)
4446
{
45-
return stk_obj().initialize_mac_layer();
47+
if(!queue) {
48+
return LORA_MAC_STATUS_PARAMETER_INVALID;
49+
}
50+
51+
return stk_obj().initialize_mac_layer(queue);
4652
}
4753

4854
lora_mac_status_t LoRaWANInterface::connect()

features/lorawan/LoRaWANInterface.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ class LoRaWANInterface: public LoRaWANBase {
4040
*
4141
* You must call this first to be able to use the LoRa stack.
4242
*
43-
* \return 0 on success, a negative error code on failure.
43+
* @param ev_queue A pointer to EventQueue provided by the application.
44+
*
45+
* @return 0 on success, a negative error code on failure.
4446
*/
45-
virtual lora_mac_status_t initialize();
47+
virtual lora_mac_status_t initialize(events::EventQueue *ev_queue) ;
4648

4749
/** Connect OTAA or ABP using Mbed-OS config system
4850
*

features/lorawan/LoRaWANStack.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-3-Clause
2626
#include <string.h>
2727
#include <stdlib.h>
2828
#include "platform/Callback.h"
29+
#include "events/EventQueue.h"
2930
#include "lorawan/LoRaWANStack.h"
3031
#if defined(FEATURE_COMMON_PAL)
3132
#include "mbed_trace.h"
@@ -77,6 +78,7 @@ SPDX-License-Identifier: BSD-3-Clause
7778
#endif //MBED_CONF_LORA_PHY
7879

7980
using namespace mbed;
81+
using namespace events;
8082

8183
/**
8284
* Helper function prototypes
@@ -129,7 +131,7 @@ lora_mac_status_t LoRaWANStack::set_application_port(uint8_t port)
129131
****************************************************************************/
130132
LoRaWANStack::LoRaWANStack()
131133
: _device_current_state(DEVICE_STATE_NOT_INITIALIZED), _mac_handlers(NULL),
132-
_num_retry(1), _duty_cycle_on(LORAWAN_DUTYCYCLE_ON)
134+
_num_retry(1), _queue(NULL), _duty_cycle_on(LORAWAN_DUTYCYCLE_ON)
133135
{
134136
#ifdef MBED_CONF_LORA_APP_PORT
135137
// is_port_valid() is not virtual, so we can call it in constructor
@@ -173,7 +175,7 @@ radio_events_t *LoRaWANStack::bind_radio_driver(LoRaRadio& radio)
173175
lora_phy.set_radio_instance(radio);
174176
return _mac_handlers;
175177
}
176-
lora_mac_status_t LoRaWANStack::initialize_mac_layer()
178+
lora_mac_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue)
177179
{
178180
if (DEVICE_STATE_NOT_INITIALIZED != _device_current_state)
179181
{
@@ -188,6 +190,9 @@ lora_mac_status_t LoRaWANStack::initialize_mac_layer()
188190

189191
tr_debug("Initializing MAC layer");
190192

193+
//store a pointer to Event Queue
194+
_queue = queue;
195+
191196
// Allocate memory for compliance test
192197
_compliance_test.app_data_buffer = compliance_test_buffer;
193198

@@ -196,7 +201,7 @@ lora_mac_status_t LoRaWANStack::initialize_mac_layer()
196201
LoRaMacPrimitives.MacMcpsIndication = callback(this, &LoRaWANStack::mcps_indication);
197202
LoRaMacPrimitives.MacMlmeConfirm = callback(this, &LoRaWANStack::mlme_confirm);
198203
LoRaMacCallbacks.TxNextPacketTimerEvent = callback(this, &LoRaWANStack::on_tx_next_packet_timer_event);
199-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, lora_phy);
204+
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, &lora_phy, queue);
200205

201206
mib_req.type = LORA_MIB_ADR;
202207
mib_req.param.adr_enable = LORAWAN_ADR_ON;
@@ -394,7 +399,7 @@ void LoRaWANStack::on_tx_next_packet_timer_event(void)
394399

395400
lora_mac_status_t LoRaWANStack::set_confirmed_msg_retry(uint8_t count)
396401
{
397-
if (count > MAX_CONFIRMED_MSG_RETRIES) {
402+
if (count >= MAX_CONFIRMED_MSG_RETRIES) {
398403
return LORA_MAC_STATUS_PARAMETER_INVALID;
399404
}
400405

@@ -1046,7 +1051,7 @@ void LoRaWANStack::mlme_confirm_handler(lora_mac_mlme_confirm_t *mlme_confirm)
10461051
// Join attempt failed.
10471052
set_device_state(DEVICE_STATE_IDLE);
10481053
lora_state_machine();
1049-
_events(JOIN_FAILURE);
1054+
_queue->call(_events, JOIN_FAILURE);
10501055
}
10511056
break;
10521057
case LORA_MLME_LINK_CHECK:
@@ -1127,14 +1132,14 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
11271132

11281133
// If sending timed out, we have a special event for that
11291134
if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_TX_TIMEOUT) {
1130-
_events(TX_TIMEOUT);
1135+
_queue->call(_events, TX_TIMEOUT);
11311136
return;
11321137
} if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_RX2_TIMEOUT) {
11331138
tr_debug("Did not receive Ack");
11341139
}
11351140

11361141
// Otherwise send a general TX_ERROR event
1137-
_events(TX_ERROR);
1142+
_queue->call(_events, TX_ERROR);
11381143
return;
11391144
}
11401145

@@ -1154,7 +1159,7 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
11541159
// data rate plus frame counter.
11551160
_lw_session.uplink_counter = mcps_confirm->uplink_counter;
11561161
_tx_msg.tx_ongoing = false;
1157-
_events(TX_DONE);
1162+
_queue->call(_events, TX_DONE);
11581163
}
11591164

11601165
/** MCPS-Indication event function
@@ -1170,7 +1175,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
11701175
}
11711176

11721177
if (mcps_indication->status != LORA_EVENT_INFO_STATUS_OK) {
1173-
_events(RX_ERROR);
1178+
_queue->call(_events, RX_ERROR);
11741179
return;
11751180
}
11761181

@@ -1219,7 +1224,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
12191224
// This may never happen as both radio and MAC are limited
12201225
// to the size 255 bytes
12211226
tr_debug("Cannot receive more than buffer capacity!");
1222-
_events(RX_ERROR);
1227+
_queue->call(_events, RX_ERROR);
12231228
return;
12241229
} else {
12251230
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
@@ -1233,7 +1238,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
12331238
// Notify application about received frame..
12341239
tr_debug("Received %d bytes", _rx_msg.rx_message.mcps_indication.buffer_size);
12351240
_rx_msg.receive_ready = true;
1236-
_events(RX_DONE);
1241+
_queue->call(_events, RX_DONE);
12371242
} else {
12381243
// Invalid port, ports 0, 224 and 225-255 are reserved.
12391244
}
@@ -1800,7 +1805,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18001805
_lw_session.active = false;
18011806

18021807
tr_debug("LoRaWAN protocol has been shut down.");
1803-
_events(DISCONNECTED);
1808+
_queue->call(_events, DISCONNECTED);
18041809
status = LORA_MAC_STATUS_DEVICE_OFF;
18051810
break;
18061811
case DEVICE_STATE_NOT_INITIALIZED:
@@ -1842,7 +1847,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18421847
// Session is now active
18431848
_lw_session.active = true;
18441849
// Tell the application that we are connected
1845-
_events(CONNECTED);
1850+
_queue->call(_events, CONNECTED);
18461851
break;
18471852
case DEVICE_STATE_ABP_CONNECTING:
18481853
/*
@@ -1874,7 +1879,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18741879
status = LORA_MAC_STATUS_OK;
18751880
// Session is now active
18761881
_lw_session.active = true;
1877-
_events(CONNECTED);
1882+
_queue->call(_events, CONNECTED);
18781883
break;
18791884
case DEVICE_STATE_SEND:
18801885
// If a transmission is ongoing, don't interrupt
@@ -1890,11 +1895,11 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18901895
break;
18911896
case LORA_MAC_STATUS_CRYPTO_FAIL:
18921897
tr_error("Crypto failed. Clearing TX buffers");
1893-
_events(TX_CRYPTO_ERROR);
1898+
_queue->call(_events, TX_CRYPTO_ERROR);
18941899
break;
18951900
default:
18961901
tr_error("Failure to schedule TX!");
1897-
_events(TX_SCHEDULING_ERROR);
1902+
_queue->call(_events, TX_SCHEDULING_ERROR);
18981903
break;
18991904
}
19001905
}

features/lorawan/LoRaWANStack.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SPDX-License-Identifier: BSD-3-Clause
2727
#define LORAWANSTACK_H_
2828

2929
#include <stdint.h>
30+
#include "events/EventQueue.h"
3031
#include "platform/Callback.h"
3132
#include "platform/NonCopyable.h"
3233
#include "lorawan/system/LoRaWANTimer.h"
@@ -59,14 +60,14 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
5960
radio_events_t *bind_radio_driver(LoRaRadio& radio);
6061

6162
/** End device initialization.
62-
*
63-
* \return 0 on success, a negative error code on failure.
63+
* @param queue A pointer to an EventQueue passed from the application.
64+
* @return LORA_MAC_STATUS_OK on success, a negative error code on failure.
6465
*/
65-
lora_mac_status_t initialize_mac_layer();
66+
lora_mac_status_t initialize_mac_layer(events::EventQueue *queue);
6667

6768
/** Sets all callbacks of the LoRaWAN interface.
6869
*
69-
* \param *event_cb An event structure representing all possible callbacks.
70+
* @param *event_cb An event structure representing all possible callbacks.
7071
*/
7172
void set_lora_event_cb(mbed::Callback<void(lora_events_t)> event_cb);
7273

@@ -418,6 +419,7 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
418419
lora_mac_rx_message_t _rx_msg;
419420
uint8_t _app_port;
420421
uint8_t _num_retry;
422+
events::EventQueue *_queue;
421423
bool _duty_cycle_on;
422424
};
423425

0 commit comments

Comments
 (0)