@@ -330,7 +330,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
330330{
331331 if (!_mqttClient.connected ())
332332 {
333- DEBUG_ERROR ( " ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
333+ DBG_ERROR ( F ( " ArduinoIoTCloudTCP::%s MQTT client connection lost" ) , __FUNCTION__);
334334
335335 /* Forcefully disconnect MQTT client and trigger a reconnection. */
336336 _mqttClient.stop ();
@@ -460,12 +460,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
460460#if OTA_ENABLED
461461void ArduinoIoTCloudTCP::onOTARequest ()
462462{
463- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s _ota_url = %s" , __FUNCTION__, _ota_url.c_str ());
464-
465- /* Status flag to prevent the reset from being executed
466- * when HTTPS download is not supported.
467- */
468- bool ota_download_success = false ;
463+ DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s _ota_url = %s" ), __FUNCTION__, _ota_url.c_str ());
469464
470465#if OTA_STORAGE_SNU
471466 /* Just to be safe delete any remains from previous updates. */
@@ -481,40 +476,49 @@ void ArduinoIoTCloudTCP::onOTARequest()
481476 return ;
482477 }
483478
484- /* The download was a success . */
485- ota_download_success = true ;
479+ /* Perform the reset to reboot to SxU . */
480+ NVIC_SystemReset () ;
486481#endif /* OTA_STORAGE_SNU */
487482
488483#if OTA_STORAGE_PORTENTA_QSPI
489484 /* Just to be safe delete any remains from previous updates. */
490485 remove (" /fs/UPDATE.BIN.LZSS" );
491486
492- // Use second partition
493- OTAPortenta.begin (QSPI_FLASH_FATFS_MBR, 2 );
487+ Arduino_OTA_Portenta::Error ota_portenta_err = Arduino_OTA_Portenta::Error::None;
488+ /* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */
489+ Arduino_OTA_Portenta_QSPI ota_portenta_qspi (QSPI_FLASH_FATFS_MBR, 2 );
494490
495- if (OTAPortenta.download (_ota_url.c_str ()))
496- {
497- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s error download to nina: %d" ), __FUNCTION__, nina_ota_err_code);
498- _ota_error = static_cast <int >(OTAError::DownloadFailed);
491+ /* Initialize the QSPI memory for OTA handling. */
492+ if ((ota_portenta_err = ota_portenta_qspi.begin ()) != Arduino_OTA_Portenta::Error::None) {
493+ DBG_ERROR (F (" Arduino_OTA_Portenta_QSPI::begin() failed with %d" ), static_cast <int >(ota_portenta_err));
499494 return ;
500495 }
501496
502- auto update_file_size = OTAPortenta.decompress ();
503- OTAPortenta.setUpdateLen (update_file_size);
497+ /* Download the OTA file from the web storage location. */
498+ int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download ((char *)(_ota_url.c_str ()));
499+ DBG_VERBOSE (F (" Arduino_OTA_Portenta_QSPI::download(%s) returns %d" ), _ota_url.c_str (), ota_portenta_qspi_download_ret_code);
504500
505- /* The download was a success. */
506- ota_download_success = true ;
501+ /* Decompress the LZSS compressed OTA file. */
502+ int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress ();
503+ DBG_VERBOSE (F (" Arduino_OTA_Portenta_QSPI::decompress() returns %d" ), ota_portenta_qspi_decompress_ret_code);
504+ if (ota_portenta_qspi_decompress_ret_code < 0 )
505+ {
506+ DBG_ERROR (F (" Arduino_OTA_Portenta_QSPI::decompress() failed with %d" ), ota_portenta_qspi_decompress_ret_code);
507+ return ;
508+ }
509+ /* Set the correct update size. */
510+ size_t const update_file_size = ota_portenta_qspi_decompress_ret_code;
511+ ota_portenta_qspi.setUpdateLen (update_file_size);
507512
508- while (1 ) {
509- OTAPortenta.update ();
513+ /* Schedule the firmware update. */
514+ if ((ota_portenta_err = ota_portenta_qspi.update ()) != Arduino_OTA_Portenta::Error::None) {
515+ DBG_ERROR (F (" Arduino_OTA_Portenta_QSPI::update() failed with %d" ), static_cast <int >(ota_portenta_err));
516+ return ;
510517 }
511- #endif /* OTA_STORAGE_PORTENTA_QSPI */
512518
513- #ifndef __AVR__
514- /* Perform the reset to reboot to SxU. */
515- if (ota_download_success)
516- NVIC_SystemReset ();
517- #endif
519+ /* Perform the reset to reboot - then the bootloader performs the actual application update. */
520+ NVIC_SystemReset ();
521+ #endif /* OTA_STORAGE_PORTENTA_QSPI */
518522}
519523#endif
520524
0 commit comments