@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-3-Clause
26
26
#include < string.h>
27
27
#include < stdlib.h>
28
28
#include " platform/Callback.h"
29
+ #include " events/EventQueue.h"
29
30
#include " lorawan/LoRaWANStack.h"
30
31
#if defined(FEATURE_COMMON_PAL)
31
32
#include " mbed_trace.h"
@@ -77,6 +78,7 @@ SPDX-License-Identifier: BSD-3-Clause
77
78
#endif // MBED_CONF_LORA_PHY
78
79
79
80
using namespace mbed ;
81
+ using namespace events ;
80
82
81
83
/* *
82
84
* Helper function prototypes
@@ -129,7 +131,7 @@ lora_mac_status_t LoRaWANStack::set_application_port(uint8_t port)
129
131
****************************************************************************/
130
132
LoRaWANStack::LoRaWANStack ()
131
133
: _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)
133
135
{
134
136
#ifdef MBED_CONF_LORA_APP_PORT
135
137
// 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)
173
175
lora_phy.set_radio_instance (radio);
174
176
return _mac_handlers;
175
177
}
176
- lora_mac_status_t LoRaWANStack::initialize_mac_layer ()
178
+ lora_mac_status_t LoRaWANStack::initialize_mac_layer (EventQueue *queue )
177
179
{
178
180
if (DEVICE_STATE_NOT_INITIALIZED != _device_current_state)
179
181
{
@@ -188,6 +190,9 @@ lora_mac_status_t LoRaWANStack::initialize_mac_layer()
188
190
189
191
tr_debug (" Initializing MAC layer" );
190
192
193
+ // store a pointer to Event Queue
194
+ _queue = queue;
195
+
191
196
// Allocate memory for compliance test
192
197
_compliance_test.app_data_buffer = compliance_test_buffer;
193
198
@@ -196,7 +201,7 @@ lora_mac_status_t LoRaWANStack::initialize_mac_layer()
196
201
LoRaMacPrimitives.MacMcpsIndication = callback (this , &LoRaWANStack::mcps_indication);
197
202
LoRaMacPrimitives.MacMlmeConfirm = callback (this , &LoRaWANStack::mlme_confirm);
198
203
LoRaMacCallbacks.TxNextPacketTimerEvent = callback (this , &LoRaWANStack::on_tx_next_packet_timer_event);
199
- LoRaMacInitialization (&LoRaMacPrimitives, &LoRaMacCallbacks, lora_phy);
204
+ LoRaMacInitialization (&LoRaMacPrimitives, &LoRaMacCallbacks, & lora_phy, queue );
200
205
201
206
mib_req.type = LORA_MIB_ADR;
202
207
mib_req.param .adr_enable = LORAWAN_ADR_ON;
@@ -394,7 +399,7 @@ void LoRaWANStack::on_tx_next_packet_timer_event(void)
394
399
395
400
lora_mac_status_t LoRaWANStack::set_confirmed_msg_retry (uint8_t count)
396
401
{
397
- if (count > MAX_CONFIRMED_MSG_RETRIES) {
402
+ if (count >= MAX_CONFIRMED_MSG_RETRIES) {
398
403
return LORA_MAC_STATUS_PARAMETER_INVALID;
399
404
}
400
405
@@ -1046,7 +1051,7 @@ void LoRaWANStack::mlme_confirm_handler(lora_mac_mlme_confirm_t *mlme_confirm)
1046
1051
// Join attempt failed.
1047
1052
set_device_state (DEVICE_STATE_IDLE);
1048
1053
lora_state_machine ();
1049
- _events ( JOIN_FAILURE);
1054
+ _queue-> call (_events, JOIN_FAILURE);
1050
1055
}
1051
1056
break ;
1052
1057
case LORA_MLME_LINK_CHECK:
@@ -1127,14 +1132,14 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
1127
1132
1128
1133
// If sending timed out, we have a special event for that
1129
1134
if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_TX_TIMEOUT) {
1130
- _events ( TX_TIMEOUT);
1135
+ _queue-> call (_events, TX_TIMEOUT);
1131
1136
return ;
1132
1137
} if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_RX2_TIMEOUT) {
1133
1138
tr_debug (" Did not receive Ack" );
1134
1139
}
1135
1140
1136
1141
// Otherwise send a general TX_ERROR event
1137
- _events ( TX_ERROR);
1142
+ _queue-> call (_events, TX_ERROR);
1138
1143
return ;
1139
1144
}
1140
1145
@@ -1154,7 +1159,7 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
1154
1159
// data rate plus frame counter.
1155
1160
_lw_session.uplink_counter = mcps_confirm->uplink_counter ;
1156
1161
_tx_msg.tx_ongoing = false ;
1157
- _events ( TX_DONE);
1162
+ _queue-> call (_events, TX_DONE);
1158
1163
}
1159
1164
1160
1165
/* * MCPS-Indication event function
@@ -1170,7 +1175,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
1170
1175
}
1171
1176
1172
1177
if (mcps_indication->status != LORA_EVENT_INFO_STATUS_OK) {
1173
- _events ( RX_ERROR);
1178
+ _queue-> call (_events, RX_ERROR);
1174
1179
return ;
1175
1180
}
1176
1181
@@ -1219,7 +1224,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
1219
1224
// This may never happen as both radio and MAC are limited
1220
1225
// to the size 255 bytes
1221
1226
tr_debug (" Cannot receive more than buffer capacity!" );
1222
- _events ( RX_ERROR);
1227
+ _queue-> call (_events, RX_ERROR);
1223
1228
return ;
1224
1229
} else {
1225
1230
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
@@ -1233,7 +1238,7 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
1233
1238
// Notify application about received frame..
1234
1239
tr_debug (" Received %d bytes" , _rx_msg.rx_message .mcps_indication .buffer_size );
1235
1240
_rx_msg.receive_ready = true ;
1236
- _events ( RX_DONE);
1241
+ _queue-> call (_events, RX_DONE);
1237
1242
} else {
1238
1243
// Invalid port, ports 0, 224 and 225-255 are reserved.
1239
1244
}
@@ -1800,7 +1805,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
1800
1805
_lw_session.active = false ;
1801
1806
1802
1807
tr_debug (" LoRaWAN protocol has been shut down." );
1803
- _events ( DISCONNECTED);
1808
+ _queue-> call (_events, DISCONNECTED);
1804
1809
status = LORA_MAC_STATUS_DEVICE_OFF;
1805
1810
break ;
1806
1811
case DEVICE_STATE_NOT_INITIALIZED:
@@ -1842,7 +1847,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
1842
1847
// Session is now active
1843
1848
_lw_session.active = true ;
1844
1849
// Tell the application that we are connected
1845
- _events ( CONNECTED);
1850
+ _queue-> call (_events, CONNECTED);
1846
1851
break ;
1847
1852
case DEVICE_STATE_ABP_CONNECTING:
1848
1853
/*
@@ -1874,7 +1879,7 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
1874
1879
status = LORA_MAC_STATUS_OK;
1875
1880
// Session is now active
1876
1881
_lw_session.active = true ;
1877
- _events ( CONNECTED);
1882
+ _queue-> call (_events, CONNECTED);
1878
1883
break ;
1879
1884
case DEVICE_STATE_SEND:
1880
1885
// If a transmission is ongoing, don't interrupt
@@ -1890,11 +1895,11 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
1890
1895
break ;
1891
1896
case LORA_MAC_STATUS_CRYPTO_FAIL:
1892
1897
tr_error (" Crypto failed. Clearing TX buffers" );
1893
- _events ( TX_CRYPTO_ERROR);
1898
+ _queue-> call (_events, TX_CRYPTO_ERROR);
1894
1899
break ;
1895
1900
default :
1896
1901
tr_error (" Failure to schedule TX!" );
1897
- _events ( TX_SCHEDULING_ERROR);
1902
+ _queue-> call (_events, TX_SCHEDULING_ERROR);
1898
1903
break ;
1899
1904
}
1900
1905
}
0 commit comments