Skip to content

Commit 94103f9

Browse files
author
Cruz Monrreal
authored
Merge pull request #6586 from kivaisan/new_receive_method
Lora: Introduce new receive API which returns port and flags
2 parents e1a63f9 + 19883f1 commit 94103f9

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
@@ -387,8 +387,35 @@ int16_t LoRaWANStack::handle_tx(uint8_t port, const uint8_t* data,
387387
return (status == LORAWAN_STATUS_OK) ? len : (int16_t) status;
388388
}
389389

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

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

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

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

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

438457
// check the length of received message whether we can fit into user
439458
// buffer completely or not
440459
if (_rx_msg.msg.mcps_indication.buffer_size > length &&
441-
_rx_msg.prev_read_size == 0) {
460+
_rx_msg.prev_read_size == 0) {
442461
// we can't fit into user buffer. Invoke counter measures
443462
_rx_msg.pending_size = _rx_msg.msg.mcps_indication.buffer_size - length;
444463
base_size = length;
@@ -614,12 +633,12 @@ void LoRaWANStack::mcps_indication_handler(loramac_mcps_indication_t *mcps_indic
614633
default: {
615634
if (is_port_valid(mcps_indication->port) == true ||
616635
mcps_indication->type == MCPS_PROPRIETARY) {
617-
618636
// Valid message arrived.
619637
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
620638
_rx_msg.msg.mcps_indication.buffer_size = mcps_indication->buffer_size;
621639
_rx_msg.msg.mcps_indication.port = mcps_indication->port;
622640
_rx_msg.msg.mcps_indication.buffer = mcps_indication->buffer;
641+
_rx_msg.msg.mcps_indication.type = mcps_indication->type;
623642

624643
// Notify application about received frame..
625644
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
@@ -299,16 +299,19 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
299299
uint16_t length, uint8_t flags, bool null_allowed = false);
300300

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

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

0 commit comments

Comments
 (0)