Skip to content

Commit a797771

Browse files
committed
disabled setInsecure() for ESP32 compatibility
1 parent fcb4a7d commit a797771

File tree

1 file changed

+91
-89
lines changed

1 file changed

+91
-89
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
119119
_sslClient.setClient(_connection->getClient());
120120
_sslClient.setEccSlot(static_cast<int>(ECCX08Slot::Key), _eccx08_cert.bytes(), _eccx08_cert.length());
121121
#elif defined(BOARD_ESP)
122+
#ifndef ESP32
122123
_sslClient.setInsecure();
123124
#endif
125+
#endif
124126

125127
_mqttClient.setClient(_sslClient);
126128
#ifdef BOARD_ESP
@@ -147,11 +149,11 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
147149
#if OTA_STORAGE_SNU
148150
String const nina_fw_version = WiFi.firmwareVersion();
149151
if (nina_fw_version < "1.4.1") {
150-
_ota_cap = false;
151-
DBG_WARNING(F("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s"), __FUNCTION__, nina_fw_version.c_str());
152+
_ota_cap = false;
153+
DBG_WARNING(F("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s"), __FUNCTION__, nina_fw_version.c_str());
152154
}
153155
else {
154-
_ota_cap = true;
156+
_ota_cap = true;
155157
}
156158
#endif /* OTA_STORAGE_SNU */
157159

@@ -175,7 +177,7 @@ void ArduinoIoTCloudTCP::update()
175177

176178
/* Check for new data from the MQTT client. */
177179
if (_mqttClient.connected())
178-
_mqttClient.poll();
180+
_mqttClient.poll();
179181
}
180182

181183
int ArduinoIoTCloudTCP::connected()
@@ -198,9 +200,9 @@ void ArduinoIoTCloudTCP::printDebugInfo()
198200
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
199201
{
200202
if (_connection->check() == NetworkConnectionState::CONNECTED)
201-
return State::SyncTime;
203+
return State::SyncTime;
202204
else
203-
return State::ConnectPhy;
205+
return State::ConnectPhy;
204206
}
205207

206208
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
@@ -213,7 +215,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
213215
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
214216
{
215217
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))
216-
return State::SubscribeMqttTopics;
218+
return State::SubscribeMqttTopics;
217219

218220
DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not connect to %s:%d"), __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
219221
return State::ConnectPhy;
@@ -223,26 +225,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
223225
{
224226
if (!_mqttClient.subscribe(_dataTopicIn))
225227
{
226-
DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _dataTopicIn.c_str());
227-
return State::SubscribeMqttTopics;
228+
DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _dataTopicIn.c_str());
229+
return State::SubscribeMqttTopics;
228230
}
229231

230232
if (_shadowTopicIn != "")
231233
{
232-
if (!_mqttClient.subscribe(_shadowTopicIn))
233-
{
234-
DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _shadowTopicIn.c_str());
235-
return State::SubscribeMqttTopics;
236-
}
234+
if (!_mqttClient.subscribe(_shadowTopicIn))
235+
{
236+
DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _shadowTopicIn.c_str());
237+
return State::SubscribeMqttTopics;
238+
}
237239
}
238240

239241
DBG_INFO(F("Connected to Arduino IoT Cloud"));
240242
execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT);
241243

242244
if (_shadowTopicIn != "")
243-
return State::RequestLastValues;
245+
return State::RequestLastValues;
244246
else
245-
return State::Connected;
247+
return State::Connected;
246248
}
247249

248250
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
@@ -251,9 +253,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
251253
unsigned long const now = millis();
252254
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
253255
{
254-
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values requested"), __FUNCTION__, now);
255-
requestLastValue();
256-
_lastSyncRequestTickTime = now;
256+
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values requested"), __FUNCTION__, now);
257+
requestLastValue();
258+
_lastSyncRequestTickTime = now;
257259
}
258260

