|
| 1 | +/* |
| 2 | + This file is part of UNOR4USBBridge_OTA. |
| 3 | +
|
| 4 | + Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 5 | +
|
| 6 | + This software is released under the GNU General Public License version 3, |
| 7 | + which covers the main part of arduino-cli. |
| 8 | + The terms of this license can be found at: |
| 9 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +
|
| 11 | + You can be released from the requirements of the above licenses by purchasing |
| 12 | + a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + otherwise use the software for commercial activities involving the Arduino |
| 14 | + software without disclosing the source code of your own applications. To purchase |
| 15 | + a commercial license, send an email to [email protected]. |
| 16 | +*/ |
| 17 | + |
| 18 | +/****************************************************************************** |
| 19 | + INCLUDE |
| 20 | + ******************************************************************************/ |
| 21 | +#include <Update.h> |
| 22 | +#include <SPIFFS.h> |
| 23 | +#include <Arduino_ESP32_OTA.h> |
| 24 | +#include "OTA.h" |
| 25 | + |
| 26 | +/****************************************************************************** |
| 27 | + PUBLIC MEMBER FUNCTIONS |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic) |
| 31 | +{ |
| 32 | + /* initialize private variables */ |
| 33 | + otaInit(); |
| 34 | + |
| 35 | + /* ... initialize CRC ... */ |
| 36 | + crc32Init(); |
| 37 | + |
| 38 | + /* ... configure board Magic number */ |
| 39 | + setMagic(magic); |
| 40 | + |
| 41 | + if(!SPIFFS.begin()) { |
| 42 | + DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__); |
| 43 | + return Error::OtaStorageInit; |
| 44 | + } |
| 45 | + |
| 46 | + if(SPIFFS.exists(file_path)) { |
| 47 | + SPIFFS.remove(file_path); |
| 48 | + } |
| 49 | + |
| 50 | + _spiffs = true; |
| 51 | + |
| 52 | + SPIFFS.end(); |
| 53 | + return Error::None; |
| 54 | +} |
| 55 | + |
| 56 | +void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data) |
| 57 | +{ |
| 58 | + if(_spiffs) { |
| 59 | + int ret = fwrite(&data, sizeof(data), 1, _file); |
| 60 | + } else { |
| 61 | + Arduino_ESP32_OTA::write_byte_to_flash(data); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path) |
| 66 | +{ |
| 67 | + if(!SPIFFS.begin()) { |
| 68 | + DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__); |
| 69 | + return static_cast<int>(Error::OtaStorageInit); |
| 70 | + } |
| 71 | + |
| 72 | + String spiffs_path = String("/spiffs") + String(file_path); |
| 73 | + _file = fopen(spiffs_path.c_str(), "wb"); |
| 74 | + |
| 75 | + if(!_file) { |
| 76 | + DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__); |
| 77 | + return static_cast<int>(Error::OtaStorageInit); |
| 78 | + } |
| 79 | + |
| 80 | + /* Download and decode OTA file */ |
| 81 | + size_t size = download(ota_url); |
| 82 | + |
| 83 | + fclose(_file); |
| 84 | + _file = nullptr; |
| 85 | + SPIFFS.end(); |
| 86 | + return size; |
| 87 | +} |
| 88 | + |
| 89 | +Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify() |
| 90 | +{ |
| 91 | + /* ... then finalize ... */ |
| 92 | + crc32Finalize(); |
| 93 | + |
| 94 | + if(!crc32Verify()) { |
| 95 | + DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__); |
| 96 | + return Error::OtaHeaderCrc; |
| 97 | + } |
| 98 | + return Error::None; |
| 99 | +} |
| 100 | + |
| 101 | +Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update() |
| 102 | +{ |
| 103 | + if (!Update.end(true)) { |
| 104 | + DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString()); |
| 105 | + return Error::OtaStorageEnd; |
| 106 | + } |
| 107 | + return Error::None; |
| 108 | +} |
0 commit comments