Skip to content

Commit bc378d5

Browse files
committed
Employing the template method pattern to only implement the state machine in the base class - all derived classes import their actual behaviour within their template methods
1 parent c1292ff commit bc378d5

10 files changed

+72
-136
lines changed

src/Arduino_ConnectionHandler.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ConnectionHandler::ConnectionHandler(bool const keep_alive)
2929
: _keep_alive{keep_alive}
3030
, _netConnectionState{NetworkConnectionState::INIT}
31+
, _lastConnectionTickTime{millis()}
3132
{
3233

3334
}
@@ -36,6 +37,30 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive)
3637
PUBLIC MEMBER FUNCTIONS
3738
******************************************************************************/
3839

40+
NetworkConnectionState ConnectionHandler::check()
41+
{
42+
unsigned long const now = millis();
43+
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
44+
45+
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
46+
{
47+
_lastConnectionTickTime = now;
48+
49+
switch (_netConnectionState)
50+
{
51+
case NetworkConnectionState::INIT: _netConnectionState = update_handleInit (); break;
52+
case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting (); break;
53+
case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected (); break;
54+
case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting(); break;
55+
case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected (); break;
56+
case NetworkConnectionState::ERROR: break;
57+
case NetworkConnectionState::CLOSED: break;
58+
}
59+
}
60+
61+
return _netConnectionState;
62+
}
63+
3964
void ConnectionHandler::connect()
4065
{
4166
if (_netConnectionState != NetworkConnectionState::INIT && _netConnectionState != NetworkConnectionState::CONNECTING)

src/Arduino_ConnectionHandler.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class ConnectionHandler {
154154
ConnectionHandler(bool const keep_alive);
155155

156156

157-
virtual NetworkConnectionState check() = 0;
157+
NetworkConnectionState check();
158158

159159
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
160160
virtual unsigned long getTime() = 0;
@@ -187,8 +187,16 @@ class ConnectionHandler {
187187

188188
void execCallback(NetworkConnectionEvent const event);
189189

190+
virtual NetworkConnectionState update_handleInit () = 0;
191+
virtual NetworkConnectionState update_handleConnecting () = 0;
192+
virtual NetworkConnectionState update_handleConnected () = 0;
193+
virtual NetworkConnectionState update_handleDisconnecting() = 0;
194+
virtual NetworkConnectionState update_handleDisconnected () = 0;
195+
196+
190197
private:
191198

199+
unsigned long _lastConnectionTickTime;
192200
OnNetworkEventCallback _on_connect_event_callback = NULL,
193201
_on_disconnect_event_callback = NULL,
194202
_on_error_event_callback = NULL;

src/Arduino_GSMConnectionHandler.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, c
3939
, _apn(apn)
4040
, _login(login)
4141
, _pass(pass)
42-
, _lastConnectionTickTime(millis())
4342
{
4443

4544
}
@@ -53,32 +52,8 @@ unsigned long GSMConnectionHandler::getTime()
5352
return _gsm.getTime();
5453
}
5554

56-
NetworkConnectionState GSMConnectionHandler::check()
57-
{
58-
unsigned long const now = millis();
59-
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
60-
61-
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
62-
{
63-
_lastConnectionTickTime = now;
64-
65-
switch (_netConnectionState)
66-
{
67-
case NetworkConnectionState::INIT: _netConnectionState = update_handleInit(); break;
68-
case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting(); break;
69-
case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected(); break;
70-
case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting(); break;
71-
case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected(); break;
72-
case NetworkConnectionState::ERROR: break;
73-
case NetworkConnectionState::CLOSED: break;
74-
}
75-
}
76-
77-
return _netConnectionState;
78-
}
79-
8055
/******************************************************************************
81-
PRIVATE MEMBER FUNCTIONS
56+
PROTECTED MEMBER FUNCTIONS
8257
******************************************************************************/
8358

8459
NetworkConnectionState GSMConnectionHandler::update_handleInit()

src/Arduino_GSMConnectionHandler.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,30 @@ class GSMConnectionHandler : public ConnectionHandler
3939

4040

4141
virtual unsigned long getTime() override;
42-
virtual NetworkConnectionState check() override;
4342
virtual Client & getClient() override { return _gsm_client; };
4443
virtual UDP & getUDP() override { return _gsm_udp; };
4544

4645

46+
protected:
47+
48+
virtual NetworkConnectionState update_handleInit () override;
49+
virtual NetworkConnectionState update_handleConnecting () override;
50+
virtual NetworkConnectionState update_handleConnected () override;
51+
virtual NetworkConnectionState update_handleDisconnecting() override;
52+
virtual NetworkConnectionState update_handleDisconnected () override;
53+
54+
4755
private:
4856

4957
const char * _pin;
5058
const char * _apn;
5159
const char * _login;
5260
const char * _pass;
53-
unsigned long _lastConnectionTickTime;
5461

5562
GSM _gsm;
5663
GPRS _gprs;
5764
GSMUDP _gsm_udp;
5865
GSMClient _gsm_client;
59-
60-
NetworkConnectionState update_handleInit ();
61-
NetworkConnectionState update_handleConnecting ();
62-
NetworkConnectionState update_handleConnected ();
63-
NetworkConnectionState update_handleDisconnecting();
64-
NetworkConnectionState update_handleDisconnected ();
6566
};
6667

6768
#endif /* #ifdef BOARD_HAS_GSM */

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * a
5050
, _appkey(appkey)
5151
, _band(band)
5252
, _device_class(device_class)
53-
, _lastConnectionTickTime(millis())
5453
{
5554

5655
}
@@ -59,30 +58,6 @@ LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * a
5958
PUBLIC MEMBER FUNCTIONS
6059
******************************************************************************/
6160

62-
NetworkConnectionState LoRaConnectionHandler::check()
63-
{
64-
unsigned long const now = millis();
65-
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
66-
67-
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
68-
{
69-
_lastConnectionTickTime = now;
70-
71-
switch (_netConnectionState)
72-
{
73-
case NetworkConnectionState::INIT: _netConnectionState = update_handleInit(); break;
74-
case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting(); break;
75-
case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected(); break;
76-
case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting(); break;
77-
case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected(); break;
78-
case NetworkConnectionState::ERROR: break;
79-
case NetworkConnectionState::CLOSED: break;
80-
}
81-
}
82-
83-
return _netConnectionState;
84-
}
85-
8661
int LoRaConnectionHandler::write(const uint8_t * buf, size_t size)
8762
{
8863
_modem.beginPacket();
@@ -122,7 +97,7 @@ bool LoRaConnectionHandler::available()
12297
}
12398

12499
/******************************************************************************
125-
PRIVATE MEMBER FUNCTIONS
100+
PROTECTED MEMBER FUNCTIONS
126101
******************************************************************************/
127102

128103
NetworkConnectionState LoRaConnectionHandler::update_handleInit()

src/Arduino_LoRaConnectionHandler.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,27 @@ class LoRaConnectionHandler : public ConnectionHandler
3535
LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band = _lora_band::EU868, _lora_class const device_class = _lora_class::CLASS_A);
3636

