|
| 1 | +/* |
| 2 | + * This example demonstrates how to download a lzss file and decompress it in two ways: |
| 3 | + * -1 download the file on the filesystem and then decompress the downloaded file on the filesystem |
| 4 | + * -2 download and decompress the file on the fly |
| 5 | + * this sketch also provides a comparison in terms of speed and execution time |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +/****************************************************************************** |
| 10 | + * INCLUDE |
| 11 | + ******************************************************************************/ |
| 12 | + |
| 13 | +#include <Arduino_Portenta_OTA.h> |
| 14 | + |
| 15 | +#include <WiFi.h> |
| 16 | + |
| 17 | +#include "arduino_secrets.h" |
| 18 | +#include <decompress/lzss.h> |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * CONSTANT |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +/* Please enter your sensitive data in the Secret tab/arduino_secrets.h */ |
| 25 | +static char const SSID[] = SECRET_SSID; /* your network SSID (name) */ |
| 26 | +static char const PASS[] = SECRET_PASS; /* your network password (use for WPA, or use as key for WEP) */ |
| 27 | + |
| 28 | + |
| 29 | +#if defined(ARDUINO_NICLA_VISION) |
| 30 | +static char const URL_FILE[] = "https://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.NICLA_VISION.ota"; |
| 31 | +#elif defined(ARDUINO_PORTENTA_H7_M7) |
| 32 | +static char const URL_FILE[] = "https://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.PORTENTA_H7_M7.ota"; |
| 33 | +#elif defined(ARDUINO_OPTA) |
| 34 | +static char const URL_FILE[] = "https://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.OPTA.ota"; |
| 35 | +#elif defined(ARDUINO_GIGA) |
| 36 | +static char const URL_FILE[] = "https://downloads.arduino.cc/ota/OTA_Usage_Portenta.ino.GIGA.ota"; |
| 37 | +#else |
| 38 | +#error "Board not supported" |
| 39 | +#endif |
| 40 | + |
| 41 | +static char const DOWNLOAD_DESTINATION[] = "/fs/UPDATE.BIN.LZSS"; |
| 42 | +static char const DECOMPRESSED_DESTINATION[] = "/fs/UPDATE.BIN"; |
| 43 | + |
| 44 | +LZSSDecoder *decoder = nullptr; |
| 45 | +FILE* download_target = nullptr; |
| 46 | + |
| 47 | +/****************************************************************************** |
| 48 | + * SETUP/LOOP |
| 49 | + ******************************************************************************/ |
| 50 | +void decompress_on_the_fly_cbk(const char*, uint32_t); |
| 51 | +void putc_file(const uint8_t c); |
| 52 | + |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); |
| 55 | + while (!Serial) {} |
| 56 | + |
| 57 | + if (WiFi.status() == WL_NO_SHIELD) |
| 58 | + { |
| 59 | + Serial.println("Communication with WiFi module failed!"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + int status = WL_IDLE_STATUS; |
| 64 | + while (status != WL_CONNECTED) |
| 65 | + { |
| 66 | + Serial.print ("Attempting to connect to '"); |
| 67 | + Serial.print (SSID); |
| 68 | + Serial.println("'"); |
| 69 | + status = WiFi.begin(SSID, PASS); |
| 70 | + if(status != WL_CONNECTED) { |
| 71 | + delay(10000); |
| 72 | + } |
| 73 | + } |
| 74 | + Serial.print ("You're connected to '"); |
| 75 | + Serial.print (WiFi.SSID()); |
| 76 | + Serial.println("'"); |
| 77 | + |
| 78 | + // Init fs |
| 79 | + mbed::BlockDevice * _bd_raw_qspi = mbed::BlockDevice::get_default_instance();; |
| 80 | + auto _bd_qspi = new mbed::MBRBlockDevice(_bd_raw_qspi, 2); |
| 81 | + auto _fs_qspi = new mbed::FATFileSystem("fs"); |
| 82 | + int const err_mount = _fs_qspi->mount(_bd_qspi); |
| 83 | + if (err_mount) { |
| 84 | + Serial.print("Error while mounting the filesystem. Err = "); |
| 85 | + Serial.println(err_mount); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + MbedSocketClass * socket = static_cast<MbedSocketClass*>(&WiFi); |
| 90 | + remove(DOWNLOAD_DESTINATION); |
| 91 | + remove(DECOMPRESSED_DESTINATION); |
| 92 | + |
| 93 | + uint32_t start; |
| 94 | + int bytes; |
| 95 | + float elapsed, speed; |
| 96 | + start = millis(); |
| 97 | + Serial.println("Starting download to QSPI ..."); |
| 98 | + bytes = socket->download(URL_FILE, DOWNLOAD_DESTINATION, true /* is_https */); |
| 99 | + if (bytes <= 0) |
| 100 | + { |
| 101 | + Serial.print ("MbedSocketClass::download failed with error code "); |
| 102 | + Serial.println(bytes); |
| 103 | + return; |
| 104 | + } |
| 105 | + Serial.print (bytes); |
| 106 | + Serial.println(" bytes stored."); |
| 107 | + |
| 108 | + elapsed = (millis()-start)/1000.0; // elapsed expressed in seconds |
| 109 | + speed = (bytes/elapsed)/1024; |
| 110 | + |
| 111 | + Serial.print("download elapsed "); |
| 112 | + Serial.print(elapsed); |
| 113 | + Serial.print("s speed: "); |
| 114 | + Serial.print(speed); |
| 115 | + Serial.println("KBps"); |
| 116 | + |
| 117 | + FILE* downloaded_file = fopen(DOWNLOAD_DESTINATION, "rb"); |
| 118 | + FILE* decompressed_file = fopen(DECOMPRESSED_DESTINATION, "wb"); |
| 119 | + |
| 120 | + start = millis(); |
| 121 | + lzss_init(downloaded_file, decompressed_file, bytes, nullptr); |
| 122 | + |
| 123 | + lzss_decode(); |
| 124 | + /* Write the data remaining in the write buffer to |
| 125 | + * the file. |
| 126 | + */ |
| 127 | + lzss_flush(); |
| 128 | + |
| 129 | + elapsed = (millis()-start)/1000.0; // elapsed expressed in seconds |
| 130 | + |
| 131 | + Serial.print("decompress elapsed "); |
| 132 | + Serial.print(elapsed); |
| 133 | + Serial.print("s"); |
| 134 | + Serial.print(" size "); |
| 135 | + Serial.println(ftell(decompressed_file)); |
| 136 | + |
| 137 | + fclose(downloaded_file); |
| 138 | + fclose(decompressed_file); |
| 139 | + |
| 140 | + // On the fly decompression |
| 141 | + remove(DOWNLOAD_DESTINATION); |
| 142 | + remove(DECOMPRESSED_DESTINATION); |
| 143 | + |
| 144 | + download_target = fopen(DECOMPRESSED_DESTINATION, "wb"); |
| 145 | + decoder = new LZSSDecoder(putc_file); |
| 146 | + |
| 147 | + Serial.println("Starting download & decompress on the fly"); |
| 148 | + start = millis(); |
| 149 | + bytes = socket->download(URL_FILE, true /* is_https */, decompress_on_the_fly_cbk); |
| 150 | + if (bytes <= 0) |
| 151 | + { |
| 152 | + Serial.print ("MbedSocketClass::download failed with error code "); |
| 153 | + Serial.println(bytes); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + Serial.print("downloaded "); |
| 158 | + Serial.print(bytes); |
| 159 | + Serial.print(" bytes "); |
| 160 | + |
| 161 | + elapsed = (millis()-start)/1000.0; // elapsed expressed in seconds |
| 162 | + speed = (bytes/elapsed)/1024; |
| 163 | + |
| 164 | + Serial.print (ftell(download_target)); |
| 165 | + Serial.println(" bytes stored."); |
| 166 | + |
| 167 | + Serial.print("download elapsed "); |
| 168 | + Serial.print(elapsed); |
| 169 | + Serial.print("s speed: "); |
| 170 | + Serial.print(speed); |
| 171 | + Serial.println("KBps"); |
| 172 | + |
| 173 | + delete decoder; |
| 174 | + fclose(download_target); |
| 175 | +} |
| 176 | + |
| 177 | +void loop() { |
| 178 | +} |
| 179 | + |
| 180 | +void decompress_on_the_fly_cbk(const char* buffer, uint32_t size) { |
| 181 | + decoder->decompress((uint8_t*)buffer, size); |
| 182 | +} |
| 183 | + |
| 184 | +void putc_file(const uint8_t c) { |
| 185 | + fwrite(&c, 1, 1, download_target); |
| 186 | +} |
| 187 | + |
0 commit comments