|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2020 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 | + |
| 22 | +#include <ArduinoIoTCloud_Config.h> |
| 23 | +#if OTA_STORAGE_SNU |
| 24 | + |
| 25 | +#include "OTAStorage_SNU.h" |
| 26 | + |
| 27 | +#include <WiFiStorage.h> |
| 28 | + |
| 29 | +/****************************************************************************** |
| 30 | + * CONSTANTS |
| 31 | + ******************************************************************************/ |
| 32 | + |
| 33 | +static char const SNU_UPDATE_FILENAME[] = "/fs/UPDATE.BIN"; |
| 34 | +static char const SNU_TEMP_UPDATE_FILENAME[] = "/fs/UPDATE.BIN.TMP"; |
| 35 | + |
| 36 | +/****************************************************************************** |
| 37 | + * PUBLIC MEMBER FUNCTIONS |
| 38 | + ******************************************************************************/ |
| 39 | + |
| 40 | +bool OTAStorage_SNU::init() |
| 41 | +{ |
| 42 | + /* Ensure that there are no remains of previous |
| 43 | + * aborted downloads still existing in the memory |
| 44 | + * of the nina module. |
| 45 | + */ |
| 46 | + WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME); |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +bool OTAStorage_SNU::open(char const * /* file_name */) |
| 51 | +{ |
| 52 | + /* There's no need to explicitly open the file |
| 53 | + * because when writing to it the file will always |
| 54 | + * be opened with "ab+" mode and closed after each |
| 55 | + * call to 'write'. |
| 56 | + */ |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes) |
| 61 | +{ |
| 62 | + WiFiStorageFile file(SNU_TEMP_UPDATE_FILENAME); |
| 63 | + |
| 64 | + /* We have to write in chunks because otherwise we exceed the size of |
| 65 | + * the SPI buffer within the nina module. |
| 66 | + */ |
| 67 | + size_t bytes_written = 0; |
| 68 | + size_t const WRITE_CHUNK_SIZE = 32; |
| 69 | + |
| 70 | + for(; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE) |
| 71 | + { |
| 72 | + if (file.write(buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE) |
| 73 | + return bytes_written; |
| 74 | + } |
| 75 | + |
| 76 | + bytes_written += file.write(buf + bytes_written, num_bytes - bytes_written); |
| 77 | + |
| 78 | + return bytes_written; |
| 79 | +} |
| 80 | + |
| 81 | +void OTAStorage_SNU::close() |
| 82 | +{ |
| 83 | + /* Files are closed after each file operation on the nina side. */ |
| 84 | +} |
| 85 | + |
| 86 | +void OTAStorage_SNU::remove(char const * /* file_name */) |
| 87 | +{ |
| 88 | + WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME); |
| 89 | +} |
| 90 | + |
| 91 | +bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */) |
| 92 | +{ |
| 93 | + return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME); |
| 94 | +} |
| 95 | + |
| 96 | +void OTAStorage_SNU::deinit() |
| 97 | +{ |
| 98 | + /* Nothing to do */ |
| 99 | +} |
| 100 | + |
| 101 | +#endif /* OTA_STORAGE_SNU */ |
0 commit comments