Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FL-2369] NFC refactoring #1095

Merged
merged 33 commits into from
Apr 19, 2022
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4f8a2ff
nfc: clean up scenes
gornekich Mar 29, 2022
e4e3a6b
nfc worker: remove field on from worker
gornekich Mar 29, 2022
9c9e64d
nfc worker: move full data exchange to furi hal
gornekich Mar 29, 2022
4bc23cb
nfc_device: check UID length
gornekich Apr 1, 2022
f6c5a10
nfc protocol: introduce mifare common API
gornekich Mar 29, 2022
76d8fed
nfc: move common data to furi hal nfc
gornekich Apr 4, 2022
e58a590
nfc: rename emv_decoder -> emv
gornekich Apr 4, 2022
b3b3ffb
nfc: move emv data structure to emv lib
gornekich Apr 4, 2022
869ed46
nfc: remove deactivate after detection
gornekich Apr 4, 2022
2d59f1d
nfc: rework furi hal nfc detect
gornekich Apr 4, 2022
ee4bca9
nfc: clean up CLI commands and type
gornekich Apr 4, 2022
23584db
nfc: remove unused includes and function
gornekich Apr 4, 2022
18d5bfa
nfc: add TxRxType enum
gornekich Apr 4, 2022
250867a
nfc: read mifare ultralight refactoring
gornekich Apr 5, 2022
9a94eff
nfc: refactore mifare ultralight start
gornekich Apr 6, 2022
eac23d1
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 13, 2022
e403f5c
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 15, 2022
778512f
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 17, 2022
d9fed7c
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 18, 2022
3fd5782
rfal: fix custom data exchange
gornekich Apr 18, 2022
01ed549
nfc: refactor read bank card
gornekich Apr 18, 2022
d26a9cc
nfc: refactor read emv application
gornekich Apr 18, 2022
8afaaad
nfc: refactor emv test emulation
gornekich Apr 18, 2022
9738e4f
nfc: refactor uid emulation
gornekich Apr 18, 2022
5744747
nfc: add limit to uid emulation logger
gornekich Apr 18, 2022
e4a2ddb
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 18, 2022
a4d2393
fix source formatting
gornekich Apr 19, 2022
8b39fdf
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 19, 2022
f97e4f6
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 19, 2022
a30be16
furi_hal_nfc: fix data exchange full
gornekich Apr 19, 2022
fd290c7
nfc: fix mifare ultralight type load
gornekich Apr 19, 2022
e2386df
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 19, 2022
e8bce8b
Merge branch 'dev' into gornek/2369_nfc_refactoring
skotopes Apr 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nfc: remove deactivate after detection
gornekich committed Apr 6, 2022
commit 869ed4617af9e42f9dc95c8a7947acc7dd7ba10b
13 changes: 7 additions & 6 deletions applications/nfc/nfc_cli.c
Original file line number Diff line number Diff line change
@@ -29,8 +29,7 @@ void nfc_cli_detect(Cli* cli, string_t args) {
printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
while(!cmd_exit) {
cmd_exit |= cli_cmd_interrupt_received(cli);
cmd_exit |= furi_hal_nfc_detect(&dev_list, &dev_cnt, 400, true);
if(dev_cnt > 0) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 400)) {
printf("Found %d devices\r\n", dev_cnt);
for(uint8_t i = 0; i < dev_cnt; i++) {
printf("%d found: %s ", i + 1, nfc_get_rfal_type(dev_list[i].type));
@@ -43,10 +42,12 @@ void nfc_cli_detect(Cli* cli, string_t args) {
}
printf("\r\n");
}
break;
}
furi_hal_nfc_sleep();
osDelay(50);
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

