Skip to content

Commit ee687fc

Browse files
adding OTADOWNLOADSTART OTADOWNLOADPROGRESS AT commands to support non blocking download
1 parent 39ca673 commit ee687fc

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

UNOR4USBBridge/cmds_ota.h

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include "at_handler.h"
55
#include "OTA.h"
6-
#include "BossaUnoR4WiFi.h"
76

87
Arduino_UNOWIFIR4_OTA OTA;
8+
void otaDownloadProgress(CAtHandler* at_handler);
99

1010
void CAtHandler::add_cmds_ota() {
1111
/* ....................................................................... */
@@ -123,6 +123,75 @@ void CAtHandler::add_cmds_ota() {
123123
}
124124
};
125125

126+
/* ....................................................................... */
127+
command_table[_OTA_DOWNLOAD_START] = [this](auto & srv, auto & parser) {
128+
/* ....................................................................... */
129+
130+
switch (parser.cmd_mode) {
131+
case chAT::CommandMode::Write: {
132+
if (parser.args.size() == 1) {
133+
auto &url = parser.args[0];
134+
if (url.empty()) {
135+
return chAT::CommandStatus::ERROR;
136+
}
137+
138+
int ota_error = OTA.startDownload(url.c_str());
139+
String error = String((int)ota_error) + _ENDL;
140+
srv.write_response_prompt();
141+
srv.write_str((const char *)(error.c_str()));
142+
srv.write_line_end();
143+
144+
this->addTask(std::bind(&otaDownloadProgress, this));
145+
146+
return chAT::CommandStatus::OK;
147+
} else if(parser.args.size() == 2) {
148+
auto &url = parser.args[0];
149+
if (url.empty()) {
150+
return chAT::CommandStatus::ERROR;
151+
}
152+
153+
auto &path = parser.args[1];
154+
if (path.empty()) {
155+
return chAT::CommandStatus::ERROR;
156+
}
157+
158+
int ota_error = OTA.startDownload(url.c_str(), path.c_str());
159+
160+
this->addTask(std::bind(&otaDownloadProgress, this));
161+
162+
String error = String((int)ota_error) + _ENDL;
163+
srv.write_response_prompt();
164+
srv.write_str((const char *)(error.c_str()));
165+
srv.write_line_end();
166+
return chAT::CommandStatus::OK;
167+
} else {
168+
return chAT::CommandStatus::ERROR;
169+
}
170+
171+
}
172+
default:
173+
return chAT::CommandStatus::ERROR;
174+
}
175+
};
176+
177+
/* ....................................................................... */
178+
command_table[_OTA_DOWNLOAD_PROGRESS] = [this](auto & srv, auto & parser) {
179+
/* ....................................................................... */
180+
switch (parser.cmd_mode) {
181+
case chAT::CommandMode::Run: {
182+
int progress = OTA.downloadProgress();
183+
String pogress_str = String((int)progress) + _ENDL;
184+
srv.write_response_prompt();
185+
srv.write_str((const char *)(pogress_str.c_str()));
186+
srv.write_line_end();
187+
188+
return chAT::CommandStatus::OK;
189+
}
190+
default:
191+
return chAT::CommandStatus::ERROR;
192+
}
193+
};
194+
126195
/* ....................................................................... */
127196
command_table[_OTA_VERIFY] = [this](auto & srv, auto & parser) {
128197
/* ....................................................................... */
@@ -162,8 +231,8 @@ void CAtHandler::add_cmds_ota() {
162231
return chAT::CommandStatus::ERROR;
163232
}
164233

165-
int flash_error = BOSSA.program(path.c_str(), Serial, GPIO_BOOT, GPIO_RST);
166-
String error = String(flash_error) + "\r\n";
234+
int flash_error = OTA.update(path.c_str());
235+
String error = String(flash_error) + _ENDL;
167236
srv.write_response_prompt();
168237
srv.write_str((const char *)(error.c_str()));
169238
srv.write_line_end();
@@ -191,4 +260,15 @@ void CAtHandler::add_cmds_ota() {
191260
};
192261
}
193262

263+
// in order to make the download progress downloadPoll must be called periodically
264+
// until the end, this function readds itself to the task list until completed
265+
void otaDownloadProgress(CAtHandler* at_handler) {
266+
auto res = OTA.downloadPoll();
267+
268+
// continue to progress the download if the state is not completed or not an error
269+
if(res == 0) {
270+
at_handler->addTask(std::bind(&otaDownloadProgress, at_handler));
271+
}
272+
};
273+
194274
#endif

UNOR4USBBridge/commands.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ enum file_op {
119119
#define _OTA_SETCAROOT "+OTASETCAROOT"
120120
#define _OTA_BEGIN "+OTABEGIN"
121121
#define _OTA_DOWNLOAD "+OTADOWNLOAD"
122+
#define _OTA_DOWNLOAD_START "+OTADOWNLOADSTART"
123+
#define _OTA_DOWNLOAD_PROGRESS "+OTADOWNLOADPROGRESS"
122124
#define _OTA_VERIFY "+OTAVERIFY"
123125
#define _OTA_UPDATE "+OTAUPDATE"
124126
#define _OTA_RESET "+OTARESET"

0 commit comments

Comments
 (0)