BA / Mbed OS BaBoRo1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaWANInterface.cpp Source File

LoRaWANInterface.cpp

Go to the documentation of this file.
00001 /**
00002  * @file
00003  *
00004  * @brief      Implementation of LoRaWANBase
00005  *
00006  * Copyright (c) 2017, Arm Limited and affiliates.
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "lorawan/LoRaWANInterface.h"
00023 
00024 using namespace events;
00025 
00026 inline LoRaWANStack& stk_obj()
00027 {
00028     return LoRaWANStack::get_lorawan_stack();
00029 }
00030 
00031 LoRaWANInterface::LoRaWANInterface(LoRaRadio& radio) : _link_check_requested(false)
00032 {
00033     // Pass mac_handlers to radio to the radio driver after
00034     // binding radio driver to PHY layer
00035     radio_events_t *events = stk_obj().bind_radio_driver(radio);
00036     radio.lock();
00037     radio.init_radio(events);
00038     radio.unlock();
00039 }
00040 
00041 LoRaWANInterface::~LoRaWANInterface()
00042 {
00043 }
00044 
00045 lorawan_status_t LoRaWANInterface::initialize(EventQueue *queue)
00046 {
00047     if(!queue) {
00048         return LORAWAN_STATUS_PARAMETER_INVALID;
00049     }
00050 
00051     return stk_obj().initialize_mac_layer(queue);
00052 }
00053 
00054 lorawan_status_t LoRaWANInterface::connect()
00055 {
00056     // connection attempt without parameters.
00057     // System tries to look for configuration in mbed_lib.json that can be
00058     // overridden by mbed_app.json. However, if none of the json files are
00059     // available (highly unlikely), we still fallback to some default parameters.
00060     // Check lorawan_data_structure for fallback defaults.
00061 
00062     lorawan_connect_t connection_params;
00063 
00064     if (MBED_CONF_LORA_OVER_THE_AIR_ACTIVATION) {
00065         static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI;
00066         static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI;
00067         static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY;
00068         /**
00069          *
00070          * OTAA join
00071          */
00072         connection_params.connect_type  = LORAWAN_CONNECTION_OTAA;
00073         connection_params.connection_u.otaa .app_eui = app_eui;
00074         connection_params.connection_u.otaa .dev_eui = dev_eui;
00075         connection_params.connection_u.otaa .app_key = app_key;
00076         connection_params.connection_u.otaa .nb_trials = MBED_CONF_LORA_NB_TRIALS;
00077 
00078         return connect(connection_params);
00079     } else {
00080         static uint8_t nwk_skey[] = MBED_CONF_LORA_NWKSKEY;
00081         static uint8_t app_skey[] = MBED_CONF_LORA_APPSKEY;
00082         static uint32_t dev_addr = MBED_CONF_LORA_DEVICE_ADDRESS;
00083         static uint32_t nwk_id = (MBED_CONF_LORA_DEVICE_ADDRESS & LORAWAN_NETWORK_ID_MASK);
00084 
00085         /**
00086          *
00087          * ABP connection
00088          */
00089         connection_params.connect_type  = LORAWAN_CONNECTION_ABP;
00090         connection_params.connection_u.abp .nwk_id = nwk_id;
00091         connection_params.connection_u.abp .dev_addr = dev_addr;
00092         connection_params.connection_u.abp .nwk_skey = nwk_skey;
00093         connection_params.connection_u.abp .app_skey = app_skey;
00094 
00095         return connect(connection_params);
00096     }
00097 }
00098 
00099 lorawan_status_t LoRaWANInterface::connect(const lorawan_connect_t &connect)
00100 {
00101     lorawan_status_t mac_status;
00102 
00103     if (connect.connect_type  == LORAWAN_CONNECTION_OTAA) {
00104         mac_status = stk_obj().join_request_by_otaa(connect);
00105     } else if (connect.connect_type  == LORAWAN_CONNECTION_ABP) {
00106         mac_status = stk_obj().activation_by_personalization(connect);
00107     } else {
00108         return LORAWAN_STATUS_PARAMETER_INVALID;
00109     }
00110 
00111     return mac_status;
00112 }
00113 
00114 lorawan_status_t LoRaWANInterface::disconnect()
00115 {
00116     return stk_obj().shutdown();
00117 }
00118 
00119 lorawan_status_t LoRaWANInterface::add_link_check_request()
00120 {
00121     _link_check_requested = true;
00122     return stk_obj().set_link_check_request();
00123 }
00124 
00125 void LoRaWANInterface::remove_link_check_request()
00126 {
00127     _link_check_requested = false;
00128 }
00129 
00130 lorawan_status_t LoRaWANInterface::set_datarate(uint8_t data_rate)
00131 {
00132     return stk_obj().set_channel_data_rate(data_rate);
00133 }
00134 
00135 lorawan_status_t LoRaWANInterface::set_confirmed_msg_retries(uint8_t count)
00136 {
00137     return stk_obj().set_confirmed_msg_retry(count);
00138 }
00139 
00140 lorawan_status_t LoRaWANInterface::enable_adaptive_datarate()
00141 {
00142     return stk_obj().enable_adaptive_datarate(true);
00143 }
00144 
00145 lorawan_status_t LoRaWANInterface::disable_adaptive_datarate()
00146 {
00147     return stk_obj().enable_adaptive_datarate(false);
00148 }
00149 
00150 lorawan_status_t LoRaWANInterface::set_channel_plan(const lorawan_channelplan_t &channel_plan)
00151 {
00152     return stk_obj().add_channels(channel_plan);
00153 }
00154 
00155 lorawan_status_t LoRaWANInterface::get_channel_plan(lorawan_channelplan_t &channel_plan)
00156 {
00157     return stk_obj().get_enabled_channels(channel_plan);
00158 }
00159 
00160 lorawan_status_t LoRaWANInterface::remove_channel(uint8_t id)
00161 {
00162     return stk_obj().remove_a_channel(id);
00163 }
00164 
00165 lorawan_status_t LoRaWANInterface::remove_channel_plan()
00166 {
00167     return stk_obj().drop_channel_list();
00168 }
00169 
00170 int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data,
00171                                uint16_t length, int flags)
00172 {
00173     if (_link_check_requested) {
00174         // add a link check request with normal data, until the application
00175         // explicitly removes it.
00176         add_link_check_request();
00177     }
00178 
00179     if (data) {
00180         return stk_obj().handle_tx(port, data, length, flags);
00181     } else {
00182         return LORAWAN_STATUS_PARAMETER_INVALID;
00183     }
00184 }
00185 
00186 int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
00187                                   int flags)
00188 {
00189     if (data && length > 0) {
00190         return stk_obj().handle_rx(port, data, length, flags);
00191     } else {
00192         return LORAWAN_STATUS_PARAMETER_INVALID;
00193     }
00194 }
00195 
00196 lorawan_status_t LoRaWANInterface::add_app_callbacks(lorawan_app_callbacks_t *callbacks)
00197   {
00198 
00199      if (!callbacks || !callbacks->events) {
00200          // Event Callback is mandatory
00201          return LORAWAN_STATUS_PARAMETER_INVALID;
00202      }
00203 
00204      stk_obj().set_lora_callbacks(callbacks);
00205      return LORAWAN_STATUS_OK;
00206   }