|
| 1 | +From 1e5cd05357b11381f5505f345afa4892804986f7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Roberto Jung Drebes <drebes@users.noreply.github.com> |
| 3 | +Date: Wed, 29 May 2024 15:54:42 -0400 |
| 4 | +Subject: [PATCH] initiator_poll_target for PCSC |
| 5 | + |
| 6 | +--- |
| 7 | + libnfc/drivers/pcsc.c | 42 ++++++++++++++++++++++++++++++++++++++---- |
| 8 | + 1 file changed, 38 insertions(+), 4 deletions(-) |
| 9 | + |
| 10 | +diff --git a/libnfc/drivers/pcsc.c b/libnfc/drivers/pcsc.c |
| 11 | +index 106d99ac..0bae4ed8 100644 |
| 12 | +--- a/libnfc/drivers/pcsc.c |
| 13 | ++++ b/libnfc/drivers/pcsc.c |
| 14 | +@@ -176,7 +176,8 @@ static int pcsc_get_status(struct nfc_device *pnd, int *target_present, uint8_t |
| 15 | + |
| 16 | + data->last_error = SCardStatus(data->hCard, NULL, &reader_len, &state, &protocol, atr, &dw_atr_len); |
| 17 | + if (data->last_error != SCARD_S_SUCCESS |
| 18 | +- && data->last_error != SCARD_W_RESET_CARD) { |
| 19 | ++ && data->last_error != SCARD_W_RESET_CARD |
| 20 | ++ && data->last_error != SCARD_W_REMOVED_CARD) { |
| 21 | + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Get status failed"); |
| 22 | + return NFC_EIO; |
| 23 | + } |
| 24 | +@@ -193,7 +194,8 @@ static int pcsc_reconnect(struct nfc_device *pnd, DWORD share_mode, DWORD protoc |
| 25 | + |
| 26 | + data->last_error = SCardReconnect(data->hCard, share_mode, protocol, disposition, &data->ioCard.dwProtocol); |
| 27 | + if (data->last_error != SCARD_S_SUCCESS |
| 28 | +- && data->last_error != SCARD_W_RESET_CARD) { |
| 29 | ++ && data->last_error != SCARD_W_RESET_CARD |
| 30 | ++ && data->last_error != SCARD_E_NO_SMARTCARD) { |
| 31 | + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Reconnect failed"); |
| 32 | + return NFC_EIO; |
| 33 | + } |
| 34 | +@@ -882,6 +884,38 @@ static int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t |
| 35 | + return resp_len; |
| 36 | + } |
| 37 | + |
| 38 | ++static int |
| 39 | ++pcsc_initiator_poll_target(struct nfc_device *pnd, |
| 40 | ++ const nfc_modulation *pnmModulations, const size_t szModulations, |
| 41 | ++ const uint8_t uiPollNr, const uint8_t uiPeriod, |
| 42 | ++ nfc_target *pnt) |
| 43 | ++{ |
| 44 | ++ static int periodFactor = 150000; |
| 45 | ++ int period = uiPeriod * periodFactor; |
| 46 | ++ |
| 47 | ++ if (pnd == NULL) |
| 48 | ++ return 0; |
| 49 | ++ |
| 50 | ++ for (int j = 0; j < uiPollNr; j++) |
| 51 | ++ { |
| 52 | ++ for (unsigned int i = 0; i < szModulations; i++) |
| 53 | ++ { |
| 54 | ++ const nfc_modulation nm = pnmModulations[i]; |
| 55 | ++ |
| 56 | ++ nfc_target nt; |
| 57 | ++ int res = pcsc_initiator_select_passive_target(pnd, nm, 0, 0, &nt); |
| 58 | ++ if (res > 0 && pnt) |
| 59 | ++ { |
| 60 | ++ memcpy(pnt, &nt, sizeof(nfc_target)); |
| 61 | ++ return res; |
| 62 | ++ } |
| 63 | ++ } |
| 64 | ++ usleep(period); |
| 65 | ++ } |
| 66 | ++ |
| 67 | ++ return 0; |
| 68 | ++} |
| 69 | ++ |
| 70 | + static int pcsc_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt) |
| 71 | + { |
| 72 | + uint8_t atr[MAX_ATR_SIZE]; |
| 73 | +@@ -938,7 +972,7 @@ static int pcsc_device_set_property_bool(struct nfc_device *pnd, const nfc_prope |
| 74 | + case NP_ACTIVATE_FIELD: |
| 75 | + if (bEnable == false) { |
| 76 | + struct pcsc_data *data = pnd->driver_data; |
| 77 | +- pcsc_reconnect(pnd, data->dwShareMode, data->ioCard.dwProtocol, SCARD_RESET_CARD); |
| 78 | ++ pcsc_reconnect(pnd, data->dwShareMode, data->ioCard.dwProtocol, SCARD_LEAVE_CARD); |
| 79 | + } |
| 80 | + return NFC_SUCCESS; |
| 81 | + default: |
| 82 | +@@ -1053,7 +1087,7 @@ const struct nfc_driver pcsc_driver = { |
| 83 | + .initiator_init = pcsc_initiator_init, |
| 84 | + .initiator_init_secure_element = NULL, // No secure-element support |
| 85 | + .initiator_select_passive_target = pcsc_initiator_select_passive_target, |
| 86 | +- .initiator_poll_target = NULL, |
| 87 | ++ .initiator_poll_target = pcsc_initiator_poll_target, |
| 88 | + .initiator_select_dep_target = NULL, |
| 89 | + .initiator_deselect_target = NULL, |
| 90 | + .initiator_transceive_bytes = pcsc_initiator_transceive_bytes, |
0 commit comments