Skip to content

Commit 847a802

Browse files
fabik111mirkokurt
authored andcommitted
refactor Lora handler like wifi handler
1 parent 50c8425 commit 847a802

File tree

2 files changed

+121
-132
lines changed

2 files changed

+121
-132
lines changed

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 114 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_a
4343
_on_connect_event_callback(NULL),
4444
_on_disconnect_event_callback(NULL),
4545
_on_error_event_callback(NULL) {
46-
netConnectionState = NetworkConnectionState::INIT;
46+
netConnectionState = NetworkConnectionState::INIT;
4747
}
4848

4949
/******************************************************************************
@@ -93,22 +93,39 @@ int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) {
9393
err = modem.endPacket(true);
9494
/*Error manager according pr #68 of MKRWAN repo*/
9595
if (err != size) {
96-
switch (err) {
97-
case -20: {Serial.println("Message length is bigger than max LoRa packet!");} break;
98-
case -1: {Serial.println("Message ack was not received, the message could not be delivered");} break;
99-
case -2: {Serial.println("LoRa generic error (LORA_ERROR)");} break;
100-
case -3: {Serial.println("LoRa malformed param error (LORA_ERROR_PARAM");} break;
101-
case -4: {Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)");} break;
102-
case -5: {Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)");} break;
103-
case -6: {Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)");} break;
104-
case -7: {Serial.println("LoRa rx error (LORA_ERROR_RX)");} break;
105-
case -8: {Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)");} break;
106-
}
107-
}
108-
else {
109-
Serial.println("Message sent correctly!");
110-
}
111-
return err;
96+
switch (err) {
97+
case -20: {
98+
Serial.println("Message length is bigger than max LoRa packet!");
99+
} break;
100+
case -1: {
101+
Serial.println("Message ack was not received, the message could not be delivered");
102+
} break;
103+
case -2: {
104+
Serial.println("LoRa generic error (LORA_ERROR)");
105+
} break;
106+
case -3: {
107+
Serial.println("LoRa malformed param error (LORA_ERROR_PARAM");
108+
} break;
109+
case -4: {
110+
Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)");
111+
} break;
112+
case -5: {
113+
Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)");
114+
} break;
115+
case -6: {
116+
Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)");
117+
} break;
118+
case -7: {
119+
Serial.println("LoRa rx error (LORA_ERROR_RX)");
120+
} break;
121+
case -8: {
122+
Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)");
123+
} break;
124+
}
125+
} else {
126+
Serial.println("Message sent correctly!");
127+
}
128+
return err;
112129
}
113130

114131
int LoRaConnectionHandler::read() {
@@ -126,138 +143,105 @@ void LoRaConnectionHandler::update() {
126143
if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */
127144

128145
lastConnectionTickTime = now;
129-
130146
switch (netConnectionState) {
131-
case NetworkConnectionState::INIT: {
132-
Debug.print(DBG_VERBOSE, "::INIT");
133-
if (!modem.begin(band)) {
134-
Debug.print(DBG_VERBOSE, "Failed to start module");
135-
changeConnectionState(NetworkConnectionState::ERROR);
136-
};
137-
delay(1000);
138-
139-
changeConnectionState(NetworkConnectionState::CONNECTING);
140-
}
141-
break;
142-
case NetworkConnectionState::CONNECTING: {
143-
Debug.print(DBG_VERBOSE, "::CONNECTING");
144-
networkStatus = modem.joinOTAA(appeui, appkey);
145-
if (networkStatus != true) {
146-
changeConnectionState(NetworkConnectionState::ERROR);
147-
return;
148-
}
149-
150-
Debug.print(DBG_INFO, "Connected to the network");
151-
changeConnectionState(NetworkConnectionState::CONNECTED);
152-
return;
153-
}
154-
break;
155-
case NetworkConnectionState::CONNECTED: {
156-
157-
networkStatus = modem.connected();
158-
Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus);
159-
if (networkStatus != true) {
160-
changeConnectionState(NetworkConnectionState::DISCONNECTED);
161-
return;
162-
}
163-
Debug.print(DBG_VERBOSE, "Connected to the network");
164-
}
165-
break;
166-
case NetworkConnectionState::DISCONNECTING: {
167-
changeConnectionState(NetworkConnectionState::DISCONNECTED);
168-
}
169-
break;
170-
case NetworkConnectionState::DISCONNECTED: {
171-
if (keepAlive) {
172-
changeConnectionState(NetworkConnectionState::INIT);
173-
} else {
174-
changeConnectionState(NetworkConnectionState::CLOSED);
175-
}
176-
}
177-
break;
178-
case NetworkConnectionState::ERROR: {
179-
180-
}
181-
break;
182-
case NetworkConnectionState::CLOSED: {
183-
184-
}
185-
break;
147+
case NetworkConnectionState::INIT: netConnectionState = update_handleInit(); break;
148+
case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting(); break;
149+
case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected(); break;
150+
case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break;
151+
case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected(); break;
152+
case NetworkConnectionState::ERROR: break;
153+
case NetworkConnectionState::CLOSED: break;
186154
}
187-
} /* time bracket */
155+
}
188156
}
189157

190158
/******************************************************************************
191159
PRIVATE MEMBER FUNCTIONS
192160
******************************************************************************/
193161

