Skip to content

Commit

Permalink
Rewrote offline uid copy to avoid 2 mode swaps
Browse files Browse the repository at this point in the history
We also skip HF if we managed to clone a LF tag.
  • Loading branch information
doegox committed Aug 22, 2023
1 parent ab9e9ba commit 0741683
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions firmware/application/src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,41 +507,46 @@ static void btn_fn_copy_ic_uid(void) {
uint8_t slot_now = tag_emulation_get_slot();
tag_specific_type_t tag_type[2];
tag_emulation_get_specific_type_by_slot(slot_now, tag_type);
tag_data_buffer_t* buffer = get_buffer_by_tag_type(tag_type[0]);

nfc_tag_14a_coll_res_entity_t* antres;

if(tag_type[1] == TAG_TYPE_EM410X) {
uint8_t status;

bool is_reader_mode_now = get_device_mode() == DEVICE_MODE_READER;
// first, we need switch to reader mode.
if (!is_reader_mode_now) {
// enter reader mode
reader_mode_enter();
bsp_delay_ms(8);
NRF_LOG_INFO("Start reader mode to offline copy.")
}

uint8_t id_buffer[5] = { 0x00 };
status = PcdScanEM410X(id_buffer);

if(status == LF_TAG_OK) {
tag_data_buffer_t* buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X);
memcpy(buffer->buffer, id_buffer, LF_EM410X_TAG_ID_SIZE);
tag_emulation_load_by_buffer(TAG_TYPE_EM410X, false);
bool is_reader_mode_now = get_device_mode() == DEVICE_MODE_READER;
// first, we need switch to reader mode.
if (!is_reader_mode_now) {
// enter reader mode
reader_mode_enter();
bsp_delay_ms(8);
NRF_LOG_INFO("Start reader mode to offline copy.")
}

// keep reader mode or exit reader mode.
offline_status_ok();
} else {
switch(tag_type[1]) {
case TAG_TYPE_EM410X:
uint8_t status;
uint8_t id_buffer[5] = { 0x00 };
status = PcdScanEM410X(id_buffer);

if(status == LF_TAG_OK) {
tag_data_buffer_t* buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X);
memcpy(buffer->buffer, id_buffer, LF_EM410X_TAG_ID_SIZE);
tag_emulation_load_by_buffer(TAG_TYPE_EM410X, false);
NRF_LOG_INFO("Offline LF uid copied")
offline_status_ok();
// no need to check for HF tag if we already cloned a LF tag
goto exit;
} else {
NRF_LOG_INFO("No LF tag found");
offline_status_error();
}
break;
case TAG_TYPE_UNKNOWN:
// empty LF slot, nothing to do, move on to HF
break;
default:
NRF_LOG_ERROR("Unsupported LF tag type")
offline_status_error();
}

if (!is_reader_mode_now) {
tag_mode_enter();
}
}

tag_data_buffer_t* buffer = get_buffer_by_tag_type(tag_type[0]);
switch(tag_type[0]) {
case TAG_TYPE_MIFARE_Mini:
case TAG_TYPE_MIFARE_1024:
Expand All @@ -560,24 +565,22 @@ static void btn_fn_copy_ic_uid(void) {
break;
}

case TAG_TYPE_UNKNOWN:
// empty HF slot, nothing to do
goto exit;

default:
NRF_LOG_ERROR("Unsupported tag type")
NRF_LOG_ERROR("Unsupported HF tag type")
offline_status_error();
return;
goto exit;
}

bool is_reader_mode_now = get_device_mode() == DEVICE_MODE_READER;
// first, we need switch to reader mode.
if (!is_reader_mode_now) {
// enter reader mode
reader_mode_enter();
bsp_delay_ms(8);
// finish HF reader initialization
pcd_14a_reader_reset();
pcd_14a_reader_antenna_on();
bsp_delay_ms(8);
NRF_LOG_INFO("Start reader mode to offline copy.")
}

// select a tag
picc_14a_tag_t tag;
uint8_t status;
Expand All @@ -590,13 +593,13 @@ static void btn_fn_copy_ic_uid(void) {
memcpy(antres->atqa, tag.atqa, 2);
// copy sak
antres->sak[0] = tag.sak;
NRF_LOG_INFO("Offline uid copied")
NRF_LOG_INFO("Offline HF uid copied")
offline_status_ok();
} else {
NRF_LOG_INFO("No tag found: %d", status);
NRF_LOG_INFO("No HF tag found");
offline_status_error();
}

exit:
// keep reader mode or exit reader mode.
if (!is_reader_mode_now) {
tag_mode_enter();
Expand Down

0 comments on commit 0741683

Please sign in to comment.