-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathConnectionHandlerInterface.cpp
More file actions
150 lines (123 loc) · 5.71 KB
/
ConnectionHandlerInterface.cpp
File metadata and controls
150 lines (123 loc) · 5.71 KB
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
146
147
148
149
150
/*
This file is part of the Arduino_ConnectionHandler library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "ConnectionHandlerInterface.h"
/******************************************************************************
CONSTRUCTOR/DESTRUCTOR
******************************************************************************/
ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter interface)
: _keep_alive{keep_alive}
, _check_internet_availability{false}
, _interface{interface}
, _lastConnectionTickTime{millis()}
, _current_net_connection_state{NetworkConnectionState::INIT}
, _timeoutTable(DefaultTimeoutTable)
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState ConnectionHandler::check()
{
unsigned long const now = millis();
unsigned int const connectionTickTimeInterval =
_timeoutTable.intervals[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: next_net_connection_state = update_handleError (); 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;
}
NetworkConnectionState ConnectionHandler::update_handleError()
{
if (_keep_alive) {
return NetworkConnectionState::INIT;
}
return NetworkConnectionState::ERROR;
}
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;
}