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,26 +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 }
89
- , _ask_user_before_executing_ota{false }
68
+ , _ota(&_message_stream)
90
69
, _get_ota_confirmation{nullptr }
91
70
#endif /* OTA_ENABLED */
92
71
{
@@ -107,8 +86,16 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
107
86
_brokerPort = brokerPort;
108
87
#endif
109
88
89
+ /* Setup broker TLS client */
90
+ _brokerClient.begin (connection);
91
+
92
+ #if OTA_ENABLED
93
+ /* Setup OTA TLS client */
94
+ _otaClient.begin (connection);
95
+ #endif
96
+
110
97
/* Setup TimeService */
111
- _time_service.begin (&connection );
98
+ _time_service.begin (_connection );
112
99
113
100
/* Setup retry timers */
114
101
_connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
@@ -148,33 +135,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
148
135
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device certificate." , __FUNCTION__);
149
136
return 0 ;
150
137
}
151
- _sslClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
138
+ _brokerClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
139
+ #if OTA_ENABLED
140
+ _otaClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
141
+ #endif
152
142
#endif
153
143
#endif
144
+
154
145
#if defined(BOARD_HAS_SECRET_KEY)
155
146
}
156
147
#endif
157
148
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
149
+ _mqttClient.setClient (_brokerClient);
176
150
177
- _mqttClient.setClient (_sslClient);
178
151
#ifdef BOARD_HAS_SECRET_KEY
179
152
if (_password.length ())
180
153
{
@@ -187,32 +160,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
187
160
_mqttClient.setConnectionTimeout (1500 );
188
161
_mqttClient.setId (getDeviceId ().c_str ());
189
162
190
- _deviceTopicOut = getTopic_deviceout ();
191
- _deviceTopicIn = getTopic_devicein ();
192
- _messageTopicIn = getTopic_messagein ();
193
163
_messageTopicOut = getTopic_messageout ();
164
+ _messageTopicIn = getTopic_messagein ();
194
165
195
166
_thing.begin ();
196
167
_device.begin ();
197
168
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
169
+ #if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
170
+ _ota.setClient (&_otaClient);
171
+ #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
172
+
173
+ #if OTA_ENABLED && defined(OTA_BASIC_AUTH)
174
+ _ota.setAuthentication (getDeviceId ().c_str (), _password.c_str ());
175
+ #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH)
216
176
217
177
#ifdef BOARD_HAS_OFFLOADED_ECCX08
218
178
if (String (WiFi.firmwareVersion ()) < String (" 1.4.4" )) {
@@ -323,9 +283,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
323
283
/* Subscribe to message topic to receive commands */
324
284
_mqttClient.subscribe (_messageTopicIn);
325
285
326
- /* Temoporarly subscribe to device topic to receive OTA properties */
327
- _mqttClient.subscribe (_deviceTopicIn);
328
-
329
286
/* Reconfigure timers for next state */
330
287
_connection_attempt.begin (AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331
288
@@ -360,50 +317,24 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
360
317
/* Call CloudDevice process to get configuration */
361
318
_device.update ();
362
319
363
- if (_device.isAttached ()) {
364
- /* Call CloudThing process to synchronize properties */
365
- _thing.update ();
366
- }
367
-
368
- #if OTA_ENABLED
369
- if (_device.connected ()) {
370
- handle_OTARequest ();
320
+ #if OTA_ENABLED
321
+ if (_get_ota_confirmation != nullptr &&
322
+ _ota.getState () == OTACloudProcessInterface::State::OtaAvailable &&
323
+ _get_ota_confirmation ()) {
324
+ _ota.approveOta ();
371
325
}
372
- #endif /* OTA_ENABLED */
373
326
374
- return State::Connected ;
375
- }
327
+ _ota. update () ;
328
+ # endif // OTA_ENABLED
376
329
377
- #if OTA_ENABLED
378
- void ArduinoIoTCloudTCP::handle_OTARequest () {
379
- /* Request a OTA download if the hidden property
380
- * OTA request has been set.
381
- */
382
330
383
- if (_ota_req)
384
- {
385
- bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation ());
386
- bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
387
- if (perform_ota_now) {
388
- /* Clear the error flag. */
389
- _ota_error = static_cast <int >(OTAError::None);
390
- /* Clear the request flag. */
391
- _ota_req = false ;
392
- /* Transmit the cleared request flags to the cloud. */
393
- sendDevicePropertyToCloud (" OTA_REQ" );
394
- /* Call member function to handle OTA request. */
395
- _ota_error = OTA::onRequest (_ota_url, _connection->getInterface ());
396
- /* If something fails send the OTA error to the cloud */
397
- sendDevicePropertyToCloud (" OTA_ERROR" );
398
- }
331
+ if (_device.isAttached ()) {
332
+ /* Call CloudThing process to synchronize properties */
333
+ _thing.update ();
399
334
}
400
335
401
- /* Check if we have received the OTA_URL property and provide
402
- * echo to the cloud.
403
- */
404
- sendDevicePropertyToCloud (" OTA_URL" );
336
+ return State::Connected;
405
337
}
406
- #endif /* OTA_ENABLED */
407
338
408
339
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
409
340
{
@@ -441,11 +372,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
441
372
bytes[i] = _mqttClient.read ();
442
373
}
443
374
444
- /* Topic for OTA properties and device configuration */
445
- if (_deviceTopicIn == topic) {
446
- CBORDecoder::decode (_device.getPropertyContainer (), (uint8_t *)bytes, length);
447
- }
448
-
449
375
/* Topic for user input data */
450
376
if (_dataTopicIn == topic) {
451
377
CBORDecoder::decode (_thing.getPropertyContainer (), (uint8_t *)bytes, length);
@@ -505,6 +431,14 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
505
431
}
506
432
break ;
507
433
434
+ #if OTA_ENABLED
435
+ case CommandId::OtaUpdateCmdDownId:
436
+ {
437
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] ota update received" , __FUNCTION__, millis ());
438
+ _ota.handleMessage ((Message*)&command);
439
+ }
440
+ #endif
441
+
508
442
default :
509
443
break ;
510
444
}
@@ -525,14 +459,6 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
525
459
_thing.getPropertyContainerIndex ());
526
460
break ;
527
461
528
- #if OTA_ENABLED
529
- case DeviceBeginCmdId:
530
- sendDevicePropertyToCloud (" OTA_CAP" );
531
- sendDevicePropertyToCloud (" OTA_ERROR" );
532
- sendDevicePropertyToCloud (" OTA_SHA256" );
533
- break ;
534
- #endif
535
-
536
462
default :
537
463
break ;
538
464
}
@@ -565,21 +491,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
565
491
}
566
492
}
567
493
568
- #if OTA_ENABLED
569
- void ArduinoIoTCloudTCP::sendDevicePropertyToCloud (String const name)
570
- {
571
- PropertyContainer temp_device_property_container;
572
- unsigned int last_device_property_index = 0 ;
573
-
574
- Property* p = getProperty (this ->_device .getPropertyContainer (), name);
575
- if (p != nullptr )
576
- {
577
- addPropertyToContainer (temp_device_property_container, *p, p->name (), p->isWriteableByCloud () ? Permission::ReadWrite : Permission::Read);
578
- sendPropertyContainerToCloud (_deviceTopicOut, temp_device_property_container, last_device_property_index);
579
- }
580
- }
581
- #endif
582
-
583
494
void ArduinoIoTCloudTCP::attachThing ()
584
495
{
585
496
0 commit comments