Firmware Upload via Binary Transfer (#23462)

This commit is contained in:
GHGiampy
2022-01-09 08:14:03 +01:00
committed by Scott Lahteine
parent 2ca1d844d7
commit 67e5298a34
8 changed files with 770 additions and 24 deletions

View File

@ -195,11 +195,15 @@ char *createFilename(char * const buffer, const dir_t &p) {
}
//
// Return 'true' if the item is a folder or G-code file
// Return 'true' if the item is something Marlin can read
//
bool CardReader::is_dir_or_gcode(const dir_t &p) {
bool CardReader::is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, bool onlyBin/*=false*/)) {
//uint8_t pn0 = p.name[0];
#if DISABLED(CUSTOM_FIRMWARE_UPLOAD)
constexpr bool onlyBin = false;
#endif
if ( (p.attributes & DIR_ATT_HIDDEN) // Hidden by attribute
// When readDir() > 0 these must be false:
//|| pn0 == DIR_NAME_FREE || pn0 == DIR_NAME_DELETED // Clear or Deleted entry
@ -211,7 +215,11 @@ bool CardReader::is_dir_or_gcode(const dir_t &p) {
return (
flag.filenameIsDir // All Directories are ok
|| (p.name[8] == 'G' && p.name[9] != '~') // Non-backup *.G* files are accepted
|| (!onlyBin && p.name[8] == 'G'
&& p.name[9] != '~') // Non-backup *.G* files are accepted
|| ( onlyBin && p.name[8] == 'B'
&& p.name[9] == 'I'
&& p.name[10] == 'N') // BIN files are accepted
);
}
@ -222,7 +230,7 @@ int CardReader::countItems(SdFile dir) {
dir_t p;
int c = 0;
while (dir.readDir(&p, longFilename) > 0)
c += is_dir_or_gcode(p);
c += is_visible_entity(p);
#if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
nrFiles = c;
@ -237,7 +245,7 @@ int CardReader::countItems(SdFile dir) {
void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0;) {
if (is_dir_or_gcode(p)) {
if (is_visible_entity(p)) {
if (cnt == index) {
createFilename(filename, p);
return; // 0 based index
@ -253,7 +261,7 @@ void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
void CardReader::selectByName(SdFile dir, const char * const match) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0; cnt++) {
if (is_dir_or_gcode(p)) {
if (is_visible_entity(p)) {
createFilename(filename, p);
if (strcasecmp(match, filename) == 0) return;
}
@ -272,6 +280,7 @@ void CardReader::selectByName(SdFile dir, const char * const match) {
*/
void CardReader::printListing(
SdFile parent, const char * const prepend
OPTARG(CUSTOM_FIRMWARE_UPLOAD, bool onlyBin/*=false*/)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames/*=false*/)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong/*=nullptr*/)
) {
@ -297,12 +306,12 @@ void CardReader::printListing(
char pathLong[lenPrependLong + strlen(longFilename) + 1];
if (prependLong) { strcpy(pathLong, prependLong); pathLong[lenPrependLong - 1] = '/'; }
strcpy(pathLong + lenPrependLong, longFilename);
printListing(child, path, true, pathLong);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin), true, pathLong);
}
else
printListing(child, path);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin));
#else
printListing(child, path);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin));
#endif
}
else {
@ -310,7 +319,7 @@ void CardReader::printListing(
return;
}
}
else if (is_dir_or_gcode(p)) {
else if (is_visible_entity(p OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin))) {
if (prepend) { SERIAL_ECHO(prepend); SERIAL_CHAR('/'); }
SERIAL_ECHO(createFilename(filename, p));
SERIAL_CHAR(' ');
@ -330,10 +339,16 @@ void CardReader::printListing(
//
// List all files on the SD card
//
void CardReader::ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames/*=false*/)) {
void CardReader::ls(
TERN_(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin/*=false*/)
#if BOTH(CUSTOM_FIRMWARE_UPLOAD, LONG_FILENAME_HOST_SUPPORT)
,
#endif
TERN_(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames/*=false*/)
) {
if (flag.mounted) {
root.rewind();
printListing(root, nullptr OPTARG(LONG_FILENAME_HOST_SUPPORT, includeLongNames));
printListing(root, nullptr OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin) OPTARG(LONG_FILENAME_HOST_SUPPORT, includeLongNames));
}
}

View File

@ -204,7 +204,13 @@ public:
FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
#endif
static void ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames=false));
static void ls(
TERN_(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false)
#if BOTH(CUSTOM_FIRMWARE_UPLOAD, LONG_FILENAME_HOST_SUPPORT)
,
#endif
TERN_(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames=false)
);
#if ENABLED(POWER_LOSS_RECOVERY)
static bool jobRecoverFileExists();
@ -331,12 +337,13 @@ private:
//
// Directory items
//
static bool is_dir_or_gcode(const dir_t &p);
static bool is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false));
static int countItems(SdFile dir);
static void selectByIndex(SdFile dir, const uint8_t index);
static void selectByName(SdFile dir, const char * const match);
static void printListing(
SdFile parent, const char * const prepend
OPTARG(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames=false)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong=nullptr)
);