| 1 | #include <catch2/catch_test_macros.hpp> |
| 2 | |
| 3 | #include "core/core.hpp" |
| 4 | |
| 5 | #include <boost/nowide/cstdio.hpp> |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | using namespace bgcode::core; |
| 10 | |
| 11 | class ScopedFile |
| 12 | { |
| 13 | public: |
| 14 | explicit ScopedFile(FILE* file) : m_file(file) {} |
| 15 | ~ScopedFile() { if (m_file != nullptr) fclose(m_file); } |
| 16 | private: |
| 17 | FILE* m_file{ nullptr }; |
| 18 | }; |
| 19 | |
| 20 | static std::string checksum_type_as_string(EChecksumType type) |
| 21 | { |
| 22 | switch (type) |
| 23 | { |
| 24 | case EChecksumType::None: { return "None"; } |
| 25 | case EChecksumType::CRC32: { return "CRC32"; } |
| 26 | } |
| 27 | return ""; |
| 28 | }; |
| 29 | |
| 30 | static std::string block_type_as_string(EBlockType type) |
| 31 | { |
| 32 | switch (type) |
| 33 | { |
| 34 | case EBlockType::FileMetadata: { return "FileMetadata"; } |
| 35 | case EBlockType::GCode: { return "GCode"; } |
| 36 | case EBlockType::SlicerMetadata: { return "SlicerMetadata"; } |
| 37 | case EBlockType::PrinterMetadata: { return "PrinterMetadata"; } |
| 38 | case EBlockType::PrintMetadata: { return "PrintMetadata"; } |
| 39 | case EBlockType::Thumbnail: { return "Thumbnail"; } |
| 40 | } |
| 41 | return ""; |
| 42 | }; |
| 43 | |
| 44 | static std::string compression_type_as_string(ECompressionType type) |
| 45 | { |
| 46 | switch (type) |
| 47 | { |
| 48 | case ECompressionType::None: { return "None"; } |
| 49 | case ECompressionType::Deflate: { return "Deflate"; } |
| 50 | case ECompressionType::Heatshrink_11_4: { return "Heatshrink 11,4"; } |
| 51 | case ECompressionType::Heatshrink_12_4: { return "Heatshrink 12,4"; } |
| 52 | } |
| 53 | return ""; |
| 54 | }; |
| 55 | |
| 56 | static std::string metadata_encoding_as_string(EMetadataEncodingType type) |
| 57 | { |
| 58 | switch (type) |
| 59 | { |
| 60 | case EMetadataEncodingType::INI: { return "INI"; } |
| 61 | case EMetadataEncodingType::JSON: { return "JSON"; } |
| 62 | } |
| 63 | return ""; |
| 64 | }; |
| 65 | |
| 66 | static std::string gcode_encoding_as_string(EGCodeEncodingType type) |
| 67 | { |
| 68 | switch (type) |
| 69 | { |
| 70 | case EGCodeEncodingType::None: { return "None"; } |
| 71 | case EGCodeEncodingType::MeatPack: { return "MeatPack"; } |
| 72 | case EGCodeEncodingType::MeatPackComments: { return "MeatPackComments"; } |
| 73 | } |
| 74 | return ""; |
| 75 | }; |
| 76 | |
| 77 | static std::string thumbnail_format_as_string(EThumbnailFormat type) |
| 78 | { |
| 79 | switch (type) |
| 80 | { |
| 81 | case EThumbnailFormat::JPG: { return "JPG"; } |
| 82 | case EThumbnailFormat::PNG: { return "PNG"; } |
| 83 | case EThumbnailFormat::QOI: { return "QOI"; } |
| 84 | } |
| 85 | return ""; |
| 86 | }; |
| 87 | |
| 88 | TEST_CASE("File transversal", "[Core]") |
| 89 | { |
| 90 | const std::string filename = std::string(TEST_DATA_DIR) + "/mini_cube_b.bgcode"; |
| 91 | std::cout << "\nTEST: File transversal\n"; |
| 92 | std::cout << "File:" << filename << "\n"; |
| 93 | |
| 94 | const size_t MAX_CHECKSUM_CACHE_SIZE = 2048; |
| 95 | std::byte checksum_verify_buffer[MAX_CHECKSUM_CACHE_SIZE]; |
| 96 | |
| 97 | FILE* file = boost::nowide::fopen(filename.c_str(), "rb"); |
| 98 | REQUIRE(file != nullptr); |
| 99 | ScopedFile scoped_file(file); |
| 100 | REQUIRE(is_valid_binary_gcode(*file, true, checksum_verify_buffer, sizeof(checksum_verify_buffer)) == EResult::Success); |
| 101 | |
| 102 | fseek(file, 0, SEEK_END); |
| 103 | const long file_size = ftell(file); |
| 104 | rewind(file); |
| 105 | |
| 106 | FileHeader file_header; |
| 107 | REQUIRE(read_header(*file, file_header, nullptr) == EResult::Success); |
| 108 | std::cout << "Checksum type: " << checksum_type_as_string((EChecksumType)file_header.checksum_type) << "\n"; |
| 109 | |
| 110 | BlockHeader block_header; |
| 111 | |
| 112 | do |
| 113 | { |
| 114 | // read block header |
| 115 | REQUIRE(read_next_block_header(*file, file_header, block_header, checksum_verify_buffer, sizeof(checksum_verify_buffer)) == EResult::Success); |
| 116 | std::cout << "Block: " << block_type_as_string((EBlockType)block_header.type); |
| 117 | std::cout << " - compression: " << compression_type_as_string((ECompressionType)block_header.compression); |
| 118 | switch ((EBlockType)block_header.type) |
| 119 | { |
| 120 | case EBlockType::FileMetadata: |
| 121 | case EBlockType::PrinterMetadata: |
| 122 | case EBlockType::PrintMetadata: |
| 123 | case EBlockType::SlicerMetadata: |
| 124 | { |
| 125 | const long curr_pos = ftell(file); |
| 126 | uint16_t encoding; |
| 127 | const size_t rsize = fread(&encoding, 1, sizeof(encoding), file); |
| 128 | REQUIRE((ferror(file) == 0 && rsize == sizeof(encoding))); |
| 129 | fseek(file, curr_pos, SEEK_SET); |
| 130 | std::cout << " - encoding: " << metadata_encoding_as_string((EMetadataEncodingType)encoding); |
| 131 | break; |
| 132 | } |
| 133 | case EBlockType::GCode: |
| 134 | { |
| 135 | const long curr_pos = ftell(file); |
| 136 | uint16_t encoding; |
| 137 | const size_t rsize = fread(&encoding, 1, sizeof(encoding), file); |
| 138 | REQUIRE((ferror(file) == 0 && rsize == sizeof(encoding))); |
| 139 | fseek(file, curr_pos, SEEK_SET); |
| 140 | std::cout << " - encoding: " << gcode_encoding_as_string((EGCodeEncodingType)encoding); |
| 141 | break; |
| 142 | } |
| 143 | case EBlockType::Thumbnail: |
| 144 | { |
| 145 | const long curr_pos = ftell(file); |
| 146 | ThumbnailParams thumbnail_params; |
| 147 | REQUIRE(thumbnail_params.read(*file) == EResult::Success); |
| 148 | fseek(file, curr_pos, SEEK_SET); |
| 149 | std::cout << " - format: " << thumbnail_format_as_string((EThumbnailFormat)thumbnail_params.format); |
| 150 | std::cout << " (size: " << thumbnail_params.width << "x" << thumbnail_params.height << ")"; |
| 151 | break; |
| 152 | } |
| 153 | default: { break; } |
| 154 | } |
| 155 | std::cout << " - data size: " << ((block_header.compressed_size == 0) ? block_header.uncompressed_size : block_header.compressed_size); |
| 156 | std::cout << "\n"; |
| 157 | |
| 158 | // move to next block header |
| 159 | REQUIRE(skip_block(*file, file_header, block_header) == EResult::Success); |
| 160 | if (ftell(file) == file_size) |
| 161 | break; |
| 162 | } while (true); |
| 163 | } |
| 164 | |
| 165 | TEST_CASE("Search for GCode blocks", "[Core]") |
| 166 | { |
| 167 | const std::string filename = std::string(TEST_DATA_DIR) + "/mini_cube_b.bgcode"; |
| 168 | std::cout << "\nTEST: Search for GCode blocks\n"; |
| 169 | std::cout << "File:" << filename << "\n"; |
| 170 | |
| 171 | const size_t MAX_CHECKSUM_CACHE_SIZE = 2048; |
| 172 | std::byte checksum_verify_buffer[MAX_CHECKSUM_CACHE_SIZE]; |
| 173 | |
| 174 | FILE* file = boost::nowide::fopen(filename.c_str(), "rb"); |
| 175 | REQUIRE(file != nullptr); |
| 176 | ScopedFile scoped_file(file); |
| 177 | REQUIRE(is_valid_binary_gcode(*file, true, checksum_verify_buffer, sizeof(checksum_verify_buffer)) == EResult::Success); |
| 178 | |
| 179 | fseek(file, 0, SEEK_END); |
| 180 | const long file_size = ftell(file); |
| 181 | rewind(file); |
| 182 | |
| 183 | FileHeader file_header; |
| 184 | REQUIRE(read_header(*file, file_header, nullptr) == EResult::Success); |
| 185 | |
| 186 | BlockHeader block_header; |
| 187 | |
| 188 | do |
| 189 | { |
| 190 | // search and read block header by type |
| 191 | REQUIRE(read_next_block_header(*file, file_header, block_header, EBlockType::GCode, checksum_verify_buffer, sizeof(checksum_verify_buffer)) == EResult::Success); |
| 192 | std::cout << "Block type: " << block_type_as_string((EBlockType)block_header.type) << "\n"; |
| 193 | |
| 194 | // move to next block header |
| 195 | REQUIRE(skip_block(*file, file_header, block_header) == EResult::Success); |
| 196 | if (ftell(file) == file_size) |
| 197 | break; |
| 198 | } while (true); |
| 199 | } |