Implement ens_fs and move ens stuff to ens directory

This commit is contained in:
H1ghBre4k3r 2021-04-12 19:04:46 +02:00 committed by Patrick Rathje
parent f367e22db6
commit d38996a021
9 changed files with 380 additions and 12 deletions

219
for_future_use.txt Normal file
View File

@ -0,0 +1,219 @@
// ens_fs_t fs;
// int rc = ens_fs_init(&fs, FLASH_AREA_ID(storage), sizeof(uint32_t));
// if(rc) {
// printk("FS init err %d\n", rc);
// }
// printk("Size: %d\n", fs.area->fa_size);
// rc = ens_fs_page_erase(&fs, 0, 1);
// if(rc) {
// printk("Erase err %d\n", rc);
// }
// uint32_t test;
// rc = ens_fs_read(&fs, 0, &test);
// if(rc) {
// printk("1 read error %d\n", rc);
// }
// printk("test: %ul\n", test);
// test = 6;
// rc = ens_fs_write(&fs, 0, &test);
// if(rc) {
// printk("1 write error %d\n", rc);
// }
// rc = ens_fs_read(&fs, 0, &test);
// if(rc) {
// printk("2 read error %d\n", rc);
// }
// printk("test: %ul\n", test);
// test = 5;
// rc = ens_fs_write(&fs, 0, &test);
// if(rc) {
// printk("2 write error %d\n", rc);
// }
// rc = ens_fs_read(&fs, 0, &test);
// if(rc) {
// printk("3 read error %d\n", rc);
// }
// printk("test: %ul\n", test);
// printf("\n" FLASH_NAME " SPI flash testing\n");
// printf("==========================\n");
// const struct device* flash_dev = device_get_binding(FLASH_DEVICE);
// if (!flash_dev) {
// printf("SPI flash driver %s was not found!\n", FLASH_DEVICE);
// return;
// }
// /* Write protection needs to be disabled before each write or
// * erase, since the flash component turns on write protection
// * automatically after completion of write and erase
// * operations.
// */
// // printf("\nTest 1: Flash erase\n");
// // flash_write_protection_set(flash_dev, false);
// // rc = flash_erase(flash_dev, FLASH_TEST_REGION_OFFSET, FLASH_SECTOR_SIZE);
// // if (rc != 0) {
// // printf("Flash erase failed! %d\n", rc);
// // } else {
// // printf("Flash erase succeeded!\n");
// // }
// printf("\nTest 2: Flash write\n");
// printk("pages: %d\n", flash_get_page_count(flash_dev));
// uint32_t offset = FLASH_AREA_OFFSET(storage);
// struct flash_pages_info info;
// flash_get_page_info_by_offs(flash_dev, offset, &info);
// printk("page_size: %d\n", info.size);
// flash_write_protection_set(flash_dev, false);
// if(flash_erase(flash_dev, offset, info.size)) {
// printk("erase err\n");
// }
// uint8_t test;
// flash_read(flash_dev, offset, &test, sizeof(test));
// printk("Test: %u\n", test);
// test = 6;
// flash_write_protection_set(flash_dev, false);
// flash_write(flash_dev, offset, &test, sizeof(test));
// flash_read(flash_dev, offset, &test, sizeof(test));
// printk("Test: %u\n", test);
// test = 5;
// flash_write_protection_set(flash_dev, false);
// flash_write(flash_dev, offset, &test, sizeof(test));
// flash_read(flash_dev, offset, &test, sizeof(test));
// printk("Test: %u\n", test);
// uint32_t start_time = k_cycle_get_32();
// for(int i = 0; i < 100; i++) {
// contact_t contact;
// contact.cnt = i;
// flash_write_protection_set(flash_dev, false);
// if(flash_write(flash_dev, offset + i * 32, &contact, sizeof(contact))) {
// printk("write err\n");
// }
// // if(flash_read(flash_dev, offset + i * 32, &contact, sizeof(contact))) {
// // printk("read err\n");
// // }
// // printk("round: %d, contact: %d, len: %d\n", i, contact.cnt, sizeof(contact));
// }
// uint64_t nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time);
// printk("adding 100 contacts %llu ms\n", nanoseconds_spent);
// printk("average: %llu\n", nanoseconds_spent / 100);
// start_time = k_cycle_get_32();
// for(int i = 0; i < 100; i++) {
// contact_t contact;
// if(flash_read(flash_dev, offset + i * 32, &contact, sizeof(contact))) {
// printk("read err\n");
// }
// if(contact.cnt != i) {
// printk("Expected does not match!\n");
// }
// // printk("round: %d, contact: %d, len: %d\n", i, contact.cnt, sizeof(contact));
// }
// nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time);
// printk("loading 100 contacts %llu ms\n", nanoseconds_spent);
// printk("average: %llu\n", nanoseconds_spent / 100);
// for(int i = 0; i < 100; i++) {
// memset(&expected, 0, len);
// expected.cnt = i;
// flash_write_protection_set(flash_dev, false);
// rc = flash_erase(flash_dev, FLASH_TEST_REGION_OFFSET, len);
// if (rc != 0) {
// printf("Flash erase failed! %d\n", rc);
// } else {
// printf("Flash erase succeeded!\n");
// }
// flash_write_protection_set(flash_dev, false);
// rc = flash_write(flash_dev, FLASH_TEST_REGION_OFFSET, &expected, len);
// if (rc != 0) {
// printf("Flash write failed! %d\n", rc);
// return;
// }
// memset(&buf, 0, len);
// rc = flash_read(flash_dev, FLASH_TEST_REGION_OFFSET, &buf, len);
// if (rc != 0) {
// printf("Flash read failed! %d\n", rc);
// return;
// }
// if (memcmp(&expected, &buf, len) == 0) {
// printf("Data read matches data written. Good!!\n");
// } else {
// // const uint8_t* wp = &expected;
// // const uint8_t* rp = &buf;
// // const uint8_t* rpe = rp + len;
// printf("Data read does not match data written!!\n");
// // while (rp < rpe) {
// // printf("%08x wrote %02x read %02x %s\n", (uint32_t)(FLASH_TEST_REGION_OFFSET + (rp - buf)), *wp, *rp,
// // (*rp == *wp) ? "match" : "MISMATCH");
// // ++rp;
// // ++wp;
// // }
// }
// }
// const struct flash_area *area;
// err = flash_area_open(FLASH_AREA_ID(storage), &area);
// if(err) {
// printk("Error during flash_area_open (err %d)\n", err);
// }
// const struct device* dev = device_get_binding(area->fa_dev_name);
// printk("write block size: %d\n", flash_get_write_block_size(dev));
// uint32_t start_time = k_cycle_get_32();
// uint32_t test;
// for(int i = 0; i < 100; i++) {
// test = i;
// flash_write_protection_set(dev, false);
// int rc = flash_write(dev, area->fa_off + i * sizeof(test), &test, sizeof(test));
// if(rc) {
// printk("Run %d, err %d\n", i, rc);
// }
// // contact.cnt = 0;
// flash_write_protection_set(dev, false);
// rc = flash_read(dev, area->fa_off + i * sizeof(test), &test, sizeof(test));
// if(rc) {
// printk("Run %d, err %d\n", i, rc);
// }
// printk("Round: %d, test: %d, size: %d\n", i, test, sizeof(test));
// }
// uint64_t nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time);
// printk("adding 100 contacts %llu ms\n", nanoseconds_spent);
// printk("average: %llu\n", nanoseconds_spent / 100);
// start_time = k_cycle_get_32();
// for(int i = 0; i < 100; i++) {
// contact_t contact;
// int rc = flash_read(dev, i * 32, &contact, sizeof(contact));
// if(rc) {
// printk("Run %d, err %d\n", i, rc);
// }
// }
// nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time);
// printk("loading 100 contacts %llu ms\n", nanoseconds_spent);
// printk("average: %llu\n", nanoseconds_spent / 100);

