Skip to content

Commit 1ff3170

Browse files
committed
Add constructor and code to make ArduinoCloud work without ConnectionHandler
1 parent 2ea6f62 commit 1ff3170

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/ArduinoIoTCloud.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
2929
: _connection{nullptr}
30+
, _client{nullptr}
31+
, _adapter{NetworkAdapter::WIFI}
3032
, _last_checked_property_index{0}
3133
, _time_service(TimeService)
3234
, _tz_offset{0}

src/ArduinoIoTCloud.h

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class ArduinoIoTCloudClass
103103
inline bool deviceNotAttached() { return _thing_id == ""; }
104104

105105
inline ConnectionHandler * getConnection() { return _connection; }
106+
inline Client * getClient() { return _client; }
107+
106108

107109
inline unsigned long getInternalTime() { return _time_service.getTime(); }
108110
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
@@ -153,6 +155,8 @@ class ArduinoIoTCloudClass
153155
protected:
154156

155157
ConnectionHandler * _connection;
158+
Client * _client;
159+
NetworkAdapter _adapter;
156160
PropertyContainer _device_property_container;
157161
PropertyContainer _thing_property_container;
158162
unsigned int _last_checked_property_index;

src/ArduinoIoTCloudTCP.cpp

+33-15
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,22 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
121121
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
122122
{
123123
_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);
128134
}
129135

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)
131137
{
138+
_client = &client;
139+
132140
_brokerAddress = brokerAddress;
133141
_brokerPort = brokerPort;
134142

@@ -160,9 +168,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
160168
#endif
161169

162170
#if defined(BOARD_HAS_ECCX08)
163-
_sslClient.setClient(_connection->getClient());
171+
_sslClient.setClient(client);
164172
#elif defined(ARDUINO_PORTENTA_C33)
165-
_sslClient.setClient(_connection->getClient());
173+
_sslClient.setClient(client);
166174
_sslClient.setCACert(AIoTSSCert);
167175
#elif defined(BOARD_HAS_SE050)
168176
_sslClient.appendCustomCACert(AIoTSSCert);
@@ -234,7 +242,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
234242
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
235243
if (enable_watchdog) {
236244
watchdog_enable();
237-
bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false;
245+
bool const use_ethernet = _adapter == NetworkAdapter::ETHERNET ? true : false;
238246
watchdog_enable_network_feed(use_ethernet);
239247
}
240248
#endif
@@ -301,6 +309,11 @@ void ArduinoIoTCloudTCP::printDebugInfo()
301309

302310
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
303311
{
312+
if (_connection == nullptr)
313+
{
314+
return State::SyncTime;
315+
}
316+
304317
if (_connection->check() == NetworkConnectionState::CONNECTED)
305318
{
306319
bool const is_retry_attempt = (_last_connection_attempt_cnt > 0);
@@ -313,12 +326,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
313326

314327
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
315328
{
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;
322340
}
323341

324342
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
@@ -585,7 +603,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
585603
/* Transmit the cleared request flags to the cloud. */
586604
sendDevicePropertyToCloud("OTA_REQ");
587605
/* Call member function to handle OTA request. */
588-
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
606+
_ota_error = OTA::onRequest(_ota_url, _adapter);
589607
/* If something fails send the OTA error to the cloud */
590608
sendDevicePropertyToCloud("OTA_ERROR");
591609
}

src/ArduinoIoTCloudTCP.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
8181

8282
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
8383
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
84+
int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
8485
#else
8586
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
87+
int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
8688
#endif
87-
int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
89+
8890

8991
#ifdef BOARD_HAS_SECRET_KEY
9092
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
@@ -189,6 +191,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
189191
onOTARequestCallbackFunc _get_ota_confirmation;
190192
#endif /* OTA_ENABLED */
191193

194+
int begin(Client & client, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
195+
192196
inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");}
193197
inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");}
194198
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }

0 commit comments

Comments
 (0)