Skip to content

Commit eed539b

Browse files
authored
Merge pull request #252 from arduino-libraries/refactor-export-ota-request-handler
Refactor OTA request handler code
2 parents 8697692 + b0fc197 commit eed539b

File tree

4 files changed

+168
-84
lines changed

4 files changed

+168
-84
lines changed

src/ArduinoIoTCloudTCP.cpp

+8-74
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
#include "tls/utility/CryptoUtil.h"
3434
#endif
3535

36-
#if defined(ARDUINO_PORTENTA_H7_M7)
36+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
37+
# include <algorithm>
3738
# include "tls/utility/SHA256.h"
3839
# include <stm32h7xx_hal_rtc_ex.h>
3940
#endif
@@ -45,6 +46,7 @@
4546

4647
#include "utility/watchdog/Watchdog.h"
4748

49+
4850
/******************************************************************************
4951
* EXTERN
5052
******************************************************************************/
@@ -579,83 +581,15 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
579581
#if OTA_ENABLED
580582
void ArduinoIoTCloudTCP::onOTARequest()
581583
{
582-
#ifdef ARDUINO_ARCH_SAMD
583-
samd_watchdog_reset();
584-
#endif /* ARDUINO_ARCH_SAMD */
585-
586584
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
587585

588-
#if OTA_STORAGE_SNU
589-
/* Just to be safe delete any remains from previous updates. */
590-
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
591-
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
592-
593586
#ifdef ARDUINO_ARCH_SAMD
594-
samd_watchdog_reset();
595-
#endif /* ARDUINO_ARCH_SAMD */
596-
597-
/* Trigger direct download to nina module. */
598-
uint8_t nina_ota_err_code = 0;
599-
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))
600-
{
601-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code);
602-
_ota_error = static_cast<int>(OTAError::DownloadFailed);
603-
return;
604-
}
605-
606-
/* Perform the reset to reboot to SxU. */
607-
NVIC_SystemReset();
608-
#endif /* OTA_STORAGE_SNU */
609-
610-
#if OTA_STORAGE_PORTENTA_QSPI
611-
mbed_watchdog_reset();
612-
613-
Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None;
614-
/* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */
615-
Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2);
616-
617-
mbed_watchdog_reset();
618-
619-
/* Initialize the QSPI memory for OTA handling. */
620-
if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) {
621-
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast<int>(ota_portenta_err));
622-
return;
623-
}
624-
625-
mbed_watchdog_reset();
626-
627-
/* Just to be safe delete any remains from previous updates. */
628-
remove("/fs/UPDATE.BIN");
629-
remove("/fs/UPDATE.BIN.LZSS");
630-
631-
mbed_watchdog_reset();
632-
633-
/* Download the OTA file from the web storage location. */
634-
int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download((char*)(_ota_url.c_str()), true /* is_https */);
635-
DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", _ota_url.c_str(), ota_portenta_qspi_download_ret_code);
636-
637-
mbed_watchdog_reset();
638-
639-
/* Decompress the LZSS compressed OTA file. */
640-
int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress();
641-
DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::decompress() returns %d", ota_portenta_qspi_decompress_ret_code);
642-
if (ota_portenta_qspi_decompress_ret_code < 0)
643-
{
644-
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::decompress() failed with %d", ota_portenta_qspi_decompress_ret_code);
645-
return;
646-
}
647-
648-
mbed_watchdog_reset();
649-
650-
/* Schedule the firmware update. */
651-
if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) {
652-
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::update() failed with %d", static_cast<int>(ota_portenta_err));
653-
return;
654-
}
587+
_ota_error = samd_onOTARequest(_ota_url.c_str());
588+
#endif
655589

656-
/* Perform the reset to reboot - then the bootloader performs the actual application update. */
657-
NVIC_SystemReset();
658-
#endif /* OTA_STORAGE_PORTENTA_QSPI */
590+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
591+
_ota_error = portenta_h7_onOTARequest(_ota_url.c_str());
592+
#endif
659593
}
660594
#endif
661595

