Skip to content

Commit 753cce9

Browse files
committed
ArduinoIoTCloud integration
1 parent 97c365b commit 753cce9

File tree

3 files changed

+58
-183
lines changed

3 files changed

+58
-183
lines changed

src/ArduinoIoTCloudDevice.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ ArduinoCloudDevice::State ArduinoCloudDevice::handleConnected() {
133133
}
134134
return State::SendCapabilities;
135135
}
136-
137136
return State::Connected;
138137
}
139138

src/ArduinoIoTCloudTCP.cpp

+45-134
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,26 +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}
89-
, _ask_user_before_executing_ota{false}
68+
, _ota(&_message_stream)
9069
, _get_ota_confirmation{nullptr}
9170
#endif /* OTA_ENABLED */
9271
{
@@ -107,8 +86,16 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
10786
_brokerPort = brokerPort;
10887
#endif
10988

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+
11097
/* Setup TimeService */
111-
_time_service.begin(&connection);
98+
_time_service.begin(_connection);
11299

113100
/* Setup retry timers */
114101
_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,
148135
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
149136
return 0;
150137
}
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
152142
#endif
153143
#endif
144+
154145
#if defined(BOARD_HAS_SECRET_KEY)
155146
}
156147
#endif
157148

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);
176150

177-
_mqttClient.setClient(_sslClient);
178151
#ifdef BOARD_HAS_SECRET_KEY
179152
if(_password.length())
180153
{
@@ -187,32 +160,19 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
187160
_mqttClient.setConnectionTimeout(1500);
188161
_mqttClient.setId(getDeviceId().c_str());
189162

190-
_deviceTopicOut = getTopic_deviceout();
191-
_deviceTopicIn = getTopic_devicein();
192-
_messageTopicIn = getTopic_messagein();
193163
_messageTopicOut = getTopic_messageout();
164+
_messageTopicIn = getTopic_messagein();
194165

195166
_thing.begin();
196167
_device.begin();
197168

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)
216176

217177
#ifdef BOARD_HAS_OFFLOADED_ECCX08
218178
if (String(WiFi.firmwareVersion()) < String("1.4.4")) {
@@ -323,9 +283,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
323283
/* Subscribe to message topic to receive commands */
324284
_mqttClient.subscribe(_messageTopicIn);
325285

326-
/* Temoporarly subscribe to device topic to receive OTA properties */
327-
_mqttClient.subscribe(_deviceTopicIn);
328-
329286
/* Reconfigure timers for next state */
330287
_connection_attempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331288

@@ -361,50 +318,24 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
361318
/* Call CloudDevice process to get configuration */
362319
_device.update();
363320

364-
if (_device.isAttached()) {
365-
/* Call CloudThing process to synchronize properties */
366-
_thing.update();
367-
}
368-
369-
#if OTA_ENABLED
370-
if (_device.connected()) {
371-
handle_OTARequest();
321+
#if OTA_ENABLED
322+
if(_get_ota_confirmation != nullptr &&
323+
_ota.getState() == OTACloudProcessInterface::State::OtaAvailable &&
324+
_get_ota_confirmation()) {
325+
_ota.approveOta();
372326
}
373-
#endif /* OTA_ENABLED */
374327

375-
return State::Connected;
376-
}
328+
_ota.update();
329+
#endif // OTA_ENABLED
377330

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-
*/
383331

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-
}
332+
if (_device.isAttached()) {
333+
/* Call CloudThing process to synchronize properties */
334+
_thing.update();
400335
}
401336

402-
/* Check if we have received the OTA_URL property and provide
403-
* echo to the cloud.
404-
*/
405-
sendDevicePropertyToCloud("OTA_URL");
337+
return State::Connected;
406338
}
407-
#endif /* OTA_ENABLED */
408339

409340
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
410341
{
@@ -442,11 +373,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
442373
bytes[i] = _mqttClient.read();
443374
}
444375

445-
/* Topic for OTA properties and device configuration */
446-
if (_deviceTopicIn == topic) {
447-
CBORDecoder::decode(_device.getPropertyContainer(), (uint8_t*)bytes, length);
448-
}
449-
450376
/* Topic for user input data */
451377
if (_dataTopicIn == topic) {
452378
CBORDecoder::decode(_thing.getPropertyContainer(), (uint8_t*)bytes, length);
@@ -506,6 +432,14 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
506432
}
507433
break;
508434

435+
#if OTA_ENABLED
436+
case CommandId::OtaUpdateCmdDownId:
437+
{
438+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] ota update received", __FUNCTION__, millis());
439+
_ota.handleMessage((Message*)&command);
440+
}
441+
#endif
442+
509443
default:
510444
break;
511445
}
@@ -526,14 +460,6 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
526460
_thing.getPropertyContainerIndex());
527461
break;
528462

529-
#if OTA_ENABLED
530-
case DeviceBeginCmdId:
531-
sendDevicePropertyToCloud("OTA_CAP");
532-
sendDevicePropertyToCloud("OTA_ERROR");
533-
sendDevicePropertyToCloud("OTA_SHA256");
534-
break;
535-
#endif
536-
537463
default:
538464
break;
539465
}
@@ -566,21 +492,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
566492
}
567493
}
568494

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-
584495
void ArduinoIoTCloudTCP::attachThing()
585496
{
586497

src/ArduinoIoTCloudTCP.h

+13-48
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"
@@ -107,9 +94,13 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
10794
*/
10895
void onOTARequestCb(onOTARequestCallbackFunc cb) {
10996
_get_ota_confirmation = cb;
110-
_ask_user_before_executing_ota = true;
97+
98+
if(_get_ota_confirmation) {
99+
_ota.setOtaPolicies(OTACloudProcessInterface::ApprovalRequired);
100+
} else {
101+
_ota.setOtaPolicies(OTACloudProcessInterface::None);
102+
}
111103
}
112-
void handle_OTARequest();
113104
#endif
114105

115106
private:
@@ -147,44 +138,21 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
147138
#endif
148139
#endif
149140

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-
141+
TLSClientMqtt _brokerClient;
166142
MqttClient _mqttClient;
167143

168-
String _deviceTopicOut;
169-
String _deviceTopicIn;
170144
String _messageTopicOut;
171145
String _messageTopicIn;
172146
String _dataTopicOut;
173147
String _dataTopicIn;
174148

149+
175150
#if OTA_ENABLED
176-
bool _ota_cap;
177-
int _ota_error;
178-
String _ota_img_sha256;
179-
String _ota_url;
180-
bool _ota_req;
181-
bool _ask_user_before_executing_ota;
151+
TLSClientOta _otaClient;
152+
OTACloudProcess _ota;
182153
onOTARequestCallbackFunc _get_ota_confirmation;
183154
#endif /* OTA_ENABLED */
184155

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-
188156
inline String getTopic_messageout() { return String("/a/d/" + getDeviceId() + "/c/up");}
189157
inline String getTopic_messagein () { return String("/a/d/" + getDeviceId() + "/c/dw");}
190158

@@ -206,9 +174,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
206174
void detachThing();
207175
int write(String const topic, byte const data[], int const length);
208176

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

214179
/******************************************************************************

0 commit comments

Comments
 (0)