2021-04-12 19:20:15 +02:00
|
|
|
#ifndef ENS_FS_H
|
|
|
|
#define ENS_FS_H
|
2021-04-12 19:04:46 +02:00
|
|
|
|
|
|
|
#include <device.h>
|
2021-04-25 21:59:12 +02:00
|
|
|
#include <fs/fs.h>
|
2021-04-12 19:04:46 +02:00
|
|
|
#include <kernel.h>
|
2021-04-25 21:59:12 +02:00
|
|
|
#include <stdint.h>
|
2021-04-12 19:04:46 +02:00
|
|
|
#include <storage/flash_map.h>
|
|
|
|
|
2022-05-27 21:52:29 +02:00
|
|
|
#define ENS_INTERR 1 // internal error
|
|
|
|
#define ENS_NOENT 2 // entry not found or invalid
|
|
|
|
#define ENS_DELENT 3 // entry got deleted
|
|
|
|
#define ENS_ADDRINU 4 // address alread in use or corrupt
|
|
|
|
#define ENS_INVARG 5 // invalid argument
|
|
|
|
|
2021-04-12 19:04:46 +02:00
|
|
|
typedef struct ens_fs {
|
|
|
|
/**
|
|
|
|
* Flash area for this file system.
|
|
|
|
*/
|
|
|
|
const struct flash_area* area;
|
|
|
|
/**
|
2021-04-25 21:59:12 +02:00
|
|
|
* Size of each individual entry. The last byte will be used by
|
|
|
|
* ens_fs to store metadata about each individual entry.
|
|
|
|
*
|
2021-04-14 18:47:55 +02:00
|
|
|
* @attention has to be multiple of drivers write size
|
2021-04-12 19:04:46 +02:00
|
|
|
*/
|
|
|
|
size_t entry_size;
|
|
|
|
/**
|
|
|
|
* Size of each sector of this fs.
|
|
|
|
*/
|
|
|
|
uint16_t sector_size;
|
|
|
|
/**
|
|
|
|
* Amount of sectors in this fs.
|
|
|
|
*/
|
|
|
|
uint16_t sector_count;
|
2021-05-02 22:09:02 +02:00
|
|
|
/**
|
|
|
|
* Lock for this fs.
|
|
|
|
*/
|
|
|
|
struct k_mutex ens_fs_lock;
|
2021-05-14 00:31:14 +02:00
|
|
|
/**
|
|
|
|
* Size for entries, which is used interally.
|
|
|
|
*
|
|
|
|
* usually 2^(ceil(log(entry_size + 1)))
|
|
|
|
*/
|
|
|
|
// TODO lome: maybe introduce macro for this?
|
|
|
|
size_t interal_size;
|
|
|
|
/**
|
|
|
|
* Buffer of size internal_size for performing actions regarding fs entries. This is needed, because internal_size
|
|
|
|
* differs from entry_size and we need a buffer for working with our interal datatype.
|
|
|
|
*/
|
|
|
|
// TODO lome: maybe move this into functions where it's needed?
|
|
|
|
uint8_t* buffer;
|
2021-04-12 19:04:46 +02:00
|
|
|
} ens_fs_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the file system.
|
2021-04-25 21:59:12 +02:00
|
|
|
*
|
2021-04-12 19:04:46 +02:00
|
|
|
* @param fs file system
|
|
|
|
* @param id id of the partition
|
|
|
|
* @param size of each entry in the file-system. Has to power of 2
|
|
|
|
*
|
|
|
|
* @return 0 on success, -errno otherwise
|
|
|
|
*/
|
2021-05-02 21:50:13 +02:00
|
|
|
int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size);
|
2021-04-12 19:04:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an entry from this file system.
|
|
|
|
*
|
|
|
|
* @param fs file system
|
|
|
|
* @param id id of entry
|
|
|
|
* @param data destination address of data to be read
|
|
|
|
*
|
|
|
|
* @return 0 on success, -errno otherwise
|
|
|
|
*/
|
2021-05-02 21:50:13 +02:00
|
|
|
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest);
|
2021-04-12 19:04:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data to the file system.
|
|
|
|
*
|
|
|
|
* @param fs file system
|
|
|
|
* @param id id of the entry
|
|
|
|
* @param data data to be written
|
|
|
|
*
|
|
|
|
* @return 0 on success, -errno otherwise
|
|
|
|
*/
|
|
|
|
int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an entry from the file system.
|
|
|
|
*
|
|
|
|
* @param fs file system
|
|
|
|
* @param id id of the entry to be deleted
|
|
|
|
*
|
|
|
|
* @return 0 on success, -errno otherwise
|
|
|
|
*/
|
|
|
|
int ens_fs_delete(ens_fs_t* fs, uint64_t id);
|
|
|
|
|
|
|
|
/**
|
2021-05-17 19:16:47 +02:00
|
|
|
* Reqeust some free space in flash. Returns -ENS_INVARG, if the given id is not at the start of a page.
|
2021-04-12 19:04:46 +02:00
|
|
|
*
|
|
|
|
* @param fs file system
|
2021-05-17 19:16:47 +02:00
|
|
|
* @param id id of the entry to make space for
|
2021-04-25 21:59:12 +02:00
|
|
|
*
|
2021-05-17 19:16:47 +02:00
|
|
|
* @return the positive amount of deleted entries, -errno otherwise
|
2021-04-12 19:04:46 +02:00
|
|
|
*/
|
2021-05-17 19:16:47 +02:00
|
|
|
uint64_t ens_fs_make_space(ens_fs_t* fs, uint64_t id);
|
2021-04-12 19:04:46 +02:00
|
|
|
|
|
|
|
#endif
|