Skip to content

Commit 840f6bd

Browse files
authored
Merge pull request #351 from pennam/ota_move_code
Ota: move code
2 parents d24c841 + ddcfdec commit 840f6bd

8 files changed

+162
-97
lines changed

src/ArduinoIoTCloudTCP.cpp

+4-73
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
138138
#endif /* AVR */
139139

140140
#if OTA_ENABLED && !defined(__AVR__)
141-
_ota_img_sha256 = getOTAImageSHA256();
141+
_ota_img_sha256 = OTA::getImageSHA256();
142142
DEBUG_VERBOSE("SHA256: HASH(%d) = %s", strlen(_ota_img_sha256.c_str()), _ota_img_sha256.c_str());
143143
#endif /* OTA_ENABLED */
144144

@@ -205,38 +205,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
205205
addPropertyReal(_tz_offset, "tz_offset", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
206206
addPropertyReal(_tz_dst_until, "tz_dst_until", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
207207

208-
#if OTA_STORAGE_PORTENTA_QSPI
209-
#define BOOTLOADER_ADDR (0x8000000)
210-
uint32_t bootloader_data_offset = 0x1F000;
211-
uint8_t* bootloader_data = (uint8_t*)(BOOTLOADER_ADDR + bootloader_data_offset);
212-
uint8_t currentBootloaderVersion = bootloader_data[1];
213-
if (currentBootloaderVersion < 22) {
214-
_ota_cap = false;
215-
DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, update the bootloader", __FUNCTION__);
216-
DEBUG_WARNING("ArduinoIoTCloudTCP::%s File -> Examples -> Portenta_System -> PortentaH7_updateBootloader", __FUNCTION__);
217-
}
218-
else {
219-
_ota_cap = true;
220-
}
221-
#endif
222-
223-
#if OTA_STORAGE_SNU && OTA_ENABLED
224-
if (String(WiFi.firmwareVersion()) < String("1.4.1")) {
225-
_ota_cap = false;
226-
DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
227-
}
228-
else {
229-
_ota_cap = true;
230-
}
231-
#endif /* OTA_STORAGE_SNU */
232-
233-
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_NICLA_VISION)
234-
_ota_cap = true;
235-
#endif
236-
237-
#if defined(ARDUINO_ARCH_ESP32) && OTA_ENABLED
238-
/* NOTE: here is possible to check if current partition scheme is OTA compatible */
239-
_ota_cap = true;
208+
#if OTA_ENABLED
209+
_ota_cap = OTA::isCapable();
240210
#endif
241211

242212
#ifdef BOARD_HAS_OFFLOADED_ECCX08
@@ -609,7 +579,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
609579
/* Transmit the cleared request flags to the cloud. */
610580
sendDevicePropertyToCloud("OTA_REQ");
611581
/* Call member function to handle OTA request. */
612-
onOTARequest();
582+
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
613583
/* If something fails send the OTA error to the cloud */
614584
sendDevicePropertyToCloud("OTA_ERROR");
615585
}
@@ -762,45 +732,6 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
762732
return 0;
763733
}
764734

