mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2024-12-04 16:45:43 +01:00
Format ens files and add .clang-format file
This commit is contained in:
parent
522a2aab9d
commit
80cba23823
1
.clang-format
Normal file
1
.clang-format
Normal file
@ -0,0 +1 @@
|
|||||||
|
{ BasedOnStyle: Chromium, UseTab: Never, IndentWidth: 4, TabWidth: 4, ColumnLimit: 120 }
|
@ -1,8 +1,8 @@
|
|||||||
#ifndef ENS_ERROR_H
|
#ifndef ENS_ERROR_H
|
||||||
#define ENS_ERROR_H
|
#define ENS_ERROR_H
|
||||||
|
|
||||||
#define ENS_INTERR 1 // internal error
|
#define ENS_INTERR 1 // internal error
|
||||||
#define ENS_NOENT 2 // entry not found or invalid
|
#define ENS_NOENT 2 // entry not found or invalid
|
||||||
#define ENS_INVARG 3 // invalid argument
|
#define ENS_INVARG 3 // invalid argument
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,10 +1,10 @@
|
|||||||
#include <drivers/flash.h>
|
#include <drivers/flash.h>
|
||||||
#include <storage/flash_map.h>
|
#include <storage/flash_map.h>
|
||||||
#include <sys/crc.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/crc.h>
|
||||||
|
|
||||||
#include "ens_fs.h"
|
|
||||||
#include "ens_error.h"
|
#include "ens_error.h"
|
||||||
|
#include "ens_fs.h"
|
||||||
|
|
||||||
#define SEED 42
|
#define SEED 42
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check, if entry size is multiple of flash_area_align
|
// check, if entry size is multiple of flash_area_align
|
||||||
if((entry_size % flash_area_align(fs->area)) != 0) {
|
if ((entry_size % flash_area_align(fs->area)) != 0) {
|
||||||
flash_area_close(fs->area);
|
flash_area_close(fs->area);
|
||||||
return -ENS_INVARG;
|
return -ENS_INVARG;
|
||||||
}
|
}
|
||||||
@ -36,10 +36,9 @@ int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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* dist) {
|
||||||
|
|
||||||
// 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, dist, fs->entry_size)) {
|
if (flash_area_read(fs->area, offset, dist, fs->entry_size)) {
|
||||||
// opening of flash area was not successful
|
// opening of flash area was not successful
|
||||||
return -ENS_INTERR;
|
return -ENS_INTERR;
|
||||||
}
|
}
|
||||||
@ -48,7 +47,7 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
|
|||||||
uint8_t* obj = dist;
|
uint8_t* obj = dist;
|
||||||
uint8_t metadata = crc7_be(SEED, obj, fs->entry_size - 1) | 1;
|
uint8_t metadata = crc7_be(SEED, obj, fs->entry_size - 1) | 1;
|
||||||
int isInvalid = memcmp(&obj[fs->entry_size - 1], &metadata, 1);
|
int isInvalid = memcmp(&obj[fs->entry_size - 1], &metadata, 1);
|
||||||
if(isInvalid) {
|
if (isInvalid) {
|
||||||
// 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(dist, 0, fs->entry_size);
|
memset(dist, 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
|
||||||
@ -59,13 +58,12 @@ int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
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;
|
return -ENS_INTERR;
|
||||||
}
|
}
|
||||||
@ -74,18 +72,17 @@ 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 ens_fs_delete(ens_fs_t* fs, uint64_t id) {
|
||||||
|
|
||||||
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;
|
return -ENS_INTERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
return -ENS_INTERR;
|
||||||
}
|
}
|
||||||
@ -94,12 +91,11 @@ int ens_fs_delete(ens_fs_t* fs, uint64_t id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 offset, uint64_t sector_count) {
|
||||||
|
|
||||||
// calculate the next page start before (or at) the given offset
|
// calculate the next page start before (or at) the given offset
|
||||||
uint64_t start = (offset - offset % fs->sector_size) * fs->entry_size;
|
uint64_t start = (offset - offset % 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;
|
return -ENS_INTERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,14 +69,14 @@ int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* t
|
|||||||
// try to find the oldest contact in our timerange
|
// try to find the oldest contact in our timerange
|
||||||
record_t start_rec;
|
record_t start_rec;
|
||||||
int rc = load_contact(&start_rec, oldest_sn);
|
int rc = load_contact(&start_rec, oldest_sn);
|
||||||
if(rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
// if starting timestamp lies in our bounds, perform binary search
|
// if starting timestamp lies in our bounds, perform binary search
|
||||||
if(start_rec.timestamp < *ts_start) {
|
if (start_rec.timestamp < *ts_start) {
|
||||||
rc = find_record_via_binary_search(&start_rec, *ts_start, oldest_sn, latest_sn);
|
rc = find_record_via_binary_search(&start_rec, *ts_start, oldest_sn, latest_sn);
|
||||||
|
|
||||||
if(rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,14 +84,14 @@ int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* t
|
|||||||
// try to find the newest contact within out timerange
|
// try to find the newest contact within out timerange
|
||||||
record_t end_rec;
|
record_t end_rec;
|
||||||
rc = load_contact(&end_rec, latest_sn);
|
rc = load_contact(&end_rec, latest_sn);
|
||||||
if(rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
// if ending timestamp lies in our bounds, perform binary search
|
// if ending timestamp lies in our bounds, perform binary search
|
||||||
if(end_rec.timestamp > *ts_end) {
|
if (end_rec.timestamp > *ts_end) {
|
||||||
rc = find_record_via_binary_search(&end_rec, *ts_end, oldest_sn, latest_sn);
|
rc = find_record_via_binary_search(&end_rec, *ts_end, oldest_sn, latest_sn);
|
||||||
|
|
||||||
if(rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,6 @@ int ens_records_iterator_init_range(record_iterator_t* iterator,
|
|||||||
record_sequence_number_t* opt_start,
|
record_sequence_number_t* opt_start,
|
||||||
record_sequence_number_t* opt_end);
|
record_sequence_number_t* opt_end);
|
||||||
|
|
||||||
// TODO: This function should call with the relevant start and end sequence numbers (retrieved through e.g. binary
|
|
||||||
// search / metadata)
|
|
||||||
// TODO: Do we guarantee that higher sequence numbers have at least our timestamp and lower sequence numbers up to our
|
// TODO: Do we guarantee that higher sequence numbers have at least our timestamp and lower sequence numbers up to our
|
||||||
// timestamp?
|
// timestamp?
|
||||||
int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* ts_start, uint32_t* ts_end);
|
int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* ts_start, uint32_t* ts_end);
|
||||||
|
@ -11,7 +11,7 @@ typedef uint32_t record_sequence_number_t;
|
|||||||
/**
|
/**
|
||||||
* Mask a given sequence number to get rid of MSB.
|
* Mask a given sequence number to get rid of MSB.
|
||||||
* TODO: maybe as #define?
|
* TODO: maybe as #define?
|
||||||
*
|
*
|
||||||
* @param sn sequence number to mask
|
* @param sn sequence number to mask
|
||||||
* @return masked sequence number
|
* @return masked sequence number
|
||||||
*/
|
*/
|
||||||
@ -19,7 +19,7 @@ record_sequence_number_t sn_mask(record_sequence_number_t sn);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare to sequence numbers for equality.
|
* Compare to sequence numbers for equality.
|
||||||
*
|
*
|
||||||
* @param a first sequence number
|
* @param a first sequence number
|
||||||
* @param b second sequence number
|
* @param b second sequence number
|
||||||
* @return 1, if sequence numbers are equal, 0 otherwise.
|
* @return 1, if sequence numbers are equal, 0 otherwise.
|
||||||
|
@ -72,7 +72,8 @@ int init_contact_storage(void) {
|
|||||||
struct flash_pages_info info;
|
struct flash_pages_info info;
|
||||||
// define the nvs file system
|
// define the nvs file system
|
||||||
info_fs.offset = FLASH_AREA_OFFSET(storage);
|
info_fs.offset = FLASH_AREA_OFFSET(storage);
|
||||||
rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), info_fs.offset, &info);
|
rc =
|
||||||
|
flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), info_fs.offset, &info);
|
||||||
|
|
||||||
if (rc) {
|
if (rc) {
|
||||||
// Error during retrieval of page information
|
// Error during retrieval of page information
|
||||||
@ -118,7 +119,6 @@ int load_contact(record_t* dest, record_sequence_number_t sn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int add_contact(record_t* src) {
|
int add_contact(record_t* src) {
|
||||||
|
|
||||||
// Check, if next sn would be at start of page
|
// Check, if next sn would be at start of page
|
||||||
record_sequence_number_t potential_next_sn = sn_increment(get_latest_sequence_number());
|
record_sequence_number_t potential_next_sn = sn_increment(get_latest_sequence_number());
|
||||||
storage_id_t potential_next_id = convert_sn_to_storage_id(potential_next_sn);
|
storage_id_t potential_next_id = convert_sn_to_storage_id(potential_next_sn);
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
#define CONTACT_STORAGE_H
|
#define CONTACT_STORAGE_H
|
||||||
|
|
||||||
#include "../contacts.h" // Requires contact_t in contacts.h!
|
#include "../contacts.h" // Requires contact_t in contacts.h!
|
||||||
#include "sequencenumber.h"
|
|
||||||
#include "../covid_types.h"
|
#include "../covid_types.h"
|
||||||
|
#include "sequencenumber.h"
|
||||||
|
|
||||||
typedef uint16_t storage_id_t;
|
typedef uint16_t storage_id_t;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user