194-
void LoRaConnectionHandler::changeConnectionState(NetworkConnectionState _newState) {
195-
if (_newState == netConnectionState) {
196-
return;
162+
NetworkConnectionState LoRaConnectionHandler::update_handleInit() {
163+
Debug.print(DBG_VERBOSE, "::INIT");
164+
if (!modem.begin(band)) {
165+
Debug.print(DBG_VERBOSE, "Failed to start module");
166+
execNetworkEventCallback(_on_error_event_callback, 0);
167+
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
168+
};
169+
delay(1000);
170+
Debug.print(DBG_INFO, "Connecting to the network");
171+
connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING;
172+
return NetworkConnectionState::CONNECTING;
173+
}
174+
175+
NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() {
176+
Debug.print(DBG_VERBOSE, "::CONNECTING");
177+
bool networkStatus = modem.joinOTAA(appeui, appkey);
178+
if (networkStatus != true) {
179+
execNetworkEventCallback(_on_error_event_callback, 0);
180+
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
181+
return NetworkConnectionState::ERROR;
197182
}
198-
int newInterval = CHECK_INTERVAL_INIT;
199-
switch (_newState) {
200-
case NetworkConnectionState::INIT: {
201-
Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT");
202-
newInterval = CHECK_INTERVAL_INIT;
203-
}
204-
break;
205-
case NetworkConnectionState::CONNECTING: {
206-
Debug.print(DBG_INFO, "Connecting to the network");
207-
newInterval = CHECK_INTERVAL_CONNECTING;
208-
}
209-
break;
210-
case NetworkConnectionState::CONNECTED: {
211-
execNetworkEventCallback(_on_connect_event_callback, 0);
212-
newInterval = CHECK_INTERVAL_CONNECTED;
213-
}
214-
break;
215-
case NetworkConnectionState::GETTIME: {
216-
}
217-
break;
218-
case NetworkConnectionState::DISCONNECTING: {
219-
Debug.print(DBG_VERBOSE, "Disconnecting from the network");
220-
}
221-
break;
222-
case NetworkConnectionState::DISCONNECTED: {
223-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
224-
//Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
225-
226-
Debug.print(DBG_ERROR, "Connection to the network lost.");
227-
if (keepAlive) {
228-
Debug.print(DBG_ERROR, "Attempting reconnection");
229-
}
230-
231-
newInterval = CHECK_INTERVAL_DISCONNECTED;
232-
}
233-
break;
234-
case NetworkConnectionState::CLOSED: {
235-
236-
Debug.print(DBG_VERBOSE, "Connection to the network terminated");
237-
}
238-
break;
239-
case NetworkConnectionState::ERROR: {
240-
execNetworkEventCallback(_on_error_event_callback, 0);
241-
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
242-
}
243-
break;
183+
184+
Debug.print(DBG_INFO, "Connected to the network");
185+
connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED;
186+
execNetworkEventCallback(_on_connect_event_callback, 0);
187+
return NetworkConnectionState::CONNECTED;
188+
}
189+
190+
NetworkConnectionState LoRaConnectionHandler::update_handleConnected() {
191+
192+
bool networkStatus = modem.connected();
193+
Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus);
194+
if (networkStatus != true) {
195+
execNetworkEventCallback(_on_disconnect_event_callback, 0);
196+
//Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
197+
198+
Debug.print(DBG_ERROR, "Connection to the network lost.");
199+
if (keepAlive) {
200+
Debug.print(DBG_ERROR, "Attempting reconnection");
201+
}
202+
connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED;
203+
return NetworkConnectionState::DISCONNECTED;
204+
}
205+
Debug.print(DBG_VERBOSE, "Connected to the network");
206+
207+
return NetworkConnectionState::CONNECTED;
208+
}
209+
210+
NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() {
211+
execNetworkEventCallback(_on_disconnect_event_callback, 0);
212+
//Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
213+
214+
Debug.print(DBG_ERROR, "Connection to the network lost.");
215+
if (keepAlive) {
216+
Debug.print(DBG_ERROR, "Attempting reconnection");
244217
}
245-
connectionTickTimeInterval = newInterval;
246-
lastConnectionTickTime = millis();
247-
netConnectionState = _newState;
248-
//connectionStateChanged(netConnectionState);
218+
connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED;
219+
return NetworkConnectionState::DISCONNECTED;
220+
}
221+
222+
NetworkConnectionState LoRaConnectionHandler::update_handleDisconnected() {
223+
if (keepAlive) {
224+
Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT");
225+
connectionTickTimeInterval = CHECK_INTERVAL_INIT;
226+
return NetworkConnectionState::INIT;
227+
} else {
228+
Debug.print(DBG_VERBOSE, "Connection to the network terminated");
229+
return NetworkConnectionState::CLOSED;
230+
}
231+
249232
}
250233

251234
void LoRaConnectionHandler::connect() {
252235
if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) {
253236
return;
254237
}
255238
keepAlive = true;
256-
changeConnectionState(NetworkConnectionState::INIT);
239+
connectionTickTimeInterval = CHECK_INTERVAL_INIT;
240+
netConnectionState = NetworkConnectionState::INIT;
257241

258242
}
259243
void LoRaConnectionHandler::disconnect() {
260244
// do nothing
261-
return;
245+
return;
262246
}
263-
#endif
247+
#endif

src/Arduino_LoRaConnectionHandler.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler {
5555

5656
private:
5757

58-
void changeConnectionState(NetworkConnectionState _newState);
59-
6058
const int CHECK_INTERVAL_IDLE = 100;
6159
const int CHECK_INTERVAL_INIT = 100;
6260
const int CHECK_INTERVAL_CONNECTING = 500;
@@ -80,6 +78,13 @@ class LoRaConnectionHandler : public LPWANConnectionHandler {
8078
_on_error_event_callback;
8179

8280
static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg);
81+
NetworkConnectionState update_handleInit();
82+
NetworkConnectionState update_handleConnecting();
83+
NetworkConnectionState update_handleConnected();
84+
85+
NetworkConnectionState update_handleDisconnecting();
86+
NetworkConnectionState update_handleDisconnected();
87+
8388

8489
};
8590

0 commit comments

Comments
 (0)