Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaMacChannelPlan.cpp Source File

LoRaMacChannelPlan.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 "lorastack/mac/LoRaMacChannelPlan.h"
00027 
00028 LoRaMacChannelPlan::LoRaMacChannelPlan() : _lora_phy(NULL)
00029 {
00030 }
00031 
00032 LoRaMacChannelPlan::~LoRaMacChannelPlan()
00033 {
00034 }
00035 
00036 void LoRaMacChannelPlan::activate_channelplan_subsystem(LoRaPHY *phy)
00037 {
00038     _lora_phy = phy;
00039 }
00040 
00041 lorawan_status_t LoRaMacChannelPlan::set_plan(const lorawan_channelplan_t& plan)
00042 {
00043     channel_params_t  mac_layer_ch_params;
00044     lorawan_status_t status;
00045 
00046     uint8_t max_num_channels;
00047 
00048     if (!_lora_phy->is_custom_channel_plan_supported()) {
00049         return LORAWAN_STATUS_SERVICE_UNKNOWN;
00050     }
00051 
00052     max_num_channels = _lora_phy->get_max_nb_channels();
00053 
00054     // check if user is setting more channels than supported
00055     if (plan.nb_channels > max_num_channels) {
00056         return LORAWAN_STATUS_PARAMETER_INVALID;
00057     }
00058 
00059     for (uint8_t i = 0; i < plan.nb_channels; i++) {
00060 
00061         mac_layer_ch_params.band  = plan.channels[i].ch_param.band ;
00062 
00063         mac_layer_ch_params.dr_range .fields.max  = plan.channels[i].ch_param.dr_range .fields.max ;
00064         mac_layer_ch_params.dr_range .fields.min  = plan.channels[i].ch_param.dr_range .fields.min ;
00065         mac_layer_ch_params.dr_range .value  = plan.channels[i].ch_param.dr_range .value ;
00066         mac_layer_ch_params.frequency  = plan.channels[i].ch_param.frequency ;
00067         mac_layer_ch_params.rx1_frequency  = plan.channels[i].ch_param.rx1_frequency ;
00068 
00069         status = _lora_phy->add_channel(&mac_layer_ch_params, plan.channels[i].id);
00070 
00071         if (status != LORAWAN_STATUS_OK) {
00072             return status;
00073         }
00074     }
00075 
00076     return LORAWAN_STATUS_OK;
00077 }
00078 
00079 lorawan_status_t LoRaMacChannelPlan::get_plan(lorawan_channelplan_t& plan,
00080                                               const channel_params_t * channel_list)
00081 {
00082     uint8_t max_num_channels;
00083     uint16_t *channel_mask;
00084     uint8_t count = 0;
00085 
00086     if (!_lora_phy->is_custom_channel_plan_supported()) {
00087         return LORAWAN_STATUS_SERVICE_UNKNOWN;
00088     }
00089 
00090     max_num_channels = _lora_phy->get_max_nb_channels();
00091 
00092     channel_mask = _lora_phy->get_channel_mask(false);
00093 
00094     for (uint8_t i = 0; i < max_num_channels; i++) {
00095         // skip the channels which are not enabled
00096         if (_lora_phy->mask_bit_test(channel_mask, i) == 0) {
00097             continue;
00098         }
00099 
00100         // otherwise add them to the channel_plan struct
00101         plan.channels[count].id = i;
00102         plan.channels[count].ch_param.frequency  = channel_list[i].frequency ;
00103         plan.channels[count].ch_param.dr_range .value  = channel_list[i].dr_range .value ;
00104         plan.channels[count].ch_param.dr_range .fields.min  = channel_list[i].dr_range .fields.min ;
00105         plan.channels[count].ch_param.dr_range .fields.max  = channel_list[i].dr_range .fields.max ;
00106         plan.channels[count].ch_param.band  = channel_list[i].band ;
00107         plan.channels[count].ch_param.rx1_frequency  = channel_list[i].rx1_frequency ;
00108         count++;
00109     }
00110 
00111     plan.nb_channels = count;
00112 
00113     return LORAWAN_STATUS_OK;
00114 }
00115 
00116 lorawan_status_t LoRaMacChannelPlan::remove_plan()
00117 {
00118     lorawan_status_t status = LORAWAN_STATUS_OK;
00119 
00120     uint8_t max_num_channels;
00121     uint16_t *channel_mask;
00122     uint16_t *default_channel_mask;
00123 
00124     if (!_lora_phy->is_custom_channel_plan_supported()) {
00125         return LORAWAN_STATUS_SERVICE_UNKNOWN;
00126     }
00127 
00128     max_num_channels = _lora_phy->get_max_nb_channels();
00129 
00130     channel_mask = _lora_phy->get_channel_mask(false);
00131 
00132     default_channel_mask = _lora_phy->get_channel_mask(true);
00133 
00134     for (uint8_t i = 0; i < max_num_channels; i++) {
00135         // skip any default channels
00136         if (_lora_phy->mask_bit_test(default_channel_mask, i) != 0) {
00137             continue;
00138         }
00139 
00140         // skip any channels which are not currently enabled
00141         if (_lora_phy->mask_bit_test(channel_mask, i) == 0) {
00142             continue;
00143         }
00144 
00145         status = remove_single_channel(i);
00146 
00147         if (status != LORAWAN_STATUS_OK) {
00148             return status;
00149         }
00150     }
00151 
00152     return status;
00153 }
00154 
00155 lorawan_status_t LoRaMacChannelPlan::remove_single_channel(uint8_t channel_id)
00156 {
00157     uint8_t max_num_channels;
00158 
00159     if (!_lora_phy->is_custom_channel_plan_supported()) {
00160         return LORAWAN_STATUS_SERVICE_UNKNOWN;
00161     }
00162 
00163     max_num_channels = _lora_phy->get_max_nb_channels();
00164 
00165     // According to specification channel IDs start from 0 and last valid
00166     // channel ID is N-1 where N=MAX_NUM_CHANNELS.
00167     // So any ID which is larger or equal to the Max number of channels is invalid
00168     if (channel_id >= max_num_channels) {
00169         return LORAWAN_STATUS_PARAMETER_INVALID;
00170     }
00171 
00172     if (_lora_phy->remove_channel(channel_id) == false) {
00173         return LORAWAN_STATUS_PARAMETER_INVALID;
00174     }
00175 
00176     _lora_phy->put_radio_to_sleep();
00177 
00178     return LORAWAN_STATUS_OK;
00179 }
00180