@@ -149,11 +149,11 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
149
149
#if OTA_STORAGE_SNU
150
150
String const nina_fw_version = WiFi.firmwareVersion ();
151
151
if (nina_fw_version < " 1.4.1" ) {
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 ());
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 ());
154
154
}
155
155
else {
156
- _ota_cap = true ;
156
+ _ota_cap = true ;
157
157
}
158
158
#endif /* OTA_STORAGE_SNU */
159
159
@@ -177,7 +177,7 @@ void ArduinoIoTCloudTCP::update()
177
177
178
178
/* Check for new data from the MQTT client. */
179
179
if (_mqttClient.connected ())
180
- _mqttClient.poll ();
180
+ _mqttClient.poll ();
181
181
}
182
182
183
183
int ArduinoIoTCloudTCP::connected ()
@@ -200,9 +200,9 @@ void ArduinoIoTCloudTCP::printDebugInfo()
200
200
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy ()
201
201
{
202
202
if (_connection->check () == NetworkConnectionState::CONNECTED)
203
- return State::SyncTime;
203
+ return State::SyncTime;
204
204
else
205
- return State::ConnectPhy;
205
+ return State::ConnectPhy;
206
206
}
207
207
208
208
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime ()
@@ -215,7 +215,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
215
215
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker ()
216
216
{
217
217
if (_mqttClient.connect (_brokerAddress.c_str (), _brokerPort))
218
- return State::SubscribeMqttTopics;
218
+ return State::SubscribeMqttTopics;
219
219
220
220
DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not connect to %s:%d" ), __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
221
221
return State::ConnectPhy;
@@ -225,26 +225,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
225
225
{
226
226
if (!_mqttClient.subscribe (_dataTopicIn))
227
227
{
228
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _dataTopicIn.c_str ());
229
- return State::SubscribeMqttTopics;
228
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _dataTopicIn.c_str ());
229
+ return State::SubscribeMqttTopics;
230
230
}
231
231
232
232
if (_shadowTopicIn != " " )
233
233
{
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
- }
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
+ }
239
239
}
240
240
241
241
DBG_INFO (F (" Connected to Arduino IoT Cloud" ));
242
242
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
243
243
244
244
if (_shadowTopicIn != " " )
245
- return State::RequestLastValues;
245
+ return State::RequestLastValues;
246
246
else
247
- return State::Connected;
247
+ return State::Connected;
248
248
}
249
249
250
250
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues ()
@@ -253,9 +253,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
253
253
unsigned long const now = millis ();
254
254
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
255
255
{
256
- DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values requested" ), __FUNCTION__, now);
257
- requestLastValue ();
258
- _lastSyncRequestTickTime = now;
256
+ DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values requested" ), __FUNCTION__, now);
257
+ requestLastValue ();
258
+ _lastSyncRequestTickTime = now;
259
259
}
260
260
261
261
return State::RequestLastValues;
@@ -265,62 +265,62 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
265
265
{
266
266
if (!_mqttClient.connected ())
267
267
{
268
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s MQTT client connection lost" ), __FUNCTION__);
268
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s MQTT client connection lost" ), __FUNCTION__);
269
269
270
- /* Forcefully disconnect MQTT client and trigger a reconnection. */
271
- _mqttClient.stop ();
270
+ /* Forcefully disconnect MQTT client and trigger a reconnection. */
271
+ _mqttClient.stop ();
272
272
273
- /* The last message was definitely lost, trigger a retransmit. */
274
- _mqtt_data_request_retransmit = true ;
273
+ /* The last message was definitely lost, trigger a retransmit. */
274
+ _mqtt_data_request_retransmit = true ;
275
275
276
- /* We are not connected anymore, trigger the callback for a disconnected event. */
277
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
276
+ /* We are not connected anymore, trigger the callback for a disconnected event. */
277
+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
278
278
279
- return State::ConnectPhy;
279
+ return State::ConnectPhy;
280
280
}
281
281
/* We are connected so let's to our stuff here. */
282
282
else
283
283
{
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
- }
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
300
301
- /* Check if any properties need encoding and send them to
302
- * the cloud if necessary.
303
- */
304
- sendPropertiesToCloud ();
301
+ /* Check if any properties need encoding and send them to
302
+ * the cloud if necessary.
303
+ */
304
+ sendPropertiesToCloud ();
305
305
306
306
#if OTA_ENABLED
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
- }
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
+ }
321
321
#endif /* OTA_ENABLED */
322
322
323
- return State::Connected;
323
+ return State::Connected;
324
324
}
325
325
}
326
326
@@ -336,20 +336,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
336
336
byte bytes[length];
337
337
338
338
for (int i = 0 ; i < length; i++) {
339
- bytes[i] = _mqttClient.read ();
339
+ bytes[i] = _mqttClient.read ();
340
340
}
341
341
342
342
if (_dataTopicIn == topic) {
343
- CBORDecoder::decode (_property_container, (uint8_t *)bytes, length);
343
+ CBORDecoder::decode (_property_container, (uint8_t *)bytes, length);
344
344
}
345
345
346
346
if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues))
347
347
{
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;
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;
353
353
}
354
354
}
355
355
@@ -359,16 +359,16 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud()
359
359
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
360
360
361
361
if (CBOREncoder::encode (_property_container, data, sizeof (data), bytes_encoded, false ) == CborNoError)
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
- }
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
+ }
372
372
}
373
373
374
374
void ArduinoIoTCloudTCP::requestLastValue ()
@@ -383,12 +383,12 @@ void ArduinoIoTCloudTCP::requestLastValue()
383
383
int ArduinoIoTCloudTCP::write (String const topic, byte const data[], int const length)
384
384
{
385
385
if (_mqttClient.beginMessage (topic, length, false , 0 )) {
386
- if (_mqttClient.write (data, length)) {
387
- if (_mqttClient.endMessage ()) {
388
- return 1 ;
386
+ if (_mqttClient.write (data, length)) {
387
+ if (_mqttClient.endMessage ()) {
388
+ return 1 ;
389
+ }
389
390
}
390
391
}
391
- }
392
392
return 0 ;
393
393
}
394
394
@@ -411,9 +411,9 @@ void ArduinoIoTCloudTCP::onOTARequest()
411
411
uint8_t nina_ota_err_code = 0 ;
412
412
if (!WiFiStorage.downloadOTA (_ota_url.c_str (), &nina_ota_err_code))
413
413
{
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 ;
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 ;
417
417
}
418
418
419
419
/* The download was a success. */
@@ -422,7 +422,7 @@ void ArduinoIoTCloudTCP::onOTARequest()
422
422
423
423
/* Perform the reset to reboot to SxU. */
424
424
if (ota_download_success)
425
- NVIC_SystemReset ();
425
+ NVIC_SystemReset ();
426
426
}
427
427
#endif
428
428
0 commit comments