btstack_tlv_flash_bank: extract btstack_tlv_flash_bank_read and btstack_tlv_flash_bank_write

This commit is contained in:
Matthias Ringwald 2017-10-31 14:32:49 +01:00
parent 15de820685
commit e8efffc8d1

View File

@ -51,6 +51,11 @@
// - Value: Len in bytes
#define BTSTACK_TLV_HEADER_LEN 8
// #ifdef BTSTACK_FLASH_ALIGNMENT_MAX
// #define BTSTACK_FLASH_ALIGNMENT 16
// #endif
static const char * btstack_tlv_header_magic = "BTstack";
// TLV Iterator
@ -62,9 +67,25 @@ typedef struct {
uint32_t len;
} tlv_iterator_t;
// support unaligned flash read/write
static void btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size);
}
static void btstack_tlv_flash_bank_write(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, const uint8_t * buffer, uint32_t size){
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size);
}
// static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank * self, uint32_t size){
// uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
// return (size + aligment-1) & ~(alignement - 1);
// }
// iterator
static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
uint8_t entry[8];
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, it->bank, it->offset, entry, 8);
btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8);
it->tag = big_endian_read_32(entry, 0);
it->len = big_endian_read_32(entry, 4);
}
@ -98,8 +119,8 @@ static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterato
static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
uint8_t header0[BTSTACK_TLV_HEADER_LEN];
uint8_t header1[BTSTACK_TLV_HEADER_LEN];
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
if (!valid0 && !valid1) return -1;
@ -116,7 +137,7 @@ static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self,
uint8_t header[BTSTACK_TLV_HEADER_LEN];
memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
}
/**
@ -130,7 +151,7 @@ static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, i
memset(empty16, 0xff, sizeof(empty16));
while (offset < size){
uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, copy_size);
btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
if (memcmp(buffer, empty16, copy_size)) {
log_info("not erased %x - %x", offset, offset + copy_size);
return 0;
@ -174,8 +195,8 @@ static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
uint8_t copy_buffer[32];
while (bytes_to_copy){
int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
tag_index += bytes_this_iteration;
next_write_pos += bytes_this_iteration;
bytes_to_copy -= bytes_this_iteration;
@ -186,7 +207,7 @@ static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
// prepare new one
uint8_t epoch_buffer;
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
self->current_bank = next_bank;
self->write_offset = next_write_pos;
@ -210,7 +231,7 @@ static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_ban
log_info("Erase tag '%x' at position %u", tag, it.offset);
// overwrite tag with invalid tag
uint32_t zero_tag = 0;
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
}
tlv_iterator_fetch_next(self, &it);
}
@ -246,7 +267,7 @@ static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t
if (tag_index == 0) return 0;
if (!buffer) return tag_len;
int copy_size = btstack_min(buffer_size, tag_len);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index + 8, buffer, copy_size);
btstack_tlv_flash_bank_read(self, self->current_bank, tag_index + 8, buffer, copy_size);
return copy_size;
}
@ -281,10 +302,10 @@ static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const
log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
// write value first
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset + 8, data, data_size);
btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset + 8, data, data_size);
// then entry
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset, entry, sizeof(entry));
btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
// overwrite old entries (if exists)
btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);