Skip to content

Commit f33ef65

Browse files
Hasnain Virktheotherjimmy
authored andcommitted
Adding LoRaWANInterface - implementing, LoRaWANBase
This class is the doorway for the user application into the Mbed-OS implementation of LoRaWAN protocol. It implements LoRaWANBase and hence would work with any stack implementation underneath, ensuring seemless portability for applications. It takes a pre-constructed object of LoRaRadio and delegates it in the downward direction. Before calling connect() user must call initialize() function in order to initialize stack and mac layers. connect() APIs can be used to either provide relevent keys and connection method at runtime or compile time (using Mbed config system). enable_adaptive_datarate() and disable_adaptive_datarate() are used to turn on/off automatic rate control. Otherwisem set_datarate() could be used to set a particular data rate on the current channel. set_confirmed_msg_retries() is valid only for CONFIRMED messages. It means that the stack will retry for a given number of times before timing out. set_channel_plan() and get_channel_plan() are used to set or get a particular channel plan. These APIs are particularly useful in case of ABP (activation by personalization). Because in case of OTAA(over the air activation), by default the stack takes in a CF List (carrier frequency list) sent by the base station in conjunction with Network server. This list overwrites all user configured channels or channel plan. set_channel_plan() can be used to set a single channel as well by setting the parameter for number of channels to 1. remove_channel_plan() or remove_channel() are used to remove a currently active channel plan or a specific channel. send() and receive() APIs follow posix design except the socket descriptor is replaced with port number here. lora_event_callback() API is used to set a callback function from the application side which is used by the stack to inform user of particular events like CONNECTED, DISCONNECTED, CRYPTO_FAILURE, TX_TIMEOUT etc.
1 parent 2f860d2 commit f33ef65

File tree

2 files changed

+516
-0
lines changed

2 files changed

+516
-0
lines changed

