Skip to content

Commit c58dad7

Browse files
committed
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.
1 parent 8697692 commit c58dad7

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

src/ArduinoIoTCloudTCP.cpp

+1-23
Original file line numberDiff line numberDiff line change
@@ -579,34 +579,12 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
579579
#if OTA_ENABLED
580580
void ArduinoIoTCloudTCP::onOTARequest()
581581
{
582-
#ifdef ARDUINO_ARCH_SAMD
583-
samd_watchdog_reset();
584-
#endif /* ARDUINO_ARCH_SAMD */
585-
586582
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
587583

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-
593584
#ifdef ARDUINO_ARCH_SAMD
594-
samd_watchdog_reset();
585+
_ota_error = samd_onOTARequest(_ota_url.c_str());
595586
#endif /* ARDUINO_ARCH_SAMD */
596587

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-
610588
#if OTA_STORAGE_PORTENTA_QSPI
611589
mbed_watchdog_reset();
612590

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.h>
27+
#include <Arduino_DebugUtils.h>
28+
29+
#include "../watchdog/Watchdog.h"
30+
31+
#if OTA_STORAGE_SNU
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

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ enum class OTAError : int
5151
DownloadFailed = 1,
5252
};
5353

54+
/******************************************************************************
55+
* FUNCTION DEFINITION
56+
******************************************************************************/
57+
58+
#ifdef ARDUINO_ARCH_SAMD
59+
int samd_onOTARequest(char const * ota_url);
60+
#endif
61+
5462
#endif /* OTA_ENABLED */
5563

5664
#endif /* ARDUINO_OTA_LOGIC_H_ */

0 commit comments

Comments
 (0)