2222#include < AIoTC_Config.h>
2323
2424#ifdef HAS_TCP
25- #include < ArduinoIoTCloudTCP.h>
26-
27- #if defined(BOARD_HAS_SECRET_KEY)
28- #include " tls/AIoTCUPCert.h"
29- #endif
3025
31- #ifdef BOARD_HAS_ECCX08
32- #include " tls/BearSSLTrustAnchors.h"
33- #endif
34-
35- #if defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE)
36- #include " tls/AIoTCSSCert.h"
37- #endif
26+ #include < ArduinoIoTCloudTCP.h>
3827
3928#if OTA_ENABLED
40- #include " utility/ ota/OTA.h"
29+ #include " ota/OTA.h"
4130#endif
4231
4332#include < algorithm>
@@ -67,25 +56,16 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
6756, _mqtt_data_buf{0 }
6857, _mqtt_data_len{0 }
6958, _mqtt_data_request_retransmit{false }
70- #ifdef BOARD_HAS_ECCX08
71- , _sslClient(nullptr , ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM, getTime)
72- #endif
7359#ifdef BOARD_HAS_SECRET_KEY
7460, _password(" " )
7561#endif
7662, _mqttClient{nullptr }
77- , _deviceTopicOut(" " )
78- , _deviceTopicIn(" " )
7963, _messageTopicOut(" " )
8064, _messageTopicIn(" " )
8165, _dataTopicOut(" " )
8266, _dataTopicIn(" " )
8367#if OTA_ENABLED
84- , _ota_cap{false }
85- , _ota_error{static_cast <int >(OTAError::None)}
86- , _ota_img_sha256{" Inv." }
87- , _ota_url{" " }
88- , _ota_req{false }
68+ , _ota(&_message_stream)
8969, _ask_user_before_executing_ota{false }
9070, _get_ota_confirmation{nullptr }
9171#endif /* OTA_ENABLED */
@@ -107,8 +87,16 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
10787 _brokerPort = brokerPort;
10888#endif
10989
90+ /* Setup broker TLS client */
91+ _brokerClient.begin (connection);
92+
93+ #if OTA_ENABLED
94+ /* Setup OTA TLS client */
95+ _otaClient.begin (connection);
96+ #endif
97+
11098 /* Setup TimeService */
111- _time_service.begin (&connection );
99+ _time_service.begin (_connection );
112100
113101 /* Setup retry timers */
114102 _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
@@ -148,33 +136,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
148136 DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device certificate." , __FUNCTION__);
149137 return 0 ;
150138 }
151- _sslClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
139+ _brokerClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
140+ #if OTA_ENABLED
141+ _otaClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
142+ #endif
152143 #endif
153144#endif
145+
154146#if defined(BOARD_HAS_SECRET_KEY)
155147 }
156148#endif
157149
158- #if defined(BOARD_HAS_OFFLOADED_ECCX08)
159-
160- #elif defined(BOARD_HAS_ECCX08)
161- _sslClient.setClient (_connection->getClient ());
162- #elif defined(ARDUINO_PORTENTA_C33)
163- _sslClient.setClient (_connection->getClient ());
164- _sslClient.setCACert (AIoTSSCert);
165- #elif defined(ARDUINO_NICLA_VISION)
166- _sslClient.appendCustomCACert (AIoTSSCert);
167- #elif defined(ARDUINO_EDGE_CONTROL)
168- _sslClient.appendCustomCACert (AIoTUPCert);
169- #elif defined(ARDUINO_UNOR4_WIFI)
170-
171- #elif defined(ARDUINO_ARCH_ESP32)
172- _sslClient.setCACertBundle (x509_crt_bundle);
173- #elif defined(ARDUINO_ARCH_ESP8266)
174- _sslClient.setInsecure ();
175- #endif
150+ _mqttClient.setClient (_brokerClient);
176151
177- _mqttClient.setClient (_sslClient);
178152#ifdef BOARD_HAS_SECRET_KEY
179153 if (_password.length ())
180154 {
@@ -187,32 +161,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
187161 _mqttClient.setConnectionTimeout (1500 );
188162 _mqttClient.setId (getDeviceId ().c_str ());
189163
190- _deviceTopicOut = getTopic_deviceout ();
191- _deviceTopicIn = getTopic_devicein ();
192- _messageTopicIn = getTopic_messagein ();
193164 _messageTopicOut = getTopic_messageout ();
165+ _messageTopicIn = getTopic_messagein ();
194166
195167 _thing.begin ();
196168 _device.begin ();
197169
198- #if OTA_ENABLED
199- Property* p;
200- p = new CloudWrapperBool (_ota_cap);
201- addPropertyToContainer (_device.getPropertyContainer (), *p, " OTA_CAP" , Permission::Read, -1 );
202- p = new CloudWrapperInt (_ota_error);
203- addPropertyToContainer (_device.getPropertyContainer (), *p, " OTA_ERROR" , Permission::Read, -1 );
204- p = new CloudWrapperString (_ota_img_sha256);
205- addPropertyToContainer (_device.getPropertyContainer (), *p, " OTA_SHA256" , Permission::Read, -1 );
206- p = new CloudWrapperString (_ota_url);
207- addPropertyToContainer (_device.getPropertyContainer (), *p, " OTA_URL" , Permission::ReadWrite, -1 );
208- p = new CloudWrapperBool (_ota_req);
209- addPropertyToContainer (_device.getPropertyContainer (), *p, " OTA_REQ" , Permission::ReadWrite, -1 );
210-
211- _ota_cap = OTA::isCapable ();
212-
213- _ota_img_sha256 = OTA::getImageSHA256 ();
214- DEBUG_VERBOSE (" SHA256: HASH(%d) = %s" , strlen (_ota_img_sha256.c_str ()), _ota_img_sha256.c_str ());
215- #endif // OTA_ENABLED
170+ #if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
171+ _ota.setClient (&_otaClient);
172+ #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
173+
174+ #if OTA_ENABLED && defined(OTA_BASIC_AUTH)
175+ _ota.setAuthentication (getDeviceId ().c_str (), _password.c_str ());
176+ #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH)
216177
217178#ifdef BOARD_HAS_OFFLOADED_ECCX08
218179 if (String (WiFi.firmwareVersion ()) < String (" 1.4.4" )) {
@@ -323,9 +284,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
323284 /* Subscribe to message topic to receive commands */
324285 _mqttClient.subscribe (_messageTopicIn);
325286
326- /* Temoporarly subscribe to device topic to receive OTA properties */
327- _mqttClient.subscribe (_deviceTopicIn);
328-
329287 /* Reconfigure timers for next state */
330288 _connection_attempt.begin (AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331289
@@ -361,51 +319,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
361319 /* Call CloudDevice process to get configuration */
362320 _device.update ();
363321
322+ #if OTA_ENABLED
323+ _ota.update ();
324+ #endif // OTA_ENABLED
325+
326+
364327 if (_device.isAttached ()) {
365328 /* Call CloudThing process to synchronize properties */
366329 _thing.update ();
367330 }
368331
369- #if OTA_ENABLED
370- if (_device.connected ()) {
371- handle_OTARequest ();
372- }
373- #endif /* OTA_ENABLED */
374-
375332 return State::Connected;
376333}
377334
378- #if OTA_ENABLED
379- void ArduinoIoTCloudTCP::handle_OTARequest () {
380- /* Request a OTA download if the hidden property
381- * OTA request has been set.
382- */
383-
384- if (_ota_req)
385- {
386- bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation ());
387- bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
388- if (perform_ota_now) {
389- /* Clear the error flag. */
390- _ota_error = static_cast <int >(OTAError::None);
391- /* Clear the request flag. */
392- _ota_req = false ;
393- /* Transmit the cleared request flags to the cloud. */
394- sendDevicePropertyToCloud (" OTA_REQ" );
395- /* Call member function to handle OTA request. */
396- _ota_error = OTA::onRequest (_ota_url, _connection->getInterface ());
397- /* If something fails send the OTA error to the cloud */
398- sendDevicePropertyToCloud (" OTA_ERROR" );
399- }
400- }
401-
402- /* Check if we have received the OTA_URL property and provide
403- * echo to the cloud.
404- */
405- sendDevicePropertyToCloud (" OTA_URL" );
406- }
407- #endif /* OTA_ENABLED */
408-
409335ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
410336{
411337 if (!_mqttClient.connected ()) {
@@ -442,11 +368,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
442368 bytes[i] = _mqttClient.read ();
443369 }
444370
445- /* Topic for OTA properties and device configuration */
446- if (_deviceTopicIn == topic) {
447- CBORDecoder::decode (_device.getPropertyContainer (), (uint8_t *)bytes, length);
448- }
449-
450371 /* Topic for user input data */
451372 if (_dataTopicIn == topic) {
452373 CBORDecoder::decode (_thing.getPropertyContainer (), (uint8_t *)bytes, length);
@@ -506,6 +427,14 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
506427 }
507428 break ;
508429
430+ #if OTA_ENABLED
431+ case CommandId::OtaUpdateCmdDownId:
432+ {
433+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] ota update received" , __FUNCTION__, millis ());
434+ _ota.handleMessage ((Message*)&command);
435+ }
436+ #endif
437+
509438 default :
510439 break ;
511440 }
@@ -526,14 +455,6 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
526455 _thing.getPropertyContainerIndex ());
527456 break ;
528457
529- #if OTA_ENABLED
530- case DeviceBeginCmdId:
531- sendDevicePropertyToCloud (" OTA_CAP" );
532- sendDevicePropertyToCloud (" OTA_ERROR" );
533- sendDevicePropertyToCloud (" OTA_SHA256" );
534- break ;
535- #endif
536-
537458 default :
538459 break ;
539460 }
@@ -566,21 +487,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
566487 }
567488}
568489
569- #if OTA_ENABLED
570- void ArduinoIoTCloudTCP::sendDevicePropertyToCloud (String const name)
571- {
572- PropertyContainer temp_device_property_container;
573- unsigned int last_device_property_index = 0 ;
574-
575- Property* p = getProperty (this ->_device .getPropertyContainer (), name);
576- if (p != nullptr )
577- {
578- addPropertyToContainer (temp_device_property_container, *p, p->name (), p->isWriteableByCloud () ? Permission::ReadWrite : Permission::Read);
579- sendPropertyContainerToCloud (_deviceTopicOut, temp_device_property_container, last_device_property_index);
580- }
581- }
582- #endif
583-
584490void ArduinoIoTCloudTCP::attachThing ()
585491{
586492
0 commit comments