-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGenericConnectionHandler.cpp
More file actions
186 lines (148 loc) · 5.93 KB
/
GenericConnectionHandler.cpp
File metadata and controls
186 lines (148 loc) · 5.93 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
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 "GenericConnectionHandler.h"
#include "Arduino_ConnectionHandler.h"
/******************************************************************************
LOCAL MODULE FUNCTIONS
******************************************************************************/
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) {
switch(adapter) {
#if defined(BOARD_HAS_WIFI)
case NetworkAdapter::WIFI:
return new WiFiConnectionHandler();
break;
#endif
#if defined(BOARD_HAS_ETHERNET)
case NetworkAdapter::ETHERNET:
return new EthernetConnectionHandler();
break;
#endif
#if defined(BOARD_HAS_NB)
case NetworkAdapter::NB:
return new NBConnectionHandler();
break;
#endif
#if defined(BOARD_HAS_GSM)
case NetworkAdapter::GSM:
return new GSMConnectionHandler();
break;
#endif
#if defined(BOARD_HAS_CATM1_NBIOT)
case NetworkAdapter::CATM1:
return new CatM1ConnectionHandler();
break;
#endif
#if defined(BOARD_HAS_CELLULAR)
case NetworkAdapter::CELL:
return new CellularConnectionHandler();
break;
#endif
default:
DEBUG_ERROR("Network adapter not supported by this platform: %d", adapter);
return nullptr;
}
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) {
if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) {
// If the internal connection handler is already being used and not in INIT phase we cannot update the settings
return false;
} else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) {
// If the internal connection handler is already being used and in INIT phase and the interface type is being changed
// -> we need to deallocate the previously allocated handler
// if interface type is not being changed -> we just need to call updateSettings
delete _ch;
_ch = nullptr;
}
if(_ch == nullptr) {
_ch = instantiate_handler(s.type);
}
if(_ch != nullptr) {
_interface = s.type;
_ch->setKeepAlive(_keep_alive);
_ch->enableCheckInternetAvailability(_check_internet_availability);
return _ch->updateSetting(s);
} else {
_interface = NetworkAdapter::NONE;
return false;
}
}
void GenericConnectionHandler::getSetting(models::NetworkSetting& s) {
if(_ch != nullptr) {
_ch->getSetting(s);
} else {
s.type = NetworkAdapter::NONE;
}
}
#if !defined(BOARD_HAS_LORA)
unsigned long GenericConnectionHandler::getTime() {
return _ch != nullptr ? _ch->getTime() : 0;
}
int GenericConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
return _ch != nullptr ? _ch->ping(ip, ttl, count) : 0;
}
int GenericConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
return _ch != nullptr ? _ch->ping(hostname, ttl, count) : 0;
}
int GenericConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
return _ch != nullptr ? _ch->ping(host, ttl, count) : 0;
}
Client & GenericConnectionHandler::getClient() {
return _ch->getClient(); // NOTE _ch may be nullptr
}
UDP & GenericConnectionHandler::getUDP() {
return _ch->getUDP(); // NOTE _ch may be nullptr
}
#endif // !defined(BOARD_HAS_LORA)
void GenericConnectionHandler::connect() {
if(_ch!=nullptr) {
_ch->connect();
}
ConnectionHandler::connect();
}
void GenericConnectionHandler::disconnect() {
if(_ch!=nullptr) {
_ch->disconnect();
}
ConnectionHandler::disconnect();
}
void GenericConnectionHandler::setKeepAlive(bool keep_alive) {
_keep_alive = keep_alive;
if(_ch!=nullptr) {
_ch->setKeepAlive(keep_alive);
}
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState GenericConnectionHandler::updateConnectionState() {
return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleInit() {
return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleConnecting() {
return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleConnected() {
return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() {
return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() {
return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT;
}
NetworkConnectionState GenericConnectionHandler::update_handleError() {
return _ch != nullptr ? _ch->update_handleError() : NetworkConnectionState::INIT;
}