From e620b310b7cd4d0de5b9b795356c95dae5381bd9 Mon Sep 17 00:00:00 2001 From: gornekich Date: Wed, 26 May 2021 02:54:44 +0300 Subject: [PATCH] [FL-1039] Emv emulation (#491) * nfc: add emulate emv to submenu * api-hal-nfc: add emv emulation --- applications/nfc/nfc.c | 26 +++++- applications/nfc/nfc_i.h | 1 + applications/nfc/nfc_types.h | 2 + applications/nfc/nfc_views.c | 6 ++ applications/nfc/nfc_views.h | 2 + applications/nfc/nfc_worker.c | 72 +++++++++++++--- applications/nfc/nfc_worker_i.h | 2 + .../targets/api-hal-include/api-hal-nfc.h | 7 +- firmware/targets/f5/api-hal/api-hal-nfc.c | 86 +++++++++++++++---- firmware/targets/f6/api-hal/api-hal-nfc.c | 86 +++++++++++++++---- lib/nfc_protocols/emv_decoder.c | 34 ++++++++ lib/nfc_protocols/emv_decoder.h | 6 ++ 12 files changed, 282 insertions(+), 48 deletions(-) mode change 100755 => 100644 applications/nfc/nfc_worker.c diff --git a/applications/nfc/nfc.c b/applications/nfc/nfc.c index d82f32ee..c57e7a74 100755 --- a/applications/nfc/nfc.c +++ b/applications/nfc/nfc.c @@ -27,8 +27,10 @@ void nfc_menu_callback(void* context, uint32_t index) { } else if(index == 1) { message.type = NfcMessageTypeReadEMV; } else if(index == 2) { - message.type = NfcMessageTypeEmulate; + message.type = NfcMessageTypeEmulateEMV; } else if(index == 3) { + message.type = NfcMessageTypeEmulate; + } else if(index == 4) { message.type = NfcMessageTypeField; } furi_check(osMessageQueuePut(message_queue, &message, 0, osWaitForever) == osOK); @@ -52,8 +54,9 @@ Nfc* nfc_alloc() { nfc->submenu = submenu_alloc(); submenu_add_item(nfc->submenu, "Detect", 0, nfc_menu_callback, nfc); submenu_add_item(nfc->submenu, "Read EMV", 1, nfc_menu_callback, nfc); - submenu_add_item(nfc->submenu, "Emulate", 2, nfc_menu_callback, nfc); - submenu_add_item(nfc->submenu, "Field", 3, nfc_menu_callback, nfc); + submenu_add_item(nfc->submenu, "Emulate EMV", 2, nfc_menu_callback, nfc); + submenu_add_item(nfc->submenu, "Emulate", 3, nfc_menu_callback, nfc); + submenu_add_item(nfc->submenu, "Field", 4, nfc_menu_callback, nfc); View* submenu_view = submenu_get_view(nfc->submenu); view_set_previous_callback(submenu_view, nfc_view_exit); view_dispatcher_add_view(nfc->view_dispatcher, NfcViewMenu, submenu_view); @@ -74,6 +77,13 @@ Nfc* nfc_alloc() { view_allocate_model(nfc->view_read_emv, ViewModelTypeLocking, sizeof(NfcViewReadModel)); view_dispatcher_add_view(nfc->view_dispatcher, NfcViewReadEmv, nfc->view_read_emv); + // Emulate EMV + nfc->view_emulate_emv = view_alloc(); + view_set_context(nfc->view_emulate_emv, nfc); + view_set_draw_callback(nfc->view_emulate_emv, nfc_view_emulate_emv_draw); + view_set_previous_callback(nfc->view_emulate_emv, nfc_view_stop); + view_dispatcher_add_view(nfc->view_dispatcher, NfcViewEmulateEMV, nfc->view_emulate_emv); + // Emulate nfc->view_emulate = view_alloc(); view_set_context(nfc->view_emulate, nfc); @@ -118,6 +128,14 @@ void nfc_free(Nfc* nfc) { view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewRead); view_free(nfc->view_detect); + // Read EMV + view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewReadEmv); + view_free(nfc->view_read_emv); + + // Emulate EMV + view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewEmulateEMV); + view_free(nfc->view_emulate_emv); + // Emulate view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewEmulate); view_free(nfc->view_emulate); @@ -214,6 +232,8 @@ int32_t nfc_task(void* p) { return true; }); nfc_start(nfc, NfcViewReadEmv, NfcWorkerStateReadEMV); + } else if(message.type == NfcMessageTypeEmulateEMV) { + nfc_start(nfc, NfcViewEmulateEMV, NfcWorkerStateEmulateEMV); } else if(message.type == NfcMessageTypeEmulate) { nfc_start(nfc, NfcViewEmulate, NfcWorkerStateEmulate); } else if(message.type == NfcMessageTypeField) { diff --git a/applications/nfc/nfc_i.h b/applications/nfc/nfc_i.h index ecc18e52..0ade0471 100644 --- a/applications/nfc/nfc_i.h +++ b/applications/nfc/nfc_i.h @@ -28,6 +28,7 @@ struct Nfc { View* view_detect; View* view_read_emv; + View* view_emulate_emv; View* view_emulate; View* view_field; View* view_cli; diff --git a/applications/nfc/nfc_types.h b/applications/nfc/nfc_types.h index 15441ace..4cf46e97 100644 --- a/applications/nfc/nfc_types.h +++ b/applications/nfc/nfc_types.h @@ -72,6 +72,7 @@ typedef enum { // Main worker states NfcWorkerStatePoll, NfcWorkerStateReadEMV, + NfcWorkerStateEmulateEMV, NfcWorkerStateEmulate, NfcWorkerStateField, // Transition @@ -81,6 +82,7 @@ typedef enum { typedef enum { NfcMessageTypeDetect, NfcMessageTypeReadEMV, + NfcMessageTypeEmulateEMV, NfcMessageTypeEmulate, NfcMessageTypeField, NfcMessageTypeStop, diff --git a/applications/nfc/nfc_views.c b/applications/nfc/nfc_views.c index de2c3e6c..1d10c444 100644 --- a/applications/nfc/nfc_views.c +++ b/applications/nfc/nfc_views.c @@ -133,6 +133,12 @@ void nfc_view_read_emv_draw(Canvas* canvas, void* model) { } } +void nfc_view_emulate_emv_draw(Canvas* canvas, void* model) { + canvas_clear(canvas); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 0, 12, "Emulating EMV"); +} + void nfc_view_emulate_draw(Canvas* canvas, void* model) { canvas_clear(canvas); canvas_set_font(canvas, FontPrimary); diff --git a/applications/nfc/nfc_views.h b/applications/nfc/nfc_views.h index 365186b9..9a4dbc0c 100644 --- a/applications/nfc/nfc_views.h +++ b/applications/nfc/nfc_views.h @@ -11,6 +11,7 @@ typedef enum { NfcViewMenu, NfcViewRead, NfcViewReadEmv, + NfcViewEmulateEMV, NfcViewEmulate, NfcViewField, NfcViewError, @@ -28,6 +29,7 @@ void nfc_view_read_nfcf_draw(Canvas* canvas, NfcViewReadModel* model); void nfc_view_read_nfcv_draw(Canvas* canvas, NfcViewReadModel* model); void nfc_view_read_emv_draw(Canvas* canvas, void* model); +void nfc_view_emulate_emv_draw(Canvas* canvas, void* model); void nfc_view_emulate_draw(Canvas* canvas, void* model); void nfc_view_field_draw(Canvas* canvas, void* model); diff --git a/applications/nfc/nfc_worker.c b/applications/nfc/nfc_worker.c old mode 100755 new mode 100644 index 074974b9..b769d31b --- a/applications/nfc/nfc_worker.c +++ b/applications/nfc/nfc_worker.c @@ -64,6 +64,8 @@ void nfc_worker_task(void* context) { nfc_worker_poll(nfc_worker); } else if(nfc_worker->state == NfcWorkerStateReadEMV) { nfc_worker_read_emv(nfc_worker); + } else if(nfc_worker->state == NfcWorkerStateEmulateEMV) { + nfc_worker_emulate_emv(nfc_worker); } else if(nfc_worker->state == NfcWorkerStateEmulate) { nfc_worker_emulate(nfc_worker); } else if(nfc_worker->state == NfcWorkerStateField) { @@ -77,7 +79,6 @@ void nfc_worker_task(void* context) { void nfc_worker_read_emv(NfcWorker* nfc_worker) { ReturnCode err; rfalNfcDevice* dev_list; - rfalNfcDevice* dev_active; EmvApplication emv_app = {}; uint8_t dev_cnt = 0; uint8_t tx_buff[255] = {}; @@ -94,11 +95,9 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { if(api_hal_nfc_detect(&dev_list, &dev_cnt, 100, false)) { // Card was found. Check that it supports EMV if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) { - dev_active = &dev_list[0]; FURI_LOG_I(NFC_WORKER_TAG, "Send select PPSE command"); tx_len = emv_prepare_select_ppse(tx_buff); - err = api_hal_nfc_data_exchange( - dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); if(err != ERR_NONE) { FURI_LOG_E(NFC_WORKER_TAG, "Error during selection PPSE request: %d", err); message.type = NfcMessageTypeEMVNotFound; @@ -117,8 +116,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { } FURI_LOG_I(NFC_WORKER_TAG, "Starting application ..."); tx_len = emv_prepare_select_app(tx_buff, &emv_app); - err = api_hal_nfc_data_exchange( - dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); if(err != ERR_NONE) { FURI_LOG_E( NFC_WORKER_TAG, "Error during application selection request: %d", err); @@ -140,8 +138,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { } FURI_LOG_I(NFC_WORKER_TAG, "Starting Get Processing Options command ..."); tx_len = emv_prepare_get_proc_opt(tx_buff, &emv_app); - err = api_hal_nfc_data_exchange( - dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); if(err != ERR_NONE) { FURI_LOG_E( NFC_WORKER_TAG, "Error during Get Processing Options command: %d", err); @@ -171,7 +168,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { for(uint8_t record = record_start; record <= record_end; ++record) { tx_len = emv_prepare_read_sfi_record(tx_buff, sfi, record); err = api_hal_nfc_data_exchange( - dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false); + tx_buff, tx_len, &rx_buff, &rx_len, false); if(err != ERR_NONE) { FURI_LOG_E( NFC_WORKER_TAG, @@ -215,6 +212,61 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { api_hal_nfc_deactivate(); } +void nfc_worker_emulate_emv(NfcWorker* nfc_worker) { + ReturnCode err; + uint8_t tx_buff[255] = {}; + uint16_t tx_len = 0; + uint8_t* rx_buff; + uint16_t* rx_len; + + while(nfc_worker->state == NfcWorkerStateEmulateEMV) { + if(api_hal_nfc_listen(1000)) { + FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected"); + // Read data from POS terminal + err = api_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false); + if(err == ERR_NONE) { + FURI_LOG_I(NFC_WORKER_TAG, "Received Select PPSE"); + } else { + FURI_LOG_E(NFC_WORKER_TAG, "Error in 1st data exchange: select PPSE"); + api_hal_nfc_deactivate(); + continue; + } + FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT PPSE ANS"); + tx_len = emv_select_ppse_ans(tx_buff); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); + if(err == ERR_NONE) { + FURI_LOG_I(NFC_WORKER_TAG, "Received Select APP"); + } else { + FURI_LOG_E(NFC_WORKER_TAG, "Error in 2nd data exchange: select APP"); + api_hal_nfc_deactivate(); + continue; + } + + FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT APP ANS"); + tx_len = emv_select_app_ans(tx_buff); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); + if(err == ERR_NONE) { + FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL"); + } else { + FURI_LOG_E(NFC_WORKER_TAG, "Error in 3rd data exchange: receive PDOL"); + api_hal_nfc_deactivate(); + continue; + } + + FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS"); + tx_len = emv_get_proc_opt_ans(tx_buff); + err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false); + if(err == ERR_NONE) { + FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL"); + } + api_hal_nfc_deactivate(); + } else { + FURI_LOG_W(NFC_WORKER_TAG, "Can't find reader"); + } + osDelay(20); + } +} + void nfc_worker_poll(NfcWorker* nfc_worker) { rfalNfcDevice* dev_list; uint8_t dev_cnt; @@ -249,7 +301,7 @@ void nfc_worker_poll(NfcWorker* nfc_worker) { furi_check( osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); } - osDelay(20); + osDelay(5); } } diff --git a/applications/nfc/nfc_worker_i.h b/applications/nfc/nfc_worker_i.h index 7877aca5..ba85f4b6 100644 --- a/applications/nfc/nfc_worker_i.h +++ b/applications/nfc/nfc_worker_i.h @@ -31,6 +31,8 @@ void nfc_worker_task(void* context); void nfc_worker_read_emv(NfcWorker* nfc_worker); +void nfc_worker_emulate_emv(NfcWorker* nfc_worker); + void nfc_worker_poll(NfcWorker* nfc_worker); void nfc_worker_emulate(NfcWorker* nfc_worker); diff --git a/firmware/targets/api-hal-include/api-hal-nfc.h b/firmware/targets/api-hal-include/api-hal-nfc.h index bded81d7..08767d03 100644 --- a/firmware/targets/api-hal-include/api-hal-nfc.h +++ b/firmware/targets/api-hal-include/api-hal-nfc.h @@ -44,10 +44,15 @@ void api_hal_nfc_exit_sleep(); */ bool api_hal_nfc_detect(rfalNfcDevice** dev_list, uint8_t* dev_cnt, uint32_t cycles, bool deactivate); +/** + * NFC listen + */ +bool api_hal_nfc_listen(uint32_t timeout); + /** * NFC data exchange */ -ReturnCode api_hal_nfc_data_exchange(rfalNfcDevice* dev, uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate); +ReturnCode api_hal_nfc_data_exchange(uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate); /** * NFC deactivate and start sleep diff --git a/firmware/targets/f5/api-hal/api-hal-nfc.c b/firmware/targets/f5/api-hal/api-hal-nfc.c index ecc4825a..d7736569 100644 --- a/firmware/targets/f5/api-hal/api-hal-nfc.c +++ b/firmware/targets/f5/api-hal/api-hal-nfc.c @@ -2,6 +2,7 @@ #include static bool dev_is_found = false; +const uint32_t clocks_in_ms = 64 * 1000; ReturnCode api_hal_nfc_init() { // Check if Nfc worker was started @@ -72,7 +73,7 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc FURI_LOG_D("HAL NFC", "Found %d devices", dev_cnt); break; } - osDelay(5); + osDelay(10); } if(deactivate) { rfalNfcDeactivate(false); @@ -86,35 +87,86 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc return true; } -ReturnCode api_hal_nfc_data_exchange(rfalNfcDevice* dev, uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate) { - furi_assert(dev); - furi_assert(tx_buff); +bool api_hal_nfc_listen(uint32_t timeout) { + api_hal_nfc_exit_sleep(); + rfalLowPowerModeStop(); + + rfalNfcState state = rfalNfcGetState(); + if(state == RFAL_NFC_STATE_NOTINIT) { + rfalNfcInitialize(); + } else if(state >= RFAL_NFC_STATE_ACTIVATED) { + rfalNfcDeactivate(false); + } + rfalNfcDiscoverParam params; + params.compMode = RFAL_COMPLIANCE_MODE_EMV; + params.techs2Find = RFAL_NFC_LISTEN_TECH_A; + params.totalDuration = 1000; + params.devLimit = 1; + params.wakeupEnabled = false; + params.wakeupConfigDefault = true; + params.nfcfBR = RFAL_BR_212; + params.ap2pBR = RFAL_BR_424; + params.maxBR = RFAL_BR_KEEP; + params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; + params.notifyCb = NULL; + + params.lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_04; + params.lmConfigPA.nfcid[0] = 0XCF; + params.lmConfigPA.nfcid[1] = 0x72; + params.lmConfigPA.nfcid[2] = 0xD4; + params.lmConfigPA.nfcid[3] = 0x40; + params.lmConfigPA.SENS_RES[0] = 0x04; + params.lmConfigPA.SENS_RES[1] = 0x00; + params.lmConfigPA.SEL_RES = 0x20; + + uint32_t start = DWT->CYCCNT; + rfalNfcDiscover(¶ms); + while(state != RFAL_NFC_STATE_ACTIVATED) { + rfalNfcWorker(); + state = rfalNfcGetState(); + FURI_LOG_D("HAL NFC", "Current state %d", state); + if(DWT->CYCCNT - start > timeout * clocks_in_ms) { + rfalNfcDeactivate(true); + return false; + } + if(state == RFAL_NFC_STATE_LISTEN_ACTIVATION) { + continue; + } + taskYIELD(); + } + + return true; +} + +ReturnCode api_hal_nfc_data_exchange(uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate) { furi_assert(rx_buff); furi_assert(rx_len); ReturnCode ret; - rfalNfcDevice* active_dev; rfalNfcState state = RFAL_NFC_STATE_ACTIVATED; - - ret = rfalNfcGetActiveDevice(&active_dev); - if(ret != ERR_NONE) { - return ret; - } - if (active_dev != dev) { - return ERR_NOTFOUND; - } ret = rfalNfcDataExchangeStart(tx_buff, tx_len, rx_buff, rx_len, 0); if(ret != ERR_NONE) { return ret; } - FURI_LOG_D("HAL NFC", "Start data exchange"); + uint32_t start = DWT->CYCCNT; while(state != RFAL_NFC_STATE_DATAEXCHANGE_DONE) { rfalNfcWorker(); state = rfalNfcGetState(); - FURI_LOG_D("HAL NFC", "Data exchange status: %d", rfalNfcDataExchangeGetStatus()); - osDelay(10); + ret = rfalNfcDataExchangeGetStatus(); + FURI_LOG_D("HAL NFC", "Nfc st: %d Data st: %d", state, ret); + if(ret > ERR_SLEEP_REQ) { + return ret; + } + if(ret == ERR_BUSY) { + if(DWT->CYCCNT - start > 1000 * clocks_in_ms) { + return ERR_TIMEOUT; + } + continue; + } else { + start = DWT->CYCCNT; + } + taskYIELD(); } - FURI_LOG_D("HAL NFC", "Data exchange complete"); if(deactivate) { rfalNfcDeactivate(false); rfalLowPowerModeStart(); diff --git a/firmware/targets/f6/api-hal/api-hal-nfc.c b/firmware/targets/f6/api-hal/api-hal-nfc.c index ecc4825a..d7736569 100644 --- a/firmware/targets/f6/api-hal/api-hal-nfc.c +++ b/firmware/targets/f6/api-hal/api-hal-nfc.c @@ -2,6 +2,7 @@ #include static bool dev_is_found = false; +const uint32_t clocks_in_ms = 64 * 1000; ReturnCode api_hal_nfc_init() { // Check if Nfc worker was started @@ -72,7 +73,7 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc FURI_LOG_D("HAL NFC", "Found %d devices", dev_cnt); break; } - osDelay(5); + osDelay(10); } if(deactivate) { rfalNfcDeactivate(false); @@ -86,35 +87,86 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc return true; } -ReturnCode api_hal_nfc_data_exchange(rfalNfcDevice* dev, uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate) { - furi_assert(dev); - furi_assert(tx_buff); +bool api_hal_nfc_listen(uint32_t timeout) { + api_hal_nfc_exit_sleep(); + rfalLowPowerModeStop(); + + rfalNfcState state = rfalNfcGetState(); + if(state == RFAL_NFC_STATE_NOTINIT) { + rfalNfcInitialize(); + } else if(state >= RFAL_NFC_STATE_ACTIVATED) { + rfalNfcDeactivate(false); + } + rfalNfcDiscoverParam params; + params.compMode = RFAL_COMPLIANCE_MODE_EMV; + params.techs2Find = RFAL_NFC_LISTEN_TECH_A; + params.totalDuration = 1000; + params.devLimit = 1; + params.wakeupEnabled = false; + params.wakeupConfigDefault = true; + params.nfcfBR = RFAL_BR_212; + params.ap2pBR = RFAL_BR_424; + params.maxBR = RFAL_BR_KEEP; + params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; + params.notifyCb = NULL; + + params.lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_04; + params.lmConfigPA.nfcid[0] = 0XCF; + params.lmConfigPA.nfcid[1] = 0x72; + params.lmConfigPA.nfcid[2] = 0xD4; + params.lmConfigPA.nfcid[3] = 0x40; + params.lmConfigPA.SENS_RES[0] = 0x04; + params.lmConfigPA.SENS_RES[1] = 0x00; + params.lmConfigPA.SEL_RES = 0x20; + + uint32_t start = DWT->CYCCNT; + rfalNfcDiscover(¶ms); + while(state != RFAL_NFC_STATE_ACTIVATED) { + rfalNfcWorker(); + state = rfalNfcGetState(); + FURI_LOG_D("HAL NFC", "Current state %d", state); + if(DWT->CYCCNT - start > timeout * clocks_in_ms) { + rfalNfcDeactivate(true); + return false; + } + if(state == RFAL_NFC_STATE_LISTEN_ACTIVATION) { + continue; + } + taskYIELD(); + } + + return true; +} + +ReturnCode api_hal_nfc_data_exchange(uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate) { furi_assert(rx_buff); furi_assert(rx_len); ReturnCode ret; - rfalNfcDevice* active_dev; rfalNfcState state = RFAL_NFC_STATE_ACTIVATED; - - ret = rfalNfcGetActiveDevice(&active_dev); - if(ret != ERR_NONE) { - return ret; - } - if (active_dev != dev) { - return ERR_NOTFOUND; - } ret = rfalNfcDataExchangeStart(tx_buff, tx_len, rx_buff, rx_len, 0); if(ret != ERR_NONE) { return ret; } - FURI_LOG_D("HAL NFC", "Start data exchange"); + uint32_t start = DWT->CYCCNT; while(state != RFAL_NFC_STATE_DATAEXCHANGE_DONE) { rfalNfcWorker(); state = rfalNfcGetState(); - FURI_LOG_D("HAL NFC", "Data exchange status: %d", rfalNfcDataExchangeGetStatus()); - osDelay(10); + ret = rfalNfcDataExchangeGetStatus(); + FURI_LOG_D("HAL NFC", "Nfc st: %d Data st: %d", state, ret); + if(ret > ERR_SLEEP_REQ) { + return ret; + } + if(ret == ERR_BUSY) { + if(DWT->CYCCNT - start > 1000 * clocks_in_ms) { + return ERR_TIMEOUT; + } + continue; + } else { + start = DWT->CYCCNT; + } + taskYIELD(); } - FURI_LOG_D("HAL NFC", "Data exchange complete"); if(deactivate) { rfalNfcDeactivate(false); rfalLowPowerModeStart(); diff --git a/lib/nfc_protocols/emv_decoder.c b/lib/nfc_protocols/emv_decoder.c index 527811f5..de3dae0d 100755 --- a/lib/nfc_protocols/emv_decoder.c +++ b/lib/nfc_protocols/emv_decoder.c @@ -37,6 +37,25 @@ const PDOLValue* pdol_values[] = { &pdol_unpredict_number, }; +static const uint8_t select_ppse_ans[] = { + 0x6F, 0x29, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, + 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, + 0xA5, 0x17, 0xBF, 0x0C, 0x14, 0x61, 0x12, 0x4F, 0x07, + 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x50, 0x04, + 0x56, 0x49, 0x53, 0x41, 0x87, 0x01, 0x01, 0x90, 0x00}; +static const uint8_t select_app_ans[] = { + 0x6F, 0x20, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, + 0x10, 0x10, 0xA5, 0x15, 0x50, 0x04, 0x56, 0x49, 0x53, + 0x41, 0x9F, 0x38, 0x0C, 0x9F, 0x66, 0x04, 0x9F, 0x02, + 0x06, 0x9F, 0x37, 0x04, 0x5F, 0x2A, 0x02, 0x90, 0x00}; +static const uint8_t pdol_ans[] = { + 0x77, 0x40, 0x82, 0x02, 0x20, 0x00, 0x57, 0x13, 0x55, 0x70, 0x73, 0x83, + 0x85, 0x87, 0x73, 0x31, 0xD1, 0x80, 0x22, 0x01, 0x38, 0x84, 0x77, 0x94, + 0x00, 0x00, 0x1F, 0x5F, 0x34, 0x01, 0x00, 0x9F, 0x10, 0x07, 0x06, 0x01, + 0x11, 0x03, 0x80, 0x00, 0x00, 0x9F, 0x26, 0x08, 0x7A, 0x65, 0x7F, 0xD3, + 0x52, 0x96, 0xC9, 0x85, 0x9F, 0x27, 0x01, 0x00, 0x9F, 0x36, 0x02, 0x06, + 0x0C, 0x9F, 0x6C, 0x02, 0x10, 0x00, 0x90, 0x00}; + static uint16_t emv_parse_TLV(uint8_t* dest, uint8_t* src, uint16_t* idx) { uint8_t len = src[*idx + 1]; memcpy(dest, &src[*idx + 2], len); @@ -203,3 +222,18 @@ bool emv_decode_read_sfi_record(uint8_t* buff, uint16_t len, EmvApplication* app } return false; } + +uint16_t emv_select_ppse_ans(uint8_t* buff) { + memcpy(buff, select_ppse_ans, sizeof(select_ppse_ans)); + return sizeof(select_ppse_ans); +} + +uint16_t emv_select_app_ans(uint8_t* buff) { + memcpy(buff, select_app_ans, sizeof(select_app_ans)); + return sizeof(select_app_ans); +} + +uint16_t emv_get_proc_opt_ans(uint8_t* buff) { + memcpy(buff, pdol_ans, sizeof(pdol_ans)); + return sizeof(pdol_ans); +} diff --git a/lib/nfc_protocols/emv_decoder.h b/lib/nfc_protocols/emv_decoder.h index 8239218d..31401f80 100755 --- a/lib/nfc_protocols/emv_decoder.h +++ b/lib/nfc_protocols/emv_decoder.h @@ -37,6 +37,7 @@ typedef struct { APDU afl; } EmvApplication; +/* Terminal emulation */ uint16_t emv_prepare_select_ppse(uint8_t* dest); bool emv_decode_ppse_response(uint8_t* buff, uint16_t len, EmvApplication* app); @@ -48,3 +49,8 @@ bool emv_decode_get_proc_opt(uint8_t* buff, uint16_t len, EmvApplication* app); uint16_t emv_prepare_read_sfi_record(uint8_t* dest, uint8_t sfi, uint8_t record_num); bool emv_decode_read_sfi_record(uint8_t* buff, uint16_t len, EmvApplication* app); + +/* Card emulation */ +uint16_t emv_select_ppse_ans(uint8_t* buff); +uint16_t emv_select_app_ans(uint8_t* buff); +uint16_t emv_get_proc_opt_ans(uint8_t* buff);