Skip to content

Commit 00f785a

Browse files
committed
TCP: add begin method to configure clients without ConnectionHandler
1 parent 734e061 commit 00f785a

File tree

8 files changed

+121
-86
lines changed

8 files changed

+121
-86
lines changed

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry)
7070
{
7171
_connection = &connection;
7272
_retryEnable = retry;
73-
_time_service.begin(nullptr);
73+
_time_service.begin();
7474
return 1;
7575
}
7676

src/ArduinoIoTCloudTCP.cpp

Lines changed: 82 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -80,63 +80,29 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
8080
* PUBLIC MEMBER FUNCTIONS
8181
******************************************************************************/
8282

83-
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
83+
int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
8484
{
8585
_connection = &connection;
86-
_brokerAddress = brokerAddress;
87-
88-
_authMode = ArduinoIoTAuthenticationMode::CERTIFICATE;
89-
#if defined (BOARD_HAS_SECRET_KEY)
90-
/* If board supports and sketch is configured for username and password login */
91-
if(_password.length()) {
92-
_authMode = ArduinoIoTAuthenticationMode::PASSWORD;
93-
}
86+
#if OTA_ENABLED
87+
return begin(_connection->getClient(), TLSClientOta::getNewClient(_connection->getInterface()), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect);
88+
#else
89+
return begin(_connection->getClient(), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect);
9490
#endif
91+
}
9592

96-
/* If board is configured for certificate authentication and mTLS */
97-
if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE)
98-
{
99-
#if defined(BOARD_HAS_SECURE_ELEMENT)
100-
if (!_selement.begin())
101-
{
102-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
103-
#if defined(ARDUINO_UNOWIFIR4)
104-
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
105-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
106-
}
107-
#endif
108-
return 0;
109-
}
110-
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
111-
{
112-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
113-
return 0;
114-
}
115-
if (!_writeCertOnConnect) {
116-
/* No update pending read certificate stored in secure element */
117-
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
118-
{
119-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
120-
return 0;
121-
}
122-
}
123-
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
124-
_brokerTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
125-
#if OTA_ENABLED
126-
_otaTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
127-
#endif
128-
#endif
129-
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort;
93+
int ArduinoIoTCloudTCP::begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
94+
{
95+
#if OTA_ENABLED
96+
_otaClient = &otaClient;
13097
#endif
131-
}
132-
else
133-
{
134-
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
135-
}
98+
return begin(brokerClient, ntpClient, enableWatchdog, brokerAddress, _brokerPort, autoReconnect);
99+
}
136100

137-
/* Setup retry timers */
138-
_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
139-
return begin(enableWatchdog, _brokerAddress, _brokerPort, autoReconnect);
101+
int ArduinoIoTCloudTCP::begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
102+
{
103+
_brokerClient = &brokerClient;
104+
_ntpClient = &ntpClient;
105+
return begin(enableWatchdog, brokerAddress, brokerPort, autoReconnect);
140106
}
141107

142108
void ArduinoIoTCloudTCP::update()
@@ -175,18 +141,21 @@ void ArduinoIoTCloudTCP::update()
175141
/* Poll the network configurator to check if it is updating the configuration.
176142
* The polling must be performed only if the the first configuration is completed.
177143
*/
178-
#if NETWORK_CONFIGURATOR_ENABLED
144+
#if NETWORK_CONFIGURATOR_ENABLED
179145
if(_configurator != nullptr && _state > State::Init && _configurator->update() == NetworkConfiguratorStates::UPDATING_CONFIG){
180146
_state = State::ConfigPhy;
181147
}
182-
#endif
148+
#endif
183149

