mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2025-01-08 04:14:25 +01:00
Use lock for ens_fs
This commit is contained in:
parent
b729d794bf
commit
6b6a40d383
@ -1,4 +1,5 @@
|
||||
#include <drivers/flash.h>
|
||||
#include <kernel.h>
|
||||
#include <storage/flash_map.h>
|
||||
#include <string.h>
|
||||
#include <sys/crc.h>
|
||||
@ -11,7 +12,7 @@
|
||||
// mask for metadata
|
||||
#define CRC_MASK 254
|
||||
|
||||
#define GET_CHECKSUM(x) (x&CRC_MASK)
|
||||
#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)) {
|
||||
@ -37,24 +38,31 @@ int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size) {
|
||||
}
|
||||
fs->sector_size = info.size;
|
||||
|
||||
k_mutex_init(&fs->ens_fs_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
|
||||
int rc = 0;
|
||||
k_mutex_lock(&fs->ens_fs_lock, K_FOREVER);
|
||||
|
||||
// read the entry from flash
|
||||
uint64_t offset = id * fs->entry_size;
|
||||
|
||||
if (flash_area_read(fs->area, offset, dest, fs->entry_size)) {
|
||||
// opening of flash area was not successful
|
||||
return -ENS_INTERR;
|
||||
rc = -ENS_INTERR;
|
||||
goto end;
|
||||
}
|
||||
|
||||
// check, if checksum and not-deleted flag are as expected
|
||||
uint8_t* obj = dest;
|
||||
|
||||
|
||||
int isNotDeleted = obj[fs->entry_size - 1] & 1;
|
||||
if(!isNotDeleted) {
|
||||
if (!isNotDeleted) {
|
||||
// entry got deleted
|
||||
return -ENS_DELENT;
|
||||
rc = -ENS_DELENT;
|
||||
goto end;
|
||||
}
|
||||
|
||||
// crc stored in entry
|
||||
@ -68,13 +76,18 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
|
||||
// if checksum is not equal to calculated checksum or if deleted flag is not 1, set memory to 0
|
||||
memset(dest, 0, fs->entry_size);
|
||||
// we do not know, if object got deleted or data is corrupt
|
||||
return -ENS_NOENT;
|
||||
rc = -ENS_NOENT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
end:
|
||||
k_mutex_unlock(&fs->ens_fs_lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data) {
|
||||
int rc = 0;
|
||||
k_mutex_lock(&fs->ens_fs_lock, K_FOREVER);
|
||||
|
||||
// set CRC and not-deleted-flag
|
||||
uint8_t* obj = data;
|
||||
obj[fs->entry_size - 1] = crc7_be(SEED, obj, fs->entry_size - 1) | 1;
|
||||
@ -82,39 +95,50 @@ int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data) {
|
||||
uint64_t offset = id * fs->entry_size;
|
||||
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
||||
// writing to flash was not successful
|
||||
return -ENS_INTERR;
|
||||
rc = -ENS_INTERR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
k_mutex_unlock(&fs->ens_fs_lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int ens_fs_delete(ens_fs_t* fs, uint64_t id) {
|
||||
int rc = 0;
|
||||
k_mutex_lock(&fs->ens_fs_lock, K_FOREVER);
|
||||
|
||||
uint8_t data[fs->entry_size];
|
||||
|
||||
uint64_t offset = id * fs->entry_size;
|
||||
if (flash_area_read(fs->area, offset, data, fs->entry_size)) {
|
||||
// reading was not successful
|
||||
return -ENS_INTERR;
|
||||
rc = -ENS_INTERR;
|
||||
goto end;
|
||||
}
|
||||
|
||||
// set memory to 0, so not-deleted flag is 0
|
||||
memset(data, 0, fs->entry_size);
|
||||
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
||||
// writing was not successful
|
||||
return -ENS_INTERR;
|
||||
rc = -ENS_INTERR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
end:
|
||||
k_mutex_unlock(&fs->ens_fs_lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int ens_fs_page_erase(ens_fs_t* fs, uint64_t entry_id, uint64_t sector_count) {
|
||||
int rc = 0;
|
||||
k_mutex_lock(&fs->ens_fs_lock, K_FOREVER);
|
||||
|
||||
// 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)) {
|
||||
return -ENS_INTERR;
|
||||
rc = -ENS_INTERR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
k_mutex_unlock(&fs->ens_fs_lock);
|
||||
return rc;
|
||||
}
|
||||
|
@ -27,6 +27,10 @@ typedef struct ens_fs {
|
||||
* Amount of sectors in this fs.
|
||||
*/
|
||||
uint16_t sector_count;
|
||||
/**
|
||||
* Lock for this fs.
|
||||
*/
|
||||
struct k_mutex ens_fs_lock;
|
||||
} ens_fs_t;
|
||||
|
||||
/**
|
||||
@ -76,11 +80,11 @@ int ens_fs_delete(ens_fs_t* fs, uint64_t id);
|
||||
* Erase a given amount of flash sectors. Starting at offset (if offset is not at page start, it will be rounded down).
|
||||
*
|
||||
* @param fs file system
|
||||
* @param offset offset to start erasing
|
||||
* @param id id of the entry to erase the page for
|
||||
* @param sector_count count of sectors to erase
|
||||
*
|
||||
* @return 0 on success, -errno otherwise
|
||||
*/
|
||||
int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count);
|
||||
int ens_fs_page_erase(ens_fs_t* fs, uint64_t id, uint64_t sector_count);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user