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

Fix multiple results of the PR:

- cleanup of sequence number utilites
- dynamic calculation of nvs sectors for fs information
- usage of buffer in ens_fs
- check for max entry size (page size)
- usage of interal entry size, which is always the next larger power of 2 to a given entry size
This commit is contained in:
H1ghBre4k3r 2021-05-14 00:31:14 +02:00 committed by Patrick Rathje
parent 3abbc61757
commit 796cf0f089
8 changed files with 105 additions and 60 deletions

View File

@ -1,5 +1,6 @@
#include <drivers/flash.h>
#include <kernel.h>
#include <math.h>
#include <storage/flash_map.h>
#include <string.h>
#include <sys/crc.h>
@ -20,13 +21,6 @@ int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size) {
return -ENS_INTERR;
}
// check, if entry size is multiple of flash_area_align
if ((entry_size % flash_area_align(fs->area)) != 0) {
flash_area_close(fs->area);
return -ENS_INVARG;
}
fs->entry_size = entry_size;
// get all information needed for the needed for the fs
const struct device* dev = flash_area_get_device(fs->area);
fs->sector_count = flash_get_page_count(dev);
@ -38,6 +32,24 @@ int ens_fs_init(ens_fs_t* fs, uint8_t flash_id, uint64_t entry_size) {
}
fs->sector_size = info.size;
// check, if needed internal size fits onto one page
uint64_t internal_size = pow(2, ceil(log(entry_size + 1) / log(2)));
if (internal_size > info.size) {
flash_area_close(fs->area);
return -ENS_INVARG;
}
fs->entry_size = entry_size;
fs->interal_size = internal_size;
// allocate buffer and set it to 0
void* ptr = k_malloc(internal_size);
if (ptr == NULL) {
flash_area_close(fs->area);
return -ENS_INTERR;
}
memset(ptr, 0, internal_size);
// init the lock for the fs
k_mutex_init(&fs->ens_fs_lock);
return 0;
}
@ -47,39 +59,41 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dest) {
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)) {
uint64_t offset = id * fs->interal_size;
if (flash_area_read(fs->area, offset, fs->buffer, fs->interal_size)) {
// opening of flash area was not successful
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) {
// entry got deleted
rc = -ENS_DELENT;
goto end;
}
// store pointer to buffer in variable, so we don't have to write (fs->buffer) everytime
uint8_t* obj = fs->buffer;
// crc stored in entry
uint8_t entryCRC = GET_CHECKSUM(obj[fs->entry_size - 1]);
uint8_t entryCRC = GET_CHECKSUM(obj[fs->entry_size]);
// calculated crc
uint8_t checkCRC = crc7_be(SEED, obj, fs->entry_size - 1);
uint8_t checkCRC = crc7_be(SEED, obj, fs->entry_size);
// check, if the entry is corrupted
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(dest, 0, fs->entry_size);
memset(obj, 0, fs->interal_size);
// we do not know, if object got deleted or data is corrupt
rc = -ENS_NOENT;
goto end;
}
// check, if checksum and not-deleted flag are as expected
int isNotDeleted = obj[fs->entry_size] & 1;
if (!isNotDeleted) {
// entry got deleted
rc = -ENS_DELENT;
}
end:
memcpy(dest, obj, fs->entry_size);
k_mutex_unlock(&fs->ens_fs_lock);
return rc;
}
@ -88,12 +102,15 @@ 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;
// copy data into interal buffer
memcpy(fs->buffer, data, fs->entry_size);
uint64_t offset = id * fs->entry_size;
if (flash_area_write(fs->area, offset, data, fs->entry_size)) {
// set CRC and not-deleted-flag
uint8_t* obj = fs->buffer;
obj[fs->entry_size] = crc7_be(SEED, obj, fs->entry_size) | 1;
uint64_t offset = id * fs->interal_size;
if (flash_area_write(fs->area, offset, data, fs->interal_size)) {
// writing to flash was not successful
rc = -ENS_INTERR;
}
@ -105,24 +122,15 @@ int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data) {
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
rc = -ENS_INTERR;
goto end;
}
uint64_t offset = id * fs->interal_size;
// 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)) {
memset(fs->buffer, 0, fs->entry_size);
if (flash_area_write(fs->area, offset, fs->buffer, fs->entry_size)) {
// writing was not successful
rc = -ENS_INTERR;
}
end:
k_mutex_unlock(&fs->ens_fs_lock);
return rc;
}

View File

