Skip to content

[WIRE-120] OTA code import and refactoring #24

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 3 commits into from
Dec 1, 2023
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
108 changes: 108 additions & 0 deletions UNOR4USBBridge/OTA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
This file is part of UNOR4USBBridge_OTA.

Copyright 2023 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

/******************************************************************************
INCLUDE
******************************************************************************/
#include <Update.h>
#include <SPIFFS.h>
#include <Arduino_ESP32_OTA.h>
#include "OTA.h"

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

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

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

/* ... configure board Magic number */
setMagic(magic);

if(!SPIFFS.begin()) {
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
return Error::OtaStorageInit;
}

if(SPIFFS.exists(file_path)) {
SPIFFS.remove(file_path);
}

_spiffs = true;

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

void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data)
{
if(_spiffs) {
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)
{
if(!SPIFFS.begin()) {
DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__);
return static_cast<int>(Error::OtaStorageInit);
}

String spiffs_path = String("/spiffs") + String(file_path);
_file = fopen(spiffs_path.c_str(), "wb");

if(!_file) {
DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__);
return static_cast<int>(Error::OtaStorageInit);
}

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

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

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify()
{
/* ... then finalize ... */
crc32Finalize();

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

Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update()
{
if (!Update.end(true)) {
DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString());
return Error::OtaStorageEnd;
}
return Error::None;
}
60 changes: 60 additions & 0 deletions UNOR4USBBridge/OTA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This file is part of UNOR4USBBridge_OTA.

Copyright 2023 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_UNOWIFIR4_OTA_H_
#define ARDUINO_UNOWIFIR4_OTA_H_

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <Arduino_ESP32_OTA.h>

/******************************************************************************
* DEFINES
******************************************************************************/
#define ARDUINO_RA4M1_OTA_MAGIC 0x23411002

/******************************************************************************
* CLASS DECLARATION
******************************************************************************/

class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA
{

public:

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);
void write_byte_to_flash(uint8_t data);
Arduino_ESP32_OTA::Error verify();
Arduino_ESP32_OTA::Error update();

private:

FILE* _file;
bool _spiffs;
};

#endif /* ARDUINO_UNOWIFIR4_OTA_H_ */
4 changes: 2 additions & 2 deletions UNOR4USBBridge/cmds_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define CMDS_OTA_H

#include "at_handler.h"
#include "Arduino_ESP32_OTA.h"
#include "OTA.h"
#include <BossaArduino.h>

Arduino_ESP32_OTA OTA;
Arduino_UNOWIFIR4_OTA OTA;

void CAtHandler::add_cmds_ota() {
/* ....................................................................... */
Expand Down
7 changes: 2 additions & 5 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ if [ ! -d hardware ]; then
sed s#PWD#$PWD#g arduino-cli.yaml.orig > arduino-cli.yaml

cd hardware/esp32-patched/esp32/libraries
git clone https://github.com/arduino-libraries/ArduinoBLE.git
git clone https://github.com/pennam/Arduino_ESP32_OTA.git
cd Arduino_ESP32_OTA/
git checkout e27f822406986354197a09516edaaeea3ed18e79
cd ..
git clone https://github.com/arduino-libraries/ArduinoBLE.git -b 1.3.6
git clone https://github.com/arduino-libraries/Arduino_ESP32_OTA.git -b 0.2.0
git clone https://github.com/pennam/BOSSA.git
cd BOSSA
git checkout 43a8feff9dfd07109d25cb2005279dd92e81d8fc
Expand Down