259261
return State::RequestLastValues;
@@ -263,62 +265,62 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
263265
{
264266
if (!_mqttClient.connected())
265267
{
266-
DBG_ERROR(F("ArduinoIoTCloudTCP::%s MQTT client connection lost"), __FUNCTION__);
268+
DBG_ERROR(F("ArduinoIoTCloudTCP::%s MQTT client connection lost"), __FUNCTION__);
267269

268-
/* Forcefully disconnect MQTT client and trigger a reconnection. */
269-
_mqttClient.stop();
270+
/* Forcefully disconnect MQTT client and trigger a reconnection. */
271+
_mqttClient.stop();
270272

271-
/* The last message was definitely lost, trigger a retransmit. */
272-
_mqtt_data_request_retransmit = true;
273+
/* The last message was definitely lost, trigger a retransmit. */
274+
_mqtt_data_request_retransmit = true;
273275

274-
/* We are not connected anymore, trigger the callback for a disconnected event. */
275-
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
276+
/* We are not connected anymore, trigger the callback for a disconnected event. */
277+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
276278

277-
return State::ConnectPhy;
279+
return State::ConnectPhy;
278280
}
279281
/* We are connected so let's to our stuff here. */
280282
else
281283
{
282-
/* Check if a primitive property wrapper is locally changed.
283-
* This function requires an existing time service which in
284-
* turn requires an established connection. Not having that
285-
* leads to a wrong time set in the time service which inhibits
286-
* the connection from being established due to a wrong data
287-
* in the reconstructed certificate.
288-
*/
289-
updateTimestampOnLocallyChangedProperties(_property_container);
290-
291-
/* Retransmit data in case there was a lost transaction due
292-
* to phy layer or MQTT connectivity loss.
293-
*/
294-
if(_mqtt_data_request_retransmit && (_mqtt_data_len > 0)) {
295-
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
296-
_mqtt_data_request_retransmit = false;
297-
}
298-
299-
/* Check if any properties need encoding and send them to
300-
* the cloud if necessary.
301-
*/
302-
sendPropertiesToCloud();
284+
/* Check if a primitive property wrapper is locally changed.
285+
* This function requires an existing time service which in
286+
* turn requires an established connection. Not having that
287+
* leads to a wrong time set in the time service which inhibits
288+
* the connection from being established due to a wrong data
289+
* in the reconstructed certificate.
290+
*/
291+
updateTimestampOnLocallyChangedProperties(_property_container);
292+
293+
/* Retransmit data in case there was a lost transaction due
294+
* to phy layer or MQTT connectivity loss.
295+
*/
296+
if(_mqtt_data_request_retransmit && (_mqtt_data_len > 0)) {
297+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
298+
_mqtt_data_request_retransmit = false;
299+
}
300+
301+
/* Check if any properties need encoding and send them to
302+
* the cloud if necessary.
303+
*/
304+
sendPropertiesToCloud();
303305

304306
#if OTA_ENABLED
305-
/* Request a OTA download if the hidden property
306-
* OTA request has been set.
307-
*/
308-
if (_ota_req)
309-
{
310-
/* Clear the error flag. */
311-
_ota_error = static_cast<int>(OTAError::None);
312-
/* Transmit the cleared error flag to the cloud. */
313-
sendPropertiesToCloud();
314-
/* Clear the request flag. */
315-
_ota_req = false;
316-
/* Call member function to handle OTA request. */
317-
onOTARequest();
318-
}
307+
/* Request a OTA download if the hidden property
308+
* OTA request has been set.
309+
*/
310+
if (_ota_req)
311+
{
312+
/* Clear the error flag. */
313+
_ota_error = static_cast<int>(OTAError::None);
314+
/* Transmit the cleared error flag to the cloud. */
315+
sendPropertiesToCloud();
316+
/* Clear the request flag. */
317+
_ota_req = false;
318+
/* Call member function to handle OTA request. */
319+
onOTARequest();
320+
}
319321
#endif /* OTA_ENABLED */
320322

321-
return State::Connected;
323+
return State::Connected;
322324
}
323325
}
324326

@@ -334,20 +336,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
334336
byte bytes[length];
335337

336338
for (int i = 0; i < length; i++) {
337-
bytes[i] = _mqttClient.read();
339+
bytes[i] = _mqttClient.read();
338340
}
339341

340342
if (_dataTopicIn == topic) {
341-
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length);
343+
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length);
342344
}
343345

344346
if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues))
345347
{
346-
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values received"), __FUNCTION__, millis());
347-
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
348-
sendPropertiesToCloud();
349-
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
350-
_state = State::Connected;
348+
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values received"), __FUNCTION__, millis());
349+
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
350+
sendPropertiesToCloud();
351+
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
352+
_state = State::Connected;
351353
}
352354
}
353355

@@ -357,16 +359,16 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud()
357359
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
358360

359361
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
360-
if (bytes_encoded > 0)
361-
{
362-
/* If properties have been encoded store them in the back-up buffer
363-
* in order to allow retransmission in case of failure.
364-
*/
365-
_mqtt_data_len = bytes_encoded;
366-
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
367-
/* Transmit the properties to the MQTT broker */
368-
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
369-
}
362+
if (bytes_encoded > 0)
363+
{
364+
/* If properties have been encoded store them in the back-up buffer
365+
* in order to allow retransmission in case of failure.
366+
*/
367+
_mqtt_data_len = bytes_encoded;
368+
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
369+
/* Transmit the properties to the MQTT broker */
370+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
371+
}
370372
}
371373

372374
void ArduinoIoTCloudTCP::requestLastValue()
@@ -381,11 +383,11 @@ void ArduinoIoTCloudTCP::requestLastValue()
381383
int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const length)
382384
{
383385
if (_mqttClient.beginMessage(topic, length, false, 0)) {
384-
if (_mqttClient.write(data, length)) {
385-
if (_mqttClient.endMessage()) {
386-
return 1;
387-
}
388-
}
386+
if (_mqttClient.write(data, length)) {
387+
if (_mqttClient.endMessage()) {
388+
return 1;
389+
}
390+
}
389391
}
390392
return 0;
391393
}
@@ -409,9 +411,9 @@ void ArduinoIoTCloudTCP::onOTARequest()
409411
uint8_t nina_ota_err_code = 0;
410412
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))
411413
{
412-
DBG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code);
413-
_ota_error = static_cast<int>(OTAError::DownloadFailed);
414-
return;
414+
DBG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code);
415+
_ota_error = static_cast<int>(OTAError::DownloadFailed);
416+
return;
415417
}
416418

417419
/* The download was a success. */
@@ -420,7 +422,7 @@ void ArduinoIoTCloudTCP::onOTARequest()
420422

421423
/* Perform the reset to reboot to SxU. */
422424
if (ota_download_success)
423-
NVIC_SystemReset();
425+
NVIC_SystemReset();
424426
}
425427
#endif
426428

0 commit comments

Comments
 (0)