mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
Merge pull request #377 from liamcottle/rescue/cli-file-manager
Basic File Manager for Rescue CLI
This commit is contained in:
commit
381bb50eb7
3 changed files with 82 additions and 0 deletions
|
|
@ -42,6 +42,20 @@ void DataStore::begin() {
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
#endif
|
#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", false);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DataStore::removeFile(const char* filename) {
|
||||||
|
return _fs->remove(filename);
|
||||||
|
}
|
||||||
|
|
||||||
bool DataStore::formatFileSystem() {
|
bool DataStore::formatFileSystem() {
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
return _fs->format();
|
return _fs->format();
|
||||||
|
|
|
||||||
|
|
@ -37,4 +37,6 @@ public:
|
||||||
void saveChannels(DataStoreHost* host);
|
void saveChannels(DataStoreHost* host);
|
||||||
uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]);
|
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);
|
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
|
||||||
|
File openRead(const char* filename);
|
||||||
|
bool removeFile(const char* filename);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1305,6 +1305,72 @@ void MyMesh::checkCLIRescueCmd() {
|
||||||
} else {
|
} else {
|
||||||
Serial.println(" Error: erase failed");
|
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);
|
||||||
|
if(root){
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while (file) {
|
||||||
|
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
Serial.printf("[dir] %s\n", file.name());
|
||||||
|
} else {
|
||||||
|
Serial.printf("[file] %s (%d bytes)\n", file.name(), file.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// move to next file
|
||||||
|
file = root.openNextFile();
|
||||||
|
|
||||||
|
}
|
||||||
|
root.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
} 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 (memcmp(cli_command, "rm ", 3) == 0) {
|
||||||
|
|
||||||
|
// get path from command e.g: "rm /adv_blobs"
|
||||||
|
const char *path = &cli_command[4];
|
||||||
|
|
||||||
|
// ensure path is not empty, or root dir
|
||||||
|
if(!path || strlen(path) == 0 || strcmp(path, "/") == 0){
|
||||||
|
Serial.println("Invalid path provided");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// remove file
|
||||||
|
bool removed = _store->removeFile(path);
|
||||||
|
if(removed){
|
||||||
|
Serial.println("File removed");
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to remove file");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} else if (strcmp(cli_command, "reboot") == 0) {
|
} else if (strcmp(cli_command, "reboot") == 0) {
|
||||||
board.reboot(); // doesn't return
|
board.reboot(); // doesn't return
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue