Skip to content

Commit 53cb6e0

Browse files
authored
Merge pull request #6279 from AnttiKauppila/master
LoRa refactoring
2 parents 2c7f909 + 3225785 commit 53cb6e0

20 files changed

+996
-1454
lines changed

features/lorawan/LoRaWANInterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class LoRaWANInterface: public LoRaWANBase {
3434
*
3535
*/
3636
LoRaWANInterface(LoRaRadio& radio);
37+
3738
virtual ~LoRaWANInterface();
3839

3940
/** Initialize the LoRa stack.
@@ -44,7 +45,7 @@ class LoRaWANInterface: public LoRaWANBase {
4445
*
4546
* @return 0 on success, a negative error code on failure.
4647
*/
47-
virtual lorawan_status_t initialize(events::EventQueue *ev_queue) ;
48+
virtual lorawan_status_t initialize(events::EventQueue *ev_queue);
4849

4950
/** Connect OTAA or ABP using Mbed-OS config system
5051
*

features/lorawan/LoRaWANStack.cpp

Lines changed: 103 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,15 @@ lorawan_status_t LoRaWANStack::set_application_port(uint8_t port)
8787
LoRaWANStack::LoRaWANStack()
8888
: _loramac(_lora_time), _lora_phy(_lora_time),
8989
_device_current_state(DEVICE_STATE_NOT_INITIALIZED), _mac_handlers(NULL),
90-
_num_retry(1), _queue(NULL), _duty_cycle_on(MBED_CONF_LORA_DUTY_CYCLE_ON)
90+
_num_retry(1), _app_port(INVALID_PORT), _duty_cycle_on(MBED_CONF_LORA_DUTY_CYCLE_ON),
91+
_queue(NULL)
9192
{
9293
#ifdef MBED_CONF_LORA_APP_PORT
93-
// is_port_valid() is not virtual, so we can call it in constructor
9494
if (is_port_valid(MBED_CONF_LORA_APP_PORT)) {
9595
_app_port = MBED_CONF_LORA_APP_PORT;
9696
} else {
9797
tr_error("User defined port in .json is illegal.");
98-
_app_port = INVALID_PORT;
9998
}
100-
101-
#else
102-
// initialize it to INVALID_PORT (255) an illegal port number.
103-
// user should set the port
104-
_app_port = INVALID_PORT;
10599
#endif
106100

107101
memset(&_lw_session, 0, sizeof(_lw_session));
@@ -175,95 +169,6 @@ lorawan_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue)
175169
return lora_state_machine();
176170
}
177171

178-
#if defined(LORAWAN_COMPLIANCE_TEST)
179-
/**
180-
*
181-
* Prepares the upload message to reserved ports
182-
*
183-
* \param port Application port
184-
*/
185-
void LoRaWANStack::prepare_special_tx_frame(uint8_t port)
186-
{
187-
if (port == 224) {
188-
// Clear any normal message stuff before compliance test.
189-
memset(&_tx_msg, 0, sizeof(_tx_msg));
190-
191-
if (_compliance_test.link_check == true) {
192-
_compliance_test.link_check = false;
193-
_compliance_test.state = 1;
194-
_tx_msg.f_buffer_size = 3;
195-
_tx_msg.f_buffer[0] = 5;
196-
_tx_msg.f_buffer[1] = _compliance_test.demod_margin;
197-
_tx_msg.f_buffer[2] = _compliance_test.nb_gateways;
198-
} else {
199-
switch (_compliance_test.state) {
200-
case 4:
201-
_compliance_test.state = 1;
202-
_tx_msg.f_buffer_size = _compliance_test.app_data_size;
203-
204-
_tx_msg.f_buffer[0] = _compliance_test.app_data_buffer[0];
205-
for(uint8_t i = 1; i < MIN(_compliance_test.app_data_size, MBED_CONF_LORA_TX_MAX_SIZE); ++i) {
206-
_tx_msg.f_buffer[i] = _compliance_test.app_data_buffer[i];
207-
}
208-
break;
209-
case 1:
210-
_tx_msg.f_buffer_size = 2;
211-
_tx_msg.f_buffer[0] = _compliance_test.downlink_counter >> 8;
212-
_tx_msg.f_buffer[1] = _compliance_test.downlink_counter;
213-
break;
214-
}
215-
}
216-
}
217-
}
218-
219-
/** Hands over the compliance test frame to MAC layer
220-
*
221-
* \return returns the state of the LoRa MAC
222-
*/
223-
lorawan_status_t LoRaWANStack::send_compliance_test_frame_to_mac()
224-
{
225-
loramac_mcps_req_t mcps_req;
226-
227-
get_phy_params_t phy_params;
228-
phy_param_t default_datarate;
229-
phy_params.attribute = PHY_DEF_TX_DR;
230-
default_datarate = _lora_phy.get_phy_params(&phy_params);
231-
232-
prepare_special_tx_frame(_compliance_test.app_port);
233-
234-
if (!_compliance_test.is_tx_confirmed) {
235-
mcps_req.type = MCPS_UNCONFIRMED;
236-
mcps_req.req.unconfirmed.fport = _compliance_test.app_port;
237-
mcps_req.f_buffer = _tx_msg.f_buffer;
238-
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
239-
mcps_req.req.unconfirmed.data_rate = default_datarate.value;
240-
241-
tr_info("Transmit unconfirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
242-
243-
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
244-
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
245-
}
246-
} else if (_compliance_test.is_tx_confirmed) {
247-
mcps_req.type = MCPS_CONFIRMED;
248-
mcps_req.req.confirmed.fport = _compliance_test.app_port;
249-
mcps_req.f_buffer = _tx_msg.f_buffer;
250-
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
251-
mcps_req.req.confirmed.nb_trials = _num_retry;
252-
mcps_req.req.confirmed.data_rate = default_datarate.value;
253-
254-
tr_info("Transmit confirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
255-
256-
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
257-
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
258-
}
259-
} else {
260-
return LORAWAN_STATUS_SERVICE_UNKNOWN;
261-
}
262-
263-
return mcps_request_handler(&mcps_req);
264-
}
265-
#endif
266-
267172
uint16_t LoRaWANStack::check_possible_tx_size(uint16_t size)
268173
{
269174
loramac_tx_info_t tx_info;
@@ -286,51 +191,48 @@ lorawan_status_t LoRaWANStack::send_frame_to_mac()
286191
lorawan_status_t status;
287192
loramac_mib_req_confirm_t mib_get_params;
288193

289-
get_phy_params_t phy_params;
290-
phy_param_t default_datarate;
291-
phy_params.attribute = PHY_DEF_TX_DR;
292-
default_datarate = _lora_phy.get_phy_params(&phy_params);
293-
294194
mcps_req.type = _tx_msg.type;
295195

296196
if (MCPS_UNCONFIRMED == mcps_req.type) {
297-
mcps_req.req.unconfirmed.fport = _tx_msg.message_u.unconfirmed.fport;
298197
mcps_req.f_buffer = _tx_msg.f_buffer;
299-
300198
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
301199

200+
mcps_req.fport = _tx_msg.fport;
201+
mcps_req.nb_trials = 1;
302202
mib_get_params.type = MIB_CHANNELS_DATARATE;
303203
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
304204
tr_debug("Couldn't get MIB parameters: Using default data rate");
305-
mcps_req.req.unconfirmed.data_rate = default_datarate.value;
205+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
306206
} else {
307-
mcps_req.req.unconfirmed.data_rate = mib_get_params.param.channel_data_rate;
207+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
308208
}
309209

310210
} else if (mcps_req.type == MCPS_CONFIRMED) {
311-
mcps_req.req.confirmed.fport = _tx_msg.message_u.confirmed.fport;
312211
mcps_req.f_buffer = _tx_msg.f_buffer;
313212
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
314-
mcps_req.req.confirmed.nb_trials = _tx_msg.message_u.confirmed.nb_trials;
213+
mcps_req.fport = _tx_msg.fport;
214+
mcps_req.nb_trials = _tx_msg.nb_trials;
315215

316216
mib_get_params.type = MIB_CHANNELS_DATARATE;
317217
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
318218
tr_debug("Couldn't get MIB parameters: Using default data rate");
319-
mcps_req.req.confirmed.data_rate = default_datarate.value;
219+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
320220
} else {
321-
mcps_req.req.confirmed.data_rate = mib_get_params.param.channel_data_rate;
221+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
322222
}
323223

324224
} else if ( mcps_req.type == MCPS_PROPRIETARY) {
325225
mcps_req.f_buffer = _tx_msg.f_buffer;
326226
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
227+
mcps_req.fport = 0;
228+
mcps_req.nb_trials = 1;
327229

328230
mib_get_params.type = MIB_CHANNELS_DATARATE;
329231
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
330232
tr_debug("Couldn't get MIB parameters: Using default data rate");
331-
mcps_req.req.proprietary.data_rate = default_datarate.value;
233+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
332234
} else {
333-
mcps_req.req.proprietary.data_rate = mib_get_params.param.channel_data_rate;
235+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
334236
}
335237

