Skip to content

Commit 4728520

Browse files
author
Antti Kauppila
committed
LoRa: LoRaMacMcps refactored to remove dependency to LoRaMac.
- This is internal logic only and there are no functionality changes - Some compliance test stuff have been moved to end of files - Some internal data structures removed as useless after refactor
1 parent 2b6d662 commit 4728520

File tree

9 files changed

+371
-450
lines changed

9 files changed

+371
-450
lines changed

features/lorawan/LoRaWANInterface.h

Lines changed: 1 addition & 0 deletions
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.

features/lorawan/LoRaWANStack.cpp

Lines changed: 101 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -175,90 +175,6 @@ lorawan_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue)
175175
return lora_state_machine();
176176
}
177177

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-
prepare_special_tx_frame(_compliance_test.app_port);
228-
229-
if (!_compliance_test.is_tx_confirmed) {
230-
mcps_req.type = MCPS_UNCONFIRMED;
231-
mcps_req.req.unconfirmed.fport = _compliance_test.app_port;
232-
mcps_req.f_buffer = _tx_msg.f_buffer;
233-
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
234-
mcps_req.req.unconfirmed.data_rate = _lora_phy.get_default_tx_datarate();
235-
236-
tr_info("Transmit unconfirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
237-
238-
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
239-
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
240-
}
241-
} else if (_compliance_test.is_tx_confirmed) {
242-
mcps_req.type = MCPS_CONFIRMED;
243-
mcps_req.req.confirmed.fport = _compliance_test.app_port;
244-
mcps_req.f_buffer = _tx_msg.f_buffer;
245-
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
246-
mcps_req.req.confirmed.nb_trials = _num_retry;
247-
mcps_req.req.confirmed.data_rate = _lora_phy.get_default_tx_datarate();
248-
249-
tr_info("Transmit confirmed compliance test frame %d bytes.", mcps_req.f_buffer_size);
250-
251-
for (uint8_t i = 0; i < mcps_req.f_buffer_size; ++i) {
252-
tr_info("Byte %d, data is 0x%x", i+1, ((uint8_t*)mcps_req.f_buffer)[i]);
253-
}
254-
} else {
255-
return LORAWAN_STATUS_SERVICE_UNKNOWN;
256-
}
257-
258-
return mcps_request_handler(&mcps_req);
259-
}
260-
#endif
261-
262178
uint16_t LoRaWANStack::check_possible_tx_size(uint16_t size)
263179
{
264180
loramac_tx_info_t tx_info;
@@ -284,43 +200,45 @@ lorawan_status_t LoRaWANStack::send_frame_to_mac()
284200
mcps_req.type = _tx_msg.type;
285201

286202
if (MCPS_UNCONFIRMED == mcps_req.type) {
287-
mcps_req.req.unconfirmed.fport = _tx_msg.message_u.unconfirmed.fport;
288203
mcps_req.f_buffer = _tx_msg.f_buffer;
289-
290204
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
291205

206+
mcps_req.fport = _tx_msg.fport;
207+
mcps_req.nb_trials = 1;
292208
mib_get_params.type = MIB_CHANNELS_DATARATE;
293209
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
294210
tr_debug("Couldn't get MIB parameters: Using default data rate");
295-
mcps_req.req.unconfirmed.data_rate = _lora_phy.get_default_tx_datarate();
211+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
296212
} else {
297-
mcps_req.req.unconfirmed.data_rate = mib_get_params.param.channel_data_rate;
213+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
298214
}
299215

300216
} else if (mcps_req.type == MCPS_CONFIRMED) {
301-
mcps_req.req.confirmed.fport = _tx_msg.message_u.confirmed.fport;
302217
mcps_req.f_buffer = _tx_msg.f_buffer;
303218
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
304-
mcps_req.req.confirmed.nb_trials = _tx_msg.message_u.confirmed.nb_trials;
219+
mcps_req.fport = _tx_msg.fport;
220+
mcps_req.nb_trials = _tx_msg.nb_trials;
305221

306222
mib_get_params.type = MIB_CHANNELS_DATARATE;
307223
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
308224
tr_debug("Couldn't get MIB parameters: Using default data rate");
309-
mcps_req.req.confirmed.data_rate = _lora_phy.get_default_tx_datarate();
225+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
310226
} else {
311-
mcps_req.req.confirmed.data_rate = mib_get_params.param.channel_data_rate;
227+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
312228
}
313229

314230
} else if ( mcps_req.type == MCPS_PROPRIETARY) {
315231
mcps_req.f_buffer = _tx_msg.f_buffer;
316232
mcps_req.f_buffer_size = _tx_msg.f_buffer_size;
233+
mcps_req.fport = 0;
234+
mcps_req.nb_trials = 1;
317235

318236
mib_get_params.type = MIB_CHANNELS_DATARATE;
319237
if(mib_get_request(&mib_get_params) != LORAWAN_STATUS_OK) {
320238
tr_debug("Couldn't get MIB parameters: Using default data rate");
321-
mcps_req.req.proprietary.data_rate = _lora_phy.get_default_tx_datarate();
239+
mcps_req.data_rate = _lora_phy.get_default_tx_datarate();
322240
} else {
323-
mcps_req.req.proprietary.data_rate = mib_get_params.param.channel_data_rate;
241+
mcps_req.data_rate = mib_get_params.param.channel_data_rate;
324242
}
325243

326244
} else {
@@ -651,7 +569,7 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
651569
|| (flags & MSG_FLAG_MASK) == MSG_UNCONFIRMED_PROPRIETARY) {
652570

653571
_tx_msg.type = MCPS_UNCONFIRMED;
654-
_tx_msg.message_u.unconfirmed.fport = _app_port;
572+
_tx_msg.fport = _app_port;
655573
}
656574

657575
// Handles all confirmed messages, including proprietary and multicast
@@ -660,8 +578,8 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
660578
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_PROPRIETARY) {
661579

662580
_tx_msg.type = MCPS_CONFIRMED;
663-
_tx_msg.message_u.confirmed.fport = _app_port;
664-
_tx_msg.message_u.confirmed.nb_trials = _num_retry;
581+
_tx_msg.fport = _app_port;
582+
_tx_msg.nb_trials = _num_retry;
665583
}
666584

667585
tr_info("RTS = %u bytes, PEND = %u", _tx_msg.f_buffer_size, _tx_msg.pending_size);
@@ -1357,3 +1275,89 @@ lorawan_status_t LoRaWANStack::lora_state_machine()
13571275

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

features/lorawan/LoRaWANStack.h

Lines changed: 37 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,25 @@ 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 _app_port;
459+
uint8_t _num_retry;
460+
events::EventQueue *_queue;
461+
bool _duty_cycle_on;
462+
463+
427464
#if defined(LORAWAN_COMPLIANCE_TEST)
428465
/**
429466
* This function is used only for compliance testing
@@ -439,28 +476,10 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
439476
* Used only for compliance testing
440477
*/
441478
lorawan_status_t send_compliance_test_frame_to_mac();
442-
#endif
443479

444-
LoRaWANTimeHandler _lora_time;
445-
LoRaMac _loramac;
446-
LoRaPHY_region _lora_phy;
447-
loramac_primitives_t LoRaMacPrimitives;
448-
449-
#if defined(LORAWAN_COMPLIANCE_TEST)
450480
uint8_t compliance_test_buffer[MBED_CONF_LORA_TX_MAX_SIZE];
451481
compliance_test_t _compliance_test;
452482
#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;
464483
};
465484

466485
#endif /* LORAWANSTACK_H_ */

0 commit comments

Comments
 (0)