Skip to content

Commit 381bb50

Browse files
authored
Merge pull request #377 from liamcottle/rescue/cli-file-manager
Basic File Manager for Rescue CLI
2 parents 9c83348 + 7f79d0c commit 381bb50

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ void DataStore::begin() {
4242
#include <LittleFS.h>
4343
#endif
4444

45+
File DataStore::openRead(const char* filename) {
46+
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
47+
return _fs->open(filename, FILE_O_READ);
48+
#elif defined(RP2040_PLATFORM)
49+
return _fs->open(filename, "r");
50+
#else
51+
return _fs->open(filename, "r", false);
52+
#endif
53+
}
54+
55+
bool DataStore::removeFile(const char* filename) {
56+
return _fs->remove(filename);
57+
}
58+
4559
bool DataStore::formatFileSystem() {
4660
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
4761
return _fs->format();

examples/companion_radio/DataStore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ class DataStore {
3737
void saveChannels(DataStoreHost* host);
3838
uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]);
3939
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
40+
File openRead(const char* filename);
41+
bool removeFile(const char* filename);
4042
};

examples/companion_radio/MyMesh.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,72 @@ void MyMesh::checkCLIRescueCmd() {
13051305
} else {
13061306
Serial.println(" Error: erase failed");
13071307
}
1308+
} else if (memcmp(cli_command, "ls", 2) == 0) {
1309+
1310+
// get path from command e.g: "ls /adafruit"
1311+
const char *path = &cli_command[3];
1312+
1313+
// log each file and directory
1314+
File root = _store->openRead(path);
1315+
if(root){
1316+
File file = root.openNextFile();
1317+
while (file) {
1318+
1319+
if (file.isDirectory()) {
1320+
Serial.printf("[dir] %s\n", file.name());
1321+
} else {
1322+
Serial.printf("[file] %s (%d bytes)\n", file.name(), file.size());
1323+
}
1324+
1325+
// move to next file
1326+
file = root.openNextFile();
1327+
1328+
}
1329+
root.close();
1330+
}
1331+
1332+
} else if (memcmp(cli_command, "cat", 3) == 0) {
1333+
1334+
// get path from command e.g: "cat /contacts3"
1335+
const char *path = &cli_command[4];
1336+
1337+
// log file content as hex
1338+
File file = _store->openRead(path);
1339+
if(file){
1340+
1341+
// get file content
1342+
int file_size = file.available();
1343+
uint8_t buffer[file_size];
1344+
file.read(buffer, file_size);
1345+
1346+
// print hex
1347+
mesh::Utils::printHex(Serial, buffer, file_size);
1348+
Serial.print("\n");
1349+
1350+
file.close();
1351+
1352+
}
1353+
1354+
} else if (memcmp(cli_command, "rm ", 3) == 0) {
1355+
1356+
// get path from command e.g: "rm /adv_blobs"
1357+
const char *path = &cli_command[4];
1358+
1359+
// ensure path is not empty, or root dir
1360+
if(!path || strlen(path) == 0 || strcmp(path, "/") == 0){
1361+
Serial.println("Invalid path provided");
1362+
} else {
1363+
1364+
// remove file
1365+
bool removed = _store->removeFile(path);
1366+
if(removed){
1367+
Serial.println("File removed");
1368+
} else {
1369+
Serial.println("Failed to remove file");
1370+
}
1371+
1372+
}
1373+
13081374
} else if (strcmp(cli_command, "reboot") == 0) {
13091375
board.reboot(); // doesn't return
13101376
} else {

0 commit comments

Comments
 (0)