forked from andreagilardoni/Arduino_ConnectionHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericConnectionHandler.cpp
More file actions
160 lines (128 loc) · 4.95 KB
/
GenericConnectionHandler.cpp
File metadata and controls
160 lines (128 loc) · 4.95 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
/*
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"
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter);
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;
}
}
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;
}
#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
unsigned long GenericConnectionHandler::getTime() {
return _ch != nullptr ? _ch->getTime() : 0;
}
Client & GenericConnectionHandler::getClient() {
return _ch->getClient(); // NOTE _ch may be nullptr
}
UDP & GenericConnectionHandler::getUDP() {
return _ch->getUDP(); // NOTE _ch may be nullptr
}
#endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
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);
}
}
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.print(DBG_ERROR, "Network adapter not supported by this platform: %d", adapter);
return nullptr;
}
}