Fix FDS records conflicts (dumps/nicks/settings overwriting each other)

This commit is contained in:
Philippe Teuwen
2023-08-17 04:00:47 +02:00
parent c6d95f34f7
commit 997f58d0d0
4 changed files with 41 additions and 44 deletions
@@ -384,7 +384,7 @@ void tag_emulation_sense_switch(tag_sense_type_t type, bool enable) {
*/
void tag_emulation_load_config(void) {
// 读取卡槽配置数据
bool ret = fds_read_sync(FDS_CONFIG_RECORD_FILE_ID, FDS_CONFIG_RECORD_FILE_KEY, sizeof(slotConfig), (uint8_t *)&slotConfig);
bool ret = fds_read_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig), (uint8_t *)&slotConfig);
if (ret) {
// 读取完成后,我们先保存一份当前配置的BCC,后面保存的时候可以作为变动对比的参考
calc_14a_crc_lut((uint8_t *)&slotConfig, sizeof(slotConfig), (uint8_t *)&m_slot_config_crc);
@@ -403,7 +403,7 @@ void tag_emulation_save_config(void) {
calc_14a_crc_lut((uint8_t *)&slotConfig, sizeof(slotConfig), (uint8_t *)&new_calc_crc);
if (new_calc_crc != m_slot_config_crc) { // 在保存之前,先确保卡槽配置有变动了
NRF_LOG_INFO("Save tag slot config start.");
bool ret = fds_write_sync(FDS_CONFIG_RECORD_FILE_ID, FDS_CONFIG_RECORD_FILE_KEY, sizeof(slotConfig) / 4, (uint8_t *)&slotConfig);
bool ret = fds_write_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig) / 4, (uint8_t *)&slotConfig);
if (ret) {
NRF_LOG_INFO("Save tag slot config success.");
} else {
@@ -9,35 +9,24 @@ NRF_LOG_MODULE_REGISTER();
void get_fds_map_by_slot_auto_inc_id(uint16_t key, uint16_t id, uint8_t slot, tag_sense_type_t sense_type, fds_slot_record_map_t* map) {
map->key = key + slot;
uint8_t base_id = 0;
switch(sense_type) {
case TAG_SENSE_HF:
base_id = 0;
break;
case TAG_SENSE_LF:
base_id = 1;
break;
case TAG_SENSE_NO:
// never to here...(if dev wrong, must fix)
APP_ERROR_CHECK(NRF_ERROR_INVALID_PARAM);
static void get_fds_map_by_slot_auto_inc_id(uint16_t id, uint8_t slot, tag_sense_type_t sense_type, fds_slot_record_map_t* map) {
if ((sense_type == TAG_SENSE_NO) || (slot > 7)) {
APP_ERROR_CHECK(NRF_ERROR_INVALID_PARAM);
}
map->id = id + base_id;
map->id = id + slot;
map->key = sense_type;
}
/**
* 根据卡槽和卡槽中指定的场类型获得其在FDS中对应的数据的KEY和ID
* Obtain the KEY and ID of the corresponding data in FDS according to the card slot and the field type specified in the card slot
*/
void get_fds_map_by_slot_sense_type_for_dump(uint8_t slot, tag_sense_type_t sense_type, fds_slot_record_map_t* map) {
// 根据 @see FDS_SLOT_TAG_DUMP_FILE_KEY 的约定,每个slot以其为起点,每个slot都有其单独的key的record,并且每个slot中独特的场类型也有一个数据的id
get_fds_map_by_slot_auto_inc_id(FDS_SLOT_TAG_DUMP_FILE_KEY, FDS_SLOT_TAG_DUMP_FILE_ID, slot, sense_type, map);
get_fds_map_by_slot_auto_inc_id(FDS_SLOT_TAG_DUMP_FILE_ID_BASE, slot, sense_type, map);
}
/**
* 根据卡槽和卡槽中指定的场类型获得其在FDS中对应的数据的KEY和ID
* Obtain the KEY and ID of the corresponding data in FDS according to the card slot and the field type specified in the card slot
*/
void get_fds_map_by_slot_sense_type_for_nick(uint8_t slot, tag_sense_type_t sense_type, fds_slot_record_map_t* map) {
get_fds_map_by_slot_auto_inc_id(FDS_SLOT_TAG_NICK_NAME_KEY, FDS_SLOT_TAG_NICK_NAME_ID, slot, sense_type, map);
get_fds_map_by_slot_auto_inc_id(FDS_SLOT_TAG_NICK_NAME_FILE_ID_BASE, slot, sense_type, map);
}
+2 -2
View File
@@ -62,7 +62,7 @@ void settings_migrate(void)
void settings_load_config(void)
{
bool ret = fds_read_sync(FDS_SETTINGS_ID, FDS_SETTINGS_KEY, sizeof(config), (uint8_t *)&config);
bool ret = fds_read_sync(FDS_SETTINGS_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config), (uint8_t *)&config);
if (ret) {
NRF_LOG_INFO("Load config done.");
// After the reading is complete, we first save a copy of the current CRC, which can be used as a reference for comparison of changes when saving later
@@ -89,7 +89,7 @@ uint8_t settings_save_config(void)
// We are saving the configuration, we need to calculate the crc code of the current configuration to judge whether the following data is updated
if (config_did_change()) { // Before saving, make sure that the configuration has changed
NRF_LOG_INFO("Save config start.");
bool ret = fds_write_sync(FDS_SETTINGS_ID, FDS_SETTINGS_KEY, sizeof(config) / 4, (uint8_t *)&config);
bool ret = fds_write_sync(FDS_SETTINGS_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config) / 4, (uint8_t *)&config);
if (ret) {
NRF_LOG_INFO("Save config success.");
update_config_crc();
+28 -20
View File
@@ -2,29 +2,37 @@
#define FDS_IDS_H
/*
* 卡槽配置,只有一份,一致即可
* Card slot configuration, only one, consistent
*/
#define FDS_CONFIG_RECORD_FILE_KEY 0x1066
#define FDS_CONFIG_RECORD_FILE_ID 0x1066
/*
* 每个卡槽有高低频两种数据,其中key是跟卡槽走的,而id+n就等于数据索引,固定某个索引为指定类型即可
* 每个slot的file_key都不一样
* 每个slot有两种类型的卡片,因此有两个数据ID(当前)
*/
#define FDS_SLOT_TAG_DUMP_FILE_KEY 0x1067
#define FDS_SLOT_TAG_DUMP_FILE_ID 0x1067
/*
* 每个卡槽有高低频两种数据,因此卡槽的昵称也要有两种,其中key是跟卡槽走的,而id+n就等于数据索引,固定某个索引为指定类型即可
*/
#define FDS_SLOT_TAG_NICK_NAME_KEY 0x1068
#define FDS_SLOT_TAG_NICK_NAME_ID 0x1068
#define FDS_EMULATION_CONFIG_FILE_ID 0x1000
#define FDS_EMULATION_CONFIG_RECORD_KEY 0x1
/*
* Slot for settings like LED animation mode and future options
*/
#define FDS_SETTINGS_KEY 0x1069
#define FDS_SETTINGS_ID 0x1069
#define FDS_SETTINGS_FILE_ID 0x1001
#define FDS_SETTINGS_RECORD_KEY 0x1
#endif
/*
* Each card slot has two types of data, high and low frequency
* FDS file ID follows the card slot, starting from 0x1100 to 0x1107
* FDS record key mirrors TAG_SENSE_LF/HF so is 1 for LF, 2 for HF (currently)
*/
#define FDS_SLOT_TAG_DUMP_FILE_ID_BASE 0x1100
/*
* Each card slot has two types of data, high and low frequency, so it can get two names
* FDS file ID follows the card slot, starting from 0x1200 to 0x1207
* FDS record key mirrors TAG_SENSE_LF/HF so is 1 for LF, 2 for HF (currently)
*/
#define FDS_SLOT_TAG_NICK_NAME_FILE_ID_BASE 0x1200
// Note that previously assigned records may need to be cleaned from Flash.
// Taking into account the possible overlaps, it boils down to
// ID 0x1066 Keys 0x1066
// ID 0x1067 Keys 0x1067-0x106e
// ID 0x1068 Keys 0x1067-0x106f
// ID 0x1069 Keys 0x1068-0x1070
#endif