3737

38-
virtual NetworkConnectionState check() override;
3938
virtual int write(const uint8_t *buf, size_t size) override;
4039
virtual int read() override;
4140
virtual bool available() override;
4241

4342

43+
protected:
44+
45+
virtual NetworkConnectionState update_handleInit () override;
46+
virtual NetworkConnectionState update_handleConnecting () override;
47+
virtual NetworkConnectionState update_handleConnected () override;
48+
virtual NetworkConnectionState update_handleDisconnecting() override;
49+
virtual NetworkConnectionState update_handleDisconnected () override;
50+
51+
4452
private:
4553

4654
char const * _appeui;
4755
char const * _appkey;
4856
_lora_band _band;
4957
_lora_class _device_class;
50-
unsigned long _lastConnectionTickTime;
5158
LoRaModem _modem;
52-
53-
NetworkConnectionState update_handleInit();
54-
NetworkConnectionState update_handleConnecting();
55-
NetworkConnectionState update_handleConnected();
56-
NetworkConnectionState update_handleDisconnecting();
57-
NetworkConnectionState update_handleDisconnected();
5859
};
5960

6061
#endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */

src/Arduino_NBConnectionHandler.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, cha
5959
, _apn(apn)
6060
, _login(login)
6161
, _pass(pass)
62-
, _lastConnectionTickTime(millis())
6362
{
6463

6564
}
@@ -73,30 +72,6 @@ unsigned long NBConnectionHandler::getTime()
7372
return _nb.getTime();
7473
}
7574

76-
NetworkConnectionState NBConnectionHandler::check()
77-
{
78-
unsigned long const now = millis();
79-
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
80-
81-
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
82-
{
83-
_lastConnectionTickTime = now;
84-
85-
switch (_netConnectionState)
86-
{
87-
case NetworkConnectionState::INIT: _netConnectionState = update_handleInit (); break;
88-
case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting (); break;
89-
case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected (); break;
90-
case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting(); break;
91-
case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected (); break;
92-
case NetworkConnectionState::ERROR: break;
93-
case NetworkConnectionState::CLOSED: break;
94-
}
95-
}
96-
97-
return _netConnectionState;
98-
}
99-
10075
/******************************************************************************
10176
PRIVATE MEMBER FUNCTIONS
10277
******************************************************************************/

