1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-12-05 00:55:43 +01:00

Implement correct read using crc and deleted flag and implement deletion

This commit is contained in:
H1ghBre4k3r 2021-04-14 18:47:55 +02:00 committed by Patrick Rathje
parent ca35b6a82c
commit 9ae226d50b
2 changed files with 38 additions and 13 deletions

View File

@ -1,6 +1,7 @@
#include <drivers/flash.h>
#include <storage/flash_map.h>
#include <sys/crc.h>
#include <string.h>
#include "ens_fs.h"
@ -9,17 +10,27 @@
int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) {
int rc = flash_area_open(id, &fs->area);
if (rc) {
return rc;
goto end;
}
// Check, if entry size is multiple of flash_area_align
if((entry_size % flash_area_align(fs->area)) != 0) {
flash_area_close(fs->area);
// TODO lome: set custom error code
rc = -1;
goto end;
}
fs->entry_size = entry_size;
const struct device* dev = flash_area_get_device(fs->area);
fs->sector_count = flash_get_page_count(dev);
struct flash_pages_info info;
rc = flash_get_page_info_by_offs(dev, fs->area->fa_off, &info);
fs->sector_size = info.size;
fs->entry_size = entry_size;
return 0;
end:
return rc;
}
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
@ -31,7 +42,14 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
goto end;
}
// TODO lome: Read deleted flag and CRC
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);
if(isInvalid) {
// If checksum is not equal to calculated checksum or if deleted flag is not 1, set memory to 0
rc = -1;
memset(dist, 0, fs->entry_size);
}
end:
return rc;
@ -55,15 +73,20 @@ end:
int ens_fs_delete(ens_fs_t* fs, uint64_t id) {
// TODO lome: Maybe overwrite last 4 bytes? Or make it file system dependend?
// uint8_t data = 0;
// uint64_t offset = (id + 1) * fs->entry_size - 1;
uint8_t data[fs->entry_size];
// int rc = flash_area_write(fs->area, offset, &data, 0);
// if(rc) {
// goto end;
// }
return 0;
uint64_t offset = id * fs->entry_size;
int rc = flash_area_read(fs->area, offset, data, fs->entry_size);
if(rc) {
goto end;
}
// set memory to 0, so not-deleted flag is 0
memset(data, 0, fs->entry_size);
rc = flash_area_write(fs->area, offset, data, fs->entry_size);
end:
return rc;
}
int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count) {

View File

@ -15,7 +15,9 @@ typedef struct ens_fs {
const struct flash_area* area;
/**
* Size of each individual entry. The last byte will be used by
* ens_fs to store metadata about each individual entry.
* ens_fs to store metadata about each individual entry.
*
* @attention has to be multiple of drivers write size
*/
size_t entry_size;
/**