184150
#if OTA_ENABLED
185151
/* OTA FSM needs to reach the Idle state before being able to run independently from
186152
* the mqttClient. The state can be reached only after the mqttClient is connected to
187153
* the broker.
154+
*
155+
* We also have to check that the OTA client is not null. It can happen if we don't
156+
* use the ArduinoConnectionHandler library and the user doesn't provide it.
188157
*/
189-
if(_state <= State::ConnectPhy){
158+
if ((_state <= State::ConnectPhy) || (_otaClient == nullptr)) {
190159
return;
191160
}
192161

@@ -238,11 +207,61 @@ void ArduinoIoTCloudTCP::disconnect() {
238207

239208
int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
240209
{
210+
_authMode = ArduinoIoTAuthenticationMode::CERTIFICATE;
211+
#if defined (BOARD_HAS_SECRET_KEY)
212+
/* If board supports and sketch is configured for username and password login */
213+
if(_password.length()) {
214+
_authMode = ArduinoIoTAuthenticationMode::PASSWORD;
215+
}
216+
#endif
217+
218+
/* If board is configured for certificate authentication and mTLS */
219+
if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE)
220+
{
221+
#if defined(BOARD_HAS_SECURE_ELEMENT)
222+
if (!_selement.begin())
223+
{
224+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
225+
#if defined(ARDUINO_UNOWIFIR4)
226+
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
227+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
228+
}
229+
#endif
230+
return 0;
231+
}
232+
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
233+
{
234+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
235+
return 0;
236+
}
237+
if (!_writeCertOnConnect) {
238+
/* No update pending read certificate stored in secure element */
239+
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
240+
{
241+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
242+
return 0;
243+
}
244+
}
245+
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
246+
_brokerTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
247+
#if OTA_ENABLED
248+
_otaTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
249+
#endif
250+
#endif
251+
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort;
252+
#endif
253+
}
254+
else
255+
{
256+
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
257+
}
258+
259+
/* Setup retry timers */
260+
_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
261+
241262
_enableWatchdog = enableWatchdog;
242263
_brokerAddress = brokerAddress;
243-
_brokerPort = brokerPort;
244264
_autoReconnect = autoReconnect;
245-
246265
_state = State::ConfigPhy;
247266

248267
_mqttClient.setClient(_brokerTLSClient);
@@ -283,6 +302,7 @@ int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, u
283302
#if defined (ARDUINO_UNOWIFIR4)
284303
if (String(WiFi.firmwareVersion()) < String("0.2.0")) {
285304
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion());
305+
return 0;
286306
}
287307
#endif
288308

@@ -302,10 +322,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy()
302322
return State::Init;
303323
}
304324

305-
if(_configurator->update() == NetworkConfiguratorStates::CONFIGURED) {
306-
_configurator->disconnectAgent();
307-
return State::Init;
308-
}
325+
if (_configurator->update() == NetworkConfiguratorStates::CONFIGURED) {
326+
_configurator->disconnectAgent();
327+
return State::Init;
328+
}
309329
return State::ConfigPhy;
310330
#else
311331
return State::Init;
@@ -315,12 +335,11 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy()
315335
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init()
316336
{
317337
/* Setup broker TLS client */
318-
/* Setup broker TLS client */
319-
_brokerTLSClient.begin(*_connection, _authMode);
338+
_brokerTLSClient.begin(_brokerClient, _authMode);
320339

321340
#if OTA_ENABLED
322341
/* Setup OTA TLS client */
323-
_otaTLSClient.begin(*_connection);
342+
_otaTLSClient.begin(_otaClient);
324343
#endif
325344

326345
/* Setup TimeService */

src/ArduinoIoTCloudTCP.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
7474
virtual void printDebugInfo() override;
7575
virtual void disconnect () override;
7676

77-
int begin(ConnectionHandler & connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
77+
int begin(ConnectionHandler& connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
78+
int begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
79+
int begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
7880

7981
#if defined(BOARD_HAS_SECURE_ELEMENT)
8082
int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature);
@@ -159,15 +161,20 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
159161
bool _writeCertOnConnect;
160162
#endif
161163

164+
/* Base client from sketch or ConnectionHandler */
165+
Client * _brokerClient;
162166
TLSClientBroker _brokerTLSClient;
163167
MqttClient _mqttClient;
168+
UDP * _ntpClient;
164169

165170
String _messageTopicOut;
166171
String _messageTopicIn;
167172
String _dataTopicOut;
168173
String _dataTopicIn;
169174

170175
#if OTA_ENABLED
176+
/* Base client from sketch or ConnectionHandler */
177+
Client * _otaClient;
171178
TLSClientOta _otaTLSClient;
172179
ArduinoCloudOTA _ota;
173180
onOTARequestCallbackFunc _get_ota_confirmation;

src/tls/utility/TLSClientBroker.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,28 @@
3131
#endif
3232

3333

34-
void TLSClientBroker::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) {
34+
void TLSClientBroker::begin(Client* client, ArduinoIoTAuthenticationMode authMode) {
35+
36+
/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. )
37+
* The Client must be instantiated in the user sketch, for example:
38+
* WiFiClientSecure client;
39+
*/
3540

3641
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
3742
/* Arduino Root CA is configured in nina-fw
3843
* https://github.com/arduino/nina-fw/blob/master/arduino/libraries/ArduinoBearSSL/src/BearSSLTrustAnchors.h
3944
*/
4045
(void)authMode;
46+
(void)client;
4147
#elif defined(BOARD_HAS_ECCX08)
4248
(void)authMode;
43-
setClient(connection.getClient());
49+
setClient(*client);
4450
setProfile(aiotc_client_profile_init);
4551
setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM);
4652
ArduinoBearSSL.onGetTime(getTime);
4753
#elif defined(ARDUINO_PORTENTA_C33)
4854
(void)authMode;
49-
setClient(connection.getClient());
55+
setClient(*client);
5056
setCACert(AIoTSSCert);
5157
#elif defined(ARDUINO_NICLA_VISION)
5258
(void)authMode;
@@ -61,7 +67,7 @@ void TLSClientBroker::begin(ConnectionHandler & connection, ArduinoIoTAuthentica
6167
* also present in older firmware revisions
6268
* https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/f09ca94fdcab845b8368d4435fdac9f6999d21d2/certificates/certificates.pem#L852
6369
*/
64-
(void)connection;
70+
(void)client;
6571
/* Temporary force CACert to add new CA without rebuilding firmware */
6672
if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) {
6773
setCACert(AIoTSSCert);

src/tls/utility/TLSClientBroker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ enum class ArduinoIoTAuthenticationMode
7373
#endif
7474

7575
public:
76-
void begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE);
76+
void begin(Client* client, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE);
7777

7878
};

