mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2024-11-12 22:48:52 +01:00
Add sn_mask to mask a sequence number
This commit is contained in:
parent
b98eea970c
commit
de21af38f2
@ -36,7 +36,6 @@ void main(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
printk("init contacts\n");
|
||||
init_contacts();
|
||||
err = init_io();
|
||||
if (err) {
|
||||
|
@ -6,7 +6,6 @@
|
||||
int ens_records_iterator_init_range(record_iterator_t* iterator,
|
||||
record_sequence_number_t* opt_start,
|
||||
record_sequence_number_t* opt_end) {
|
||||
// TODO: if start or end is null, use currently available numbers
|
||||
iterator->sn_next = opt_start ? *opt_start : get_oldest_sequence_number();
|
||||
iterator->sn_end = opt_end ? *opt_end : get_latest_sequence_number();
|
||||
if (get_num_contacts() == 0) {
|
||||
|
@ -1,10 +1,13 @@
|
||||
#include "sequencenumber.h"
|
||||
|
||||
record_sequence_number_t sn_mask(record_sequence_number_t sn) {
|
||||
return (sn & SN_MASK);
|
||||
}
|
||||
|
||||
int sequence_number_eq(record_sequence_number_t a, record_sequence_number_t b) {
|
||||
return (a & SN_MASK) == (b & SN_MASK);
|
||||
return sn_mask(a) == sn_mask(b);
|
||||
}
|
||||
|
||||
record_sequence_number_t sequence_number_increment(record_sequence_number_t sn) {
|
||||
return (++sn & SN_MASK);
|
||||
return sn_mask(++sn);
|
||||
}
|
@ -8,6 +8,15 @@
|
||||
|
||||
typedef uint32_t record_sequence_number_t;
|
||||
|
||||
/**
|
||||
* Mask a given sequence number to get rid of MSB.
|
||||
* TODO: maybe as #define?
|
||||
*
|
||||
* @param sn sequence number to mask
|
||||
* @return masked sequence number
|
||||
*/
|
||||
record_sequence_number_t sn_mask(record_sequence_number_t sn);
|
||||
|
||||
/**
|
||||
* Compare to sequence numbers for equality.
|
||||
*
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <zephyr.h>
|
||||
|
||||
#include "storage.h"
|
||||
#include "sequencenumber.h"
|
||||
|
||||
// Maybe use this as param for init function
|
||||
#define SEC_COUNT 8U
|
||||
@ -14,12 +15,14 @@
|
||||
#define STORED_CONTACTS_INFO_ID 0
|
||||
#define CONTACTS_OFFSET 1
|
||||
#define MAX_CONTACTS 65535
|
||||
#define ADDRESS_ID 1
|
||||
|
||||
static struct nvs_fs fs;
|
||||
|
||||
// TODO lome: load this from flash
|
||||
static stored_contacts_information_t contact_information;
|
||||
// Information about currently stored contacts
|
||||
static stored_contacts_information_t contact_information = {
|
||||
.oldest_contact = 0,
|
||||
.count = 0
|
||||
};
|
||||
|
||||
inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
|
||||
return (storage_id_t)(sn % MAX_CONTACTS) + CONTACTS_OFFSET;
|
||||
@ -31,6 +34,24 @@ void increment_storaed_contact_counter() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load our initial storage information from storage.
|
||||
*/
|
||||
int load_storage_information() {
|
||||
size_t size = sizeof(contact_information);
|
||||
int rc = nvs_read(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
||||
|
||||
// Check, if read what we wanted
|
||||
if(rc != size) {
|
||||
// Write our initial data to storage
|
||||
rc = nvs_write(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
||||
if(rc <= 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_contact_storage(void) {
|
||||
int rc = 0;
|
||||
struct flash_pages_info info;
|
||||
@ -39,13 +60,23 @@ int init_contact_storage(void) {
|
||||
rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), fs.offset, &info);
|
||||
|
||||
if (rc) {
|
||||
// Error during retrieval of page information
|
||||
return rc;
|
||||
}
|
||||
fs.sector_size = info.size;
|
||||
fs.sector_count = SEC_COUNT;
|
||||
|
||||
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
||||
|
||||
if(rc) {
|
||||
// Error during nvs_init
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Load the current storage information
|
||||
rc = load_storage_information();
|
||||
|
||||
printk("Currently %d contacts stored!\n", contact_information.count);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
@ -71,13 +102,14 @@ int add_contact(contact_t* src) {
|
||||
}
|
||||
|
||||
// TODO handle start and end
|
||||
// TODO lome: do we need this?
|
||||
int delete_contact(record_sequence_number_t sn) {
|
||||
storage_id_t id = convert_sn_to_storage_id(sn);
|
||||
return nvs_delete(&fs, id);
|
||||
}
|
||||
|
||||
record_sequence_number_t get_latest_sequence_number() {
|
||||
return contact_information.oldest_contact + contact_information.count;
|
||||
return sn_mask(contact_information.oldest_contact + contact_information.count);
|
||||
}
|
||||
|
||||
record_sequence_number_t get_oldest_sequence_number() {
|
||||
|
Loading…
Reference in New Issue
Block a user