336238
} else {
@@ -661,7 +563,7 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
661563
|| (flags & MSG_FLAG_MASK) == MSG_UNCONFIRMED_PROPRIETARY) {
662564

663565
_tx_msg.type = MCPS_UNCONFIRMED;
664-
_tx_msg.message_u.unconfirmed.fport = _app_port;
566+
_tx_msg.fport = _app_port;
665567
}
666568

667569
// Handles all confirmed messages, including proprietary and multicast
@@ -670,8 +572,8 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
670572
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_PROPRIETARY) {
671573

672574
_tx_msg.type = MCPS_CONFIRMED;
673-
_tx_msg.message_u.confirmed.fport = _app_port;
674-
_tx_msg.message_u.confirmed.nb_trials = _num_retry;
575+
_tx_msg.fport = _app_port;
576+
_tx_msg.nb_trials = _num_retry;
675577
}
676578

677579
tr_info("RTS = %u bytes, PEND = %u", _tx_msg.f_buffer_size, _tx_msg.pending_size);
@@ -1367,3 +1269,89 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
13671269

13681270
return status;
13691271
}
1272+
1273+
#if defined(LORAWAN_COMPLIANCE_TEST)
1274+
/**
1275+
*
1276+
* Prepares the upload message to reserved ports
1277+
*
1278+
* \param port Application port
1279+
*/
1280+
void LoRaWANStack::prepare_special_tx_frame(uint8_t port)
1281+
{
1282+
if (port == 224) {
1283+
// Clear any normal message stuff before compliance test.
1284+
memset(&_tx_msg, 0, sizeof(_tx_msg));
1285+
1286+
if (_compliance_test.link_check == true) {
1287+
_compliance_test.link_check = false;
1288+
_compliance_test.state = 1;
1289+
_tx_msg.f_buffer_size = 3;
1290+
_tx_msg.f_buffer[0] = 5;
1291+
_tx_msg.f_buffer[1] = _compliance_test.demod_margin;
1292+
_tx_msg.f_buffer[2] = _compliance_test.nb_gateways;
1293+
} else {
1294+
switch (_compliance_test.state) {
1295+
case 4:
1296+
_compliance_test.state = 1;
1297+
_tx_msg.f_buffer_size = _compliance_test.app_data_size;
1298+
1299+
_tx_msg.f_buffer[0] = _compliance_test.app_data_buffer[0];
1300+
for(uint8_t i = 1; i < MIN(_compliance_test.app_data_size, MBED_CONF_LORA_TX_MAX_SIZE); ++i) {
1301+
_tx_msg.f_buffer[i] = _compliance_test.app_data_buffer[i];
1302+
}
1303+
break;
1304+
case 1:
1305+
_tx_msg.f_buffer_size = 2;
1306+
_tx_msg.f_buffer[0] = _compliance_test.downlink_counter >> 8;
1307+
_tx_msg.f_buffer[1] = _compliance_test.downlink_counter;
1308+
break;
1309+
}
1310+
}
1311+
}
1312+
}
1313+
1314+
/** Hands over the compliance test frame to MAC layer
1315+
*
1316+
* \return returns the state of the LoRa MAC
1317+
*/
1318+
lorawan_status_t LoRaWANStack::send_compliance_test_frame_to_mac()
1319+
{
1320+
loramac_mcps_req_t mcps_req;
1321+
1322+
prepare_special_tx_frame(_compliance_test.app_port);
1323+
1324+
if (!_compliance_test.is_tx_confirmed) {
1325+
mcps_req.type = MCPS_UNCONFIRMED;
1326+
mcps_req.f_buffer = _tx_msg.f_buffer;
1327+
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
1328+
mcps_req.fport = _compliance_test.app_port;
1329+
mcps_req.nb_trials = 1;
1330+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
1331+
1332+
tr_info("Transmit unconfirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
1333+
1334+
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
1335+
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
1336+
}
1337+
} else if (_compliance_test.is_tx_confirmed) {
1338+
mcps_req.type = MCPS_CONFIRMED;
1339+
mcps_req.f_buffer = _tx_msg.f_buffer;
1340+
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
1341+
mcps_req.fport = _compliance_test.app_port;
1342+
mcps_req.nb_trials = _num_retry;
1343+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
1344+
1345+
tr_info("Transmit confirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
1346+
1347+
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
1348+
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
1349+
}
1350+
} else {
1351+
return LORAWAN_STATUS_SERVICE_UNKNOWN;
1352+
}
1353+
1354+
return mcps_request_handler(&mcps_req);
1355+
}
1356+
#endif
1357+

