From 7224fbae1c3ca7ffba54df36d13128c2dcf241cf Mon Sep 17 00:00:00 2001 From: Hasnain Virk Date: Fri, 9 Feb 2018 16:23:33 +0200 Subject: [PATCH] Style Changes in MAC layer Style changed according to Mbed-OS guidelines. --- features/lorawan/LoRaWANStack.cpp | 22 +- features/lorawan/lorastack/mac/LoRaMac.cpp | 1657 ++++++++--------- features/lorawan/lorastack/mac/LoRaMac.h | 543 +++--- .../lorawan/lorastack/mac/LoRaMacCommand.cpp | 452 +++-- .../lorawan/lorastack/mac/LoRaMacCommand.h | 149 +- .../lorawan/lorastack/mac/LoRaMacCrypto.cpp | 244 +-- .../lorawan/lorastack/mac/LoRaMacCrypto.h | 106 +- .../lorawan/lorastack/mac/LoRaMacMcps.cpp | 44 +- features/lorawan/lorastack/mac/LoRaMacMib.cpp | 34 +- .../lorawan/lorastack/mac/LoRaMacMlme.cpp | 58 +- features/lorawan/lorastack/mac/LoRaMacMlme.h | 4 +- .../lorawan/system/lorawan_data_structures.h | 2 +- 12 files changed, 1594 insertions(+), 1721 deletions(-) diff --git a/features/lorawan/LoRaWANStack.cpp b/features/lorawan/LoRaWANStack.cpp index 63013be3ab..9b176368cf 100644 --- a/features/lorawan/LoRaWANStack.cpp +++ b/features/lorawan/LoRaWANStack.cpp @@ -130,7 +130,7 @@ LoRaWANStack& LoRaWANStack::get_lorawan_stack() radio_events_t *LoRaWANStack::bind_radio_driver(LoRaRadio& radio) { // Store pointer to callback routines inside MAC layer (non-IRQ safe) - _mac_handlers = _loramac.GetPhyEventHandlers(); + _mac_handlers = _loramac.get_phy_event_handlers(); // passes the reference to radio driver down to PHY layer _lora_phy.set_radio_instance(radio); return _mac_handlers; @@ -154,7 +154,7 @@ lorawan_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue) #endif _lora_time.activate_timer_subsystem(queue); - _loramac.LoRaMacInitialization(&LoRaMacPrimitives, &_lora_phy, queue); + _loramac.initialize(&LoRaMacPrimitives, &_lora_phy, queue); loramac_mib_req_confirm_t mib_req; @@ -267,7 +267,7 @@ lorawan_status_t LoRaWANStack::send_compliance_test_frame_to_mac() uint16_t LoRaWANStack::check_possible_tx_size(uint16_t size) { loramac_tx_info_t tx_info; - if (_loramac.LoRaMacQueryTxPossible(size, &tx_info) == LORAWAN_STATUS_LENGTH_ERROR) { + if (_loramac.query_tx_possible(size, &tx_info) == LORAWAN_STATUS_LENGTH_ERROR) { // Cannot transmit this much. Return how much data can be sent // at the moment return tx_info.max_possible_payload_size; @@ -404,7 +404,7 @@ lorawan_status_t LoRaWANStack::add_channels(const lorawan_channelplan_t &channel return LORAWAN_STATUS_NOT_INITIALIZED; } - return _loramac.AddChannelPlan(channel_plan); + return _loramac.add_channel_plan(channel_plan); } lorawan_status_t LoRaWANStack::drop_channel_list() @@ -414,7 +414,7 @@ lorawan_status_t LoRaWANStack::drop_channel_list() return LORAWAN_STATUS_NOT_INITIALIZED; } - return _loramac.RemoveChannelPlan(); + return _loramac.remove_channel_plan(); } lorawan_status_t LoRaWANStack::remove_a_channel(uint8_t channel_id) @@ -425,7 +425,7 @@ lorawan_status_t LoRaWANStack::remove_a_channel(uint8_t channel_id) return LORAWAN_STATUS_NOT_INITIALIZED; } - return _loramac.RemoveSingleChannel(channel_id); + return _loramac.remove_single_channel(channel_id); } lorawan_status_t LoRaWANStack::get_enabled_channels(lorawan_channelplan_t& channel_plan) @@ -438,7 +438,7 @@ lorawan_status_t LoRaWANStack::get_enabled_channels(lorawan_channelplan_t& chann return LORAWAN_STATUS_BUSY; } - return _loramac.GetChannelPlan(channel_plan); + return _loramac.get_channel_plan(channel_plan); } lorawan_status_t LoRaWANStack::enable_adaptive_datarate(bool adr_enabled) @@ -771,7 +771,7 @@ lorawan_status_t LoRaWANStack::mlme_request_handler(loramac_mlme_req_t *mlme_req return LORAWAN_STATUS_PARAMETER_INVALID; } - return _loramac.LoRaMacMlmeRequest(mlme_request); + return _loramac.mlme_request(mlme_request); } /** MLME-Confirm event function @@ -843,7 +843,7 @@ lorawan_status_t LoRaWANStack::mcps_request_handler(loramac_mcps_req_t *mcps_req return LORAWAN_STATUS_PARAMETER_INVALID; } - return _loramac.LoRaMacMcpsRequest(mcps_request); + return _loramac.mcps_request(mcps_request); } /** MCPS-Confirm event function @@ -1154,7 +1154,7 @@ lorawan_status_t LoRaWANStack::mib_set_request(loramac_mib_req_confirm_t *mib_se if (NULL == mib_set_params) { return LORAWAN_STATUS_PARAMETER_INVALID; } - return _loramac.LoRaMacMibSetRequestConfirm(mib_set_params); + return _loramac.mib_set_request_confirm(mib_set_params); } lorawan_status_t LoRaWANStack::mib_get_request(loramac_mib_req_confirm_t *mib_get_params) @@ -1162,7 +1162,7 @@ lorawan_status_t LoRaWANStack::mib_get_request(loramac_mib_req_confirm_t *mib_ge if(NULL == mib_get_params) { return LORAWAN_STATUS_PARAMETER_INVALID; } - return _loramac.LoRaMacMibGetRequestConfirm(mib_get_params); + return _loramac.mib_get_request_confirm(mib_get_params); } lorawan_status_t LoRaWANStack::set_link_check_request() diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index fb55cb38f2..ab5c2b9358 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -36,7 +36,6 @@ SPDX-License-Identifier: BSD-3-Clause using namespace events; - /*! * Maximum length of the fOpts field */ @@ -116,7 +115,7 @@ LoRaMac::LoRaMac(LoRaWANTimeHandler &lora_time) _params.sys_params.adr_on = false; _params.sys_params.max_duty_cycle = 0; - LoRaMacPrimitives = NULL; + mac_primitives = NULL; ev_queue = NULL; } @@ -130,35 +129,35 @@ LoRaMac::~LoRaMac() **************************************************************************/ void LoRaMac::handle_tx_done(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRadioTxDone); + const int ret = ev_queue->call(this, &LoRaMac::on_radio_tx_done); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { - const int ret = ev_queue->call(this, &LoRaMac::OnRadioRxDone, payload, size, rssi, snr); + const int ret = ev_queue->call(this, &LoRaMac::on_radio_rx_done, payload, size, rssi, snr); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_rx_error(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRadioRxError); + const int ret = ev_queue->call(this, &LoRaMac::on_radio_rx_error); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_rx_timeout(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRadioRxTimeout); + const int ret = ev_queue->call(this, &LoRaMac::on_radio_rx_timeout); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_tx_timeout(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRadioTxTimeout); + const int ret = ev_queue->call(this, &LoRaMac::on_radio_tx_timeout); MBED_ASSERT(ret != 0); (void)ret; } @@ -181,35 +180,35 @@ void LoRaMac::handle_fhss_change_channel(uint8_t cur_channel) void LoRaMac::handle_mac_state_check_timer_event(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnMacStateCheckTimerEvent); + const int ret = ev_queue->call(this, &LoRaMac::on_mac_state_check_timer_event); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_delayed_tx_timer_event(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnTxDelayedTimerEvent); + const int ret = ev_queue->call(this, &LoRaMac::on_tx_delayed_timer_event); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_ack_timeout() { - const int ret = ev_queue->call(this, &LoRaMac::OnAckTimeoutTimerEvent); + const int ret = ev_queue->call(this, &LoRaMac::on_ack_timeout_timer_event); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_rx1_timer_event(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRxWindow1TimerEvent); + const int ret = ev_queue->call(this, &LoRaMac::on_rx_window1_timer_event); MBED_ASSERT(ret != 0); (void)ret; } void LoRaMac::handle_rx2_timer_event(void) { - const int ret = ev_queue->call(this, &LoRaMac::OnRxWindow2TimerEvent); + const int ret = ev_queue->call(this, &LoRaMac::on_rx_window2_timer_event); MBED_ASSERT(ret != 0); (void)ret; } @@ -217,121 +216,116 @@ void LoRaMac::handle_rx2_timer_event(void) /*************************************************************************** * Radio event callbacks - delegated to Radio driver * **************************************************************************/ -void LoRaMac::OnRadioTxDone( void ) +void LoRaMac::on_radio_tx_done( void ) { - get_phy_params_t getPhy; - phy_param_t phyParam; - set_band_txdone_params_t txDone; - lorawan_time_t curTime = _lora_time.get_current_time( ); + get_phy_params_t get_phy; + phy_param_t phy_param; + set_band_txdone_params_t tx_done_params; + lorawan_time_t cur_time = _lora_time.get_current_time( ); loramac_mlme_confirm_t mlme_confirm = mlme.get_confirmation(); - if( _params.dev_class != CLASS_C ) - { + if (_params.dev_class != CLASS_C) { lora_phy->put_radio_to_sleep(); - } - else - { - OpenContinuousRx2Window( ); + } else { + open_continuous_rx2_window(); } // Setup timers - if( _params.is_rx_window_enabled == true ) - { - _lora_time.start( _params.timers.rx_window1_timer, _params.rx_window1_delay ); - if( _params.dev_class != CLASS_C ) - { - _lora_time.start( _params.timers.rx_window2_timer, _params.rx_window2_delay ); + if(_params.is_rx_window_enabled == true) { + _lora_time.start(_params.timers.rx_window1_timer, _params.rx_window1_delay); + + if (_params.dev_class != CLASS_C) { + _lora_time.start(_params.timers.rx_window2_timer, _params.rx_window2_delay); } - if( ( _params.dev_class == CLASS_C ) || ( _params.is_node_ack_requested == true ) ) - { - getPhy.attribute = PHY_ACK_TIMEOUT; - phyParam = lora_phy->get_phy_params(&getPhy); - _lora_time.start( _params.timers.ack_timeout_timer, _params.rx_window2_delay + phyParam.value ); + + if ((_params.dev_class == CLASS_C ) || + (_params.is_node_ack_requested == true)) { + get_phy.attribute = PHY_ACK_TIMEOUT; + phy_param = lora_phy->get_phy_params(&get_phy); + _lora_time.start(_params.timers.ack_timeout_timer, + _params.rx_window2_delay + phy_param.value); } - } - else - { + } else { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_OK; mlme_confirm.status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT; - if( _params.flags.value == 0 ) - { + if (_params.flags.value == 0) { _params.flags.bits.mcps_req = 1; } + _params.flags.bits.mac_done = 1; } // Verify if the last uplink was a join request - if( ( _params.flags.bits.mlme_req == 1 ) && ( mlme_confirm.req_type == MLME_JOIN ) ) - { + if ((_params.flags.bits.mlme_req == 1) && + (mlme_confirm.req_type == MLME_JOIN)) { _params.is_last_tx_join_request = true; - } - else - { + } else { _params.is_last_tx_join_request = false; } // Store last Tx channel _params.last_channel_idx = _params.channel; - // Update last tx done time for the current channel - txDone.channel = _params.channel; - txDone.joined = _params.is_nwk_joined; - txDone.last_tx_done_time = curTime; - lora_phy->set_last_tx_done(&txDone); - // Update Aggregated last tx done time - _params.timers.aggregated_last_tx_time = curTime; - if( _params.is_node_ack_requested == false ) - { + // Update last tx done time for the current channel + tx_done_params.channel = _params.channel; + tx_done_params.joined = _params.is_nwk_joined; + tx_done_params.last_tx_done_time = cur_time; + lora_phy->set_last_tx_done(&tx_done_params); + + // Update Aggregated last tx done time + _params.timers.aggregated_last_tx_time = cur_time; + + if (_params.is_node_ack_requested == false) { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_OK; _params.ul_nb_rep_counter++; } } -void LoRaMac::PrepareRxDoneAbort( void ) +void LoRaMac::prepare_rx_done_abort(void) { _params.mac_state |= LORAMAC_RX_ABORT; - if( _params.is_node_ack_requested ) - { + if (_params.is_node_ack_requested) { handle_ack_timeout(); } _params.flags.bits.mcps_ind = 1; _params.flags.bits.mac_done = 1; - // Trig OnMacCheckTimerEvent call as soon as possible - _lora_time.start( _params.timers.mac_state_check_timer, 1 ); + // Trigger MAC state check event timer as soon as possible + _lora_time.start(_params.timers.mac_state_check_timer, 1); } -void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) +void LoRaMac::on_radio_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, + int8_t snr) { - loramac_mhdr_t macHdr; - loramac_frame_ctrl_t fCtrl; - cflist_params_t applyCFList; - get_phy_params_t getPhy; - phy_param_t phyParam; - bool skipIndication = false; + loramac_mhdr_t mac_hdr; + loramac_frame_ctrl_t fctrl; + cflist_params_t cflist; + get_phy_params_t get_phy; + phy_param_t phy_param; + bool skip_indication = false; - uint8_t pktHeaderLen = 0; + uint8_t pkt_header_len = 0; uint32_t address = 0; - uint8_t appPayloadStartIndex = 0; - uint8_t frameLen = 0; + uint8_t app_payload_start_index = 0; + uint8_t frame_len = 0; uint32_t mic = 0; - uint32_t micRx = 0; + uint32_t mic_rx = 0; - uint16_t sequenceCounter = 0; - uint16_t sequenceCounterPrev = 0; - uint16_t sequenceCounterDiff = 0; - uint32_t downLinkCounter = 0; + uint16_t sequence_counter = 0; + uint16_t sequence_counter_prev = 0; + uint16_t sequence_counter_diff = 0; + uint32_t downlink_counter = 0; - multicast_params_t *curMulticastParams = NULL; - uint8_t *nwkSKey = _params.keys.nwk_skey; - uint8_t *appSKey = _params.keys.app_skey; + multicast_params_t *cur_multicast_params = NULL; + uint8_t *nwk_skey = _params.keys.nwk_skey; + uint8_t *app_skey = _params.keys.app_skey; uint8_t multicast = 0; - bool isMicOk = false; + bool is_mic_ok = false; mcps.get_confirmation().ack_received = false; mcps.get_indication().rssi = rssi; @@ -351,365 +345,340 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8 _lora_time.stop( _params.timers.rx_window2_timer ); - macHdr.value = payload[pktHeaderLen++]; + mac_hdr.value = payload[pkt_header_len++]; + + switch (mac_hdr.bits.mtype) { - switch( macHdr.bits.mtype ) - { case FRAME_TYPE_JOIN_ACCEPT: - if( _params.is_nwk_joined == true ) - { + if (_params.is_nwk_joined == true) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; - PrepareRxDoneAbort( ); + prepare_rx_done_abort(); return; } - if (0 != LoRaMacJoinDecrypt( payload + 1, size - 1, - _params.keys.app_key, - _params.payload + 1 )) { + if (0 != decrypt_join_frame(payload + 1, size - 1, + _params.keys.app_key, + _params.payload + 1)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; return; } - _params.payload[0] = macHdr.value; + _params.payload[0] = mac_hdr.value; - if (0 != LoRaMacJoinComputeMic( _params.payload, size - LORAMAC_MFR_LEN, - _params.keys.app_key, &mic )) { + if (0 != compute_join_frame_mic(_params.payload, size - LORAMAC_MFR_LEN, + _params.keys.app_key, &mic)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; return; } - micRx |= ( uint32_t ) _params.payload[size - LORAMAC_MFR_LEN]; - micRx |= ( ( uint32_t ) _params.payload[size - LORAMAC_MFR_LEN + 1] << 8 ); - micRx |= ( ( uint32_t ) _params.payload[size - LORAMAC_MFR_LEN + 2] << 16 ); - micRx |= ( ( uint32_t ) _params.payload[size - LORAMAC_MFR_LEN + 3] << 24 ); + mic_rx |= (uint32_t) _params.payload[size - LORAMAC_MFR_LEN]; + mic_rx |= ((uint32_t) _params.payload[size - LORAMAC_MFR_LEN + 1] << 8); + mic_rx |= ((uint32_t) _params.payload[size - LORAMAC_MFR_LEN + 2] << 16); + mic_rx |= ((uint32_t) _params.payload[size - LORAMAC_MFR_LEN + 3] << 24); - if( micRx == mic ) - { - if (0 != LoRaMacJoinComputeSKeys( _params.keys.app_key, - _params.payload + 1, - _params.dev_nonce, - _params.keys.nwk_skey, - _params.keys.app_skey )) { + if (mic_rx == mic) { + + if (0 != compute_skeys_for_join_frame(_params.keys.app_key, + _params.payload + 1, + _params.dev_nonce, + _params.keys.nwk_skey, + _params.keys.app_skey)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; return; } - _params.net_id = ( uint32_t ) _params.payload[4]; - _params.net_id |= ( ( uint32_t ) _params.payload[5] << 8 ); - _params.net_id |= ( ( uint32_t ) _params.payload[6] << 16 ); + _params.net_id = (uint32_t) _params.payload[4]; + _params.net_id |= ((uint32_t) _params.payload[5] << 8); + _params.net_id |= ((uint32_t) _params.payload[6] << 16); - _params.dev_addr = ( uint32_t ) _params.payload[7]; - _params.dev_addr |= ( ( uint32_t ) _params.payload[8] << 8 ); - _params.dev_addr |= ( ( uint32_t ) _params.payload[9] << 16 ); - _params.dev_addr |= ( ( uint32_t ) _params.payload[10] << 24 ); + _params.dev_addr = (uint32_t) _params.payload[7]; + _params.dev_addr |= ((uint32_t) _params.payload[8] << 8); + _params.dev_addr |= ((uint32_t) _params.payload[9] << 16); + _params.dev_addr |= ((uint32_t) _params.payload[10] << 24); // DLSettings - _params.sys_params.rx1_dr_offset = ( _params.payload[11] >> 4 ) & 0x07; + _params.sys_params.rx1_dr_offset = (_params.payload[11] >> 4) & 0x07; _params.sys_params.rx2_channel.datarate = _params.payload[11] & 0x0F; // RxDelay - _params.sys_params.recv_delay1 = ( _params.payload[12] & 0x0F ); - if( _params.sys_params.recv_delay1 == 0 ) - { + _params.sys_params.recv_delay1 = (_params.payload[12] & 0x0F); + + if (_params.sys_params.recv_delay1 == 0) { _params.sys_params.recv_delay1 = 1; } + _params.sys_params.recv_delay1 *= 1000; _params.sys_params.recv_delay2 = _params.sys_params.recv_delay1 + 1000; // Apply CF list - applyCFList.payload = &_params.payload[13]; + cflist.payload = &_params.payload[13]; // Size of the regular payload is 12. Plus 1 byte MHDR and 4 bytes MIC - applyCFList.size = size - 17; + cflist.size = size - 17; - lora_phy->apply_cf_list(&applyCFList); + lora_phy->apply_cf_list(&cflist); mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_OK; _params.is_nwk_joined = true; - } - else - { + } else { mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL; } + break; + case FRAME_TYPE_DATA_CONFIRMED_DOWN: case FRAME_TYPE_DATA_UNCONFIRMED_DOWN: { // Check if the received payload size is valid - getPhy.datarate = mcps.get_indication().rx_datarate; - getPhy.attribute = PHY_MAX_PAYLOAD; + get_phy.datarate = mcps.get_indication().rx_datarate; + get_phy.attribute = PHY_MAX_PAYLOAD; // Get the maximum payload length - if( _params.is_repeater_supported == true ) - { - getPhy.attribute = PHY_MAX_PAYLOAD_REPEATER; + if (_params.is_repeater_supported == true) { + get_phy.attribute = PHY_MAX_PAYLOAD_REPEATER; } - phyParam = lora_phy->get_phy_params(&getPhy); - if( MAX( 0, ( int16_t )( ( int16_t )size - ( int16_t )LORA_MAC_FRMPAYLOAD_OVERHEAD ) ) > (int32_t)phyParam.value ) - { + + phy_param = lora_phy->get_phy_params(&get_phy); + + if (MAX(0, (int16_t) ((int16_t)size - (int16_t)LORA_MAC_FRMPAYLOAD_OVERHEAD )) > (int32_t)phy_param.value) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; - PrepareRxDoneAbort( ); + prepare_rx_done_abort(); return; } - address = payload[pktHeaderLen++]; - address |= ( (uint32_t)payload[pktHeaderLen++] << 8 ); - address |= ( (uint32_t)payload[pktHeaderLen++] << 16 ); - address |= ( (uint32_t)payload[pktHeaderLen++] << 24 ); + address = payload[pkt_header_len++]; + address |= ((uint32_t)payload[pkt_header_len++] << 8); + address |= ((uint32_t)payload[pkt_header_len++] << 16); + address |= ((uint32_t)payload[pkt_header_len++] << 24); - if( address != _params.dev_addr ) - { - curMulticastParams = _params.multicast_channels; - while( curMulticastParams != NULL ) - { - if( address == curMulticastParams->address ) - { + if (address != _params.dev_addr) { + + cur_multicast_params = _params.multicast_channels; + + while (cur_multicast_params != NULL) { + + if (address == cur_multicast_params->address) { multicast = 1; - nwkSKey = curMulticastParams->nwk_skey; - appSKey = curMulticastParams->app_skey; - downLinkCounter = curMulticastParams->dl_frame_counter; + nwk_skey = cur_multicast_params->nwk_skey; + app_skey = cur_multicast_params->app_skey; + downlink_counter = cur_multicast_params->dl_frame_counter; break; } - curMulticastParams = curMulticastParams->Next; + + cur_multicast_params = cur_multicast_params->next; } - if( multicast == 0 ) - { + + if (multicast == 0) { // We are not the destination of this frame. mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL; - PrepareRxDoneAbort( ); + prepare_rx_done_abort(); return; } - } - else - { + } else { multicast = 0; - nwkSKey = _params.keys.nwk_skey; - appSKey = _params.keys.app_skey; - downLinkCounter = _params.dl_frame_counter; + nwk_skey = _params.keys.nwk_skey; + app_skey = _params.keys.app_skey; + downlink_counter = _params.dl_frame_counter; } - fCtrl.value = payload[pktHeaderLen++]; + fctrl.value = payload[pkt_header_len++]; - sequenceCounter = ( uint16_t )payload[pktHeaderLen++]; - sequenceCounter |= ( uint16_t )payload[pktHeaderLen++] << 8; + sequence_counter = (uint16_t )payload[pkt_header_len++]; + sequence_counter |= (uint16_t)payload[pkt_header_len++] << 8; - appPayloadStartIndex = 8 + fCtrl.bits.fopts_len; + app_payload_start_index = 8 + fctrl.bits.fopts_len; - micRx |= ( uint32_t )payload[size - LORAMAC_MFR_LEN]; - micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 1] << 8 ); - micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 2] << 16 ); - micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 3] << 24 ); + mic_rx |= (uint32_t)payload[size - LORAMAC_MFR_LEN]; + mic_rx |= ((uint32_t)payload[size - LORAMAC_MFR_LEN + 1] << 8); + mic_rx |= ((uint32_t)payload[size - LORAMAC_MFR_LEN + 2] << 16); + mic_rx |= ((uint32_t)payload[size - LORAMAC_MFR_LEN + 3] << 24); - sequenceCounterPrev = ( uint16_t )downLinkCounter; - sequenceCounterDiff = ( sequenceCounter - sequenceCounterPrev ); + sequence_counter_prev = (uint16_t)downlink_counter; + sequence_counter_diff = (sequence_counter - sequence_counter_prev); - if( sequenceCounterDiff < ( 1 << 15 ) ) - { - downLinkCounter += sequenceCounterDiff; - LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounter, &mic ); - if( micRx == mic ) - { - isMicOk = true; + if (sequence_counter_diff < (1 << 15)) { + + downlink_counter += sequence_counter_diff; + compute_mic(payload, size - LORAMAC_MFR_LEN, nwk_skey, + address, DOWN_LINK, downlink_counter, &mic); + if (mic_rx == mic) { + is_mic_ok = true; } - } - else - { + + } else { // check for sequence roll-over - uint32_t downLinkCounterTmp = downLinkCounter + 0x10000 + ( int16_t )sequenceCounterDiff; - LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounterTmp, &mic ); - if( micRx == mic ) - { - isMicOk = true; - downLinkCounter = downLinkCounterTmp; + uint32_t downlink_counter_tmp = downlink_counter + 0x10000 + (int16_t)sequence_counter_diff; + compute_mic(payload, size - LORAMAC_MFR_LEN, nwk_skey, + address, DOWN_LINK, downlink_counter_tmp, &mic); + + if (mic_rx == mic ) { + is_mic_ok = true; + downlink_counter = downlink_counter_tmp; } } // Check for a the maximum allowed counter difference - getPhy.attribute = PHY_MAX_FCNT_GAP; - phyParam = lora_phy->get_phy_params( &getPhy ); - if( sequenceCounterDiff >= phyParam.value ) - { + get_phy.attribute = PHY_MAX_FCNT_GAP; + phy_param = lora_phy->get_phy_params(&get_phy); + + if (sequence_counter_diff >= phy_param.value) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS; - mcps.get_indication().dl_frame_counter = downLinkCounter; - PrepareRxDoneAbort( ); + mcps.get_indication().dl_frame_counter = downlink_counter; + prepare_rx_done_abort( ); return; } - if( isMicOk == true ) - { + if (is_mic_ok == true) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_OK; mcps.get_indication().multicast = multicast; - mcps.get_indication().fpending_status = fCtrl.bits.fpending; + mcps.get_indication().fpending_status = fctrl.bits.fpending; mcps.get_indication().buffer = NULL; mcps.get_indication().buffer_size = 0; - mcps.get_indication().dl_frame_counter = downLinkCounter; + mcps.get_indication().dl_frame_counter = downlink_counter; mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_OK; _params.adr_ack_counter = 0; - mac_commands.ClearRepeatBuffer(); + mac_commands.clear_repeat_buffer(); // Update 32 bits downlink counter - if( multicast == 1 ) - { + if (multicast == 1) { mcps.get_indication().type = MCPS_MULTICAST; - if( ( curMulticastParams->dl_frame_counter == downLinkCounter ) && - ( curMulticastParams->dl_frame_counter != 0 ) ) - { + if ((cur_multicast_params->dl_frame_counter == downlink_counter) && + (cur_multicast_params->dl_frame_counter != 0)) { + mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED; - mcps.get_indication().dl_frame_counter = downLinkCounter; - PrepareRxDoneAbort( ); + mcps.get_indication().dl_frame_counter = downlink_counter; + prepare_rx_done_abort(); + return; } - curMulticastParams->dl_frame_counter = downLinkCounter; - } - else - { - if( macHdr.bits.mtype == FRAME_TYPE_DATA_CONFIRMED_DOWN ) - { + + cur_multicast_params->dl_frame_counter = downlink_counter; + + } else { + + if (mac_hdr.bits.mtype == FRAME_TYPE_DATA_CONFIRMED_DOWN) { _params.is_srv_ack_requested = true; mcps.get_indication().type = MCPS_CONFIRMED; - if( ( _params.dl_frame_counter == downLinkCounter ) && - ( _params.dl_frame_counter != 0 ) ) - { + if ((_params.dl_frame_counter == downlink_counter ) && + (_params.dl_frame_counter != 0)) { // Duplicated confirmed downlink. Skip indication. // In this case, the MAC layer shall accept the MAC commands // which are included in the downlink retransmission. // It should not provide the same frame to the application // layer again. - skipIndication = true; + skip_indication = true; } - } - else - { + } else { _params.is_srv_ack_requested = false; mcps.get_indication().type = MCPS_UNCONFIRMED; - if( ( _params.dl_frame_counter == downLinkCounter ) && - ( _params.dl_frame_counter != 0 ) ) - { + if ((_params.dl_frame_counter == downlink_counter) && + (_params.dl_frame_counter != 0)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED; - mcps.get_indication().dl_frame_counter = downLinkCounter; - PrepareRxDoneAbort( ); + mcps.get_indication().dl_frame_counter = downlink_counter; + prepare_rx_done_abort(); return; } } - _params.dl_frame_counter = downLinkCounter; + _params.dl_frame_counter = downlink_counter; } // This must be done before parsing the payload and the MAC commands. // We need to reset the MacCommandsBufferIndex here, since we need // to take retransmissions and repetitions into account. Error cases // will be handled in function OnMacStateCheckTimerEvent. - if( mcps.get_confirmation().req_type == MCPS_CONFIRMED ) - { - if( fCtrl.bits.ack == 1 ) - {// Reset MacCommandsBufferIndex when we have received an ACK. - mac_commands.ClearCommandBuffer(); + if (mcps.get_confirmation().req_type == MCPS_CONFIRMED) { + if (fctrl.bits.ack == 1) { + // Reset MacCommandsBufferIndex when we have received an ACK. + mac_commands.clear_command_buffer(); } - } - else - {// Reset the variable if we have received any valid frame. - mac_commands.ClearCommandBuffer(); + } else { + // Reset the variable if we have received any valid frame. + mac_commands.clear_command_buffer(); } // Process payload and MAC commands - if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 ) - { - uint8_t port = payload[appPayloadStartIndex++]; - frameLen = ( size - 4 ) - appPayloadStartIndex; + if (((size - 4) - app_payload_start_index) > 0) { + uint8_t port = payload[app_payload_start_index++]; + frame_len = (size - 4) - app_payload_start_index; mcps.get_indication().port = port; - if( port == 0 ) - { + if (port == 0) { // Only allow frames which do not have fOpts - if( fCtrl.bits.fopts_len == 0 ) - { - if (0 != LoRaMacPayloadDecrypt( payload + appPayloadStartIndex, - frameLen, - nwkSKey, - address, - DOWN_LINK, - downLinkCounter, - _params.payload )) { + if (fctrl.bits.fopts_len == 0) { + if (0 != decrypt_payload(payload + app_payload_start_index, + frame_len, + nwk_skey, + address, + DOWN_LINK, + downlink_counter, + _params.payload)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; } // Decode frame payload MAC commands - if (mac_commands.ProcessMacCommands( _params.payload, 0, frameLen, snr, - mlme.get_confirmation(), - _params.sys_params, *lora_phy ) != LORAWAN_STATUS_OK ) { + if (mac_commands.process_mac_commands(_params.payload, 0, frame_len, snr, + mlme.get_confirmation(), + _params.sys_params, *lora_phy) != LORAWAN_STATUS_OK) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; } + } else { + skip_indication = true; } - else - { - skipIndication = true; - } - } - else - { - if( fCtrl.bits.fopts_len > 0 ) - { + } else { + if (fctrl.bits.fopts_len > 0) { // Decode Options field MAC commands. Omit the fPort. - if (mac_commands.ProcessMacCommands( payload, 8, appPayloadStartIndex - 1, snr, - mlme.get_confirmation(), - _params.sys_params, *lora_phy ) != LORAWAN_STATUS_OK ) { + if (mac_commands.process_mac_commands(payload, 8, app_payload_start_index - 1, snr, + mlme.get_confirmation(), + _params.sys_params, *lora_phy ) != LORAWAN_STATUS_OK) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; } } - if (0 != LoRaMacPayloadDecrypt( payload + appPayloadStartIndex, - frameLen, - appSKey, - address, - DOWN_LINK, - downLinkCounter, - _params.payload )) { + if (0 != decrypt_payload(payload + app_payload_start_index, + frame_len, + app_skey, + address, + DOWN_LINK, + downlink_counter, + _params.payload)) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; } - if( skipIndication == false ) - { + if (skip_indication == false) { mcps.get_indication().buffer = _params.payload; - mcps.get_indication().buffer_size = frameLen; + mcps.get_indication().buffer_size = frame_len; mcps.get_indication().is_data_recvd = true; } } - } - else - { - if( fCtrl.bits.fopts_len > 0 ) - { + } else { + if (fctrl.bits.fopts_len > 0) { // Decode Options field MAC commands - if (mac_commands.ProcessMacCommands( payload, 8, appPayloadStartIndex, snr, - mlme.get_confirmation(), - _params.sys_params, *lora_phy ) != LORAWAN_STATUS_OK ) { + if (mac_commands.process_mac_commands(payload, 8, app_payload_start_index, snr, + mlme.get_confirmation(), + _params.sys_params, *lora_phy) != LORAWAN_STATUS_OK) { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; } } } - if( skipIndication == false ) - { + if (skip_indication == false) { // Check if the frame is an acknowledgement - if( fCtrl.bits.ack == 1 ) - { + if (fctrl.bits.ack == 1) { mcps.get_confirmation().ack_received = true; mcps.get_indication().is_ack_recvd = true; // Stop the AckTimeout timer as no more retransmissions // are needed. - _lora_time.stop( _params.timers.ack_timeout_timer ); - } - else - { + _lora_time.stop(_params.timers.ack_timeout_timer); + } else { mcps.get_confirmation().ack_received = false; - if( _params.ack_timeout_retry_counter > _params.max_ack_timeout_retries ) - { + if (_params.ack_timeout_retry_counter > _params.max_ack_timeout_retries) { // Stop the AckTimeout timer as no more retransmissions // are needed. _lora_time.stop( _params.timers.ack_timeout_timer ); @@ -719,49 +688,45 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8 // Provide always an indication, skip the callback to the user application, // in case of a confirmed downlink retransmission. _params.flags.bits.mcps_ind = 1; - _params.flags.bits.mcps_ind_skip = skipIndication; - } - else - { + _params.flags.bits.mcps_ind_skip = skip_indication; + } else { mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL; - PrepareRxDoneAbort( ); + prepare_rx_done_abort( ); return; } } + break; + case FRAME_TYPE_PROPRIETARY: { - memcpy( _params.payload, &payload[pktHeaderLen], size ); + memcpy(_params.payload, &payload[pkt_header_len], size); mcps.get_indication().type = MCPS_PROPRIETARY; mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_OK; mcps.get_indication().buffer = _params.payload; - mcps.get_indication().buffer_size = size - pktHeaderLen; + mcps.get_indication().buffer_size = size - pkt_header_len; _params.flags.bits.mcps_ind = 1; break; } default: mcps.get_indication().status = LORAMAC_EVENT_INFO_STATUS_ERROR; - PrepareRxDoneAbort( ); + prepare_rx_done_abort(); break; } _params.flags.bits.mac_done = 1; - // Trig OnMacCheckTimerEvent call as soon as possible - _lora_time.start( _params.timers.mac_state_check_timer, 1 ); + _lora_time.start(_params.timers.mac_state_check_timer, 1); } -void LoRaMac::OnRadioTxTimeout( void ) +void LoRaMac::on_radio_tx_timeout( void ) { - if( _params.dev_class != CLASS_C ) - { + if (_params.dev_class != CLASS_C) { lora_phy->put_radio_to_sleep(); - } - else - { - OpenContinuousRx2Window( ); + } else { + open_continuous_rx2_window(); } mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; @@ -771,36 +736,29 @@ void LoRaMac::OnRadioTxTimeout( void ) _params.flags.bits.mac_done = 1; } -void LoRaMac::OnRadioRxError( void ) +void LoRaMac::on_radio_rx_error( void ) { - if( _params.dev_class != CLASS_C ) - { + if (_params.dev_class != CLASS_C) { lora_phy->put_radio_to_sleep(); - } - else - { - OpenContinuousRx2Window( ); + } else { + open_continuous_rx2_window(); } - if( _params.rx_slot == RX_SLOT_WIN_1 ) - { - if( _params.is_node_ack_requested == true ) - { + if (_params.rx_slot == RX_SLOT_WIN_1) { + if (_params.is_node_ack_requested == true) { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX1_ERROR; } mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX1_ERROR; - if( _lora_time.get_elapsed_time( _params.timers.aggregated_last_tx_time ) >= _params.rx_window2_delay ) - { - _lora_time.stop( _params.timers.rx_window2_timer ); + if (_lora_time.get_elapsed_time(_params.timers.aggregated_last_tx_time) >= _params.rx_window2_delay) { + _lora_time.stop(_params.timers.rx_window2_timer); _params.flags.bits.mac_done = 1; } - } - else - { - if( _params.is_node_ack_requested == true ) - { + + } else { + + if (_params.is_node_ack_requested == true) { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX2_ERROR; } @@ -810,42 +768,34 @@ void LoRaMac::OnRadioRxError( void ) } } -void LoRaMac::OnRadioRxTimeout( void ) +void LoRaMac::on_radio_rx_timeout(void) { - if( _params.dev_class != CLASS_C ) - { + if (_params.dev_class != CLASS_C) { lora_phy->put_radio_to_sleep(); - } - else - { - OpenContinuousRx2Window( ); + } else { + open_continuous_rx2_window(); } - if( _params.rx_slot == RX_SLOT_WIN_1 ) - { - if( _params.is_node_ack_requested == true ) - { + if (_params.rx_slot == RX_SLOT_WIN_1) { + if (_params.is_node_ack_requested == true) { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX1_TIMEOUT; } mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX1_TIMEOUT; - if( _lora_time.get_elapsed_time( _params.timers.aggregated_last_tx_time ) >= _params.rx_window2_delay ) - { - _lora_time.stop( _params.timers.rx_window2_timer ); + if (_lora_time.get_elapsed_time(_params.timers.aggregated_last_tx_time ) >= _params.rx_window2_delay) { + _lora_time.stop(_params.timers.rx_window2_timer); _params.flags.bits.mac_done = 1; } - } - else - { - if( _params.is_node_ack_requested == true ) - { + + } else { + + if (_params.is_node_ack_requested == true) { mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT; } mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT; - if( _params.dev_class != CLASS_C ) - { + if (_params.dev_class != CLASS_C) { _params.flags.bits.mac_done = 1; } } @@ -854,87 +804,74 @@ void LoRaMac::OnRadioRxTimeout( void ) /*************************************************************************** * Timer event callbacks - deliberated locally * **************************************************************************/ -void LoRaMac::OnMacStateCheckTimerEvent( void ) +void LoRaMac::on_mac_state_check_timer_event(void) { - get_phy_params_t getPhy; - phy_param_t phyParam; - bool txTimeout = false; + get_phy_params_t get_phy; + phy_param_t phy_param; + bool tx_timeout = false; - _lora_time.stop( _params.timers.mac_state_check_timer ); + _lora_time.stop(_params.timers.mac_state_check_timer); - if( _params.flags.bits.mac_done == 1 ) - { - if( ( _params.mac_state & LORAMAC_RX_ABORT ) == LORAMAC_RX_ABORT ) - { + if (_params.flags.bits.mac_done == 1) { + + if ((_params.mac_state & LORAMAC_RX_ABORT) == LORAMAC_RX_ABORT) { _params.mac_state &= ~LORAMAC_RX_ABORT; _params.mac_state &= ~LORAMAC_TX_RUNNING; } - if( ( _params.flags.bits.mlme_req == 1 ) || ( ( _params.flags.bits.mcps_req == 1 ) ) ) - { - if( ( mcps.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) || - ( mlme.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) ) - { + if ((_params.flags.bits.mlme_req == 1) || (_params.flags.bits.mcps_req == 1)) { + + if ((mcps.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT) || + ( mlme.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT)) { // Stop transmit cycle due to tx timeout. _params.mac_state &= ~LORAMAC_TX_RUNNING; - mac_commands.ClearCommandBuffer(); + mac_commands.clear_command_buffer(); mcps.get_confirmation().nb_retries = _params.ack_timeout_retry_counter; mcps.get_confirmation().ack_received = false; mcps.get_confirmation().tx_toa = 0; - txTimeout = true; + tx_timeout = true; } } - if( ( _params.is_node_ack_requested == false ) && ( txTimeout == false ) ) - { - if( ( _params.flags.bits.mlme_req == 1 ) || ( ( _params.flags.bits.mcps_req == 1 ) ) ) - { - if( ( _params.flags.bits.mlme_req == 1 ) && ( mlme.get_confirmation().req_type == MLME_JOIN ) ) - {// Procedure for the join request + if ((_params.is_node_ack_requested == false) && (tx_timeout == false)) { + if ((_params.flags.bits.mlme_req == 1) || ((_params.flags.bits.mcps_req == 1))) { + if ((_params.flags.bits.mlme_req == 1) && (mlme.get_confirmation().req_type == MLME_JOIN)) { + // Procedure for the join request mlme.get_confirmation().nb_retries = _params.join_request_trial_counter; - if( mlme.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_OK ) - {// Node joined successfully + if (mlme.get_confirmation().status == LORAMAC_EVENT_INFO_STATUS_OK) { + // Node joined successfully _params.ul_frame_counter = 0; _params.ul_nb_rep_counter = 0; _params.mac_state &= ~LORAMAC_TX_RUNNING; - } - else - { - if( _params.join_request_trial_counter >= _params.max_join_request_trials ) - { + } else { + if (_params.join_request_trial_counter >= _params.max_join_request_trials) { _params.mac_state &= ~LORAMAC_TX_RUNNING; - } - else - { + } else { _params.flags.bits.mac_done = 0; // Sends the same frame again handle_delayed_tx_timer_event(); } } - } - else - {// Procedure for all other frames - if( ( _params.ul_nb_rep_counter >= _params.sys_params.retry_num ) || ( _params.flags.bits.mcps_ind == 1 ) ) - { - if( _params.flags.bits.mcps_ind == 0 ) - { // Maximum repetitions without downlink. Reset MacCommandsBufferIndex. Increase ADR Ack counter. + } else { + // Procedure for all other frames + if ((_params.ul_nb_rep_counter >= _params.sys_params.retry_num) || + (_params.flags.bits.mcps_ind == 1)) { + if (_params.flags.bits.mcps_ind == 0) { + // Maximum repetitions without downlink. Reset MacCommandsBufferIndex. Increase ADR Ack counter. // Only process the case when the MAC did not receive a downlink. - mac_commands.ClearCommandBuffer(); + mac_commands.clear_command_buffer(); _params.adr_ack_counter++; } _params.ul_nb_rep_counter = 0; - if( _params.is_ul_frame_counter_fixed == false ) - { + if (_params.is_ul_frame_counter_fixed == false) { _params.ul_frame_counter++; } _params.mac_state &= ~LORAMAC_TX_RUNNING; - } - else - { + } else { _params.flags.bits.mac_done = 0; // Sends the same frame again handle_delayed_tx_timer_event(); @@ -943,15 +880,13 @@ void LoRaMac::OnMacStateCheckTimerEvent( void ) } } - if( _params.flags.bits.mcps_ind == 1 ) - {// Procedure if we received a frame - if( ( mcps.get_confirmation().ack_received == true ) || - ( _params.ack_timeout_retry_counter > _params.max_ack_timeout_retries ) ) - { + if (_params.flags.bits.mcps_ind == 1) { + // Procedure if we received a frame + if ((mcps.get_confirmation().ack_received == true) || + (_params.ack_timeout_retry_counter > _params.max_ack_timeout_retries)) { _params.is_ack_retry_timeout_expired = false; _params.is_node_ack_requested = false; - if( _params.is_ul_frame_counter_fixed == false ) - { + if (_params.is_ul_frame_counter_fixed == false) { _params.ul_frame_counter++; } mcps.get_confirmation().nb_retries = _params.ack_timeout_retry_counter; @@ -960,156 +895,150 @@ void LoRaMac::OnMacStateCheckTimerEvent( void ) } } - if( ( _params.is_ack_retry_timeout_expired == true ) && ( ( _params.mac_state & LORAMAC_TX_DELAYED ) == 0 ) ) - {// Retransmissions procedure for confirmed uplinks + if ((_params.is_ack_retry_timeout_expired == true) && + ((_params.mac_state & LORAMAC_TX_DELAYED) == 0)) { + + // Retransmissions procedure for confirmed uplinks _params.is_ack_retry_timeout_expired = false; - if( ( _params.ack_timeout_retry_counter < _params.max_ack_timeout_retries ) && - ( _params.ack_timeout_retry_counter <= MAX_ACK_RETRIES ) ) - { + if ((_params.ack_timeout_retry_counter < _params.max_ack_timeout_retries) && + (_params.ack_timeout_retry_counter <= MAX_ACK_RETRIES)) { + _params.ack_timeout_retry_counter++; - if( ( _params.ack_timeout_retry_counter % 2 ) == 1 ) - { - getPhy.attribute = PHY_NEXT_LOWER_TX_DR; - getPhy.datarate = _params.sys_params.channel_data_rate; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.channel_data_rate = phyParam.value; + if ((_params.ack_timeout_retry_counter % 2) == 1) { + get_phy.attribute = PHY_NEXT_LOWER_TX_DR; + get_phy.datarate = _params.sys_params.channel_data_rate; + phy_param = lora_phy->get_phy_params( &get_phy ); + _params.sys_params.channel_data_rate = phy_param.value; } + // Try to send the frame again - if( ScheduleTx( ) == LORAWAN_STATUS_OK ) - { + if (schedule_tx() == LORAWAN_STATUS_OK) { _params.flags.bits.mac_done = 0; - } - else - { + } else { // The DR is not applicable for the payload size mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR; - mac_commands.ClearCommandBuffer(); + mac_commands.clear_command_buffer(); _params.mac_state &= ~LORAMAC_TX_RUNNING; _params.is_node_ack_requested = false; mcps.get_confirmation().ack_received = false; mcps.get_confirmation().nb_retries = _params.ack_timeout_retry_counter; mcps.get_confirmation().data_rate = _params.sys_params.channel_data_rate; - if( _params.is_ul_frame_counter_fixed == false ) - { + + if (_params.is_ul_frame_counter_fixed == false) { _params.ul_frame_counter++; } } - } - else - { + } else { lora_phy->restore_default_channels(); _params.mac_state &= ~LORAMAC_TX_RUNNING; - mac_commands.ClearCommandBuffer(); + mac_commands.clear_command_buffer(); _params.is_node_ack_requested = false; mcps.get_confirmation().ack_received = false; mcps.get_confirmation().nb_retries = _params.ack_timeout_retry_counter; - if( _params.is_ul_frame_counter_fixed == false ) - { + + if (_params.is_ul_frame_counter_fixed == false) { _params.ul_frame_counter++; } } } } + // Handle reception for Class B and Class C - if( ( _params.mac_state & LORAMAC_RX ) == LORAMAC_RX ) - { + if ((_params.mac_state & LORAMAC_RX) == LORAMAC_RX) { _params.mac_state &= ~LORAMAC_RX; } - if( _params.mac_state == LORAMAC_IDLE ) - { - if( _params.flags.bits.mcps_req == 1 ) - { + + if (_params.mac_state == LORAMAC_IDLE) { + if (_params.flags.bits.mcps_req == 1) { _params.flags.bits.mcps_req = 0; - LoRaMacPrimitives->mcps_confirm( &mcps.get_confirmation() ); + mac_primitives->mcps_confirm(&mcps.get_confirmation()); } - if( _params.flags.bits.mlme_req == 1 ) - { + if (_params.flags.bits.mlme_req == 1) { _params.flags.bits.mlme_req = 0; - LoRaMacPrimitives->mlme_confirm(&mlme.get_confirmation()); + mac_primitives->mlme_confirm(&mlme.get_confirmation()); } // Verify if sticky MAC commands are pending or not - if( mac_commands.IsStickyMacCommandPending( ) == true ) - {// Setup MLME indication - SetMlmeScheduleUplinkIndication( ); + if (mac_commands.is_sticky_mac_command_pending() == true) { + // Setup MLME indication + set_mlme_schedule_ul_indication(); } // Procedure done. Reset variables. _params.flags.bits.mac_done = 0; - } - else - { + } else { // Operation not finished restart timer - _lora_time.start( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT ); + _lora_time.start(_params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT); } // Handle MCPS indication - if( _params.flags.bits.mcps_ind == 1 ) - { + if (_params.flags.bits.mcps_ind == 1) { _params.flags.bits.mcps_ind = 0; - if( _params.dev_class== CLASS_C ) - {// Activate RX2 window for Class C - OpenContinuousRx2Window( ); + + if (_params.dev_class== CLASS_C) { + // Activate RX2 window for Class C + open_continuous_rx2_window(); } - if( _params.flags.bits.mcps_ind_skip == 0 ) - { - LoRaMacPrimitives->mcps_indication( &mcps.get_indication() ); + + if (_params.flags.bits.mcps_ind_skip == 0) { + mac_primitives->mcps_indication(&mcps.get_indication()); } + _params.flags.bits.mcps_ind_skip = 0; } // Handle MLME indication - if( _params.flags.bits.mlme_ind == 1 ) - { + if (_params.flags.bits.mlme_ind == 1) { _params.flags.bits.mlme_ind = 0; - LoRaMacPrimitives->mlme_indication(&mlme.get_indication()); + mac_primitives->mlme_indication(&mlme.get_indication()); } } -void LoRaMac::OnTxDelayedTimerEvent( void ) +void LoRaMac::on_tx_delayed_timer_event(void) { - loramac_mhdr_t macHdr; - loramac_frame_ctrl_t fCtrl; + loramac_mhdr_t mac_hdr; + loramac_frame_ctrl_t fctrl; lorawan_status_t status = LORAWAN_STATUS_OK; - _lora_time.stop( _params.timers.tx_delayed_timer ); + _lora_time.stop(_params.timers.tx_delayed_timer); _params.mac_state &= ~LORAMAC_TX_DELAYED; - if( ( _params.flags.bits.mlme_req == 1 ) && ( mlme.get_confirmation().req_type == MLME_JOIN ) ) - { - ResetMacParameters( ); + if ((_params.flags.bits.mlme_req == 1 ) && + (mlme.get_confirmation().req_type == MLME_JOIN)) { + + reset_mac_parameters(); _params.sys_params.channel_data_rate = lora_phy->get_alternate_DR(_params.join_request_trial_counter + 1); - macHdr.value = 0; - macHdr.bits.mtype = FRAME_TYPE_JOIN_REQ; + mac_hdr.value = 0; + mac_hdr.bits.mtype = FRAME_TYPE_JOIN_REQ; - fCtrl.value = 0; - fCtrl.bits.adr = _params.sys_params.adr_on; + fctrl.value = 0; + fctrl.bits.adr = _params.sys_params.adr_on; /* In case of join request retransmissions, the stack must prepare * the frame again, because the network server keeps track of the random * LoRaMacDevNonce values to prevent reply attacks. */ - status = PrepareFrame( &macHdr, &fCtrl, 0, NULL, 0 ); + status = prepare_frame(&mac_hdr, &fctrl, 0, NULL, 0); } if (status == LORAWAN_STATUS_OK) { - ScheduleTx( ); + schedule_tx(); } else { tr_error("Delayed TX: PrepareFrame returned error %d", status); } } -void LoRaMac::OnRxWindow1TimerEvent( void ) +void LoRaMac::on_rx_window1_timer_event(void) { - _lora_time.stop( _params.timers.rx_window1_timer ); - _params.rx_slot= RX_SLOT_WIN_1; + _lora_time.stop(_params.timers.rx_window1_timer); + _params.rx_slot = RX_SLOT_WIN_1; _params.rx_window1_config.channel = _params.channel; _params.rx_window1_config.dr_offset = _params.sys_params.rx1_dr_offset; @@ -1118,18 +1047,20 @@ void LoRaMac::OnRxWindow1TimerEvent( void ) _params.rx_window1_config.is_rx_continuous = false; _params.rx_window1_config.rx_slot = _params.rx_slot; - if( _params.dev_class== CLASS_C ) - { + if (_params.dev_class == CLASS_C) { lora_phy->put_radio_to_standby(); } - lora_phy->rx_config(&_params.rx_window1_config, ( int8_t* )&mcps.get_indication().rx_datarate); - RxWindowSetup( _params.rx_window1_config.is_rx_continuous, _params.sys_params.max_rx_win_time ); + lora_phy->rx_config(&_params.rx_window1_config, + (int8_t*) &mcps.get_indication().rx_datarate); + + rx_window_setup(_params.rx_window1_config.is_rx_continuous, + _params.sys_params.max_rx_win_time); } -void LoRaMac::OnRxWindow2TimerEvent( void ) +void LoRaMac::on_rx_window2_timer_event(void) { - _lora_time.stop( _params.timers.rx_window2_timer ); + _lora_time.stop(_params.timers.rx_window2_timer); _params.rx_window2_config.channel = _params.channel; _params.rx_window2_config.frequency = _params.sys_params.rx2_channel.frequency; @@ -1137,74 +1068,72 @@ void LoRaMac::OnRxWindow2TimerEvent( void ) _params.rx_window2_config.is_repeater_supported = _params.is_repeater_supported; _params.rx_window2_config.rx_slot = RX_SLOT_WIN_2; - if( _params.dev_class!= CLASS_C ) - { + if (_params.dev_class != CLASS_C) { _params.rx_window2_config.is_rx_continuous = false; - } - else - { + } else { // Setup continuous listening for class c _params.rx_window2_config.is_rx_continuous = true; } - if(lora_phy->rx_config(&_params.rx_window2_config, ( int8_t* )&mcps.get_indication().rx_datarate) == true ) - { - RxWindowSetup( _params.rx_window2_config.is_rx_continuous, _params.sys_params.max_rx_win_time ); - _params.rx_slot= RX_SLOT_WIN_2; + if (lora_phy->rx_config(&_params.rx_window2_config, + (int8_t*) &mcps.get_indication().rx_datarate) == true) { + + rx_window_setup(_params.rx_window2_config.is_rx_continuous, + _params.sys_params.max_rx_win_time); + + _params.rx_slot = RX_SLOT_WIN_2; } } -void LoRaMac::OnAckTimeoutTimerEvent( void ) +void LoRaMac::on_ack_timeout_timer_event(void) { - _lora_time.stop( _params.timers.ack_timeout_timer ); + _lora_time.stop(_params.timers.ack_timeout_timer); - if( _params.is_node_ack_requested == true ) - { + if (_params.is_node_ack_requested == true) { _params.is_ack_retry_timeout_expired = true; _params.mac_state &= ~LORAMAC_ACK_REQ; } - if( _params.dev_class== CLASS_C ) - { + if (_params.dev_class == CLASS_C) { _params.flags.bits.mac_done = 1; } } -void LoRaMac::RxWindowSetup( bool rxContinuous, uint32_t maxRxWindow ) +void LoRaMac::rx_window_setup(bool rx_continuous, uint32_t max_rx_window_time) { - lora_phy->setup_rx_window(rxContinuous, maxRxWindow); + lora_phy->setup_rx_window(rx_continuous, max_rx_window_time); } -bool LoRaMac::ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen ) +bool LoRaMac::validate_payload_length(uint8_t length, int8_t datarate, + uint8_t fopts_len) { - get_phy_params_t getPhy; - phy_param_t phyParam; - uint16_t maxN = 0; + get_phy_params_t get_phy; + phy_param_t phy_param; + uint16_t max_value = 0; uint16_t payloadSize = 0; // Setup PHY request - getPhy.datarate = datarate; - getPhy.attribute = PHY_MAX_PAYLOAD; + get_phy.datarate = datarate; + get_phy.attribute = PHY_MAX_PAYLOAD; // Get the maximum payload length - if( _params.is_repeater_supported == true ) - { - getPhy.attribute = PHY_MAX_PAYLOAD_REPEATER; + if (_params.is_repeater_supported == true) { + get_phy.attribute = PHY_MAX_PAYLOAD_REPEATER; } - phyParam = lora_phy->get_phy_params(&getPhy); - maxN = phyParam.value; + phy_param = lora_phy->get_phy_params(&get_phy); + max_value = phy_param.value; // Calculate the resulting payload size - payloadSize = ( lenN + fOptsLen ); + payloadSize = (length + fopts_len); // Validation of the application payload size - if( ( payloadSize <= maxN ) && ( payloadSize <= LORAMAC_PHY_MAXPAYLOAD ) ) - { + if ((payloadSize <= max_value) && + (payloadSize <= LORAMAC_PHY_MAXPAYLOAD)) { return true; } return false; } -void LoRaMac::SetMlmeScheduleUplinkIndication( void ) +void LoRaMac::set_mlme_schedule_ul_indication(void) { mlme.get_indication().indication_type = MLME_SCHEDULE_UPLINK; _params.flags.bits.mlme_ind = 1; @@ -1212,23 +1141,24 @@ void LoRaMac::SetMlmeScheduleUplinkIndication( void ) // This is not actual transmission. It just schedules a message in response // to MCPS request -lorawan_status_t LoRaMac::Send( loramac_mhdr_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize ) +lorawan_status_t LoRaMac::send(loramac_mhdr_t *machdr, uint8_t fport, + void *fbuffer, uint16_t fbuffer_size) { - loramac_frame_ctrl_t fCtrl; + loramac_frame_ctrl_t fctrl; - fCtrl.value = 0; - fCtrl.bits.fopts_len = 0; - fCtrl.bits.fpending = 0; - fCtrl.bits.ack = false; - fCtrl.bits.adr_ack_req = false; - fCtrl.bits.adr = _params.sys_params.adr_on; + fctrl.value = 0; + fctrl.bits.fopts_len = 0; + fctrl.bits.fpending = 0; + fctrl.bits.ack = false; + fctrl.bits.adr_ack_req = false; + fctrl.bits.adr = _params.sys_params.adr_on; // Prepare the frame - lorawan_status_t status = PrepareFrame( macHdr, &fCtrl, fPort, fBuffer, fBufferSize ); + lorawan_status_t status = prepare_frame(machdr, &fctrl, fport, fbuffer, + fbuffer_size); // Validate status - if( status != LORAWAN_STATUS_OK ) - { + if (status != LORAWAN_STATUS_OK) { return status; } @@ -1237,12 +1167,12 @@ lorawan_status_t LoRaMac::Send( loramac_mhdr_t *macHdr, uint8_t fPort, void *fBu mcps.get_confirmation().ack_received = false; mcps.get_confirmation().ul_frame_counter = _params.ul_frame_counter; - status = ScheduleTx( ); + status = schedule_tx(); return status; } -lorawan_status_t LoRaMac::ScheduleTx( void ) +lorawan_status_t LoRaMac::schedule_tx(void) { lorawan_time_t dutyCycleTimeOff = 0; channel_selection_params_t nextChan; @@ -1250,17 +1180,16 @@ lorawan_status_t LoRaMac::ScheduleTx( void ) phy_param_t phyParam; // Check if the device is off - if( _params.sys_params.max_duty_cycle == 255 ) - { + if (_params.sys_params.max_duty_cycle == 255) { return LORAWAN_STATUS_DEVICE_OFF; } - if( _params.sys_params.max_duty_cycle == 0 ) - { + + if (_params.sys_params.max_duty_cycle == 0) { _params.timers.aggregated_timeoff = 0; } // Update Backoff - CalculateBackOff( _params.last_channel_idx ); + calculate_backOff(_params.last_channel_idx); nextChan.aggregate_timeoff = _params.timers.aggregated_timeoff; nextChan.current_datarate = _params.sys_params.channel_data_rate; @@ -1270,12 +1199,12 @@ lorawan_status_t LoRaMac::ScheduleTx( void ) nextChan.last_aggregate_tx_time = _params.timers.aggregated_last_tx_time; // Select channel - while( lora_phy->set_next_channel(&nextChan, &_params.channel, &dutyCycleTimeOff, - &_params.timers.aggregated_timeoff ) == false ) - { + while (lora_phy->set_next_channel(&nextChan, &_params.channel, + &dutyCycleTimeOff, + &_params.timers.aggregated_timeoff) == false) { // Set the default datarate getPhy.attribute = PHY_DEF_TX_DR; - phyParam = lora_phy->get_phy_params( &getPhy ); + phyParam = lora_phy->get_phy_params(&getPhy); _params.sys_params.channel_data_rate = phyParam.value; // Update datarate in the function parameters nextChan.current_datarate = _params.sys_params.channel_data_rate; @@ -1288,73 +1217,72 @@ lorawan_status_t LoRaMac::ScheduleTx( void ) _params.sys_params.rx1_dr_offset); lora_phy->compute_rx_win_params(dr_offset, _params.sys_params.min_rx_symb, - _params.sys_params.max_sys_rx_error, - &_params.rx_window1_config ); + _params.sys_params.max_sys_rx_error, + &_params.rx_window1_config); + // Compute Rx2 windows parameters lora_phy->compute_rx_win_params(_params.sys_params.rx2_channel.datarate, _params.sys_params.min_rx_symb, _params.sys_params.max_sys_rx_error, - &_params.rx_window2_config ); + &_params.rx_window2_config); - if( _params.is_nwk_joined == false ) - { - _params.rx_window1_delay = _params.sys_params.join_accept_delay1 + _params.rx_window1_config.window_offset; - _params.rx_window2_delay = _params.sys_params.join_accept_delay2 + _params.rx_window2_config.window_offset; - } - else - { - if( ValidatePayloadLength( _params.payload_length, - _params.sys_params.channel_data_rate, - mac_commands.GetLength() ) == false ) - { + if (_params.is_nwk_joined == false) { + _params.rx_window1_delay = _params.sys_params.join_accept_delay1 + + _params.rx_window1_config.window_offset; + _params.rx_window2_delay = _params.sys_params.join_accept_delay2 + + _params.rx_window2_config.window_offset; + } else { + if (validate_payload_length(_params.payload_length, + _params.sys_params.channel_data_rate, + mac_commands.get_mac_cmd_length()) == false) { return LORAWAN_STATUS_LENGTH_ERROR; } - _params.rx_window1_delay = _params.sys_params.recv_delay1 + _params.rx_window1_config.window_offset; - _params.rx_window2_delay = _params.sys_params.recv_delay2 + _params.rx_window2_config.window_offset; + _params.rx_window1_delay = _params.sys_params.recv_delay1 + + _params.rx_window1_config.window_offset; + _params.rx_window2_delay = _params.sys_params.recv_delay2 + + _params.rx_window2_config.window_offset; } // Schedule transmission of frame - if( dutyCycleTimeOff == 0 ) - { + if (dutyCycleTimeOff == 0) { // Try to send now - return SendFrameOnChannel( _params.channel ); - } - else - { + return send_frame_on_channel(_params.channel); + } else { // Send later - prepare timer _params.mac_state |= LORAMAC_TX_DELAYED; tr_debug("Next Transmission in %lu ms", dutyCycleTimeOff); - _lora_time.start( _params.timers.tx_delayed_timer, dutyCycleTimeOff ); + _lora_time.start(_params.timers.tx_delayed_timer, dutyCycleTimeOff); return LORAWAN_STATUS_OK; } } -void LoRaMac::CalculateBackOff( uint8_t channel ) +void LoRaMac::calculate_backOff(uint8_t channel) { - backoff_params_t calcBackOff; + backoff_params_t backoff_params; - calcBackOff.joined = _params.is_nwk_joined; + backoff_params.joined = _params.is_nwk_joined; _params.is_dutycycle_on = MBED_CONF_LORA_DUTY_CYCLE_ON; - calcBackOff.dc_enabled = _params.is_dutycycle_on; - calcBackOff.channel = channel; - calcBackOff.elapsed_time = _lora_time.get_elapsed_time( _params.timers.mac_init_time ); - calcBackOff.tx_toa = _params.timers.tx_toa; - calcBackOff.last_tx_was_join_req = _params.is_last_tx_join_request; + backoff_params.dc_enabled = _params.is_dutycycle_on; + backoff_params.channel = channel; + backoff_params.elapsed_time = _lora_time.get_elapsed_time(_params.timers.mac_init_time); + backoff_params.tx_toa = _params.timers.tx_toa; + backoff_params.last_tx_was_join_req = _params.is_last_tx_join_request; // Update regional back-off - lora_phy->calculate_backoff(&calcBackOff); + lora_phy->calculate_backoff(&backoff_params); // Update aggregated time-off - _params.timers.aggregated_timeoff = _params.timers.aggregated_timeoff + - ( _params.timers.tx_toa * _params.sys_params.aggregated_duty_cycle - _params.timers.tx_toa ); + _params.timers.aggregated_timeoff = _params.timers.aggregated_timeoff + + (_params.timers.tx_toa * _params.sys_params.aggregated_duty_cycle + - _params.timers.tx_toa); } -void LoRaMac::ResetMacParameters( void ) +void LoRaMac::reset_mac_parameters(void) { - get_phy_params_t getPhy; - phy_param_t phyParam; + get_phy_params_t get_phy; + phy_param_t phy_param; _params.is_nwk_joined = false; @@ -1372,53 +1300,52 @@ void LoRaMac::ResetMacParameters( void ) _params.sys_params.max_duty_cycle = 0; _params.sys_params.aggregated_duty_cycle = 1; - mac_commands.ClearCommandBuffer(); - mac_commands.ClearRepeatBuffer(); - mac_commands.ClearMacCommandsInNextTx(); + mac_commands.clear_command_buffer(); + mac_commands.clear_repeat_buffer(); + mac_commands.clear_mac_commands_in_next_tx(); _params.is_rx_window_enabled = true; - getPhy.attribute = PHY_DEF_TX_POWER; - phyParam = lora_phy->get_phy_params( &getPhy ); + get_phy.attribute = PHY_DEF_TX_POWER; + phy_param = lora_phy->get_phy_params(&get_phy); - _params.sys_params.channel_tx_power = phyParam.value; + _params.sys_params.channel_tx_power = phy_param.value; - getPhy.attribute = PHY_DEF_TX_DR; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.channel_data_rate = phyParam.value; + get_phy.attribute = PHY_DEF_TX_DR; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.channel_data_rate = phy_param.value; - getPhy.attribute = PHY_DEF_DR1_OFFSET; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx1_dr_offset = phyParam.value; + get_phy.attribute = PHY_DEF_DR1_OFFSET; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx1_dr_offset = phy_param.value; - getPhy.attribute = PHY_DEF_RX2_FREQUENCY; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx2_channel.frequency = phyParam.value; - getPhy.attribute = PHY_DEF_RX2_DR; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx2_channel.datarate = phyParam.value; + get_phy.attribute = PHY_DEF_RX2_FREQUENCY; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx2_channel.frequency = phy_param.value; + get_phy.attribute = PHY_DEF_RX2_DR; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx2_channel.datarate = phy_param.value; - getPhy.attribute = PHY_DEF_UPLINK_DWELL_TIME; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.uplink_dwell_time = phyParam.value; + get_phy.attribute = PHY_DEF_UPLINK_DWELL_TIME; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.uplink_dwell_time = phy_param.value; - getPhy.attribute = PHY_DEF_MAX_EIRP; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.max_eirp = phyParam.value; + get_phy.attribute = PHY_DEF_MAX_EIRP; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.max_eirp = phy_param.value; - getPhy.attribute = PHY_DEF_ANTENNA_GAIN; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.antenna_gain = phyParam.value; + get_phy.attribute = PHY_DEF_ANTENNA_GAIN; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.antenna_gain = phy_param.value; _params.is_node_ack_requested = false; _params.is_srv_ack_requested = false; // Reset Multicast downlink counters multicast_params_t *cur = _params.multicast_channels; - while( cur != NULL ) - { + while (cur != NULL) { cur->dl_frame_counter = 0; - cur = cur->Next; + cur = cur->next; } // Initialize channel index. @@ -1426,89 +1353,89 @@ void LoRaMac::ResetMacParameters( void ) _params.last_channel_idx = _params.channel; } -bool LoRaMac::IsFPortAllowed( uint8_t fPort ) +bool LoRaMac::is_fPort_allowed (uint8_t fPort) { - if( ( fPort == 0 ) || ( fPort > 224 ) ) - { + if ((fPort == 0) || (fPort > 224)) { return false; } return true; } -void LoRaMac::OpenContinuousRx2Window( void ) +void LoRaMac::open_continuous_rx2_window (void) { - handle_rx2_timer_event( ); + handle_rx2_timer_event(); _params.rx_slot = RX_SLOT_WIN_CLASS_C; } -static void memcpy_convert_endianess( uint8_t *dst, const uint8_t *src, uint16_t size ) +static void memcpy_convert_endianess(uint8_t *dst, const uint8_t *src, + uint16_t size) { - dst = dst + ( size - 1 ); - while( size-- ) - { + dst = dst + (size - 1); + while (size--) { *dst-- = *src++; } } -lorawan_status_t LoRaMac::PrepareFrame( loramac_mhdr_t *macHdr, loramac_frame_ctrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize ) +lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr, + loramac_frame_ctrl_t *fctrl, + uint8_t fport, void *fbuffer, + uint16_t fbuffer_size) { uint16_t i; - uint8_t pktHeaderLen = 0; + uint8_t pkt_header_len = 0; uint32_t mic = 0; - const void* payload = fBuffer; - uint8_t framePort = fPort; + const void* payload = fbuffer; + uint8_t frame_port = fport; lorawan_status_t status = LORAWAN_STATUS_OK; _params.buffer_pkt_len = 0; _params.is_node_ack_requested = false; - if( fBuffer == NULL ) - { - fBufferSize = 0; + if (fbuffer == NULL) { + fbuffer_size = 0; } - _params.payload_length = fBufferSize; + _params.payload_length = fbuffer_size; - _params.buffer[pktHeaderLen++] = macHdr->value; + _params.buffer[pkt_header_len++] = machdr->value; + + switch (machdr->bits.mtype) { - switch( macHdr->bits.mtype ) - { case FRAME_TYPE_JOIN_REQ: - _params.buffer_pkt_len = pktHeaderLen; - memcpy_convert_endianess( _params.buffer + _params.buffer_pkt_len, - _params.keys.app_eui, 8 ); + _params.buffer_pkt_len = pkt_header_len; + memcpy_convert_endianess(_params.buffer + _params.buffer_pkt_len, + _params.keys.app_eui, 8); _params.buffer_pkt_len += 8; - memcpy_convert_endianess( _params.buffer + _params.buffer_pkt_len, - _params.keys.dev_eui, 8 ); + memcpy_convert_endianess(_params.buffer + _params.buffer_pkt_len, + _params.keys.dev_eui, 8); _params.buffer_pkt_len += 8; _params.dev_nonce = lora_phy->get_radio_rng(); _params.buffer[_params.buffer_pkt_len++] = _params.dev_nonce & 0xFF; - _params.buffer[_params.buffer_pkt_len++] = ( _params.dev_nonce >> 8 ) & 0xFF; + _params.buffer[_params.buffer_pkt_len++] = (_params.dev_nonce >> 8) & 0xFF; - if (0 != LoRaMacJoinComputeMic( _params.buffer, - _params.buffer_pkt_len & 0xFF, - _params.keys.app_key, &mic )) { + if (0 != compute_join_frame_mic(_params.buffer, + _params.buffer_pkt_len & 0xFF, + _params.keys.app_key, &mic)) { return LORAWAN_STATUS_CRYPTO_FAIL; } _params.buffer[_params.buffer_pkt_len++] = mic & 0xFF; - _params.buffer[_params.buffer_pkt_len++] = ( mic >> 8 ) & 0xFF; - _params. buffer[_params.buffer_pkt_len++] = ( mic >> 16 ) & 0xFF; - _params.buffer[_params.buffer_pkt_len++] = ( mic >> 24 ) & 0xFF; + _params.buffer[_params.buffer_pkt_len++] = (mic >> 8) & 0xFF; + _params.buffer[_params.buffer_pkt_len++] = (mic >> 16) & 0xFF; + _params.buffer[_params.buffer_pkt_len++] = (mic >> 24) & 0xFF; break; case FRAME_TYPE_DATA_CONFIRMED_UP: _params.is_node_ack_requested = true; //Intentional fallthrough - case FRAME_TYPE_DATA_UNCONFIRMED_UP: - { - if( _params.is_nwk_joined == false ) - { - return LORAWAN_STATUS_NO_NETWORK_JOINED; // No network has been joined yet + case FRAME_TYPE_DATA_UNCONFIRMED_UP: { + if (_params.is_nwk_joined == false) { + // No network has been joined yet + return LORAWAN_STATUS_NO_NETWORK_JOINED; } if (_params.sys_params.adr_on) { @@ -1516,124 +1443,109 @@ lorawan_status_t LoRaMac::PrepareFrame( loramac_mhdr_t *macHdr, loramac_frame_ct _params.sys_params.channel_data_rate, _params.sys_params.channel_tx_power, _params.adr_ack_counter)) { - fCtrl->bits.adr_ack_req = 1; + fctrl->bits.adr_ack_req = 1; } } - if( _params.is_srv_ack_requested == true ) - { + if (_params.is_srv_ack_requested == true) { _params.is_srv_ack_requested = false; - fCtrl->bits.ack = 1; + fctrl->bits.ack = 1; } - _params.buffer[pktHeaderLen++] = ( _params.dev_addr ) & 0xFF; - _params.buffer[pktHeaderLen++] = ( _params.dev_addr >> 8 ) & 0xFF; - _params.buffer[pktHeaderLen++] = ( _params.dev_addr >> 16 ) & 0xFF; - _params.buffer[pktHeaderLen++] = ( _params.dev_addr >> 24 ) & 0xFF; + _params.buffer[pkt_header_len++] = (_params.dev_addr) & 0xFF; + _params.buffer[pkt_header_len++] = (_params.dev_addr >> 8) & 0xFF; + _params.buffer[pkt_header_len++] = (_params.dev_addr >> 16) & 0xFF; + _params.buffer[pkt_header_len++] = (_params.dev_addr >> 24) & 0xFF; - _params.buffer[pktHeaderLen++] = fCtrl->value; + _params.buffer[pkt_header_len++] = fctrl->value; - _params.buffer[pktHeaderLen++] = _params.ul_frame_counter & 0xFF; - _params.buffer[pktHeaderLen++] = ( _params.ul_frame_counter >> 8 ) & 0xFF; + _params.buffer[pkt_header_len++] = _params.ul_frame_counter & 0xFF; + _params.buffer[pkt_header_len++] = (_params.ul_frame_counter >> 8) + & 0xFF; // Copy the MAC commands which must be re-send into the MAC command buffer - mac_commands.CopyRepeatCommandsToBuffer(); + mac_commands.copy_repeat_commands_to_buffer(); - const uint8_t mac_commands_len = mac_commands.GetLength(); - if( ( payload != NULL ) && ( _params.payload_length > 0 ) ) - { - if( mac_commands.IsMacCommandsInNextTx() == true ) - { - if( mac_commands_len <= LORA_MAC_COMMAND_MAX_FOPTS_LENGTH ) - { - fCtrl->bits.fopts_len += mac_commands_len; + const uint8_t mac_commands_len = mac_commands.get_mac_cmd_length(); + + if ((payload != NULL) && (_params.payload_length > 0)) { + if (mac_commands.is_mac_command_in_next_tx() == true) { + if (mac_commands_len <= LORA_MAC_COMMAND_MAX_FOPTS_LENGTH) { + fctrl->bits.fopts_len += mac_commands_len; // Update FCtrl field with new value of OptionsLength - _params.buffer[0x05] = fCtrl->value; + _params.buffer[0x05] = fctrl->value; - const uint8_t *buffer = mac_commands.GetMacCommandsBuffer(); - for( i = 0; i < mac_commands_len; i++ ) - { - _params.buffer[pktHeaderLen++] = buffer[i]; + const uint8_t *buffer = + mac_commands.get_mac_commands_buffer(); + for (i = 0; i < mac_commands_len; i++) { + _params.buffer[pkt_header_len++] = buffer[i]; } - } - else - { + } else { _params.payload_length = mac_commands_len; - payload = mac_commands.GetMacCommandsBuffer(); - framePort = 0; + payload = mac_commands.get_mac_commands_buffer(); + frame_port = 0; } } - } - else - { - if( ( mac_commands_len > 0 ) && ( mac_commands.IsMacCommandsInNextTx() == true ) ) - { + } else { + if ((mac_commands_len > 0) + && (mac_commands.is_mac_command_in_next_tx() == true)) { _params.payload_length = mac_commands_len; - payload = mac_commands.GetMacCommandsBuffer(); - framePort = 0; + payload = mac_commands.get_mac_commands_buffer(); + frame_port = 0; } } // Store MAC commands which must be re-send in case the device does not receive a downlink anymore - mac_commands.ParseMacCommandsToRepeat(); + mac_commands.parse_mac_commands_to_repeat(); - if( ( payload != NULL ) && ( _params.payload_length > 0 ) ) - { - _params.buffer[pktHeaderLen++] = framePort; + if ((payload != NULL) && (_params.payload_length > 0)) { + _params.buffer[pkt_header_len++] = frame_port; - if( framePort == 0 ) - { + if (frame_port == 0) { // Reset buffer index as the mac commands are being sent on port 0 - mac_commands.ClearCommandBuffer(); - if (0 != LoRaMacPayloadEncrypt( (uint8_t* ) payload, - _params.payload_length, - _params.keys.nwk_skey, - _params.dev_addr, - UP_LINK, - _params.ul_frame_counter, - &_params.buffer[pktHeaderLen] )) { + mac_commands.clear_command_buffer(); + if (0 != encrypt_payload((uint8_t*) payload, _params.payload_length, + _params.keys.nwk_skey, _params.dev_addr, + UP_LINK, + _params.ul_frame_counter, + &_params.buffer[pkt_header_len])) { status = LORAWAN_STATUS_CRYPTO_FAIL; } - } - else - { - if (0 != LoRaMacPayloadEncrypt( (uint8_t* ) payload, - _params.payload_length, - _params.keys.app_skey, - _params.dev_addr, - UP_LINK, - _params.ul_frame_counter, - &_params.buffer[pktHeaderLen] )) { + } else { + if (0 != encrypt_payload((uint8_t*) payload, _params.payload_length, + _params.keys.app_skey, _params.dev_addr, + UP_LINK, + _params.ul_frame_counter, + &_params.buffer[pkt_header_len])) { status = LORAWAN_STATUS_CRYPTO_FAIL; } } } - _params.buffer_pkt_len = pktHeaderLen + _params.payload_length; - if (0 != LoRaMacComputeMic( _params.buffer, - _params.buffer_pkt_len, - _params.keys.nwk_skey, - _params.dev_addr, - UP_LINK, - _params.ul_frame_counter, - &mic )) { + _params.buffer_pkt_len = pkt_header_len + _params.payload_length; + + if (0 != compute_mic(_params.buffer, _params.buffer_pkt_len, + _params.keys.nwk_skey, + _params.dev_addr, + UP_LINK, + _params.ul_frame_counter, &mic)) { status = LORAWAN_STATUS_CRYPTO_FAIL; } _params.buffer[_params.buffer_pkt_len + 0] = mic & 0xFF; - _params.buffer[_params.buffer_pkt_len + 1] = ( mic >> 8 ) & 0xFF; - _params.buffer[_params.buffer_pkt_len + 2] = ( mic >> 16 ) & 0xFF; - _params.buffer[_params.buffer_pkt_len + 3] = ( mic >> 24 ) & 0xFF; + _params.buffer[_params.buffer_pkt_len + 1] = (mic >> 8) & 0xFF; + _params.buffer[_params.buffer_pkt_len + 2] = (mic >> 16) & 0xFF; + _params.buffer[_params.buffer_pkt_len + 3] = (mic >> 24) & 0xFF; _params.buffer_pkt_len += LORAMAC_MFR_LEN; } - break; + break; case FRAME_TYPE_PROPRIETARY: - if( ( fBuffer != NULL ) && (_params.payload_length > 0 ) ) - { - memcpy( _params.buffer + pktHeaderLen, ( uint8_t* ) fBuffer, _params.payload_length ); - _params.buffer_pkt_len = pktHeaderLen + _params.payload_length; + if ((fbuffer != NULL) && (_params.payload_length > 0)) { + memcpy(_params.buffer + pkt_header_len, (uint8_t*) fbuffer, + _params.payload_length); + _params.buffer_pkt_len = pkt_header_len + _params.payload_length; } break; default: @@ -1643,10 +1555,10 @@ lorawan_status_t LoRaMac::PrepareFrame( loramac_mhdr_t *macHdr, loramac_frame_ct return status; } -lorawan_status_t LoRaMac::SendFrameOnChannel( uint8_t channel ) +lorawan_status_t LoRaMac::send_frame_on_channel(uint8_t channel) { tx_config_params_t tx_config; - int8_t txPower = 0; + int8_t tx_power = 0; tx_config.channel = channel; tx_config.datarate = _params.sys_params.channel_data_rate; @@ -1655,23 +1567,23 @@ lorawan_status_t LoRaMac::SendFrameOnChannel( uint8_t channel ) tx_config.antenna_gain = _params.sys_params.antenna_gain; tx_config.pkt_len = _params.buffer_pkt_len; - lora_phy->tx_config(&tx_config, &txPower, &_params.timers.tx_toa); + lora_phy->tx_config(&tx_config, &tx_power, &_params.timers.tx_toa); mlme.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_ERROR; mcps.get_confirmation().status = LORAMAC_EVENT_INFO_STATUS_ERROR; mcps.get_confirmation().data_rate = _params.sys_params.channel_data_rate; - mcps.get_confirmation().tx_power = txPower; + mcps.get_confirmation().tx_power = tx_power; // Store the time on air mcps.get_confirmation().tx_toa = _params.timers.tx_toa; mlme.get_confirmation().tx_toa = _params.timers.tx_toa; // Starts the MAC layer status check timer - _lora_time.start( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT ); + _lora_time.start(_params.timers.mac_state_check_timer, + MAC_STATE_CHECK_TIMEOUT); - if( _params.is_nwk_joined == false ) - { + if (_params.is_nwk_joined == false) { _params.join_request_trial_counter++; } @@ -1683,60 +1595,62 @@ lorawan_status_t LoRaMac::SendFrameOnChannel( uint8_t channel ) return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::SetTxContinuousWave( uint16_t timeout ) +lorawan_status_t LoRaMac::set_tx_continuous_wave(uint16_t timeout) { - cw_mode_params_t continuousWave; + cw_mode_params_t continuous_wave; - continuousWave.channel = _params.channel; - continuousWave.datarate = _params.sys_params.channel_data_rate; - continuousWave.tx_power = _params.sys_params.channel_tx_power; - continuousWave.max_eirp = _params.sys_params.max_eirp; - continuousWave.antenna_gain = _params.sys_params.antenna_gain; - continuousWave.timeout = timeout; + continuous_wave.channel = _params.channel; + continuous_wave.datarate = _params.sys_params.channel_data_rate; + continuous_wave.tx_power = _params.sys_params.channel_tx_power; + continuous_wave.max_eirp = _params.sys_params.max_eirp; + continuous_wave.antenna_gain = _params.sys_params.antenna_gain; + continuous_wave.timeout = timeout; - lora_phy->set_tx_cont_mode(&continuousWave); + lora_phy->set_tx_cont_mode(&continuous_wave); // Starts the MAC layer status check timer - _lora_time.start( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT ); + _lora_time.start(_params.timers.mac_state_check_timer, + MAC_STATE_CHECK_TIMEOUT); _params.mac_state |= LORAMAC_TX_RUNNING; return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power ) +lorawan_status_t LoRaMac::set_tx_continuous_wave1(uint16_t timeout, + uint32_t frequency, + uint8_t power) { - cw_mode_params_t continuousWave; + cw_mode_params_t continuous_wave; - continuousWave.channel = 0; - continuousWave.datarate = 0; - continuousWave.tx_power = power; - continuousWave.max_eirp = 0; - continuousWave.antenna_gain = 0; - continuousWave.timeout = timeout; + continuous_wave.channel = 0; + continuous_wave.datarate = 0; + continuous_wave.tx_power = power; + continuous_wave.max_eirp = 0; + continuous_wave.antenna_gain = 0; + continuous_wave.timeout = timeout; - lora_phy->set_tx_cont_mode(&continuousWave); + lora_phy->set_tx_cont_mode(&continuous_wave); // Starts the MAC layer status check timer - _lora_time.start( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT ); + _lora_time.start(_params.timers.mac_state_check_timer, + MAC_STATE_CHECK_TIMEOUT); _params.mac_state |= LORAMAC_TX_RUNNING; return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::LoRaMacInitialization(loramac_primitives_t *primitives, - LoRaPHY *phy, - EventQueue *queue) +lorawan_status_t LoRaMac::initialize(loramac_primitives_t *primitives, + LoRaPHY *phy, EventQueue *queue) { - get_phy_params_t getPhy; - phy_param_t phyParam; + get_phy_params_t get_phy; + phy_param_t phy_param; //store event queue pointer ev_queue = queue; - if(!primitives) - { + if (!primitives) { return LORAWAN_STATUS_PARAMETER_INVALID; } @@ -1752,9 +1666,9 @@ lorawan_status_t LoRaMac::LoRaMacInitialization(loramac_primitives_t *primitives mib.activate_mib_subsystem(this, lora_phy); // Activate channel planning subsystem - ch_plan.activate_channelplan_subsystem(lora_phy, &mib); + channel_plan.activate_channelplan_subsystem(lora_phy, &mib); - LoRaMacPrimitives = primitives; + mac_primitives = primitives; _params.flags.value = 0; @@ -1770,73 +1684,73 @@ lorawan_status_t LoRaMac::LoRaMacInitialization(loramac_primitives_t *primitives _params.timers.aggregated_timeoff = 0; // Reset to defaults - getPhy.attribute = PHY_DUTY_CYCLE; - phyParam = lora_phy->get_phy_params(&getPhy); + get_phy.attribute = PHY_DUTY_CYCLE; + phy_param = lora_phy->get_phy_params(&get_phy); // load default at this moment. Later can be changed using json - _params.is_dutycycle_on = ( bool ) phyParam.value; + _params.is_dutycycle_on = (bool) phy_param.value; - getPhy.attribute = PHY_DEF_TX_POWER; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.channel_tx_power = phyParam.value; + get_phy.attribute = PHY_DEF_TX_POWER; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.channel_tx_power = phy_param.value; - getPhy.attribute = PHY_DEF_TX_DR; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.channel_data_rate = phyParam.value; + get_phy.attribute = PHY_DEF_TX_DR; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.channel_data_rate = phy_param.value; - getPhy.attribute = PHY_MAX_RX_WINDOW; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.max_rx_win_time = phyParam.value; + get_phy.attribute = PHY_MAX_RX_WINDOW; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.max_rx_win_time = phy_param.value; - getPhy.attribute = PHY_RECEIVE_DELAY1; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.recv_delay1 = phyParam.value; + get_phy.attribute = PHY_RECEIVE_DELAY1; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.recv_delay1 = phy_param.value; - getPhy.attribute = PHY_RECEIVE_DELAY2; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.recv_delay2 = phyParam.value; + get_phy.attribute = PHY_RECEIVE_DELAY2; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.recv_delay2 = phy_param.value; - getPhy.attribute = PHY_JOIN_ACCEPT_DELAY1; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.join_accept_delay1 = phyParam.value; + get_phy.attribute = PHY_JOIN_ACCEPT_DELAY1; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.join_accept_delay1 = phy_param.value; - getPhy.attribute = PHY_JOIN_ACCEPT_DELAY2; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.join_accept_delay2 = phyParam.value; + get_phy.attribute = PHY_JOIN_ACCEPT_DELAY2; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.join_accept_delay2 = phy_param.value; - getPhy.attribute = PHY_DEF_DR1_OFFSET; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx1_dr_offset = phyParam.value; + get_phy.attribute = PHY_DEF_DR1_OFFSET; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx1_dr_offset = phy_param.value; - getPhy.attribute = PHY_DEF_RX2_FREQUENCY; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx2_channel.frequency = phyParam.value; + get_phy.attribute = PHY_DEF_RX2_FREQUENCY; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx2_channel.frequency = phy_param.value; - getPhy.attribute = PHY_DEF_RX2_DR; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.rx2_channel.datarate = phyParam.value; + get_phy.attribute = PHY_DEF_RX2_DR; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.rx2_channel.datarate = phy_param.value; - getPhy.attribute = PHY_DEF_UPLINK_DWELL_TIME; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.uplink_dwell_time = phyParam.value; + get_phy.attribute = PHY_DEF_UPLINK_DWELL_TIME; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.uplink_dwell_time = phy_param.value; - getPhy.attribute = PHY_DEF_DOWNLINK_DWELL_TIME; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.downlink_dwell_time = phyParam.value; + get_phy.attribute = PHY_DEF_DOWNLINK_DWELL_TIME; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.downlink_dwell_time = phy_param.value; - getPhy.attribute = PHY_DEF_MAX_EIRP; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.max_eirp = phyParam.f_value; + get_phy.attribute = PHY_DEF_MAX_EIRP; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.max_eirp = phy_param.f_value; - getPhy.attribute = PHY_DEF_ANTENNA_GAIN; - phyParam = lora_phy->get_phy_params( &getPhy ); - _params.sys_params.antenna_gain = phyParam.f_value; + get_phy.attribute = PHY_DEF_ANTENNA_GAIN; + phy_param = lora_phy->get_phy_params(&get_phy); + _params.sys_params.antenna_gain = phy_param.f_value; // Init parameters which are not set in function ResetMacParameters _params.sys_params.max_sys_rx_error = 10; _params.sys_params.min_rx_symb = 6; _params.sys_params.retry_num = 1; - ResetMacParameters( ); + reset_mac_parameters(); // Random seed initialization srand(lora_phy->get_radio_rng()); @@ -1847,15 +1761,15 @@ lorawan_status_t LoRaMac::LoRaMacInitialization(loramac_primitives_t *primitives // Initialize timers _lora_time.init(_params.timers.mac_state_check_timer, - mbed::callback(this, &LoRaMac::handle_mac_state_check_timer_event)); + mbed::callback(this, &LoRaMac::handle_mac_state_check_timer_event)); _lora_time.init(_params.timers.tx_delayed_timer, - mbed::callback(this, &LoRaMac::handle_delayed_tx_timer_event)); + mbed::callback(this, &LoRaMac::handle_delayed_tx_timer_event)); _lora_time.init(_params.timers.rx_window1_timer, - mbed::callback(this, &LoRaMac::handle_rx1_timer_event)); + mbed::callback(this, &LoRaMac::handle_rx1_timer_event)); _lora_time.init(_params.timers.rx_window2_timer, - mbed::callback(this, &LoRaMac::handle_rx2_timer_event)); + mbed::callback(this, &LoRaMac::handle_rx2_timer_event)); _lora_time.init(_params.timers.ack_timeout_timer, - mbed::callback(this, &LoRaMac::handle_ack_timeout)); + mbed::callback(this, &LoRaMac::handle_ack_timeout)); // Store the current initialization time _params.timers.mac_init_time = _lora_time.get_current_time(); @@ -1885,23 +1799,24 @@ void LoRaMac::disconnect() _params.mac_state = 0; // Clear MAC commands - mac_commands.ClearCommandBuffer(); - mac_commands.ClearRepeatBuffer(); - mac_commands.ClearMacCommandsInNextTx(); + mac_commands.clear_command_buffer(); + mac_commands.clear_repeat_buffer(); + mac_commands.clear_mac_commands_in_next_tx(); // Set internal state to idle. _params.mac_state = LORAMAC_IDLE; } -lorawan_status_t LoRaMac::LoRaMacQueryTxPossible( uint8_t size, loramac_tx_info_t* txInfo ) +lorawan_status_t LoRaMac::query_tx_possible(uint8_t size, + loramac_tx_info_t* tx_info) { - get_phy_params_t getPhy; - phy_param_t phyParam; + get_phy_params_t get_phy; + phy_param_t phy_param; - uint8_t fOptLen = mac_commands.GetLength() + mac_commands.GetRepeatLength(); + uint8_t fopt_len = mac_commands.get_mac_cmd_length() + + mac_commands.get_repeat_commands_length(); - if( txInfo == NULL ) - { + if (tx_info == NULL) { return LORAWAN_STATUS_PARAMETER_INVALID; } @@ -1913,167 +1828,143 @@ lorawan_status_t LoRaMac::LoRaMacQueryTxPossible( uint8_t size, loramac_tx_info_ } // Setup PHY request - getPhy.datarate = _params.sys_params.channel_data_rate; - getPhy.attribute = PHY_MAX_PAYLOAD; + get_phy.datarate = _params.sys_params.channel_data_rate; + get_phy.attribute = PHY_MAX_PAYLOAD; // Change request in case repeater is supported - if( _params.is_repeater_supported == true ) - { - getPhy.attribute = PHY_MAX_PAYLOAD_REPEATER; + if (_params.is_repeater_supported == true) { + get_phy.attribute = PHY_MAX_PAYLOAD_REPEATER; } - phyParam = lora_phy->get_phy_params( &getPhy ); - txInfo->current_payload_size = phyParam.value; + phy_param = lora_phy->get_phy_params(&get_phy); + tx_info->current_payload_size = phy_param.value; // Verify if the fOpts fit into the maximum payload - if( txInfo->current_payload_size >= fOptLen ) - { - txInfo->max_possible_payload_size = txInfo->current_payload_size - fOptLen; - } - else - { - txInfo->max_possible_payload_size = txInfo->current_payload_size; + if (tx_info->current_payload_size >= fopt_len) { + tx_info->max_possible_payload_size = tx_info->current_payload_size - fopt_len; + } else { + tx_info->max_possible_payload_size = tx_info->current_payload_size; // The fOpts don't fit into the maximum payload. Omit the MAC commands to // ensure that another uplink is possible. - fOptLen = 0; - mac_commands.ClearCommandBuffer(); - mac_commands.ClearRepeatBuffer(); + fopt_len = 0; + mac_commands.clear_command_buffer(); + mac_commands.clear_repeat_buffer(); } // Verify if the fOpts and the payload fit into the maximum payload - if( ValidatePayloadLength( size, _params.sys_params.channel_data_rate, - fOptLen ) == false ) - { + if (validate_payload_length(size, _params.sys_params.channel_data_rate, + fopt_len) == false) { return LORAWAN_STATUS_LENGTH_ERROR; } return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::AddChannelPlan(const lorawan_channelplan_t& plan) +lorawan_status_t LoRaMac::add_channel_plan(const lorawan_channelplan_t& plan) { // Validate if the MAC is in a correct state - if( ( _params.mac_state & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) - { - if( ( _params.mac_state & LORAMAC_TX_CONFIG ) != LORAMAC_TX_CONFIG ) - { + if ((_params.mac_state & LORAMAC_TX_RUNNING) == LORAMAC_TX_RUNNING) { + if ((_params.mac_state & LORAMAC_TX_CONFIG) != LORAMAC_TX_CONFIG) { return LORAWAN_STATUS_BUSY; } } - return ch_plan.set_plan(plan); + return channel_plan.set_plan(plan); } -lorawan_status_t LoRaMac::RemoveChannelPlan() +lorawan_status_t LoRaMac::remove_channel_plan() { - if( ( _params.mac_state & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) - { - if( ( _params.mac_state & LORAMAC_TX_CONFIG ) != LORAMAC_TX_CONFIG ) - { + if ((_params.mac_state & LORAMAC_TX_RUNNING) == LORAMAC_TX_RUNNING) { + if ((_params.mac_state & LORAMAC_TX_CONFIG) != LORAMAC_TX_CONFIG) { return LORAWAN_STATUS_BUSY; } } - return ch_plan.remove_plan(); + return channel_plan.remove_plan(); } -lorawan_status_t LoRaMac::GetChannelPlan(lorawan_channelplan_t& plan) +lorawan_status_t LoRaMac::get_channel_plan(lorawan_channelplan_t& plan) { - return ch_plan.get_plan(plan, &_params); + return channel_plan.get_plan(plan, &_params); } -lorawan_status_t LoRaMac::RemoveSingleChannel(uint8_t id) +lorawan_status_t LoRaMac::remove_single_channel(uint8_t id) { - if( ( _params.mac_state & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) - { - if( ( _params.mac_state & LORAMAC_TX_CONFIG ) != LORAMAC_TX_CONFIG ) - { + if ((_params.mac_state & LORAMAC_TX_RUNNING) == LORAMAC_TX_RUNNING) { + if ((_params.mac_state & LORAMAC_TX_CONFIG) != LORAMAC_TX_CONFIG) { return LORAWAN_STATUS_BUSY; } } - return ch_plan.remove_single_channel(id); + return channel_plan.remove_single_channel(id); } -lorawan_status_t LoRaMac::LoRaMacMulticastChannelLink( multicast_params_t *channelParam ) +lorawan_status_t LoRaMac::multicast_channel_link(multicast_params_t *channel_param) { - if( channelParam == NULL ) - { + if (channel_param == NULL) { return LORAWAN_STATUS_PARAMETER_INVALID; } - if( ( _params.mac_state & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) - { + if ((_params.mac_state & LORAMAC_TX_RUNNING) == LORAMAC_TX_RUNNING) { return LORAWAN_STATUS_BUSY; } // Reset downlink counter - channelParam->dl_frame_counter = 0; + channel_param->dl_frame_counter = 0; - if( _params.multicast_channels == NULL ) - { + if (_params.multicast_channels == NULL) { // New node is the fist element - _params.multicast_channels = channelParam; - } - else - { + _params.multicast_channels = channel_param; + } else { multicast_params_t *cur = _params.multicast_channels; // Search the last node in the list - while( cur->Next != NULL ) - { - cur = cur->Next; + while (cur->next != NULL) { + cur = cur->next; } // This function always finds the last node - cur->Next = channelParam; + cur->next = channel_param; } return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::LoRaMacMulticastChannelUnlink( multicast_params_t *channelParam ) +lorawan_status_t LoRaMac::multicast_channel_unlink( + multicast_params_t *channel_param) { - if( channelParam == NULL ) - { + if (channel_param == NULL) { return LORAWAN_STATUS_PARAMETER_INVALID; } - if( ( _params.mac_state & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) - { + if ((_params.mac_state & LORAMAC_TX_RUNNING) == LORAMAC_TX_RUNNING) { return LORAWAN_STATUS_BUSY; } - if( _params.multicast_channels != NULL ) - { - if( _params.multicast_channels == channelParam ) - { - // First element - _params.multicast_channels = channelParam->Next; - } - else - { + if (_params.multicast_channels != NULL) { + if (_params.multicast_channels == channel_param) { + // First element + _params.multicast_channels = channel_param->next; + } else { multicast_params_t *cur = _params.multicast_channels; // Search the node in the list - while( cur->Next && cur->Next != channelParam ) - { - cur = cur->Next; + while (cur->next && cur->next != channel_param) { + cur = cur->next; } // If we found the node, remove it - if( cur->Next ) - { - cur->Next = channelParam->Next; + if (cur->next) { + cur->next = channel_param->next; } } - channelParam->Next = NULL; + channel_param->next = NULL; } return LORAWAN_STATUS_OK; } -lorawan_status_t LoRaMac::LoRaMacMlmeRequest( loramac_mlme_req_t *mlmeRequest ) +lorawan_status_t LoRaMac::mlme_request( loramac_mlme_req_t *mlmeRequest ) { return mlme.set_request(mlmeRequest, &_params); } -lorawan_status_t LoRaMac::LoRaMacMcpsRequest( loramac_mcps_req_t *mcpsRequest ) +lorawan_status_t LoRaMac::mcps_request( loramac_mcps_req_t *mcpsRequest ) { if (_params.mac_state != LORAMAC_IDLE) { return LORAWAN_STATUS_BUSY; @@ -2082,25 +1973,25 @@ lorawan_status_t LoRaMac::LoRaMacMcpsRequest( loramac_mcps_req_t *mcpsRequest ) return mcps.set_request(mcpsRequest, &_params); } -lorawan_status_t LoRaMac::LoRaMacMibGetRequestConfirm( loramac_mib_req_confirm_t *mibGet ) +lorawan_status_t LoRaMac::mib_get_request_confirm( loramac_mib_req_confirm_t *mibGet ) { return mib.get_request(mibGet, &_params); } -lorawan_status_t LoRaMac::LoRaMacMibSetRequestConfirm( loramac_mib_req_confirm_t *mibSet ) +lorawan_status_t LoRaMac::mib_set_request_confirm( loramac_mib_req_confirm_t *mibSet ) { return mib.set_request(mibSet, &_params); } -radio_events_t *LoRaMac::GetPhyEventHandlers() +radio_events_t *LoRaMac::get_phy_event_handlers() { - RadioEvents.tx_done = mbed::callback(this, &LoRaMac::handle_tx_done); - RadioEvents.rx_done = mbed::callback(this, &LoRaMac::handle_rx_done); - RadioEvents.rx_error = mbed::callback(this, &LoRaMac::handle_rx_error); - RadioEvents.tx_timeout = mbed::callback(this, &LoRaMac::handle_tx_timeout); - RadioEvents.rx_timeout = mbed::callback(this, &LoRaMac::handle_rx_timeout); + radio_events.tx_done = mbed::callback(this, &LoRaMac::handle_tx_done); + radio_events.rx_done = mbed::callback(this, &LoRaMac::handle_rx_done); + radio_events.rx_error = mbed::callback(this, &LoRaMac::handle_rx_error); + radio_events.tx_timeout = mbed::callback(this, &LoRaMac::handle_tx_timeout); + radio_events.rx_timeout = mbed::callback(this, &LoRaMac::handle_rx_timeout); - return &RadioEvents; + return &radio_events; } #if defined(LORAWAN_COMPLIANCE_TEST) diff --git a/features/lorawan/lorastack/mac/LoRaMac.h b/features/lorawan/lorastack/mac/LoRaMac.h index 030f6f3e4a..b1676f8489 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.h +++ b/features/lorawan/lorastack/mac/LoRaMac.h @@ -37,8 +37,8 @@ * SPDX-License-Identifier: BSD-3-Clause * */ -#ifndef __LORAMAC_H__ -#define __LORAMAC_H__ +#ifndef MBED_LORAWAN_MAC_H__ +#define MBED_LORAWAN_MAC_H__ #include "lorawan/system/LoRaWANTimer.h" #include "lorastack/phy/LoRaPHY.h" @@ -50,63 +50,63 @@ #include "lorastack/mac/LoRaMacMib.h" #include "lorastack/mac/LoRaMacChannelPlan.h" -class LoRaMac -{ +class LoRaMac { + public: - /*! - * \brief Constructor + + /** + * Constructor */ LoRaMac(LoRaWANTimeHandler &lora_time); - /*! - * \brief Destructor + /** + * Destructor */ ~LoRaMac(); - /*! - * \brief LoRaMAC layer initialization + /** + * @brief LoRaMAC layer initialization * - * \details In addition to the initialization of the LoRaMAC layer, this + * @details In addition to the initialization of the LoRaMAC layer, this * function initializes the callback primitives of the MCPS and * MLME services. Every data field of \ref loramac_primitives_t must be * set to a valid callback function. * - * \param primitives [in] - A pointer to the structure defining the LoRaMAC + * @param primitives [in] A pointer to the structure defining the LoRaMAC * event functions. Refer to \ref loramac_primitives_t. * - * \param phy [in]- A pointer to the selected PHY layer. + * @param phy [in] A pointer to the selected PHY layer. * - * \param queue [in]- A pointer to the application provided EventQueue. + * @param queue [in] A pointer to the application provided EventQueue. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t LoRaMacInitialization(loramac_primitives_t *primitives, - LoRaPHY *phy, - events::EventQueue *queue); + lorawan_status_t initialize(loramac_primitives_t *primitives, LoRaPHY *phy, + events::EventQueue *queue); - /*! - * \brief Disconnect LoRaMac layer + /** + * @brief Disconnect LoRaMac layer * - * \details Cancels all outstanding requests and sets LoRaMac's + * @details Cancels all outstanding requests and sets LoRaMac's * internal state to idle. */ void disconnect(void); - /*! - * \brief Queries the LoRaMAC whether it is possible to send the next frame with + /** + * @brief Queries the LoRaMAC whether it is possible to send the next frame with * a given payload size. The LoRaMAC takes the scheduled MAC commands into * account and reports when the frame can be sent. * - * \param size [in]- The size of the applicable payload to be sent next. - * \param txInfo [out] - The structure \ref loramac_tx_info_t contains - * information on the actual maximum payload possible - * (according to the configured datarate or the next - * datarate according to ADR), and the maximum frame - * size, taking the scheduled MAC commands into account. + * @param size [in] The size of the applicable payload to be sent next. + * @param tx_info [out] The structure \ref loramac_tx_info_t contains + * information on the actual maximum payload possible + * (according to the configured datarate or the next + * datarate according to ADR), and the maximum frame + * size, taking the scheduled MAC commands into account. * - * \retval `LoRaMacStatus_t` The status of the operation. When the parameters are + * @return `lorawan_status_t` The status of the operation. When the parameters are * not valid, the function returns \ref LORAWAN_STATUS_PARAMETER_INVALID. * In case of a length error caused by the applicable payload in combination * with the MAC commands, the function returns \ref LORAWAN_STATUS_LENGTH_ERROR. @@ -116,192 +116,201 @@ public: * If the query is valid, and the LoRaMAC is able to send the frame, * the function returns \ref LORAWAN_STATUS_OK. */ - lorawan_status_t LoRaMacQueryTxPossible( uint8_t size, loramac_tx_info_t* txInfo ); + lorawan_status_t query_tx_possible(uint8_t size, loramac_tx_info_t* tx_info); - /*! - * \brief Adds a channel plan to the system. + /** + * @brief Adds a channel plan to the system. * - * \details Adds a whole channel plan or a single new channel to the. + * @details Adds a whole channel plan or a single new channel if the plan + * contains only one channel and 'plan.nb_channels' is set to 1. * Please note that this functionality is not available in all regions. * Information on the allowed ranges is available at the * LoRaWAN Regional Parameters V1.0.2rB. * - * \param plan [in] - A reference to application provided channel plan. + * @param plan [in] A reference to application provided channel plan. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t AddChannelPlan(const lorawan_channelplan_t& plan); + lorawan_status_t add_channel_plan(const lorawan_channelplan_t& plan); - /*! - * \brief Removes a channel plan from the system. + /** + * @brief Removes a channel plan from the system. * - * \details Removes the whole active channel plan except the 'Default Channels'.. + * @details Removes the whole active channel plan except the 'Default Channels'. * Please note that this functionality is not available in all regions. * Information on the allowed ranges is available at the * LoRaWAN Regional Parameters V1.0.2rB. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t RemoveChannelPlan(); + lorawan_status_t remove_channel_plan(); - /*! - * \brief Access active channel plan. + /** + * @brief Access active channel plan. * - * \details Provides access to the current active channel plan. + * @details Provides access to the current active channel plan. * - * \param plan [out] - A reference to application provided channel plan data - * structure which will be filled in with active channel - * plan. + * @param plan [out] A reference to application provided channel plan data + * structure which will be filled in with active channel + * plan. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t GetChannelPlan(lorawan_channelplan_t& plan); + lorawan_status_t get_channel_plan(lorawan_channelplan_t& plan); - /*! - * \brief Remove a given channel from the active plan. + /** + * @brief Remove a given channel from the active plan. * - * \details Deactivates the given channel. + * @details Deactivates the given channel. * - * \param id - Id of the channel. + * @param id Id of the channel. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t RemoveSingleChannel( uint8_t id ); + lorawan_status_t remove_single_channel(uint8_t id); - /*! - * \brief LoRaMAC multicast channel link service. + /** + * @brief LoRaMAC multicast channel link service. * - * \details Links a multicast channel into the linked list. + * @details Links a multicast channel into the linked list. * - * \param [in] channelParam - The multicast channel parameters to link. + * @param [in] channel_param The multicast channel parameters to link. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t LoRaMacMulticastChannelLink( multicast_params_t *channelParam ); + lorawan_status_t multicast_channel_link(multicast_params_t *channel_param); - /*! - * \brief LoRaMAC multicast channel unlink service. + /** + * @brief LoRaMAC multicast channel unlink service. * - * \details Unlinks a multicast channel from the linked list. + * @details Unlinks a multicast channel from the linked list. * - * \param [in] channelParam - The multicast channel parameters to unlink. + * @param [in] channel_param The multicast channel parameters to unlink. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t LoRaMacMulticastChannelUnlink( multicast_params_t *channelParam ); + lorawan_status_t multicast_channel_unlink(multicast_params_t *channel_param); - /*! - * \brief LoRaMAC MIB-GET. + /** + * @brief Get parameter values from MIB service. * - * \details The MAC information base service to get the attributes of the LoRaMac layer. + * @details The MAC information base service to get the attributes of the LoRaMac layer. * * The following code-snippet shows how to use the API to get the * parameter `AdrEnable`, defined by the enumeration type * \ref MIB_ADR. - * \code - * MibRequestConfirm_t mibReq; - * mibReq.Type = MIB_ADR; * - * if( LoRaMacMibGetRequestConfirm( &mibReq ) == LORAWAN_STATUS_OK ) - * { + * @code + * + * loramac_mib_req_confirm_t mib_get; + * mib_get.type = MIB_ADR; + * + * if (mib_get_request_confirm(&mib_get) == LORAWAN_STATUS_OK) { * // LoRaMAC updated the parameter mibParam.AdrEnable * } - * \endcode * - * \param [in] mibGet - The MIB-GET request to perform. Refer to \ref loramac_mib_req_confirm_t. + * @endcode * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @param [in] mib_get The MIB-GET request to perform. Refer to + * \ref loramac_mib_req_confirm_t. + * + * @return `lorawan_status_t` The status of the operation. + * The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_SERVICE_UNKNOWN * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t LoRaMacMibGetRequestConfirm( loramac_mib_req_confirm_t *mibGet ); + lorawan_status_t mib_get_request_confirm(loramac_mib_req_confirm_t *mib_get); - /*! - * \brief LoRaMAC MIB-SET. + /** + * @brief Set attributes for MAC layer using MIB service. * - * \details The MAC information base service to set the attributes of the LoRaMac layer. + * @details The MAC information base service to set the attributes of the LoRaMac layer. * * The following code-snippet shows how to use the API to set the - * parameter `AdrEnable`, defined by the enumeration type + * parameter `adr_enable`, defined by the enumeration type * \ref MIB_ADR. * - * \code - * MibRequestConfirm_t mibReq; - * mibReq.Type = MIB_ADR; - * mibReq.Param.AdrEnable = true; + * @code * - * if( LoRaMacMibGetRequestConfirm( &mibReq ) == LORAWAN_STATUS_OK ) - * { + * loramac_mib_req_confirm_t mib_set; + * mib_set.Type = MIB_ADR; + * mib_set.param.adr_enable = true; + * + * if (mib_set_request_confirm(&mib_set) == LORAWAN_STATUS_OK) { * // LoRaMAC updated the parameter * } - * \endcode * - * \param [in] mibSet - The MIB-SET request to perform. Refer to \ref loramac_mib_req_confirm_t. + * @endcode * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @param [in] mib_set The MIB-SET request to perform. Refer to + * \ref loramac_mib_req_confirm_t. + * + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_SERVICE_UNKNOWN * \ref LORAWAN_STATUS_PARAMETER_INVALID */ - lorawan_status_t LoRaMacMibSetRequestConfirm( loramac_mib_req_confirm_t *mibSet ); + lorawan_status_t mib_set_request_confirm(loramac_mib_req_confirm_t *mib_set); - /*! - * \brief LoRaMAC MLME request + /** + * @brief Set forth an MLME request. * - * \details The MAC layer management entity handles the management services. The + * @details The MAC layer management entity handles the management services. The * following code-snippet shows how to use the API to perform a * network join request. * - * \code - * static uint8_t DevEui[] = + * @code + * + * static uint8_t dev_eui[] = * { * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 * }; - * static uint8_t AppEui[] = + * static uint8_t app_eui[] = * { * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 * }; - * static uint8_t AppKey[] = + * static uint8_t app_key[] = * { * 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, * 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C * }; * - * MlmeReq_t mlmeReq; - * mlmeReq.Type = MLME_JOIN; - * mlmeReq.Req.Join.DevEui = DevEui; - * mlmeReq.Req.Join.AppEui = AppEui; - * mlmeReq.Req.Join.AppKey = AppKey; + * loramac_mlme_req_t mlme_req; + * mlme_req.Type = MLME_JOIN; + * mlme_req.req.join.dev_eui = dev_eui; + * mlme_req.req.join.app_eui = app_eui; + * mlme_req.req.join.app_key = app_key; * - * if( LoRaMacMlmeRequest( &mlmeReq ) == LORAWAN_STATUS_OK ) - * { + * if (LoRaMacMlmeRequest(&mlme_req) == LORAWAN_STATUS_OK) { * // Service started successfully. Waiting for the Mlme-Confirm event * } - * \endcode * - * \param [in] mlmeRequest - The MLME request to perform. Refer to \ref loramac_mlme_req_t. + * @endcode * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @param [in] request The MLME request to perform. + * Refer to \ref loramac_mlme_req_t. + * + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_SERVICE_UNKNOWN @@ -310,33 +319,35 @@ public: * \ref LORAWAN_STATUS_LENGTH_ERROR * \ref LORAWAN_STATUS_DEVICE_OFF */ - lorawan_status_t LoRaMacMlmeRequest( loramac_mlme_req_t *mlmeRequest ); + lorawan_status_t mlme_request(loramac_mlme_req_t *request); - /*! - * \brief LoRaMAC MCPS request + /** + * @brief Set forth an MCPS request. * - * \details The MAC Common Part Sublayer handles the data services. The following + * @details The MAC Common Part Sublayer handles the data services. The following * code-snippet shows how to use the API to send an unconfirmed * LoRaMAC frame. * - * \code - * uint8_t myBuffer[] = { 1, 2, 3 }; + * @code * - * McpsReq_t mcpsReq; - * mcpsReq.Type = MCPS_UNCONFIRMED; - * mcpsReq.Req.Unconfirmed.fPort = 1; - * mcpsReq.Req.Unconfirmed.fBuffer = myBuffer; - * mcpsReq.Req.Unconfirmed.fBufferSize = sizeof( myBuffer ); + * uint8_t buffer[] = {1, 2, 3}; * - * if( LoRaMacMcpsRequest( &mcpsReq ) == LORAWAN_STATUS_OK ) - * { + * loramac_mcps_req_t request; + * request.type = MCPS_UNCONFIRMED; + * request.req.unconfirmed.fport = 1; + * request.req.unconfirmed.f_buffer = buffer; + * request.req.unconfirmed.f_buffer_size = sizeof(buffer); + * + * if (mcps_request(&request) == LORAWAN_STATUS_OK) { * // Service started successfully. Waiting for the MCPS-Confirm event * } - * \endcode * - * \param [in] mcpsRequest - The MCPS request to perform. Refer to \ref loramac_mcps_req_t. + * @endcode * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * @param [in] request The MCPS request to perform. + * Refer to \ref loramac_mcps_req_t. + * + * @return `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_BUSY * \ref LORAWAN_STATUS_SERVICE_UNKNOWN @@ -345,65 +356,76 @@ public: * \ref LORAWAN_STATUS_LENGTH_ERROR * \ref LORAWAN_STATUS_DEVICE_OFF */ - lorawan_status_t LoRaMacMcpsRequest( loramac_mcps_req_t *mcpsRequest ); + lorawan_status_t mcps_request(loramac_mcps_req_t *request); /** - * \brief LoRaMAC layer provides its callback functions for - * PHY layer + * @brief LoRaMAC layer provides its callback functions for + * PHY layer. * - * \return Pointer to callback functions for radio events + * @return Pointer to callback functions for radio events */ - radio_events_t *GetPhyEventHandlers(); + radio_events_t *get_phy_event_handlers(); - /*! - * \brief Configures the events to trigger an MLME-Indication with + /** + * @brief Configures the events to trigger an MLME-Indication with * a MLME type of MLME_SCHEDULE_UPLINK. */ - void SetMlmeScheduleUplinkIndication( void ); + void set_mlme_schedule_ul_indication(void); - /*! - * \brief LoRaMAC layer generic send frame + /** + * @brief Schedules the frame for sending. * - * \param [in] macHdr MAC header field - * \param [in] fPort MAC payload port - * \param [in] fBuffer MAC data buffer to be sent - * \param [in] fBufferSize MAC data buffer size - * \retval status Status of the operation. + * @details Prepares a full MAC frame and schedules it for physical + * transmission. + * + * @param [in] mac_hdr MAC frame header field + * @param [in] fport Payload port + * @param [in] fbuffer MAC frame data buffer to be sent + * @param [in] fbuffer_size MAC frame data buffer size + * + * @return status Status of the operation. LORAWAN_STATUS_OK in case + * of success and a negative error code in case of + * failure. */ - lorawan_status_t Send( loramac_mhdr_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize ); + lorawan_status_t send(loramac_mhdr_t *mac_hdr, uint8_t fport, void *fbuffer, + uint16_t fbuffer_size); - /*! - * \brief Sets the radio in continuous transmission mode + /** + * @brief Puts the system in continuous transmission mode * - * \remark Uses the radio parameters set on the previous transmission. + * @remark Uses the radio parameters set on the previous transmission. * - * \param [in] timeout Time in seconds while the radio is kept in continuous wave mode - * \retval status Status of the operation. + * @param [in] timeout Time in seconds while the radio is kept in continuous wave mode + * + * @return status Status of the operation. LORAWAN_STATUS_OK in case + * of success and a negative error code in case of + * failure. */ - lorawan_status_t SetTxContinuousWave( uint16_t timeout ); + lorawan_status_t set_tx_continuous_wave(uint16_t timeout); - /*! - * \brief Sets the radio in continuous transmission mode + /** + * @brief Puts the system in continuous transmission mode * - * \remark Uses the radio parameters set on the previous transmission. + * @param [in] timeout Time in seconds while the radio is kept in continuous wave mode + * @param [in] frequency RF frequency to be set. + * @param [in] power RF output power to be set. * - * \param [in] timeout Time in seconds while the radio is kept in continuous wave mode - * \param [in] frequency RF frequency to be set. - * \param [in] power RF output power to be set. - * \retval status Status of the operation. + * @return status Status of the operation. LORAWAN_STATUS_OK in case + * of success and a negative error code in case of + * failure. */ - lorawan_status_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power ); + lorawan_status_t set_tx_continuous_wave1(uint16_t timeout, uint32_t frequency, uint8_t power); - /*! - * \brief Resets MAC specific parameters to default + /** + * @brief Resets MAC specific parameters to default */ - void ResetMacParameters( void ); + void reset_mac_parameters(void); - /*! - * \brief Opens up a continuous RX 2 window. This is used for + /** + * @brief Opens up a continuous RX 2 window. This is used for * class c devices. */ - void OpenContinuousRx2Window(void); + void open_continuous_rx2_window(void); #if defined(LORAWAN_COMPLIANCE_TEST) public: // Test interface @@ -415,7 +437,7 @@ public: // Test interface * * \param [in] NextTxTime - Periodic time for next uplink. - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * \retval `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_PARAMETER_INVALID */ @@ -426,7 +448,7 @@ public: // Test interface * * \details Stops the next tx timer. * - * \retval `LoRaMacStatus_t` The status of the operation. The possible values are: + * \retval `lorawan_status_t` The status of the operation. The possible values are: * \ref LORAWAN_STATUS_OK * \ref LORAWAN_STATUS_PARAMETER_INVALID */ @@ -480,138 +502,103 @@ private: #endif private: - /*! - * \brief Function to be executed on Radio Tx Done event + /** + * Function to be executed on Radio Tx Done event */ - void OnRadioTxDone( void ); - - /*! - * \brief This function prepares the MAC to abort the execution of function - * OnRadioRxDone in case of a reception error. - */ - void PrepareRxDoneAbort( void ); - - /*! - * \brief Function to be executed on Radio Rx Done event - */ - void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); - - /*! - * \brief Function executed on Radio Tx Timeout event - */ - void OnRadioTxTimeout( void ); - - /*! - * \brief Function executed on Radio Rx error event - */ - void OnRadioRxError( void ); - - /*! - * \brief Function executed on Radio Rx Timeout event - */ - void OnRadioRxTimeout( void ); - - /*! - * \brief Function executed on Resend Frame timer event. - */ - void OnMacStateCheckTimerEvent( void ); - - /*! - * \brief Function executed on duty cycle delayed Tx timer event - */ - void OnTxDelayedTimerEvent( void ); + void on_radio_tx_done(void); /** - * \brief Function to be executed when next Tx is possible + * This function prepares the MAC to abort the execution of function + * on_radio_rx_done() in case of a reception error. */ - void OnNextTx( void ); + void prepare_rx_done_abort(void); - /*! - * \brief Function executed on first Rx window timer event + /** + * Function to be executed on Radio Rx Done event */ - void OnRxWindow1TimerEvent( void ); + void on_radio_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, + int8_t snr); - /*! - * \brief Function executed on second Rx window timer event + /** + * Function executed on Radio Tx Timeout event */ - void OnRxWindow2TimerEvent( void ); + void on_radio_tx_timeout(void); - /*! - * \brief Function executed on AckTimeout timer event + /** + * Function executed on Radio Rx error event */ - void OnAckTimeoutTimerEvent( void ); + void on_radio_rx_error(void); - /*! - * \brief Initializes and opens the reception window - * - * \param [in] rxContinuous Set to true, if the RX is in continuous mode - * \param [in] maxRxWindow Maximum RX window timeout + /** + * Function executed on Radio Rx Timeout event */ - void RxWindowSetup( bool rxContinuous, uint32_t maxRxWindow ); + void on_radio_rx_timeout(void); - /*! - * \brief Validates if the payload fits into the frame, taking the datarate - * into account. - * - * \details Refer to chapter 4.3.2 of the LoRaWAN specification, v1.0 - * - * \param lenN Length of the application payload. The length depends on the - * datarate and is region specific - * - * \param datarate Current datarate - * - * \param fOptsLen Length of the fOpts field - * - * \retval [false: payload does not fit into the frame, true: payload fits into - * the frame] + /** + *Function executed on Resend Frame timer event. */ - bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen ); + void on_mac_state_check_timer_event(void); - /*! - * \brief LoRaMAC layer frame buffer initialization - * - * \param [in] macHdr MAC header field - * \param [in] fCtrl MAC frame control field - * \param [in] fPort MAC payload port - * \param [in] fBuffer MAC data buffer to be sent - * \param [in] fBufferSize MAC data buffer size - * \retval status Status of the operation. + /** + * Function executed on duty cycle delayed Tx timer event */ - lorawan_status_t PrepareFrame( loramac_mhdr_t *macHdr, loramac_frame_ctrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize ); + void on_tx_delayed_timer_event(void); - /* - * \brief Schedules the frame according to the duty cycle - * - * \retval Status of the operation + /** + * Function executed on first Rx window timer event */ - lorawan_status_t ScheduleTx( void ); + void on_rx_window1_timer_event(void); - /* - * \brief Calculates the back-off time for the band of a channel. - * - * \param [in] channel The last Tx channel index + /** + * Function executed on second Rx window timer event */ - void CalculateBackOff( uint8_t channel ); + void on_rx_window2_timer_event(void); - /*! - * \brief LoRaMAC layer prepared frame buffer transmission with channel specification - * - * \remark PrepareFrame must be called at least once before calling this - * function. - * - * \param [in] channel Channel to transmit on - * \retval status Status of the operation. + /** + * Function executed on AckTimeout timer event */ - lorawan_status_t SendFrameOnChannel( uint8_t channel ); + void on_ack_timeout_timer_event(void); - /*! - * \brief Resets MAC specific parameters to default - * - * \param [in] fPort The fPort - * - * \retval [false: fPort not allowed, true: fPort allowed] + /** + * Initializes and opens the reception window */ - bool IsFPortAllowed( uint8_t fPort ); + void rx_window_setup(bool rx_continuous, uint32_t max_rx_window_time); + + /** + * Validates if the payload fits into the frame, taking the datarate + * into account. + * + * Please Refer to chapter 4.3.2 of the LoRaWAN specification, v1.0.2 + */ + bool validate_payload_length(uint8_t length, int8_t datarate, uint8_t fopts_len); + + /** + * Prepares MAC frame on the behest of send() API. + */ + lorawan_status_t prepare_frame(loramac_mhdr_t *mac_hdr, + loramac_frame_ctrl_t *fctrl, uint8_t fport, + void *fbuffer, uint16_t fbuffer_size); + + /** + * Schedules a transmission on the behest of send() API. + */ + lorawan_status_t schedule_tx(void); + + /** + * Calculates the back-off time for the band of a channel. + * Takes in the last used channel id as a parameter. + */ + void calculate_backOff(uint8_t channel_id); + + /** + * Hands over the MAC frame to PHY layer. + */ + lorawan_status_t send_frame_on_channel(uint8_t channel); + + /** + * Checks for Port validity. + */ + bool is_fPort_allowed(uint8_t fPort); /** * Prototypes for ISR handlers @@ -660,7 +647,7 @@ private: /** * Channel planning subsystem */ - LoRaMacChannelPlan ch_plan; + LoRaMacChannelPlan channel_plan; /** * Timer subsystem handle @@ -675,12 +662,12 @@ private: /** * Radio event callback handlers for MAC */ - radio_events_t RadioEvents; + radio_events_t radio_events; - /*! + /** * LoRaMac upper layer event functions */ - loramac_primitives_t *LoRaMacPrimitives; + loramac_primitives_t *mac_primitives; /** * EventQueue object storage @@ -688,4 +675,4 @@ private: events::EventQueue *ev_queue; }; -#endif // __LORAMAC_H__ +#endif // MBED_LORAWAN_MAC_H__ diff --git a/features/lorawan/lorastack/mac/LoRaMacCommand.cpp b/features/lorawan/lorastack/mac/LoRaMacCommand.cpp index 7d834216cd..0fc3356de3 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCommand.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacCommand.cpp @@ -33,183 +33,167 @@ SPDX-License-Identifier: BSD-3-Clause #define tr_error(...) (void(0)) //dummies if feature common pal is not added #endif //defined(FEATURE_COMMON_PAL) -/*! +/** * LoRaMAC max EIRP (dBm) table. */ -static const uint8_t LoRaMacMaxEirpTable[] = { 8, 10, 12, 13, 14, 16, 18, 20, 21, 24, 26, 27, 29, 30, 33, 36 }; +static const uint8_t max_eirp_table[] = { 8, 10, 12, 13, 14, 16, 18, 20, 21, 24, 26, 27, 29, 30, 33, 36 }; LoRaMacCommand::LoRaMacCommand(LoRaMac& lora_mac) : _lora_mac(lora_mac) { - MacCommandsInNextTx = false; - MacCommandsBufferIndex = 0; - MacCommandsBufferToRepeatIndex = 0; + mac_cmd_in_next_tx = false; + mac_cmd_buf_idx = 0; + mac_cmd_buf_idx_to_repeat = 0; - memset(MacCommandsBuffer, 0, sizeof(MacCommandsBuffer)); - memset(MacCommandsBufferToRepeat, 0, sizeof(MacCommandsBufferToRepeat)); + memset(mac_cmd_buffer, 0, sizeof(mac_cmd_buffer)); + memset(mac_cmd_buffer_to_repeat, 0, sizeof(mac_cmd_buffer_to_repeat)); } LoRaMacCommand::~LoRaMacCommand() { } -lorawan_status_t LoRaMacCommand::AddMacCommand(uint8_t cmd, uint8_t p1, uint8_t p2) +lorawan_status_t LoRaMacCommand::add_mac_command(uint8_t cmd, uint8_t p1, + uint8_t p2) { lorawan_status_t status = LORAWAN_STATUS_BUSY; // The maximum buffer length must take MAC commands to re-send into account. - const uint8_t bufLen = LORA_MAC_COMMAND_MAX_LENGTH - MacCommandsBufferToRepeatIndex; + const uint8_t bufLen = LORA_MAC_COMMAND_MAX_LENGTH + - mac_cmd_buf_idx_to_repeat; - switch( cmd ) - { + switch (cmd) { case MOTE_MAC_LINK_CHECK_REQ: - if( MacCommandsBufferIndex < bufLen ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < bufLen) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // No payload for this command status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_LINK_ADR_ANS: - if( MacCommandsBufferIndex < ( bufLen - 1 ) ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < (bufLen - 1)) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // Margin - MacCommandsBuffer[MacCommandsBufferIndex++] = p1; + mac_cmd_buffer[mac_cmd_buf_idx++] = p1; status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_DUTY_CYCLE_ANS: - if( MacCommandsBufferIndex < bufLen ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < bufLen) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // No payload for this answer status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_RX_PARAM_SETUP_ANS: - if( MacCommandsBufferIndex < ( bufLen - 1 ) ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < (bufLen - 1)) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // Status: Datarate ACK, Channel ACK - MacCommandsBuffer[MacCommandsBufferIndex++] = p1; + mac_cmd_buffer[mac_cmd_buf_idx++] = p1; // This is a sticky MAC command answer. Setup indication - _lora_mac.SetMlmeScheduleUplinkIndication(); + _lora_mac.set_mlme_schedule_ul_indication(); status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_DEV_STATUS_ANS: - if( MacCommandsBufferIndex < ( bufLen - 2 ) ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < (bufLen - 2)) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // 1st byte Battery // 2nd byte Margin - MacCommandsBuffer[MacCommandsBufferIndex++] = p1; - MacCommandsBuffer[MacCommandsBufferIndex++] = p2; + mac_cmd_buffer[mac_cmd_buf_idx++] = p1; + mac_cmd_buffer[mac_cmd_buf_idx++] = p2; status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_NEW_CHANNEL_ANS: - if( MacCommandsBufferIndex < ( bufLen - 1 ) ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < (bufLen - 1)) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // Status: Datarate range OK, Channel frequency OK - MacCommandsBuffer[MacCommandsBufferIndex++] = p1; + mac_cmd_buffer[mac_cmd_buf_idx++] = p1; status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_RX_TIMING_SETUP_ANS: - if( MacCommandsBufferIndex < bufLen ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < bufLen) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // No payload for this answer // This is a sticky MAC command answer. Setup indication - _lora_mac.SetMlmeScheduleUplinkIndication(); + _lora_mac.set_mlme_schedule_ul_indication(); status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_TX_PARAM_SETUP_ANS: - if( MacCommandsBufferIndex < bufLen ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < bufLen) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // No payload for this answer status = LORAWAN_STATUS_OK; } break; case MOTE_MAC_DL_CHANNEL_ANS: - if( MacCommandsBufferIndex < bufLen ) - { - MacCommandsBuffer[MacCommandsBufferIndex++] = cmd; + if (mac_cmd_buf_idx < bufLen) { + mac_cmd_buffer[mac_cmd_buf_idx++] = cmd; // Status: Uplink frequency exists, Channel frequency OK - MacCommandsBuffer[MacCommandsBufferIndex++] = p1; + mac_cmd_buffer[mac_cmd_buf_idx++] = p1; // This is a sticky MAC command answer. Setup indication - _lora_mac.SetMlmeScheduleUplinkIndication(); + _lora_mac.set_mlme_schedule_ul_indication(); status = LORAWAN_STATUS_OK; } break; default: return LORAWAN_STATUS_SERVICE_UNKNOWN; } - if( status == LORAWAN_STATUS_OK ) - { - MacCommandsInNextTx = true; + if (status == LORAWAN_STATUS_OK) { + mac_cmd_in_next_tx = true; } return status; } -void LoRaMacCommand::ClearCommandBuffer() +void LoRaMacCommand::clear_command_buffer() { - MacCommandsBufferIndex = 0; + mac_cmd_buf_idx = 0; } -uint8_t LoRaMacCommand::GetLength() const +uint8_t LoRaMacCommand::get_mac_cmd_length() const { - return MacCommandsBufferIndex; + return mac_cmd_buf_idx; } -uint8_t *LoRaMacCommand::GetMacCommandsBuffer() +uint8_t *LoRaMacCommand::get_mac_commands_buffer() { - return MacCommandsBuffer; + return mac_cmd_buffer; } -void LoRaMacCommand::ParseMacCommandsToRepeat() +void LoRaMacCommand::parse_mac_commands_to_repeat() { uint8_t i = 0; - uint8_t cmdCount = 0; + uint8_t cmd_cnt = 0; - for( i = 0; i < MacCommandsBufferIndex; i++ ) - { - switch( MacCommandsBuffer[i] ) - { + for (i = 0; i < mac_cmd_buf_idx; i++) { + switch (mac_cmd_buffer[i]) { // STICKY case MOTE_MAC_DL_CHANNEL_ANS: - case MOTE_MAC_RX_PARAM_SETUP_ANS: - { // 1 byte payload - MacCommandsBufferToRepeat[cmdCount++] = MacCommandsBuffer[i++]; - MacCommandsBufferToRepeat[cmdCount++] = MacCommandsBuffer[i]; + case MOTE_MAC_RX_PARAM_SETUP_ANS: { // 1 byte payload + mac_cmd_buffer_to_repeat[cmd_cnt++] = mac_cmd_buffer[i++]; + mac_cmd_buffer_to_repeat[cmd_cnt++] = mac_cmd_buffer[i]; break; } - case MOTE_MAC_RX_TIMING_SETUP_ANS: - { // 0 byte payload - MacCommandsBufferToRepeat[cmdCount++] = MacCommandsBuffer[i]; + case MOTE_MAC_RX_TIMING_SETUP_ANS: { // 0 byte payload + mac_cmd_buffer_to_repeat[cmd_cnt++] = mac_cmd_buffer[i]; break; } - // NON-STICKY - case MOTE_MAC_DEV_STATUS_ANS: - { // 2 bytes payload + // NON-STICKY + case MOTE_MAC_DEV_STATUS_ANS: { // 2 bytes payload i += 2; break; } case MOTE_MAC_LINK_ADR_ANS: - case MOTE_MAC_NEW_CHANNEL_ANS: - { // 1 byte payload + case MOTE_MAC_NEW_CHANNEL_ANS: { // 1 byte payload i++; break; } case MOTE_MAC_TX_PARAM_SETUP_ANS: case MOTE_MAC_DUTY_CYCLE_ANS: - case MOTE_MAC_LINK_CHECK_REQ: - { // 0 byte payload + case MOTE_MAC_LINK_CHECK_REQ: { // 0 byte payload break; } default: @@ -217,213 +201,210 @@ void LoRaMacCommand::ParseMacCommandsToRepeat() } } - if( cmdCount > 0 ) { - MacCommandsInNextTx = true; + if (cmd_cnt > 0) { + mac_cmd_in_next_tx = true; } else { - MacCommandsInNextTx = false; + mac_cmd_in_next_tx = false; } } -void LoRaMacCommand::ClearRepeatBuffer() +void LoRaMacCommand::clear_repeat_buffer() { - MacCommandsBufferToRepeatIndex = 0; + mac_cmd_buf_idx_to_repeat = 0; } -void LoRaMacCommand::CopyRepeatCommandsToBuffer() +void LoRaMacCommand::copy_repeat_commands_to_buffer() { // Copy the MAC commands which must be re-send into the MAC command buffer - memcpy(&MacCommandsBuffer[MacCommandsBufferIndex], MacCommandsBufferToRepeat, MacCommandsBufferToRepeatIndex); - MacCommandsBufferIndex += MacCommandsBufferToRepeatIndex; + memcpy(&mac_cmd_buffer[mac_cmd_buf_idx], mac_cmd_buffer_to_repeat, mac_cmd_buf_idx_to_repeat); + mac_cmd_buf_idx += mac_cmd_buf_idx_to_repeat; } -uint8_t LoRaMacCommand::GetRepeatLength() const +uint8_t LoRaMacCommand::get_repeat_commands_length() const { - return MacCommandsBufferToRepeatIndex; + return mac_cmd_buf_idx_to_repeat; } -void LoRaMacCommand::ClearMacCommandsInNextTx() +void LoRaMacCommand::clear_mac_commands_in_next_tx() { - MacCommandsInNextTx = false; + mac_cmd_in_next_tx = false; } -bool LoRaMacCommand::IsMacCommandsInNextTx() const +bool LoRaMacCommand::is_mac_command_in_next_tx() const { - return MacCommandsInNextTx; + return mac_cmd_in_next_tx; } -lorawan_status_t LoRaMacCommand::ProcessMacCommands(uint8_t *payload, uint8_t macIndex, - uint8_t commandsSize, uint8_t snr, - loramac_mlme_confirm_t& MlmeConfirm, - lora_mac_system_params_t &LoRaMacParams, - LoRaPHY &lora_phy) +lorawan_status_t LoRaMacCommand::process_mac_commands(uint8_t *payload, + uint8_t mac_index, + uint8_t commands_size, + uint8_t snr, + loramac_mlme_confirm_t& mlme_conf, + lora_mac_system_params_t &mac_sys_params, + LoRaPHY &lora_phy) { uint8_t status = 0; lorawan_status_t ret_value = LORAWAN_STATUS_OK; - while( macIndex < commandsSize ) - { + while (mac_index < commands_size) { // Decode Frame MAC commands - switch( payload[macIndex++] ) - { + switch (payload[mac_index++]) { case SRV_MAC_LINK_CHECK_ANS: - MlmeConfirm.status = LORAMAC_EVENT_INFO_STATUS_OK; - MlmeConfirm.demod_margin = payload[macIndex++]; - MlmeConfirm.nb_gateways = payload[macIndex++]; + mlme_conf.status = LORAMAC_EVENT_INFO_STATUS_OK; + mlme_conf.demod_margin = payload[mac_index++]; + mlme_conf.nb_gateways = payload[mac_index++]; break; - case SRV_MAC_LINK_ADR_REQ: - { - adr_req_params_t linkAdrReq; - int8_t linkAdrDatarate = DR_0; - int8_t linkAdrTxPower = TX_POWER_0; - uint8_t linkAdrNbRep = 0; - uint8_t linkAdrNbBytesParsed = 0; + case SRV_MAC_LINK_ADR_REQ: { + adr_req_params_t linkAdrReq; + int8_t linkAdrDatarate = DR_0; + int8_t linkAdrTxPower = TX_POWER_0; + uint8_t linkAdrNbRep = 0; + uint8_t linkAdrNbBytesParsed = 0; - // Fill parameter structure - linkAdrReq.payload = &payload[macIndex - 1]; - linkAdrReq.payload_size = commandsSize - ( macIndex - 1 ); - linkAdrReq.adr_enabled = LoRaMacParams.adr_on; - linkAdrReq.ul_dwell_time = LoRaMacParams.uplink_dwell_time; - linkAdrReq.current_datarate = LoRaMacParams.channel_data_rate; - linkAdrReq.current_tx_power = LoRaMacParams.channel_tx_power; - linkAdrReq.current_nb_rep = LoRaMacParams.retry_num; + // Fill parameter structure + linkAdrReq.payload = &payload[mac_index - 1]; + linkAdrReq.payload_size = commands_size - (mac_index - 1); + linkAdrReq.adr_enabled = mac_sys_params.adr_on; + linkAdrReq.ul_dwell_time = mac_sys_params.uplink_dwell_time; + linkAdrReq.current_datarate = mac_sys_params.channel_data_rate; + linkAdrReq.current_tx_power = mac_sys_params.channel_tx_power; + linkAdrReq.current_nb_rep = mac_sys_params.retry_num; - // Process the ADR requests - status = lora_phy.link_ADR_request(&linkAdrReq, &linkAdrDatarate, - &linkAdrTxPower, &linkAdrNbRep, - &linkAdrNbBytesParsed); + // Process the ADR requests + status = lora_phy.link_ADR_request(&linkAdrReq, + &linkAdrDatarate, + &linkAdrTxPower, + &linkAdrNbRep, + &linkAdrNbBytesParsed); - if( ( status & 0x07 ) == 0x07 ) - { - LoRaMacParams.channel_data_rate = linkAdrDatarate; - LoRaMacParams.channel_tx_power = linkAdrTxPower; - LoRaMacParams.retry_num = linkAdrNbRep; - } - - // Add the answers to the buffer - for( uint8_t i = 0; i < ( linkAdrNbBytesParsed / 5 ); i++ ) - { - ret_value = AddMacCommand( MOTE_MAC_LINK_ADR_ANS, status, 0 ); - } - // Update MAC index - macIndex += linkAdrNbBytesParsed - 1; + if ((status & 0x07) == 0x07) { + mac_sys_params.channel_data_rate = linkAdrDatarate; + mac_sys_params.channel_tx_power = linkAdrTxPower; + mac_sys_params.retry_num = linkAdrNbRep; } + + // Add the answers to the buffer + for (uint8_t i = 0; i < (linkAdrNbBytesParsed / 5); i++) { + ret_value = add_mac_command(MOTE_MAC_LINK_ADR_ANS, status, 0); + } + // Update MAC index + mac_index += linkAdrNbBytesParsed - 1; + } break; case SRV_MAC_DUTY_CYCLE_REQ: - LoRaMacParams.max_duty_cycle = payload[macIndex++]; - LoRaMacParams.aggregated_duty_cycle = 1 << LoRaMacParams.max_duty_cycle; - ret_value = AddMacCommand( MOTE_MAC_DUTY_CYCLE_ANS, 0, 0 ); + mac_sys_params.max_duty_cycle = payload[mac_index++]; + mac_sys_params.aggregated_duty_cycle = 1 << mac_sys_params.max_duty_cycle; + ret_value = add_mac_command(MOTE_MAC_DUTY_CYCLE_ANS, 0, 0); break; - case SRV_MAC_RX_PARAM_SETUP_REQ: - { - rx_param_setup_req_t rxParamSetupReq; + case SRV_MAC_RX_PARAM_SETUP_REQ: { + rx_param_setup_req_t rxParamSetupReq; - rxParamSetupReq.dr_offset = ( payload[macIndex] >> 4 ) & 0x07; - rxParamSetupReq.datarate = payload[macIndex] & 0x0F; - macIndex++; + rxParamSetupReq.dr_offset = (payload[mac_index] >> 4) & 0x07; + rxParamSetupReq.datarate = payload[mac_index] & 0x0F; + mac_index++; - rxParamSetupReq.frequency = ( uint32_t )payload[macIndex++]; - rxParamSetupReq.frequency |= ( uint32_t )payload[macIndex++] << 8; - rxParamSetupReq.frequency |= ( uint32_t )payload[macIndex++] << 16; - rxParamSetupReq.frequency *= 100; + rxParamSetupReq.frequency = (uint32_t) payload[mac_index++]; + rxParamSetupReq.frequency |= (uint32_t) payload[mac_index++] + << 8; + rxParamSetupReq.frequency |= (uint32_t) payload[mac_index++] + << 16; + rxParamSetupReq.frequency *= 100; - // Perform request on region - status = lora_phy.accept_rx_param_setup_req(&rxParamSetupReq); + // Perform request on region + status = lora_phy.accept_rx_param_setup_req(&rxParamSetupReq); - if( ( status & 0x07 ) == 0x07 ) - { - LoRaMacParams.rx2_channel.datarate = rxParamSetupReq.datarate; - LoRaMacParams.rx2_channel.frequency = rxParamSetupReq.frequency; - LoRaMacParams.rx1_dr_offset = rxParamSetupReq.dr_offset; - } - ret_value = AddMacCommand( MOTE_MAC_RX_PARAM_SETUP_ANS, status, 0 ); + if ((status & 0x07) == 0x07) { + mac_sys_params.rx2_channel.datarate = + rxParamSetupReq.datarate; + mac_sys_params.rx2_channel.frequency = + rxParamSetupReq.frequency; + mac_sys_params.rx1_dr_offset = rxParamSetupReq.dr_offset; } + ret_value = add_mac_command(MOTE_MAC_RX_PARAM_SETUP_ANS, status, + 0); + } break; - case SRV_MAC_DEV_STATUS_REQ: - { - uint8_t batteryLevel = BAT_LEVEL_NO_MEASURE; - // we don't have a mechanism at the moment to measure - // battery levels - ret_value = AddMacCommand( MOTE_MAC_DEV_STATUS_ANS, batteryLevel, snr ); - break; - } - case SRV_MAC_NEW_CHANNEL_REQ: - { - new_channel_req_params_t newChannelReq; - channel_params_t chParam; - - newChannelReq.channel_id = payload[macIndex++]; - newChannelReq.new_channel = &chParam; - - chParam.frequency = ( uint32_t )payload[macIndex++]; - chParam.frequency |= ( uint32_t )payload[macIndex++] << 8; - chParam.frequency |= ( uint32_t )payload[macIndex++] << 16; - chParam.frequency *= 100; - chParam.rx1_frequency = 0; - chParam.dr_range.value = payload[macIndex++]; - - status = lora_phy.request_new_channel(&newChannelReq); - - ret_value = AddMacCommand( MOTE_MAC_NEW_CHANNEL_ANS, status, 0 ); - } + case SRV_MAC_DEV_STATUS_REQ: { + uint8_t batteryLevel = BAT_LEVEL_NO_MEASURE; + // we don't have a mechanism at the moment to measure + // battery levels + ret_value = add_mac_command(MOTE_MAC_DEV_STATUS_ANS, + batteryLevel, snr); break; - case SRV_MAC_RX_TIMING_SETUP_REQ: - { - uint8_t delay = payload[macIndex++] & 0x0F; + } + case SRV_MAC_NEW_CHANNEL_REQ: { + new_channel_req_params_t newChannelReq; + channel_params_t chParam; - if( delay == 0 ) - { - delay++; - } - LoRaMacParams.recv_delay1 = delay * 1000; - LoRaMacParams.recv_delay2 = LoRaMacParams.recv_delay1 + 1000; - ret_value = AddMacCommand( MOTE_MAC_RX_TIMING_SETUP_ANS, 0, 0 ); - } + newChannelReq.channel_id = payload[mac_index++]; + newChannelReq.new_channel = &chParam; + + chParam.frequency = (uint32_t) payload[mac_index++]; + chParam.frequency |= (uint32_t) payload[mac_index++] << 8; + chParam.frequency |= (uint32_t) payload[mac_index++] << 16; + chParam.frequency *= 100; + chParam.rx1_frequency = 0; + chParam.dr_range.value = payload[mac_index++]; + + status = lora_phy.request_new_channel(&newChannelReq); + + ret_value = add_mac_command(MOTE_MAC_NEW_CHANNEL_ANS, status, 0); + } break; - case SRV_MAC_TX_PARAM_SETUP_REQ: - { - tx_param_setup_req_t txParamSetupReq; - uint8_t eirpDwellTime = payload[macIndex++]; + case SRV_MAC_RX_TIMING_SETUP_REQ: { + uint8_t delay = payload[mac_index++] & 0x0F; - txParamSetupReq.ul_dwell_time = 0; - txParamSetupReq.dl_dwell_time = 0; - - if( ( eirpDwellTime & 0x20 ) == 0x20 ) - { - txParamSetupReq.dl_dwell_time = 1; - } - if( ( eirpDwellTime & 0x10 ) == 0x10 ) - { - txParamSetupReq.ul_dwell_time = 1; - } - txParamSetupReq.max_eirp = eirpDwellTime & 0x0F; - - // Check the status for correctness - if(lora_phy.accept_tx_param_setup_req(&txParamSetupReq)) - { - // Accept command - LoRaMacParams.uplink_dwell_time = txParamSetupReq.ul_dwell_time; - LoRaMacParams.downlink_dwell_time = txParamSetupReq.dl_dwell_time; - LoRaMacParams.max_eirp = LoRaMacMaxEirpTable[txParamSetupReq.max_eirp]; - // Add command response - ret_value = AddMacCommand( MOTE_MAC_TX_PARAM_SETUP_ANS, 0, 0 ); - } + if (delay == 0) { + delay++; } + mac_sys_params.recv_delay1 = delay * 1000; + mac_sys_params.recv_delay2 = mac_sys_params.recv_delay1 + 1000; + ret_value = add_mac_command(MOTE_MAC_RX_TIMING_SETUP_ANS, 0, 0); + } break; - case SRV_MAC_DL_CHANNEL_REQ: - { - dl_channel_req_params_t dlChannelReq; + case SRV_MAC_TX_PARAM_SETUP_REQ: { + tx_param_setup_req_t txParamSetupReq; + uint8_t eirpDwellTime = payload[mac_index++]; - dlChannelReq.channel_id = payload[macIndex++]; - dlChannelReq.rx1_frequency = ( uint32_t )payload[macIndex++]; - dlChannelReq.rx1_frequency |= ( uint32_t )payload[macIndex++] << 8; - dlChannelReq.rx1_frequency |= ( uint32_t )payload[macIndex++] << 16; - dlChannelReq.rx1_frequency *= 100; + txParamSetupReq.ul_dwell_time = 0; + txParamSetupReq.dl_dwell_time = 0; - status = lora_phy.dl_channel_request(&dlChannelReq); - - ret_value = AddMacCommand( MOTE_MAC_DL_CHANNEL_ANS, status, 0 ); + if ((eirpDwellTime & 0x20) == 0x20) { + txParamSetupReq.dl_dwell_time = 1; } + if ((eirpDwellTime & 0x10) == 0x10) { + txParamSetupReq.ul_dwell_time = 1; + } + txParamSetupReq.max_eirp = eirpDwellTime & 0x0F; + + // Check the status for correctness + if (lora_phy.accept_tx_param_setup_req(&txParamSetupReq)) { + // Accept command + mac_sys_params.uplink_dwell_time = + txParamSetupReq.ul_dwell_time; + mac_sys_params.downlink_dwell_time = + txParamSetupReq.dl_dwell_time; + mac_sys_params.max_eirp = + max_eirp_table[txParamSetupReq.max_eirp]; + // Add command response + ret_value = add_mac_command(MOTE_MAC_TX_PARAM_SETUP_ANS, 0, 0); + } + } + break; + case SRV_MAC_DL_CHANNEL_REQ: { + dl_channel_req_params_t dlChannelReq; + + dlChannelReq.channel_id = payload[mac_index++]; + dlChannelReq.rx1_frequency = (uint32_t) payload[mac_index++]; + dlChannelReq.rx1_frequency |= (uint32_t) payload[mac_index++] << 8; + dlChannelReq.rx1_frequency |= (uint32_t) payload[mac_index++] << 16; + dlChannelReq.rx1_frequency *= 100; + + status = lora_phy.dl_channel_request(&dlChannelReq); + + ret_value = add_mac_command(MOTE_MAC_DL_CHANNEL_ANS, status, 0); + } break; default: // Unknown command. ABORT MAC commands processing @@ -433,10 +414,9 @@ lorawan_status_t LoRaMacCommand::ProcessMacCommands(uint8_t *payload, uint8_t ma return ret_value; } -bool LoRaMacCommand::IsStickyMacCommandPending() +bool LoRaMacCommand::is_sticky_mac_command_pending() { - if( MacCommandsBufferToRepeatIndex > 0 ) - { + if (mac_cmd_buf_idx_to_repeat > 0) { // Sticky MAC commands pending return true; } diff --git a/features/lorawan/lorastack/mac/LoRaMacCommand.h b/features/lorawan/lorastack/mac/LoRaMacCommand.h index 787df24e74..3aa54c633d 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCommand.h +++ b/features/lorawan/lorastack/mac/LoRaMacCommand.h @@ -51,129 +51,130 @@ class LoRaMac; -class LoRaMacCommand -{ +class LoRaMacCommand { + public: LoRaMacCommand(LoRaMac &lora_mac); ~LoRaMacCommand(); - /*! - * \brief Adds a new MAC command to be sent. + /** + * @brief Adds a new MAC command to be sent. * - * \remark MAC layer internal function + * @remark MAC layer internal function * - * \param [in] cmd MAC command to be added - * [MOTE_MAC_LINK_CHECK_REQ, - * MOTE_MAC_LINK_ADR_ANS, - * MOTE_MAC_DUTY_CYCLE_ANS, - * MOTE_MAC_RX2_PARAM_SET_ANS, - * MOTE_MAC_DEV_STATUS_ANS - * MOTE_MAC_NEW_CHANNEL_ANS] - * \param [in] p1 1st parameter ( optional depends on the command ) - * \param [in] p2 2nd parameter ( optional depends on the command ) + * @param [in] cmd MAC command to be added + * [MOTE_MAC_LINK_CHECK_REQ, + * MOTE_MAC_LINK_ADR_ANS, + * MOTE_MAC_DUTY_CYCLE_ANS, + * MOTE_MAC_RX2_PARAM_SET_ANS, + * MOTE_MAC_DEV_STATUS_ANS + * MOTE_MAC_NEW_CHANNEL_ANS] + * @param [in] p1 1st parameter (optional depends on the command) + * @param [in] p2 2nd parameter (optional depends on the command) * - * \retval status Function status [0: OK, 1: Unknown command, 2: Buffer full] + * @return status Function status [0: OK, 1: Unknown command, 2: Buffer full] */ - lorawan_status_t AddMacCommand(uint8_t cmd, uint8_t p1, uint8_t p2); + lorawan_status_t add_mac_command(uint8_t cmd, uint8_t p1, uint8_t p2); - /*! - * \brief Clear MAC command buffer. + /** + * @brief Clear MAC command buffer. */ - void ClearCommandBuffer(); + void clear_command_buffer(void); - /*! - * \brief Get the length of MAC commands + /** + * @brief Get the length of MAC commands * - * \retval status Length of used MAC buffer (bytes) + * @return status Length of used MAC buffer (bytes) */ - uint8_t GetLength() const; + uint8_t get_mac_cmd_length() const; - /*! - * \brief Get MAC command buffer + /** + * @brief Get MAC command buffer * - * \retval Pointer to MAC command buffer + * @return Pointer to MAC command buffer */ - uint8_t *GetMacCommandsBuffer(); + uint8_t *get_mac_commands_buffer(); - /*! - * \brief Parses the MAC commands which must be resent. + /** + * @brief Parses the MAC commands which must be resent. */ - void ParseMacCommandsToRepeat(); + void parse_mac_commands_to_repeat(); - /*! - * \brief Clear MAC command repeat buffer. + /** + * @brief Clear MAC command repeat buffer. */ - void ClearRepeatBuffer(); + void clear_repeat_buffer(); - /*! - * \brief Copy MAC commands from repeat buffer to actual MAC command buffer. + /** + * @brief Copy MAC commands from repeat buffer to actual MAC command buffer. */ - void CopyRepeatCommandsToBuffer(); + void copy_repeat_commands_to_buffer(); - /*! - * \brief Get the length of MAC commands in repeat buffer + /** + * @brief Get the length of MAC commands in repeat buffer * - * \retval status Length of used MAC Repeat buffer (bytes) + * @return status Length of used MAC Repeat buffer (bytes) */ - uint8_t GetRepeatLength() const; + uint8_t get_repeat_commands_length() const; - /*! - * \brief Clear MAC commands in next TX. + /** + * @brief Clear MAC commands in next TX. */ - void ClearMacCommandsInNextTx(); + void clear_mac_commands_in_next_tx(); - /*! - * \brief Check if MAC command buffer has commands to be sent in next TX + /** + * @brief Check if MAC command buffer has commands to be sent in next TX * - * \retval status True: buffer has MAC commands to be sent, false: no commands in buffer] + * @return status True: buffer has MAC commands to be sent, false: no commands in buffer] */ - bool IsMacCommandsInNextTx() const; + bool is_mac_command_in_next_tx() const; - /*! - * \brief Decodes MAC commands in the fOpts field and in the payload - - * \retval status Function status. LORAWAN_STATUS_OK if command successful. - */ - lorawan_status_t ProcessMacCommands(uint8_t *payload, uint8_t macIndex, - uint8_t commandsSize, uint8_t snr, - loramac_mlme_confirm_t& MlmeConfirm, - lora_mac_system_params_t& LoRaMacParams, - LoRaPHY& lora_phy); - - /*! - * \brief Verifies if sticky MAC commands are pending. + /** + * @brief Decodes MAC commands in the fOpts field and in the payload * - * \retval [true: sticky MAC commands pending, false: No MAC commands pending] + * @return status Function status. LORAWAN_STATUS_OK if command successful. */ - bool IsStickyMacCommandPending(); + lorawan_status_t process_mac_commands(uint8_t *payload, uint8_t mac_index, + uint8_t commands_size, uint8_t snr, + loramac_mlme_confirm_t& mlme_conf, + lora_mac_system_params_t& mac_params, + LoRaPHY& lora_phy); + + /** + * @brief Verifies if sticky MAC commands are pending. + * + * @return [true: sticky MAC commands pending, false: No MAC commands pending] + */ + bool is_sticky_mac_command_pending(); private: LoRaMac& _lora_mac; - /*! + /** * Indicates if the MAC layer wants to send MAC commands */ - bool MacCommandsInNextTx; + bool mac_cmd_in_next_tx; - /*! - * Contains the current MacCommandsBuffer index + /** + * Contains the current Mac command buffer index in 'mac_cmd_buffer' */ - uint8_t MacCommandsBufferIndex; + uint8_t mac_cmd_buf_idx; - /*! - * Contains the current MacCommandsBuffer index for MAC commands to repeat + /** + * Contains the current Mac command buffer index for MAC commands to repeat in + * 'mac_cmd_buffer_to_repeat' */ - uint8_t MacCommandsBufferToRepeatIndex; + uint8_t mac_cmd_buf_idx_to_repeat; - /*! + /** * Buffer containing the MAC layer commands */ - uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH]; + uint8_t mac_cmd_buffer[LORA_MAC_COMMAND_MAX_LENGTH]; - /*! + /** * Buffer containing the MAC layer commands which must be repeated */ - uint8_t MacCommandsBufferToRepeat[LORA_MAC_COMMAND_MAX_LENGTH]; + uint8_t mac_cmd_buffer_to_repeat[LORA_MAC_COMMAND_MAX_LENGTH]; }; #endif //__LORAMACCOMMAND_H__ diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp index 5919c37a6f..75deb84197 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp @@ -41,251 +41,253 @@ /** * MIC field computation initial data */ -static uint8_t MicBlockB0[] = { 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; +static uint8_t mic_block_b0[] = {0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /** * Contains the computed MIC field. * * \remark Only the 4 first bytes are used */ -static uint8_t Mic[16]; +static uint8_t computed_mic[16]; /** * Encryption aBlock and sBlock */ -static uint8_t aBlock[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; -static uint8_t sBlock[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; +static uint8_t a_block[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +static uint8_t s_block[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /** * AES computation context variable */ -static mbedtls_aes_context AesContext; +static mbedtls_aes_context aes_ctx; /** * CMAC computation context variable */ -static mbedtls_cipher_context_t AesCmacCtx[1]; +static mbedtls_cipher_context_t aes_cmac_ctx[1]; #define AES_CMAC_KEY_LENGTH 16 -/** - * \brief Computes the LoRaMAC frame MIC field - * - * \param [in] buffer Data buffer - * \param [in] size Data buffer size - * \param [in] key AES key to be used - * \param [in] address Frame address - * \param [in] dir Frame direction [0: uplink, 1: downlink] - * \param [in] sequenceCounter Frame sequence counter - * \param [out] mic Computed MIC field - */ -int LoRaMacComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic ) +int compute_mic(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint32_t *mic) { int ret = 0; - MicBlockB0[5] = dir; + mic_block_b0[5] = dir; - MicBlockB0[6] = ( address ) & 0xFF; - MicBlockB0[7] = ( address >> 8 ) & 0xFF; - MicBlockB0[8] = ( address >> 16 ) & 0xFF; - MicBlockB0[9] = ( address >> 24 ) & 0xFF; + mic_block_b0[6] = (address) & 0xFF; + mic_block_b0[7] = (address >> 8) & 0xFF; + mic_block_b0[8] = (address >> 16) & 0xFF; + mic_block_b0[9] = (address >> 24) & 0xFF; - MicBlockB0[10] = ( sequenceCounter ) & 0xFF; - MicBlockB0[11] = ( sequenceCounter >> 8 ) & 0xFF; - MicBlockB0[12] = ( sequenceCounter >> 16 ) & 0xFF; - MicBlockB0[13] = ( sequenceCounter >> 24 ) & 0xFF; + mic_block_b0[10] = (seq_counter) & 0xFF; + mic_block_b0[11] = (seq_counter >> 8) & 0xFF; + mic_block_b0[12] = (seq_counter >> 16) & 0xFF; + mic_block_b0[13] = (seq_counter >> 24) & 0xFF; - MicBlockB0[15] = size & 0xFF; + mic_block_b0[15] = size & 0xFF; + + mbedtls_cipher_init(aes_cmac_ctx); - mbedtls_cipher_init(AesCmacCtx); const mbedtls_cipher_info_t* cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB); + if (NULL != cipher_info) { - ret = mbedtls_cipher_setup(AesCmacCtx, cipher_info); + ret = mbedtls_cipher_setup(aes_cmac_ctx, cipher_info); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_starts(AesCmacCtx, key, AES_CMAC_KEY_LENGTH*8); + ret = mbedtls_cipher_cmac_starts(aes_cmac_ctx, key, + AES_CMAC_KEY_LENGTH * 8); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_update(AesCmacCtx, MicBlockB0, LORAMAC_MIC_BLOCK_B0_SIZE); + ret = mbedtls_cipher_cmac_update(aes_cmac_ctx, mic_block_b0, + LORAMAC_MIC_BLOCK_B0_SIZE); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_update(AesCmacCtx, buffer, size & 0xFF); + ret = mbedtls_cipher_cmac_update(aes_cmac_ctx, buffer, size & 0xFF); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_finish(AesCmacCtx, Mic); + ret = mbedtls_cipher_cmac_finish(aes_cmac_ctx, computed_mic); if (0 != ret) goto exit; - *mic = ( uint32_t )( ( uint32_t )Mic[3] << 24 | ( uint32_t )Mic[2] << 16 | ( uint32_t )Mic[1] << 8 | ( uint32_t )Mic[0] ); + *mic = (uint32_t) ((uint32_t) computed_mic[3] << 24 + | (uint32_t) computed_mic[2] << 16 + | (uint32_t) computed_mic[1] << 8 | (uint32_t) computed_mic[0]); } else { ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED; } -exit: - mbedtls_cipher_free( AesCmacCtx ); +exit: mbedtls_cipher_free(aes_cmac_ctx); return ret; } -int LoRaMacPayloadEncrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *encBuffer ) +int encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint8_t *enc_buffer) { uint16_t i; uint8_t bufferIndex = 0; uint16_t ctr = 1; int ret = 0; - mbedtls_aes_init(&AesContext); - ret = mbedtls_aes_setkey_enc(&AesContext, key, 16*8); + mbedtls_aes_init(&aes_ctx); + ret = mbedtls_aes_setkey_enc(&aes_ctx, key, 16 * 8); if (0 != ret) goto exit; - aBlock[5] = dir; + a_block[5] = dir; - aBlock[6] = ( address ) & 0xFF; - aBlock[7] = ( address >> 8 ) & 0xFF; - aBlock[8] = ( address >> 16 ) & 0xFF; - aBlock[9] = ( address >> 24 ) & 0xFF; + a_block[6] = (address) & 0xFF; + a_block[7] = (address >> 8) & 0xFF; + a_block[8] = (address >> 16) & 0xFF; + a_block[9] = (address >> 24) & 0xFF; - aBlock[10] = ( sequenceCounter ) & 0xFF; - aBlock[11] = ( sequenceCounter >> 8 ) & 0xFF; - aBlock[12] = ( sequenceCounter >> 16 ) & 0xFF; - aBlock[13] = ( sequenceCounter >> 24 ) & 0xFF; + a_block[10] = (seq_counter) & 0xFF; + a_block[11] = (seq_counter >> 8) & 0xFF; + a_block[12] = (seq_counter >> 16) & 0xFF; + a_block[13] = (seq_counter >> 24) & 0xFF; - while( size >= 16 ) - { - aBlock[15] = ( ( ctr ) & 0xFF ); + while (size >= 16) { + a_block[15] = ((ctr) & 0xFF); ctr++; - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, aBlock, sBlock); + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, + s_block); if (0 != ret) goto exit; - for( i = 0; i < 16; i++ ) - { - encBuffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i]; + for (i = 0; i < 16; i++) { + enc_buffer[bufferIndex + i] = buffer[bufferIndex + i] ^ s_block[i]; } size -= 16; bufferIndex += 16; } - if( size > 0 ) - { - aBlock[15] = ( ( ctr ) & 0xFF ); - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, aBlock, sBlock); + if (size > 0) { + a_block[15] = ((ctr) & 0xFF); + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, + s_block); if (0 != ret) goto exit; - for( i = 0; i < size; i++ ) - { - encBuffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i]; + for (i = 0; i < size; i++) { + enc_buffer[bufferIndex + i] = buffer[bufferIndex + i] ^ s_block[i]; } } -exit: - mbedtls_aes_free(&AesContext); +exit: mbedtls_aes_free(&aes_ctx); return ret; } -int LoRaMacPayloadDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *decBuffer ) +int decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint8_t *dec_buffer) { - return LoRaMacPayloadEncrypt( buffer, size, key, address, dir, sequenceCounter, decBuffer ); + return encrypt_payload(buffer, size, key, address, dir, seq_counter, + dec_buffer); } -int LoRaMacJoinComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t *mic ) +int compute_join_frame_mic(const uint8_t *buffer, uint16_t size, + const uint8_t *key, uint32_t *mic) { int ret = 0; - mbedtls_cipher_init(AesCmacCtx); + mbedtls_cipher_init(aes_cmac_ctx); const mbedtls_cipher_info_t* cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB); + if (NULL != cipher_info) { - ret = mbedtls_cipher_setup(AesCmacCtx, cipher_info); + ret = mbedtls_cipher_setup(aes_cmac_ctx, cipher_info); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_starts(AesCmacCtx, key, AES_CMAC_KEY_LENGTH*8); + ret = mbedtls_cipher_cmac_starts(aes_cmac_ctx, key, + AES_CMAC_KEY_LENGTH * 8); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_update(AesCmacCtx, buffer, size & 0xFF); + ret = mbedtls_cipher_cmac_update(aes_cmac_ctx, buffer, size & 0xFF); if (0 != ret) goto exit; - ret = mbedtls_cipher_cmac_finish(AesCmacCtx, Mic); + ret = mbedtls_cipher_cmac_finish(aes_cmac_ctx, computed_mic); if (0 != ret) goto exit; - *mic = ( uint32_t )( ( uint32_t )Mic[3] << 24 | ( uint32_t )Mic[2] << 16 | ( uint32_t )Mic[1] << 8 | ( uint32_t )Mic[0] ); + *mic = (uint32_t) ((uint32_t) computed_mic[3] << 24 + | (uint32_t) computed_mic[2] << 16 + | (uint32_t) computed_mic[1] << 8 | (uint32_t) computed_mic[0]); } else { ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED; } -exit: - mbedtls_cipher_free(AesCmacCtx); +exit: mbedtls_cipher_free(aes_cmac_ctx); return ret; } -int LoRaMacJoinDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint8_t *decBuffer ) +int decrypt_join_frame(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint8_t *dec_buffer) { int ret = 0; - mbedtls_aes_init(&AesContext); + mbedtls_aes_init(&aes_ctx); - ret = mbedtls_aes_setkey_enc(&AesContext, key, 16*8); + ret = mbedtls_aes_setkey_enc(&aes_ctx, key, 16 * 8); if (0 != ret) goto exit; - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, buffer, decBuffer); + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, + dec_buffer); if (0 != ret) goto exit; // Check if optional CFList is included - if( size >= 16 ) - { - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, buffer + 16, decBuffer + 16); + if (size >= 16) { + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, buffer + 16, + dec_buffer + 16); } -exit: - mbedtls_aes_free(&AesContext); +exit: mbedtls_aes_free(&aes_ctx); return ret; } -int LoRaMacJoinComputeSKeys( const uint8_t *key, const uint8_t *appNonce, uint16_t devNonce, uint8_t *nwkSKey, uint8_t *appSKey ) +int compute_skeys_for_join_frame(const uint8_t *key, const uint8_t *app_nonce, + uint16_t dev_nonce, uint8_t *nwk_skey, + uint8_t *app_skey) { uint8_t nonce[16]; - uint8_t *pDevNonce = ( uint8_t * )&devNonce; + uint8_t *p_dev_nonce = (uint8_t *) &dev_nonce; int ret = 0; - mbedtls_aes_init(&AesContext); + mbedtls_aes_init(&aes_ctx); - ret = mbedtls_aes_setkey_enc(&AesContext, key, 16*8); + ret = mbedtls_aes_setkey_enc(&aes_ctx, key, 16 * 8); if (0 != ret) goto exit; - memset( nonce, 0, sizeof( nonce ) ); + memset(nonce, 0, sizeof(nonce)); nonce[0] = 0x01; - memcpy( nonce + 1, appNonce, 6 ); - memcpy( nonce + 7, pDevNonce, 2 ); - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, nonce, nwkSKey); + memcpy(nonce + 1, app_nonce, 6); + memcpy(nonce + 7, p_dev_nonce, 2); + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, nwk_skey); if (0 != ret) goto exit; - memset( nonce, 0, sizeof( nonce ) ); + memset(nonce, 0, sizeof(nonce)); nonce[0] = 0x02; - memcpy( nonce + 1, appNonce, 6 ); - memcpy( nonce + 7, pDevNonce, 2 ); - ret = mbedtls_aes_crypt_ecb(&AesContext, MBEDTLS_AES_ENCRYPT, nonce, appSKey); + memcpy(nonce + 1, app_nonce, 6); + memcpy(nonce + 7, p_dev_nonce, 2); + ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, app_skey); -exit: - mbedtls_aes_free(&AesContext); + exit: mbedtls_aes_free(&aes_ctx); return ret; } #else @@ -294,8 +296,8 @@ exit: // user knows what is wrong and in addition to that these ensure that // Mbed-OS compiles properly under normal conditions where LoRaWAN in conjunction // with mbedTLS is not being used. -int LoRaMacComputeMic( const uint8_t *, uint16_t , const uint8_t *, uint32_t, - uint8_t dir, uint32_t, uint32_t * ) +int compute_mic(const uint8_t *, uint16_t , const uint8_t *, uint32_t, + uint8_t dir, uint32_t, uint32_t *) { MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); @@ -303,8 +305,8 @@ int LoRaMacComputeMic( const uint8_t *, uint16_t , const uint8_t *, uint32_t, return LORAWAN_STATUS_CRYPTO_FAIL; } -int LoRaMacPayloadEncrypt( const uint8_t *, uint16_t , const uint8_t *, uint32_t, - uint8_t , uint32_t , uint8_t * ) +int encrypt_payload(const uint8_t *, uint16_t , const uint8_t *, uint32_t, + uint8_t , uint32_t , uint8_t *) { MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); @@ -312,15 +314,8 @@ int LoRaMacPayloadEncrypt( const uint8_t *, uint16_t , const uint8_t *, uint32_t return LORAWAN_STATUS_CRYPTO_FAIL; } -int LoRaMacPayloadDecrypt( const uint8_t *, uint16_t , const uint8_t *, uint32_t, - uint8_t , uint32_t , uint8_t * ) -{ - MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); - - // Never actually reaches here - return LORAWAN_STATUS_CRYPTO_FAIL; -} -int LoRaMacJoinComputeMic( const uint8_t *, uint16_t , const uint8_t *, uint32_t * ) +int decrypt_payload(const uint8_t *, uint16_t , const uint8_t *, uint32_t, + uint8_t , uint32_t , uint8_t *) { MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); @@ -328,7 +323,7 @@ int LoRaMacJoinComputeMic( const uint8_t *, uint16_t , const uint8_t *, uint32_t return LORAWAN_STATUS_CRYPTO_FAIL; } -int LoRaMacJoinDecrypt( const uint8_t *, uint16_t , const uint8_t *, uint8_t * ) +int compute_join_frame_mic(const uint8_t *, uint16_t , const uint8_t *, uint32_t *) { MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); @@ -336,7 +331,16 @@ int LoRaMacJoinDecrypt( const uint8_t *, uint16_t , const uint8_t *, uint8_t * ) return LORAWAN_STATUS_CRYPTO_FAIL; } -int LoRaMacJoinComputeSKeys( const uint8_t *, const uint8_t *, uint16_t , uint8_t *, uint8_t * ) +int decrypt_join_frame(const uint8_t *, uint16_t , const uint8_t *, uint8_t *) +{ + MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); + + // Never actually reaches here + return LORAWAN_STATUS_CRYPTO_FAIL; +} + +int compute_skeys_for_join_frame(const uint8_t *, const uint8_t *, uint16_t , + uint8_t *, uint8_t *) { MBED_ASSERT("[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS"); diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.h b/features/lorawan/lorastack/mac/LoRaMacCrypto.h index cf09becb3e..af3fa75cff 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.h +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.h @@ -29,84 +29,94 @@ SPDX-License-Identifier: BSD-3-Clause /** * Computes the LoRaMAC frame MIC field * - * \param [in] buffer - Data buffer - * \param [in] size - Data buffer size - * \param [in] key - AES key to be used - * \param [in] address - Frame address - * \param [in] dir - Frame direction [0: uplink, 1: downlink] - * \param [in] sequenceCounter - Frame sequence counter - * \param [out] mic - Computed MIC field + * @param [in] buffer - Data buffer + * @param [in] size - Data buffer size + * @param [in] key - AES key to be used + * @param [in] address - Frame address + * @param [in] dir - Frame direction [0: uplink, 1: downlink] + * @param [in] seq_counter - Frame sequence counter + * @param [out] mic - Computed MIC field * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code */ -int LoRaMacComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic ); +int compute_mic(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint32_t *mic); /** - * Computes the LoRaMAC payload encryption + * Performs payload encryption * - * \param [in] buffer - Data buffer - * \param [in] size - Data buffer size - * \param [in] key - AES key to be used - * \param [in] address - Frame address - * \param [in] dir - Frame direction [0: uplink, 1: downlink] - * \param [in] sequenceCounter - Frame sequence counter - * \param [out] encBuffer - Encrypted buffer + * @param [in] buffer - Data buffer + * @param [in] size - Data buffer size + * @param [in] key - AES key to be used + * @param [in] address - Frame address + * @param [in] dir - Frame direction [0: uplink, 1: downlink] + * @param [in] seq_counter - Frame sequence counter + * @param [out] enc_buffer - Encrypted buffer * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code */ -int LoRaMacPayloadEncrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *encBuffer ); +int encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint8_t *enc_buffer); /** - * Computes the LoRaMAC payload decryption + * Performs payload decryption * - * \param [in] buffer - Data buffer - * \param [in] size - Data buffer size - * \param [in] key - AES key to be used - * \param [in] address - Frame address - * \param [in] dir - Frame direction [0: uplink, 1: downlink] - * \param [in] sequenceCounter - Frame sequence counter - * \param [out] decBuffer - Decrypted buffer + * @param [in] buffer - Data buffer + * @param [in] size - Data buffer size + * @param [in] key - AES key to be used + * @param [in] address - Frame address + * @param [in] dir - Frame direction [0: uplink, 1: downlink] + * @param [in] seq_counter - Frame sequence counter + * @param [out] dec_buffer - Decrypted buffer * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code */ -int LoRaMacPayloadDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *decBuffer ); +int decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, + uint32_t address, uint8_t dir, uint32_t seq_counter, + uint8_t *dec_buffer); /** * Computes the LoRaMAC Join Request frame MIC field * - * \param [in] buffer - Data buffer - * \param [in] size - Data buffer size - * \param [in] key - AES key to be used - * \param [out] mic - Computed MIC field + * @param [in] buffer - Data buffer + * @param [in] size - Data buffer size + * @param [in] key - AES key to be used + * @param [out] mic - Computed MIC field * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code * */ -int LoRaMacJoinComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t *mic ); +int compute_join_frame_mic(const uint8_t *buffer, uint16_t size, + const uint8_t *key, uint32_t *mic); /** * Computes the LoRaMAC join frame decryption * - * \param [in] buffer - Data buffer - * \param [in] size - Data buffer size - * \param [in] key - AES key to be used - * \param [out] decBuffer - Decrypted buffer + * @param [in] buffer - Data buffer + * @param [in] size - Data buffer size + * @param [in] key - AES key to be used + * @param [out] dec_buffer - Decrypted buffer * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code */ -int LoRaMacJoinDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint8_t *decBuffer ); +int decrypt_join_frame(const uint8_t *buffer, uint16_t size, + const uint8_t *key, uint8_t *dec_buffer); /** * Computes the LoRaMAC join frame decryption * - * \param [in] key - AES key to be used - * \param [in] appNonce - Application nonce - * \param [in] devNonce - Device nonce - * \param [out] nwkSKey - Network session key - * \param [out] appSKey - Application session key + * @param [in] key - AES key to be used + * @param [in] app_nonce - Application nonce + * @param [in] dev_nonce - Device nonce + * @param [out] nwk_skey - Network session key + * @param [out] app_skey - Application session key * - * \return 0 if successful, or a cipher specific error code + * @return 0 if successful, or a cipher specific error code */ -int LoRaMacJoinComputeSKeys( const uint8_t *key, const uint8_t *appNonce, uint16_t devNonce, uint8_t *nwkSKey, uint8_t *appSKey ); +int compute_skeys_for_join_frame(const uint8_t *key, const uint8_t *app_nonce, + uint16_t dev_nonce, uint8_t *nwk_skey, + uint8_t *app_skey ); #endif // MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__ diff --git a/features/lorawan/lorastack/mac/LoRaMacMcps.cpp b/features/lorawan/lorastack/mac/LoRaMacMcps.cpp index 17aa3194ee..c5331abd16 100644 --- a/features/lorawan/lorastack/mac/LoRaMacMcps.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacMcps.cpp @@ -52,15 +52,15 @@ lorawan_status_t LoRaMacMcps::set_request(loramac_mcps_req_t *mcpsRequest, get_phy_params_t get_phy; phy_param_t phyParam; lorawan_status_t status = LORAWAN_STATUS_SERVICE_UNKNOWN; - loramac_mhdr_t macHdr; + loramac_mhdr_t machdr; verification_params_t verify; - uint8_t fPort = 0; - void *fBuffer; - uint16_t fBufferSize; + uint8_t fport = 0; + void *fbuffer; + uint16_t fbuffer_size; int8_t datarate = DR_0; - bool readyToSend = false; + bool ready_to_send = false; - macHdr.value = 0; + machdr.value = 0; // Before performing any MCPS request, clear the confirmation structure memset((uint8_t*) &confirmation, 0, sizeof(confirmation)); @@ -72,34 +72,34 @@ lorawan_status_t LoRaMacMcps::set_request(loramac_mcps_req_t *mcpsRequest, switch (mcpsRequest->type) { case MCPS_UNCONFIRMED: { - readyToSend = true; + ready_to_send = true; params->max_ack_timeout_retries = 1; - macHdr.bits.mtype = FRAME_TYPE_DATA_UNCONFIRMED_UP; - fPort = mcpsRequest->req.unconfirmed.fport; - fBuffer = mcpsRequest->f_buffer; - fBufferSize = mcpsRequest->f_buffer_size; + machdr.bits.mtype = FRAME_TYPE_DATA_UNCONFIRMED_UP; + fport = mcpsRequest->req.unconfirmed.fport; + fbuffer = mcpsRequest->f_buffer; + fbuffer_size = mcpsRequest->f_buffer_size; datarate = mcpsRequest->req.unconfirmed.data_rate; break; } case MCPS_CONFIRMED: { - readyToSend = true; + ready_to_send = true; params->max_ack_timeout_retries = mcpsRequest->req.confirmed.nb_trials; - macHdr.bits.mtype = FRAME_TYPE_DATA_CONFIRMED_UP; - fPort = mcpsRequest->req.confirmed.fport; - fBuffer = mcpsRequest->f_buffer; - fBufferSize = mcpsRequest->f_buffer_size; + machdr.bits.mtype = FRAME_TYPE_DATA_CONFIRMED_UP; + fport = mcpsRequest->req.confirmed.fport; + fbuffer = mcpsRequest->f_buffer; + fbuffer_size = mcpsRequest->f_buffer_size; datarate = mcpsRequest->req.confirmed.data_rate; break; } case MCPS_PROPRIETARY: { - readyToSend = true; + ready_to_send = true; params->max_ack_timeout_retries = 1; - macHdr.bits.mtype = FRAME_TYPE_PROPRIETARY; - fBuffer = mcpsRequest->f_buffer; - fBufferSize = mcpsRequest->f_buffer_size; + machdr.bits.mtype = FRAME_TYPE_PROPRIETARY; + fbuffer = mcpsRequest->f_buffer; + fbuffer_size = mcpsRequest->f_buffer_size; datarate = mcpsRequest->req.proprietary.data_rate; break; } @@ -122,7 +122,7 @@ lorawan_status_t LoRaMacMcps::set_request(loramac_mcps_req_t *mcpsRequest, // Some regions have limitations for the minimum datarate. datarate = MAX(datarate, (int8_t)phyParam.value); - if (readyToSend == true) { + if (ready_to_send == true) { if (params->sys_params.adr_on == false) { verify.datarate = datarate; @@ -133,7 +133,7 @@ lorawan_status_t LoRaMacMcps::set_request(loramac_mcps_req_t *mcpsRequest, } } - status = _lora_mac->Send(&macHdr, fPort, fBuffer, fBufferSize); + status = _lora_mac->send(&machdr, fport, fbuffer, fbuffer_size); if (status == LORAWAN_STATUS_OK) { confirmation.req_type = mcpsRequest->type; params->flags.bits.mcps_req = 1; diff --git a/features/lorawan/lorastack/mac/LoRaMacMib.cpp b/features/lorawan/lorastack/mac/LoRaMacMib.cpp index 2849469494..981dd141d8 100644 --- a/features/lorawan/lorastack/mac/LoRaMacMib.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacMib.cpp @@ -75,7 +75,7 @@ lorawan_status_t LoRaMacMib::set_request(loramac_mib_req_confirm_t *mibSet, params->sys_params.min_rx_symb, params->sys_params.max_sys_rx_error, ¶ms->rx_window2_config); - _lora_mac->OpenContinuousRx2Window(); + _lora_mac->open_continuous_rx2_window(); break; } } @@ -144,7 +144,7 @@ lorawan_status_t LoRaMacMib::set_request(loramac_mib_req_confirm_t *mibSet, params->sys_params.max_sys_rx_error, ¶ms->rx_window2_config); - _lora_mac->OpenContinuousRx2Window(); + _lora_mac->open_continuous_rx2_window(); } } else { status = LORAWAN_STATUS_PARAMETER_INVALID; @@ -272,7 +272,7 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, { lorawan_status_t status = LORAWAN_STATUS_OK; get_phy_params_t get_phy; - phy_param_t phyParam; + phy_param_t phy_param; rx2_channel_params rx2_channel; if( mibGet == NULL ) @@ -330,9 +330,9 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, case MIB_CHANNELS: { get_phy.attribute = PHY_CHANNELS; - phyParam = _lora_phy->get_phy_params( &get_phy ); + phy_param = _lora_phy->get_phy_params( &get_phy ); - mibGet->param.channel_list = phyParam.channel_params; + mibGet->param.channel_list = phy_param.channel_params; break; } case MIB_RX2_CHANNEL: @@ -343,12 +343,12 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, case MIB_RX2_DEFAULT_CHANNEL: { get_phy.attribute = PHY_DEF_RX2_DR; - phyParam = _lora_phy->get_phy_params( &get_phy ); - rx2_channel.datarate = phyParam.value; + phy_param = _lora_phy->get_phy_params( &get_phy ); + rx2_channel.datarate = phy_param.value; get_phy.attribute = PHY_DEF_RX2_FREQUENCY; - phyParam = _lora_phy->get_phy_params( &get_phy ); - rx2_channel.frequency = phyParam.value; + phy_param = _lora_phy->get_phy_params( &get_phy ); + rx2_channel.frequency = phy_param.value; mibGet->param.rx2_channel = rx2_channel; break; @@ -356,17 +356,17 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, case MIB_CHANNELS_DEFAULT_MASK: { get_phy.attribute = PHY_CHANNELS_DEFAULT_MASK; - phyParam = _lora_phy->get_phy_params( &get_phy ); + phy_param = _lora_phy->get_phy_params( &get_phy ); - mibGet->param.default_channel_mask = phyParam.channel_mask; + mibGet->param.default_channel_mask = phy_param.channel_mask; break; } case MIB_CHANNELS_MASK: { get_phy.attribute = PHY_CHANNELS_MASK; - phyParam = _lora_phy->get_phy_params( &get_phy ); + phy_param = _lora_phy->get_phy_params( &get_phy ); - mibGet->param.channel_mask = phyParam.channel_mask; + mibGet->param.channel_mask = phy_param.channel_mask; break; } case MIB_CHANNELS_NB_REP: @@ -402,8 +402,8 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, case MIB_CHANNELS_DEFAULT_DATARATE: { get_phy.attribute = PHY_DEF_TX_DR; - phyParam = _lora_phy->get_phy_params( &get_phy ); - mibGet->param.default_channel_data_rate = phyParam.value; + phy_param = _lora_phy->get_phy_params( &get_phy ); + mibGet->param.default_channel_data_rate = phy_param.value; break; } case MIB_CHANNELS_DATARATE: @@ -414,8 +414,8 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet, case MIB_CHANNELS_DEFAULT_TX_POWER: { get_phy.attribute = PHY_DEF_TX_POWER; - phyParam = _lora_phy->get_phy_params( &get_phy ); - mibGet->param.default_channel_tx_pwr = phyParam.value; + phy_param = _lora_phy->get_phy_params( &get_phy ); + mibGet->param.default_channel_tx_pwr = phy_param.value; break; } case MIB_CHANNELS_TX_POWER: diff --git a/features/lorawan/lorastack/mac/LoRaMacMlme.cpp b/features/lorawan/lorastack/mac/LoRaMacMlme.cpp index 9f2eb6f668..35c0b623a8 100644 --- a/features/lorawan/lorastack/mac/LoRaMacMlme.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacMlme.cpp @@ -43,17 +43,17 @@ void LoRaMacMlme::activate_mlme_subsystem(LoRaMac *mac, LoRaPHY *phy, _mac_cmd = cmd; } -lorawan_status_t LoRaMacMlme::set_request(loramac_mlme_req_t *mlmeRequest, +lorawan_status_t LoRaMacMlme::set_request(loramac_mlme_req_t *request, loramac_protocol_params *params) { - if (mlmeRequest && params && _lora_mac && _lora_phy && _mac_cmd) { + if (request && params && _lora_mac && _lora_phy && _mac_cmd) { lorawan_status_t status = LORAWAN_STATUS_SERVICE_UNKNOWN; - loramac_mhdr_t macHdr; + loramac_mhdr_t machdr; verification_params_t verify; get_phy_params_t get_phy; - phy_param_t phyParam; + phy_param_t phy_param; if (params->mac_state != LORAMAC_IDLE) { @@ -66,73 +66,73 @@ lorawan_status_t LoRaMacMlme::set_request(loramac_mlme_req_t *mlmeRequest, confirmation.status = LORAMAC_EVENT_INFO_STATUS_ERROR; - switch (mlmeRequest->type) { + switch (request->type) { case MLME_JOIN: { if ((params->mac_state & LORAMAC_TX_DELAYED) == LORAMAC_TX_DELAYED) { return LORAWAN_STATUS_BUSY; } - if ((mlmeRequest->req.join.dev_eui == NULL) - || (mlmeRequest->req.join.app_eui == NULL) - || (mlmeRequest->req.join.app_key == NULL) - || (mlmeRequest->req.join.nb_trials == 0)) { + if ((request->req.join.dev_eui == NULL) + || (request->req.join.app_eui == NULL) + || (request->req.join.app_key == NULL) + || (request->req.join.nb_trials == 0)) { return LORAWAN_STATUS_PARAMETER_INVALID; } // Verify the parameter NbTrials for the join procedure - verify.nb_join_trials = mlmeRequest->req.join.nb_trials; + verify.nb_join_trials = request->req.join.nb_trials; if (_lora_phy->verify(&verify, PHY_NB_JOIN_TRIALS) == false) { // Value not supported, get default get_phy.attribute = PHY_DEF_NB_JOIN_TRIALS; - phyParam = _lora_phy->get_phy_params(&get_phy); - mlmeRequest->req.join.nb_trials = (uint8_t) phyParam.value; + phy_param = _lora_phy->get_phy_params(&get_phy); + request->req.join.nb_trials = (uint8_t) phy_param.value; } params->flags.bits.mlme_req = 1; - confirmation.req_type = mlmeRequest->type; + confirmation.req_type = request->type; - params->keys.dev_eui = mlmeRequest->req.join.dev_eui; - params->keys.app_eui = mlmeRequest->req.join.app_eui; - params->keys.app_key = mlmeRequest->req.join.app_key; - params->max_join_request_trials = mlmeRequest->req.join.nb_trials; + params->keys.dev_eui = request->req.join.dev_eui; + params->keys.app_eui = request->req.join.app_eui; + params->keys.app_key = request->req.join.app_key; + params->max_join_request_trials = request->req.join.nb_trials; // Reset variable JoinRequestTrials params->join_request_trial_counter = 0; // Setup header information - macHdr.value = 0; - macHdr.bits.mtype = FRAME_TYPE_JOIN_REQ; + machdr.value = 0; + machdr.bits.mtype = FRAME_TYPE_JOIN_REQ; - _lora_mac->ResetMacParameters(); + _lora_mac->reset_mac_parameters(); params->sys_params.channel_data_rate = _lora_phy->get_alternate_DR(params->join_request_trial_counter + 1); - status = _lora_mac->Send(&macHdr, 0, NULL, 0); + status = _lora_mac->send(&machdr, 0, NULL, 0); break; } case MLME_LINK_CHECK: { params->flags.bits.mlme_req = 1; // LoRaMac will send this command piggy-backed - confirmation.req_type = mlmeRequest->type; + confirmation.req_type = request->type; - status = _mac_cmd->AddMacCommand(MOTE_MAC_LINK_CHECK_REQ, 0, 0); + status = _mac_cmd->add_mac_command(MOTE_MAC_LINK_CHECK_REQ, 0, 0); break; } case MLME_TXCW: { - confirmation.req_type = mlmeRequest->type; + confirmation.req_type = request->type; params->flags.bits.mlme_req = 1; - status = _lora_mac->SetTxContinuousWave(mlmeRequest->req.cw_tx_mode.timeout); + status = _lora_mac->set_tx_continuous_wave(request->req.cw_tx_mode.timeout); break; } case MLME_TXCW_1: { - confirmation.req_type = mlmeRequest->type; + confirmation.req_type = request->type; params->flags.bits.mlme_req = 1; - status = _lora_mac->SetTxContinuousWave1(mlmeRequest->req.cw_tx_mode.timeout, - mlmeRequest->req.cw_tx_mode.frequency, - mlmeRequest->req.cw_tx_mode.power); + status = _lora_mac->set_tx_continuous_wave1(request->req.cw_tx_mode.timeout, + request->req.cw_tx_mode.frequency, + request->req.cw_tx_mode.power); break; } default: diff --git a/features/lorawan/lorastack/mac/LoRaMacMlme.h b/features/lorawan/lorastack/mac/LoRaMacMlme.h index deb73f75f2..2be3db0b71 100644 --- a/features/lorawan/lorastack/mac/LoRaMacMlme.h +++ b/features/lorawan/lorastack/mac/LoRaMacMlme.h @@ -66,13 +66,13 @@ public: * to the central MAC control. It also modifies or uses protocol information * provided in the MAC protocol data structure. * - * @param mlmeRequest pointer to MLME request structure + * @param request pointer to MLME request structure * @param params pointer to MAC protocol parameters * * @return LORAWAN_STATUS_OK if everything goes well otherwise * a negative error code is returned. */ - lorawan_status_t set_request(loramac_mlme_req_t *mlmeRequest, loramac_protocol_params *params); + lorawan_status_t set_request(loramac_mlme_req_t *request, loramac_protocol_params *params); /** Grants access to MLME confirmation data * diff --git a/features/lorawan/system/lorawan_data_structures.h b/features/lorawan/system/lorawan_data_structures.h index 96f48543e4..10fa5995fc 100644 --- a/features/lorawan/system/lorawan_data_structures.h +++ b/features/lorawan/system/lorawan_data_structures.h @@ -368,7 +368,7 @@ typedef struct multicast_params_s { /*! * A reference pointer to the next multicast channel parameters in the list. */ - struct multicast_params_s *Next; + struct multicast_params_s *next; } multicast_params_t; /*!