|
|
@ -31,10 +31,11 @@ static int32_t sector_mgr_file_get_size(int32_t fileuuid); |
|
|
|
static zeeprom_sector_info_t* sector_mgr_find_fileheader_by_filename(uint8_t* fileid); |
|
|
|
static int32_t sector_mgr_get_sector_offset(zeeprom_sector_info_t* sector); |
|
|
|
static int32_t sector_mgr_get_sector_rom_add(zeeprom_sector_info_t* sector); |
|
|
|
static int32_t sector_mgr_file_get_checksum(int32_t fileuuid); |
|
|
|
|
|
|
|
static void zeeprom_read(int32_t add, uint8_t* data, uint16_t len); |
|
|
|
static void zeeprom_write(int32_t add, const uint8_t* data, uint16_t len); |
|
|
|
static int32_t compute_checksum(uint8_t* data, uint16_t len); |
|
|
|
static void zeeprom_read(int32_t add, uint8_t* data, uint16_t len); |
|
|
|
static void zeeprom_write(int32_t add, const uint8_t* data, uint16_t len); |
|
|
|
static uint32_t compute_checksum(const uint8_t* data, uint16_t len); |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* CODE * |
|
|
@ -45,8 +46,8 @@ int zeeprom_fs_init() { // |
|
|
|
SingleLeadECG_eeprom_init(); |
|
|
|
if (!m_eeprom_header_inited) { |
|
|
|
zeeprom_read(0, (uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header)); |
|
|
|
int32_t checksum_val = compute_checksum((uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header) - sizeof(m_eeprom_header.checksum)); |
|
|
|
if (checksum_val != m_eeprom_header.checksum) { |
|
|
|
uint32_t checksum_val = compute_checksum((uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header) - sizeof(m_eeprom_header.sector_info_headers_checksum)); |
|
|
|
if (checksum_val != m_eeprom_header.sector_info_headers_checksum) { |
|
|
|
memset(&m_eeprom_header, 0, sizeof(m_eeprom_header)); |
|
|
|
} |
|
|
|
} |
|
|
@ -101,11 +102,13 @@ int zeeprom_fs_open(uint8_t* filename, wrflag_t wrflag) { |
|
|
|
memcpy(sectorHeaderInfo->filename, filename, 8); |
|
|
|
sectorHeaderInfo->sector_index_in_file = 0; |
|
|
|
sectorHeaderInfo->datalen = 0; |
|
|
|
sectorHeaderInfo->fileuuid = m_eeprom_header.fileuuid++; |
|
|
|
sectorHeaderInfo->sector_data_checksum = 0; |
|
|
|
sectorHeaderInfo->fileuuid = m_eeprom_header.fileuuid_cnt++; |
|
|
|
fileHander->fileuuid = sectorHeaderInfo->fileuuid; |
|
|
|
fileHander->sector_header = sectorHeaderInfo; |
|
|
|
fileHander->file_offset = 0; |
|
|
|
fileHander->file_size = 0; |
|
|
|
fileHander->checksum = 0; |
|
|
|
return fileHander->fd; |
|
|
|
|
|
|
|
} else { |
|
|
@ -119,6 +122,8 @@ int zeeprom_fs_open(uint8_t* filename, wrflag_t wrflag) { |
|
|
|
fileHander->fd = fileHander->fd; |
|
|
|
fileHander->sector_header = sectorHeaderInfo; |
|
|
|
fileHander->file_size = sector_mgr_file_get_size(fileHander->fileuuid); |
|
|
|
fileHander->checksum = sector_mgr_file_get_checksum(fileHander->fileuuid); |
|
|
|
ZLOGI("filesize:%d", fileHander->file_size); |
|
|
|
memcpy(fileHander->filename, filename, 8); |
|
|
|
memcpy(fileHander->usrdata, sectorHeaderInfo->usrdata, sizeof(sectorHeaderInfo->usrdata)); |
|
|
|
sector_mgr_open_sector(fileHander->fileuuid); |
|
|
@ -138,7 +143,7 @@ int zeeprom_fs_write(int fileid, const uint8_t* data, int32_t size) { |
|
|
|
zeeprom_sector_info_t* endsector = NULL; |
|
|
|
int32_t wadd = 0; |
|
|
|
|
|
|
|
ZASSERT(size == 256); |
|
|
|
ZASSERT(size == EEPROM_SECTOR_MIN_WR_SIZE); |
|
|
|
|
|
|
|
fileHander = filehandler_find(fileid); |
|
|
|
if (!fileHander) { |
|
|
@ -168,13 +173,15 @@ int zeeprom_fs_write(int fileid, const uint8_t* data, int32_t size) { |
|
|
|
ZLOGE("sector_mgr_get_sector_rom_add fail"); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
ZASSERT(wadd % 256 == 0); |
|
|
|
ZASSERT(wadd % EEPROM_SECTOR_MIN_WR_SIZE == 0); |
|
|
|
ZASSERT(wadd >= EEPROM_SECTOR_SIZE); |
|
|
|
zeeprom_write(wadd + endsector->datalen, data, size); |
|
|
|
|
|
|
|
endsector->datalen = endsector->datalen + size; |
|
|
|
endsector->sector_data_checksum += compute_checksum(data, size); |
|
|
|
fileHander->file_offset += size; |
|
|
|
fileHander->file_size += size; |
|
|
|
fileHander->checksum += compute_checksum(data, size); |
|
|
|
|
|
|
|
if (endsector->datalen == EEPROM_SECTOR_SIZE) { |
|
|
|
zeeprom_sector_info_t* newsector = sector_mgr_force_find_idle_sector(); |
|
|
@ -184,8 +191,9 @@ int zeeprom_fs_write(int fileid, const uint8_t* data, int32_t size) { |
|
|
|
} |
|
|
|
|
|
|
|
memset(newsector, 0, sizeof(zeeprom_sector_info_t)); |
|
|
|
newsector->usage = 1; |
|
|
|
newsector->opened = 1; |
|
|
|
newsector->usage = 1; |
|
|
|
newsector->opened = 1; |
|
|
|
newsector->sector_data_checksum = 0; |
|
|
|
memcpy(newsector->filename, header_sinfo->filename, 8); |
|
|
|
newsector->sector_index_in_file = endsector->sector_index_in_file + 1; |
|
|
|
newsector->datalen = 0; |
|
|
@ -204,8 +212,8 @@ int zeeprom_fs_close(int fileid) { |
|
|
|
} |
|
|
|
|
|
|
|
sector_mgr_close_sector(fileHander->fileuuid); |
|
|
|
int32_t checksum_val = compute_checksum((uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header) - sizeof(m_eeprom_header.checksum)); |
|
|
|
m_eeprom_header.checksum = checksum_val; |
|
|
|
uint32_t checksum_val = compute_checksum((uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header) - sizeof(m_eeprom_header.sector_info_headers_checksum)); |
|
|
|
m_eeprom_header.sector_info_headers_checksum = checksum_val; |
|
|
|
// zeeprom_write(0, (uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header)); |
|
|
|
|
|
|
|
filehandler_rlease(fileid); |
|
|
@ -226,10 +234,14 @@ int zeeprom_fs_read(int fileid, uint8_t* data, int32_t size) { |
|
|
|
* @brief |
|
|
|
* ÕÒµ½µ±Ç°ÉÈÇø |
|
|
|
*/ |
|
|
|
ZASSERT(size == 256); |
|
|
|
ZASSERT(size == EEPROM_SECTOR_MIN_WR_SIZE); |
|
|
|
filehandler_t* fileHander = filehandler_find(fileid); |
|
|
|
if (!fileHander) return -1; |
|
|
|
|
|
|
|
if (fileHander->file_offset + size > fileHander->file_size) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
zeeprom_sector_info_t* header_sinfo = fileHander->sector_header; |
|
|
|
if (!header_sinfo) { |
|
|
|
return -1; |
|
|
@ -244,7 +256,7 @@ int zeeprom_fs_read(int fileid, uint8_t* data, int32_t size) { |
|
|
|
} |
|
|
|
|
|
|
|
int32_t radd = sector_mgr_get_sector_rom_add(sector); |
|
|
|
ZASSERT(radd % 256 == 0); |
|
|
|
ZASSERT(radd % EEPROM_SECTOR_MIN_WR_SIZE == 0); |
|
|
|
radd += sector_off; |
|
|
|
|
|
|
|
zeeprom_read(radd, data, size); |
|
|
@ -293,6 +305,11 @@ int zeeprom_fs_get_filesize_by_fileuuid(int32_t fileuuid) { |
|
|
|
return sector_mgr_file_get_size(fileuuid); |
|
|
|
} |
|
|
|
|
|
|
|
int zeeprom_fs_get_checksum_by_fileuuid(int32_t fileuuid) { |
|
|
|
ZASSERT(m_is_init); |
|
|
|
return sector_mgr_file_get_checksum(fileuuid); |
|
|
|
} |
|
|
|
|
|
|
|
static void zeeprom_read(int32_t add, uint8_t* data, uint16_t len) { // |
|
|
|
int wrsize = EEPROM_SECTOR_MIN_WR_SIZE; |
|
|
|
|
|
|
@ -323,8 +340,8 @@ static void zeeprom_write(int32_t add, const uint8_t* data, uint16_t len) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static int32_t compute_checksum(uint8_t* data, uint16_t len) { |
|
|
|
int32_t sum = 0; |
|
|
|
static uint32_t compute_checksum(const uint8_t* data, uint16_t len) { |
|
|
|
uint32_t sum = 0; |
|
|
|
for (int i = 0; i < len; i++) { |
|
|
|
sum += data[i]; |
|
|
|
} |
|
|
@ -522,6 +539,18 @@ static int32_t sector_mgr_file_get_size(int32_t fileuuid) { |
|
|
|
return filesize; |
|
|
|
} |
|
|
|
|
|
|
|
static int32_t sector_mgr_file_get_checksum(int32_t fileuuid) { |
|
|
|
int32_t checksum = 0; |
|
|
|
for (int i = 0; i < MAX_SECTOR_NUM; i++) { |
|
|
|
zeeprom_sector_info_t* sinfo = &m_eeprom_header.sectorinfos[i]; |
|
|
|
|
|
|
|
if (sinfo->usage == 1 && sinfo->fileuuid == fileuuid) { |
|
|
|
checksum += sinfo->sector_data_checksum; |
|
|
|
} |
|
|
|
} |
|
|
|
return checksum; |
|
|
|
} |
|
|
|
|
|
|
|
static const char* filename2str(uint8_t* filename) { |
|
|
|
static char filename_str[32]; |
|
|
|
sprintf(filename_str, "%02x%02x%02x%02x%02x%02x%02x%02x", filename[0], filename[1], filename[2], filename[3], filename[4], filename[5], filename[6], filename[7]); |
|
|
|