Skip to content

Commit dc98228

Browse files
author
Antti Kauppila
committed
LoRa: Struct cleanups
- Unneeded structs removed and replaced by variables in functions
1 parent 489eecf commit dc98228

File tree

5 files changed

+105
-227
lines changed

5 files changed

+105
-227
lines changed

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ void LoRaMac::handle_fhss_change_channel(uint8_t cur_channel)
182182
**************************************************************************/
183183
void LoRaMac::on_radio_tx_done( void )
184184
{
185-
set_band_txdone_params_t tx_done_params;
186185
lorawan_time_t cur_time = _lora_time.get_current_time( );
187186

188187
if (_device_class != CLASS_C) {
@@ -223,10 +222,7 @@ void LoRaMac::on_radio_tx_done( void )
223222

224223
_params.last_channel_idx = _params.channel;
225224

226-
tx_done_params.channel = _params.channel;
227-
tx_done_params.joined = _is_nwk_joined;
228-
tx_done_params.last_tx_done_time = cur_time;
229-
_lora_phy.set_last_tx_done(&tx_done_params);
225+
_lora_phy.set_last_tx_done(_params.channel, _is_nwk_joined, cur_time);
230226

231227
_params.timers.aggregated_last_tx_time = cur_time;
232228

@@ -257,7 +253,6 @@ void LoRaMac::on_radio_rx_done(uint8_t *payload, uint16_t size, int16_t rssi,
257253
{
258254
loramac_mhdr_t mac_hdr;
259255
loramac_frame_ctrl_t fctrl;
260-
cflist_params_t cflist;
261256

262257
uint8_t pkt_header_len = 0;
263258
uint32_t address = 0;
@@ -361,11 +356,8 @@ void LoRaMac::on_radio_rx_done(uint8_t *payload, uint16_t size, int16_t rssi,
361356
_params.sys_params.recv_delay1 *= 1000;
362357
_params.sys_params.recv_delay2 = _params.sys_params.recv_delay1 + 1000;
363358

364-
cflist.payload = &_params.payload[13];
365359
// Size of the regular payload is 12. Plus 1 byte MHDR and 4 bytes MIC
366-
cflist.size = size - 17;
367-
368-
_lora_phy.apply_cf_list(&cflist);
360+
_lora_phy.apply_cf_list(&_params.payload[13], size - 17);
369361

370362
_mlme_confirmation.status = LORAMAC_EVENT_INFO_STATUS_OK;
371363
_is_nwk_joined = true;
@@ -1182,17 +1174,12 @@ lorawan_status_t LoRaMac::schedule_tx(void)
11821174

11831175
void LoRaMac::calculate_backOff(uint8_t channel)
11841176
{
1185-
backoff_params_t backoff_params;
1177+
lorawan_time_t elapsed_time = _lora_time.get_elapsed_time(_params.timers.mac_init_time);
11861178

1187-
backoff_params.joined = _is_nwk_joined;
11881179
_params.is_dutycycle_on = MBED_CONF_LORA_DUTY_CYCLE_ON;
1189-
backoff_params.dc_enabled = _params.is_dutycycle_on;
1190-
backoff_params.channel = channel;
1191-
backoff_params.elapsed_time = _lora_time.get_elapsed_time(_params.timers.mac_init_time);
1192-
backoff_params.tx_toa = _params.timers.tx_toa;
1193-
backoff_params.last_tx_was_join_req = _params.is_last_tx_join_request;
11941180

1195-
_lora_phy.calculate_backoff(&backoff_params);
1181+
_lora_phy.calculate_backoff(_is_nwk_joined, _params.is_last_tx_join_request, _params.is_dutycycle_on,
1182+
channel, elapsed_time, _params.timers.tx_toa);
11961183

11971184
// Update aggregated time-off. This must be an assignment and no incremental
11981185
// update as we do only calculate the time-off based on the last transmission

features/lorawan/lorastack/mac/LoRaMacCommand.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,8 @@ lorawan_status_t LoRaMacCommand::process_mac_commands(uint8_t *payload, uint8_t
345345
break;
346346
}
347347
case SRV_MAC_NEW_CHANNEL_REQ: {
348-
new_channel_req_params_t newChannelReq;
349348
channel_params_t chParam;
350-
351-
newChannelReq.channel_id = payload[mac_index++];
352-
newChannelReq.new_channel = &chParam;
349+
int8_t channel_id = payload[mac_index++];
353350

354351
chParam.frequency = (uint32_t) payload[mac_index++];
355352
chParam.frequency |= (uint32_t) payload[mac_index++] << 8;
@@ -358,7 +355,7 @@ lorawan_status_t LoRaMacCommand::process_mac_commands(uint8_t *payload, uint8_t
358355
chParam.rx1_frequency = 0;
359356
chParam.dr_range.value = payload[mac_index++];
360357

361-
status = lora_phy.request_new_channel(&newChannelReq);
358+
status = lora_phy.request_new_channel(channel_id, &chParam);
362359

363360
ret_value = add_mac_command(MOTE_MAC_NEW_CHANNEL_ANS, status, 0);
364361
}
@@ -375,44 +372,47 @@ lorawan_status_t LoRaMacCommand::process_mac_commands(uint8_t *payload, uint8_t
375372
}
376373
break;
377374
case SRV_MAC_TX_PARAM_SETUP_REQ: {
378-
tx_param_setup_req_t txParamSetupReq;
379375
uint8_t eirpDwellTime = payload[mac_index++];
376+
uint8_t ul_dwell_time;
377+
uint8_t dl_dwell_time;
378+
uint8_t max_eirp;
380379

381-
txParamSetupReq.ul_dwell_time = 0;
382-
txParamSetupReq.dl_dwell_time = 0;
380+
ul_dwell_time = 0;
381+
dl_dwell_time = 0;
383382

384383
if ((eirpDwellTime & 0x20) == 0x20) {
385-
txParamSetupReq.dl_dwell_time = 1;
384+
dl_dwell_time = 1;
386385
}
387386
if ((eirpDwellTime & 0x10) == 0x10) {
388-
txParamSetupReq.ul_dwell_time = 1;
387+
ul_dwell_time = 1;
389388
}
390-
txParamSetupReq.max_eirp = eirpDwellTime & 0x0F;
389+
max_eirp = eirpDwellTime & 0x0F;
391390

392391
// Check the status for correctness
393-
if (lora_phy.accept_tx_param_setup_req(&txParamSetupReq)) {
392+
if (lora_phy.accept_tx_param_setup_req(ul_dwell_time, dl_dwell_time)) {
394393
// Accept command
395394
mac_sys_params.uplink_dwell_time =
396-
txParamSetupReq.ul_dwell_time;
395+
ul_dwell_time;
397396
mac_sys_params.downlink_dwell_time =
398-
txParamSetupReq.dl_dwell_time;
397+
dl_dwell_time;
399398
mac_sys_params.max_eirp =
400-
max_eirp_table[txParamSetupReq.max_eirp];
399+
max_eirp_table[max_eirp];
401400
// Add command response
402401
ret_value = add_mac_command(MOTE_MAC_TX_PARAM_SETUP_ANS, 0, 0);
403402
}
404403
}
405404
break;
406405
case SRV_MAC_DL_CHANNEL_REQ: {
407-
dl_channel_req_params_t dlChannelReq;
408406

409-
dlChannelReq.channel_id = payload[mac_index++];
410-
dlChannelReq.rx1_frequency = (uint32_t) payload[mac_index++];
411-
dlChannelReq.rx1_frequency |= (uint32_t) payload[mac_index++] << 8;
412-
dlChannelReq.rx1_frequency |= (uint32_t) payload[mac_index++] << 16;
413-
dlChannelReq.rx1_frequency *= 100;
407+
uint8_t channel_id = payload[mac_index++];
408+
uint32_t rx1_frequency;
409+
410+
rx1_frequency = (uint32_t) payload[mac_index++];
411+
rx1_frequency |= (uint32_t) payload[mac_index++] << 8;
412+
rx1_frequency |= (uint32_t) payload[mac_index++] << 16;
413+
rx1_frequency *= 100;
414414

415-
status = lora_phy.dl_channel_request(&dlChannelReq);
415+
status = lora_phy.dl_channel_request(channel_id, rx1_frequency);
416416

417417
ret_value = add_mac_command(MOTE_MAC_DL_CHANNEL_ANS, status, 0);
418418
}

features/lorawan/lorastack/phy/LoRaPHY.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,22 @@ void LoRaPHY::handle_send(uint8_t *buf, uint8_t size)
112112
_radio->unlock();
113113
}
114114

115-
uint8_t LoRaPHY::request_new_channel(new_channel_req_params_t* params)
115+
uint8_t LoRaPHY::request_new_channel(int8_t channel_id, channel_params_t* new_channel)
116116
{
117117
if (!phy_params.custom_channelplans_supported) {
118118
return 0;
119119
}
120120

121121
uint8_t status = 0x03;
122122

123-
if (params->new_channel->frequency == 0) {
123+
if (new_channel->frequency == 0) {
124124
// Remove
125-
if (remove_channel(params->channel_id) == false) {
125+
if (remove_channel(channel_id) == false) {
126126
status &= 0xFC;
127127
}
128128
} else {
129129

130-
switch (add_channel(params->new_channel, params->channel_id)) {
130+
switch (add_channel(new_channel, channel_id)) {
131131
case LORAWAN_STATUS_OK:
132132
{
133133
break;
@@ -175,7 +175,7 @@ bool LoRaPHY::verify_channel_DR(uint8_t nb_channels, uint16_t* channel_mask,
175175
if (mask_bit_test(channel_mask, i)) {
176176
// Check datarate validity for enabled channels
177177
if (val_in_range(dr, (channels[i].dr_range.fields.min & 0x0F),
178-
(channels[i].dr_range.fields.max & 0x0F))) {
178+
(channels[i].dr_range.fields.max & 0x0F))) {
179179
// At least 1 channel has been found we can return OK.
180180
return true;
181181
}
@@ -247,22 +247,18 @@ void LoRaPHY::copy_channel_mask(uint16_t* dest_mask, uint16_t* src_mask, uint8_t
247247
}
248248
}
249249

250-
void LoRaPHY::set_last_tx_done(set_band_txdone_params_t* last_tx_params)
250+
void LoRaPHY::set_last_tx_done(uint8_t channel, bool joined, lorawan_time_t last_tx_done_time)
251251
{
252-
if (!last_tx_params) {
253-
return;
254-
}
255-
256252
band_t *band_table = (band_t *) phy_params.bands.table;
257253
channel_params_t *channel_list = phy_params.channels.channel_list;
258254

259-
if (last_tx_params->joined == true) {
260-
band_table[channel_list[last_tx_params->channel].band].last_tx_time = last_tx_params->last_tx_done_time;
255+
if (joined == true) {
256+
band_table[channel_list[channel].band].last_tx_time = last_tx_done_time;
261257
return;
262258
}
263259

264-
band_table[channel_list[last_tx_params->channel].band].last_tx_time = last_tx_params->last_tx_done_time;
265-
band_table[channel_list[last_tx_params->channel].band].last_join_tx_time = last_tx_params->last_tx_done_time;
260+
band_table[channel_list[channel].band].last_tx_time = last_tx_done_time;
261+
band_table[channel_list[channel].band].last_join_tx_time = last_tx_done_time;
266262

267263
}
268264

@@ -692,7 +688,7 @@ bool LoRaPHY::verify_nb_join_trials(uint8_t nb_join_trials)
692688
return true;
693689
}
694690

695-
void LoRaPHY::apply_cf_list(cflist_params_t* cf_list)
691+
void LoRaPHY::apply_cf_list(const uint8_t* payload, uint8_t size)
696692
{
697693
// if the underlying PHY doesn't support CF-List, ignore the request
698694
if (!phy_params.cflist_supported) {
@@ -703,10 +699,10 @@ void LoRaPHY::apply_cf_list(cflist_params_t* cf_list)
703699

704700
// Setup default datarate range
705701
new_channel.dr_range.value = (phy_params.default_max_datarate << 4)
706-
| phy_params.default_datarate;
702+
| phy_params.default_datarate;
707703

708704
// Size of the optional CF list
709-
if (cf_list->size != 16) {
705+
if (size != 16) {
710706
return;
711707
}
712708

@@ -721,9 +717,9 @@ void LoRaPHY::apply_cf_list(cflist_params_t* cf_list)
721717
channel_id < phy_params.max_channel_cnt; i+=phy_params.default_channel_cnt, channel_id++) {
722718
if (channel_id < (phy_params.cflist_channel_cnt + phy_params.default_channel_cnt)) {
723719
// Channel frequency
724-
new_channel.frequency = (uint32_t) cf_list->payload[i];
725-
new_channel.frequency |= ((uint32_t) cf_list->payload[i + 1] << 8);
726-
new_channel.frequency |= ((uint32_t) cf_list->payload[i + 2] << 16);
720+
new_channel.frequency = (uint32_t) payload[i];
721+
new_channel.frequency |= ((uint32_t) payload[i + 1] << 8);
722+
new_channel.frequency |= ((uint32_t) payload[i + 2] << 16);
727723
new_channel.frequency *= 100;
728724

729725
// Initialize alternative frequency to 0
@@ -1057,11 +1053,11 @@ uint8_t LoRaPHY::accept_rx_param_setup_req(rx_param_setup_req_t* params)
10571053
return status;
10581054
}
10591055

1060-
bool LoRaPHY::accept_tx_param_setup_req(tx_param_setup_req_t *params)
1056+
bool LoRaPHY::accept_tx_param_setup_req(uint8_t ul_dwell_time, uint8_t dl_dwell_time)
10611057
{
10621058
if (phy_params.accept_tx_param_setup_req) {
1063-
phy_params.ul_dwell_time_setting = params->ul_dwell_time;
1064-
phy_params.dl_dwell_time_setting = params->dl_dwell_time;
1059+
phy_params.ul_dwell_time_setting = ul_dwell_time;
1060+
phy_params.dl_dwell_time_setting = dl_dwell_time;
10651061
}
10661062

10671063
return phy_params.accept_tx_param_setup_req;
@@ -1084,7 +1080,7 @@ bool LoRaPHY::verify_frequency(uint32_t freq)
10841080
return false;
10851081
}
10861082

1087-
uint8_t LoRaPHY::dl_channel_request(dl_channel_req_params_t* params)
1083+
uint8_t LoRaPHY::dl_channel_request(uint8_t channel_id, uint32_t rx1_frequency)
10881084
{
10891085
if (!phy_params.dl_channel_req_supported) {
10901086
return 0;
@@ -1093,18 +1089,18 @@ uint8_t LoRaPHY::dl_channel_request(dl_channel_req_params_t* params)
10931089
uint8_t status = 0x03;
10941090

10951091
// Verify if the frequency is supported
1096-
if (verify_frequency(params->rx1_frequency) == false) {
1092+
if (verify_frequency(rx1_frequency) == false) {
10971093
status &= 0xFE;
10981094
}
10991095

11001096
// Verify if an uplink frequency exists
1101-
if (phy_params.channels.channel_list[params->channel_id].frequency == 0) {
1097+
if (phy_params.channels.channel_list[channel_id].frequency == 0) {
11021098
status &= 0xFD;
11031099
}
11041100

11051101
// Apply Rx1 frequency, if the status is OK
11061102
if (status == 0x03) {
1107-
phy_params.channels.channel_list[params->channel_id].rx1_frequency = params->rx1_frequency;
1103+
phy_params.channels.channel_list[channel_id].rx1_frequency = rx1_frequency;
11081104
}
11091105

11101106
return status;
@@ -1158,23 +1154,24 @@ int8_t LoRaPHY::get_alternate_DR(uint8_t nb_trials)
11581154
return datarate;
11591155
}
11601156

1161-
void LoRaPHY::calculate_backoff(backoff_params_t* calc_backoff)
1157+
void LoRaPHY::calculate_backoff(bool joined, bool last_tx_was_join_req, bool dc_enabled, uint8_t channel,
1158+
lorawan_time_t elapsed_time, lorawan_time_t tx_toa)
11621159
{
11631160
band_t *band_table = (band_t *) phy_params.bands.table;
11641161
channel_params_t *channel_list = phy_params.channels.channel_list;
11651162

1166-
uint8_t band_idx = channel_list[calc_backoff->channel].band;
1163+
uint8_t band_idx = channel_list[channel].band;
11671164
uint16_t duty_cycle = band_table[band_idx].duty_cycle;
11681165
uint16_t join_duty_cycle = 0;
11691166

11701167
// Reset time-off to initial value.
11711168
band_table[band_idx].off_time = 0;
11721169

1173-
if (calc_backoff->joined == false) {
1170+
if (joined == false) {
11741171
// Get the join duty cycle
1175-
if (calc_backoff->elapsed_time < 3600000) {
1172+
if (elapsed_time < 3600000) {
11761173
join_duty_cycle = BACKOFF_DC_1_HOUR;
1177-
} else if (calc_backoff->elapsed_time < (3600000 + 36000000)) {
1174+
} else if (elapsed_time < (3600000 + 36000000)) {
11781175
join_duty_cycle = BACKOFF_DC_10_HOURS;
11791176
} else {
11801177
join_duty_cycle = BACKOFF_DC_24_HOURS;
@@ -1186,12 +1183,12 @@ void LoRaPHY::calculate_backoff(backoff_params_t* calc_backoff)
11861183

11871184
// No back-off if the last frame was not a join request and when the
11881185
// duty cycle is not enabled
1189-
if (calc_backoff->dc_enabled == false &&
1190-
calc_backoff->last_tx_was_join_req == false) {
1186+
if (dc_enabled == false &&
1187+
last_tx_was_join_req == false) {
11911188
band_table[band_idx].off_time = 0;
11921189
} else {
11931190
// Apply band time-off.
1194-
band_table[band_idx].off_time = calc_backoff->tx_toa * duty_cycle - calc_backoff->tx_toa;
1191+
band_table[band_idx].off_time = tx_toa * duty_cycle - tx_toa;
11951192
}
11961193
}
11971194

@@ -1350,7 +1347,7 @@ bool LoRaPHY::remove_channel(uint8_t channel_id)
13501347

13511348

13521349
// Remove the channel from the list of channels
1353-
const channel_params_t empty_channel = { 0, 0, { 0 }, 0 };
1350+
const channel_params_t empty_channel = { 0, 0, {0}, 0 };
13541351
phy_params.channels.channel_list[channel_id] = empty_channel;
13551352

13561353
return disable_channel(phy_params.channels.mask, channel_id,

0 commit comments

Comments
 (0)