mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2025-01-08 04:14:25 +01:00
Apply suggestions for ens_fs
This commit is contained in:
parent
96eef185c9
commit
b729d794bf
@ -3,6 +3,7 @@
|
||||
|
||||
#define ENS_INTERR 1 // internal error
|
||||
#define ENS_NOENT 2 // entry not found or invalid
|
||||
#define ENS_INVARG 3 // invalid argument
|
||||
#define ENS_DELENT 3 // entry got deleted
|
||||
#define ENS_INVARG 4 // invalid argument
|
||||
|
||||
#endif
|
@ -8,8 +8,13 @@
|
||||
|
||||
#define SEED 42
|
||||
|
||||
int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) {
|
||||
if (flash_area_open(id, &fs->area)) {
|
||||
// mask for metadata
|
||||
#define CRC_MASK 254
|
||||
|
||||
#define GET_CHECKSUM(x) (x&CRC_MASK)
|
||||
|
||||
int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size) {
|
||||
if (flash_area_open(flash_id, &fs->area)) {
|
||||
// opening of flash area was not successful
|
||||
return -ENS_INTERR;
|
||||
}
|
||||
@ -35,21 +40,33 @@ int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
|
||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
|
||||
// read the entry from flash
|
||||
uint64_t offset = id * fs->entry_size;
|
||||
if (flash_area_read(fs->area, offset, dist, fs->entry_size)) {
|
||||
if (flash_area_read(fs->area, offset, dest, fs->entry_size)) {
|
||||
// opening of flash area was not successful
|
||||
return -ENS_INTERR;
|
||||
}
|
||||
|
||||
// check, if checksum and not-deleted flag are as expected
|
||||
uint8_t* obj = dist;
|
||||
uint8_t metadata = crc7_be(SEED, obj, fs->entry_size - 1) | 1;
|
||||
int isInvalid = memcmp(&obj[fs->entry_size - 1], &metadata, 1);
|
||||
uint8_t* obj = dest;
|
||||
|
||||
int isNotDeleted = obj[fs->entry_size - 1] & 1;
|
||||
if(!isNotDeleted) {
|
||||
// entry got deleted
|
||||
return -ENS_DELENT;
|
||||
}
|
||||
|
||||
// crc stored in entry
|
||||
uint8_t entryCRC = GET_CHECKSUM(obj[fs->entry_size - 1]);
|
||||
|
||||
// calculated crc
|
||||
uint8_t checkCRC = crc7_be(SEED, obj, fs->entry_size - 1);
|
||||
|
||||
int isInvalid = memcmp(&entryCRC, &checkCRC, 1);
|
||||
if (isInvalid) {
|
||||
// if checksum is not equal to calculated checksum or if deleted flag is not 1, set memory to 0
|
||||
memset(dist, 0, fs->entry_size);
|
||||
memset(dest, 0, fs->entry_size);
|
||||
// we do not know, if object got deleted or data is corrupt
|
||||
return -ENS_NOENT;
|
||||
}
|
||||
@ -90,9 +107,9 @@ int ens_fs_delete(ens_fs_t* fs, uint64_t id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count) {
|
||||
// calculate the next page start before (or at) the given offset
|
||||
uint64_t start = (offset - offset % fs->sector_size) * fs->entry_size;
|
||||
int ens_fs_page_erase(ens_fs_t* fs, uint64_t entry_id, uint64_t sector_count) {
|
||||
// calculate the next page start before (or at) the given entry_id
|
||||
uint64_t start = (entry_id - entry_id % fs->sector_size) * fs->entry_size;
|
||||
|
||||
// erase given amount of pages, starting for the given offset
|
||||
if (flash_area_erase(fs->area, start, fs->sector_size * sector_count)) {
|
||||
|
@ -27,10 +27,6 @@ typedef struct ens_fs {
|
||||
* Amount of sectors in this fs.
|
||||
*/
|
||||
uint16_t sector_count;
|
||||
/**
|
||||
* Lock for this fs.
|
||||
*/
|
||||
// struct k_mutex* lock;
|
||||
} ens_fs_t;
|
||||
|
||||
/**
|
||||
@ -42,7 +38,7 @@ typedef struct ens_fs {
|
||||
*
|
||||
* @return 0 on success, -errno otherwise
|
||||
*/
|
||||
int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size);
|
||||
int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size);
|
||||
|
||||
/**
|
||||
* Read an entry from this file system.
|
||||
@ -53,7 +49,7 @@ int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size);
|
||||
*
|
||||
* @return 0 on success, -errno otherwise
|
||||
*/
|
||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist);
|
||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest);
|
||||
|
||||
/**
|
||||
* Write data to the file system.
|
||||
|
Loading…
x
Reference in New Issue
Block a user