Skip to content

Commit ce0aeb4

Browse files
defining a wrapper connection handler with the objective of being generic
1 parent efcbd55 commit ce0aeb4

File tree

3 files changed

+225
-1
lines changed

3 files changed

+225
-1
lines changed

src/ConnectionHandlerInterface.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ConnectionHandler {
5151

5252
virtual ~ConnectionHandler() {}
5353

54-
NetworkConnectionState check();
54+
virtual NetworkConnectionState check();
5555

5656
#if not defined(BOARD_HAS_LORA)
5757
virtual unsigned long getTime() = 0;

src/GenericConnectionHandler.cpp

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
This file is part of the Arduino_ConnectionHandler library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
/******************************************************************************
12+
INCLUDE
13+
******************************************************************************/
14+
15+
#include "GenericConnectionHandler.h"
16+
#include "Arduino_ConnectionHandler.h"
17+
18+
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter);
19+
20+
bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) {
21+
if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) {
22+
// If the internal connection handler is already being used and not in INIT phase we cannot update the settings
23+
return false;
24+
} else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) {
25+
// If the internal connection handler is already being used and in INIT phase and the interface type is being changed
26+
// -> we need to deallocate the previously allocated handler
27+
28+
// if interface type is not being changed -> we just need to call updateSettings
29+
delete _ch;
30+
_ch = nullptr;
31+
}
32+
33+
if(_ch == nullptr) {
34+
_ch = instantiate_handler(s.type);
35+
}
36+
37+
if(_ch != nullptr) {
38+
_interface = s.type;
39+
_ch->setKeepAlive(_keep_alive);
40+
return _ch->updateSetting(s);
41+
} else {
42+
_interface = NetworkAdapter::NONE;
43+
44+
return false;
45+
}
46+
}
47+
48+
NetworkConnectionState GenericConnectionHandler::updateConnectionState() {
49+
return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::INIT;
50+
}
51+
52+
NetworkConnectionState GenericConnectionHandler::update_handleInit() {
53+
return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT;
54+
}
55+
56+
NetworkConnectionState GenericConnectionHandler::update_handleConnecting() {
57+
return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT;
58+
}
59+
60+
NetworkConnectionState GenericConnectionHandler::update_handleConnected() {
61+
return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT;
62+
}
63+
64+
NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() {
65+
return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT;
66+
}
67+
68+
NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() {
69+
return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT;
70+
}
71+
72+
#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
73+
unsigned long GenericConnectionHandler::getTime() {
74+
return _ch != nullptr ? _ch->getTime() : 0;
75+
}
76+
77+
Client & GenericConnectionHandler::getClient() {
78+
return _ch->getClient(); // NOTE _ch may be nullptr
79+
}
80+
81+
UDP & GenericConnectionHandler::getUDP() {
82+
return _ch->getUDP(); // NOTE _ch may be nullptr
83+
}
84+
85+
#endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
86+
87+
void GenericConnectionHandler::connect() {
88+
if(_ch!=nullptr) {
89+
_ch->connect();
90+
}
91+
ConnectionHandler::connect();
92+
}
93+
94+
void GenericConnectionHandler::disconnect() {
95+
if(_ch!=nullptr) {
96+
_ch->disconnect();
97+
}
98+
ConnectionHandler::disconnect();
99+
}
100+
101+
void GenericConnectionHandler::setKeepAlive(bool keep_alive) {
102+
_keep_alive = keep_alive;
103+
104+
if(_ch!=nullptr) {
105+
_ch->setKeepAlive(keep_alive);
106+
}
107+
}
108+
109+
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) {
110+
switch(adapter) {
111+
#if defined(BOARD_HAS_WIFI)
112+
case NetworkAdapter::WIFI:
113+
return new WiFiConnectionHandler();
114+
break;
115+
#endif
116+
117+
#if defined(BOARD_HAS_ETHERNET)
118+
case NetworkAdapter::ETHERNET:
119+
return new EthernetConnectionHandler();
120+
break;
121+
#endif
122+
123+
#if defined(BOARD_HAS_NB)
124+
case NetworkAdapter::NB:
125+
return new NBConnectionHandler();
126+
break;
127+
#endif
128+
129+
#if defined(BOARD_HAS_GSM)
130+
case NetworkAdapter::GSM:
131+
return new GSMConnectionHandler();
132+
break;
133+
#endif
134+
135+
#if defined(BOARD_HAS_CATM1_NBIOT)
136+
case NetworkAdapter::CATM1:
137+
return new CatM1ConnectionHandler();
138+
break;
139+
#endif
140+
141+
#if defined(BOARD_HAS_CELLULAR)
142+
case NetworkAdapter::CELL:
143+
return new CellularConnectionHandler();
144+
break;
145+
#endif
146+
147+
default:
148+
Debug.print(DBG_ERROR, "Network adapter not supported by this platform: %d", adapter);
149+
return nullptr;
150+
}
151+
}

src/GenericConnectionHandler.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
This file is part of the Arduino_ConnectionHandler library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#ifndef ARDUINO_GENERIC_CONNECTION_HANDLER_H_
12+
#define ARDUINO_GENERIC_CONNECTION_HANDLER_H_
13+
14+
/******************************************************************************
15+
INCLUDE
16+
******************************************************************************/
17+
18+
#include "ConnectionHandlerInterface.h"
19+
20+
/******************************************************************************
21+
CLASS DECLARATION
22+
******************************************************************************/
23+
24+
/** GenericConnectionHandler class
25+
* This class aims to wrap a connectionHandler and provide a generic way to
26+
* instantiate a specific connectionHandler type
27+
*/
28+
class GenericConnectionHandler : public ConnectionHandler
29+
{
30+
public:
31+
32+
GenericConnectionHandler(bool const keep_alive=true): ConnectionHandler(keep_alive), _ch(nullptr) {}
33+
34+
#if defined(BOARD_HAS_NOTECARD) || defined(BOARD_HAS_LORA)
35+
virtual bool available() = 0;
36+
virtual int read() = 0;
37+
virtual int write(const uint8_t *buf, size_t size) = 0;
38+
#else
39+
unsigned long getTime() override;
40+
41+
/*
42+
* NOTE: The following functions have a huge risk of returning a reference to a non existing memory location
43+
* It is important to make sure that the internal connection handler is already allocated before calling them
44+
* When updateSettings is called and the internal connectionHandler is reallocated the references to TCP and UDP
45+
* handles should be deleted.
46+
*/
47+
Client & getClient() override;
48+
UDP & getUDP() override;
49+
#endif
50+
51+
bool updateSetting(const models::NetworkSetting& s) override;
52+
53+
void connect() override;
54+
void disconnect() override;
55+
56+
void setKeepAlive(bool keep_alive=true) override;
57+
58+
protected:
59+
60+
NetworkConnectionState updateConnectionState() override;
61+
62+
NetworkConnectionState update_handleInit () override;
63+
NetworkConnectionState update_handleConnecting () override;
64+
NetworkConnectionState update_handleConnected () override;
65+
NetworkConnectionState update_handleDisconnecting() override;
66+
NetworkConnectionState update_handleDisconnected () override;
67+
68+
private:
69+
70+
ConnectionHandler* _ch;
71+
};
72+
73+
#endif /* ARDUINO_GENERIC_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)