diff --git a/examples/Test_deflate/Test_deflate.ino b/examples/Test_deflate/Test_deflate.ino index 2555fad..03067c9 100644 --- a/examples/Test_deflate/Test_deflate.ino +++ b/examples/Test_deflate/Test_deflate.ino @@ -52,6 +52,8 @@ void testStreamToStream() verify(fzFileName, inputFilename); + tarGzFS.remove(fzFileName); + } @@ -90,6 +92,8 @@ void testBufferToBuffer() verify(fzFileName, inputFilename); + tarGzFS.remove(fzFileName); + } @@ -132,6 +136,8 @@ void testStreamToBuffer() verify(fzFileName, inputFilename); + tarGzFS.remove(fzFileName); + } @@ -175,6 +181,8 @@ void testBufferToStream() verify(fzFileName, inputFilename); + tarGzFS.remove(fzFileName); + } @@ -198,12 +206,12 @@ void setup() { testStreamToStream(); // tested OK on ESP32/RP2040/ESP8266 (any file size) printMem(); - testBufferToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size) - printMem(); - testBufferToStream(); // tested OK on ESP32/RP2040/ESP8266 (small file size) - printMem(); - testStreamToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size) - printMem(); + // testBufferToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size) + // printMem(); + // testBufferToStream(); // tested OK on ESP32/RP2040/ESP8266 (small file size) + // printMem(); + // testStreamToBuffer(); // tested OK on ESP32/RP2040/ESP8266 (small file size) + // printMem(); Serial.println(); Serial.println("All tests completed"); diff --git a/examples/Test_tar_packer/Test_tar_packer.ino b/examples/Test_tar_packer/Test_tar_packer.ino index 8af7531..9b62938 100644 --- a/examples/Test_tar_packer/Test_tar_packer.ino +++ b/examples/Test_tar_packer/Test_tar_packer.ino @@ -5,32 +5,54 @@ //#define DEST_FS_USES_FFAT // ESP32 only //#define DEST_FS_USES_SD_MMC // ESP32 only -// #define ESP32_TARGZ_DISABLE_COMPRESSION -// #define ESP32_TARGZ_DISABLE_DECOMPRESSION #include -// Uncomment this macro to download the generated files when using SPIFFS/LittleFS -// Comment out this macro if you get a "sketch too big" compilation error +// Uncomment `USE_WEBSERVER` to download the generated files when using SPIFFS/LittleFS +// Comment out `USE_WEBSERVER` if you get a "sketch too big" compilation error #define USE_WEBSERVER +// Comment out `USE_ETHERNET` to use WiFi instead of Ethernet +// #define USE_ETHERNET #if defined USE_WEBSERVER - #include + #if !defined USE_ETHERNET + #include + #else + #include + + static bool eth_connected = false; + + // WARNING: onEvent is called from a separate FreeRTOS task (thread)! + void onEvent(arduino_event_id_t event) { + switch (event) { + case ARDUINO_EVENT_ETH_START: Serial.println("ETH Started"); ETH.setHostname("esp32-ethernet"); break; + case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break; + case ARDUINO_EVENT_ETH_GOT_IP: Serial.println("ETH Got IP"); Serial.println(ETH); eth_connected = true; break; + case ARDUINO_EVENT_ETH_LOST_IP: Serial.println("ETH Lost IP"); eth_connected = false; break; + case ARDUINO_EVENT_ETH_DISCONNECTED: Serial.println("ETH Disconnected"); eth_connected = false; break; + case ARDUINO_EVENT_ETH_STOP: Serial.println("ETH Stopped"); eth_connected = false; break; + default: break; + } + } + + #endif + #include #include WebServer server(80); #endif - const char* src_path = "/"; // source folder (no ending slash except if root) const char* tar_path = "/test.tar"; // output tar archive to create const char* targz_path = "/test.tar.gz"; // output tar archive to create -const char* dst_path = "data"; // optional virtual folder in the tar archive, no ending slash, set to NULL to put everything in the root +const char* dst_path = nullptr; // optional virtual folder in the tar archive, no ending slash, set to NULL to put everything in the root std::vector dirEntities; // storage for scanned dir entities + void removeTempFiles() { + Serial.println("Cleaning up temporary files"); // cleanup test from other examples to prevent SPIFFS from exploding :) if(tarGzFS.exists(tar_path)) tarGzFS.remove(tar_path); @@ -39,71 +61,69 @@ void removeTempFiles() } -bool testTarGzPackerFS() +void testTarPacker() { - // Read source directory "/", append content under "data/" in tar output, and compress to "test.tar.gz" - // src_path: source, path to dir (will recursively fetch files) - // targz_path: destination, .tar.gz compressed file - // dst_path: optional, rootdir name in tar archive - // NOTE: Source and destination are on the same filesystem. - removeTempFiles(); + // tar_path: destination, .tar packed file + // dst_path: optional path prefix in tar archive + removeTempFiles(); // cleanup previous examples Serial.println(); - Serial.println("TarGzPacker::compress(&tarGzFS, src_path, targz_path, dst_path )"); - size_t dstLen = TarGzPacker::compress(&tarGzFS, src_path, targz_path, dst_path ); - Serial.printf("Source folder '%s' compressed to %d bytes in %s\n", src_path, dstLen, targz_path); - Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() ); - return dstLen > 0; + Serial.printf("TarPacker::pack_files(&tarGzFS, dirEntities, &tarGzFS, tar_path, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() ); + TarPacker::setProgressCallBack( LZPacker::defaultProgressCallback ); + auto ret = TarPacker::pack_files(&tarGzFS, dirEntities, &tarGzFS, tar_path, dst_path); + Serial.printf("Wrote %d bytes to %s\n", ret, tar_path); } +void testTarPackerStream() +{ + // tar_path: destination, .tar packed file + // dst_path: optional path prefix in tar archive + removeTempFiles(); // cleanup previous examples + Serial.println(); + Serial.printf("TarPacker::pack_files(&tarGzFS, dirEntities, &tar, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() ); + TarPacker::setProgressCallBack( LZPacker::defaultProgressCallback ); + auto tar = tarGzFS.open(tar_path, "w"); + if(!tar) + return; + auto ret = TarPacker::pack_files(&tarGzFS, dirEntities, &tar, dst_path); + tar.close(); + Serial.printf("Wrote %d bytes to %s\n", ret, tar_path); +} -bool testTarGzPackerStream() +void testTarGzPacker() { - // Read source directory "/", put content under "data/" in tar archive, and compress to stream - // src_path: source, path to dir (will recursively fetch files) - // targz_path: optional, name of the .tar.gz compressed file to prevent self-inclusion - // dst_path: optional, rootdir name in tar archive - removeTempFiles(); + removeTempFiles(); // cleanup previous examples + // targz_path: name of the .tar.gz compressed file + // dst_path: optional path prefix in tar archive Serial.println(); - Serial.printf("TarGzPacker::compress( &tarStream, &tarGzOutput ); Free heap: %lu bytes\n", ESP.getFreeHeap() ); - TarGzStream tarStream(&tarGzFS, &dirEntities, src_path, targz_path, dst_path); - Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() ); - size_t srcLen = tarStream.size(); - File tarGzOutput = tarGzFS.open(targz_path, "w"); // NOTE: tarGzOutput can also be a network client, or even Serial - size_t dstLen = TarGzPacker::compress( &tarStream, &tarGzOutput ); - tarGzOutput.close(); - Serial.printf("Compressed %d bytes to %s (%d bytes)\n", srcLen, targz_path, dstLen); - Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() ); - return dstLen > 0; + Serial.printf("TarGzPacker::compress(&tarGzFS, dirEntities, &tarGzFS, targz_path, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() ); + auto ret = TarGzPacker::compress(&tarGzFS, dirEntities, &tarGzFS, targz_path, dst_path); + Serial.printf("Wrote %d bytes to %s\n", ret, targz_path); } - -bool testTarPacker() +void testTarGzPackerStream() { - // Read source directory "/", put contents under "data/" in tar archive, and pack to "test.tar" - // src_path: source, path to dir (will recursively fetch files) - // tar_path: destination, .tar packed file - // NOTE: Source and destination are on the same filesystem. - removeTempFiles(); + // targz_path: name of the .tar.gz compressed file + // dst_path: optional path prefix in tar archive + removeTempFiles(); // cleanup previous examples Serial.println(); - Serial.printf("TarPacker::pack_files(&tarGzFS, &dirEntities, tar_path, dst_path); Free heap: %lu bytes\n", ESP.getFreeHeap() ); - ssize_t dstLen = TarPacker::pack_files(&tarGzFS, &dirEntities, tar_path, dst_path); - if( dstLen <= 0 ) {// test failed - Serial.printf("Packing failed (ret=%d)\n", dstLen); - return false; - } - File tar = tarGzFS.open(tar_path); - size_t tar_size = tar.size(); - tar.close(); - Serial.printf("Wrote %d bytes to %s, file size=%d\n", dstLen, tar_path, tar_size ); - return ( tar_size == dstLen ); + Serial.printf("TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() ); + auto gz = tarGzFS.open(targz_path, "w"); + if(!gz) + return; + auto ret = TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path); + gz.close(); + Serial.printf("Wrote %d bytes to %s\n", ret, targz_path); } void setup() { Serial.begin(115200); - Serial.println("TarPacker test"); + + delay(5000); // NOTE: USB-CDC is a drag + + Serial.println("TarPacker/TarGzPacker test"); if(! tarGzFS.begin() ) { Serial.println("Failed to start filesystem, halting"); @@ -111,31 +131,42 @@ void setup() } removeTempFiles(); // cleanup previous examples + Serial.println("Gathering directory entitites"); TarPacker::collectDirEntities(&dirEntities, &tarGzFS, src_path, 3); // collect dir and files at %{src_path} LZPacker::setProgressCallBack( LZPacker::defaultProgressCallback ); - Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() ); + Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() ); - // testTarGzPackerFS(); + testTarPacker(); + testTarPackerStream(); + testTarGzPacker(); testTarGzPackerStream(); - // testTarPacker(); - Serial.printf("Free heap: %lu bytes\n", ESP.getFreeHeap() ); + Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() ); + #if defined USE_WEBSERVER - // start WiFI - WiFi.mode(WIFI_STA); - WiFi.begin(); - printf("Connect to WiFi...\n"); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - printf("."); - } - printf("connected.\n"); + #if !defined USE_ETHERNET // start WiFI + WiFi.mode(WIFI_STA); + WiFi.begin(); + Serial.printf("Connect to WiFi...\n"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.printf("."); + } + Serial.printf("connected.\n"); + Serial.printf("open or \n", WiFi.getHostname(), WiFi.localIP().toString().c_str()); + #else // start ethernet + Network.onEvent(onEvent); // Will call onEvent() from another thread. + ETH.begin(); + while (!eth_connected) { + delay(500); + printf("."); + } + #endif // start webserver, serve filesystem at root server.serveStatic("/", tarGzFS, "/", nullptr); server.begin(); - printf("open or \n", WiFi.getHostname(), WiFi.localIP().toString().c_str()); #endif } diff --git a/examples/Test_tar_packer/data/ESP32-targz.bmp b/examples/Test_tar_packer/data/ESP32-targz.bmp deleted file mode 100644 index af37521..0000000 Binary files a/examples/Test_tar_packer/data/ESP32-targz.bmp and /dev/null differ diff --git a/examples/Test_tar_packer/data/index.htm b/examples/Test_tar_packer/data/index.htm index 75b8643..77f88fb 100755 --- a/examples/Test_tar_packer/data/index.htm +++ b/examples/Test_tar_packer/data/index.htm @@ -29,10 +29,29 @@ } @media (orientation: portrait) and (min-width: 24rem) { - body { --section-width: 88"https://upload.wikimedia.org/wikipedia/commons/1/1a/SVG_example_markup_grid.svg">SVG_example_markup_grid.svg - - -