mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2025-01-09 12:44:24 +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 <drivers/flash.h>
|
||||||
|
#include <kernel.h>
|
||||||
#include <storage/flash_map.h>
|
#include <storage/flash_map.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/crc.h>
|
#include <sys/crc.h>
|
||||||
@ -37,15 +38,21 @@ int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size) {
|
|||||||
}
|
}
|
||||||
fs->sector_size = info.size;
|
fs->sector_size = info.size;
|
||||||
|
|
||||||
|
k_mutex_init(&fs->ens_fs_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
|
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
|
// read the entry from flash
|
||||||
uint64_t offset = id * fs->entry_size;
|
uint64_t offset = id * fs->entry_size;
|
||||||
|
|
||||||
if (flash_area_read(fs->area, offset, dest, fs->entry_size)) {
|
if (flash_area_read(fs->area, offset, dest, fs->entry_size)) {
|
||||||
// opening of flash area was not successful
|
// 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
|
// check, if checksum and not-deleted flag are as expected
|
||||||
@ -54,7 +61,8 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
|
|||||||
int isNotDeleted = obj[fs->entry_size - 1] & 1;
|
int isNotDeleted = obj[fs->entry_size - 1] & 1;
|
||||||
if (!isNotDeleted) {
|
if (!isNotDeleted) {
|
||||||
// entry got deleted
|
// entry got deleted
|
||||||
return -ENS_DELENT;
|
rc = -ENS_DELENT;
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// crc stored in entry
|
// 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
|
// 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);
|
memset(dest, 0, fs->entry_size);
|
||||||
// we do not know, if object got deleted or data is corrupt
|
// 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 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
|
// set CRC and not-deleted-flag
|
||||||
uint8_t* obj = data;
|
uint8_t* obj = data;
|
||||||
obj[fs->entry_size - 1] = crc7_be(SEED, obj, fs->entry_size - 1) | 1;
|
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;
|
uint64_t offset = id * fs->entry_size;
|
||||||
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
||||||
// writing to flash was not successful
|
// 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 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];
|
uint8_t data[fs->entry_size];
|
||||||
|
|
||||||
uint64_t offset = id * fs->entry_size;
|
uint64_t offset = id * fs->entry_size;
|
||||||
if (flash_area_read(fs->area, offset, data, fs->entry_size)) {
|
if (flash_area_read(fs->area, offset, data, fs->entry_size)) {
|
||||||
// reading was not successful
|
// reading was not successful
|
||||||
return -ENS_INTERR;
|
rc = -ENS_INTERR;
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set memory to 0, so not-deleted flag is 0
|
// set memory to 0, so not-deleted flag is 0
|
||||||
memset(data, 0, fs->entry_size);
|
memset(data, 0, fs->entry_size);
|
||||||
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
|
||||||
// writing was not successful
|
// 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 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
|
// 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;
|
uint64_t start = (entry_id - entry_id % fs->sector_size) * fs->entry_size;
|
||||||
|
|
||||||
// erase given amount of pages, starting for the given offset
|
// erase given amount of pages, starting for the given offset
|
||||||
if (flash_area_erase(fs->area, start, fs->sector_size * sector_count)) {
|
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.
|
* Amount of sectors in this fs.
|
||||||
*/
|
*/
|
||||||
uint16_t sector_count;
|
uint16_t sector_count;
|
||||||
|
/**
|
||||||
|
* Lock for this fs.
|
||||||
|
*/
|
||||||
|
struct k_mutex ens_fs_lock;
|
||||||
} ens_fs_t;
|
} 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).
|
* 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 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
|
* @param sector_count count of sectors to erase
|
||||||
*
|
*
|
||||||
* @return 0 on success, -errno otherwise
|
* @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
|
#endif
|
Loading…
x
Reference in New Issue
Block a user