Skip to content

Commit 7c217e5

Browse files
committed
Move OTA functions in newly added OTA.cpp file
1 parent 86520bf commit 7c217e5

File tree

5 files changed

+142
-65
lines changed

5 files changed

+142
-65
lines changed

src/ArduinoIoTCloudTCP.cpp

+6-41
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

@@ -239,6 +239,10 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
239239
_ota_cap = true;
240240
#endif
241241

242+
#if OTA_ENABLED
243+
OTA::setNetworkAdapter(_connection->getInterface());
244+
#endif
245+
242246
#ifdef BOARD_HAS_OFFLOADED_ECCX08
243247
if (String(WiFi.firmwareVersion()) < String("1.4.4")) {
244248
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());
@@ -609,7 +613,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
609613
/* Transmit the cleared request flags to the cloud. */
610614
sendDevicePropertyToCloud("OTA_REQ");
611615
/* Call member function to handle OTA request. */
612-
onOTARequest();
616+
_ota_error = OTA::onRequest(_ota_url);
613617
/* If something fails send the OTA error to the cloud */
614618
sendDevicePropertyToCloud("OTA_ERROR");
615619
}
@@ -762,45 +766,6 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
762766
return 0;
763767
}
764768

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-
804769
void ArduinoIoTCloudTCP::updateThingTopics()
805770
{
806771
_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-portenta-h7.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535

3636
#include "../watchdog/Watchdog.h"
3737

38+
/******************************************************************************
39+
* LOCAL VARIABLES
40+
******************************************************************************/
41+
42+
static NetworkAdapter _ota_adapter = NetworkAdapter::WIFI;
43+
3844
/******************************************************************************
3945
* EXTERN
4046
******************************************************************************/
@@ -45,7 +51,7 @@ extern RTC_HandleTypeDef RTCHandle;
4551
* FUNCTION DEFINITION
4652
******************************************************************************/
4753

48-
int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet)
54+
int portenta_h7_onOTARequest(char const * ota_url)
4955
{
5056
watchdog_reset();
5157

@@ -76,7 +82,7 @@ int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet)
7682
/* Download the OTA file from the web storage location. */
7783
MbedSocketClass * download_socket = static_cast<MbedSocketClass*>(&WiFi);
7884
#if defined (BOARD_HAS_ETHERNET)
79-
if(use_ethernet) {
85+
if(_ota_adapter == NetworkAdapter::ETHERNET) {
8086
download_socket = static_cast<MbedSocketClass*>(&Ethernet);
8187
}
8288
#endif
@@ -143,4 +149,9 @@ String portenta_h7_getOTAImageSHA256()
143149
return sha256_str;
144150
}
145151

152+
void portenta_h7_setNetworkAdapter(NetworkAdapter iface)
153+
{
154+
_ota_adapter = iface;
155+
}
156+
146157
#endif /* BOARD_STM32H7 */

src/utility/ota/OTA.cpp

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

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);
66+
static String getImageSHA256();
67+
static void setNetworkAdapter(NetworkAdapter iface);
68+
69+
};
70+
71+
#endif /* OTA_ENABLED */
7772

7873
#endif /* ARDUINO_OTA_LOGIC_H_ */

0 commit comments

Comments
 (0)