features/lorawan/LoRaWANInterface.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/**
2+
* @file
3+
*
4+
* @brief Implementation of LoRaWANBase
5+
*
6+
* Copyright (c) 2017, Arm Limited and affiliates.
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
#include "lorawan/LoRaWANInterface.h"
23+
24+
inline LoRaWANStack& stk_obj()
25+
{
26+
return LoRaWANStack::get_lorawan_stack();
27+
}
28+
29+
LoRaWANInterface::LoRaWANInterface(LoRaRadio& radio)
30+
{
31+
// Pass mac_handlers to radio to the radio driver after
32+
// binding radio driver to PHY layer
33+
radio_events_t *events = stk_obj().bind_radio_driver(radio);
34+
radio.lock();
35+
radio.init_radio(events);
36+
radio.unlock();
37+
}
38+
39+
LoRaWANInterface::~LoRaWANInterface()
40+
{
41+
}
42+
43+
lora_mac_status_t LoRaWANInterface::initialize()
44+
{
45+
return stk_obj().initialize_mac_layer();
46+
}
47+
48+
lora_mac_status_t LoRaWANInterface::connect()
49+
{
50+
// connection attempt without parameters.
51+
// System tries to look for configuration in mbed_lib.json that can be
52+
// overridden by mbed_app.json. However, if none of the json files are
53+
// available (highly unlikely), we still fallback to some default parameters.
54+
// Check lorawan_data_structure for fallback defaults.
55+
56+
lorawan_connect_t connection_params;
57+
58+
if (OVER_THE_AIR_ACTIVATION != 0) {
59+
static uint8_t dev_eui[] = LORAWAN_DEVICE_EUI;
60+
static uint8_t app_eui[] = LORAWAN_APPLICATION_EUI;
61+
static uint8_t app_key[] = LORAWAN_APPLICATION_KEY;
62+
/**
63+
*
64+
* OTAA join
65+
*/
66+
connection_params.connect_type = LORAWAN_CONNECTION_OTAA;
67+
connection_params.connection_u.otaa.app_eui = app_eui;
68+
connection_params.connection_u.otaa.dev_eui = dev_eui;
69+
connection_params.connection_u.otaa.app_key = app_key;
70+
connection_params.connection_u.otaa.nb_trials = LORAWAN_NB_TRIALS;
71+
72+
return connect(connection_params);
73+
} else {
74+
static uint8_t nwk_skey[] = LORAWAN_NWKSKEY;
75+
static uint8_t app_skey[] = LORAWAN_APPSKEY;
76+
static uint32_t dev_addr = LORAWAN_DEVICE_ADDRESS;
77+
static uint32_t nwk_id = (LORAWAN_DEVICE_ADDRESS & LORAWAN_NETWORK_ID_MASK);
78+
79+
/**
80+
*
81+
* ABP connection
82+
*/
83+
connection_params.connect_type = LORAWAN_CONNECTION_ABP;
84+
connection_params.connection_u.abp.nwk_id = nwk_id;
85+
connection_params.connection_u.abp.dev_addr = dev_addr;
86+
connection_params.connection_u.abp.nwk_skey = nwk_skey;
87+
connection_params.connection_u.abp.app_skey = app_skey;
88+
89+
return connect(connection_params);
90+
}
91+
}
92+
93+
lora_mac_status_t LoRaWANInterface::connect(const lorawan_connect_t &connect)
94+
{
95+
lora_mac_status_t mac_status;
96+
97+
if (connect.connect_type == LORAWAN_CONNECTION_OTAA) {
98+
mac_status = stk_obj().join_request_by_otaa(connect);
99+
} else if (connect.connect_type == LORAWAN_CONNECTION_ABP) {
100+
mac_status = stk_obj().activation_by_personalization(connect);
101+
} else {
102+
return LORA_MAC_STATUS_PARAMETER_INVALID;
103+
}
104+
105+
return mac_status;
106+
}
107+
108+
lora_mac_status_t LoRaWANInterface::disconnect()
109+
{
110+
stk_obj().shutdown();
111+
return LORA_MAC_STATUS_OK;
112+
}
113+
114+
lora_mac_status_t LoRaWANInterface::set_datarate(uint8_t data_rate)
115+
{
116+
return stk_obj().set_channel_data_rate(data_rate);
117+
}
118+
119+
lora_mac_status_t LoRaWANInterface::set_confirmed_msg_retries(uint8_t count)
120+
{
121+
return stk_obj().set_confirmed_msg_retry(count);
122+
}
123+
124+
lora_mac_status_t LoRaWANInterface::enable_adaptive_datarate()
125+
{
126+
return stk_obj().enable_adaptive_datarate(true);
127+
}
128+
129+
lora_mac_status_t LoRaWANInterface::disable_adaptive_datarate()
130+
{
131+
return stk_obj().enable_adaptive_datarate(false);
132+
}
133+
134+
lora_mac_status_t LoRaWANInterface::set_channel_plan(const lora_channelplan_t &channel_plan)
135+
{
136+
return stk_obj().add_channels(channel_plan);
137+
}
138+
139+
lora_mac_status_t LoRaWANInterface::get_channel_plan(lora_channelplan_t &channel_plan)
140+
{
141+
return stk_obj().get_enabled_channels(channel_plan);
142+
}
143+
144+
lora_mac_status_t LoRaWANInterface::remove_channel(uint8_t id)
145+
{
146+
return stk_obj().remove_a_channel(id);
147+
}
148+
149+
lora_mac_status_t LoRaWANInterface::remove_channel_plan()
150+
{
151+
return stk_obj().drop_channel_list();
152+
}
153+
154+
int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data,
155+
uint16_t length, int flags)
156+
{
157+
if (data) {
158+
return stk_obj().handle_tx(port, data, length, flags);
159+
} else {
160+
return LORA_MAC_STATUS_PARAMETER_INVALID;
161+
}
162+
}
163+
164+
int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
165+
int flags)
166+
{
167+
if (data && length > 0) {
168+
return stk_obj().handle_rx(port, data, length, flags);
169+
} else {
170+
return LORA_MAC_STATUS_PARAMETER_INVALID;
171+
}
172+
}
173+
174+
void LoRaWANInterface::lora_event_callback(mbed::Callback<void(lora_events_t)> event_cb)
175+
{
176+
stk_obj().set_lora_event_cb(event_cb);
177+
}

0 commit comments

Comments
 (0)