src/utility/ota/OTA-portenta-h7.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
19+
20+
/******************************************************************************
21+
* INCLUDE
22+
******************************************************************************/
23+
24+
#include "OTA.h"
25+
26+
#include <Arduino_DebugUtils.h>
27+
#include <Arduino_Portenta_OTA.h>
28+
29+
#include "../watchdog/Watchdog.h"
30+
31+
/******************************************************************************
32+
* FUNCTION DEFINITION
33+
******************************************************************************/
34+
35+
int portenta_h7_onOTARequest(char const * ota_url)
36+
{
37+
mbed_watchdog_reset();
38+
39+
Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None;
40+
/* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */
41+
Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2);
42+
43+
mbed_watchdog_reset();
44+
45+
/* Initialize the QSPI memory for OTA handling. */
46+
if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) {
47+
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast<int>(ota_portenta_err));
48+
return static_cast<int>(ota_portenta_err);
49+
}
50+
51+
mbed_watchdog_reset();
52+
53+
/* Just to be safe delete any remains from previous updates. */
54+
remove("/fs/UPDATE.BIN");
55+
remove("/fs/UPDATE.BIN.LZSS");
56+
57+
mbed_watchdog_reset();
58+
59+
/* Download the OTA file from the web storage location. */
60+
int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */);
61+
DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", ota_url, ota_portenta_qspi_download_ret_code);
62+
63+
mbed_watchdog_reset();
64+
65+
/* Decompress the LZSS compressed OTA file. */
66+
int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress();
67+
DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::decompress() returns %d", ota_portenta_qspi_decompress_ret_code);
68+
if (ota_portenta_qspi_decompress_ret_code < 0)
69+
{
70+
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::decompress() failed with %d", ota_portenta_qspi_decompress_ret_code);
71+
return ota_portenta_qspi_decompress_ret_code;
72+
}
73+
74+
mbed_watchdog_reset();
75+
76+
/* Schedule the firmware update. */
77+
if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) {
78+
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::update() failed with %d", static_cast<int>(ota_portenta_err));
79+
return static_cast<int>(ota_portenta_err);
80+
}
81+
82+
/* Perform the reset to reboot - then the bootloader performs the actual application update. */
83+
NVIC_SystemReset();
84+
}
85+
86+
#endif /* defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) */

src/utility/ota/OTA-samd.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifdef ARDUINO_ARCH_SAMD
19+
20+
/******************************************************************************
21+
* INCLUDE
22+
******************************************************************************/
23+
24+
#include "OTA.h"
25+
26+
#include <Arduino_DebugUtils.h>
27+
28+
#include "../watchdog/Watchdog.h"
29+
30+
#if OTA_STORAGE_SNU
31+
# include <SNU.h>
32+
# include <WiFiNINA.h> /* WiFiStorage */
33+
#endif
34+
35+
/******************************************************************************
36+
* FUNCTION DEFINITION
37+
******************************************************************************/
38+
39+
int samd_onOTARequest(char const * ota_url)
40+
{
41+
samd_watchdog_reset();
42+
43+
#if OTA_STORAGE_SNU
44+
/* Just to be safe delete any remains from previous updates. */
45+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
46+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
47+
48+
samd_watchdog_reset();
49+
50+
/* Trigger direct download to nina module. */
51+
uint8_t nina_ota_err_code = 0;
52+
if (!WiFiStorage.downloadOTA(ota_url, &nina_ota_err_code))
53+
{
54+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code);
55+
return static_cast<int>(OTAError::DownloadFailed);
56+
}
57+
58+
/* Perform the reset to reboot to SxU. */
59+
NVIC_SystemReset();
60+
#endif /* OTA_STORAGE_SNU */
61+
}
62+
63+
#endif /* ARDUINO_ARCH_SAMD */

src/utility/ota/OTA.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
******************************************************************************/
2424

2525
#include <AIoTC_Config.h>
26-
#if OTA_ENABLED
27-
28-
#if OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2)
29-
#include <SNU.h>
30-
#endif /* OTA_STORAGE_SNU */
3126

3227
#if OTA_STORAGE_SSU
3328
#include <SSU.h>
@@ -37,10 +32,6 @@
3732
#include <SFU.h>
3833
#endif /* OTA_STORAGE_SFU */
3934

40-
#if OTA_STORAGE_PORTENTA_QSPI
41-
#include <Arduino_Portenta_OTA.h>
42-
#endif /* OTA_STORAGE_PORTENTA_QSPI */
43-
4435
/******************************************************************************
4536
* TYPEDEF
4637
******************************************************************************/
@@ -51,6 +42,16 @@ enum class OTAError : int
5142
DownloadFailed = 1,
5243
};
5344

54-
#endif /* OTA_ENABLED */
45+
/******************************************************************************
46+
* FUNCTION DEFINITION
47+
******************************************************************************/
48+
49+
#ifdef ARDUINO_ARCH_SAMD
50+
int samd_onOTARequest(char const * ota_url);
51+
#endif
52+
53+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
54+
int portenta_h7_onOTARequest(char const * ota_url);
55+
#endif
5556

5657
#endif /* ARDUINO_OTA_LOGIC_H_ */

0 commit comments

Comments
 (0)