Skip to content

Commit 19883f1

Browse files
author
Kimmo Vaisanen
committed
Lora: Introduce new receive API which returns port and flags
This is a fix for issue #6389. Currently when application receives RX_DONE event from stack, it has to provide the correct port value to receive method in order to read the received message. The problem is that current API does not provide any way to know in to which port message was received. This commit introduces a new receive() method, which instead of checking these values, will return them to application.
1 parent 5e62d17 commit 19883f1

File tree

5 files changed

+118
-42
lines changed

5 files changed

+118
-42
lines changed

features/lorawan/LoRaWANBase.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class LoRaWANBase {
220220
virtual int16_t send(uint8_t port, const uint8_t* data,
221221
uint16_t length, int flags) = 0;
222222

223-
/** Receives a message from the Network Server.
223+
/** Receives a message from the Network Server on a specific port.
224224
*
225225
* @param port The application port number. Port numbers 0 and 224
226226
* are reserved, whereas port numbers from 1 to 223
@@ -259,8 +259,31 @@ class LoRaWANBase {
259259
* nothing available to read at the moment.
260260
* iv) A negative error code on failure.
261261
*/
262-
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length,
263-
int flags) = 0;
262+
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length, int flags) = 0;
263+
264+
/** Receives a message from the Network Server from any port.
265+
*
266+
* @param data A pointer to buffer where the received data will be
267+
* stored.
268+
*
269+
* @param length The size of data in bytes
270+
*
271+
* @param port Return the number of port to which message was received.
272+
*
273+
* @param flags Return flags to determine what type of message was received.
274+
* MSG_UNCONFIRMED_FLAG = 0x01
275+
* MSG_CONFIRMED_FLAG = 0x02
276+
* MSG_MULTICAST_FLAG = 0x04
277+
* MSG_PROPRIETARY_FLAG = 0x08
278+
*
279+
* @return It could be one of these:
280+
* i) 0 if there is nothing else to read.
281+
* ii) Number of bytes written to user buffer.
282+
* iii) LORAWAN_STATUS_WOULD_BLOCK if there is
283+
* nothing available to read at the moment.
284+
* iv) A negative error code on failure.
285+
*/
286+
virtual int16_t receive(uint8_t* data, uint16_t length, uint8_t& port, int& flags) = 0;
264287

265288
/** Add application callbacks to the stack.
266289
*

features/lorawan/LoRaWANInterface.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,19 @@ lorawan_status_t LoRaWANInterface::remove_channel_plan()
107107
return stk_obj().drop_channel_list();
108108
}
109109

110-
int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data,
111-
uint16_t length, int flags)
110+
int16_t LoRaWANInterface::send(uint8_t port, const uint8_t* data, uint16_t length, int flags)
112111
{
113112
return stk_obj().handle_tx(port, data, length, flags);
113+
}
114114

115+
int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length, int flags)
116+
{
117+
return stk_obj().handle_rx(data, length, port, flags, true);
115118
}
116119

117-
int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
118-
int flags)
120+
int16_t LoRaWANInterface::receive(uint8_t* data, uint16_t length, uint8_t& port, int& flags)
119121
{
120-
return stk_obj().handle_rx(port, data, length, flags);
122+
return stk_obj().handle_rx(data, length, port, flags, false);
121123
}
122124

123125
lorawan_status_t LoRaWANInterface::add_app_callbacks(lorawan_app_callbacks_t *callbacks)

features/lorawan/LoRaWANInterface.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class LoRaWANInterface: public LoRaWANBase {
317317
virtual int16_t send(uint8_t port, const uint8_t* data, uint16_t length,
318318
int flags);
319319

320-
/** Receives a message from the Network Server.
320+
/** Receives a message from the Network Server on a specific port.
321321
*
322322
* @param port The application port number. Port numbers 0 and 224
323323
* are reserved, whereas port numbers from 1 to 223
@@ -356,8 +356,31 @@ class LoRaWANInterface: public LoRaWANBase {
356356
* nothing available to read at the moment.
357357
* iv) A negative error code on failure.
358358
*/
359-
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length,
360-
int flags);
359+
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length, int flags);
360+
361+
/** Receives a message from the Network Server on any port.
362+
*
363+
* @param data A pointer to buffer where the received data will be
364+
* stored.
365+
*
366+
* @param length The size of data in bytes
367+
*
368+
* @param port Return the number of port to which message was received.
369+
*
370+
* @param flags Return flags to determine what type of message was received.
371+
* MSG_UNCONFIRMED_FLAG = 0x01
372+
* MSG_CONFIRMED_FLAG = 0x02
373+
* MSG_MULTICAST_FLAG = 0x04
374+
* MSG_PROPRIETARY_FLAG = 0x08
375+
*
376+
* @return It could be one of these:
377+
* i) 0 if there is nothing else to read.
378+
* ii) Number of bytes written to user buffer.
379+
* iii) LORAWAN_STATUS_WOULD_BLOCK if there is
380+
* nothing available to read at the moment.
381+
* iv) A negative error code on failure.
382+
*/
383+
virtual int16_t receive(uint8_t* data, uint16_t length, uint8_t& port, int& flags);
361384

