mirror of https://github.com/ARMmbed/mbed-os.git
Adding base class for LoRaWAN interfaces
All network interfaces for LoRaWAN protocol must implement this class. In order to be compatible with Mbed-OS applications, any implementation of this class must use the data structures and Mbed-OS timers provided. lorawan_data_structures may look repetitive but this is essential as we have a plan to use a reference implementation for LoRaWAN mac layer from Semtech. Some of the data structures provide seemless transition from semtech implementation (as MAC layer) to the Mbed-OS control layers above. features/lorawan/lorastack is the placeholder for future items like mac and phy layers. system/ will contain all the common bits.pull/6087/head
parent
69664c5394
commit
c9804bd167
|
@ -0,0 +1,84 @@
|
||||||
|
/**
|
||||||
|
/ _____) _ | |
|
||||||
|
( (____ _____ ____ _| |_ _____ ____| |__
|
||||||
|
\____ \| ___ | (_ _) ___ |/ ___) _ \
|
||||||
|
_____) ) ____| | | || |_| ____( (___| | | |
|
||||||
|
(______/|_____)_|_|_| \__)_____)\____)_| |_|
|
||||||
|
(C)2013 Semtech
|
||||||
|
|
||||||
|
Description: Timer objects and scheduling management
|
||||||
|
|
||||||
|
License: Revised BSD License, see LICENSE.TXT file include in the project
|
||||||
|
|
||||||
|
Maintainer: Miguel Luis and Gregory Cristian
|
||||||
|
|
||||||
|
|
||||||
|
Copyright (c) 2017, Arm Limited and affiliates.
|
||||||
|
|
||||||
|
SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lorawan/system/LoRaWANTimer.h"
|
||||||
|
|
||||||
|
static mbed::Timer TimeCounter;
|
||||||
|
static mbed::Ticker LoadTimeCounter;
|
||||||
|
|
||||||
|
volatile uint32_t CurrentTime = 0;
|
||||||
|
|
||||||
|
void TimerResetTimeCounter( void )
|
||||||
|
{
|
||||||
|
CurrentTime = CurrentTime + TimeCounter.read_us( ) / 1000;
|
||||||
|
TimeCounter.reset( );
|
||||||
|
TimeCounter.start( );
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerTimeCounterInit( void )
|
||||||
|
{
|
||||||
|
TimeCounter.start( );
|
||||||
|
LoadTimeCounter.attach( mbed::callback( &TimerResetTimeCounter ), 10 );
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerTime_t TimerGetCurrentTime( void )
|
||||||
|
{
|
||||||
|
CurrentTime += TimeCounter.read_us( ) / 1000;
|
||||||
|
TimeCounter.reset( );
|
||||||
|
TimeCounter.start( );
|
||||||
|
return ( ( TimerTime_t )CurrentTime );
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerTime_t TimerGetElapsedTime( TimerTime_t savedTime )
|
||||||
|
{
|
||||||
|
CurrentTime += TimeCounter.read_us( ) / 1000;
|
||||||
|
TimeCounter.reset( );
|
||||||
|
TimeCounter.start( );
|
||||||
|
return ( TimerTime_t )( CurrentTime - savedTime );
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerTime_t TimerGetFutureTime( TimerTime_t eventInFuture )
|
||||||
|
{
|
||||||
|
CurrentTime += TimeCounter.read_us( ) / 1000;
|
||||||
|
TimeCounter.reset( );
|
||||||
|
TimeCounter.start( );
|
||||||
|
return ( TimerTime_t )( CurrentTime + eventInFuture );
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) )
|
||||||
|
{
|
||||||
|
obj->value = 0;
|
||||||
|
obj->Callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerStart( TimerEvent_t *obj )
|
||||||
|
{
|
||||||
|
obj->Timer.attach_us( mbed::callback( obj->Callback ), obj->value * 1000 );
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerStop( TimerEvent_t *obj )
|
||||||
|
{
|
||||||
|
obj->Timer.detach( );
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerSetValue( TimerEvent_t *obj, uint32_t value )
|
||||||
|
{
|
||||||
|
obj->value = value;
|
||||||
|
}
|
|
@ -0,0 +1,107 @@
|
||||||
|
/**
|
||||||
|
/ _____) _ | |
|
||||||
|
( (____ _____ ____ _| |_ _____ ____| |__
|
||||||
|
\____ \| ___ | (_ _) ___ |/ ___) _ \
|
||||||
|
_____) ) ____| | | || |_| ____( (___| | | |
|
||||||
|
(______/|_____)_|_|_| \__)_____)\____)_| |_|
|
||||||
|
(C)2013 Semtech
|
||||||
|
|
||||||
|
Description: Timer objects and scheduling management
|
||||||
|
|
||||||
|
License: Revised BSD License, see LICENSE.TXT file include in the project
|
||||||
|
|
||||||
|
Maintainer: Miguel Luis and Gregory Cristian
|
||||||
|
|
||||||
|
|
||||||
|
Copyright (c) 2017, Arm Limited and affiliates.
|
||||||
|
|
||||||
|
SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MBED_LORAWAN_SYS_TIMER_H__
|
||||||
|
#define MBED_LORAWAN_SYS_TIMER_H__
|
||||||
|
#include "drivers/Timer.h"
|
||||||
|
#include "drivers/Ticker.h"
|
||||||
|
#include "lorawan/system/lorawan_data_structures.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Timer object description
|
||||||
|
*/
|
||||||
|
typedef struct TimerEvent_s
|
||||||
|
{
|
||||||
|
uint32_t value;
|
||||||
|
void ( *Callback )( void );
|
||||||
|
mbed::Ticker Timer;
|
||||||
|
}TimerEvent_t;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Initializes the timer used to get the current time.
|
||||||
|
*
|
||||||
|
* \remark The current time corresponds to the time since system startup.
|
||||||
|
*/
|
||||||
|
void TimerTimeCounterInit( void );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Initializes the timer object.
|
||||||
|
*
|
||||||
|
* \remark The TimerSetValue function must be called before starting the timer.
|
||||||
|
* This function initializes the timestamp and reloads the value at 0.
|
||||||
|
*
|
||||||
|
* \param [in] obj The structure containing the timer object parameters.
|
||||||
|
* \param [in] callback The function callback called at the end of the timeout.
|
||||||
|
*/
|
||||||
|
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Starts and adds the timer object to the list of timer events.
|
||||||
|
*
|
||||||
|
* \param [in] obj The structure containing the timer object parameters.
|
||||||
|
*/
|
||||||
|
void TimerStart( TimerEvent_t *obj );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Stops and removes the timer object from the list of timer events.
|
||||||
|
*
|
||||||
|
* \param [in] obj The structure containing the timer object parameters.
|
||||||
|
*/
|
||||||
|
void TimerStop( TimerEvent_t *obj );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Resets the timer object.
|
||||||
|
*
|
||||||
|
* \param [in] obj The structure containing the timer object parameters.
|
||||||
|
*/
|
||||||
|
void TimerReset( TimerEvent_t *obj );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Set a new timeout value.
|
||||||
|
*
|
||||||
|
* \param [in] obj The structure containing the timer object parameters.
|
||||||
|
* \param [in] value The new timeout value.
|
||||||
|
*/
|
||||||
|
void TimerSetValue( TimerEvent_t *obj, uint32_t value );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Read the current time.
|
||||||
|
*
|
||||||
|
* \retval time The current time.
|
||||||
|
*/
|
||||||
|
TimerTime_t TimerGetCurrentTime( void );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Return the time elapsed since a fixed moment in time.
|
||||||
|
*
|
||||||
|
* \param [in] savedTime The fixed moment in time.
|
||||||
|
* \retval time The elapsed time.
|
||||||
|
*/
|
||||||
|
TimerTime_t TimerGetElapsedTime( TimerTime_t savedTime );
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Return the time elapsed since a fixed moment in time.
|
||||||
|
*
|
||||||
|
* \param [in] eventInFuture The fixed moment in the future.
|
||||||
|
* \retval time The difference between now and a future event.
|
||||||
|
*/
|
||||||
|
TimerTime_t TimerGetFutureTime( TimerTime_t eventInFuture );
|
||||||
|
|
||||||
|
#endif // MBED_LORAWAN_SYS_TIMER_H__
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,250 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef LORAWAN_BASE_H_
|
||||||
|
#define LORAWAN_BASE_H_
|
||||||
|
|
||||||
|
#include "lorawan/system/lorawan_data_structures.h"
|
||||||
|
|
||||||
|
class LoRaWANBase {
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** Initialize the LoRa stack.
|
||||||
|
*
|
||||||
|
* You must call this before using the LoRa stack.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, a negative error code on
|
||||||
|
* failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t initialize() = 0;
|
||||||
|
|
||||||
|
/** Connect OTAA or ABP by setup.
|
||||||
|
*
|
||||||
|
* Connect by Over The Air Activation or Activation By Personalization.
|
||||||
|
* The connection type is selected at the setup.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, a negative error code on
|
||||||
|
* failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t connect() = 0;
|
||||||
|
|
||||||
|
/** Connect OTAA or ABP by parameters
|
||||||
|
*
|
||||||
|
* Connect by Over The Air Activation or Activation By Personalization.
|
||||||
|
* The connection type is selected using the parameters.
|
||||||
|
* You need to define the parameters in the main application.
|
||||||
|
*
|
||||||
|
* @param connect Options how end-device will connect to gateway
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, negative error code
|
||||||
|
* on failure
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t connect(const lorawan_connect_t &connect) = 0;
|
||||||
|
|
||||||
|
/** Disconnects the current session.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, a negative error code on failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t disconnect() = 0;
|
||||||
|
|
||||||
|
/** Sets up a particular data rate of choice
|
||||||
|
*
|
||||||
|
* @param data_rate Intended data rate e.g., DR_0, DR_1 etc.
|
||||||
|
* Caution is advised as the macro DR_* can mean different
|
||||||
|
* things while being in a different region.
|
||||||
|
* @return LORA_MAC_STATUS_OK if everything goes well, otherwise
|
||||||
|
* a negative error code.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t set_datarate(uint8_t data_rate) = 0;
|
||||||
|
|
||||||
|
/** Enables adaptive data rate (ADR)
|
||||||
|
*
|
||||||
|
* Underlying LoRaPHY and LoRaMac layers handle the data rate automatically
|
||||||
|
* for the user based upon radio conditions (network congestion).
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, negative error code
|
||||||
|
* on failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t enable_adaptive_datarate() = 0;
|
||||||
|
|
||||||
|
/** Disables adaptive data rate
|
||||||
|
*
|
||||||
|
* When adaptive data rate (ADR) is disabled, user can either set a certain
|
||||||
|
* data rate or the Mac layer will choose a default value.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, negative error code
|
||||||
|
* on failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t disable_adaptive_datarate() = 0;
|
||||||
|
|
||||||
|
/** Sets up retry counter for confirmed messages
|
||||||
|
*
|
||||||
|
* Valid only for confirmed messages.
|
||||||
|
*
|
||||||
|
* Number of trials to transmit the frame, if the LoRaMAC layer did not
|
||||||
|
* receive an acknowledgment. The MAC performs a data-rate adaptation,
|
||||||
|
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
|
||||||
|
* to the table on page 64.
|
||||||
|
*
|
||||||
|
* Note, that if the number of trials is set to 1 or 2, the MAC will not decrease
|
||||||
|
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment.
|
||||||
|
*
|
||||||
|
* @param count number of retries for confirmed messages
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK or a negative error code
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t set_confirmed_msg_retries(uint8_t count) = 0;
|
||||||
|
|
||||||
|
/** Sets channel plan
|
||||||
|
*
|
||||||
|
* @param channel_plan The defined channel plans to be set.
|
||||||
|
* @return 0 on success, a negative error code on failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t set_channel_plan(const lora_channelplan_t &channel_plan) = 0;
|
||||||
|
|
||||||
|
/** Gets the current channel plan.
|
||||||
|
*
|
||||||
|
* @param channel_plan The current channel information.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, a negative error
|
||||||
|
* code on failure.
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t get_channel_plan(lora_channelplan_t &channel_plan) = 0;
|
||||||
|
|
||||||
|
/** Removes currently active channel plan
|
||||||
|
*
|
||||||
|
* Default channels (channels where Base Stations are listening) are not
|
||||||
|
* allowed to be removed. So when a plan is abolished, only non-default
|
||||||
|
* channels are removed.
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, negative error
|
||||||
|
* code on failure
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t remove_channel_plan() = 0;
|
||||||
|
|
||||||
|
/** Removes a given single channel
|
||||||
|
*
|
||||||
|
* Default channels (channels where Base Stations are listening) are not
|
||||||
|
* allowed to be removed.
|
||||||
|
*
|
||||||
|
* @param index The channel index
|
||||||
|
*
|
||||||
|
* @return LORA_MAC_STATUS_OK on success, negative error
|
||||||
|
* code on failure
|
||||||
|
*/
|
||||||
|
virtual lora_mac_status_t remove_channel(uint8_t index) = 0;
|
||||||
|
|
||||||
|
/** Send message to gateway
|
||||||
|
*
|
||||||
|
* @param port The application port number. Port numbers 0 and 224
|
||||||
|
* are reserved, whereas port numbers from 1 to 223
|
||||||
|
* (0x01 to 0xDF) are valid port numbers.
|
||||||
|
* Anything out of this range is illegal.
|
||||||
|
*
|
||||||
|
* @param data A pointer to the data being sent. The ownership of the
|
||||||
|
* buffer is not transferred. The data is copied to the
|
||||||
|
* internal buffers.
|
||||||
|
*
|
||||||
|
* @param length The size of data in bytes.
|
||||||
|
*
|
||||||
|
* @param flags A flag used to determine what type of
|
||||||
|
* message is being sent, for example:
|
||||||
|
*
|
||||||
|
* MSG_UNCONFIRMED_FLAG = 0x01
|
||||||
|
* MSG_CONFIRMED_FLAG = 0x02
|
||||||
|
* MSG_MULTICAST_FLAG = 0x04
|
||||||
|
* MSG_PROPRIETARY_FLAG = 0x08
|
||||||
|
* MSG_MULTICAST_FLAG and MSG_PROPRIETARY_FLAG can be
|
||||||
|
* used in conjunction with MSG_UNCONFIRMED_FLAG and
|
||||||
|
* MSG_CONFIRMED_FLAG depending on the intended use.
|
||||||
|
*
|
||||||
|
* MSG_PROPRIETARY_FLAG|MSG_CONFIRMED_FLAG mask will set
|
||||||
|
* a confirmed message flag for a proprietary message.
|
||||||
|
* MSG_CONFIRMED_FLAG and MSG_UNCONFIRMED_FLAG are
|
||||||
|
* mutually exclusive.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return The number of bytes sent, or
|
||||||
|
* LORA_MAC_STATUS_WOULD_BLOCK if another TX is
|
||||||
|
* ongoing, or a negative error code on failure.
|
||||||
|
*/
|
||||||
|
virtual int16_t send(uint8_t port, const uint8_t* data,
|
||||||
|
uint16_t length, int flags) = 0;
|
||||||
|
|
||||||
|
/** Receives a message from the Network Server.
|
||||||
|
*
|
||||||
|
* @param port The application port number. Port numbers 0 and 224
|
||||||
|
* are reserved, whereas port numbers from 1 to 223
|
||||||
|
* (0x01 to 0xDF) are valid port numbers.
|
||||||
|
* Anything out of this range is illegal.
|
||||||
|
*
|
||||||
|
* @param data A pointer to buffer where the received data will be
|
||||||
|
* stored.
|
||||||
|
*
|
||||||
|
* @param length The size of data in bytes.
|
||||||
|
*
|
||||||
|
* @param flags A flag is used to determine what type of
|
||||||
|
* message is being sent, for example:
|
||||||
|
*
|
||||||
|
* MSG_UNCONFIRMED_FLAG = 0x01,
|
||||||
|
* MSG_CONFIRMED_FLAG = 0x02
|
||||||
|
* MSG_MULTICAST_FLAG = 0x04,
|
||||||
|
* MSG_PROPRIETARY_FLAG = 0x08
|
||||||
|
*
|
||||||
|
* MSG_MULTICAST_FLAG and MSG_PROPRIETARY_FLAG can be
|
||||||
|
* used in conjunction with MSG_UNCONFIRMED_FLAG and
|
||||||
|
* MSG_CONFIRMED_FLAG depending on the intended use.
|
||||||
|
*
|
||||||
|
* MSG_PROPRIETARY_FLAG|MSG_CONFIRMED_FLAG mask will set
|
||||||
|
* a confirmed message flag for a proprietary message.
|
||||||
|
*
|
||||||
|
* MSG_CONFIRMED_FLAG and MSG_UNCONFIRMED_FLAG are
|
||||||
|
* not mutually exclusive, i.e., the user can subscribe to
|
||||||
|
* receive both CONFIRMED AND UNCONFIRMED messages at
|
||||||
|
* the same time.
|
||||||
|
*
|
||||||
|
* @return It could be one of these:
|
||||||
|
* i) 0 if there is nothing else to read.
|
||||||
|
* ii) Number of bytes still pending to read.
|
||||||
|
* iii) LORA_MAC_STATUS_WOULD_BLOCK if there is
|
||||||
|
* nothing available to read at the moment.
|
||||||
|
* iv) A negative error code on failure.
|
||||||
|
*/
|
||||||
|
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length,
|
||||||
|
int flags) = 0;
|
||||||
|
|
||||||
|
/** Callback handler.
|
||||||
|
*
|
||||||
|
* Events that can be posted to user:
|
||||||
|
*
|
||||||
|
* CONNECTED - When the connection is complete
|
||||||
|
* DISCONNECTED - When the protocol is shut down in response to disconnect()
|
||||||
|
* TX_DONE - When a packet is sent
|
||||||
|
* TX_TIMEOUT, - When stack was unable to send packet in TX window
|
||||||
|
* TX_ERROR, - A general TX error
|
||||||
|
* TX_CRYPTO_ERROR, - If MIC fails, or any other crypto relted error
|
||||||
|
* TX_SCHEDULING_ERROR, - When stack is unable to schedule packet
|
||||||
|
* RX_DONE, - When there is something to receive
|
||||||
|
* RX_TIMEOUT, - Not yet mapped
|
||||||
|
* RX_ERROR - A general RX error
|
||||||
|
*
|
||||||
|
* @param cb A pointer to the callback function.
|
||||||
|
*/
|
||||||
|
virtual void lora_event_callback(mbed::Callback<void(lora_events_t)> cb) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* LORAWAN_BASE_H_ */
|
Loading…
Reference in New Issue