Skip to content

Commit 186c5c7

Browse files
defining a wrapper connection handler with the objective of being generic
1 parent 8a5e987 commit 186c5c7

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

src/GenericConnectionHandler.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
This software is released under the GNU General Public License version 3,
5+
which covers the main part of arduino-cli.
6+
The terms of this license can be found at:
7+
https://www.gnu.org/licenses/gpl-3.0.en.html
8+
You can be released from the requirements of the above licenses by purchasing
9+
a commercial license. Buying such a license is mandatory if you want to modify or
10+
otherwise use the software for commercial activities involving the Arduino
11+
software without disclosing the source code of your own applications. To purchase
12+
a commercial license, send an email to [email protected].
13+
*/
14+
15+
/******************************************************************************
16+
INCLUDE
17+
******************************************************************************/
18+
19+
#include "GenericConnectionHandler.h"
20+
#include "Arduino_ConnectionHandler.h"
21+
22+
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter);
23+
24+
bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) {
25+
26+
if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) {
27+
// If the internal connection handler is already being used and not in INIT phase we cannot update the settings
28+
return false;
29+
} else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) {
30+
// If the internal connection handler is already being used and in INIT phase and the interface type is being changed
31+
// -> we need to deallocate the previously allocated handler
32+
33+
// if interface type is not being changed -> we just need to call updateSettings
34+
delete _ch;
35+
_ch = nullptr;
36+
}
37+
38+
if(_ch == nullptr) {
39+
_ch = instantiate_handler(s.type);
40+
}
41+
42+
if(_ch != nullptr) {
43+
_interface = s.type;
44+
_ch->setKeepAlive(_keep_alive);
45+
return _ch->updateSetting(s);
46+
} else {
47+
_interface = NetworkAdapter::NONE;
48+
49+
return false;
50+
}
51+
}
52+
53+
NetworkConnectionState GenericConnectionHandler::update_handleInit() {
54+
return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT;
55+
}
56+
57+
NetworkConnectionState GenericConnectionHandler::update_handleConnecting() {
58+
return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT;
59+
}
60+
61+
NetworkConnectionState GenericConnectionHandler::update_handleConnected() {
62+
return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT;
63+
}
64+
65+
NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() {
66+
return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT;
67+
}
68+
69+
NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() {
70+
return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT;
71+
}
72+
73+
#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
74+
unsigned long GenericConnectionHandler::getTime() {
75+
return _ch != nullptr ? _ch->getTime() : 0;
76+
}
77+
78+
Client & GenericConnectionHandler::getClient() {
79+
return _ch->getClient(); // NOTE _ch may be nullptr
80+
}
81+
82+
UDP & GenericConnectionHandler::getUDP() {
83+
return _ch->getUDP(); // NOTE _ch may be nullptr
84+
}
85+
86+
#endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))
87+
88+
void GenericConnectionHandler::connect() {
89+
if(_ch!=nullptr) {
90+
_ch->connect();
91+
}
92+
ConnectionHandler::connect();
93+
}
94+
95+
void GenericConnectionHandler::disconnect() {
96+
if(_ch!=nullptr) {
97+
_ch->disconnect();
98+
}
99+
ConnectionHandler::disconnect();
100+
}
101+
102+
void GenericConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
103+
if(_ch!=nullptr) {
104+
_ch->addCallback(event, callback);
105+
}
106+
}
107+
108+
void GenericConnectionHandler::setKeepAlive(bool keep_alive) {
109+
_keep_alive = keep_alive;
110+
111+
if(_ch!=nullptr) {
112+
_ch->setKeepAlive(keep_alive);
113+
}
114+
}
115+
116+
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) {
117+
switch(adapter) {
118+
#if defined(BOARD_HAS_WIFI)
119+
case NetworkAdapter::WIFI:
120+
return new WiFiConnectionHandler();
121+
break;
122+
#endif
123+
124+
#if defined(BOARD_HAS_ETHERNET)
125+
case NetworkAdapter::ETHERNET:
126+
return new EthernetConnectionHandler();
127+
break;
128+
#endif
129+
130+
#if defined(BOARD_HAS_NB)
131+
case NetworkAdapter::NB:
132+
return new NBConnectionHandler();
133+
break;
134+
#endif
135+
136+
#if defined(BOARD_HAS_GSM)
137+
case NetworkAdapter::GSM:
138+
return new GSMConnectionHandler();
139+
break;
140+
#endif
141+
142+
#if defined(BOARD_HAS_CATM1_NBIOT)
143+
case NetworkAdapter::CATM1:
144+
return new CatM1ConnectionHandler();
145+
break;
146+
#endif
147+
148+
#if defined(BOARD_HAS_CELLULAR)
149+
case NetworkAdapter::CELL:
150+
return new CellularConnectionHandler();
151+
break;
152+
#endif
153+
154+
default:
155+
Debug.print(DBG_ERROR, "Network adapter not supported by this platform: %d", adapter);
156+
return nullptr;
157+
}
158+
}

src/GenericConnectionHandler.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_GENERIC_CONNECTION_HANDLER_H_
19+
#define ARDUINO_GENERIC_CONNECTION_HANDLER_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include "ConnectionHandlerInterface.h"
26+
27+
/******************************************************************************
28+
CLASS DECLARATION
29+
******************************************************************************/
30+
31+
/** GenericConnectionHandler class
32+
* This class aims to wrap a connectionHandler and provide a generic way to
33+
* instantiate a specific connectionHandler type
34+
*/
35+
class GenericConnectionHandler : public ConnectionHandler
36+
{
37+
public:
38+
39+
GenericConnectionHandler(bool const keep_alive=true): ConnectionHandler(keep_alive), _ch(nullptr) {}
40+
41+
#if defined(BOARD_HAS_NOTECARD) || defined(BOARD_HAS_LORA)
42+
virtual bool available() = 0;
43+
virtual int read() = 0;
44+
virtual int write(const uint8_t *buf, size_t size) = 0;
45+
#else
46+
unsigned long getTime() override;
47+
48+
/*
49+
* NOTE: The following functions have a huge risk of returning a reference to a non existing memory location
50+
* It is important to make sure that the internal connection handler is already allocated before calling them
51+
* When updateSettings is called and the internal connectionHandler is reallocated the references to TCP and UDP
52+
* handles should be deleted.
53+
*/
54+
Client & getClient() override;
55+
UDP & getUDP() override;
56+
#endif
57+
58+
bool updateSetting(const models::NetworkSetting& s) override;
59+
60+
void connect() override;
61+
void disconnect() override;
62+
void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) override;
63+
64+
void setKeepAlive(bool keep_alive=true) override;
65+
66+
protected:
67+
68+
NetworkConnectionState update_handleInit () override;
69+
NetworkConnectionState update_handleConnecting () override;
70+
NetworkConnectionState update_handleConnected () override;
71+
NetworkConnectionState update_handleDisconnecting() override;
72+
NetworkConnectionState update_handleDisconnected () override;
73+
74+
private:
75+
76+
ConnectionHandler* _ch;
77+
};
78+
79+
#endif /* ARDUINO_GENERIC_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)