2017-11-27 13:34:56 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @brief Implementation of LoRaWANBase
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017, Arm Limited and affiliates.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lorawan/LoRaWANInterface.h"
|
|
|
|
|
2017-12-11 10:20:11 +00:00
|
|
|
using namespace events;
|
|
|
|
|
2017-11-27 13:34:56 +00:00
|
|
|
inline LoRaWANStack& stk_obj()
|
|
|
|
{
|
|
|
|
return LoRaWANStack::get_lorawan_stack();
|
|
|
|
}
|
|
|
|
|
2017-12-15 10:30:40 +00:00
|
|
|
LoRaWANInterface::LoRaWANInterface(LoRaRadio& radio) : _link_check_requested(false)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
// Pass mac_handlers to radio to the radio driver after
|
|
|
|
// binding radio driver to PHY layer
|
|
|
|
radio_events_t *events = stk_obj().bind_radio_driver(radio);
|
|
|
|
radio.lock();
|
|
|
|
radio.init_radio(events);
|
|
|
|
radio.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
LoRaWANInterface::~LoRaWANInterface()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::initialize(EventQueue *queue)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
2017-12-11 10:20:11 +00:00
|
|
|
if(!queue) {
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_PARAMETER_INVALID;
|
2017-12-11 10:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stk_obj().initialize_mac_layer(queue);
|
2017-11-27 13:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::connect()
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
// connection attempt without parameters.
|
|
|
|
// System tries to look for configuration in mbed_lib.json that can be
|
|
|
|
// overridden by mbed_app.json. However, if none of the json files are
|
|
|
|
// available (highly unlikely), we still fallback to some default parameters.
|
|
|
|
// Check lorawan_data_structure for fallback defaults.
|
|
|
|
|
|
|
|
lorawan_connect_t connection_params;
|
|
|
|
|
2018-01-11 09:49:52 +00:00
|
|
|
if (MBED_CONF_LORA_OVER_THE_AIR_ACTIVATION) {
|
|
|
|
static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI;
|
|
|
|
static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI;
|
|
|
|
static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY;
|
2017-11-27 13:34:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* OTAA join
|
|
|
|
*/
|
|
|
|
connection_params.connect_type = LORAWAN_CONNECTION_OTAA;
|
|
|
|
connection_params.connection_u.otaa.app_eui = app_eui;
|
|
|
|
connection_params.connection_u.otaa.dev_eui = dev_eui;
|
|
|
|
connection_params.connection_u.otaa.app_key = app_key;
|
2018-01-11 09:49:52 +00:00
|
|
|
connection_params.connection_u.otaa.nb_trials = MBED_CONF_LORA_NB_TRIALS;
|
2017-11-27 13:34:56 +00:00
|
|
|
|
|
|
|
return connect(connection_params);
|
|
|
|
} else {
|
2018-01-11 09:49:52 +00:00
|
|
|
static uint8_t nwk_skey[] = MBED_CONF_LORA_NWKSKEY;
|
|
|
|
static uint8_t app_skey[] = MBED_CONF_LORA_APPSKEY;
|
|
|
|
static uint32_t dev_addr = MBED_CONF_LORA_DEVICE_ADDRESS;
|
|
|
|
static uint32_t nwk_id = (MBED_CONF_LORA_DEVICE_ADDRESS & LORAWAN_NETWORK_ID_MASK);
|
2017-11-27 13:34:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* ABP connection
|
|
|
|
*/
|
|
|
|
connection_params.connect_type = LORAWAN_CONNECTION_ABP;
|
|
|
|
connection_params.connection_u.abp.nwk_id = nwk_id;
|
|
|
|
connection_params.connection_u.abp.dev_addr = dev_addr;
|
|
|
|
connection_params.connection_u.abp.nwk_skey = nwk_skey;
|
|
|
|
connection_params.connection_u.abp.app_skey = app_skey;
|
|
|
|
|
|
|
|
return connect(connection_params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::connect(const lorawan_connect_t &connect)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t mac_status;
|
2017-11-27 13:34:56 +00:00
|
|
|
|
|
|
|
if (connect.connect_type == LORAWAN_CONNECTION_OTAA) {
|
|
|
|
mac_status = stk_obj().join_request_by_otaa(connect);
|
|
|
|
} else if (connect.connect_type == LORAWAN_CONNECTION_ABP) {
|
|
|
|
mac_status = stk_obj().activation_by_personalization(connect);
|
|
|
|
} else {
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_PARAMETER_INVALID;
|
2017-11-27 13:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mac_status;
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::disconnect()
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
2018-02-09 07:59:15 +00:00
|
|
|
return stk_obj().shutdown();
|
2017-11-27 13:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::add_link_check_request()
|
2017-12-15 10:30:40 +00:00
|
|
|
{
|
|
|
|
_link_check_requested = true;
|
|
|
|
return stk_obj().set_link_check_request();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoRaWANInterface::remove_link_check_request()
|
|
|
|
{
|
|
|
|
_link_check_requested = false;
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::set_datarate(uint8_t data_rate)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().set_channel_data_rate(data_rate);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::set_confirmed_msg_retries(uint8_t count)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().set_confirmed_msg_retry(count);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::enable_adaptive_datarate()
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().enable_adaptive_datarate(true);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::disable_adaptive_datarate()
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().enable_adaptive_datarate(false);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::set_channel_plan(const lorawan_channelplan_t &channel_plan)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().add_channels(channel_plan);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::get_channel_plan(lorawan_channelplan_t &channel_plan)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().get_enabled_channels(channel_plan);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::remove_channel(uint8_t id)
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().remove_a_channel(id);
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::remove_channel_plan()
|
2017-11-27 13:34:56 +00:00
|
|
|
{
|
|
|
|
return stk_obj().drop_channel_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data,
|
|
|
|
uint16_t length, int flags)
|
|
|
|
{
|
2017-12-15 10:30:40 +00:00
|
|
|
if (_link_check_requested) {
|
|
|
|
// add a link check request with normal data, until the application
|
|
|
|
// explicitly removes it.
|
|
|
|
add_link_check_request();
|
|
|
|
}
|
|
|
|
|
2017-11-27 13:34:56 +00:00
|
|
|
if (data) {
|
|
|
|
return stk_obj().handle_tx(port, data, length, flags);
|
|
|
|
} else {
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_PARAMETER_INVALID;
|
2017-11-27 13:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
if (data && length > 0) {
|
|
|
|
return stk_obj().handle_rx(port, data, length, flags);
|
|
|
|
} else {
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_PARAMETER_INVALID;
|
2017-11-27 13:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 14:39:31 +00:00
|
|
|
lorawan_status_t LoRaWANInterface::add_app_callbacks(lorawan_app_callbacks_t *callbacks)
|
2017-12-21 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (!callbacks || !callbacks->events) {
|
|
|
|
// Event Callback is mandatory
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_PARAMETER_INVALID;
|
2017-12-21 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stk_obj().set_lora_callbacks(callbacks);
|
2018-01-12 14:39:31 +00:00
|
|
|
return LORAWAN_STATUS_OK;
|
2017-12-21 11:37:38 +00:00
|
|
|
}
|