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" )) {
@@ -323,9 +284,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
323
284
/* Subscribe to message topic to receive commands */
324
285
_mqttClient.subscribe (_messageTopicIn);
325
286
326
- /* Temoporarly subscribe to device topic to receive OTA properties */
327
- _mqttClient.subscribe (_deviceTopicIn);
328
-
329
287
/* Reconfigure timers for next state */
330
288
_connection_attempt.begin (AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331
289
@@ -361,51 +319,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
361
319
/* Call CloudDevice process to get configuration */
362
320
_device.update ();
363
321
322
+ #if OTA_ENABLED
323
+ _ota.update ();
324
+ #endif // OTA_ENABLED
325
+
326
+
364
327
if (_device.isAttached ()) {
365
328
/* Call CloudThing process to synchronize properties */
366
329
_thing.update ();
367
330
}
368
331
369
- #if OTA_ENABLED
370
- if (_device.connected ()) {
371
- handle_OTARequest ();
372
- }
373
- #endif /* OTA_ENABLED */
374
-
375
332
return State::Connected;
376
333
}
377
334
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
335
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
410
336
{
411
337
if (!_mqttClient.connected ()) {
@@ -442,11 +368,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
442
368
bytes[i] = _mqttClient.read ();
443
369
}
444
370
445
- /* Topic for OTA properties and device configuration */
446
- if (_deviceTopicIn == topic) {
447
- CBORDecoder::decode (_device.getPropertyContainer (), (uint8_t *)bytes, length);
448
- }
449
-
450
371
/* Topic for user input data */
451
372
if (_dataTopicIn == topic) {
452
373
CBORDecoder::decode (_thing.getPropertyContainer (), (uint8_t *)bytes, length);
@@ -506,6 +427,14 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
506
427
}
507
428
break ;
508
429
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
+
509
438
default :
510
439
break ;
511
440
}
@@ -526,14 +455,6 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
526
455
_thing.getPropertyContainerIndex ());
527
456
break ;
528
457
529
- #if OTA_ENABLED
530
- case DeviceBeginCmdId:
531
- sendDevicePropertyToCloud (" OTA_CAP" );
532
- sendDevicePropertyToCloud (" OTA_ERROR" );
533
- sendDevicePropertyToCloud (" OTA_SHA256" );
534
- break ;
535
- #endif
536
-
537
458
default :
538
459
break ;
539
460
}
@@ -566,21 +487,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
566
487
}
567
488
}
568
489
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
490
void ArduinoIoTCloudTCP::attachThing ()
585
491
{
586
492
0 commit comments