View File

@ -23,7 +23,7 @@
#include "contacts.h"
#include "exposure-notification.h"
#include "covid.h"
#include "storage.h"
#include "ens/storage.h"
static contact_t contacts[MAX_CONTACTS];
static uint32_t contact_count = 0;

61
src/ens/ens_fs.c Normal file
View File

@ -0,0 +1,61 @@
#include "ens_fs.h"
#include <drivers/flash.h>
#include <storage/flash_map.h>
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;
}
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;
}
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) {
uint64_t offset = id * fs->entry_size;
int rc = flash_area_read(fs->area, offset, dist, fs->entry_size);
if(rc) {
goto end;
}
// TODO lome: Read deleted flag and CRC
end:
return rc;
}
int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data) {
uint64_t offset = id * fs->entry_size;
// TODO lome: set CRC
int rc = flash_area_write(fs->area, offset, data, fs->entry_size);
if(rc) {
goto end;
}
end:
return rc;
}
int ens_fs_delete(ens_fs_t* fs, uint64_t id) {
// TODO lome: Overwrite deleted flag.
return 0;
}
int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count) {
uint64_t start = (offset - offset % fs->sector_size) * fs->entry_size;
int rc = flash_area_erase(fs->area, start, fs->sector_size * sector_count);
return rc;
}

