Skip to content

Commit 6acab6e

Browse files
pennamandreagilardoni
authored andcommitted
ArduinoIoTCloud integration
1 parent 82a4523 commit 6acab6e

File tree

3 files changed

+46
-180
lines changed

3 files changed

+46
-180
lines changed

Diff for: src/ArduinoIoTCloudDevice.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ ArduinoCloudDevice::State ArduinoCloudDevice::handleConnected() {
140140
}
141141
return State::SendCapabilities;
142142
}
143-
144143
return State::Connected;
145144
}
146145

Diff for: src/ArduinoIoTCloudTCP.cpp

+39-133
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,11 @@
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-
409335
ArduinoIoTCloudTCP::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-
584490
void ArduinoIoTCloudTCP::attachThing()
585491
{
586492

Diff for: src/ArduinoIoTCloudTCP.h

+7-46
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,11 @@
3636
#endif
3737
#endif
3838

39-
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
40-
#include "WiFiSSLClient.h"
41-
#elif defined(BOARD_HAS_ECCX08)
42-
#include "tls/BearSSLClient.h"
43-
#elif defined(ARDUINO_PORTENTA_C33)
44-
#include <SSLClient.h>
45-
#elif defined(ARDUINO_NICLA_VISION)
46-
#include <WiFiSSLSE050Client.h>
47-
#elif defined(ARDUINO_EDGE_CONTROL)
48-
#include <GSMSSLClient.h>
49-
#elif defined(ARDUINO_UNOR4_WIFI)
50-
#include <WiFiSSLClient.h>
51-
#elif defined(BOARD_ESP)
52-
#include <WiFiClientSecure.h>
53-
#endif
39+
#include <tls/utility/TLSClientMqtt.h>
40+
#include <tls/utility/TLSClientOta.h>
5441

5542
#if OTA_ENABLED
56-
#include <utility/ota/OTA.h>
43+
#include <ota/OTA.h>
5744
#endif
5845

5946
#include "cbor/MessageDecoder.h"
@@ -109,7 +96,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
10996
_get_ota_confirmation = cb;
11097
_ask_user_before_executing_ota = true;
11198
}
112-
void handle_OTARequest();
11399
#endif
114100

115101
private:
@@ -147,44 +133,22 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
147133
#endif
148134
#endif
149135

150-
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
151-
WiFiBearSSLClient _sslClient;
152-
#elif defined(BOARD_HAS_ECCX08)
153-
BearSSLClient _sslClient;
154-
#elif defined(ARDUINO_PORTENTA_C33)
155-
SSLClient _sslClient;
156-
#elif defined(ARDUINO_NICLA_VISION)
157-
WiFiSSLSE050Client _sslClient;
158-
#elif defined(ARDUINO_EDGE_CONTROL)
159-
GSMSSLClient _sslClient;
160-
#elif defined(ARDUINO_UNOR4_WIFI)
161-
WiFiSSLClient _sslClient;
162-
#elif defined(BOARD_ESP)
163-
WiFiClientSecure _sslClient;
164-
#endif
165-
136+
TLSClientMqtt _brokerClient;
166137
MqttClient _mqttClient;
167138

168-
String _deviceTopicOut;
169-
String _deviceTopicIn;
170139
String _messageTopicOut;
171140
String _messageTopicIn;
172141
String _dataTopicOut;
173142
String _dataTopicIn;
174143

144+
175145
#if OTA_ENABLED
176-
bool _ota_cap;
177-
int _ota_error;
178-
String _ota_img_sha256;
179-
String _ota_url;
180-
bool _ota_req;
146+
TLSClientOta _otaClient;
147+
OTACloudProcess _ota;
181148
bool _ask_user_before_executing_ota;
182149
onOTARequestCallbackFunc _get_ota_confirmation;
183150
#endif /* OTA_ENABLED */
184151

185-
inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");}
186-
inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");}
187-
188152
inline String getTopic_messageout() { return String("/a/d/" + getDeviceId() + "/c/up");}
189153
inline String getTopic_messagein () { return String("/a/d/" + getDeviceId() + "/c/dw");}
190154

@@ -206,9 +170,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
206170
void detachThing();
207171
int write(String const topic, byte const data[], int const length);
208172

209-
#if OTA_ENABLED
210-
void sendDevicePropertyToCloud(String const name);
211-
#endif
212173
};
213174

214175
/******************************************************************************

0 commit comments

Comments
 (0)