void nfc_cli_emulate(Cli* cli, string_t args) {
@@ -71,11 +72,11 @@ void nfc_cli_emulate(Cli* cli, string_t args) {
while(!cli_cmd_interrupt_received(cli)) {
if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) {
printf("Reader detected\r\n");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
osDelay(50);
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

void nfc_cli_field(Cli* cli, string_t args) {
@@ -96,7 +97,7 @@ void nfc_cli_field(Cli* cli, string_t args) {
}

furi_hal_nfc_field_off();
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

static void nfc_cli(Cli* cli, string_t args, void* context) {
59 changes: 30 additions & 29 deletions applications/nfc/nfc_worker.c
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ int32_t nfc_worker_task(void* context) {
} else if(nfc_worker->state == NfcWorkerStateReadMifareDesfire) {
nfc_worker_read_mifare_desfire(nfc_worker);
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
furi_hal_power_insomnia_exit();

@@ -123,7 +123,7 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
NfcDeviceData* dev_data = nfc_worker->dev_data;

while(nfc_worker->state == NfcWorkerStateDetect) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, true)) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000)) {
// Process first found device
dev = &dev_list[0];
dev_data->nfc_data.uid_len = dev->nfcidLen;
@@ -166,6 +166,7 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
}
break;
}
furi_hal_nfc_sleep();
osDelay(100);
}
}
@@ -218,7 +219,7 @@ void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {

while(nfc_worker->state == NfcWorkerStateReadEMVApp) {
memset(&emv_app, 0, sizeof(emv_app));
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000)) {
// Card was found. Check that it supports EMV
if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
@@ -234,7 +235,7 @@ void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {
err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
if(err != ERR_NONE) {
FURI_LOG_D(TAG, "Error during selection PPSE request: %d", err);
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Select PPSE response received. Start parsing response");
@@ -249,18 +250,18 @@ void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {
break;
} else {
FURI_LOG_D(TAG, "Can't find pay application");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
} else {
// Can't find EMV card
FURI_LOG_W(TAG, "Card doesn't support EMV");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
} else {
// Can't find EMV card
FURI_LOG_D(TAG, "Can't find any cards");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
osDelay(20);
}
@@ -280,7 +281,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {

while(nfc_worker->state == NfcWorkerStateReadEMV) {
memset(&emv_app, 0, sizeof(emv_app));
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000)) {
// Card was found. Check that it supports EMV
if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
@@ -296,7 +297,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
if(err != ERR_NONE) {
FURI_LOG_D(TAG, "Error during selection PPSE request: %d", err);
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Select PPSE response received. Start parsing response");
@@ -306,15 +307,15 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
} else {
FURI_LOG_D(TAG, "Can't find pay application");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Starting application ...");
tx_len = emv_prepare_select_app(tx_buff, &emv_app);
err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
if(err != ERR_NONE) {
FURI_LOG_D(TAG, "Error during application selection request: %d", err);
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Select application response received. Start parsing response");
@@ -325,15 +326,15 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Can't find card name, but PDOL is present.");
} else {
FURI_LOG_D(TAG, "Can't find card name or PDOL");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Starting Get Processing Options command ...");
tx_len = emv_prepare_get_proc_opt(tx_buff, &emv_app);
err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
if(err != ERR_NONE) {
FURI_LOG_D(TAG, "Error during Get Processing Options command: %d", err);
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
if(emv_decode_get_proc_opt(rx_buff, *rx_len, &emv_app)) {
@@ -397,17 +398,17 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
} else {
FURI_LOG_D(TAG, "Can't read card number");
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
} else {
// Can't find EMV card
FURI_LOG_W(TAG, "Card doesn't support EMV");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
} else {
// Can't find EMV card
FURI_LOG_D(TAG, "Can't find any cards");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
osDelay(20);
}
@@ -474,7 +475,7 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Received Select PPSE");
} else {
FURI_LOG_D(TAG, "Error in 1st data exchange: select PPSE");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}
FURI_LOG_D(TAG, "Transive SELECT PPSE ANS");
@@ -484,7 +485,7 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Received Select APP");
} else {
FURI_LOG_D(TAG, "Error in 2nd data exchange: select APP");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}

@@ -495,7 +496,7 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Received PDOL");
} else {
FURI_LOG_D(TAG, "Error in 3rd data exchange: receive PDOL");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}

@@ -506,7 +507,7 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Transive PDOL ANS");
} else {
FURI_LOG_D(TAG, "Error in 4rd data exchange: Transive PDOL ANS");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
continue;
}

@@ -521,7 +522,7 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
FURI_LOG_D(TAG, "Transive Debug message");
}
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
} else {
FURI_LOG_D(TAG, "Can't find reader");
}
@@ -542,9 +543,9 @@ void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
nfc_device_data_clear(result);

while(nfc_worker->state == NfcWorkerStateReadMifareUl) {
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
memset(&mf_ul_read, 0, sizeof(mf_ul_read));
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300)) {
if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA &&
mf_ul_check_card_type(
dev_list[0].dev.nfca.sensRes.anticollisionInfo,
@@ -576,8 +577,8 @@ void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
err = ERR_NONE;
mf_ul_set_default_version(&mf_ul_read);
// Reinit device
furi_hal_nfc_deactivate();
if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
furi_hal_nfc_sleep();
if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300)) {
FURI_LOG_D(TAG, "Lost connection. Restarting search");
continue;
}
@@ -708,7 +709,7 @@ void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker) {

// Detect Mifare Classic card
while(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300)) {
dev = &dev_list[0];
if(mf_classic_get_type(
dev->nfcid,
@@ -743,7 +744,7 @@ void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker) {
mf_classic_auth_init_context(&auth_ctx, reader.cuid, curr_sector);
bool sector_key_found = false;
while(nfc_mf_classic_dict_get_next_key(nfc_worker->dict_stream, &curr_key)) {
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
if(furi_hal_nfc_activate_nfca(300, &reader.cuid)) {
if(!card_found_notified) {
if(reader.type == MfClassicType1k) {
@@ -849,8 +850,8 @@ void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker) {
MifareDesfireData* data = &result->mf_df_data;

while(nfc_worker->state == NfcWorkerStateReadMifareDesfire) {
furi_hal_nfc_deactivate();
if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
furi_hal_nfc_sleep();
if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300)) {
osDelay(100);
continue;
}
12 changes: 2 additions & 10 deletions firmware/targets/f7/furi_hal/furi_hal_nfc.c
Original file line number Diff line number Diff line change
@@ -48,11 +48,7 @@ void furi_hal_nfc_exit_sleep() {
rfalLowPowerModeStop();
}

bool furi_hal_nfc_detect(
rfalNfcDevice** dev_list,
uint8_t* dev_cnt,
uint32_t timeout,
bool deactivate) {
bool furi_hal_nfc_detect(rfalNfcDevice** dev_list, uint8_t* dev_cnt, uint32_t timeout) {
furi_assert(dev_list);
furi_assert(dev_cnt);

@@ -96,10 +92,6 @@ bool furi_hal_nfc_detect(
osThreadYield();
}
rfalNfcGetDevicesFound(dev_list, dev_cnt);
if(deactivate) {
rfalNfcDeactivate(false);
rfalLowPowerModeStart();
}
return true;
}

@@ -512,7 +504,7 @@ ReturnCode furi_hal_nfc_exchange_full(
return err;
}

void furi_hal_nfc_deactivate() {
void furi_hal_nfc_sleep() {
rfalNfcDeactivate(false);
rfalLowPowerModeStart();
}
8 changes: 2 additions & 6 deletions firmware/targets/furi_hal_include/furi_hal_nfc.h
Original file line number Diff line number Diff line change
@@ -111,11 +111,7 @@ void furi_hal_nfc_exit_sleep();
*
* @return true on success
*/
bool furi_hal_nfc_detect(
rfalNfcDevice** dev_list,
uint8_t* dev_cnt,
uint32_t timeout,
bool deactivate);
bool furi_hal_nfc_detect(rfalNfcDevice** dev_list, uint8_t* dev_cnt, uint32_t timeout);

/** Activate NFC-A tag
*
@@ -207,7 +203,7 @@ ReturnCode furi_hal_nfc_exchange_full(

/** NFC deactivate and start sleep
*/
void furi_hal_nfc_deactivate();
void furi_hal_nfc_sleep();

void furi_hal_nfc_stop();

4 changes: 2 additions & 2 deletions lib/nfc_protocols/mifare_classic.c
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ bool mf_classic_auth_attempt(
}

if(need_halt) {
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
furi_hal_nfc_activate_nfca(300, &auth_ctx->cuid);
}

@@ -248,7 +248,7 @@ bool mf_classic_read_sector(
uint8_t first_block;
bool sector_read = false;

furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
do {
// Activate card
if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;