Skip to content

Commit

Permalink
Changed fds_write_sync to take length in bytes instead of next multip…
Browse files Browse the repository at this point in the history
…le of 4
  • Loading branch information
doegox committed Oct 11, 2023
1 parent 6236fdd commit 446b57e
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...

## [unreleased][unreleased]
- Changed fds_write_sync to take length in bytes instead of next multiple of 4 (@doegox)
- Fixed field LED when LF reading and HF cloning (@doegox)
- Added renaming of slot into "cloned" when having cloned an ID/UID with a button (@doegox)
- Fixed spurious LED in battery level, assigned battery level to long press B (@doegox)
Expand Down
2 changes: 1 addition & 1 deletion firmware/application/src/app_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ static data_frame_tx_t *cmd_processor_set_slot_tag_nick(uint16_t cmd, uint16_t s
buffer[0] = length - 2;
memcpy(buffer + 1, data + 2, buffer[0]);

bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer);
bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer);
if (!ret) {
return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL);
}
Expand Down
7 changes: 3 additions & 4 deletions firmware/application/src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,17 +702,16 @@ static void btn_fn_copy_ic_uid(void) {
if (lf_copy_succeeded || hf_copy_succeeded) {
fds_slot_record_map_t map_info;
char *nick = "cloned";
uint8_t buffer[((((1+strlen(nick))-1)/4)+1)*4];
uint8_t buffer[36];
buffer[0] = strlen(nick);
memcpy(buffer + 1, nick, buffer[0]);
// ASSERT(sizeof(buffer) % 4 == 0);
if (lf_copy_succeeded) {
get_fds_map_by_slot_sense_type_for_nick(slot_now, TAG_SENSE_LF, &map_info);
fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer);
fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer);
}
if (hf_copy_succeeded) {
get_fds_map_by_slot_sense_type_for_nick(slot_now, TAG_SENSE_HF, &map_info);
fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer);
fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer);
}
}
// keep reader mode or exit reader mode.
Expand Down
14 changes: 6 additions & 8 deletions firmware/application/src/rfid/nfctag/hf/nfc_mf1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,8 @@ void nfc_tag_mf1_reset_handler() {
* @return Suppose type == tag_type_mifare_1024,
* The length of the information should be the anti -collision information plus the configuration information plus the length of the sector
*/
static int get_information_size_by_tag_type(tag_specific_type_t type, bool auth_align) {
int size_raw = sizeof(nfc_tag_14a_coll_res_entity_t) + sizeof(nfc_tag_mf1_configure_t) + (get_block_max_by_tag_type(type) * NFC_TAG_MF1_DATA_SIZE);
int size_align = size_raw + (size_raw % 4);
return auth_align ? size_align : size_raw;
static int get_information_size_by_tag_type(tag_specific_type_t type) {
return sizeof(nfc_tag_14a_coll_res_entity_t) + sizeof(nfc_tag_mf1_configure_t) + (get_block_max_by_tag_type(type) * NFC_TAG_MF1_DATA_SIZE);
}

/** @brief MF1's callback before saving data
Expand All @@ -1124,7 +1122,7 @@ int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer)
m_tag_information->config.mode_block_write = NFC_TAG_MF1_WRITE_SHADOW;
}
// Save the corresponding size data according to the current label type
return get_information_size_by_tag_type(type, false);
return get_information_size_by_tag_type(type);
} else {
return 0;
}
Expand All @@ -1136,7 +1134,7 @@ int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer)
*/
int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffer) {
// Make sure that external capacity is enough to convert to an information structure
int info_size = get_information_size_by_tag_type(type, false);
int info_size = get_information_size_by_tag_type(type);
if (buffer->length >= info_size) {
//Convert the data buffer to MF1 structure type
m_tag_information = (nfc_tag_mf1_information_t *)buffer->buffer;
Expand Down Expand Up @@ -1200,9 +1198,9 @@ bool nfc_tag_mf1_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type);
fds_slot_record_map_t map_info;
get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info);
int info_size = get_information_size_by_tag_type(tag_type, true); // auto 4 byte align.
int info_size = get_information_size_by_tag_type(tag_type);
NRF_LOG_INFO("MF1 info size: %d", info_size);
bool ret = fds_write_sync(map_info.id, map_info.key, info_size / 4, p_mf1_information);
bool ret = fds_write_sync(map_info.id, map_info.key, info_size, p_mf1_information);
if (ret) {
NRF_LOG_INFO("Factory slot data success.");
} else {
Expand Down
4 changes: 2 additions & 2 deletions firmware/application/src/rfid/nfctag/hf/nfc_ntag.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ bool nfc_tag_ntag_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type);
fds_slot_record_map_t map_info;
get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info);
int info_size = get_information_size_by_tag_type(tag_type); // auto 4 byte align.
int info_size = get_information_size_by_tag_type(tag_type);
NRF_LOG_INFO("NTAG info size: %d", info_size);
bool ret = fds_write_sync(map_info.id, map_info.key, info_size / 4, p_ntag_information);
bool ret = fds_write_sync(map_info.id, map_info.key, info_size, p_ntag_information);
if (ret) {
NRF_LOG_INFO("Factory slot data success.");
} else {
Expand Down
4 changes: 2 additions & 2 deletions firmware/application/src/rfid/nfctag/lf/lf_tag_em.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ int lf_tag_em410x_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffe
*/
bool lf_tag_em410x_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
// default id, must to align(4), more word...
uint8_t tag_id[8] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x88 };
uint8_t tag_id[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x88 };
// Write the data in Flash
tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type);
fds_slot_record_map_t map_info; // Get the special card slot FDS record information
get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info);
//Call the blocked FDS to write the function, and write the data of the specified field type of the card slot into the Flash
bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(tag_id) / 4, (uint8_t *)tag_id);
bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(tag_id), (uint8_t *)tag_id);
if (ret) {
NRF_LOG_INFO("Factory slot data success.");
} else {
Expand Down
6 changes: 2 additions & 4 deletions firmware/application/src/rfid/nfctag/tag_emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ static void save_data_by_tag_type(uint8_t slot, tag_specific_type_t tag_type) {
// Get the special card slot FDS record information
fds_slot_record_map_t map_info;
get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info);
// Calculate the length of the data to be saved (automatically fill in the whole word)
int data_word_length = (data_byte_length / 4) + (data_byte_length % 4 > 0 ? 1 : 0);
// Call the blocked FDS to write the function, and write the data of the specified field type of the card slot into the Flash
bool ret = fds_write_sync(map_info.id, map_info.key, data_word_length, buffer->buffer);
bool ret = fds_write_sync(map_info.id, map_info.key, data_byte_length, buffer->buffer);
if (ret) {
NRF_LOG_INFO("Save tag slot data success.");
} else {
Expand Down Expand Up @@ -505,7 +503,7 @@ static 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) { // Before saving, make sure that the card slot configuration has changed
NRF_LOG_INFO("Save tag slot config start.");
bool ret = fds_write_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig) / 4, (uint8_t *)&slotConfig);
bool ret = fds_write_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig), (uint8_t *)&slotConfig);
if (ret) {
NRF_LOG_INFO("Save tag slot config success.");
m_slot_config_crc = new_calc_crc;
Expand Down
2 changes: 1 addition & 1 deletion firmware/application/src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config) / 4, (uint8_t *)&config);
bool ret = fds_write_sync(FDS_SETTINGS_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config), (uint8_t *)&config);
if (ret) {
NRF_LOG_INFO("Save config success.");
update_config_crc();
Expand Down
7 changes: 6 additions & 1 deletion firmware/application/src/utils/fds_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static ret_code_t fds_write_record_nogc(uint16_t id, uint16_t key, uint16_t data
/**
* Write record
*/
bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void *buffer) {
bool fds_write_sync(uint16_t id, uint16_t key, uint16_t length, void *buffer) {
// Make only one task running
APP_ERROR_CHECK_BOOL(!fds_operation_info.waiting);
// write result
Expand All @@ -117,6 +117,11 @@ bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void
fds_operation_info.key = key;
fds_operation_info.success = false;
fds_operation_info.waiting = true;
// compute needed words
if (length == 0) {
return ret;
}
uint16_t data_length_words = ((length - 1) / 4) + 1;

// CCall the write implementation function without automatic GC
ret_code_t err_code = fds_write_record_nogc(id, key, data_length_words, buffer);
Expand Down
2 changes: 1 addition & 1 deletion firmware/application/src/utils/fds_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


bool fds_read_sync(uint16_t id, uint16_t key, uint16_t *length, uint8_t *buffer);
bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void *buffer);
bool fds_write_sync(uint16_t id, uint16_t key, uint16_t length, void *buffer);
int fds_delete_sync(uint16_t id, uint16_t key);
bool fds_is_exists(uint16_t id, uint16_t key);
void fds_util_init(void);
Expand Down

0 comments on commit 446b57e

Please sign in to comment.