Skip to content

Commit 3f8a7e7

Browse files
committed
OTA code import and refactoring
1 parent ec62308 commit 3f8a7e7

File tree

4 files changed

+172
-4
lines changed

4 files changed

+172
-4
lines changed

UNOR4USBBridge/OTA.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
This file is part of UNOR4USBBridge_OTA.
3+
4+
Copyright 2023 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+
#include <Update.h>
22+
#include <SPIFFS.h>
23+
#include <Arduino_ESP32_OTA.h>
24+
#include "OTA.h"
25+
26+
/******************************************************************************
27+
PUBLIC MEMBER FUNCTIONS
28+
******************************************************************************/
29+
30+
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic)
31+
{
32+
/* initialize private variables */
33+
otaInit();
34+
35+
/* ... initialize CRC ... */
36+
crc32Init();
37+
38+
/* ... configure board Magic number */
39+
setMagic(magic);
40+
41+
if(!SPIFFS.begin()) {
42+
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
43+
return Error::OtaStorageInit;
44+
}
45+
46+
if(SPIFFS.exists(file_path)) {
47+
SPIFFS.remove(file_path);
48+
}
49+
50+
_spiffs = true;
51+
52+
SPIFFS.end();
53+
return Error::None;
54+
}
55+
56+
void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data)
57+
{
58+
if(_spiffs) {
59+
int ret = fwrite(&data, sizeof(data), 1, _file);
60+
} else {
61+
Arduino_ESP32_OTA::write_byte_to_flash(data);
62+
}
63+
}
64+
65+
int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
66+
{
67+
if(!SPIFFS.begin()) {
68+
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
69+
return static_cast<int>(Error::OtaStorageInit);
70+
}
71+
72+
String spiffs_path = String("/spiffs") + String(file_path);
73+
_file = fopen(spiffs_path.c_str(), "wb");
74+
75+
if(!_file) {
76+
DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__);
77+
return static_cast<int>(Error::OtaStorageInit);
78+
}
79+
80+
/* Download and decode OTA file */
81+
size_t size = download(ota_url);
82+
83+
fclose(_file);
84+
_file = nullptr;
85+
SPIFFS.end();
86+
return size;
87+
}
88+
89+
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify()
90+
{
91+
/* ... then finalize ... */
92+
crc32Finalize();
93+
94+
if(!crc32Verify()) {
95+
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
96+
return Error::OtaHeaderCrc;
97+
}
98+
return Error::None;
99+
}
100+
101+
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update()
102+
{
103+
if (!Update.end(true)) {
104+
DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString());
105+
return Error::OtaStorageEnd;
106+
}
107+
return Error::None;
108+
}

UNOR4USBBridge/OTA.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
This file is part of UNOR4USBBridge_OTA.
3+
4+
Copyright 2023 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+
#ifndef ARDUINO_UNOWIFIR4_OTA_H_
19+
#define ARDUINO_UNOWIFIR4_OTA_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#include <Arduino_ESP32_OTA.h>
26+
27+
/******************************************************************************
28+
* DEFINES
29+
******************************************************************************/
30+
#define ARDUINO_RA4M1_OTA_MAGIC 0x23411002
31+
32+
/******************************************************************************
33+
* CLASS DECLARATION
34+
******************************************************************************/
35+
36+
class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA
37+
{
38+
39+
public:
40+
41+
enum class UNO_WiFi_R4_Error : int
42+
{
43+
StorageConfig = -1,
44+
};
45+
46+
using Arduino_ESP32_OTA::begin;
47+
Arduino_ESP32_OTA::Error begin(const char* file_path, uint32_t magic = ARDUINO_RA4M1_OTA_MAGIC);
48+
using Arduino_ESP32_OTA::download;
49+
int download(const char * ota_url, const char* file_path);
50+
void write_byte_to_flash(uint8_t data);
51+
Arduino_ESP32_OTA::Error verify();
52+
Arduino_ESP32_OTA::Error update();
53+
54+
private:
55+
56+
FILE* _file;
57+
bool _spiffs;
58+
};
59+
60+
#endif /* ARDUINO_UNOWIFIR4_OTA_H_ */

UNOR4USBBridge/cmds_ota.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define CMDS_OTA_H
33

44
#include "at_handler.h"
5-
#include "Arduino_ESP32_OTA.h"
5+
#include "OTA.h"
66
#include <BossaArduino.h>
77

8-
Arduino_ESP32_OTA OTA;
8+
Arduino_UNOWIFIR4_OTA OTA;
99

1010
void CAtHandler::add_cmds_ota() {
1111
/* ....................................................................... */

compile.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ if [ ! -d hardware ]; then
1313

1414
cd hardware/esp32-patched/esp32/libraries
1515
git clone https://github.com/arduino-libraries/ArduinoBLE.git
16-
git clone https://github.com/pennam/Arduino_ESP32_OTA.git
16+
git clone https://github.com/arduino-libraries/Arduino_ESP32_OTA.git
1717
cd Arduino_ESP32_OTA/
18-
git checkout e27f822406986354197a09516edaaeea3ed18e79
18+
git checkout 68318d92d12f837e9184e33860bedb51a57d88b2
1919
cd ..
2020
git clone https://github.com/pennam/BOSSA.git
2121
cd BOSSA

0 commit comments

Comments
 (0)