From c58dad775dc1074d98ce15f60311342590d57c79 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 10 May 2021 08:30:12 +0200 Subject: [PATCH 1/5] Extract samd related OTA request code into function 'samd_onOTARequest'. The main imperative behind doing this is that the method ArduinoIoTCloudTCP::onOTARequest is getting really cluttered and for the sake of clarity its better to extract the code into a separate function. --- src/ArduinoIoTCloudTCP.cpp | 24 +------------- src/utility/ota/OTA-samd.cpp | 63 ++++++++++++++++++++++++++++++++++++ src/utility/ota/OTA.h | 8 +++++ 3 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 src/utility/ota/OTA-samd.cpp diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index ae9a28caa..a13e4231e 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -579,34 +579,12 @@ 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(); + _ota_error = samd_onOTARequest(_ota_url.c_str()); #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(); diff --git a/src/utility/ota/OTA-samd.cpp b/src/utility/ota/OTA-samd.cpp new file mode 100644 index 000000000..50e5fbf04 --- /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 + +#include "../watchdog/Watchdog.h" + +#if OTA_STORAGE_SNU +# 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..051aface6 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -51,6 +51,14 @@ enum class OTAError : int DownloadFailed = 1, }; +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +int samd_onOTARequest(char const * ota_url); +#endif + #endif /* OTA_ENABLED */ #endif /* ARDUINO_OTA_LOGIC_H_ */ From 790fc11610a3af37b4d52f2981d1634d32811bd3 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 10 May 2021 08:32:55 +0200 Subject: [PATCH 2/5] The #if OTA_ENABLED preprocessor guards can be safely removed here, since it will not have any impact on the compilation process but enhances clarity for the programmer. --- src/utility/ota/OTA.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 051aface6..62b34a69a 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -23,7 +23,6 @@ ******************************************************************************/ #include -#if OTA_ENABLED #if OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) #include @@ -59,6 +58,4 @@ enum class OTAError : int int samd_onOTARequest(char const * ota_url); #endif -#endif /* OTA_ENABLED */ - #endif /* ARDUINO_OTA_LOGIC_H_ */ From 306d569f67e440100c714885859b2beae5fd0fd4 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 10 May 2021 08:49:44 +0200 Subject: [PATCH 3/5] Extracting Portenta H7 OTA code to method 'portenta_h7_onOTARequest' --- src/ArduinoIoTCloudTCP.cpp | 54 ++---------------- src/utility/ota/OTA-portenta-h7.cpp | 85 +++++++++++++++++++++++++++++ src/utility/ota/OTA-samd.cpp | 1 - src/utility/ota/OTA.h | 4 ++ 4 files changed, 93 insertions(+), 51 deletions(-) create mode 100644 src/utility/ota/OTA-portenta-h7.cpp diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index a13e4231e..6e63d181b 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -583,57 +583,11 @@ void ArduinoIoTCloudTCP::onOTARequest() #ifdef ARDUINO_ARCH_SAMD _ota_error = samd_onOTARequest(_ota_url.c_str()); -#endif /* ARDUINO_ARCH_SAMD */ - -#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; - } +#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..19c712a2a --- /dev/null +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -0,0 +1,85 @@ +/* + 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 "../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 index 50e5fbf04..06ddd3136 100644 --- a/src/utility/ota/OTA-samd.cpp +++ b/src/utility/ota/OTA-samd.cpp @@ -23,7 +23,6 @@ #include "OTA.h" -#include #include #include "../watchdog/Watchdog.h" diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 62b34a69a..cbcad0344 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -58,4 +58,8 @@ enum class OTAError : int 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_ */ From 40ea7d5f76d854c44cc2b94cb1e429a621df54fe Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 10 May 2021 08:52:32 +0200 Subject: [PATCH 4/5] Move inclusion of Arduino_Portenta_OTA library within OTA-portenta-h7.cpp (no need for it to be included outside of this module. --- src/ArduinoIoTCloudTCP.cpp | 4 +++- src/utility/ota/OTA-portenta-h7.cpp | 1 + src/utility/ota/OTA.h | 4 ---- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 6e63d181b..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 ******************************************************************************/ diff --git a/src/utility/ota/OTA-portenta-h7.cpp b/src/utility/ota/OTA-portenta-h7.cpp index 19c712a2a..b8ec0772a 100644 --- a/src/utility/ota/OTA-portenta-h7.cpp +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -24,6 +24,7 @@ #include "OTA.h" #include +#include #include "../watchdog/Watchdog.h" diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index cbcad0344..70a5916b1 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -36,10 +36,6 @@ #include #endif /* OTA_STORAGE_SFU */ -#if OTA_STORAGE_PORTENTA_QSPI - #include -#endif /* OTA_STORAGE_PORTENTA_QSPI */ - /****************************************************************************** * TYPEDEF ******************************************************************************/ From b0fc197f920089ffa3a30af838ed7a2bcaad4400 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 10 May 2021 09:00:39 +0200 Subject: [PATCH 5/5] Moving inclusion of SNU library into OTA-samd.cpp - where it belongs. --- src/utility/ota/OTA-samd.cpp | 1 + src/utility/ota/OTA.h | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/utility/ota/OTA-samd.cpp b/src/utility/ota/OTA-samd.cpp index 06ddd3136..c5ee8a8d5 100644 --- a/src/utility/ota/OTA-samd.cpp +++ b/src/utility/ota/OTA-samd.cpp @@ -28,6 +28,7 @@ #include "../watchdog/Watchdog.h" #if OTA_STORAGE_SNU +# include # include /* WiFiStorage */ #endif diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 70a5916b1..6a9b2312f 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -24,10 +24,6 @@ #include -#if OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#endif /* OTA_STORAGE_SNU */ - #if OTA_STORAGE_SSU #include #endif /* OTA_STORAGE_SSU */