src/Arduino_NBConnectionHandler.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ class NBConnectionHandler : public ConnectionHandler
4040

4141

4242
virtual unsigned long getTime() override;
43-
virtual NetworkConnectionState check() override;
4443
virtual Client & getClient() override { return _nb_client; };
4544
virtual UDP & getUDP() override { return _nb_udp; };
4645

4746

47+
protected:
48+
49+
virtual NetworkConnectionState update_handleInit () override;
50+
virtual NetworkConnectionState update_handleConnecting () override;
51+
virtual NetworkConnectionState update_handleConnected () override;
52+
virtual NetworkConnectionState update_handleDisconnecting() override;
53+
virtual NetworkConnectionState update_handleDisconnected () override;
54+
55+
4856
private:
4957

5058
void changeConnectionState(NetworkConnectionState _newState);
@@ -53,18 +61,11 @@ class NBConnectionHandler : public ConnectionHandler
5361
char const * _apn;
5462
char const * _login;
5563
char const * _pass;
56-
unsigned long _lastConnectionTickTime;
5764

5865
NB _nb;
5966
GPRS _nb_gprs;
6067
NBUDP _nb_udp;
6168
NBClient _nb_client;
62-
63-
NetworkConnectionState update_handleInit ();
64-
NetworkConnectionState update_handleConnecting ();
65-
NetworkConnectionState update_handleConnected ();
66-
NetworkConnectionState update_handleDisconnecting();
67-
NetworkConnectionState update_handleDisconnected ();
6869
};
6970

7071
#endif /* #ifdef BOARD_HAS_NB */

src/Arduino_WiFiConnectionHandler.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pas
3131
: ConnectionHandler{keep_alive}
3232
, _ssid{ssid}
3333
, _pass{pass}
34-
, _lastConnectionTickTime{millis()}
3534
{
3635

3736
}
@@ -49,32 +48,8 @@ unsigned long WiFiConnectionHandler::getTime()
4948
#endif
5049
}
5150

52-
NetworkConnectionState WiFiConnectionHandler::check()
53-
{
54-
unsigned long const now = millis();
55-
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
56-
57-
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
58-
{
59-
_lastConnectionTickTime = now;
60-
61-
switch (_netConnectionState)
62-
{
63-
case NetworkConnectionState::INIT: _netConnectionState = update_handleInit (); break;
64-
case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting (); break;
65-
case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected (); break;
66-
case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting(); break;
67-
case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected (); break;
68-
case NetworkConnectionState::ERROR: break;
69-
case NetworkConnectionState::CLOSED: break;
70-
}
71-
}
72-
73-
return _netConnectionState;
74-
}
75-
7651
/******************************************************************************
77-
PRIVATE MEMBER FUNCTIONS
52+
PROTECTED MEMBER FUNCTIONS
7853
******************************************************************************/
7954

8055
NetworkConnectionState WiFiConnectionHandler::update_handleInit()

src/Arduino_WiFiConnectionHandler.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ class WiFiConnectionHandler : public ConnectionHandler
3838

3939

4040
virtual unsigned long getTime() override;
41-
virtual NetworkConnectionState check() override;
4241
virtual Client & getClient() override{ return _wifi_client; }
4342
virtual UDP & getUDP() override { return _wifi_udp; }
4443

4544

45+
protected:
46+
47+
virtual NetworkConnectionState update_handleInit () override;
48+
virtual NetworkConnectionState update_handleConnecting () override;
49+
virtual NetworkConnectionState update_handleConnected () override;
50+
virtual NetworkConnectionState update_handleDisconnecting() override;
51+
virtual NetworkConnectionState update_handleDisconnected () override;
52+
4653
private:
4754

4855
char const * _ssid;
4956
char const * _pass;
50-
unsigned long _lastConnectionTickTime;
5157

5258
WiFiUDP _wifi_udp;
5359
WiFiClient _wifi_client;
54-
55-
NetworkConnectionState update_handleInit ();
56-
NetworkConnectionState update_handleConnecting ();
57-
NetworkConnectionState update_handleConnected ();
58-
NetworkConnectionState update_handleDisconnecting();
59-
NetworkConnectionState update_handleDisconnected ();
6060
};
6161

6262
#endif /* #ifdef BOARD_HAS_WIFI */

0 commit comments

Comments
 (0)