[FL-1442] Fix nfc emulation (#520)
* api-hal-nfc: rework nfc detection * nfc: increase nfc detection timeout * api-hal-nfc: add 2 emulation parameters for mifare and emv * nfc: choose emulation parameters
This commit is contained in:
		
							parent
							
								
									9e69164b1b
								
							
						
					
					
						commit
						9943c93189
					
				| @ -24,7 +24,7 @@ void nfc_cli_detect(Cli* cli, string_t args, void* context) { | |||||||
|     printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n"); |     printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n"); | ||||||
|     while(!cmd_exit) { |     while(!cmd_exit) { | ||||||
|         cmd_exit |= cli_cmd_interrupt_received(cli); |         cmd_exit |= cli_cmd_interrupt_received(cli); | ||||||
|         cmd_exit |= api_hal_nfc_detect(&dev_list, &dev_cnt, 100, true); |         cmd_exit |= api_hal_nfc_detect(&dev_list, &dev_cnt, 1000, true); | ||||||
|         if(dev_cnt > 0) { |         if(dev_cnt > 0) { | ||||||
|             printf("Found %d devices\r\n", dev_cnt); |             printf("Found %d devices\r\n", dev_cnt); | ||||||
|             for(uint8_t i = 0; i < dev_cnt; i++) { |             for(uint8_t i = 0; i < dev_cnt; i++) { | ||||||
| @ -57,7 +57,7 @@ void nfc_cli_emulate(Cli* cli, string_t args, void* context) { | |||||||
|     printf("Press Ctrl+C to abort\r\n"); |     printf("Press Ctrl+C to abort\r\n"); | ||||||
| 
 | 
 | ||||||
|     while(!cli_cmd_interrupt_received(cli)) { |     while(!cli_cmd_interrupt_received(cli)) { | ||||||
|         if(api_hal_nfc_listen(100)) { |         if(api_hal_nfc_listen(ApiHalNfcEmulateParamsMifare, 100)) { | ||||||
|             printf("Reader detected\r\n"); |             printf("Reader detected\r\n"); | ||||||
|             api_hal_nfc_deactivate(); |             api_hal_nfc_deactivate(); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -97,7 +97,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) { | |||||||
|         furi_check( |         furi_check( | ||||||
|             osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); |             osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); | ||||||
|         memset(&emv_app, 0, sizeof(emv_app)); |         memset(&emv_app, 0, sizeof(emv_app)); | ||||||
|         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 100, false)) { |         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) { | ||||||
|             // Card was found. Check that it supports EMV
 |             // Card was found. Check that it supports EMV
 | ||||||
|             if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) { |             if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) { | ||||||
|                 FURI_LOG_I(NFC_WORKER_TAG, "Send select PPSE command"); |                 FURI_LOG_I(NFC_WORKER_TAG, "Send select PPSE command"); | ||||||
| @ -224,7 +224,7 @@ void nfc_worker_emulate_emv(NfcWorker* nfc_worker) { | |||||||
|     uint16_t* rx_len; |     uint16_t* rx_len; | ||||||
| 
 | 
 | ||||||
|     while(nfc_worker->state == NfcWorkerStateEmulateEMV) { |     while(nfc_worker->state == NfcWorkerStateEmulateEMV) { | ||||||
|         if(api_hal_nfc_listen(1000)) { |         if(api_hal_nfc_listen(ApiHalNfcEmulateParamsEMV, 1000)) { | ||||||
|             FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected"); |             FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected"); | ||||||
|             // Read data from POS terminal
 |             // Read data from POS terminal
 | ||||||
|             err = api_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false); |             err = api_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false); | ||||||
| @ -279,7 +279,7 @@ void nfc_worker_poll(NfcWorker* nfc_worker) { | |||||||
|     furi_check(osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); |     furi_check(osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); | ||||||
| 
 | 
 | ||||||
|     while(nfc_worker->state == NfcWorkerStatePoll) { |     while(nfc_worker->state == NfcWorkerStatePoll) { | ||||||
|         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 100, true)) { |         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 1000, true)) { | ||||||
|             // Send message with first device found
 |             // Send message with first device found
 | ||||||
|             message.type = NfcMessageTypeDeviceFound; |             message.type = NfcMessageTypeDeviceFound; | ||||||
|             if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA) { |             if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA) { | ||||||
| @ -326,7 +326,7 @@ void nfc_worker_read_mf_ultralight(NfcWorker* nfc_worker) { | |||||||
|             osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); |             osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK); | ||||||
|         api_hal_nfc_deactivate(); |         api_hal_nfc_deactivate(); | ||||||
|         memset(&mf_ul_read, 0, sizeof(mf_ul_read)); |         memset(&mf_ul_read, 0, sizeof(mf_ul_read)); | ||||||
|         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 100, false)) { |         if(api_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) { | ||||||
|             if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA && |             if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA && | ||||||
|                mf_ul_check_card_type( |                mf_ul_check_card_type( | ||||||
|                    dev_list[0].dev.nfca.sensRes.anticollisionInfo, |                    dev_list[0].dev.nfca.sensRes.anticollisionInfo, | ||||||
| @ -352,7 +352,7 @@ void nfc_worker_read_mf_ultralight(NfcWorker* nfc_worker) { | |||||||
|                     mf_ul_set_default_version(&mf_ul_read); |                     mf_ul_set_default_version(&mf_ul_read); | ||||||
|                     // Reinit device
 |                     // Reinit device
 | ||||||
|                     api_hal_nfc_deactivate(); |                     api_hal_nfc_deactivate(); | ||||||
|                     if(!api_hal_nfc_detect(&dev_list, &dev_cnt, 100, false)) { |                     if(!api_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) { | ||||||
|                         FURI_LOG_E(NFC_WORKER_TAG, "Lost connection. Restarting search"); |                         FURI_LOG_E(NFC_WORKER_TAG, "Lost connection. Restarting search"); | ||||||
|                         message.type = NfcMessageTypeMfUlNotFound; |                         message.type = NfcMessageTypeMfUlNotFound; | ||||||
|                         continue; |                         continue; | ||||||
| @ -431,7 +431,7 @@ void nfc_worker_read_mf_ultralight(NfcWorker* nfc_worker) { | |||||||
| 
 | 
 | ||||||
| void nfc_worker_emulate(NfcWorker* nfc_worker) { | void nfc_worker_emulate(NfcWorker* nfc_worker) { | ||||||
|     while(nfc_worker->state == NfcWorkerStateEmulate) { |     while(nfc_worker->state == NfcWorkerStateEmulate) { | ||||||
|         if(api_hal_nfc_listen(100)) { |         if(api_hal_nfc_listen(ApiHalNfcEmulateParamsMifare, 100)) { | ||||||
|             FURI_LOG_I(NFC_WORKER_TAG, "Reader detected"); |             FURI_LOG_I(NFC_WORKER_TAG, "Reader detected"); | ||||||
|             api_hal_nfc_deactivate(); |             api_hal_nfc_deactivate(); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -9,6 +9,12 @@ | |||||||
| extern "C" { | extern "C" { | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | /** Nfc emulation parameters */ | ||||||
|  | typedef enum { | ||||||
|  |     ApiHalNfcEmulateParamsMifare, | ||||||
|  |     ApiHalNfcEmulateParamsEMV, | ||||||
|  | } ApiHalNfcEmulateParams; | ||||||
|  | 
 | ||||||
| /**
 | /**
 | ||||||
|  * Init nfc |  * Init nfc | ||||||
|  */ |  */ | ||||||
| @ -42,12 +48,12 @@ void api_hal_nfc_exit_sleep(); | |||||||
| /**
 | /**
 | ||||||
|  * NFC poll |  * NFC poll | ||||||
|  */ |  */ | ||||||
| bool api_hal_nfc_detect(rfalNfcDevice** dev_list, uint8_t* dev_cnt, uint32_t cycles, bool deactivate); | bool api_hal_nfc_detect(rfalNfcDevice** dev_list, uint8_t* dev_cnt, uint32_t timeout, bool deactivate); | ||||||
| 
 | 
 | ||||||
| /**
 | /**
 | ||||||
|  * NFC listen |  * NFC listen | ||||||
|  */ |  */ | ||||||
| bool api_hal_nfc_listen(uint32_t timeout); | bool api_hal_nfc_listen(ApiHalNfcEmulateParams params, uint32_t timeout); | ||||||
| 
 | 
 | ||||||
| /**
 | /**
 | ||||||
|  * NFC data exchange |  * NFC data exchange | ||||||
|  | |||||||
| @ -1,8 +1,54 @@ | |||||||
| #include "api-hal-nfc.h" | #include "api-hal-nfc.h" | ||||||
| #include <st25r3916.h> | #include <st25r3916.h> | ||||||
| 
 | 
 | ||||||
| static bool dev_is_found = false; | static const uint32_t clocks_in_ms = 64 * 1000; | ||||||
| const uint32_t clocks_in_ms = 64 * 1000; | 
 | ||||||
|  | static const rfalNfcDiscoverParam api_hal_nfc_emulate_params_mifare = { | ||||||
|  |     .compMode = RFAL_COMPLIANCE_MODE_NFC, | ||||||
|  |     .techs2Find = RFAL_NFC_LISTEN_TECH_A, | ||||||
|  |     .totalDuration = 1000, | ||||||
|  |     .devLimit = 1, | ||||||
|  |     .wakeupEnabled = false, | ||||||
|  |     .wakeupConfigDefault = true, | ||||||
|  |     .nfcfBR = RFAL_BR_212, | ||||||
|  |     .ap2pBR = RFAL_BR_424, | ||||||
|  |     .maxBR = RFAL_BR_KEEP, | ||||||
|  |     .GBLen = RFAL_NFCDEP_GB_MAX_LEN, | ||||||
|  |     .notifyCb = NULL, | ||||||
|  |     .lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_07, | ||||||
|  |     .lmConfigPA.nfcid[0] = 0X36, | ||||||
|  |     .lmConfigPA.nfcid[1] = 0x9C, | ||||||
|  |     .lmConfigPA.nfcid[2] = 0xE7, | ||||||
|  |     .lmConfigPA.nfcid[3] = 0xB1, | ||||||
|  |     .lmConfigPA.nfcid[4] = 0x0A, | ||||||
|  |     .lmConfigPA.nfcid[5] = 0xC1, | ||||||
|  |     .lmConfigPA.nfcid[6] = 0x34, | ||||||
|  |     .lmConfigPA.SENS_RES[0] = 0x44, | ||||||
|  |     .lmConfigPA.SENS_RES[1] = 0x00, | ||||||
|  |     .lmConfigPA.SEL_RES = 0x00, | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | static const rfalNfcDiscoverParam api_hal_nfc_emulate_params_emv = { | ||||||
|  |     .compMode = RFAL_COMPLIANCE_MODE_EMV, | ||||||
|  |     .techs2Find = RFAL_NFC_LISTEN_TECH_A, | ||||||
|  |     .totalDuration = 1000, | ||||||
|  |     .devLimit = 1, | ||||||
|  |     .wakeupEnabled = false, | ||||||
|  |     .wakeupConfigDefault = true, | ||||||
|  |     .nfcfBR = RFAL_BR_212, | ||||||
|  |     .ap2pBR = RFAL_BR_424, | ||||||
|  |     .maxBR = RFAL_BR_KEEP, | ||||||
|  |     .GBLen = RFAL_NFCDEP_GB_MAX_LEN, | ||||||
|  |     .notifyCb = NULL, | ||||||
|  |     .lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_04, | ||||||
|  |     .lmConfigPA.nfcid[0] = 0XCF, | ||||||
|  |     .lmConfigPA.nfcid[1] = 0x72, | ||||||
|  |     .lmConfigPA.nfcid[2] = 0xD4, | ||||||
|  |     .lmConfigPA.nfcid[3] = 0x40, | ||||||
|  |     .lmConfigPA.SENS_RES[0] = 0x04, | ||||||
|  |     .lmConfigPA.SENS_RES[1] = 0x00, | ||||||
|  |     .lmConfigPA.SEL_RES = 0x20, | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| ReturnCode api_hal_nfc_init() { | ReturnCode api_hal_nfc_init() { | ||||||
|     // Check if Nfc worker was started
 |     // Check if Nfc worker was started
 | ||||||
| @ -34,19 +80,13 @@ void api_hal_nfc_exit_sleep() { | |||||||
|     rfalLowPowerModeStop(); |     rfalLowPowerModeStop(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void api_hal_nfc_change_state_cb(rfalNfcState st) { | bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t timeout, bool deactivate) { | ||||||
|     FURI_LOG_D("HAL NFC", "NFC worker state: %d", st); |  | ||||||
|     if(st >= RFAL_NFC_STATE_POLL_SELECT) { |  | ||||||
|         dev_is_found = true; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cycles, bool deactivate) { |  | ||||||
|     furi_assert(dev_list); |     furi_assert(dev_list); | ||||||
|     furi_assert(dev_cnt); |     furi_assert(dev_cnt); | ||||||
| 
 | 
 | ||||||
|     rfalLowPowerModeStop(); |     rfalLowPowerModeStop(); | ||||||
|     if(rfalNfcGetState() == RFAL_NFC_STATE_NOTINIT) { |     rfalNfcState state = rfalNfcGetState(); | ||||||
|  |     if(state == RFAL_NFC_STATE_NOTINIT) { | ||||||
|         rfalNfcInitialize(); |         rfalNfcInitialize(); | ||||||
|     } |     } | ||||||
|     rfalNfcDiscoverParam params; |     rfalNfcDiscoverParam params; | ||||||
| @ -61,33 +101,37 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc | |||||||
|     params.ap2pBR = RFAL_BR_424; |     params.ap2pBR = RFAL_BR_424; | ||||||
|     params.maxBR = RFAL_BR_KEEP; |     params.maxBR = RFAL_BR_KEEP; | ||||||
|     params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; |     params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; | ||||||
|     params.notifyCb = api_hal_nfc_change_state_cb; |     params.notifyCb = NULL; | ||||||
| 
 | 
 | ||||||
|     dev_is_found = false; |     uint32_t start = DWT->CYCCNT; | ||||||
|     rfalNfcDiscover(¶ms); |     rfalNfcDiscover(¶ms); | ||||||
|     while(--cycles) { |     while(state != RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcWorker(); |         rfalNfcWorker(); | ||||||
|         FURI_LOG_D("HAL NFC", "Current state %d", rfalNfcGetState()); |         state = rfalNfcGetState(); | ||||||
|         if(dev_is_found) { |         FURI_LOG_D("HAL NFC", "Current state %d", state); | ||||||
|  |         if(state == RFAL_NFC_STATE_POLL_ACTIVATION) { | ||||||
|  |             start = DWT->CYCCNT; | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  |         if(state == RFAL_NFC_STATE_POLL_SELECT) { | ||||||
|  |             rfalNfcSelect(0); | ||||||
|  |         } | ||||||
|  |         if(DWT->CYCCNT - start > timeout * clocks_in_ms) { | ||||||
|  |             rfalNfcDeactivate(true); | ||||||
|  |             FURI_LOG_D("HAL NFC", "Timeout"); | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |         osThreadYield(); | ||||||
|  |     } | ||||||
|     rfalNfcGetDevicesFound(dev_list, dev_cnt); |     rfalNfcGetDevicesFound(dev_list, dev_cnt); | ||||||
|             FURI_LOG_D("HAL NFC", "Found %d devices", dev_cnt); |  | ||||||
|             break; |  | ||||||
|         } |  | ||||||
|         osDelay(10); |  | ||||||
|     } |  | ||||||
|     if(deactivate) { |     if(deactivate) { | ||||||
|         rfalNfcDeactivate(false); |         rfalNfcDeactivate(false); | ||||||
|         rfalLowPowerModeStart(); |         rfalLowPowerModeStart(); | ||||||
|     } |     } | ||||||
|     if(!cycles) { |  | ||||||
|         FURI_LOG_D("HAL NFC", "Timeout"); |  | ||||||
|         return false; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool api_hal_nfc_listen(uint32_t timeout) { | bool api_hal_nfc_listen(ApiHalNfcEmulateParams params, uint32_t timeout) { | ||||||
|     api_hal_nfc_exit_sleep(); |     api_hal_nfc_exit_sleep(); | ||||||
| 
 | 
 | ||||||
|     rfalNfcState state = rfalNfcGetState(); |     rfalNfcState state = rfalNfcGetState(); | ||||||
| @ -96,30 +140,14 @@ bool api_hal_nfc_listen(uint32_t timeout) { | |||||||
|     } else if(state >= RFAL_NFC_STATE_ACTIVATED) { |     } else if(state >= RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcDeactivate(false); |         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; |     if(params == ApiHalNfcEmulateParamsMifare) { | ||||||
|     params.lmConfigPA.nfcid[0] = 0XCF; |         rfalNfcDiscover(&api_hal_nfc_emulate_params_mifare); | ||||||
|     params.lmConfigPA.nfcid[1] = 0x72; |     } else if(params == ApiHalNfcEmulateParamsEMV) { | ||||||
|     params.lmConfigPA.nfcid[2] = 0xD4; |         rfalNfcDiscover(&api_hal_nfc_emulate_params_emv); | ||||||
|     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; |     uint32_t start = DWT->CYCCNT; | ||||||
|     rfalNfcDiscover(¶ms); |  | ||||||
|     while(state != RFAL_NFC_STATE_ACTIVATED) { |     while(state != RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcWorker(); |         rfalNfcWorker(); | ||||||
|         state = rfalNfcGetState(); |         state = rfalNfcGetState(); | ||||||
| @ -132,9 +160,8 @@ bool api_hal_nfc_listen(uint32_t timeout) { | |||||||
|             start = DWT->CYCCNT; |             start = DWT->CYCCNT; | ||||||
|             continue; |             continue; | ||||||
|         } |         } | ||||||
|         taskYIELD(); |         osThreadYield(); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,8 +1,54 @@ | |||||||
| #include "api-hal-nfc.h" | #include "api-hal-nfc.h" | ||||||
| #include <st25r3916.h> | #include <st25r3916.h> | ||||||
| 
 | 
 | ||||||
| static bool dev_is_found = false; | static const uint32_t clocks_in_ms = 64 * 1000; | ||||||
| const uint32_t clocks_in_ms = 64 * 1000; | 
 | ||||||
|  | static const rfalNfcDiscoverParam api_hal_nfc_emulate_params_mifare = { | ||||||
|  |     .compMode = RFAL_COMPLIANCE_MODE_NFC, | ||||||
|  |     .techs2Find = RFAL_NFC_LISTEN_TECH_A, | ||||||
|  |     .totalDuration = 1000, | ||||||
|  |     .devLimit = 1, | ||||||
|  |     .wakeupEnabled = false, | ||||||
|  |     .wakeupConfigDefault = true, | ||||||
|  |     .nfcfBR = RFAL_BR_212, | ||||||
|  |     .ap2pBR = RFAL_BR_424, | ||||||
|  |     .maxBR = RFAL_BR_KEEP, | ||||||
|  |     .GBLen = RFAL_NFCDEP_GB_MAX_LEN, | ||||||
|  |     .notifyCb = NULL, | ||||||
|  |     .lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_07, | ||||||
|  |     .lmConfigPA.nfcid[0] = 0X36, | ||||||
|  |     .lmConfigPA.nfcid[1] = 0x9C, | ||||||
|  |     .lmConfigPA.nfcid[2] = 0xE7, | ||||||
|  |     .lmConfigPA.nfcid[3] = 0xB1, | ||||||
|  |     .lmConfigPA.nfcid[4] = 0x0A, | ||||||
|  |     .lmConfigPA.nfcid[5] = 0xC1, | ||||||
|  |     .lmConfigPA.nfcid[6] = 0x34, | ||||||
|  |     .lmConfigPA.SENS_RES[0] = 0x44, | ||||||
|  |     .lmConfigPA.SENS_RES[1] = 0x00, | ||||||
|  |     .lmConfigPA.SEL_RES = 0x00, | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | static const rfalNfcDiscoverParam api_hal_nfc_emulate_params_emv = { | ||||||
|  |     .compMode = RFAL_COMPLIANCE_MODE_EMV, | ||||||
|  |     .techs2Find = RFAL_NFC_LISTEN_TECH_A, | ||||||
|  |     .totalDuration = 1000, | ||||||
|  |     .devLimit = 1, | ||||||
|  |     .wakeupEnabled = false, | ||||||
|  |     .wakeupConfigDefault = true, | ||||||
|  |     .nfcfBR = RFAL_BR_212, | ||||||
|  |     .ap2pBR = RFAL_BR_424, | ||||||
|  |     .maxBR = RFAL_BR_KEEP, | ||||||
|  |     .GBLen = RFAL_NFCDEP_GB_MAX_LEN, | ||||||
|  |     .notifyCb = NULL, | ||||||
|  |     .lmConfigPA.nfcidLen = RFAL_LM_NFCID_LEN_04, | ||||||
|  |     .lmConfigPA.nfcid[0] = 0XCF, | ||||||
|  |     .lmConfigPA.nfcid[1] = 0x72, | ||||||
|  |     .lmConfigPA.nfcid[2] = 0xD4, | ||||||
|  |     .lmConfigPA.nfcid[3] = 0x40, | ||||||
|  |     .lmConfigPA.SENS_RES[0] = 0x04, | ||||||
|  |     .lmConfigPA.SENS_RES[1] = 0x00, | ||||||
|  |     .lmConfigPA.SEL_RES = 0x20, | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| ReturnCode api_hal_nfc_init() { | ReturnCode api_hal_nfc_init() { | ||||||
|     // Check if Nfc worker was started
 |     // Check if Nfc worker was started
 | ||||||
| @ -34,19 +80,13 @@ void api_hal_nfc_exit_sleep() { | |||||||
|     rfalLowPowerModeStop(); |     rfalLowPowerModeStop(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void api_hal_nfc_change_state_cb(rfalNfcState st) { | bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t timeout, bool deactivate) { | ||||||
|     FURI_LOG_D("HAL NFC", "NFC worker state: %d", st); |  | ||||||
|     if(st >= RFAL_NFC_STATE_POLL_SELECT) { |  | ||||||
|         dev_is_found = true; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cycles, bool deactivate) { |  | ||||||
|     furi_assert(dev_list); |     furi_assert(dev_list); | ||||||
|     furi_assert(dev_cnt); |     furi_assert(dev_cnt); | ||||||
| 
 | 
 | ||||||
|     rfalLowPowerModeStop(); |     rfalLowPowerModeStop(); | ||||||
|     if(rfalNfcGetState() == RFAL_NFC_STATE_NOTINIT) { |     rfalNfcState state = rfalNfcGetState(); | ||||||
|  |     if(state == RFAL_NFC_STATE_NOTINIT) { | ||||||
|         rfalNfcInitialize(); |         rfalNfcInitialize(); | ||||||
|     } |     } | ||||||
|     rfalNfcDiscoverParam params; |     rfalNfcDiscoverParam params; | ||||||
| @ -61,33 +101,37 @@ bool api_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t cyc | |||||||
|     params.ap2pBR = RFAL_BR_424; |     params.ap2pBR = RFAL_BR_424; | ||||||
|     params.maxBR = RFAL_BR_KEEP; |     params.maxBR = RFAL_BR_KEEP; | ||||||
|     params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; |     params.GBLen = RFAL_NFCDEP_GB_MAX_LEN; | ||||||
|     params.notifyCb = api_hal_nfc_change_state_cb; |     params.notifyCb = NULL; | ||||||
| 
 | 
 | ||||||
|     dev_is_found = false; |     uint32_t start = DWT->CYCCNT; | ||||||
|     rfalNfcDiscover(¶ms); |     rfalNfcDiscover(¶ms); | ||||||
|     while(--cycles) { |     while(state != RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcWorker(); |         rfalNfcWorker(); | ||||||
|         FURI_LOG_D("HAL NFC", "Current state %d", rfalNfcGetState()); |         state = rfalNfcGetState(); | ||||||
|         if(dev_is_found) { |         FURI_LOG_D("HAL NFC", "Current state %d", state); | ||||||
|  |         if(state == RFAL_NFC_STATE_POLL_ACTIVATION) { | ||||||
|  |             start = DWT->CYCCNT; | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  |         if(state == RFAL_NFC_STATE_POLL_SELECT) { | ||||||
|  |             rfalNfcSelect(0); | ||||||
|  |         } | ||||||
|  |         if(DWT->CYCCNT - start > timeout * clocks_in_ms) { | ||||||
|  |             rfalNfcDeactivate(true); | ||||||
|  |             FURI_LOG_D("HAL NFC", "Timeout"); | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |         osThreadYield(); | ||||||
|  |     } | ||||||
|     rfalNfcGetDevicesFound(dev_list, dev_cnt); |     rfalNfcGetDevicesFound(dev_list, dev_cnt); | ||||||
|             FURI_LOG_D("HAL NFC", "Found %d devices", dev_cnt); |  | ||||||
|             break; |  | ||||||
|         } |  | ||||||
|         osDelay(10); |  | ||||||
|     } |  | ||||||
|     if(deactivate) { |     if(deactivate) { | ||||||
|         rfalNfcDeactivate(false); |         rfalNfcDeactivate(false); | ||||||
|         rfalLowPowerModeStart(); |         rfalLowPowerModeStart(); | ||||||
|     } |     } | ||||||
|     if(!cycles) { |  | ||||||
|         FURI_LOG_D("HAL NFC", "Timeout"); |  | ||||||
|         return false; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool api_hal_nfc_listen(uint32_t timeout) { | bool api_hal_nfc_listen(ApiHalNfcEmulateParams params, uint32_t timeout) { | ||||||
|     api_hal_nfc_exit_sleep(); |     api_hal_nfc_exit_sleep(); | ||||||
| 
 | 
 | ||||||
|     rfalNfcState state = rfalNfcGetState(); |     rfalNfcState state = rfalNfcGetState(); | ||||||
| @ -96,30 +140,14 @@ bool api_hal_nfc_listen(uint32_t timeout) { | |||||||
|     } else if(state >= RFAL_NFC_STATE_ACTIVATED) { |     } else if(state >= RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcDeactivate(false); |         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; |     if(params == ApiHalNfcEmulateParamsMifare) { | ||||||
|     params.lmConfigPA.nfcid[0] = 0XCF; |         rfalNfcDiscover(&api_hal_nfc_emulate_params_mifare); | ||||||
|     params.lmConfigPA.nfcid[1] = 0x72; |     } else if(params == ApiHalNfcEmulateParamsEMV) { | ||||||
|     params.lmConfigPA.nfcid[2] = 0xD4; |         rfalNfcDiscover(&api_hal_nfc_emulate_params_emv); | ||||||
|     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; |     uint32_t start = DWT->CYCCNT; | ||||||
|     rfalNfcDiscover(¶ms); |  | ||||||
|     while(state != RFAL_NFC_STATE_ACTIVATED) { |     while(state != RFAL_NFC_STATE_ACTIVATED) { | ||||||
|         rfalNfcWorker(); |         rfalNfcWorker(); | ||||||
|         state = rfalNfcGetState(); |         state = rfalNfcGetState(); | ||||||
| @ -132,9 +160,8 @@ bool api_hal_nfc_listen(uint32_t timeout) { | |||||||
|             start = DWT->CYCCNT; |             start = DWT->CYCCNT; | ||||||
|             continue; |             continue; | ||||||
|         } |         } | ||||||
|         taskYIELD(); |         osThreadYield(); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 gornekich
						gornekich