-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathConnectionHandlerInterface.cpp
145 lines (119 loc) · 5.86 KB
/
ConnectionHandlerInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
This file is part of ArduinoIoTCloud.
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "ConnectionHandlerInterface.h"
/******************************************************************************
CONSTRUCTOR/DESTRUCTOR
******************************************************************************/
ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter interface)
: _keep_alive{keep_alive}
, _interface{interface}
, _lastConnectionTickTime{millis()}
, _current_net_connection_state{NetworkConnectionState::INIT}
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState ConnectionHandler::check()
{
unsigned long const now = millis();
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_current_net_connection_state)];
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
{
_lastConnectionTickTime = now;
NetworkConnectionState old_net_connection_state = _current_net_connection_state;
NetworkConnectionState next_net_connection_state = updateConnectionState();
/* Here we are determining whether a state transition from one state to the next has
* occurred - and if it has, we call eventually registered callbacks.
*/
if(old_net_connection_state != next_net_connection_state) {
updateCallback(next_net_connection_state);
/* It may happen that the local _current_net_connection_state
* is not updated by the updateConnectionState() call. This is the case for GenericConnection handler
* where the call of updateConnectionState() is replaced by the inner ConnectionHandler call
* that updates its state, but not the outer one. For this reason it is required to perform this call twice
*/
_current_net_connection_state = next_net_connection_state;
}
}
return _current_net_connection_state;
}
NetworkConnectionState ConnectionHandler::updateConnectionState() {
NetworkConnectionState next_net_connection_state = _current_net_connection_state;
/* While the state machine is implemented here, the concrete implementation of the
* states is done in the derived connection handlers.
*/
switch (_current_net_connection_state)
{
case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break;
case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break;
case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break;
case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting(); break;
case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break;
case NetworkConnectionState::ERROR: break;
case NetworkConnectionState::CLOSED: break;
}
/* Assign new state to the member variable holding the state */
_current_net_connection_state = next_net_connection_state;
return next_net_connection_state;
}
void ConnectionHandler::updateCallback(NetworkConnectionState next_net_connection_state) {
/* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
if(next_net_connection_state == NetworkConnectionState::CONNECTED)
{
if(_on_connect_event_callback) _on_connect_event_callback();
}
if(next_net_connection_state == NetworkConnectionState::DISCONNECTED)
{
if(_on_disconnect_event_callback) _on_disconnect_event_callback();
}
if(next_net_connection_state == NetworkConnectionState::ERROR)
{
if(_on_error_event_callback) _on_error_event_callback();
}
}
void ConnectionHandler::connect()
{
if (_current_net_connection_state != NetworkConnectionState::INIT && _current_net_connection_state != NetworkConnectionState::CONNECTING)
{
_keep_alive = true;
_current_net_connection_state = NetworkConnectionState::INIT;
}
}
void ConnectionHandler::disconnect()
{
_keep_alive = false;
_current_net_connection_state = NetworkConnectionState::DISCONNECTING;
}
void ConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback)
{
switch (event)
{
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
}
}
void ConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) {
_on_connect_event_callback = callback;
}
void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
_on_disconnect_event_callback = callback;
}
void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
_on_error_event_callback = callback;
}