765-
#if OTA_ENABLED
766-
void ArduinoIoTCloudTCP::onOTARequest()
767-
{
768-
DEBUG_INFO("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
769-
770-
#ifdef ARDUINO_ARCH_SAMD
771-
_ota_error = samd_onOTARequest(_ota_url.c_str());
772-
#endif
773-
774-
#ifdef ARDUINO_NANO_RP2040_CONNECT
775-
_ota_error = rp2040_connect_onOTARequest(_ota_url.c_str());
776-
#endif
777-
778-
#ifdef BOARD_STM32H7
779-
bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false;
780-
_ota_error = portenta_h7_onOTARequest(_ota_url.c_str(), use_ethernet);
781-
#endif
782-
783-
#ifdef ARDUINO_ARCH_ESP32
784-
_ota_error = esp32_onOTARequest(_ota_url.c_str());
785-
#endif
786-
}
787-
788-
String ArduinoIoTCloudTCP::getOTAImageSHA256()
789-
{
790-
#if defined (ARDUINO_ARCH_SAMD)
791-
return samd_getOTAImageSHA256();
792-
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
793-
return rp2040_connect_getOTAImageSHA256();
794-
#elif defined (BOARD_STM32H7)
795-
return portenta_h7_getOTAImageSHA256();
796-
#elif defined (ARDUINO_ARCH_ESP32)
797-
return esp32_getOTAImageSHA256();
798-
#else
799-
# error "No method for SHA256 checksum calculation over application image defined for this architecture."
800-
#endif
801-
}
802-
#endif
803-
804735
void ArduinoIoTCloudTCP::updateThingTopics()
805736
{
806737
_shadowTopicOut = getTopic_shadowout();

src/ArduinoIoTCloudTCP.h

-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
201201
int write(String const topic, byte const data[], int const length);
202202

203203
#if OTA_ENABLED
204-
void onOTARequest();
205-
String getOTAImageSHA256();
206204
void sendDevicePropertyToCloud(String const name);
207205
#endif
208206

src/utility/ota/OTA-esp32.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ String esp32_getOTAImageSHA256()
120120
return sha256_str;
121121
}
122122

123+
bool esp32_isOTACapable()
124+
{
125+
/* NOTE: here is possible to check if current partition scheme is OTA compatible */
126+
return true;
127+
}
128+
123129
#endif /* ARDUINO_ARCH_ESP32 */

src/utility/ota/OTA-nano-rp2040.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,9 @@ String rp2040_connect_getOTAImageSHA256()
258258
return FlashSHA256::calc(XIP_BASE, 0x100000);
259259
}
260260

261+
bool rp2040_connect_isOTACapable()
262+
{
263+
return true;
264+
}
265+
261266
#endif /* ARDUINO_NANO_RP2040_CONNECT */

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern RTC_HandleTypeDef RTCHandle;
4545
* FUNCTION DEFINITION
4646
******************************************************************************/
4747

48-
int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet)
48+
int portenta_h7_onOTARequest(char const * ota_url, NetworkAdapter iface)
4949
{
5050
watchdog_reset();
5151

@@ -76,7 +76,7 @@ int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet)
7676
/* Download the OTA file from the web storage location. */
7777
MbedSocketClass * download_socket = static_cast<MbedSocketClass*>(&WiFi);
7878
#if defined (BOARD_HAS_ETHERNET)
79-
if(use_ethernet) {
79+
if(iface == NetworkAdapter::ETHERNET) {
8080
download_socket = static_cast<MbedSocketClass*>(&Ethernet);
8181
}
8282
#endif
@@ -143,4 +143,9 @@ String portenta_h7_getOTAImageSHA256()
143143
return sha256_str;
144144
}
145145

146+
bool portenta_h7_isOTACapable()
147+
{
148+
return Arduino_Portenta_OTA::isOtaCapable();
149+
}
150+
146151
#endif /* BOARD_STM32H7 */

src/utility/ota/OTA-samd.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,17 @@ String samd_getOTAImageSHA256()
8181
return FlashSHA256::calc(0x2000, 0x40000 - 0x2000);
8282
}
8383

84+
bool samd_isOTACapable()
85+
{
86+
#if OTA_STORAGE_SNU
87+
if (String(WiFi.firmwareVersion()) < String("1.4.1")) {
88+
DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
89+
return false;
90+
} else {
91+
return true;
92+
}
93+
#endif
94+
return false;
95+
}
96+
8497
#endif /* ARDUINO_ARCH_SAMD */