362385
/** Add application callbacks to the stack.
363386
*

features/lorawan/LoRaWANStack.cpp

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,35 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
386386
return (status == LORAWAN_STATUS_OK) ? len : (int16_t) status;
387387
}
388388

389-
int16_t LoRaWANStack::handle_rx(const uint8_t port, uint8_t* data,
390-
uint16_t length, uint8_t flags)
389+
int convert_to_msg_flag(const mcps_type_t type)
390+
{
391+
int msg_flag = MSG_UNCONFIRMED_FLAG;
392+
switch (type) {
393+
case MCPS_UNCONFIRMED:
394+
msg_flag = MSG_UNCONFIRMED_FLAG;
395+
break;
396+
397+
case MCPS_CONFIRMED:
398+
msg_flag = MSG_CONFIRMED_FLAG;
399+
break;
400+
401+
case MCPS_MULTICAST:
402+
msg_flag = MSG_MULTICAST_FLAG;
403+
break;
404+
405+
case MCPS_PROPRIETARY:
406+
msg_flag = MSG_PROPRIETARY_FLAG;
407+
break;
408+
409+
default:
410+
tr_error("Unknown message type!");
411+
MBED_ASSERT(0);
412+
}
413+
414+
return msg_flag;
415+
}
416+
417+
int16_t LoRaWANStack::handle_rx(uint8_t* data, uint16_t length, uint8_t& port, int& flags, bool validate_params)
391418
{
392419
if (!_lw_session.active) {
393420
return LORAWAN_STATUS_NO_ACTIVE_SESSIONS;
@@ -408,36 +435,28 @@ int16_t LoRaWANStack::handle_rx(const uint8_t port, uint8_t* data,
408435
return LORAWAN_STATUS_PARAMETER_INVALID;
409436
}
410437

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

415-
if (_rx_msg.msg.mcps_indication.port != port) {
416-
// Nothing yet received for this particular port
417-
return LORAWAN_STATUS_WOULD_BLOCK;
443+
if (_rx_msg.msg.mcps_indication.port != port || !(flags & received_flags)) {
444+
return LORAWAN_STATUS_WOULD_BLOCK;
445+
}
418446
}
419447

420-
// check if message received is a Confirmed message and user subscribed to it or not
421-
if (_rx_msg.msg.mcps_indication.type == MCPS_CONFIRMED
422-
&& ((flags & MSG_FLAG_MASK) == MSG_CONFIRMED_FLAG
423-
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_MULTICAST
424-
|| (flags & MSG_FLAG_MASK) == MSG_CONFIRMED_PROPRIETARY)) {
425-
426-
tr_debug("RX - Confirmed Message, flags=%d", flags);
427-
}
448+
// Report values back to user
449+
port = _rx_msg.msg.mcps_indication.port;
450+
flags = received_flags;
428451

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

437456
// check the length of received message whether we can fit into user
438457
// buffer completely or not
439458
if (_rx_msg.msg.mcps_indication.buffer_size > length &&
440-
_rx_msg.prev_read_size == 0) {
459+
_rx_msg.prev_read_size == 0) {
441460
// we can't fit into user buffer. Invoke counter measures
442461
_rx_msg.pending_size = _rx_msg.msg.mcps_indication.buffer_size - length;
443462
base_size = length;
@@ -613,12 +632,12 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
613632
default: {
614633
if (is_port_valid(mcps_indication->port) == true ||
615634
mcps_indication->type == MCPS_PROPRIETARY) {
616-
617635
// Valid message arrived.
618636
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
619637
_rx_msg.msg.mcps_indication.buffer_size = mcps_indication->buffer_size;
620638
_rx_msg.msg.mcps_indication.port = mcps_indication->port;
621639
_rx_msg.msg.mcps_indication.buffer = mcps_indication->buffer;
640+
_rx_msg.msg.mcps_indication.type = mcps_indication->type;
622641

623642
// Notify application about received frame..
624643
tr_debug("Received %d bytes", _rx_msg.msg.mcps_indication.buffer_size);

features/lorawan/LoRaWANStack.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,19 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
298298
uint16_t length, uint8_t flags, bool null_allowed = false);
299299

300300
/** Receives a message from the Network Server.
301+
*
302+
* @param data A pointer to buffer where the received data will be
303+
* stored.
304+
*
305+
* @param length The size of data in bytes
301306
*
302307
* @param port The application port number. Port numbers 0 and 224
303308
* are reserved, whereas port numbers from 1 to 223
304309
* (0x01 to 0xDF) are valid port numbers.
305310
* Anything out of this range is illegal.
306311
*
307-
* @param data A pointer to buffer where the received data will be
308-
* stored.
309-
*
310-
* @param length The size of data in bytes
312+
* In return will contain the number of port to which
313+
* message was received.
311314
*
312315
* @param flags A flag is used to determine what type of
313316
* message is being received, for example:
@@ -329,15 +332,21 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
329332
* receive both CONFIRMED AND UNCONFIRMED messages at
330333
* the same time.
331334
*
335+
* In return will contain the flags to determine what kind
336+
* of message was received.
337+
*
338+
* @param validate_params If set to true, the given port and flags values will be checked
339+
* against the values received with the message. If values do not
340+
* match, LORAWAN_STATUS_WOULD_BLOCK will be returned.
341+
*
332342
* @return It could be one of these:
333343
* i) 0 if there is nothing else to read.
334344
* ii) Number of bytes written to user buffer.
335345
* iii) LORAWAN_STATUS_WOULD_BLOCK if there is
336346
* nothing available to read at the moment.
337347
* iv) A negative error code on failure.
338348
*/
339-
int16_t handle_rx(const uint8_t port, uint8_t* data,
340-
uint16_t length, uint8_t flags);
349+
int16_t handle_rx(uint8_t* data, uint16_t length, uint8_t& port, int& flags, bool validate_params);
341350

342351
/** Send Link Check Request MAC command.
343352
*

0 commit comments

Comments
 (0)