2121
2222#include " Arduino_ConnectionHandler.h"
2323
24+ /* *****************************************************************************
25+ CONSTRUCTOR/DESTRUCTOR
26+ ******************************************************************************/
27+
28+ ConnectionHandler::ConnectionHandler (bool const keep_alive)
29+ : _keep_alive{keep_alive}
30+ , _current_net_connection_state{NetworkConnectionState::INIT}
31+ , _lastConnectionTickTime{millis ()}
32+ {
33+
34+ }
35+
2436/* *****************************************************************************
2537 PUBLIC MEMBER FUNCTIONS
2638 ******************************************************************************/
2739
28- void ConnectionHandler::addCallback (NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
29- switch (event) {
30- case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break ;
31- case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break ;
32- case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break ;
33- case NetworkConnectionEvent::INIT: ; break ;
34- case NetworkConnectionEvent::CONNECTING: ; break ;
35- case NetworkConnectionEvent::DISCONNECTING: ; break ;
36- case NetworkConnectionEvent::CLOSED: ; break ;
40+ NetworkConnectionState ConnectionHandler::check ()
41+ {
42+ unsigned long const now = millis ();
43+ unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast <unsigned int >(_current_net_connection_state)];
44+
45+ if ((now - _lastConnectionTickTime) > connectionTickTimeInterval)
46+ {
47+ _lastConnectionTickTime = now;
48+ NetworkConnectionState next_net_connection_state = _current_net_connection_state;
49+
50+ /* While the state machine is implemented here, the concrete implementation of the
51+ * states is done in the derived connection handlers.
52+ */
53+ switch (_current_net_connection_state)
54+ {
55+ case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break ;
56+ case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break ;
57+ case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break ;
58+ case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting (); break ;
59+ case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break ;
60+ case NetworkConnectionState::ERROR: break ;
61+ case NetworkConnectionState::CLOSED: break ;
62+ }
63+
64+ /* Here we are determining whether a state transition from one state to the next has
65+ * occurred - and if it has, we call eventually registered callbacks.
66+ */
67+ if (next_net_connection_state != _current_net_connection_state)
68+ {
69+ /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
70+ if (next_net_connection_state == NetworkConnectionState::CONNECTED)
71+ {
72+ if (_on_connect_event_callback) _on_connect_event_callback ();
73+ }
74+ if (next_net_connection_state == NetworkConnectionState::DISCONNECTED)
75+ {
76+ if (_on_disconnect_event_callback) _on_disconnect_event_callback ();
77+ }
78+ if (next_net_connection_state == NetworkConnectionState::ERROR)
79+ {
80+ if (_on_error_event_callback) _on_error_event_callback ();
81+ }
82+
83+ /* Assign new state to the member variable holding the state */
84+ _current_net_connection_state = next_net_connection_state;
85+ }
86+ }
87+
88+ return _current_net_connection_state;
89+ }
90+
91+ void ConnectionHandler::connect ()
92+ {
93+ if (_current_net_connection_state != NetworkConnectionState::INIT && _current_net_connection_state != NetworkConnectionState::CONNECTING)
94+ {
95+ _keep_alive = true ;
96+ _current_net_connection_state = NetworkConnectionState::INIT;
97+ }
98+ }
99+
100+ void ConnectionHandler::disconnect ()
101+ {
102+ _keep_alive = false ;
103+ _current_net_connection_state = NetworkConnectionState::DISCONNECTING;
104+ }
105+
106+ void ConnectionHandler::addCallback (NetworkConnectionEvent const event, OnNetworkEventCallback callback)
107+ {
108+ switch (event)
109+ {
110+ case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break ;
111+ case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break ;
112+ case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break ;
37113 }
38114}
39115
@@ -46,10 +122,3 @@ void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
46122void ConnectionHandler::addErrorCallback (OnNetworkEventCallback callback) {
47123 _on_error_event_callback = callback;
48124}
49-
50- void ConnectionHandler::execNetworkEventCallback (OnNetworkEventCallback & callback, void * callback_arg) {
51- if (callback) {
52- (*callback)(callback_arg);
53- }
54- }
55-
0 commit comments