From 8f08431ddf46a27ddfbb769cff8a36c309720c37 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Sun, 27 May 2012 22:34:21 +0000 Subject: [PATCH] New function nfc_initiator_target_is_present() to test is a previously selected target is in the field. --- NEWS | 2 ++ include/nfc/nfc.h | 1 + libnfc/chips/pn53x.c | 23 +++++++++++++++++++++++ libnfc/chips/pn53x.h | 1 + libnfc/drivers/acr122_pcsc.c | 1 + libnfc/drivers/acr122_usb.c | 1 + libnfc/drivers/acr122s.c | 1 + libnfc/drivers/arygon.c | 1 + libnfc/drivers/pn532_uart.c | 1 + libnfc/drivers/pn53x_usb.c | 1 + libnfc/nfc-internal.h | 1 + libnfc/nfc.c | 16 +++++++++++++++- 12 files changed, 49 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index bba1b79..eb2c135 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,8 @@ API Changes: information - No more in/out function parameter: nfc_initiator_transceive_bytes() now take a constant size for Rx buffer + - New nfc_initiator_target_is_present() to test is the previously selected + target is available in the field New in 1.6.0-rc1: diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 06d77fc..61e3b93 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -86,6 +86,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar); NFC_EXPORT int nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, uint32_t *cycles); NFC_EXPORT int nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles); + NFC_EXPORT int nfc_initiator_target_is_present (nfc_device *pnd, const nfc_target nt); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 6a29417..1a78eac 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -199,6 +199,13 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT if (pbtRx[0] & 0x40) { abort(); } // MI detected CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; break; + case Diagnose: + if (pbtTx[1] == 0x06) { // Diagnose: Card presence detection + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; + } else { + CHIP_DATA(pnd)->last_status_byte = 0; + }; + break; case InDeselect: case InRelease: if (CHIP_DATA(pnd)->type == RCS360) { @@ -1624,6 +1631,22 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) return pn53x_InDeselect (pnd, 0); // 0 mean deselect all selected targets } +int +pn53x_initiator_target_is_present (struct nfc_device *pnd, const nfc_target nt) +{ + // TODO Check if nt is equal to current target + const uint8_t abtCmd[] = { Diagnose, 0x06 }; + uint8_t abtRx[1]; + int res = 0; + + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, sizeof (abtRx), -1)) < 0) + return res; + if (res==1) { + return NFC_SUCCESS; + } + return NFC_ETGRELEASED; +} + #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index c141ebf..c16a01c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -334,6 +334,7 @@ int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, uint32_t *cycles); int pn53x_initiator_deselect_target (struct nfc_device *pnd); +int pn53x_initiator_target_is_present (struct nfc_device *pnd, const nfc_target nt); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout); diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 506a3af..b3dfa33 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -502,6 +502,7 @@ const struct nfc_driver acr122_pcsc_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 98d171b..64b41d2 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -759,6 +759,7 @@ const struct nfc_driver acr122_usb_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index eaa7538..f2e1e23 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -737,6 +737,7 @@ const struct nfc_driver acr122s_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index c4ef881..b8c91ce 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -579,6 +579,7 @@ const struct nfc_driver arygon_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index b011236..934d78e 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -521,6 +521,7 @@ const struct nfc_driver pn532_uart_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 0c8a5bd..b85da1d 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -772,6 +772,7 @@ const struct nfc_driver pn53x_usb_driver = { .initiator_transceive_bits = pn53x_initiator_transceive_bits, .initiator_transceive_bytes_timed = pn53x_initiator_transceive_bytes_timed, .initiator_transceive_bits_timed = pn53x_initiator_transceive_bits_timed, + .initiator_target_is_present = pn53x_initiator_target_is_present, .target_init = pn53x_target_init, .target_send_bytes = pn53x_target_send_bytes, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 375694b..edb454b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -149,6 +149,7 @@ struct nfc_driver { int (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, uint8_t * pbtRxPar); int (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, uint32_t * cycles); int (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, uint8_t * pbtRxPar, uint32_t * cycles); + int (*initiator_target_is_present) (struct nfc_device *pnd, const nfc_target nt); int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, const size_t szRx, int timeout); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index d90ea5c..6f19616 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -687,7 +687,21 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size int nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, uint32_t *cycles) { - HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, cycles) + HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, cycles); +} + +/** @ingroup initiator + * @brief Check target presence + * @return Returns 0 on success, otherwise returns libnfc's error code. + * + * This function tests if \a nfc_target is currently present on NFC device. + * @warning The target have to be selected before check its presence + * @warning To run the test, one or more commands will be sent to target +*/ +int +nfc_initiator_target_is_present (nfc_device *pnd, const nfc_target nt) +{ + HAL (initiator_target_is_present, pnd, nt); } /** @ingroup initiator