@@ -121,14 +121,22 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
121
121
int ArduinoIoTCloudTCP::begin (ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
122
122
{
123
123
_connection = &connection;
124
- _brokerAddress = brokerAddress;
125
- _brokerPort = brokerPort;
126
- _time_service.begin (&connection);
127
- return begin (enable_watchdog, _brokerAddress, _brokerPort);
124
+
125
+ return begin (_connection->getClient (), _connection->getUDP (), _connection->getInterface (), enable_watchdog, brokerAddress, brokerPort);
126
+ }
127
+
128
+ int ArduinoIoTCloudTCP::begin (Client & client, UDP & udp, NetworkAdapter adapter, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
129
+ {
130
+ _adapter = adapter;
131
+ _time_service.begin (udp);
132
+
133
+ return begin (client, enable_watchdog, brokerAddress, brokerPort);
128
134
}
129
135
130
- int ArduinoIoTCloudTCP::begin (bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
136
+ int ArduinoIoTCloudTCP::begin (Client & client, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
131
137
{
138
+ _client = &client;
139
+
132
140
_brokerAddress = brokerAddress;
133
141
_brokerPort = brokerPort;
134
142
@@ -160,9 +168,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
160
168
#endif
161
169
162
170
#if defined(BOARD_HAS_ECCX08)
163
- _sslClient.setClient (_connection-> getClient () );
171
+ _sslClient.setClient (client );
164
172
#elif defined(ARDUINO_PORTENTA_C33)
165
- _sslClient.setClient (_connection-> getClient () );
173
+ _sslClient.setClient (client );
166
174
_sslClient.setCACert (AIoTSSCert);
167
175
#elif defined(BOARD_HAS_SE050)
168
176
_sslClient.appendCustomCACert (AIoTSSCert);
@@ -234,7 +242,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
234
242
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
235
243
if (enable_watchdog) {
236
244
watchdog_enable ();
237
- bool const use_ethernet = _connection-> getInterface () == NetworkAdapter::ETHERNET ? true : false ;
245
+ bool const use_ethernet = _adapter == NetworkAdapter::ETHERNET ? true : false ;
238
246
watchdog_enable_network_feed (use_ethernet);
239
247
}
240
248
#endif
@@ -301,6 +309,11 @@ void ArduinoIoTCloudTCP::printDebugInfo()
301
309
302
310
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy ()
303
311
{
312
+ if (_connection == nullptr )
313
+ {
314
+ return State::SyncTime;
315
+ }
316
+
304
317
if (_connection->check () == NetworkConnectionState::CONNECTED)
305
318
{
306
319
bool const is_retry_attempt = (_last_connection_attempt_cnt > 0 );
@@ -313,12 +326,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
313
326
314
327
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime ()
315
328
{
316
- #pragma GCC diagnostic push
317
- #pragma GCC diagnostic ignored "-Wunused-variable"
318
- unsigned long const internal_posix_time = _time_service.getTime ();
319
- #pragma GCC diagnostic pop
320
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d" , __FUNCTION__, internal_posix_time);
321
- return State::ConnectMqttBroker;
329
+ _time_service.sync ();
330
+ Serial.println (" _time_service.sync()" );
331
+
332
+ if (_time_service.isTimeValid ())
333
+ {
334
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d" , __FUNCTION__, _time_service.getTime ());
335
+ return State::ConnectMqttBroker;
336
+ }
337
+
338
+ // TODO: handle retry delay
339
+ return State::SyncTime;
322
340
}
323
341
324
342
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker ()
@@ -585,7 +603,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
585
603
/* Transmit the cleared request flags to the cloud. */
586
604
sendDevicePropertyToCloud (" OTA_REQ" );
587
605
/* Call member function to handle OTA request. */
588
- _ota_error = OTA::onRequest (_ota_url, _connection-> getInterface () );
606
+ _ota_error = OTA::onRequest (_ota_url, _adapter );
589
607
/* If something fails send the OTA error to the cloud */
590
608
sendDevicePropertyToCloud (" OTA_ERROR" );
591
609
}
0 commit comments