src/tls/utility/TLSClientOta.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,25 @@
3030
}
3131
#endif
3232

33-
void TLSClientOta::begin(ConnectionHandler &connection) {
33+
void TLSClientOta::begin(Client* client) {
34+
35+
/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. )
36+
* The Client must be instantiated in the user sketch, for example:
37+
* WiFiClientSecure client;
38+
*/
39+
3440
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
3541
/* AWS Root CAs are configured in nina-fw
3642
* https://github.com/arduino/nina-fw/blob/master/data/roots.pem
3743
*/
44+
(void)client;
3845
#elif defined(BOARD_HAS_ECCX08)
39-
setClient(*getNewClient(connection.getInterface()));
46+
setClient(*client);
4047
setProfile(aiotc_client_profile_init);
4148
setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM);
4249
ArduinoBearSSL.onGetTime(getTime);
4350
#elif defined(ARDUINO_PORTENTA_C33)
44-
setClient(*getNewClient(connection.getInterface()));
51+
setClient(*client);
4552
setCACert(AIoTSSCert);
4653
#elif defined(ARDUINO_NICLA_VISION)
4754
appendCustomCACert(AIoTSSCert);
@@ -51,7 +58,7 @@ void TLSClientOta::begin(ConnectionHandler &connection) {
5158
/* AWS Root CAs are configured in uno-r4-wifi-usb-bridge/libraries/Arduino_ESP32_OTA
5259
* https://github.com/arduino-libraries/Arduino_ESP32_OTA/blob/fc755e7d1d3946232107e2590662ee08d6ccdec4/src/tls/amazon_root_ca.h
5360
*/
54-
(void)connection;
61+
(void)client;
5562
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
5663
setCACert(AIoTUPCert);
5764
#elif defined(ARDUINO_ARCH_ESP32)

src/tls/utility/TLSClientOta.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,34 @@
6767
#endif
6868

6969
public:
70-
void begin(ConnectionHandler & connection);
70+
void begin(Client* client);
7171

72-
private:
73-
inline Client* getNewClient(NetworkAdapter net) {
72+
static inline Client& getNewClient(NetworkAdapter net) {
7473
switch(net) {
7574
#ifdef BOARD_HAS_WIFI
7675
case NetworkAdapter::WIFI:
77-
return new WiFiClient();
76+
return * new WiFiClient();
7877
#endif // BOARD_HAS_WIFI
7978
#ifdef BOARD_HAS_ETHERNET
8079
case NetworkAdapter::ETHERNET:
81-
return new EthernetClient();
80+
return * new EthernetClient();
8281
#endif // BOARD_HAS_ETHERNET
8382
#ifdef BOARD_HAS_NB
8483
case NetworkAdapter::NB:
85-
return new NBClient();
84+
return * new NBClient();
8685
#endif // BOARD_HAS_NB
8786
#ifdef BOARD_HAS_GSM
8887
case NetworkAdapter::GSM:
89-
return new GSMClient();
88+
return * new GSMClient();
9089
#endif // BOARD_HAS_GSM
9190
#ifdef BOARD_HAS_CATM1_NBIOT
9291
case NetworkAdapter::CATM1:
93-
return new GSMClient();
92+
return * new GSMClient();
9493
#endif // BOARD_HAS_CATM1_NBIOT
9594
#ifdef BOARD_HAS_CELLULAR
9695
case NetworkAdapter::CELL:
97-
return new TinyGsmClient(modem, 1);
96+
return * new TinyGsmClient(modem, 1);
9897
#endif // BOARD_HAS_CELLULAR
99-
default:
100-
return nullptr;
10198
}
10299
}
103100
};

src/utility/time/TimeService.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class TimeServiceClass
5959
static bool isTimeValid(unsigned long const time);
6060

6161
private:
62-
6362
ConnectionHandler * _con_hdl;
6463
bool _is_rtc_configured;
6564
bool _is_tz_configured;

0 commit comments

Comments
 (0)