22
22
#include < AIoTC_Config.h>
23
23
24
24
#ifdef HAS_TCP
25
- #include < ArduinoIoTCloudTCP.h>
26
-
27
- #if defined(BOARD_HAS_SECRET_KEY)
28
- #include " tls/AIoTCUPCert.h"
29
- #endif
30
25
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>
38
27
39
28
#if OTA_ENABLED
40
- #include " utility/ ota/OTA.h"
29
+ #include " ota/OTA.h"
41
30
#endif
42
31
43
32
#include < algorithm>
@@ -67,25 +56,16 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
67
56
, _mqtt_data_buf{0 }
68
57
, _mqtt_data_len{0 }
69
58
, _mqtt_data_request_retransmit{false }
70
- #ifdef BOARD_HAS_ECCX08
71
- , _sslClient(nullptr , ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM, getTime)
72
- #endif
73
59
#ifdef BOARD_HAS_SECRET_KEY
74
60
, _password(" " )
75
61
#endif
76
62
, _mqttClient{nullptr }
77
- , _deviceTopicOut(" " )
78
- , _deviceTopicIn(" " )
79
63
, _messageTopicOut(" " )
80
64
, _messageTopicIn(" " )
81
65
, _dataTopicOut(" " )
82
66
, _dataTopicIn(" " )
83
67
#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)
89
69
, _ask_user_before_executing_ota{false }
90
70
, _get_ota_confirmation{nullptr }
91
71
#endif /* OTA_ENABLED */
@@ -107,8 +87,16 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
107
87
_brokerPort = brokerPort;
108
88
#endif
109
89
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
+
110
98
/* Setup TimeService */
111
- _time_service.begin (&connection );
99
+ _time_service.begin (_connection );
112
100
113
101
/* Setup retry timers */
114
102
_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,
148
136
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device certificate." , __FUNCTION__);
149
137
return 0 ;
150
138
}
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
152
143
#endif
153
144
#endif
145
+
154
146
#if defined(BOARD_HAS_SECRET_KEY)
155
147
}
156
148
#endif
157
149
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);
176
151
177
- _mqttClient.setClient (_sslClient);
178
152
#ifdef BOARD_HAS_SECRET_KEY
179
153
if (_password.length ())
180
154
{
@@ -187,32 +161,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
187
161
_mqttClient.setConnectionTimeout (1500 );
188
162
_mqttClient.setId (getDeviceId ().c_str ());
189
163
190
- _deviceTopicOut = getTopic_deviceout ();
191
- _deviceTopicIn = getTopic_devicein ();
192
- _messageTopicIn = getTopic_messagein ();
193
164
_messageTopicOut = getTopic_messageout ();
165
+ _messageTopicIn = getTopic_messagein ();
194
166
195
167
_thing.begin ();
196
168
_device.begin ();
197
169
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)
216
177
217
178
#ifdef BOARD_HAS_OFFLOADED_ECCX08
218
179
if (String (WiFi.firmwareVersion ()) < String (" 1.4.4" )) {
@@ -361,51 +322,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
361
322
/* Call CloudDevice process to get configuration */
362
323
_device.update ();
363
324
325
+ #if OTA_ENABLED
326
+ _ota.update ();
327
+ #endif // OTA_ENABLED
328
+
329
+
364
330
if (_device.isAttached ()) {
365
331
/* Call CloudThing process to synchronize properties */
366
332
_thing.update ();
367
333
}
368
334
369
- #if OTA_ENABLED
370
- if (_device.connected ()) {
371
- handle_OTARequest ();
372
- }
373
- #endif /* OTA_ENABLED */
374
-
375
335
return State::Connected;
376
336
}
377
337
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
-
409
338
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
410
339
{
411
340
if (!_mqttClient.connected ()) {
@@ -442,14 +371,9 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
442
371
bytes[i] = _mqttClient.read ();
443
372
}
444
373
445
- /* Topic for OTA properties and device configuration */
446
- if (_deviceTopicIn == topic) {
447
- CBORDecoder::decode (_device.getPropertyContainer (), (uint8_t *)bytes, length);
448
- }
449
-
450
374
/* Topic for user input data */
451
375
if (_dataTopicIn == topic) {
452
- CBORDecoder::decode (_thing .getPropertyContainer (), (uint8_t *)bytes, length);
376
+ CBORDecoder::decode (getThing () .getPropertyContainer (), (uint8_t *)bytes, length);
453
377
}
454
378
455
379
/* Topic for device commands */
@@ -506,6 +430,14 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
506
430
}
507
431
break ;
508
432
433
+ #if OTA_ENABLED
434
+ case CommandID::OtaUpdateCmdDownId:
435
+ {
436
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] ota update received" , __FUNCTION__, millis ());
437
+ _ota.handleMessage ((Message*)&command);
438
+ }
439
+ #endif
440
+
509
441
default :
510
442
break ;
511
443
}
@@ -566,21 +498,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
566
498
}
567
499
}
568
500
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
-
584
501
void ArduinoIoTCloudTCP::attachThing ()
585
502
{
586
503
0 commit comments