Files
buzzer_2/firmware/libs/fs_mgmt/include/fs_mgmt.h

158 lines
6.4 KiB
C

#ifndef FS_MGMT_H
#define FS_MGMT_H
#include <zephyr/fs/fs.h>
#include "buzz_proto.h"
#define FS_AUDIO_PATH CONFIG_FS_MGMT_MOUNT_POINT CONFIG_FS_MGMT_AUDIO_SUBDIR
#define FS_SYSTEM_PATH CONFIG_FS_MGMT_MOUNT_POINT CONFIG_FS_MGMT_SYSTEM_SUBDIR
#define MAX_FILE_NAME_LEN(target, path) \
((int)(sizeof(target) - (sizeof(path)) - 1))
/** @brief Assemble a full path from a base path and a filename
* Ensures that the resulting path fits into the target buffer and is null-terminated. If the filename is too long, it will be truncated to fit.
* @param buffer Target buffer to hold the assembled path
* @param path Base path (e.g. "/sys" or "/audio")
* @param filename Name of the file to append to the base path
*/
#define FS_MGMT_ASSEMBLE_PATH(buffer, path, filename) \
snprintf(buffer, sizeof(buffer), \
"%s/%.*s", \
path, \
MAX_FILE_NAME_LEN(buffer, path), \
filename)
/**
* @brief OP-Codes for the FS write thread
*/
enum fs_write_op
{
FS_WRITE_OP_FILE_START,
FS_WRITE_OP_FILE_CHUNK,
FS_WRITE_OP_FILE_END,
FS_WRITE_OP_TAGS_START, // Schon mal vorgesehen
FS_WRITE_OP_FW_START, // Schon mal vorgesehen
FS_WRITE_OP_ABORT
};
/**
* @brief Structure representing a write message for the FS write thread
*/
struct fs_write_msg
{
enum fs_write_op op;
uint8_t *slab_ptr; /* Basis-Pointer des Memory-Slabs (für k_mem_slab_free) */
uint16_t data_offset; /* Offset ab dem slab_ptr, wo die Nutzdaten beginnen */
uint16_t data_len; /* Länge der Nutzdaten */
uint32_t metadata; /* Zusatzinfo (Start: erwartete Dateigröße, End: erwartete CRC32) */
buzz_transport_reply_fn reply_cb; /* Callback für ACKs / Success / Error */
};
/**
* @brief Wrapper around fs_open that handles power management for the flash
* Resumes the flash before opening and suspends it if opening fails
* @param file Pointer to fs_file_t structure to be initialized
* @param path Path to the file to open
* @param mode Open flags (e.g. FS_O_READ, FS_O_WRITE)
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_open(struct fs_file_t *file, const char *path, fs_mode_t mode);
/**
* @brief Wrapper around fs_close that handles power management for the flash
* Resumes the flash after closing and suspends it if closing fails
* @param file Pointer to fs_file_t structure to be closed
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_close(struct fs_file_t *file);
/**
* @brief Wrapper around fs_opendir that handles power management for the flash
* Resumes the flash before opening and suspends it if opening fails
* @param dirp Pointer to fs_dir_t structure to be initialized
* @param path Path to the directory to open
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_opendir(struct fs_dir_t *dirp, const char *path);
/**
* @brief Wrapper around fs_closedir that handles power management for the flash
* Resumes the flash after closing and suspends it if closing fails
* @param dirp Pointer to fs_dir_t structure to be closed
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_closedir(struct fs_dir_t *dirp);
/**
* @brief Unlinks (deletes) a file, ensuring the flash is active during the operation
* @param path Path to the file to unlink
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_unlink(const char *path);
/**
* @brief Wrapper around fs_statvfs that handles power management for the flash
* Resumes the flash before getting stats and suspends it afterwards
* @param path Path to the filesystem to get stats for
* @param stat Pointer to fs_statvfs structure to be filled with stats
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_statvfs(const char *path, struct fs_statvfs *stat);
/**
* @brief Wrapper around fs_stat that handles power management for the flash
* Resumes the flash before stat and suspends it afterwards
* @param path Path to file or directory
* @param entry Pointer to fs_dirent structure to receive metadata
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_stat(const char *path, struct fs_dirent *entry);
/**
* @brief Wrapper around fs_mkdir that handles power management for the flash
* Resumes the flash before creating the directory and suspends it afterwards
* @param path Path to the directory to create
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_mkdir(const char *path);
/**
* @brief Wrapper around fs_rename that handles power management for the flash
* Resumes the flash before renaming and suspends it afterwards
* @param old_path Current path of the file or directory
* @param new_path New path for the file or directory
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_rename(const char *old_path, const char *new_path);
/**
* @brief Recursively creates directories for the given path, ensuring the flash is active during the operation
* @param path Path to the directory to create (can include multiple levels, e.g. "/dir1/dir2/dir3")
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_mkdir_recursive(char *path);
/**
* @brief Recursively removes a directory and all its contents, ensuring the flash is active during the operation
* @param path Path to the directory to remove
* @param max_len Maximum length of the path buffer
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_pm_rm_recursive(char *path, size_t max_len);
/**
* @brief Gets the length of the audio data in a file, accounting for any metadata tags, ensuring the flash is active during the operation
* @param fp Pointer to an open fs_file_t structure representing the file
* @return Length of the audio data on success, negative error code on failure
*/
ssize_t fs_mgmt_get_audio_data_len(struct fs_file_t *fp);
/**
* @brief Submits a write message to the FS write thread, which will handle writing data to the filestem asynchronously, ensuring the flash is active during the operation
* @param msg Pointer to the fs_write_msg structure containing the write operation details
* @return 0 on success, negative error code on failure
*/
int fs_mgmt_submit_write(struct fs_write_msg *msg);
#endif /* FS_MGMT_H */