Skip to content

Commit

Permalink
Streamlining connect() API with posix like retcodes
Browse files Browse the repository at this point in the history
For ABP: First call to connect() or connect(params) will return LORAWAN_STATUS_OK
         and a CONNECTED event will be sent. Any subsequent call will return
         LORAWAN_STATUS_ALREADY_CONNECTED (posix EISCONN) and no event is generated.

FOR OTAA: First call to connect() or connect(params) will return LORAWAN_STATUS_CONNECT_IN_PROGRESS
          and a CONNECTED event will be sent whenever the JoinAccept is received. If the application
          calls connect again before receiving the CONNECTED event, LORAWAN_STATUS_BUSY will be returned.
          After the CONNECTED event is dispatched, any subsequent call to connect() or connect(params) API
          will be returned with LORWAN_STATUS_ALREADY_CONNECTED.

No new parameters are accepted after the first call. The application must disconnect before making
a connect() call with new parameters.
  • Loading branch information
Hasnain Virk committed Jul 10, 2018
1 parent 24db1c8 commit f0844b4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 58 deletions.
30 changes: 15 additions & 15 deletions features/lorawan/LoRaWANBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class LoRaWANBase {
* Connect by Over The Air Activation or Activation By Personalization.
* The connection type is selected at the setup.
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
virtual lorawan_status_t connect() = 0;

Expand All @@ -60,14 +60,14 @@ class LoRaWANBase {
*
* @param connect Options how end-device will connect to gateway
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
virtual lorawan_status_t connect(const lorawan_connect_t &connect) = 0;

Expand Down
30 changes: 15 additions & 15 deletions features/lorawan/LoRaWANInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ class LoRaWANInterface: public LoRaWANBase {
* is important, at least for ABP. That's why we try to restore frame counters from
* session information after a disconnection.
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
virtual lorawan_status_t connect();

Expand Down Expand Up @@ -124,14 +124,14 @@ class LoRaWANInterface: public LoRaWANBase {
*
* @param connect Options for an end device connection to the gateway.
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
virtual lorawan_status_t connect(const lorawan_connect_t &connect);

Expand Down
32 changes: 18 additions & 14 deletions features/lorawan/LoRaWANStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ SPDX-License-Identifier: BSD-3-Clause
#define CONNECTED_FLAG 0x00000004
#define USING_OTAA_FLAG 0x00000008
#define TX_DONE_FLAG 0x00000010
#define CONN_IN_PROGRESS_FLAG 0x00000020

using namespace mbed;
using namespace events;
Expand Down Expand Up @@ -155,7 +156,11 @@ lorawan_status_t LoRaWANStack::connect()
return LORAWAN_STATUS_NOT_INITIALIZED;
}

if (_loramac.nwk_joined()) {
if (_ctrl_flags & CONN_IN_PROGRESS_FLAG) {
return LORAWAN_STATUS_BUSY;
}

if (_ctrl_flags & CONNECTED_FLAG) {
return LORAWAN_STATUS_ALREADY_CONNECTED;
}

Expand All @@ -174,7 +179,11 @@ lorawan_status_t LoRaWANStack::connect(const lorawan_connect_t &connect)
return LORAWAN_STATUS_NOT_INITIALIZED;
}

if (_loramac.nwk_joined()) {
if (_ctrl_flags & CONN_IN_PROGRESS_FLAG) {
return LORAWAN_STATUS_BUSY;
}

if (_ctrl_flags & CONNECTED_FLAG) {
return LORAWAN_STATUS_ALREADY_CONNECTED;
}

Expand Down Expand Up @@ -832,6 +841,8 @@ int LoRaWANStack::convert_to_msg_flag(const mcps_type_t type)

lorawan_status_t LoRaWANStack::handle_connect(bool is_otaa)
{
_ctrl_flags |= CONN_IN_PROGRESS_FLAG;

if (is_otaa) {
tr_debug("Initiating OTAA");

Expand Down Expand Up @@ -1157,30 +1168,23 @@ void LoRaWANStack::process_joining_state(lorawan_status_t &op_status)

void LoRaWANStack::process_connected_state()
{
_ctrl_flags |= CONNECTED_FLAG;
_ctrl_flags &= ~CONN_IN_PROGRESS_FLAG;

if (_ctrl_flags & USING_OTAA_FLAG) {
tr_debug("OTAA Connection OK!");
}

_lw_session.active = true;
send_event_to_application(CONNECTED);
_ctrl_flags |= CONNECTED_FLAG;

_device_current_state = DEVICE_STATE_IDLE;
}

void LoRaWANStack::process_connecting_state(lorawan_status_t &op_status)
{
if (_device_current_state != DEVICE_STATE_IDLE
&& _device_current_state != DEVICE_STATE_SHUTDOWN) {
op_status = LORAWAN_STATUS_BUSY;
return;
}

if (_ctrl_flags & CONNECTED_FLAG) {
tr_debug("Already connected");
op_status = LORAWAN_STATUS_ALREADY_CONNECTED;
return;
}
MBED_ASSERT(_device_current_state == DEVICE_STATE_IDLE ||
_device_current_state == DEVICE_STATE_SHUTDOWN);

_device_current_state = DEVICE_STATE_CONNECTING;

Expand Down
28 changes: 14 additions & 14 deletions features/lorawan/LoRaWANStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,29 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {

/** Connect OTAA or ABP using Mbed-OS config system
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
lorawan_status_t connect();

/** Connect OTAA or ABP with parameters
*
* @param connect Options for an end device connection to the gateway.
*
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
* a 'CONNECTED' event. Otherwise a negative error code is returned.
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
*
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
* event follows.
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
*/
lorawan_status_t connect(const lorawan_connect_t &connect);

Expand Down

0 comments on commit f0844b4

Please sign in to comment.