From 254053b940403ead5bf79274e45b205fa55dfd7d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 4 Jan 2012 20:02:51 +0000 Subject: [PATCH] fix few minor bugs: - timeout values handling in pn53x_transceive() - check error code after filling it in pn53x_transceive() - fix pn53x_check_communication() return code - lower timeout default values - fix arygon_abort() return code --- libnfc/chips/pn53x.c | 26 ++++++++++++++++---------- libnfc/drivers/arygon.c | 4 ++-- libnfc/drivers/pn532_uart.c | 5 +---- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 2f504e5..ba748da 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -113,10 +113,15 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } PNCMD_TRACE (pbtTx[0]); - if (timeout > 0) + if (timeout > 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %d", timeout); - if (timeout == -1) + } else if (timeout == 0) { + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No timeout"); + } else if (timeout == -1) { timeout = CHIP_DATA (pnd)->timeout_command; + } else { + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid timeout value: %d", timeout); + } uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); @@ -144,9 +149,6 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT return NFC_ECHIP; } - if (CHIP_DATA(pnd)->last_status_byte) - return NFC_ECHIP; - if ((CHIP_DATA(pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically wakeup on external RF field CHIP_DATA(pnd)->power_mode = NORMAL; // When TgInitAsTarget reply that means an external RF have waken up the chip } @@ -194,6 +196,10 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT CHIP_DATA(pnd)->last_status_byte = 0; } log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); + + if (CHIP_DATA(pnd)->last_status_byte) + return NFC_ECHIP; + return ((0 == CHIP_DATA(pnd)->last_status_byte) ? NFC_SUCCESS : NFC_ECHIP); } @@ -880,13 +886,13 @@ pn53x_check_communication (struct nfc_device *pnd) size_t szRx = sizeof (abtRx); int res = 0; - if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 500)) < 0) return res; if (((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))) == 0) - return NFC_ECHIP; + return NFC_SUCCESS; - return NFC_SUCCESS; + return NFC_EIO; } int @@ -2709,8 +2715,8 @@ pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io) CHIP_DATA (pnd)->wb_trigged = false; memset (CHIP_DATA (pnd)->wb_mask, 0x00, PN53X_CACHE_REGISTER_SIZE); - // Set default command timeout (500 ms) - CHIP_DATA (pnd)->timeout_command = 500; + // Set default command timeout (250 ms) + CHIP_DATA (pnd)->timeout_command = 250; // Set default ATR timeout (103 ms) CHIP_DATA (pnd)->timeout_atr = 103; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 011bb57..13f2bff 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -362,7 +362,7 @@ arygon_abort (nfc_device *pnd) uart_send (DRIVER_DATA (pnd)->port, dummy, sizeof (dummy), 0); // Using Arygon device we can't send ACK frame to abort the running command - return (pn53x_check_communication (pnd) == 0) ? 0 : -1; + return pn53x_check_communication (pnd); } int @@ -519,7 +519,7 @@ arygon_reset_tama (nfc_device *pnd) size_t szRx = sizeof(abtRx); int res; - uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), 1000); + uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), 500); // Two reply are possible from ARYGON device: arygon_error_none (ie. in case the byte is well-sent) // or arygon_error_unknown_mode (ie. in case of the first byte was bad-transmitted) diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 4b1049e..ace2e6f 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -115,9 +115,6 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * // Check communication using "Diagnose" command, with "Communication test" (0x00) int res = pn53x_check_communication (pnd); - if(res < 0) { - nfc_perror (pnd, "pn53x_check_communication"); - } pn53x_data_free (pnd); nfc_device_free (pnd); uart_close (sp); @@ -301,7 +298,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i return false; } // According to PN532 application note, C106 appendix: to go out Low Vbat mode and enter in normal mode we need to send a SAMConfiguration command - if (pn53x_SAMConfiguration (pnd, 0x01, 1000) < 0) { + if (pn53x_SAMConfiguration (pnd, 0x01, 500) < 0) { return false; } }