@@ -107,11 +107,15 @@ LoRaWANStack::LoRaWANStack()
107
107
memset (&_lw_session, 0 , sizeof (_lw_session));
108
108
memset (&_tx_msg, 0 , sizeof (_tx_msg));
109
109
memset (&_rx_msg, 0 , sizeof (_rx_msg));
110
+
111
+ LoRaMacPrimitives.mcps_confirm = callback (this , &LoRaWANStack::mcps_confirm_handler);
112
+ LoRaMacPrimitives.mcps_indication = callback (this , &LoRaWANStack::mcps_indication_handler);
113
+ LoRaMacPrimitives.mlme_confirm = callback (this , &LoRaWANStack::mlme_confirm_handler);
114
+ LoRaMacPrimitives.mlme_indication = callback (this , &LoRaWANStack::mlme_indication_handler);
110
115
}
111
116
112
117
LoRaWANStack::~LoRaWANStack ()
113
118
{
114
-
115
119
}
116
120
117
121
/* ****************************************************************************
@@ -131,6 +135,7 @@ radio_events_t *LoRaWANStack::bind_radio_driver(LoRaRadio& radio)
131
135
_lora_phy.set_radio_instance (radio);
132
136
return _mac_handlers;
133
137
}
138
+
134
139
lorawan_status_t LoRaWANStack::initialize_mac_layer (EventQueue *queue)
135
140
{
136
141
if (DEVICE_STATE_NOT_INITIALIZED != _device_current_state)
@@ -139,31 +144,20 @@ lorawan_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue)
139
144
return LORAWAN_STATUS_OK;
140
145
}
141
146
142
- static loramac_primitives_t LoRaMacPrimitives;
143
- static loramac_mib_req_confirm_t mib_req;
144
-
145
- #if defined(LORAWAN_COMPLIANCE_TEST)
146
- static uint8_t compliance_test_buffer[MBED_CONF_LORA_TX_MAX_SIZE];
147
- #endif
148
-
149
147
tr_debug (" Initializing MAC layer" );
150
148
151
149
// store a pointer to Event Queue
152
150
_queue = queue;
153
151
154
152
#if defined(LORAWAN_COMPLIANCE_TEST)
155
- // Allocate memory for compliance test
156
153
_compliance_test.app_data_buffer = compliance_test_buffer;
157
154
#endif
158
155
159
156
_lora_time.TimerTimeCounterInit (queue);
160
-
161
- LoRaMacPrimitives.mcps_confirm = callback (this , &LoRaWANStack::mcps_confirm_handler);
162
- LoRaMacPrimitives.mcps_indication = callback (this , &LoRaWANStack::mcps_indication_handler);
163
- LoRaMacPrimitives.mlme_confirm = callback (this , &LoRaWANStack::mlme_confirm_handler);
164
- LoRaMacPrimitives.mlme_indication = callback (this , &LoRaWANStack::mlme_indication_handler);
165
157
_loramac.LoRaMacInitialization (&LoRaMacPrimitives, &_lora_phy, queue);
166
158
159
+ loramac_mib_req_confirm_t mib_req;
160
+
167
161
mib_req.type = MIB_ADR;
168
162
mib_req.param .is_adr_enable = MBED_CONF_LORA_ADR_ON;
169
163
mib_set_request (&mib_req);
@@ -385,7 +379,6 @@ void LoRaWANStack::mlme_indication_handler(loramac_mlme_indication_t *mlmeIndica
385
379
}
386
380
}
387
381
388
-
389
382
void LoRaWANStack::set_lora_callbacks (lorawan_app_callbacks_t *cbs)
390
383
{
391
384
if (cbs) {
@@ -597,6 +590,10 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
597
590
return LORAWAN_STATUS_WOULD_BLOCK;
598
591
}
599
592
593
+ if (!data && length > 0 ) {
594
+ return LORAWAN_STATUS_PARAMETER_INVALID;
595
+ }
596
+
600
597
#if defined(LORAWAN_COMPLIANCE_TEST)
601
598
if (_compliance_test.running ) {
602
599
return LORAWAN_STATUS_COMPLIANCE_TEST_ON;
@@ -653,7 +650,7 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
653
650
_tx_msg.f_buffer_size = length;
654
651
_tx_msg.pending_size = 0 ;
655
652
// copy user buffer upto the max_possible_size
656
- if (data && length > 0 ) {
653
+ if (length > 0 ) {
657
654
memcpy (_tx_msg.f_buffer , data, length);
658
655
}
659
656
}
@@ -679,11 +676,11 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
679
676
680
677
tr_info (" RTS = %u bytes, PEND = %u" , _tx_msg.f_buffer_size , _tx_msg.pending_size );
681
678
set_device_state (DEVICE_STATE_SEND);
682
- lora_state_machine ();
679
+ status = lora_state_machine ();
683
680
684
681
// send user the length of data which is scheduled now.
685
682
// user should take care of the pending data.
686
- return _tx_msg.f_buffer_size ;
683
+ return (status == LORAWAN_STATUS_OK) ? _tx_msg.f_buffer_size : status ;
687
684
}
688
685
689
686
int16_t LoRaWANStack::handle_rx (const uint8_t port, uint8_t * data,
@@ -785,6 +782,8 @@ lorawan_status_t LoRaWANStack::mlme_request_handler(loramac_mlme_req_t *mlme_req
785
782
void LoRaWANStack::mlme_confirm_handler (loramac_mlme_confirm_t *mlme_confirm)
786
783
{
787
784
if (NULL == mlme_confirm) {
785
+ tr_error (" mlme_confirm: struct [in] is null!" );
786
+ MBED_ASSERT (0 );
788
787
return ;
789
788
}
790
789
@@ -793,14 +792,20 @@ void LoRaWANStack::mlme_confirm_handler(loramac_mlme_confirm_t *mlme_confirm)
793
792
if (mlme_confirm->status == LORAMAC_EVENT_INFO_STATUS_OK) {
794
793
// Status is OK, node has joined the network
795
794
set_device_state (DEVICE_STATE_JOINED);
796
- lora_state_machine ();
795
+ if (lora_state_machine () != LORAWAN_STATUS_OK) {
796
+ tr_error (" Lora state machine did not return LORAWAN_STATUS_OK" );
797
+ }
797
798
} else {
798
799
// Join attempt failed.
799
800
set_device_state (DEVICE_STATE_IDLE);
800
- lora_state_machine ();
801
+ if (lora_state_machine () != LORAWAN_STATUS_IDLE) {
802
+ tr_error (" Lora state machine did not return DEVICE_STATE_IDLE !" );
803
+ }
801
804
802
805
if (_callbacks.events ) {
803
- _queue->call (_callbacks.events , JOIN_FAILURE);
806
+ const int ret = _queue->call (_callbacks.events , JOIN_FAILURE);
807
+ MBED_ASSERT (ret != 0 );
808
+ (void )ret;
804
809
}
805
810
}
806
811
break ;
@@ -818,15 +823,16 @@ void LoRaWANStack::mlme_confirm_handler(loramac_mlme_confirm_t *mlme_confirm)
818
823
{
819
824
// normal operation as oppose to compliance testing
820
825
if (_callbacks.link_check_resp ) {
821
- _queue->call (_callbacks.link_check_resp ,
822
- mlme_confirm->demod_margin ,
823
- mlme_confirm->nb_gateways );
826
+ const int ret = _queue->call (_callbacks.link_check_resp ,
827
+ mlme_confirm->demod_margin ,
828
+ mlme_confirm->nb_gateways );
829
+ MBED_ASSERT (ret != 0 );
830
+ (void )ret;
824
831
}
825
832
}
826
833
}
827
834
break ;
828
835
default :
829
- return ;
830
836
break ;
831
837
}
832
838
}
@@ -848,7 +854,8 @@ lorawan_status_t LoRaWANStack::mcps_request_handler(loramac_mcps_req_t *mcps_req
848
854
void LoRaWANStack::mcps_confirm_handler (loramac_mcps_confirm_t *mcps_confirm)
849
855
{
850
856
if (mcps_confirm == NULL ) {
851
- tr_error (" mcps_confirm: struct [in] is null." );
857
+ tr_error (" mcps_confirm: struct [in] is null!" );
858
+ MBED_ASSERT (0 );
852
859
return ;
853
860
}
854
861
@@ -865,7 +872,9 @@ void LoRaWANStack::mcps_confirm_handler(loramac_mcps_confirm_t *mcps_confirm)
865
872
// If sending timed out, we have a special event for that
866
873
if (mcps_confirm->status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT) {
867
874
if (_callbacks.events ) {
868
- _queue->call (_callbacks.events , TX_TIMEOUT);
875
+ const int ret = _queue->call (_callbacks.events , TX_TIMEOUT);
876
+ MBED_ASSERT (ret != 0 );
877
+ (void )ret;
869
878
}
870
879
return ;
871
880
} if (mcps_confirm->status == LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT) {
@@ -874,7 +883,9 @@ void LoRaWANStack::mcps_confirm_handler(loramac_mcps_confirm_t *mcps_confirm)
874
883
875
884
// Otherwise send a general TX_ERROR event
876
885
if (_callbacks.events ) {
877
- _queue->call (_callbacks.events , TX_ERROR);
886
+ const int ret = _queue->call (_callbacks.events , TX_ERROR);
887
+ MBED_ASSERT (ret != 0 );
888
+ (void )ret;
878
889
}
879
890
return ;
880
891
}
@@ -896,7 +907,9 @@ void LoRaWANStack::mcps_confirm_handler(loramac_mcps_confirm_t *mcps_confirm)
896
907
_lw_session.uplink_counter = mcps_confirm->ul_frame_counter ;
897
908
_tx_msg.tx_ongoing = false ;
898
909
if (_callbacks.events ) {
899
- _queue->call (_callbacks.events , TX_DONE);
910
+ const int ret = _queue->call (_callbacks.events , TX_DONE);
911
+ MBED_ASSERT (ret != 0 );
912
+ (void )ret;
900
913
}
901
914
}
902
915
@@ -914,7 +927,9 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
914
927
915
928
if (mcps_indication->status != LORAMAC_EVENT_INFO_STATUS_OK) {
916
929
if (_callbacks.events ) {
917
- _queue->call (_callbacks.events , RX_ERROR);
930
+ const int ret = _queue->call (_callbacks.events , RX_ERROR);
931
+ MBED_ASSERT (ret != 0 );
932
+ (void )ret;
918
933
}
919
934
return ;
920
935
}
@@ -971,7 +986,9 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
971
986
// to the size 255 bytes
972
987
tr_debug (" Cannot receive more than buffer capacity!" );
973
988
if (_callbacks.events ) {
974
- _queue->call (_callbacks.events , RX_ERROR);
989
+ const int ret = _queue->call (_callbacks.events , RX_ERROR);
990
+ MBED_ASSERT (ret != 0 );
991
+ (void )ret;
975
992
}
976
993
return ;
977
994
} else {
@@ -987,7 +1004,9 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
987
1004
tr_debug (" Received %d bytes" , _rx_msg.msg .mcps_indication .buffer_size );
988
1005
_rx_msg.receive_ready = true ;
989
1006
if (_callbacks.events ) {
990
- _queue->call (_callbacks.events , RX_DONE);
1007
+ const int ret = _queue->call (_callbacks.events , RX_DONE);
1008
+ MBED_ASSERT (ret != 0 );
1009
+ (void )ret;
991
1010
}
992
1011
993
1012
// If fPending bit is set we try to generate an empty packet
@@ -1146,7 +1165,6 @@ lorawan_status_t LoRaWANStack::mib_set_request(loramac_mib_req_confirm_t *mib_se
1146
1165
if (NULL == mib_set_params) {
1147
1166
return LORAWAN_STATUS_PARAMETER_INVALID;
1148
1167
}
1149
-
1150
1168
return _loramac.LoRaMacMibSetRequestConfirm (mib_set_params);
1151
1169
}
1152
1170
@@ -1155,10 +1173,7 @@ lorawan_status_t LoRaWANStack::mib_get_request(loramac_mib_req_confirm_t *mib_ge
1155
1173
if (NULL == mib_get_params) {
1156
1174
return LORAWAN_STATUS_PARAMETER_INVALID;
1157
1175
}
1158
-
1159
1176
return _loramac.LoRaMacMibGetRequestConfirm (mib_get_params);
1160
-
1161
-
1162
1177
}
1163
1178
1164
1179
lorawan_status_t LoRaWANStack::set_link_check_request ()
@@ -1174,10 +1189,10 @@ lorawan_status_t LoRaWANStack::set_link_check_request()
1174
1189
return mlme_request_handler (&mlme_req);
1175
1190
}
1176
1191
1177
- void LoRaWANStack::shutdown ()
1192
+ lorawan_status_t LoRaWANStack::shutdown ()
1178
1193
{
1179
1194
set_device_state (DEVICE_STATE_SHUTDOWN);
1180
- lora_state_machine ();
1195
+ return lora_state_machine ();
1181
1196
}
1182
1197
1183
1198
lorawan_status_t LoRaWANStack::lora_state_machine ()
@@ -1219,7 +1234,9 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
1219
1234
1220
1235
tr_debug (" LoRaWAN protocol has been shut down." );
1221
1236
if (_callbacks.events ) {
1222
- _queue->call (_callbacks.events , DISCONNECTED);
1237
+ const int ret = _queue->call (_callbacks.events , DISCONNECTED);
1238
+ MBED_ASSERT (ret != 0 );
1239
+ (void )ret;
1223
1240
}
1224
1241
status = LORAWAN_STATUS_DEVICE_OFF;
1225
1242
break ;
@@ -1254,7 +1271,6 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
1254
1271
return LORAWAN_STATUS_CONNECT_IN_PROGRESS;
1255
1272
} else {
1256
1273
status = LORAWAN_STATUS_PARAMETER_INVALID;
1257
- break ;
1258
1274
}
1259
1275
break ;
1260
1276
case DEVICE_STATE_JOINED:
@@ -1263,8 +1279,11 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
1263
1279
_lw_session.active = true ;
1264
1280
// Tell the application that we are connected
1265
1281
if (_callbacks.events ) {
1266
- _queue->call (_callbacks.events , CONNECTED);
1282
+ const int ret = _queue->call (_callbacks.events , CONNECTED);
1283
+ MBED_ASSERT (ret != 0 );
1284
+ (void )ret;
1267
1285
}
1286
+ status = LORAWAN_STATUS_OK;
1268
1287
break ;
1269
1288
case DEVICE_STATE_ABP_CONNECTING:
1270
1289
/*
@@ -1297,7 +1316,9 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
1297
1316
// Session is now active
1298
1317
_lw_session.active = true ;
1299
1318
if (_callbacks.events ) {
1300
- _queue->call (_callbacks.events , CONNECTED);
1319
+ const int ret = _queue->call (_callbacks.events , CONNECTED);
1320
+ MBED_ASSERT (ret != 0 );
1321
+ (void )ret;
1301
1322
}
1302
1323
break ;
1303
1324
case DEVICE_STATE_SEND:
@@ -1315,13 +1336,17 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
1315
1336
case LORAWAN_STATUS_CRYPTO_FAIL:
1316
1337
tr_error (" Crypto failed. Clearing TX buffers" );
1317
1338
if (_callbacks.events ) {
1318
- _queue->call (_callbacks.events , TX_CRYPTO_ERROR);
1339
+ const int ret = _queue->call (_callbacks.events , TX_CRYPTO_ERROR);
1340
+ MBED_ASSERT (ret != 0 );
1341
+ (void )ret;
1319
1342
}
1320
1343
break ;
1321
1344
default :
1322
1345
tr_error (" Failure to schedule TX!" );
1323
1346
if (_callbacks.events ) {
1324
- _queue->call (_callbacks.events , TX_SCHEDULING_ERROR);
1347
+ const int ret = _queue->call (_callbacks.events , TX_SCHEDULING_ERROR);
1348
+ MBED_ASSERT (ret != 0 );
1349
+ (void )ret;
1325
1350
}
1326
1351
break ;
1327
1352
}
0 commit comments