-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LFS Telecommand to list files / directories [Issue #7] #216
base: main
Are you sure you want to change the base?
Changes from 18 commits
fcb05fc
a44e5ac
ae68581
159e961
3022e02
3c8b3ac
8259399
a132979
60eb9ee
477a97e
4da1c31
70bea08
e72168f
9ef3de5
95bea18
4d44129
00a0ae9
b33e043
7970e94
7f1e54f
1b2d053
4bbf381
80631bc
faa3def
2834e27
e88f624
913278c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
|
||
|
||
/*-----------------------------INCLUDES-----------------------------*/ | ||
#include <stdint.h> | ||
|
||
|
@@ -39,7 +37,7 @@ struct lfs_config LFS_cfg = { | |
// block device configuration | ||
.read_size = FLASH_CHIP_PAGE_SIZE_BYTES, | ||
.prog_size = FLASH_CHIP_PAGE_SIZE_BYTES, | ||
.block_size = FLASH_CHIP_BLOCK_SIZE_BYTES, // FIXME: Clarify block Size 256KiB or 1KiB | ||
.block_size = FLASH_CHIP_BLOCK_SIZE_BYTES, | ||
.block_count = (FLASH_CHIP_SIZE_BYTES / FLASH_CHIP_BLOCK_SIZE_BYTES), | ||
.block_cycles = 100, // TODO: ASK ABOUT THIS (HOW FREQUENT ARE WE USING THE MODULE), | ||
.cache_size = FLASH_CHIP_PAGE_SIZE_BYTES, | ||
|
@@ -57,22 +55,21 @@ struct lfs_file_config LFS_file_cfg = { | |
|
||
// -----------------------------LITTLEFS FUNCTIONS----------------------------- | ||
|
||
|
||
/** | ||
* @brief Formats Memory Module so it can successfully mount LittleFS | ||
* @param None | ||
* @retval 0 on success, negative LFS error codes on failure | ||
*/ | ||
int8_t LFS_format() | ||
{ | ||
int8_t result = lfs_format(&LFS_filesystem, &LFS_cfg); | ||
if (result < 0) | ||
int8_t format_result = lfs_format(&LFS_filesystem, &LFS_cfg); | ||
if (format_result < 0) | ||
{ | ||
DEBUG_uart_print_str("Error formatting!\n"); | ||
return result; | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_CRITICAL, LOG_all_sinks_except(LOG_SINK_FILE), "Error formatting FLASH memory!"); | ||
return format_result; | ||
} | ||
|
||
DEBUG_uart_print_str("Formatting successful!\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_all_sinks_except(LOG_SINK_FILE), "FLASH Memory formatting successful!"); | ||
return 0; | ||
} | ||
|
||
|
@@ -83,20 +80,21 @@ int8_t LFS_format() | |
*/ | ||
int8_t LFS_mount() | ||
{ | ||
if (LFS_is_lfs_mounted) { | ||
DEBUG_uart_print_str("LittleFS already mounted!\n"); | ||
if (LFS_is_lfs_mounted) | ||
{ | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_all_sinks_except(LOG_SINK_FILE), "LittleFS already mounted!"); | ||
return 1; | ||
} | ||
|
||
// Variable to store status of LittleFS mounting | ||
int8_t mount_result = lfs_mount(&LFS_filesystem, &LFS_cfg); | ||
if (mount_result < 0) | ||
{ | ||
DEBUG_uart_print_str("Mounting unsuccessful\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_CRITICAL, LOG_all_sinks_except(LOG_SINK_FILE), "Error mounting LittleFS!"); | ||
return mount_result; | ||
} | ||
|
||
DEBUG_uart_print_str("Mounting successful\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_all_sinks_except(LOG_SINK_FILE), "LittleFS mounting successful!"); | ||
LFS_is_lfs_mounted = 1; | ||
return 0; | ||
} | ||
|
@@ -110,56 +108,80 @@ int8_t LFS_unmount() | |
{ | ||
if (!LFS_is_lfs_mounted) | ||
{ | ||
DEBUG_uart_print_str("LittleFS not mounted.\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_all_sinks_except(LOG_SINK_FILE), "LittleFS not mounted!"); | ||
return 1; | ||
} | ||
|
||
// Unmount LittleFS to release any resources used by LittleFS | ||
const int8_t unmount_result = lfs_unmount(&LFS_filesystem); | ||
if (unmount_result < 0) | ||
{ | ||
DEBUG_uart_print_str("Error un-mounting.\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_CRITICAL, LOG_all_sinks_except(LOG_SINK_FILE), "Error un-mounting LittleFS!"); | ||
return unmount_result; | ||
} | ||
|
||
DEBUG_uart_print_str("Successfully un-mounted LittleFS.\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_all_sinks_except(LOG_SINK_FILE), "LittleFS un-mounting successful!"); | ||
LFS_is_lfs_mounted = 0; | ||
return 0; | ||
} | ||
|
||
/** | ||
* @brief Lists contents of LittleFS Directory | ||
* @param root_directory Pointer to cstring holding the root directory to open and read | ||
* @param root_directory Pointer to cstring holding the root directory to open and read | ||
Saksham-P marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param offset Number of entries to skip before listing directory | ||
* @param count Number of entries to list in total (if 0, prints all entries) | ||
* @retval 0 on success, 1 if LFS is unmounted, negative LFS error codes on failure | ||
*/ | ||
int8_t LFS_list_directory(const char root_directory[]) | ||
int8_t LFS_list_directory(const char root_directory[], uint16_t offset, int16_t count) | ||
{ | ||
// Check if LFS is mounted | ||
if (!LFS_is_lfs_mounted) | ||
{ | ||
DEBUG_uart_print_str("LittleFS not mounted.\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_all_sinks_except(LOG_SINK_FILE), "LittleFS not mounted!"); | ||
return 1; | ||
} | ||
|
||
// Try to open the directory | ||
lfs_dir_t dir; | ||
int8_t open_dir_result = lfs_dir_open(&LFS_filesystem, &dir, root_directory); | ||
if (open_dir_result < 0) | ||
{ | ||
DEBUG_uart_print_str("Error opening a directory.\n"); | ||
LOG_message(LOG_SYSTEM_LFS, LOG_SEVERITY_CRITICAL, LOG_all_sinks_except(LOG_SINK_FILE), "Error opening directory: %s", root_directory); | ||
return open_dir_result; | ||
} | ||
|
||
// result is positive on success, 0 at the end of directory, or negative on failure. | ||
if (count == 0) { | ||
count = -1; | ||
} | ||
|
||
// result is positive on success, 0 at the end of directory, or negative on failure. | ||
int8_t read_dir_result = 1; | ||
while (read_dir_result >= 0) | ||
struct lfs_info info; | ||
DEBUG_uart_print_str("Name \t bytes\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is gonna be awfully difficult to read when the satellite's in space ;) Please use the logging function to display this info. |
||
while (read_dir_result > 0) | ||
Saksham-P marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
struct lfs_info info; | ||
read_dir_result = lfs_dir_read(&LFS_filesystem, &dir, &info); | ||
|
||
if (offset > 0) { | ||
offset--; | ||
continue; | ||
} | ||
|
||
if (count == 0) { | ||
break; | ||
} else { | ||
count--; | ||
} | ||
|
||
DEBUG_uart_print_str(info.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please switch all this to |
||
DEBUG_uart_print_str(", "); | ||
// TODO: The info struct contains information about directory contents | ||
if (info.type == LFS_TYPE_REG) | ||
{ | ||
DEBUG_uart_print_str("\t"); | ||
DEBUG_uart_print_uint32(info.size); | ||
DEBUG_uart_print_str(" bytes"); | ||
} | ||
DEBUG_uart_print_str("\n"); | ||
} | ||
DEBUG_uart_print_str("\n"); | ||
|
||
if (read_dir_result < 0) | ||
{ | ||
|
@@ -180,54 +202,62 @@ int8_t LFS_list_directory(const char root_directory[]) | |
} | ||
|
||
/** | ||
* @brief Removes / deletes the file specified | ||
* @param file_name Pointer to cstring holding the file name to remove | ||
* @brief Creates directory | ||
* @param dir_name Pointer to cstring holding the name of the directory | ||
* @retval 0 on success, 1 if LFS is unmounted, negative LFS error codes on failure | ||
*/ | ||
int8_t LFS_delete_file(const char file_name[]) | ||
int8_t LFS_make_directory(const char dir_name[]) | ||
{ | ||
if (!LFS_is_lfs_mounted) | ||
{ | ||
DEBUG_uart_print_str("LittleFS not mounted.\n"); | ||
return 1; | ||
} | ||
|
||
int8_t remove_result = lfs_remove(&LFS_filesystem, file_name); | ||
if (remove_result < 0) | ||
const int8_t make_dir_result = lfs_mkdir(&LFS_filesystem, dir_name); | ||
if (make_dir_result < 0) | ||
{ | ||
DEBUG_uart_print_str("Error removing file/directory.\n"); | ||
return remove_result; | ||
if (make_dir_result == LFS_ERR_EXIST) { | ||
LOG_message( | ||
LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_SINK_ALL, | ||
"Directory %d already exists.", | ||
make_dir_result | ||
); | ||
} else { | ||
LOG_message( | ||
LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_SINK_ALL, | ||
"Error %d creating directory.", | ||
make_dir_result | ||
); | ||
} | ||
return make_dir_result; | ||
} | ||
|
||
DEBUG_uart_print_str("Successfully removed file/directory.\n"); | ||
DEBUG_uart_print_str("Successfully created directory.\n"); | ||
return 0; | ||
} | ||
|
||
/** | ||
* @brief Creates directory | ||
* @param dir_name Pointer to cstring holding the name of the directory | ||
* @brief Removes / deletes the file specified | ||
* @param file_name Pointer to cstring holding the file name to remove | ||
* @retval 0 on success, 1 if LFS is unmounted, negative LFS error codes on failure | ||
*/ | ||
int8_t LFS_make_directory(const char dir_name[]) | ||
int8_t LFS_delete_file(const char file_name[]) | ||
NuclearTea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (!LFS_is_lfs_mounted) | ||
{ | ||
DEBUG_uart_print_str("LittleFS not mounted.\n"); | ||
return 1; | ||
} | ||
|
||
const int8_t make_dir_result = lfs_mkdir(&LFS_filesystem, dir_name); | ||
if (make_dir_result < 0) | ||
int8_t remove_result = lfs_remove(&LFS_filesystem, file_name); | ||
if (remove_result < 0) | ||
{ | ||
LOG_message( | ||
LOG_SYSTEM_LFS, LOG_SEVERITY_WARNING, LOG_SINK_ALL, | ||
"Error %d creating directory.", | ||
make_dir_result | ||
); | ||
return make_dir_result; | ||
DEBUG_uart_print_str("Error removing file/directory.\n"); | ||
return remove_result; | ||
} | ||
|
||
DEBUG_uart_print_str("Successfully created directory.\n"); | ||
DEBUG_uart_print_str("Successfully removed file/directory.\n"); | ||
return 0; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please convert this file's indentation to use spaces instead of tabs. There's a few places it's mixed in this repo, but in general, we prefer spaces. Ideally, never mix in the same file.