Skip to content

Commit f0844b4

Browse files
author
Hasnain Virk
committed
Streamlining connect() API with posix like retcodes
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.
1 parent 24db1c8 commit f0844b4

File tree

4 files changed

+62
-58
lines changed

4 files changed

+62
-58
lines changed

features/lorawan/LoRaWANBase.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class LoRaWANBase {
4141
* Connect by Over The Air Activation or Activation By Personalization.
4242
* The connection type is selected at the setup.
4343
*
44-
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
45-
* a 'CONNECTED' event. Otherwise a negative error code.
46-
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
47-
*
48-
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
49-
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
50-
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
51-
* event follows.
44+
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
45+
* a 'CONNECTED' event. Otherwise a negative error code is returned.
46+
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
47+
*
48+
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
49+
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
50+
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
51+
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
5252
*/
5353
virtual lorawan_status_t connect() = 0;
5454

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

features/lorawan/LoRaWANInterface.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ class LoRaWANInterface: public LoRaWANBase {
7979
* is important, at least for ABP. That's why we try to restore frame counters from
8080
* session information after a disconnection.
8181
*
82-
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
83-
* a 'CONNECTED' event. Otherwise a negative error code.
84-
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
85-
*
86-
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned followed by
87-
* a 'CONNECTED' event when the JoinAccept is received. Otherwise a negative error code
88-
* is returned. Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no
89-
* event follows.
82+
* @return For ABP: If everything goes well, LORAWAN_STATUS_OK is returned for first call followed by
83+
* a 'CONNECTED' event. Otherwise a negative error code is returned.
84+
* Any subsequent call will return LORAWAN_STATUS_ALREADY_CONNECTED and no event follows.
85+
*
86+
* For OTAA: When a JoinRequest is sent, LORAWAN_STATUS_CONNECT_IN_PROGRESS is returned for the first call.
87+
* Any subsequent call will return either LORAWAN_STATUS_BUSY (if the previous request for connection
88+
* is still underway) or LORAWAN_STATUS_ALREADY_CONNECTED (if a network was already joined successfully).
89+
* A 'CONNECTED' event is sent to the application when the JoinAccept is received.
9090
*/
9191
virtual lorawan_status_t connect();
9292

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

features/lorawan/LoRaWANStack.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ SPDX-License-Identifier: BSD-3-Clause
4444
#define CONNECTED_FLAG 0x00000004
4545
#define USING_OTAA_FLAG 0x00000008
4646
#define TX_DONE_FLAG 0x00000010
47+
#define CONN_IN_PROGRESS_FLAG 0x00000020
4748

4849
using namespace mbed;
4950
using namespace events;
@@ -155,7 +156,11 @@ lorawan_status_t LoRaWANStack::connect()
155156
return LORAWAN_STATUS_NOT_INITIALIZED;
156157
}
157158

158-
if (_loramac.nwk_joined()) {
159+
if (_ctrl_flags & CONN_IN_PROGRESS_FLAG) {
160+
return LORAWAN_STATUS_BUSY;
161+
}
162+
163+
if (_ctrl_flags & CONNECTED_FLAG) {
159164
return LORAWAN_STATUS_ALREADY_CONNECTED;
160165
}
161166

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

177-
if (_loramac.nwk_joined()) {
182+
if (_ctrl_flags & CONN_IN_PROGRESS_FLAG) {
183+
return LORAWAN_STATUS_BUSY;
184+
}
185+
186+
if (_ctrl_flags & CONNECTED_FLAG) {
178187
return LORAWAN_STATUS_ALREADY_CONNECTED;
179188
}
180189

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

833842
lorawan_status_t LoRaWANStack::handle_connect(bool is_otaa)
834843
{
844+
_ctrl_flags |= CONN_IN_PROGRESS_FLAG;
845+
835846
if (is_otaa) {
836847
tr_debug("Initiating OTAA");
837848

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

11581169
void LoRaWANStack::process_connected_state()
11591170
{
1171+
_ctrl_flags |= CONNECTED_FLAG;
1172+
_ctrl_flags &= ~CONN_IN_PROGRESS_FLAG;
1173+
11601174
if (_ctrl_flags & USING_OTAA_FLAG) {
11611175
tr_debug("OTAA Connection OK!");
11621176
}
11631177

11641178
_lw_session.active = true;
11651179
send_event_to_application(CONNECTED);
1166-
_ctrl_flags |= CONNECTED_FLAG;
11671180

11681181
_device_current_state = DEVICE_STATE_IDLE;
11691182
}
11701183

11711184
void LoRaWANStack::process_connecting_state(lorawan_status_t &op_status)
11721185
{
1173-
if (_device_current_state != DEVICE_STATE_IDLE
1174-
&& _device_current_state != DEVICE_STATE_SHUTDOWN) {
1175-
op_status = LORAWAN_STATUS_BUSY;
1176-
return;
1177-
}
1178-
1179-
if (_ctrl_flags & CONNECTED_FLAG) {
1180-
tr_debug("Already connected");
1181-
op_status = LORAWAN_STATUS_ALREADY_CONNECTED;
1182-
return;
1183-
}
1186+
MBED_ASSERT(_device_current_state == DEVICE_STATE_IDLE ||
1187+
_device_current_state == DEVICE_STATE_SHUTDOWN);
11841188

11851189
_device_current_state = DEVICE_STATE_CONNECTING;
11861190

features/lorawan/LoRaWANStack.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,29 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
8484

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

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

0 commit comments

Comments
 (0)