From 0f601752e4e9089a866c2e6d70c439e709c93b94 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 7 Jun 2025 15:23:55 +1200 Subject: [PATCH] implement ls and cat commands for rescue mode --- examples/companion_radio/DataStore.cpp | 10 ++++++ examples/companion_radio/DataStore.h | 1 + examples/companion_radio/MyMesh.cpp | 48 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index a37cfa49..795e38cc 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -42,6 +42,16 @@ void DataStore::begin() { #include #endif +File DataStore::openRead(const char* filename) { +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) + return _fs->open(filename, FILE_O_READ); +#elif defined(RP2040_PLATFORM) + return _fs->open(filename, "r"); +#else + return _fs->open(filename, "r", true); +#endif +} + bool DataStore::formatFileSystem() { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) return _fs->format(); diff --git a/examples/companion_radio/DataStore.h b/examples/companion_radio/DataStore.h index 139131e1..201dac01 100644 --- a/examples/companion_radio/DataStore.h +++ b/examples/companion_radio/DataStore.h @@ -37,4 +37,5 @@ public: void saveChannels(DataStoreHost* host); uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]); bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len); + File openRead(const char* filename); }; diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 054d0cba..79910aee 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -1305,6 +1305,54 @@ void MyMesh::checkCLIRescueCmd() { } else { Serial.println(" Error: erase failed"); } + } else if (memcmp(cli_command, "ls", 2) == 0) { + + // get path from command e.g: "ls /adafruit" + const char *path = &cli_command[3]; + + // log each file and directory + File root = _store->openRead(path); + File file = root.openNextFile(); + while (file) { + + if (file.isDirectory()) { + Serial.print("[dir] "); + Serial.println(file.name()); + } else { + Serial.print("[file] "); + Serial.print(file.name()); + Serial.print(" ("); + Serial.print(file.size()); + Serial.println(" bytes)"); + } + + // move to next file + file = root.openNextFile(); + + } + + } else if (memcmp(cli_command, "cat", 3) == 0) { + + // get path from command e.g: "cat /contacts3" + const char *path = &cli_command[4]; + + // log file content as hex + File file = _store->openRead(path); + if(file){ + + // get file content + int file_size = file.available(); + uint8_t buffer[file_size]; + file.read(buffer, file_size); + + // print hex + mesh::Utils::printHex(Serial, buffer, file_size); + Serial.print("\n"); + + file.close(); + + } + } else if (strcmp(cli_command, "reboot") == 0) { board.reboot(); // doesn't return } else {