Skip to content

Commit 39ca673

Browse files
implemented non blocking download in ota interface
1 parent a210a70 commit 39ca673

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

UNOR4USBBridge/OTA.cpp

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@
2222
#include <SPIFFS.h>
2323
#include <Arduino_ESP32_OTA.h>
2424
#include "OTA.h"
25+
#include "BossaUnoR4WiFi.h"
26+
27+
#include "FS.h"
28+
#include "SPIFFS.h"
29+
#include "at_handler.h"
2530

2631
/******************************************************************************
2732
PUBLIC MEMBER FUNCTIONS
2833
******************************************************************************/
2934

30-
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic)
31-
{
32-
/* initialize private variables */
33-
otaInit();
35+
Arduino_UNOWIFIR4_OTA::Arduino_UNOWIFIR4_OTA()
36+
: _updating_renesas(true) {
3437

35-
/* ... initialize CRC ... */
36-
crc32Init();
38+
}
3739

40+
Arduino_UNOWIFIR4_OTA::~Arduino_UNOWIFIR4_OTA() {
41+
closeStorage();
42+
}
43+
44+
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic)
45+
{
3846
/* ... configure board Magic number */
3947
setMagic(magic);
4048

@@ -47,23 +55,22 @@ Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uin
4755
SPIFFS.remove(file_path);
4856
}
4957

50-
_spiffs = true;
58+
_updating_renesas = true;
5159

5260
SPIFFS.end();
5361
return Error::None;
5462
}
5563

5664
void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data)
5765
{
58-
if(_spiffs) {
66+
if(_updating_renesas) {
5967
int ret = fwrite(&data, sizeof(data), 1, _file);
6068
} else {
6169
Arduino_ESP32_OTA::write_byte_to_flash(data);
6270
}
6371
}
6472

65-
int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
66-
{
73+
int Arduino_UNOWIFIR4_OTA::initStorage(const char* file_path) {
6774
if(!SPIFFS.begin()) {
6875
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
6976
return static_cast<int>(Error::OtaStorageInit);
@@ -76,33 +83,66 @@ int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
7683
DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__);
7784
return static_cast<int>(Error::OtaStorageInit);
7885
}
86+
return static_cast<int>(Error::None);
87+
}
7988

80-
/* Download and decode OTA file */
81-
size_t size = download(ota_url);
89+
int Arduino_UNOWIFIR4_OTA::closeStorage() {
90+
int res = 0;
91+
if(_file != nullptr) {
92+
res = fclose(_file);
93+
_file = nullptr;
94+
}
8295

83-
fclose(_file);
84-
_file = nullptr;
8596
SPIFFS.end();
86-
return size;
97+
return res;
8798
}
8899

89-
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify()
100+
int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
90101
{
91-
/* ... then finalize ... */
92-
crc32Finalize();
102+
int res = initStorage(file_path);
93103

94-
if(!crc32Verify()) {
95-
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
96-
return Error::OtaHeaderCrc;
104+
if(res < 0) {
105+
return res;
97106
}
98-
return Error::None;
107+
108+
/* Download and decode OTA file */
109+
res = download(ota_url);
110+
111+
closeStorage();
112+
113+
return res;
99114
}
100115

101-
Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update()
116+
int Arduino_UNOWIFIR4_OTA::startDownload(const char * ota_url, const char* file_path)
102117
{
103-
if (!Update.end(true)) {
104-
DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString());
105-
return Error::OtaStorageEnd;
118+
int res = initStorage(file_path);
119+
120+
if(res < 0) {
121+
return res;
106122
}
107-
return Error::None;
123+
124+
/* Download and decode OTA file */
125+
res = startDownload(ota_url);
126+
127+
if(res < 0) {
128+
closeStorage();
129+
}
130+
131+
return res;
132+
}
133+
134+
int Arduino_UNOWIFIR4_OTA::downloadPoll()
135+
{
136+
auto res = Arduino_ESP32_OTA::downloadPoll();
137+
138+
if(_updating_renesas && res != 0) {
139+
closeStorage();
140+
}
141+
142+
return res;
143+
}
144+
145+
int Arduino_UNOWIFIR4_OTA::update(const char* file_path)
146+
{
147+
return BOSSA.program(file_path, Serial, GPIO_BOOT, GPIO_RST);
108148
}

UNOR4USBBridge/OTA.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,41 @@
3535

3636
class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA
3737
{
38-
3938
public:
4039

40+
Arduino_UNOWIFIR4_OTA();
41+
~Arduino_UNOWIFIR4_OTA();
42+
4143
enum class UNO_WiFi_R4_Error : int
4244
{
4345
StorageConfig = -20,
4446
};
4547

4648
using Arduino_ESP32_OTA::begin;
4749
Arduino_ESP32_OTA::Error begin(const char* file_path, uint32_t magic = ARDUINO_RA4M1_OTA_MAGIC);
50+
4851
using Arduino_ESP32_OTA::download;
4952
int download(const char * ota_url, const char* file_path);
53+
54+
using Arduino_ESP32_OTA::startDownload;
55+
int startDownload(const char * ota_url, const char* file_path);
56+
57+
int downloadPoll() override;
58+
using Arduino_ESP32_OTA::downloadProgress;
59+
5060
void write_byte_to_flash(uint8_t data);
51-
Arduino_ESP32_OTA::Error verify();
52-
Arduino_ESP32_OTA::Error update();
5361

54-
private:
62+
using Arduino_ESP32_OTA::verify;
63+
64+
using Arduino_ESP32_OTA::update;
65+
int update(const char* file_path);
5566

67+
int initStorage(const char* file_path);
68+
int closeStorage();
69+
70+
private:
5671
FILE* _file;
57-
bool _spiffs;
72+
bool _updating_renesas;
5873
};
5974

6075
#endif /* ARDUINO_UNOWIFIR4_OTA_H_ */

0 commit comments

Comments
 (0)