Skip to content

Commit 3d05dd1

Browse files
committed
Add OTA support for ESP32 boards
1 parent 5776c7f commit 3d05dd1

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

src/AIoTC_Config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@
108108
#define OTA_STORAGE_PORTENTA_QSPI (0)
109109
#endif
110110

111-
#if (OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU || OTA_STORAGE_PORTENTA_QSPI) && !defined(ARDUINO_AVR_UNO_WIFI_REV2)
111+
#if defined(ARDUINO_ARCH_ESP32)
112+
#define OTA_STORAGE_ESP (1)
113+
#endif
114+
115+
#if (OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU || OTA_STORAGE_PORTENTA_QSPI || OTA_STORAGE_ESP) && !defined(ARDUINO_AVR_UNO_WIFI_REV2)
112116
#define OTA_ENABLED (1)
113117
#else
114118
#define OTA_ENABLED (0)

src/ArduinoIoTCloudTCP.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,27 @@
4444
# include <WiFi.h>
4545
#endif
4646

47-
#include "utility/ota/OTA.h"
47+
#if defined (ARDUINO_ARCH_ESP32) && OTA_ENABLED
48+
# include "tls/utility/SHA256.h"
49+
#include "esp_spi_flash.h"
50+
#include "esp_ota_ops.h"
51+
#include "esp_image_format.h"
52+
#endif
53+
54+
#if defined (ARDUINO_NANO_RP2040_CONNECT) || \
55+
(defined (ARDUINO_ARCH_SAMD) && OTA_ENABLED)
4856
#include "utility/ota/FlashSHA256.h"
57+
#endif
58+
59+
#if OTA_ENABLED
60+
#include "utility/ota/OTA.h"
61+
#endif
62+
4963
#include <algorithm>
5064
#include "cbor/CBOREncoder.h"
5165

5266
#include "utility/watchdog/Watchdog.h"
5367

54-
5568
/******************************************************************************
5669
* EXTERN
5770
******************************************************************************/
@@ -201,6 +214,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
201214
* 1024 bytes or 0x100'000 bytes).
202215
*/
203216
String const sha256_str = FlashSHA256::calc(XIP_BASE, 0x100000);
217+
#elif defined(ARDUINO_ARCH_ESP32)
218+
# warning "Compute image SHA256"
219+
String const sha256_str;
204220
#else
205221
# error "No method for SHA256 checksum calculation over application image defined for this architecture."
206222
#endif
@@ -300,6 +316,11 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
300316
_ota_cap = true;
301317
#endif
302318

319+
#if defined(ARDUINO_ARCH_ESP32) && OTA_ENABLED
320+
/* NOTE: here is possible to check if current partition scheme is OTA compatible */
321+
_ota_cap = true;
322+
#endif
323+
303324
#ifdef BOARD_HAS_OFFLOADED_ECCX08
304325
if (String(WiFi.firmwareVersion()) < String("1.4.4")) {
305326
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.4.4, current %s", __FUNCTION__, WiFi.firmwareVersion());
@@ -840,6 +861,10 @@ void ArduinoIoTCloudTCP::onOTARequest()
840861
bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false;
841862
_ota_error = portenta_h7_onOTARequest(_ota_url.c_str(), use_ethernet);
842863
#endif
864+
865+
#ifdef ARDUINO_ARCH_ESP32
866+
_ota_error = esp32_onOTARequest(_ota_url.c_str());
867+
#endif
843868
}
844869
#endif
845870

src/utility/ota/OTA-esp32.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 license@arduino.cc.
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include <AIoTC_Config.h>
23+
24+
#if defined ARDUINO_ARCH_ESP32 && OTA_ENABLED
25+
#include "OTA.h"
26+
#include <Arduino_DebugUtils.h>
27+
#include <Arduino_ESP_OTA.h>
28+
29+
/******************************************************************************
30+
* FUNCTION DEFINITION
31+
******************************************************************************/
32+
33+
int esp32_onOTARequest(char const * ota_url)
34+
{
35+
Arduino_ESP32_OTA::Error ota_err = Arduino_ESP32_OTA::Error::None;
36+
Arduino_ESP32_OTA ota;
37+
38+
/* Initialize the board for OTA handling. */
39+
if ((ota_err = ota.begin()) != Arduino_ESP32_OTA::Error::None)
40+
{
41+
DEBUG_ERROR("Arduino_ESP32_OTA::begin() failed with %d", static_cast<int>(ota_err));
42+
return static_cast<int>(ota_err);
43+
}
44+
45+
/* Download the OTA file from the web storage location. */
46+
int const ota_download = ota.download(ota_url);
47+
if (ota_download <= 0)
48+
{
49+
DEBUG_ERROR("Arduino_ESP_OTA::download() failed with %d", ota_download);
50+
return ota_download;
51+
}
52+
DEBUG_VERBOSE("Arduino_ESP_OTA::download() %d bytes downloaded", static_cast<int>(ota_download));
53+
54+
/* Verify update integrity and apply */
55+
if ((ota_err = ota.update()) != Arduino_ESP32_OTA::Error::None)
56+
{
57+
DEBUG_ERROR("Arduino_ESP_OTA::update() failed with %d", static_cast<int>(ota_err));
58+
return static_cast<int>(ota_err);
59+
}
60+
61+
/* Perform the reset to reboot */
62+
ota.reset();
63+
64+
return static_cast<int>(OTAError::None);
65+
}
66+
67+
#endif /* ARDUINO_ARCH_ESP32 */

src/utility/ota/OTA.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ int rp2040_connect_onOTARequest(char const * ota_url);
6666
int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet);
6767
#endif
6868

69+
#ifdef ARDUINO_ARCH_ESP32
70+
int esp32_onOTARequest(char const * ota_url);
71+
#endif
72+
6973
#endif /* ARDUINO_OTA_LOGIC_H_ */

0 commit comments

Comments
 (0)