Skip to content

Lora: Introduce new receive API which returns port and flags #6586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions features/lorawan/LoRaWANBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class LoRaWANBase {
virtual int16_t send(uint8_t port, const uint8_t* data,
uint16_t length, int flags) = 0;

/** Receives a message from the Network Server.
/** Receives a message from the Network Server on a specific port.
*
* @param port The application port number. Port numbers 0 and 224
* are reserved, whereas port numbers from 1 to 223
Expand Down Expand Up @@ -259,8 +259,31 @@ class LoRaWANBase {
* 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;
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length, int flags) = 0;

/** Receives a message from the Network Server from any port.
*
* @param data A pointer to buffer where the received data will be
* stored.
*
* @param length The size of data in bytes
*
* @param port Return the number of port to which message was received.
*
* @param flags Return flags to determine what type of message was received.
* MSG_UNCONFIRMED_FLAG = 0x01
* MSG_CONFIRMED_FLAG = 0x02
* MSG_MULTICAST_FLAG = 0x04
* MSG_PROPRIETARY_FLAG = 0x08
*
* @return It could be one of these:
* i) 0 if there is nothing else to read.
* ii) Number of bytes written to user buffer.
* iii) LORAWAN_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* data, uint16_t length, uint8_t& port, int& flags) = 0;

/** Add application callbacks to the stack.
*
Expand Down
12 changes: 7 additions & 5 deletions features/lorawan/LoRaWANInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,19 @@ lorawan_status_t LoRaWANInterface::remove_channel_plan()
return stk_obj().drop_channel_list();
}

int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data,
uint16_t length, int flags)
int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data, uint16_t length, int flags)
{
return stk_obj().handle_tx(port, data, length, flags);
}

int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length, int flags)
{
return stk_obj().handle_rx(data, length, port, flags, true);
}

int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
int flags)
int16_t LoRaWANInterface::receive(uint8_t* data, uint16_t length, uint8_t& port, int& flags)
{
return stk_obj().handle_rx(port, data, length, flags);
return stk_obj().handle_rx(data, length, port, flags, false);
}

lorawan_status_t LoRaWANInterface::add_app_callbacks(lorawan_app_callbacks_t *callbacks)
Expand Down
29 changes: 26 additions & 3 deletions features/lorawan/LoRaWANInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class LoRaWANInterface: public LoRaWANBase {
virtual int16_t send(uint8_t port, const uint8_t* data, uint16_t length,
int flags);

/** Receives a message from the Network Server.
/** Receives a message from the Network Server on a specific port.
*
* @param port The application port number. Port numbers 0 and 224
* are reserved, whereas port numbers from 1 to 223
Expand Down Expand Up @@ -356,8 +356,31 @@ class LoRaWANInterface: public LoRaWANBase {
* 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);
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length, int flags);

/** Receives a message from the Network Server on any port.
*
* @param data A pointer to buffer where the received data will be
* stored.
*
* @param length The size of data in bytes
*
* @param port Return the number of port to which message was received.
*
* @param flags Return flags to determine what type of message was received.
* MSG_UNCONFIRMED_FLAG = 0x01
* MSG_CONFIRMED_FLAG = 0x02
* MSG_MULTICAST_FLAG = 0x04
* MSG_PROPRIETARY_FLAG = 0x08
*
* @return It could be one of these:
* i) 0 if there is nothing else to read.
* ii) Number of bytes written to user buffer.
* iii) LORAWAN_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* data, uint16_t length, uint8_t& port, int& flags);

/** Add application callbacks to the stack.
*
Expand Down
69 changes: 44 additions & 25 deletions features/lorawan/LoRaWANStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,35 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
return (status == LORAWAN_STATUS_OK) ? len : (int16_t) status;
}

int16_t LoRaWANStack::handle_rx(const uint8_t port, uint8_t* data,
uint16_t length, uint8_t flags)
int convert_to_msg_flag(const mcps_type_t type)
{
int msg_flag = MSG_UNCONFIRMED_FLAG;
switch (type) {
case MCPS_UNCONFIRMED:
msg_flag = MSG_UNCONFIRMED_FLAG;
break;

case MCPS_CONFIRMED:
msg_flag = MSG_CONFIRMED_FLAG;
break;

case MCPS_MULTICAST:
msg_flag = MSG_MULTICAST_FLAG;
break;

case MCPS_PROPRIETARY:
msg_flag = MSG_PROPRIETARY_FLAG;
break;

default:
tr_error("Unknown message type!");
MBED_ASSERT(0);
}

return msg_flag;
}

int16_t LoRaWANStack::handle_rx(uint8_t* data, uint16_t length, uint8_t& port, int& flags, bool validate_params)
{
if (!_lw_session.active) {
return LORAWAN_STATUS_NO_ACTIVE_SESSIONS;
Expand All @@ -408,36 +435,28 @@ int16_t LoRaWANStack::handle_rx(const uint8_t port, uint8_t* data,
return LORAWAN_STATUS_PARAMETER_INVALID;
}

uint8_t *base_ptr = _rx_msg.msg.mcps_indication.buffer;
uint16_t base_size = _rx_msg.msg.mcps_indication.buffer_size;
bool read_complete = false;
int received_flags = convert_to_msg_flag(_rx_msg.msg.mcps_indication.type);
if (validate_params) {
// Check received message port and flags match with the ones requested by user
received_flags &= MSG_FLAG_MASK;

if (_rx_msg.msg.mcps_indication.port != port) {
// Nothing yet received for this particular port
return LORAWAN_STATUS_WOULD_BLOCK;
if (_rx_msg.msg.mcps_indication.port != port || !(flags & received_flags)) {
return LORAWAN_STATUS_WOULD_BLOCK;
}
}

// check if message received is a Confirmed message and user subscribed to it or not
if (_rx_msg.msg.mcps_indication.type == MCPS_CONFIRMED
&& ((flags & MSG_FLAG_MASK) == MSG_CONFIRMED_FLAG
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_MULTICAST
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_PROPRIETARY)) {

tr_debug("RX - Confirmed Message, flags=%d", flags);
}
// Report values back to user
port = _rx_msg.msg.mcps_indication.port;
flags = received_flags;

// check if message received is a Unconfirmed message and user subscribed to it or not
if (_rx_msg.msg.mcps_indication.type == MCPS_UNCONFIRMED
&& ((flags & MSG_FLAG_MASK) == MSG_UNCONFIRMED_FLAG
|| (flags & MSG_FLAG_MASK) == MSG_UNCONFIRMED_MULTICAST
|| (flags & MSG_FLAG_MASK) == MSG_UNCONFIRMED_PROPRIETARY)) {
tr_debug("RX - Unconfirmed Message - flags=%d", flags);
}
const uint8_t *base_ptr = _rx_msg.msg.mcps_indication.buffer;
uint16_t base_size = _rx_msg.msg.mcps_indication.buffer_size;
bool read_complete = false;

// check the length of received message whether we can fit into user
// buffer completely or not
if (_rx_msg.msg.mcps_indication.buffer_size > length &&
_rx_msg.prev_read_size == 0) {
_rx_msg.prev_read_size == 0) {
// we can't fit into user buffer. Invoke counter measures
_rx_msg.pending_size = _rx_msg.msg.mcps_indication.buffer_size - length;
base_size = length;
Expand Down Expand Up @@ -613,12 +632,12 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
default: {
if (is_port_valid(mcps_indication->port) == true ||
mcps_indication->type == MCPS_PROPRIETARY) {

// Valid message arrived.
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
_rx_msg.msg.mcps_indication.buffer_size = mcps_indication->buffer_size;
_rx_msg.msg.mcps_indication.port = mcps_indication->port;
_rx_msg.msg.mcps_indication.buffer = mcps_indication->buffer;
_rx_msg.msg.mcps_indication.type = mcps_indication->type;

// Notify application about received frame..
tr_debug("Received %d bytes", _rx_msg.msg.mcps_indication.buffer_size);
Expand Down
21 changes: 15 additions & 6 deletions features/lorawan/LoRaWANStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,19 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
uint16_t length, uint8_t flags, bool null_allowed = false);

/** Receives a message from the Network Server.
*
* @param data A pointer to buffer where the received data will be
* stored.
*
* @param length The size of data in bytes
*
* @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
* In return will contain the number of port to which
* message was received.
*
* @param flags A flag is used to determine what type of
* message is being received, for example:
Expand All @@ -329,15 +332,21 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
* receive both CONFIRMED AND UNCONFIRMED messages at
* the same time.
*
* In return will contain the flags to determine what kind
* of message was received.
*
* @param validate_params If set to true, the given port and flags values will be checked
* against the values received with the message. If values do not
* match, LORAWAN_STATUS_WOULD_BLOCK will be returned.
*
* @return It could be one of these:
* i) 0 if there is nothing else to read.
* ii) Number of bytes written to user buffer.
* iii) LORAWAN_STATUS_WOULD_BLOCK if there is
* nothing available to read at the moment.
* iv) A negative error code on failure.
*/
int16_t handle_rx(const uint8_t port, uint8_t* data,
uint16_t length, uint8_t flags);
int16_t handle_rx(uint8_t* data, uint16_t length, uint8_t& port, int& flags, bool validate_params);

/** Send Link Check Request MAC command.
*
Expand Down