Skip to content

[OTA] Non blocking download #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
url = https://github.com/arduino/arduino-esp32.git
branch = unor4wifi-2.0.9
shallow = true
[submodule "libraries/ArduinoHttpClient"]
path = libraries/ArduinoHttpClient
url = https://github.com/arduino-libraries/ArduinoHttpClient.git
94 changes: 67 additions & 27 deletions UNOR4USBBridge/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@
#include <SPIFFS.h>
#include <Arduino_ESP32_OTA.h>
#include "OTA.h"
#include "BossaUnoR4WiFi.h"

#include "FS.h"
#include "SPIFFS.h"
#include "at_handler.h"

/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic)
{
/* initialize private variables */
otaInit();
Arduino_UNOWIFIR4_OTA::Arduino_UNOWIFIR4_OTA()
: _updating_renesas(true) {

/* ... initialize CRC ... */
crc32Init();
}

Arduino_UNOWIFIR4_OTA::~Arduino_UNOWIFIR4_OTA() {
closeStorage();
}

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic)
{
/* ... configure board Magic number */
setMagic(magic);

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

_spiffs = true;
_updating_renesas = true;

SPIFFS.end();
return Error::None;
}

void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data)
{
if(_spiffs) {
if(_updating_renesas) {
int ret = fwrite(&data, sizeof(data), 1, _file);
} else {
Arduino_ESP32_OTA::write_byte_to_flash(data);
}
}

int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
{
int Arduino_UNOWIFIR4_OTA::initStorage(const char* file_path) {
if(!SPIFFS.begin()) {
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
return static_cast<int>(Error::OtaStorageInit);
Expand All @@ -76,33 +83,66 @@ int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__);
return static_cast<int>(Error::OtaStorageInit);
}
return static_cast<int>(Error::None);
}

/* Download and decode OTA file */
size_t size = download(ota_url);
int Arduino_UNOWIFIR4_OTA::closeStorage() {
int res = 0;
if(_file != nullptr) {
res = fclose(_file);
_file = nullptr;
}

fclose(_file);
_file = nullptr;
SPIFFS.end();
return size;
return res;
}

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify()
int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path)
{
/* ... then finalize ... */
crc32Finalize();
int res = initStorage(file_path);

if(!crc32Verify()) {
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
return Error::OtaHeaderCrc;
if(res < 0) {
return res;
}
return Error::None;

/* Download and decode OTA file */
res = download(ota_url);

closeStorage();

return res;
}

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update()
int Arduino_UNOWIFIR4_OTA::startDownload(const char * ota_url, const char* file_path)
{
if (!Update.end(true)) {
DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString());
return Error::OtaStorageEnd;
int res = initStorage(file_path);

if(res < 0) {
return res;
}
return Error::None;

/* Download and decode OTA file */
res = startDownload(ota_url);

if(res < 0) {
closeStorage();
}

return res;
}

int Arduino_UNOWIFIR4_OTA::downloadPoll()
{
auto res = Arduino_ESP32_OTA::downloadPoll();

if(_updating_renesas && res != 0) {
closeStorage();
}

return res;
}

int Arduino_UNOWIFIR4_OTA::update(const char* file_path)
{
return BOSSA.program(file_path, Serial, GPIO_BOOT, GPIO_RST);
}
25 changes: 20 additions & 5 deletions UNOR4USBBridge/OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,41 @@

class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA
{

public:

Arduino_UNOWIFIR4_OTA();
~Arduino_UNOWIFIR4_OTA();

enum class UNO_WiFi_R4_Error : int
{
StorageConfig = -20,
};

using Arduino_ESP32_OTA::begin;
Arduino_ESP32_OTA::Error begin(const char* file_path, uint32_t magic = ARDUINO_RA4M1_OTA_MAGIC);

using Arduino_ESP32_OTA::download;
int download(const char * ota_url, const char* file_path);

using Arduino_ESP32_OTA::startDownload;
int startDownload(const char * ota_url, const char* file_path);

int downloadPoll() override;
using Arduino_ESP32_OTA::downloadProgress;

void write_byte_to_flash(uint8_t data);
Arduino_ESP32_OTA::Error verify();
Arduino_ESP32_OTA::Error update();

private:
using Arduino_ESP32_OTA::verify;

using Arduino_ESP32_OTA::update;
int update(const char* file_path);

int initStorage(const char* file_path);
int closeStorage();

private:
FILE* _file;
bool _spiffs;
bool _updating_renesas;
};

#endif /* ARDUINO_UNOWIFIR4_OTA_H_ */
12 changes: 6 additions & 6 deletions UNOR4USBBridge/UNOR4USBBridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void ets_putc_handler(char c)

/* -------------------------------------------------------------------------- */
void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
/* -------------------------------------------------------------------------- */
switch (event) {
/* -------------------------------------------------------------------------- */
switch (event) {
case ARDUINO_EVENT_WIFI_READY:
wifi_status = WIFI_ST_IDLE_STATUS;
break;
Expand All @@ -81,7 +81,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
wifi_status = WIFI_ST_IDLE_STATUS;
break;
case ARDUINO_EVENT_WIFI_AP_START:
case ARDUINO_EVENT_WIFI_AP_START:
wifi_status = WIFI_ST_AP_LISTENING;
break;
case ARDUINO_EVENT_WIFI_AP_STOP:
Expand All @@ -91,7 +91,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) {
wifi_status = WIFI_ST_AP_CONNECTED;
break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:


break;
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
Expand Down Expand Up @@ -120,7 +120,7 @@ void atLoop(void* param) {

/* -------------------------------------------------------------------------- */
void setup() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

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

/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

if (SERIAL_USER.baudRate() != _baud) {
_baud = SERIAL_USER.baudRate();
Expand Down
94 changes: 54 additions & 40 deletions UNOR4USBBridge/at_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,76 @@ uint8_t CAtHandler::wifi_status = WIFI_ST_IDLE_STATUS;
/* -------------------------------------------------------------------------- */
CClientWrapper CAtHandler::getClient(int sock) {
/* -------------------------------------------------------------------------- */
CClientWrapper rv;

bool is_server = false;
bool is_sslclienet = false;

int internal_sock = -1;

if(sock >= START_SSL_CLIENT_SOCK) {
internal_sock = sock - START_SSL_CLIENT_SOCK;
is_sslclienet = true;
} else
if(sock >= START_CLIENT_SERVER_SOCK) {
internal_sock = sock - START_CLIENT_SERVER_SOCK;
is_server = true;
}
else {
internal_sock = sock;
}

if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) {
rv.client = nullptr;
rv.sslclient = nullptr;
rv.can_delete = -1;
return rv;
}
CClientWrapper rv;

bool is_server = false;
bool is_sslclienet = false;

int internal_sock = -1;

if (is_sslclienet) {
if(sock >= START_SSL_CLIENT_SOCK) {
internal_sock = sock - START_SSL_CLIENT_SOCK;
is_sslclienet = true;
} else
if(sock >= START_CLIENT_SERVER_SOCK) {
internal_sock = sock - START_CLIENT_SERVER_SOCK;
is_server = true;
}
else {
internal_sock = sock;
}

if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) {
rv.client = nullptr;
rv.sslclient = nullptr;
rv.can_delete = -1;
return rv;
}

if (is_sslclienet) {
rv.sslclient = sslclients[internal_sock];
rv.can_delete = internal_sock;
}
else if(is_server) {
}
else if(is_server) {
rv.client = &serverClients[internal_sock].client;
rv.can_delete = -1;
}
else {
}
else {
rv.client = clients[internal_sock];
rv.can_delete = internal_sock;
}
return rv;
}
return rv;
}



/* -------------------------------------------------------------------------- */
void CAtHandler::run() {
/* -------------------------------------------------------------------------- */
at_srv.run();
vTaskDelay(1);
/* -------------------------------------------------------------------------- */
at_srv.run();

// execute all the pending tasks

// it is intended for the tasks to add other tasks in queue after being executed,
// saving the current size of the queue
auto size = tasks.size();
for(int i=0; i<size; i++) {
auto task = tasks.front();
tasks.pop();
if(task) {
task();
}
}

vTaskDelay(1);
}



/* -------------------------------------------------------------------------- */
CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

for(int i = 0; i < MAX_CLIENT_AVAILABLE; i++) {
clients[i] = nullptr;
}
Expand Down Expand Up @@ -119,16 +133,16 @@ CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) {

if (it == command_table.end()) {
return chAT::CommandStatus::ERROR;
}
}
else {
return it->second(srv, srv.parser());
}
});

/* SET UP COMMAND TABLE */
/* SET UP COMMAND TABLE */
add_cmds_esp_generic();
add_cmds_wifi_station();
add_cmds_wifi_softAP();
add_cmds_wifi_softAP();
add_cmds_wifi_SSL();
add_cmds_wifi_netif();
add_cmds_wifi_udp();
Expand Down
Loading