Denislam Valeev / Mbed OS Nucleo_rtos_basic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaMacMlme.cpp Source File

LoRaMacMlme.cpp

00001 /**
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2013 Semtech
00008  ___ _____ _   ___ _  _____ ___  ___  ___ ___
00009 / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
00010 \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
00011 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
00012 embedded.connectivity.solutions===============
00013 
00014 Description: LoRaWAN stack layer that controls both MAC and PHY underneath
00015 
00016 License: Revised BSD License, see LICENSE.TXT file include in the project
00017 
00018 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
00019 
00020 
00021 Copyright (c) 2017, Arm Limited and affiliates.
00022 
00023 SPDX-License-Identifier: BSD-3-Clause
00024 */
00025 
00026 #include "LoRaMac.h"
00027 #include "lorastack/mac/LoRaMacMlme.h"
00028 
00029 LoRaMacMlme::LoRaMacMlme()
00030 : _lora_mac(NULL), _lora_phy(NULL), _mac_cmd(NULL)
00031 {
00032 }
00033 
00034 LoRaMacMlme::~LoRaMacMlme()
00035 {
00036 }
00037 
00038 void LoRaMacMlme::activate_mlme_subsystem(LoRaMac *mac, LoRaPHY *phy,
00039                                           LoRaMacCommand *cmd)
00040 {
00041     _lora_mac = mac;
00042     _lora_phy = phy;
00043     _mac_cmd = cmd;
00044 }
00045 
00046 lorawan_status_t LoRaMacMlme::set_request(loramac_mlme_req_t  *request,
00047                                           loramac_protocol_params *params)
00048 {
00049     if (request && params && _lora_mac && _lora_phy && _mac_cmd) {
00050 
00051         lorawan_status_t status = LORAWAN_STATUS_SERVICE_UNKNOWN;
00052         loramac_mhdr_t  machdr;
00053 
00054         verification_params_t verify;
00055         get_phy_params_t get_phy;
00056         phy_param_t  phy_param;
00057 
00058 
00059         if (params->mac_state != LORAMAC_IDLE) {
00060             return LORAWAN_STATUS_BUSY;
00061         }
00062 
00063         // Before setting a new MLME request, clear the MLME confirmation
00064         // structure
00065         memset((uint8_t*) &confirmation, 0, sizeof(confirmation));
00066 
00067         confirmation.status = LORAMAC_EVENT_INFO_STATUS_ERROR ;
00068 
00069         switch (request->type ) {
00070             case MLME_JOIN : {
00071                 if ((params->mac_state & LORAMAC_TX_DELAYED)
00072                         == LORAMAC_TX_DELAYED) {
00073                     return LORAWAN_STATUS_BUSY;
00074                 }
00075 
00076                 if ((request->req.join .dev_eui  == NULL)
00077                         || (request->req.join .app_eui  == NULL)
00078                         || (request->req.join .app_key  == NULL)
00079                         || (request->req.join .nb_trials  == 0)) {
00080                     return LORAWAN_STATUS_PARAMETER_INVALID;
00081                 }
00082 
00083                 // Verify the parameter NbTrials for the join procedure
00084                 verify.nb_join_trials = request->req.join .nb_trials ;
00085 
00086                 if (_lora_phy->verify(&verify, PHY_NB_JOIN_TRIALS ) == false) {
00087                     // Value not supported, get default
00088                     get_phy.attribute = PHY_DEF_NB_JOIN_TRIALS ;
00089                     phy_param = _lora_phy->get_phy_params(&get_phy);
00090                     request->req.join .nb_trials  = (uint8_t) phy_param.value ;
00091                 }
00092 
00093                 params->flags.bits.mlme_req = 1;
00094                 confirmation.req_type = request->type ;
00095 
00096                 params->keys.dev_eui = request->req.join .dev_eui ;
00097                 params->keys.app_eui = request->req.join .app_eui ;
00098                 params->keys.app_key = request->req.join .app_key ;
00099                 params->max_join_request_trials = request->req.join .nb_trials ;
00100 
00101                 // Reset variable JoinRequestTrials
00102                 params->join_request_trial_counter = 0;
00103 
00104                 // Setup header information
00105                 machdr.value  = 0;
00106                 machdr.bits.mtype  = FRAME_TYPE_JOIN_REQ ;
00107 
00108                 _lora_mac->reset_mac_parameters();
00109 
00110                 params->sys_params.channel_data_rate =
00111                         _lora_phy->get_alternate_DR(params->join_request_trial_counter + 1);
00112 
00113                 status = _lora_mac->send(&machdr, 0, NULL, 0);
00114                 break;
00115             }
00116             case MLME_LINK_CHECK : {
00117                 params->flags.bits.mlme_req = 1;
00118                 // LoRaMac will send this command piggy-backed
00119                 confirmation.req_type = request->type ;
00120 
00121                 status = _mac_cmd->add_mac_command(MOTE_MAC_LINK_CHECK_REQ , 0, 0);
00122                 break;
00123             }
00124             case MLME_TXCW : {
00125                 confirmation.req_type = request->type ;
00126                 params->flags.bits.mlme_req = 1;
00127                 status = _lora_mac->set_tx_continuous_wave(request->req.cw_tx_mode .timeout );
00128                 break;
00129             }
00130             case MLME_TXCW_1 : {
00131                 confirmation.req_type = request->type ;
00132                 params->flags.bits.mlme_req = 1;
00133                 status = _lora_mac->set_tx_continuous_wave1(request->req.cw_tx_mode .timeout ,
00134                                               request->req.cw_tx_mode .frequency ,
00135                                               request->req.cw_tx_mode .power );
00136                 break;
00137             }
00138             default:
00139                 break;
00140         }
00141 
00142         if (status != LORAWAN_STATUS_OK) {
00143             params->is_node_ack_requested = false;
00144             params->flags.bits.mlme_req = 0;
00145         }
00146 
00147         return status;
00148     }
00149 
00150     return LORAWAN_STATUS_PARAMETER_INVALID;
00151 }