diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index ae9a28caa..653952e42 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -33,7 +33,8 @@ #include "tls/utility/CryptoUtil.h" #endif -#if defined(ARDUINO_PORTENTA_H7_M7) +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) +# include # include "tls/utility/SHA256.h" # include #endif @@ -45,6 +46,7 @@ #include "utility/watchdog/Watchdog.h" + /****************************************************************************** * EXTERN ******************************************************************************/ @@ -579,83 +581,15 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l #if OTA_ENABLED void ArduinoIoTCloudTCP::onOTARequest() { -#ifdef ARDUINO_ARCH_SAMD - samd_watchdog_reset(); -#endif /* ARDUINO_ARCH_SAMD */ - DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str()); -#if OTA_STORAGE_SNU - /* Just to be safe delete any remains from previous updates. */ - WiFiStorage.remove("/fs/UPDATE.BIN.LZSS"); - WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP"); - #ifdef ARDUINO_ARCH_SAMD - samd_watchdog_reset(); -#endif /* ARDUINO_ARCH_SAMD */ - - /* Trigger direct download to nina module. */ - uint8_t nina_ota_err_code = 0; - if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code); - _ota_error = static_cast(OTAError::DownloadFailed); - return; - } - - /* Perform the reset to reboot to SxU. */ - NVIC_SystemReset(); -#endif /* OTA_STORAGE_SNU */ - -#if OTA_STORAGE_PORTENTA_QSPI - mbed_watchdog_reset(); - - Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None; - /* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */ - Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2); - - mbed_watchdog_reset(); - - /* Initialize the QSPI memory for OTA handling. */ - if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) { - DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast(ota_portenta_err)); - return; - } - - mbed_watchdog_reset(); - - /* Just to be safe delete any remains from previous updates. */ - remove("/fs/UPDATE.BIN"); - remove("/fs/UPDATE.BIN.LZSS"); - - mbed_watchdog_reset(); - - /* Download the OTA file from the web storage location. */ - int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download((char*)(_ota_url.c_str()), true /* is_https */); - DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", _ota_url.c_str(), ota_portenta_qspi_download_ret_code); - - mbed_watchdog_reset(); - - /* Decompress the LZSS compressed OTA file. */ - int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress(); - DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::decompress() returns %d", ota_portenta_qspi_decompress_ret_code); - if (ota_portenta_qspi_decompress_ret_code < 0) - { - DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::decompress() failed with %d", ota_portenta_qspi_decompress_ret_code); - return; - } - - mbed_watchdog_reset(); - - /* Schedule the firmware update. */ - if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) { - DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::update() failed with %d", static_cast(ota_portenta_err)); - return; - } + _ota_error = samd_onOTARequest(_ota_url.c_str()); +#endif - /* Perform the reset to reboot - then the bootloader performs the actual application update. */ - NVIC_SystemReset(); -#endif /* OTA_STORAGE_PORTENTA_QSPI */ +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) + _ota_error = portenta_h7_onOTARequest(_ota_url.c_str()); +#endif } #endif diff --git a/src/utility/ota/OTA-portenta-h7.cpp b/src/utility/ota/OTA-portenta-h7.cpp new file mode 100644 index 000000000..b8ec0772a --- /dev/null +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -0,0 +1,86 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + + This software is released under the GNU General Public License version 3, + which covers the main part of arduino-cli. + The terms of this license can be found at: + https://www.gnu.org/licenses/gpl-3.0.en.html + + You can be released from the requirements of the above licenses by purchasing + a commercial license. Buying such a license is mandatory if you want to modify or + otherwise use the software for commercial activities involving the Arduino + software without disclosing the source code of your own applications. To purchase + a commercial license, send an email to license@arduino.cc. +*/ + +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) + +/****************************************************************************** + * INCLUDE + ******************************************************************************/ + +#include "OTA.h" + +#include +#include + +#include "../watchdog/Watchdog.h" + +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +int portenta_h7_onOTARequest(char const * ota_url) +{ + mbed_watchdog_reset(); + + Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None; + /* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */ + Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2); + + mbed_watchdog_reset(); + + /* Initialize the QSPI memory for OTA handling. */ + if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) { + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast(ota_portenta_err)); + return static_cast(ota_portenta_err); + } + + mbed_watchdog_reset(); + + /* Just to be safe delete any remains from previous updates. */ + remove("/fs/UPDATE.BIN"); + remove("/fs/UPDATE.BIN.LZSS"); + + mbed_watchdog_reset(); + + /* Download the OTA file from the web storage location. */ + int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */); + DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", ota_url, ota_portenta_qspi_download_ret_code); + + mbed_watchdog_reset(); + + /* Decompress the LZSS compressed OTA file. */ + int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress(); + DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::decompress() returns %d", ota_portenta_qspi_decompress_ret_code); + if (ota_portenta_qspi_decompress_ret_code < 0) + { + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::decompress() failed with %d", ota_portenta_qspi_decompress_ret_code); + return ota_portenta_qspi_decompress_ret_code; + } + + mbed_watchdog_reset(); + + /* Schedule the firmware update. */ + if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) { + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::update() failed with %d", static_cast(ota_portenta_err)); + return static_cast(ota_portenta_err); + } + + /* Perform the reset to reboot - then the bootloader performs the actual application update. */ + NVIC_SystemReset(); +} + +#endif /* defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) */ diff --git a/src/utility/ota/OTA-samd.cpp b/src/utility/ota/OTA-samd.cpp new file mode 100644 index 000000000..c5ee8a8d5 --- /dev/null +++ b/src/utility/ota/OTA-samd.cpp @@ -0,0 +1,63 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + + This software is released under the GNU General Public License version 3, + which covers the main part of arduino-cli. + The terms of this license can be found at: + https://www.gnu.org/licenses/gpl-3.0.en.html + + You can be released from the requirements of the above licenses by purchasing + a commercial license. Buying such a license is mandatory if you want to modify or + otherwise use the software for commercial activities involving the Arduino + software without disclosing the source code of your own applications. To purchase + a commercial license, send an email to license@arduino.cc. +*/ + +#ifdef ARDUINO_ARCH_SAMD + +/****************************************************************************** + * INCLUDE + ******************************************************************************/ + +#include "OTA.h" + +#include + +#include "../watchdog/Watchdog.h" + +#if OTA_STORAGE_SNU +# include +# include /* WiFiStorage */ +#endif + +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +int samd_onOTARequest(char const * ota_url) +{ + samd_watchdog_reset(); + +#if OTA_STORAGE_SNU + /* Just to be safe delete any remains from previous updates. */ + WiFiStorage.remove("/fs/UPDATE.BIN.LZSS"); + WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP"); + + samd_watchdog_reset(); + + /* Trigger direct download to nina module. */ + uint8_t nina_ota_err_code = 0; + if (!WiFiStorage.downloadOTA(ota_url, &nina_ota_err_code)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code); + return static_cast(OTAError::DownloadFailed); + } + + /* Perform the reset to reboot to SxU. */ + NVIC_SystemReset(); +#endif /* OTA_STORAGE_SNU */ +} + +#endif /* ARDUINO_ARCH_SAMD */ diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 670b5ac07..6a9b2312f 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -23,11 +23,6 @@ ******************************************************************************/ #include -#if OTA_ENABLED - -#if OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#endif /* OTA_STORAGE_SNU */ #if OTA_STORAGE_SSU #include @@ -37,10 +32,6 @@ #include #endif /* OTA_STORAGE_SFU */ -#if OTA_STORAGE_PORTENTA_QSPI - #include -#endif /* OTA_STORAGE_PORTENTA_QSPI */ - /****************************************************************************** * TYPEDEF ******************************************************************************/ @@ -51,6 +42,16 @@ enum class OTAError : int DownloadFailed = 1, }; -#endif /* OTA_ENABLED */ +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +int samd_onOTARequest(char const * ota_url); +#endif + +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) +int portenta_h7_onOTARequest(char const * ota_url); +#endif #endif /* ARDUINO_OTA_LOGIC_H_ */