88
src/ens/ens_fs.h Normal file
View File

@ -0,0 +1,88 @@
#ifndef FCB_H
#define FCB_H
#include <device.h>
#include <kernel.h>
#include <storage/flash_map.h>
#include "fs/fs.h"
#include "stdint.h"
typedef struct ens_fs {
/**
* Flash area for this file system.
*/
const struct flash_area* area;
/**
* Size of each individual entry.
*/
size_t entry_size;
/**
* Size of each sector of this fs.
*/
uint16_t sector_size;
/**
* Amount of sectors in this fs.
*/
uint16_t sector_count;
/**
* Lock for this fs.
*/
// struct k_mutex* lock;
} ens_fs_t;
/**
* Initialize the file system.
*
* @param fs file system
* @param id id of the partition
* @param size of each entry in the file-system. Has to power of 2
*
* @return 0 on success, -errno otherwise
*/
int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size);
/**
* Read an entry from this file system.
*
* @param fs file system
* @param id id of entry
* @param data destination address of data to be read
*
* @return 0 on success, -errno otherwise
*/
int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist);
/**
* Write data to the file system.
*
* @param fs file system
* @param id id of the entry
* @param data data to be written
*
* @return 0 on success, -errno otherwise
*/
int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data);
/**
* Delete an entry from the file system.
*
* @param fs file system
* @param id id of the entry to be deleted
*
* @return 0 on success, -errno otherwise
*/
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).
*
* @param fs file system
* @param offset offset to start erasing
* @param sector_count count of sectors to erase
*
* @return 0 on success, -errno otherwise
*/
int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count);
#endif

View File

@ -6,7 +6,7 @@
#include <string.h>
#include <zephyr.h>
#include "sequencenumber.h"
#include "../sequencenumber.h"
#include "storage.h"
// Get external flash device
@ -21,7 +21,7 @@
#endif
// Maybe use this as param for init function
#define SEC_COUNT 192U
#define SEC_COUNT 96U
#define STORED_CONTACTS_INFO_ID 0
#define CONTACTS_OFFSET 1
@ -90,7 +90,7 @@ int init_contact_storage(void) {
// Error during retrieval of page information
return rc;
}
fs.sector_size = info.size * 4;
fs.sector_size = info.size * 8;
fs.sector_count = SEC_COUNT;
rc = nvs_init(&fs, FLASH_DEVICE);

View File

@ -1,7 +1,7 @@
#ifndef 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"
typedef uint16_t storage_id_t;

View File

@ -9,14 +9,13 @@
#include <bluetooth/hci.h>
#include <random/rand32.h>
#include <sys/printk.h>
#include "contacts.h"
#include "covid.h"
#include "covid_types.h"
#include "exposure-notification.h"
#include "gatt_service.h"
#include "io.h"
#include "storage.h"
#include "ens/storage.h"
void main(void) {
int err = 0;
@ -30,11 +29,12 @@ void main(void) {
return;
}
err = init_contact_storage();
if (err) {
printk("init storage failed (err %d)\n", err);
return;
}
// TODO lome: Cleanup storage
// err = init_contact_storage();
// if (err) {
// printk("init storage failed (err %d)\n", err);
// return;
// }
init_contacts();
err = init_io();