src/utility/ota/OTA.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include <AIoTC_Config.h>
23+
24+
#if OTA_ENABLED
25+
26+
#include "OTA.h"
27+
#include <Arduino_DebugUtils.h>
28+
29+
/******************************************************************************
30+
* FUNCTION DECLARATION
31+
******************************************************************************/
32+
33+
#ifdef ARDUINO_ARCH_SAMD
34+
int samd_onOTARequest(char const * url);
35+
String samd_getOTAImageSHA256();
36+
bool samd_isOTACapable();
37+
#endif
38+
39+
#ifdef ARDUINO_NANO_RP2040_CONNECT
40+
int rp2040_connect_onOTARequest(char const * url);
41+
String rp2040_connect_getOTAImageSHA256();
42+
bool rp2040_connect_isOTACapable();
43+
#endif
44+
45+
#ifdef BOARD_STM32H7
46+
int portenta_h7_onOTARequest(char const * url, NetworkAdapter iface);
47+
String portenta_h7_getOTAImageSHA256();
48+
void portenta_h7_setNetworkAdapter(NetworkAdapter iface);
49+
bool portenta_h7_isOTACapable();
50+
#endif
51+
52+
#ifdef ARDUINO_ARCH_ESP32
53+
int esp32_onOTARequest(char const * url);
54+
String esp32_getOTAImageSHA256();
55+
bool esp32_isOTACapable();
56+
#endif
57+
58+
/******************************************************************************
59+
* PUBLIC MEMBER FUNCTIONS
60+
******************************************************************************/
61+
62+
int OTA::onRequest(String url, NetworkAdapter iface)
63+
{
64+
DEBUG_INFO("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, url.c_str());
65+
66+
#if defined (ARDUINO_ARCH_SAMD)
67+
(void)iface;
68+
return samd_onOTARequest(url.c_str());
69+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
70+
(void)iface;
71+
return rp2040_connect_onOTARequest(url.c_str());
72+
#elif defined (BOARD_STM32H7)
73+
return portenta_h7_onOTARequest(url.c_str(), iface);
74+
#elif defined (ARDUINO_ARCH_ESP32)
75+
(void)iface;
76+
return esp32_onOTARequest(url.c_str());
77+
#else
78+
#error "OTA not supported for this architecture"
79+
#endif
80+
}
81+
82+
String OTA::getImageSHA256()
83+
{
84+
#if defined (ARDUINO_ARCH_SAMD)
85+
return samd_getOTAImageSHA256();
86+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
87+
return rp2040_connect_getOTAImageSHA256();
88+
#elif defined (BOARD_STM32H7)
89+
return portenta_h7_getOTAImageSHA256();
90+
#elif defined (ARDUINO_ARCH_ESP32)
91+
return esp32_getOTAImageSHA256();
92+
#else
93+
#error "No method for SHA256 checksum calculation over application image defined for this architecture."
94+
#endif
95+
}
96+
97+
bool OTA::isCapable()
98+
{
99+
#if defined (ARDUINO_ARCH_SAMD)
100+
return samd_isOTACapable();
101+
#elif defined (ARDUINO_NANO_RP2040_CONNECT)
102+
return rp2040_connect_isOTACapable();
103+
#elif defined (BOARD_STM32H7)
104+
return portenta_h7_isOTACapable();
105+
#elif defined (ARDUINO_ARCH_ESP32)
106+
return esp32_isOTACapable();
107+
#else
108+
#error "OTA not supported for this architecture"
109+
#endif
110+
}
111+
112+
#endif /* OTA_ENABLED */

src/utility/ota/OTA.h

+15-20
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
******************************************************************************/
2424

2525
#include <AIoTC_Config.h>
26+
27+
#if OTA_ENABLED
2628
#include <Arduino.h>
29+
#include <Arduino_ConnectionHandler.h>
2730

2831
/******************************************************************************
2932
* DEFINES
@@ -52,27 +55,19 @@ enum class OTAError : int
5255
};
5356

5457
/******************************************************************************
55-
* FUNCTION DEFINITION
58+
* CLASS DECLARATION
5659
******************************************************************************/
5760

58-
#ifdef ARDUINO_ARCH_SAMD
59-
int samd_onOTARequest(char const * ota_url);
60-
String samd_getOTAImageSHA256();
61-
#endif
62-
63-
#ifdef ARDUINO_NANO_RP2040_CONNECT
64-
int rp2040_connect_onOTARequest(char const * ota_url);
65-
String rp2040_connect_getOTAImageSHA256();
66-
#endif
67-
68-
#ifdef BOARD_STM32H7
69-
int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet);
70-
String portenta_h7_getOTAImageSHA256();
71-
#endif
72-
73-
#ifdef ARDUINO_ARCH_ESP32
74-
int esp32_onOTARequest(char const * ota_url);
75-
String esp32_getOTAImageSHA256();
76-
#endif
61+
class OTA
62+
{
63+
public:
64+
65+
static int onRequest(String url, NetworkAdapter iface);
66+
static String getImageSHA256();
67+
static bool isCapable();
68+
69+
};
70+
71+
#endif /* OTA_ENABLED */
7772

7873
#endif /* ARDUINO_OTA_LOGIC_H_ */

0 commit comments

Comments
 (0)