@ -31,6 +31,19 @@ typedef struct ens_fs {
* Lock for this fs.
*/
struct k_mutex ens_fs_lock;
/**
* 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;
} ens_fs_t;
/**

View File

@ -64,6 +64,8 @@ int find_record_via_binary_search(record_t* record,
return 0;
}
// TODO: This iterator does neither check if the sequence numbers wrapped around while iteration. As a result, first
// results could have later timestamps than following entries
int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* ts_start, uint32_t* ts_end) {
record_sequence_number_t oldest_sn = get_oldest_sequence_number();
record_sequence_number_t latest_sn = get_latest_sequence_number();

View File

@ -1,9 +1,28 @@
#include "sequencenumber.h"
#define SN_MASK 0xffffff
/**
* Mask a given sequence number to get rid of MSB.
*/
#define GET_MASKED_SN(x) (x & SN_MASK)
int sn_equal(record_sequence_number_t a, record_sequence_number_t b) {
return GET_MASKED_SN(a) == GET_MASKED_SN(b);
}
record_sequence_number_t sn_increment(record_sequence_number_t sn) {
return GET_MASKED_SN(++sn);
}
record_sequence_number_t sn_increment_by(record_sequence_number_t sn, uint32_t amount) {
return GET_MASKED_SN((sn + amount));
}
record_sequence_number_t sn_get_middle_sn(record_sequence_number_t older, record_sequence_number_t newer) {
if (older < newer) {
return (older + newer) / 2;
}
// TODO lome: cover case for older > newer
}

View File

@ -3,14 +3,6 @@
#include <stdint.h>
// mask for sequence numbers (2^24 - 1)
#define SN_MASK 16777215
/**
* Mask a given sequence number to get rid of MSB.
*/
#define GET_MASKED_SN(x) (x & SN_MASK)
typedef uint32_t record_sequence_number_t;
/**
@ -26,7 +18,24 @@ int sn_equal(record_sequence_number_t a, record_sequence_number_t b);
* Increment the given sequence number. Wraps around, if 2^24 is reached.
*
* @param sn sequence number to increment
* @return the incremented sequenced number
* @return the incremented sequence number
*/
record_sequence_number_t sn_increment(record_sequence_number_t sn);
/**
* Increment the given sequence number by a given amount.
*
* @param sn sequence number to increment
* @return the incremented sequence number
*/
record_sequence_number_t sn_increment_by(record_sequence_number_t sn, uint32_t amount);
/**
* Get the middle between to given sequence numbers, while handling a possible wrap-around.
*
* @param older sequence number which will be treated as the older one
* @param newer sequence number which will be treated as the newer one
* @return the sequence number in the middle
*/
record_sequence_number_t sn_get_middle_sn(record_sequence_number_t older, record_sequence_number_t newer);
#endif

View File

@ -11,9 +11,6 @@
#include "sequencenumber.h"
#include "storage.h"
// Maybe use this as param for init function
#define SEC_COUNT 8U
#define STORED_CONTACTS_INFO_ID 0
#define MAX_CONTACTS 65536
@ -89,7 +86,7 @@ int init_record_storage(void) {
return rc;
}
info_fs.sector_size = info.size;
info_fs.sector_count = SEC_COUNT;
info_fs.sector_count = FLASH_AREA_SIZE(storage) / info.size;
rc = nvs_init(&info_fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
k_mutex_init(&info_fs_lock);
@ -141,8 +138,7 @@ int add_record(record_t* src) {
if (get_num_records() == MAX_CONTACTS) {
int deletedRecordsCount = (ens_fs.sector_size / ens_fs.entry_size) * sectorsToDelete;
record_information.count -= deletedRecordsCount;
record_information.oldest_contact =
GET_MASKED_SN((record_information.oldest_contact + deletedRecordsCount));
record_information.oldest_contact = sn_increment_by(record_information.oldest_contact, deletedRecordsCount);
save_storage_information();
}
}
@ -173,7 +169,7 @@ int delete_record(record_sequence_number_t sn) {
// TODO lome: do we need lock here aswell?
record_sequence_number_t get_latest_sequence_number() {
return GET_MASKED_SN((record_information.oldest_contact + record_information.count));
return sn_increment_by(record_information.oldest_contact, record_information.count);
}
record_sequence_number_t get_oldest_sequence_number() {

View File

@ -14,10 +14,6 @@ typedef struct record {
rssi_t rssi; // TODO: Check correct
rolling_proximity_identifier_t rolling_proximity_identifier;
associated_encrypted_metadata_t associated_encrypted_metadata;
/**
* Filler space, used for storing up to 3 byte of metadata in the filesystem.
*/
uint8_t meta[3];
} record_t;
typedef struct stored_records_information {

View File

@ -30,9 +30,11 @@ CONFIG_MAIN_STACK_SIZE=2048
# Let __ASSERT do its job
CONFIG_DEBUG=y
CONFIG_LOG=y
# Need this when storage is on MX25R64
CONFIG_NORDIC_QSPI_NOR=y
CONFIG_NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_NEWLIB_LIBC=y
CONFIG_HEAP_MEM_POOL_SIZE=1024