Skip to content

Commit 02a4039

Browse files
Merge pull request #53 from andreagilardoni/non-blocking-download
[OTA] Non blocking download
2 parents aef6457 + 8f6038c commit 02a4039

11 files changed

+392
-228
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
url = https://github.com/arduino/arduino-esp32.git
2020
branch = unor4wifi-2.0.9
2121
shallow = true
22+
[submodule "libraries/ArduinoHttpClient"]
23+
path = libraries/ArduinoHttpClient
24+
url = https://github.com/arduino-libraries/ArduinoHttpClient.git

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_ */

UNOR4USBBridge/UNOR4USBBridge.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void ets_putc_handler(char c)
6161

6262
/* -------------------------------------------------------------------------- */
6363
void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
64-
/* -------------------------------------------------------------------------- */
65-
switch (event) {
64+
/* -------------------------------------------------------------------------- */
65+
switch (event) {
6666
case ARDUINO_EVENT_WIFI_READY:
6767
wifi_status = WIFI_ST_IDLE_STATUS;
6868
break;
@@ -81,7 +81,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
8181
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
8282
wifi_status = WIFI_ST_IDLE_STATUS;
8383
break;
84-
case ARDUINO_EVENT_WIFI_AP_START:
84+
case ARDUINO_EVENT_WIFI_AP_START:
8585
wifi_status = WIFI_ST_AP_LISTENING;
8686
break;
8787
case ARDUINO_EVENT_WIFI_AP_STOP:
@@ -91,7 +91,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
9191
wifi_status = WIFI_ST_AP_CONNECTED;
9292
break;
9393
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
94-
94+
9595

9696
break;
9797
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
@@ -120,7 +120,7 @@ void atLoop(void* param) {
120120

121121
/* -------------------------------------------------------------------------- */
122122
void setup() {
123-
/* -------------------------------------------------------------------------- */
123+
/* -------------------------------------------------------------------------- */
124124

125125
/* redirect stdout */
126126
stdout = funopen(NULL, NULL, &write_fn, NULL, NULL);
@@ -184,7 +184,7 @@ static uint8_t buf[2048];
184184

185185
/* -------------------------------------------------------------------------- */
186186
void loop() {
187-
/* -------------------------------------------------------------------------- */
187+
/* -------------------------------------------------------------------------- */
188188

189189
if (SERIAL_USER.baudRate() != _baud) {
190190
_baud = SERIAL_USER.baudRate();

UNOR4USBBridge/at_handler.cpp

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,76 @@ uint8_t CAtHandler::wifi_status = WIFI_ST_IDLE_STATUS;
2222
/* -------------------------------------------------------------------------- */
2323
CClientWrapper CAtHandler::getClient(int sock) {
2424
/* -------------------------------------------------------------------------- */
25-
CClientWrapper rv;
26-
27-
bool is_server = false;
28-
bool is_sslclienet = false;
29-
30-
int internal_sock = -1;
31-
32-
if(sock >= START_SSL_CLIENT_SOCK) {
33-
internal_sock = sock - START_SSL_CLIENT_SOCK;
34-
is_sslclienet = true;
35-
} else
36-
if(sock >= START_CLIENT_SERVER_SOCK) {
37-
internal_sock = sock - START_CLIENT_SERVER_SOCK;
38-
is_server = true;
39-
}
40-
else {
41-
internal_sock = sock;
42-
}
43-
44-
if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) {
45-
rv.client = nullptr;
46-
rv.sslclient = nullptr;
47-
rv.can_delete = -1;
48-
return rv;
49-
}
25+
CClientWrapper rv;
26+
27+
bool is_server = false;
28+
bool is_sslclienet = false;
29+
30+
int internal_sock = -1;
5031

51-
if (is_sslclienet) {
32+
if(sock >= START_SSL_CLIENT_SOCK) {
33+
internal_sock = sock - START_SSL_CLIENT_SOCK;
34+
is_sslclienet = true;
35+
} else
36+
if(sock >= START_CLIENT_SERVER_SOCK) {
37+
internal_sock = sock - START_CLIENT_SERVER_SOCK;
38+
is_server = true;
39+
}
40+
else {
41+
internal_sock = sock;
42+
}
43+
44+
if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) {
45+
rv.client = nullptr;
46+
rv.sslclient = nullptr;
47+
rv.can_delete = -1;
48+
return rv;
49+
}
50+
51+
if (is_sslclienet) {
5252
rv.sslclient = sslclients[internal_sock];
5353
rv.can_delete = internal_sock;
54-
}
55-
else if(is_server) {
54+
}
55+
else if(is_server) {
5656
rv.client = &serverClients[internal_sock].client;
5757
rv.can_delete = -1;
58-
}
59-
else {
58+
}
59+
else {
6060
rv.client = clients[internal_sock];
6161
rv.can_delete = internal_sock;
62-
}
63-
return rv;
62+
}
63+
return rv;
6464
}
6565

6666

6767

6868
/* -------------------------------------------------------------------------- */
6969
void CAtHandler::run() {
70-
/* -------------------------------------------------------------------------- */
71-
at_srv.run();
72-
vTaskDelay(1);
70+
/* -------------------------------------------------------------------------- */
71+
at_srv.run();
72+
73+
// execute all the pending tasks
74+
75+
// it is intended for the tasks to add other tasks in queue after being executed,
76+
// saving the current size of the queue
77+
auto size = tasks.size();
78+
for(int i=0; i<size; i++) {
79+
auto task = tasks.front();
80+
tasks.pop();
81+
if(task) {
82+
task();
83+
}
84+
}
85+
86+
vTaskDelay(1);
7387
}
7488

7589

7690

7791
/* -------------------------------------------------------------------------- */
7892
CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) {
79-
/* -------------------------------------------------------------------------- */
80-
93+
/* -------------------------------------------------------------------------- */
94+
8195
for(int i = 0; i < MAX_CLIENT_AVAILABLE; i++) {
8296
clients[i] = nullptr;
8397
}
@@ -119,16 +133,16 @@ CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) {
119133

120134
if (it == command_table.end()) {
121135
return chAT::CommandStatus::ERROR;
122-
}
136+
}
123137
else {
124138
return it->second(srv, srv.parser());
125139
}
126140
});
127141

128-
/* SET UP COMMAND TABLE */
142+
/* SET UP COMMAND TABLE */
129143
add_cmds_esp_generic();
130144
add_cmds_wifi_station();
131-
add_cmds_wifi_softAP();
145+
add_cmds_wifi_softAP();
132146
add_cmds_wifi_SSL();
133147
add_cmds_wifi_netif();
134148
add_cmds_wifi_udp();

0 commit comments

Comments
 (0)