features/lorawan/LoRaWANStack.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ SPDX-License-Identifier: BSD-3-Clause
7777
#define LORAWAN_NETWORK_ID_MASK ( uint32_t )0xFE000000
7878

7979
class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
80+
private:
81+
/** End-device states.
82+
*
83+
*/
84+
typedef enum device_states {
85+
DEVICE_STATE_NOT_INITIALIZED,
86+
DEVICE_STATE_INIT,
87+
DEVICE_STATE_JOINING,
88+
DEVICE_STATE_ABP_CONNECTING,
89+
DEVICE_STATE_JOINED,
90+
DEVICE_STATE_SEND,
91+
DEVICE_STATE_IDLE,
92+
#if defined(LORAWAN_COMPLIANCE_TEST)
93+
DEVICE_STATE_COMPLIANCE_TEST,
94+
#endif
95+
DEVICE_STATE_SHUTDOWN
96+
} device_states_t;
97+
8098
public:
8199
static LoRaWANStack& get_lorawan_stack();
82100

@@ -424,6 +442,24 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
424442
*/
425443
uint16_t check_possible_tx_size(uint16_t size);
426444

445+
private:
446+
447+
LoRaWANTimeHandler _lora_time;
448+
LoRaMac _loramac;
449+
LoRaPHY_region _lora_phy;
450+
loramac_primitives_t LoRaMacPrimitives;
451+
452+
device_states_t _device_current_state;
453+
lorawan_app_callbacks_t _callbacks;
454+
radio_events_t *_mac_handlers;
455+
lorawan_session_t _lw_session;
456+
loramac_tx_message_t _tx_msg;
457+
loramac_rx_message_t _rx_msg;
458+
uint8_t _num_retry;
459+
uint8_t _app_port;
460+
bool _duty_cycle_on;
461+
events::EventQueue *_queue;
462+
427463
#if defined(LORAWAN_COMPLIANCE_TEST)
428464
/**
429465
* This function is used only for compliance testing
@@ -439,28 +475,10 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
439475
* Used only for compliance testing
440476
*/
441477
lorawan_status_t send_compliance_test_frame_to_mac();
442-
#endif
443478

444-
LoRaWANTimeHandler _lora_time;
445-
LoRaMac _loramac;
446-
LoRaPHY_region _lora_phy;
447-
loramac_primitives_t LoRaMacPrimitives;
448-
449-
#if defined(LORAWAN_COMPLIANCE_TEST)
450479
uint8_t compliance_test_buffer[MBED_CONF_LORA_TX_MAX_SIZE];
451480
compliance_test_t _compliance_test;
452481
#endif
453-
454-
device_states_t _device_current_state;
455-
lorawan_app_callbacks_t _callbacks;
456-
radio_events_t *_mac_handlers;
457-
lorawan_session_t _lw_session;
458-
loramac_tx_message_t _tx_msg;
459-
loramac_rx_message_t _rx_msg;
460-
uint8_t _app_port;
461-
uint8_t _num_retry;
462-
events::EventQueue *_queue;
463-
bool _duty_cycle_on;
464482
};
465483

466484
#endif /* LORAWANSTACK_H_ */

0 commit comments

Comments
 (0)